Skip to content

Commit 507cb31

Browse files
committed
booleanTransformer, bigTransformer and using transformers in the constructor
1 parent a794f65 commit 507cb31

10 files changed

+171
-36
lines changed

cli.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,13 @@ nconf.argv(yargs.options({
4747
alias: 'm',
4848
describe: 'Model class name. Will default to converting snake case table name to PascalCase'
4949
},
50+
big: {
51+
describe: 'Use Big with a tranformer for the type for decimal instead of string',
52+
boolean: true,
53+
default: undefined
54+
},
5055
moment: {
51-
describe: 'Use Date | Moment for the type for dates instead of just Date',
56+
describe: 'Use Moment with a transformer for the type for dates instead of Date',
5257
boolean: true,
5358
default: undefined
5459
},

constants.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
boolTypes: ['tinyint'],
23
numberTypes: ['int', 'tinyint', 'smallint', 'mediumint', 'bigint', 'float', 'double', 'real'],
34
dateTypes: ['datetime', 'time', 'timestamp', 'date', 'year'],
45
forbiddenExportCols: ['password'],

index.d.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Big } from 'big.js';
2+
import { Moment } from 'moment';
3+
4+
type GenPartial<T> = {
5+
[P in keyof T]?: T[P] extends Moment ? Moment | string :
6+
T[P] extends Date ? Date | string :
7+
T[P] extends boolean ? boolean | 0 | 1 :
8+
T[P] extends Big ? Big | string :
9+
T[P];
10+
}
11+
12+
declare const bigTransformer {
13+
from: (value: string | number) => Big;
14+
to: (value: Big) => string;
15+
}
16+
17+
declare const booleanTransformer {
18+
from: (value: boolean | 0 | 1) => boolean;
19+
to: (value: boolean) => 0 | 1;
20+
}
21+
22+
declare const momentTransformer {
23+
from: (value: Date | string) => Moment;
24+
to: (value: Moment) => string;
25+
}

index.js

+15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ const bigTransformer = {
1616
}
1717
};
1818

