-
Notifications
You must be signed in to change notification settings - Fork 1
/
write-model.js
119 lines (101 loc) · 3.62 KB
/
write-model.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const fs = require('fs-extra');
const { boolTypes, numberTypes, dateTypes } = require('./constants');
const readBase = require('./read-base');
const writeAssign = require('./write-assign');
const writeHead = require('./write-head');
const writeRules = require('./write-rules');
const writeTojson = require('./write-tojson');
function hasColumn(column, info) {
return Object.keys(info).find(col => col === column);
}
module.exports = function(info, nconf) {
const base = nconf.get('base');
const out = nconf.get('out');
const tabs = nconf.get('tabs');
const tabSize = nconf.get('tabSize');
const columns = {
primaryGenerated: hasColumn(nconf.get('primaryGeneratedColumn'), info),
primaryGeneratedUUID: hasColumn(nconf.get('primaryGeneratedColumnUUID'), info),
primary: hasColumn(nconf.get('primaryColumn'), info),
createDate: hasColumn(nconf.get('createDateColumn'), info),
updateDate: hasColumn(nconf.get('updateDateColumn'), info),
version: hasColumn(nconf.get('versionColumn'), info)
};
const tab = tabs ? '\t' : ' '.repeat(tabSize);
let baseInfo = null;
if (base) {
baseInfo = readBase(base, out);
}
let content = writeHead(info, columns, baseInfo, nconf);
Object.keys(info).forEach(col => {
content += writeColumn(col, info[col], tab, columns, nconf);
});
if (nconf.get('rules')) {
content += writeRules(info, tab);
}
// Write constructor
content += `${tab}constructor(props?: GenPartial<${nconf.get('model')}>) {
`;
if (nconf.get('base')) {
content += `${tab}${tab}super(props);
`;
} else {
content += `${tab}${tab}this.assign(props);
`;
}
content += `${tab}}
`;
if (nconf.get('toJSON')) {
content += writeTojson(info, tab);
}
content += writeAssign(info, tab, nconf);
content += '}';
return fs.outputFileSync(out, content);
};
function writeColumn(col, info, tab, columns, conf) {
let options = `'${info.type}'`;
let type = 'string';
if (boolTypes.indexOf(info.type) > -1 && info.length === 1) {
type = 'boolean';
options = `{ type: '${info.type}', transformer: booleanTransformer }`;
} else if (numberTypes.indexOf(info.type) > -1) {
type = 'number';
} else if (dateTypes.indexOf(info.type) > -1) {
if (conf.get('moment')) {
type = 'Moment';
options = `{ type: '${info.type}', transformer: momentTransformer }`;
} else {
type = 'Date';
}
} else if (info.type.indexOf('decimal') > -1) {
if (conf.get('big')) {
type = 'Big';
options = `{ type: '${info.type}', transformer: bigTransformer }`;
} else {
type = 'number';
const results = /\((\d+)\,(\d+)\)/.exec(info.type);
if (results) {
options = `{ type: 'decimal', precision: ${results[1]}, scale: ${results[2]} }`;
}
}
}
let decorator = '@Column';
if (columns.primaryGenerated === col) {
decorator = '@PrimaryGeneratedColumn';
options = '';
} else if (columns.primaryGeneratedUUID === col) {
decorator = `@PrimaryGeneratedColumn`;
options = `'uuid'`;
} else if (columns.primary === col) {
decorator = '@PrimaryColumn';
} else if (columns.createDate === col) {
decorator = '@CreateDateColumn';
} else if (columns.updateDate === col) {
decorator = '@UpdateDateColumn';
} else if (columns.version === col) {
decorator = '@VersionColumn';
}
return `${tab}${decorator}(${options})
${tab}${col}: ${type};
`;
}