-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
328 lines (279 loc) · 10.4 KB
/
index.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
const Promise = require('bluebird');
const _ = require('lodash');
const Service = require('serviser');
const Restfulness = require('serviser-restfulness');
const jsf = require('json-schema-faker');
const sqlUtils = require('./lib/sqlUtils.js');
const SQL_TYPES = require('./lib/sqlDataTypeMapper.js');
//const FAKER_TOPICS = ['system', 'random', 'phone', 'name', 'lorem', 'internet',
//'image', 'helpers', 'hacker', 'finance', 'date', 'database', 'company',
//'commerce', 'address'];
jsf.extend('faker', () => require('faker'));
jsf.option({
useDefaultValue: false,
ignoreMissingRefs: true,
failOnInvalidTypes: false,
failOnInvalidFormat: false,
alwaysFakeOptional: true
});
const Resource = Restfulness.Resource;
Service.once('set-up', function(appManager) {
appManager.service.on('shell-cmd', registerSeedCommand);
});
/**
* @this {Service}
* @return {undefined}
*/
function registerSeedCommand(yargs) {
const appManager = this.appManager;
const config = this.config;
const knex = this.resourceManager.get('knex');
const app = this.appManager.get(Object.keys(config.get('apps'))[0]);
const ajv = app.getValidator();
const DB_VENDOR = knex.client.constructor.name;
yargs.command('seed', 'populate sql tables with testing fake data', {
exclude: {
alias: '-e',
describe: 'list of resources to exclude',
type: 'string',
default: [],
array: true
},
number: {
alias: '-n',
describe: 'how many records to seed',
type: 'number',
default: 20
},
}, command);
function command(argv) {
const resources = {};
let dbTables;
Resource.registry.forEach(function(resource) {
resources[resource.getTableName()] = resource;
});
if (!SQL_TYPES[DB_VENDOR]) {
throw new Error('Unsupported db vendor');
}
return sqlUtils.listTables(knex).then(function(listOfTablesInDb) {
tableNames = Object.keys(resources);
_.difference(tableNames, listOfTablesInDb).forEach(function(name) {
console.warn(`Skipping resource ${name} as corresponding table does not exist in db.`);
});
dbTables = _.intersection(listOfTablesInDb, tableNames);
dbTables = _.difference(dbTables, argv.exclude);
return Promise.map(dbTables, function(table) {
return sqlUtils.tableInfo(knex, table);
});
}).then(function(data) {
const seederSchemas = [];
data.forEach(function(info, index) {
const resource = resources[dbTables[index]];
const seederSchema = {
resource: resource,
table: resource.getTableName(),
requiredResources: getDependentTables(resource),
foreignKeyNames: getForeignKeyNames(resource),
uniqueKeyNames: [],
schema: {},
data: {}
};
const requiredColumns = {};
info.forEach(function(columnInfo) {
const columnName = columnInfo.name;
if (!columnInfo.nullable) {
if (columnInfo.unique) {
seederSchema.uniqueKeyNames.push(columnName);
}
requiredColumns[columnName] = {};
if (columnInfo.charLength) {//nullable
requiredColumns[columnName].maxLength
= columnInfo.charLength;
}
Object.assign(
requiredColumns[columnName],
SQL_TYPES[DB_VENDOR][columnInfo.type]
);
}
});
const requiredProps = _.union(
Object.keys(requiredColumns),
resource.getRequiredProperties()
);
//append properties which are defined as non nullable in db
//and are not defined as part of Resource property definitions
requiredProps.forEach(function(propName) {
let propSchema;
if (!resource.hasProp(propName)) {
propSchema = requiredColumns[propName];
} else {
propSchema = resource.prop(propName);
}
seederSchema.schema[propName] = dereferenceJsonSchema(ajv, propSchema);
});
if (resource.prop(resource.getKeyName()).type === 'integer') {
delete seederSchema.schema[resource.getKeyName()];
}
if (resource.hasTimestamps()) {
delete seederSchema.schema[resource.CREATED_AT];
delete seederSchema.schema[resource.UPDATED_AT];
}
if (resource.hasTimestamps(resource.DELETED_AT)) {
delete seederSchema.schema[resource.DELETED_AT];
}
seederSchemas.push(seederSchema);
});
sortSeeders(seederSchemas);
return seed(knex, seederSchemas, resources, argv.n);
}).then(function() {
process.stdout.write('OK\n');
process.exit(0);
}).catch(function(err) {
console.error(err);
process.exit(1);
});
};
}
/**
* @param {Array<Object>} seeders
* @param {Object} resources - mapping of table name -> resource
* @param {Integer} seedNumber
* @return {Promise}
*/
function seed(knex, seeders, resources, seedNumber) {
const seededData = {
primaryKeys: {},
columns: {}
};
return Promise.map(Object.keys(resources), function(tableName) {
const resource = resources[tableName];
return resource.query(knex)
.pluck(resource.getKeyName()).then(function(ids) {
seededData.primaryKeys[tableName] = ids;
});
}).then(function() {
return seeders;
}).each(function(seeder) {
const resource = seeder.resource;
return Promise.map(new Array(seedNumber), function() {
return resource.query(knex).insert(
getResourceData(seeder, seededData)
).returning(resource.getKeyName()).then(function(result) {
if (!(seededData.primaryKeys[seeder.table] instanceof Array)) {
seededData.primaryKeys[seeder.table] = [];
}
seededData.primaryKeys[seeder.table].push(result[0]);
})
})
});
}
/**
* @return {Object}
*/
function getResourceData(seeder, seededData) {
const data = {};
const primaryKeys = seededData.primaryKeys;
const columnData = seededData.columns;
Object.keys(seeder.schema).forEach(function iterator(columnName) {
if (seeder.foreignKeyNames.hasOwnProperty(columnName)) {
if (primaryKeys.hasOwnProperty(seeder.foreignKeyNames[columnName])) {
data[columnName] =
pickRandom(primaryKeys[seeder.foreignKeyNames[columnName]]);
}
} else {
data[columnName] = jsf.generate(seeder.schema[columnName]);
}
if (!(columnData[columnName] instanceof Array)) {
columnData[columnName] = [];
}
//make sure columns with unique key have original data
if (seeder.uniqueKeyNames.includes(columnName)
&& columnData[columnName].includes(data[columnName])
) {
return iterator(columnName);
}
//cache inserted data per table column
columnData[columnName].push(data[columnName]);
});
return data;
}
/**
* @param {Array} arr
* @return {mixed}
*/
function pickRandom(arr) {
return arr[_.random(0, arr.length-1, false)];
}
/**
* @param {Array<Object>} tables
* @return tables
*/
function sortSeeders(tables) {
let _found = {};
let _moveJobs = {};
for (var i = 0, len = tables.length; i < len; i++) {
let table = tables[i];
if (_moveJobs.hasOwnProperty(table.table)) {
let movIndex = _moveJobs[table.table];
//place table to new location index
if (movIndex < 0) {
tables.unshift(table);
} else {
tables.splice(movIndex, 0, table);
}
//remove the table from its previous index position
tables.splice((movIndex > i ? i : i+1), 1);
return sortSeeders(tables);
}
for (var y = 0, len2 = table.requiredResources.length; y < len2; y++) {
let requiredTable = table.requiredResources[y];
if (!_found.hasOwnProperty(requiredTable)) {
_moveJobs[requiredTable] = i-1;
continue;
}
}
_found[table.table] = i;
}
return tables;
}
/**
* @param {Resource} resource
* @return {Array<String>}
*/
function getDependentTables(resource) {
return resource.getAssociatedResourceNames().filter(function(pluralName) {
return resource.hasAssociation(pluralName, '1x1');
}).map(function(pluralName) {
return Resource.registry.getByPluralName(pluralName).getTableName();
});
}
/**
* @param {Resource} resource
* @return {Object}
*/
function getForeignKeyNames(resource) {
return resource.getAssociatedResourceNames().filter(function(pluralName) {
return resource.hasAssociation(pluralName, '1x1');
}).reduce(function(out, pluralName, index) {
out[resource.getAssociation(pluralName).localKey] =
Resource.registry.getByPluralName(pluralName).getTableName();
return out;
}, {});
}
/**
* @param {Ajv} ajv - instance validator
* @param {Object} schema - json schema
*/
function dereferenceJsonSchema(ajv, schema) {
if (!schema.hasOwnProperty('$ref')) {
return schema;
}
const _schema = _.omit(schema, ['$ref']);
const validate = ajv.getSchema(schema.$ref);
if (typeof validate !== 'function') {
return _schema;
}
validate.schema = _.cloneDeep(validate.schema);
delete validate.schema['$id'];
return Object.assign({}, validate.schema, _schema);
}