19+
const booleanTransformer = {
20+
from: function(value) {
21+
if (typeof value !== 'undefined' && value !== null) {
22+
return value ? true : false;
23+
}
24+
return value;
25+
},
26+
to: function(value) {
27+
if (value) {
28+
return value ? 1 : 0;
29+
}
30+
}
31+
};
32+
1933
const momentTransformer = {
2034
// called on value when fetching from database
2135
from: function(value) {
@@ -35,5 +49,6 @@ const momentTransformer = {
3549

3650
module.exports = {
3751
bigTransformer,
52+
booleanTransformer,
3853
momentTransformer
3954
};

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.8.0",
44
"description": "Generate typeorm models from your database tables",
55
"main": "index.js",
6+
"types": "index.d.ts",
67
"bin": {
78
"typeormgen": "cli.js",
89
"typeg": "cli.js"

write-foot.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const { boolTypes, dateTypes } = require('./constants');
2+
3+
module.exports = function(info, tab, conf) {
4+
let content = `${tab}constructor(props?: GenPartial<${conf.get('model')}>) {
5+
${tab}${tab}super(props);
6+
`;
7+
8+
let transformers = '';
9+
Object.keys(info).forEach(name => {
10+
const col = info[name];
11+
if (boolTypes.indexOf(col.type) > -1 && col.length === 1) {
12+
transformers += `${tab.repeat(3)}this.${name} = booleanTransformer.from(props.${name});
13+
`;
14+
} else if (dateTypes.indexOf(col.type) > -1) {
15+
if (conf.get('moment')) {
16+
transformers += `${tab.repeat(3)}this.${name} = momentTransformer.from(props.${name});
17+
`;
18+
} else {
19+
transformers += `${tab.repeat(3)}if (props.${name}) {
20+
${tab.repeat(3)}this.${name} = new Date(props.${name});
21+
${tab.repeat(2)}}
22+
`;
23+
}
24+
} else if (conf.get('big') && col.type === 'decimal') {
25+
transformers += `${tab.repeat(3)}this.${name} = bigTransformer.from(props.${name});
26+
`;
27+
}
28+
});
29+
30+
if (transformers !== '') {
31+
content += `
32+
${tab.repeat(2)}if (props) {
33+
${transformers}${tab.repeat(2)}}
34+
`;
35+
}
36+
37+
content += `${tab}}
38+
}
39+
`;
40+
41+
return content;
42+
};

write-head.js

+63-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
1-
module.exports = function(table, name, columns, baseInfo, hasDate, rules) {
1+
const { boolTypes, dateTypes } = require('./constants');
2+
3+
module.exports = function(info, columns, baseInfo, conf) {
4+
const hasBool = Object.keys(info).find(col => {
5+
return (boolTypes.indexOf(info[col].type) > -1 && info[col].length === 1);
6+
});
7+
8+
let hasDate = false;
9+
if (conf.get('moment')) {
10+
hasDate = Object.keys(info).find(col => {
11+
return dateTypes.indexOf(info[col].type) > -1;
12+
});
13+
}
14+
15+
let hasDecimal = false;
16+
if (conf.get('big')) {
17+
hasDecimal = Object.keys(info).find(col => {
18+
return info[col].type === 'decimal';
19+
});
20+
}
21+
222
let content = '';
3-
if (rules) {
23+
if (hasDecimal) {
24+
content += `import { Big } from 'big.js'
25+
`;
26+
}
27+
28+
29+
if (conf.get('rules')) {
430
content += `import * as joi from 'joi';
531
`;
632
}
733

8-
if (hasDate) {
34+
if (conf.get('moment') && hasDate) {
935
content += `import { Moment } from 'moment';
1036
`;
1137
}
@@ -31,20 +57,49 @@ module.exports = function(table, name, columns, baseInfo, hasDate, rules) {
3157
`;
3258

3359

60+
content += 'import { GenPartial';
61+
if (hasDecimal || hasBool || hasDate) {
62+
content += ', ';
63+
} else {
64+
content += ' ';
65+
}
66+
67+
if (hasDecimal) {
68+
content += 'bigTransformer';
69+
70+
if (hasBool || hasDate) {
71+
content += ', ';
72+
} else {
73+
content += ' ';
74+
}
75+
}
76+
77+
if (hasBool) {
78+
content += 'booleanTransformer';
79+
80+
if (hasDate) {
81+
content += ', ';
82+
} else {
83+
content += ' ';
84+
}
85+
}
86+
3487
if (hasDate) {
35-
content += `import { momentTransformer } from 'typeormgen';
36-
`;
88+
content += `momentTransformer `;
3789
}
3890

91+
content += `} from 'typeormgen';
92+
`;
93+
3994
if (baseInfo) {
4095
content += `
4196
import ${baseInfo.name} from '${baseInfo.path}';
4297
`;
4398
}
4499

45100
content += `
46-
@Entity('${table}')
47-
export default class ${name} `;
101+
@Entity('${conf.get('table')}')
102+
export default class ${conf.get('model')} `;
48103

49104
if (baseInfo) {
50105
content += `extends ${baseInfo.name} `;
@@ -54,4 +109,4 @@ export default class ${name} `;
54109
`;
55110

56111
return content;
57-
}
112+
};

write-model.js

+13-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const fs = require('fs-extra');
2-
const { numberTypes, dateTypes } = require('./constants');
2+
const { boolTypes, numberTypes, dateTypes } = require('./constants');
33
const readBase = require('./read-base');
4+
const writeFoot = require('./write-foot');
45
const writeHead = require('./write-head');
56
const writeRules = require('./write-rules');
67
const writeTojson = require('./write-tojson');
@@ -11,11 +12,7 @@ function hasColumn(column, info) {
1112

1213
module.exports = function(info, nconf) {
1314
const base = nconf.get('base');
14-
const model = nconf.get('model');
15-
const moment = nconf.get('moment');
1615
const out = nconf.get('out');
17-
const rules = nconf.get('rules');
18-
const table = nconf.get('table');
1916
const tabs = nconf.get('tabs');
2017
const tabSize = nconf.get('tabSize');
2118
const columns = {
@@ -34,14 +31,10 @@ module.exports = function(info, nconf) {
3431
baseInfo = readBase(base, out);
3532
}
3633

37-
let hasDate = false;
38-
if (moment) {
39-
hasDate = Object.keys(info).find(col => dateTypes.indexOf(info[col].type) > -1);
40-
}
41-
let content = writeHead(table, model, columns, baseInfo, hasDate, rules);
34+
let content = writeHead(info, columns, baseInfo, nconf);
4235

4336
Object.keys(info).forEach(col => {
44-
content += writeColumn(col, info[col], tab, columns, moment);
37+
content += writeColumn(col, info[col], tab, columns, nconf);
4538
});
4639

4740
if (nconf.get('rules')) {
@@ -52,24 +45,29 @@ module.exports = function(info, nconf) {
5245
content += writeTojson(info, tab);
5346
}
5447

55-
content += writeFoot(model, tab);
48+
content += writeFoot(info, tab, nconf);
5649

5750
return fs.outputFileSync(out, content);
5851
};
5952

60-
function writeColumn(col, info, tab, columns, moment) {
53+
function writeColumn(col, info, tab, columns, conf) {
6154
let options = `'${info.type}'`;
6255

6356
let type = 'string';
64-
if (numberTypes.indexOf(info.type) > -1) {
57+
if (boolTypes.indexOf(info.type) > -1 && info.length === 1) {
58+
type = 'boolean';
59+
options = `{ type: '${info.type}', transformer: booleanTransformer }`;
60+
} else if (numberTypes.indexOf(info.type) > -1) {
6561
type = 'number';
6662
} else if (dateTypes.indexOf(info.type) > -1) {
67-
if (moment) {
63+
if (conf.get('moment')) {
6864
type = 'Moment';
6965
options = `{ type: '${info.type}', transformer: momentTransformer }`;
7066
} else {
7167
type = 'Date';
7268
}
69+
} else if (conf.get('big') && info.type === 'decimal') {
70+
type = 'Big';
7371
}
7472

7573
let decorator = '@Column';
@@ -94,11 +92,3 @@ ${tab}${col}: ${type};
9492
9593
`;
9694
}
97-
98-
function writeFoot(model, tab) {
99-
return `${tab}constructor(props?: Partial<${model}>) {
100-
${tab}${tab}super(props);
101-
${tab}}
102-
}
103-
`;
104-
}

write-rules.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { numberTypes, dateTypes } = require('./constants');
1+
const { boolTypes, numberTypes, dateTypes } = require('./constants');
22

33
module.exports = function(info, tab) {
44
let content = `${tab}static rules() {
@@ -20,7 +20,9 @@ ${tab}}
2020

2121
function getJoiRule(info) {
2222
let rule;
23-
if (numberTypes.indexOf(info.type) > -1) {
23+
if (boolTypes.indexOf(info.type) > -1 && info.length === 1) {
24+
rule = 'joi.boolean()';
25+
} else if (numberTypes.indexOf(info.type) > -1) {
2426
rule = 'joi.number()';
2527
if (info.type.indexOf('int') > -1) {
2628
rule += '.integer()';

zedconfig.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
"tabSize": 4,
66
"trimWhitespaceOnSave": true,
77
"gotoExclude": [
8-
"/node_modules",
9-
"/node_modules/*"
8+
"/node_modules/**"
109
]
1110
}
1211
}

0 commit comments

Comments
 (0)