-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
executable file
·239 lines (228 loc) · 6.41 KB
/
cli.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env node
const dotenv = require('dotenv');
const Knex = require('knex');
const nconf = require('nconf');
const path = require('path');
const yargs = require('yargs');
const writeModel = require('./write-model');
const pluralize = require('pluralize');
nconf.argv(yargs.options({
base: {
alias: 'b',
describe: 'Path to a file containing a class for the model to exend. The class should be the default export'
},
browser: {
boolean: true,
describe: 'import from typeorm/browser instead of typeorm'
},
client: {
alias: 'c',
describe: 'The database client: mysql or sqlite3'
},
default: {
boolean: true,
describe: 'export the entity class as the default export'
},
filename: {
alias: 'f',
describe: 'Path to a sqlite database file'
},
host: {
alias: 'h',
describe: 'Database host url'
},
port: {
alias: 'p',
describe: 'Port the database is exposed on'
},
user: {
alias: 'u',
describe: 'Database username'
},
password: {
describe: 'Database password'
},
pluralization: {
describe: 'Whether to use plural or singular model names',
boolean: true,
default: false
},
configFile: {
describe: 'Path to json config file'
},
envFile: {
describe: 'Path to env file to be parsed by dotenv'
},
database: {
alias: 'd',
describe: 'Database name'
},
table: {
alias: 't',
describe: 'Table name to generate a model for',
demand: true
},
model: {
alias: 'm',
describe: 'Model class name. Will default to converting snake case table name to PascalCase'
},
big: {
describe: 'Use Big with a tranformer for the type for decimal instead of string',
boolean: true,
default: undefined
},
moment: {
describe: 'Use Moment with a transformer for the type for dates instead of Date',
boolean: true,
default: undefined
},
rules: {
alias: 'r',
describe: 'Output a static rules function that returns basic joi validation rules',
boolean: true,
default: undefined
},
toJSON: {
describe: 'Output a toJSON function that returns the model\'s properties',
boolean: true,
default: undefined
},
out: {
alias: 'o',
describe: 'Path to where model file will be written',
demand: true
},
tabs: {
describe: 'Use tabs instead of spaces',
boolean: true,
default: undefined
},
tabSize: {
describe: 'Number of spaces to indent lines'
},
primaryGeneratedColumn: {
describe: 'Column to decorate with @PrimaryGeneratedColumn()'
},
primaryGeneratedColumnUUID: {
describe: 'Column to decorate with @PrimaryGeneratedColumn(\'uuid\')'
},
primaryColumn: {
describe: 'Column to decorate with @PrimaryColumn()'
},
createDateColumn: {
describe: 'Column to decorate with @CreateDateColumn()'
},
updateDateColumn: {
describe: 'Column to decorate with @UpdateDateColumn()'
},
versionColumn: {
describe: 'Column to decorate with @VersionColumn()'
}
}).help('help'));
if (nconf.get('envFile')) {
dotenv.config({ path: nconf.get('envFile') });
} else {
dotenv.config({ path: path.resolve(process.cwd(), '.env') });
}
nconf.env({
transform: function(obj) {
if (obj.key === 'MYSQL_HOST') {
obj.key = 'host';
} else if (obj.key === 'MYSQL_PORT') {
obj.key = 'port';
} else if (obj.key === 'MYSQL_DATABASE') {
obj.key = 'database';
} else if (obj.key === 'MYSQL_USER') {
obj.key = 'user';
} else if (obj.key === 'MYSQL_PASSWORD') {
obj.key = 'password';
} else if (obj.key.indexOf('TYPEORMGEN_') !== 0) {
return false;
} else {
obj.key = obj.key.slice(11).toLowerCase()
.replace(/\_\w/g, sub => sub[1].toUpperCase());
}
return obj;
}
});
const configFile = nconf.get('configFile');
if (configFile) {
nconf.file(configFile);
} else {
nconf.file({
file: 'typeormgen.json',
dir: process.cwd(),
search: true
});
}
nconf.defaults({
client: 'mysql',
tabSize: 4
});
if (!nconf.get('model')) {
const table = nconf.get('table');
let model = table.toLowerCase()
.replace(/\_\w/g, sub => sub[1].toUpperCase());
model = model[0].toUpperCase() + model.slice(1);
if (!nconf.get('pluralization')) {
model = pluralize(model, 1);
}
nconf.set('model', model);
}
const client = nconf.get('client');
const knex = Knex({
client,
connection: {
filename: nconf.get('filename'),
host: nconf.get('host'),
port: nconf.get('port'),
user: nconf.get('user'),
password: nconf.get('password'),
database: nconf.get('database')
}
});
if (client === 'mysql') {
knex.raw(`DESCRIBE ${nconf.get('table')}`).then(([rows]) => {
const info = {};
rows.forEach(row => {
const openIndex = row.Type.indexOf('(');
info[row.Field] = {
type: openIndex > -1 ? row.Type.slice(0, openIndex - row.Type.length): row.Type,
length: parseInt(row.Type.slice(openIndex + 1, -1), 10),
nullable: row.Null === 'YES'
};
if (info[row.Field].type === 'decimal') {
info[row.Field].length += 1;
}
});
try {
writeModel(info, nconf);
} catch (err) {
console.error(err);
process.exit(1);
}
process.exit();
});
} else {
knex(nconf.get('table')).columnInfo().then(columns => {
const info = {};
Object.keys(columns).forEach(name => {
const column = columns[name];
info[name] = {
type: column.type,
length: parseInt(column.maxLength, 10),
nullable: column.nullable
};
if (info[name].type === 'decimal') {
info[name].length += 1;
}
});
try {
writeModel(info, nconf);
} catch (err) {
console.error(err);
process.exit(1);
}
process.exit();
});
}