-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.js
executable file
·484 lines (474 loc) · 20 KB
/
util.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
const jsonSchemaFaker = require('json-schema-faker');
const Pop = require('tree-pop');
const arrays = require('async-arrays');
const access = require('object-accessor');
//const sift = require('sift').default;
//UTIL
const handleListPage = (ob, pageNumber, req, res, urlPath, instances, options = {})=>{
let config = ob.config();
let errorConfig = ob.errorSpec();
handleList(ob, pageNumber, urlPath, instances, options, req, (err, returnValue, set, len, write, meta)=>{
if(err){
res.send(JSON.stringify({
status: 'error',
message: err.message
}));
return;
}
let result = write(returnValue, set, len, meta);
ob.returnContent(res, result, errorConfig, config);
});
};
const handleBatch = (ob, pageNumber, req, res, urlPath, instances, options, callback)=>{
let config = ob.config();
let errorConfig = ob.errorSpec();
//TODO: make default come from datasource
let primaryKey = config.primaryKey || 'id';
let identifier = ob.options.identifier || 'id';
let lookup = makeLookup(ob, primaryKey, identifier);
if(ob.api.actions && ob.api.actions.lookup){
lookup = ob.api.actions.lookup;
}
if(ob.options.actions && ob.options.actions.lookup){
lookup = ob.options.actions.lookup;
}
const populate = new Pop({
identifier,
linkSuffix: '',
expandable: ob.options.expandable,
listSuffix: 'list',
join: config.foreignKeyJoin,
lookup
});
let tpe = getExpansions(options, config);
let result = {};
let shortcircuited = false;
let error = null;
let shortcircuit = (cb, err)=>{
shortcircuited = true;
error = err;
cb();
}
arrays.forEachEmission(options.objects, (object, obIndex, objectFinished)=>{
populate.deconstruct(ob.options.name, object, tpe, res, (err, objects)=>{
let order = populate.orderBatches(objects);
arrays.forEachEmission(order, (type, index, complete)=>{
arrays.forEachEmission(objects[type], (object, index, objectSaved)=>{
if(shortcircuited) return objectSaved();
let rendered = copyJSON(object);
ob.save({ob, identifier, type, item: rendered, req}, (err, saved)=>{
if(!result[type]) result[type] = [];
if(err) return shortcircuit(objectSaved, err);
result[type].push(saved);
Object.keys(object).forEach((key)=>{
if(typeof object[key] === 'function'){
if(saved[key]){
object[key](saved[key]);
}else{
throw new Error('save executed, but no key returned for \''+key+'\'');
}
}
});
objectSaved();
});
}, ()=>{
complete();
});
}, ()=>{
objectFinished();
});
});
}, ()=>{
if(callback === true){
ob.returnContent(res, result, errorConfig, config);
}else{
callback(error, result);
}
});
};
const makeLookup = (ob, primaryKey, identifier)=>{
const lookup = (type, context, req, cb) => {
//let lcs = (new Error()).stack;
let res = ob.api.endpoints.find((item)=>{
return item.options.name === type;
});
if(!res) return cb(new Error(`Type not Found(${type})!`));
if(Array.isArray(context)){
let items = [];
arrays.forEachEmission(context, (seed, index, done)=>{
if(res.instances[seed]){
items[index] = res.instances[seed];
done();
}else{
res.generate(seed, (err, generated)=>{
generated[primaryKey] = seed;
items[index] = generated;
done();
});
}
}, ()=>{
//let keys = Object.keys(res.instances);
cb && cb(null, items);
});
}else{
const criteria = context;
// if the criteria is the lone id
let keys = Object.keys(criteria);
if( //todo: better criteria
keys.length === 1
){
let parts = ob.options.expandable(type, keys[0], criteria[keys[0]]);
if(parts){ //is a foreign key
/*let ep = ob.api.endpoints.find((item)=>{
return item.options.name === parts.type;
});*/
let instanceList = Object.keys(res.instances).map((key)=> res.instances[key]);
let instancesMeetingCriteria = instanceList.filter(ob.api.sift(criteria));
if(instancesMeetingCriteria.length){
cb && cb(null, instancesMeetingCriteria);
}else{
let id;
if(
criteria[keys[0]] &&
criteria[keys[0]]['$in'] &&
criteria[keys[0]]['$in'].length
){
id = criteria[keys[0]]['$in'].shift();
}else{
id = res.nextId(res);
}
res.generate(id, (err, generated)=>{
Object.keys(criteria).forEach((key)=>{
if(key === identifier) return;
generated[key] = criteria[key];
});
let items = [];
res.instances[generated[identifier]] = generated;
items[0] = generated;
cb && cb(null, items);
});
}
return;
}
if(criteria[identifier] && criteria[identifier]['$in']){
return lookup(type, criteria[identifier]['$in'], req, cb, {config, options});
}
}else{
/* ob.api.internal(type, 'list', {body:{
query: criteria,
includeSaved: req.body.includeSaved,
generate: req.body.generate,
saveGenerated: req.body.saveGenerated,
}}, (err, results)=>{
let endpoint = res;
let allResults = results.concat(res.instances);
let matchingResults = allResults.filter(ob.api.sift(criteria));
if(matchingResults.length){
cb && cb(null, matchingResults);
}
res.generate(res.nextId(res), (err, generated)=>{
Object.keys(criteria).forEach((key)=>{
generated[key] = criteria[key];
});
let items = [];
res.instances[generated[identifier]] = generated;
items[0] = generated;
cb && cb(null, items);
});
}); //*/
}
}
};
return lookup;
};
const getExpansions = (options, config)=>{
let tpe = [];
if(
config.foreignKey &&
(options.internal || options.link || options.external)
){
let expansions = [].concat(stringsToStructs(
options.internal || [],
'expand',
'internal'
)).concat(stringsToStructs(
options.link || [],
'expand',
'link'
)).concat(stringsToStructs(
options.external || [],
'expand',
'external'
));
tpe = expansions.map((expansion)=>{
switch(expansion.type){
case 'internal':
return expansion.expand;
case 'link': {
let parts = expansion.expand.split('+');
return parts[0]+parts[1][0].toUpperCase()+
parts[1].substring(1)+':'+parts[0]+
':'+parts[1];
}
case 'external':
return '<'+expansion.expand;
default: throw new Error('Unrecognized type:'+expansion.type);
}
});
}
return tpe;
};
const handleList = (ob, pageNumber, urlPath, instances, options, req, callback)=>{
let config = ob.config();
//let errorConfig = ob.errorSpec();
//TODO: make default come from datasource
let primaryKey = config.primaryKey || 'id';
let identifier = ob.options.identifier || 'id';
let lookup = makeLookup(ob, primaryKey, identifier);
if(ob.api.actions && ob.api.actions.lookup){
lookup = ob.api.actions.lookup;
}
if(ob.options.actions && ob.options.actions.lookup){
lookup = ob.options.actions.lookup;
}
const populate = new Pop({
identifier,
linkSuffix: '',
expandable: ob.options.expandable,
listSuffix: 'list',
join: config.foreignKeyJoin,
lookup
});
let seeds = [];
let pageSize = (options.page && options.page.size) || config.defaultSize || 30;
let resultSpec = ob.resultSpec();
let cleaned = ob.cleanedSchema(resultSpec.returnSpec);
let pageVars = (meta)=>{
let metaPage = (meta && meta.page) || {};
let pageOpts = options.page || {};
let size = metaPage.size || pageOpts.size || config.defaultSize || 30;
let finalPageNumber = metaPage.number || pageNumber;
let total = metaPage.total || seeds.length;
let pageFrom0 = finalPageNumber - 1;
let offset = pageFrom0 * size;
let count = Math.ceil(total/size);
return {size, pageFrom0, offset, count, number: finalPageNumber, total};
};
let writeResults = (results, set, size, meta)=>{
let opts = pageVars(meta);
if(config.total){
access.set(results, config.total, opts.total);
}
if(config.page){
if(config.page.size){
access.set(results, config.page.size, opts.size);
}
if(config.page.count){
access.set(results, config.page.count, opts.count);
}
if(config.page.next && pageNumber < opts.count){
access.set(results, config.page.next, urlPath+'/list/'+(pageNumber+1));
}
if(config.page.previous && pageNumber > 1){
access.set(results, config.page.previous, urlPath+'/list/'+(pageNumber-1));
}
if(config.page.number){
access.set(results, config.page.number, opts.number);
}
}
access.set(results, resultSpec.resultSetLocation, set);
return results;
};
if(ob.api.doNotSeedLists || options.doNotSeedLists){
let items = [];
lookup(ob.options.name, options.query || {}, req, (err, res, meta)=>{
if(err) return callback(err, null, null, writeResults);
if(
config.foreignKey &&
(options.internal || options.link || options.external)
){
//tpe is like: ['userTransaction:user:transaction']
let tpe = getExpansions(options, config);
populate.mutateForest(ob.options.name, res, tpe, req, (err, forest)=>{
if(err) return callback(err, null, null, writeResults);
callback(null, {}, forest, null, writeResults, meta);
});
}else{
items = items.concat(res);
callback(null, {}, items, null, writeResults, meta);
}
}, {config, options});
return;
}
let seed = options.seed|| ob.options.seed || config.seed || '3c38adefd2f5bf4';
let gen = ob.makeGenerator(seed);
let idGen = null;
if(ob.schema.properties[primaryKey].type === 'string'){
idGen = ()=>{
let value = gen.randomString(30);
return value;
};
}
if(
ob.schema.properties[primaryKey].type === 'number' ||
ob.schema.properties[primaryKey].type === 'integer'
){
idGen = ()=>{
let v = gen.randomInt(0, 10000);
return v;
};
}
let length = pageSize * gen.randomInt(1, 2) + gen.randomInt(0, pageSize);
if(options.query && options.expand){ // generate more, so we have more potential results
length = length * options.expand;
}
for(let lcv=0; lcv < length; lcv++){
seeds.push(idGen());
}
if(options.includeSaved){
let ids = Object.keys(instances).map(id => instances[id][primaryKey]);
seeds = seeds.concat(ids);
}
//let items = [];
jsonSchemaFaker.option('random', () => gen.randomInt(0, 1000)/1000);
/*const populate = new Pop({
identifier,
linkSuffix: '',
expandable: ob.options.expandable,
listSuffix: 'list',
join: config.foreignKeyJoin,
lookup
});*/
let fillList = (seeds, options, cb)=>{
let items = [];
lookup(ob.options.name, seeds, req, (err, res)=>{
if(
config.foreignKey &&
(options.internal || options.link || options.external)
){
//tpe is like: ['userTransaction:user:transaction']
let tpe = getExpansions(options, config);
arrays.forEachEmission(res, (item, index, finish)=>{
populate.tree(ob.options.name, item, tpe, res, (err, tree)=>{
items[index] = tree;
finish();
});
}, ()=>{
cb(err, items);
});
}else{
items = items.concat(res);
cb(err, items);
}
}, {config, options});
};
jsonSchemaFaker.resolve(cleaned, [], process.cwd()).then((returnValue)=>{
if(!options.query){ // we are going to do only the work on the page
try{
let opts = pageVars();
seeds = seeds.slice(opts.offset, opts.offset+opts.size);
fillList(seeds, options, (err, filled)=>{
callback(null, returnValue, filled, null, writeResults);
});
}catch(ex){ console.log(ex); }
}else{ // we do all the work: we need to reduce using full values
let opts = pageVars();
fillList(seeds, options, (err, filled)=>{
let set = filled.filter(ob.api.sift(options.query));
let filteredSetLength = set.length;
let len = set.length;
set = set.slice(opts.offset, opts.offset+opts.size);
let returnOptionValue = null;
if(options.generate && (returnOptionValue = parseInt(options.generate))){
let extraReturnSeeds = [];
for(let lcv=0; lcv < returnOptionValue; lcv++){
extraReturnSeeds.push(idGen());
}
fillList(extraReturnSeeds, options, (err, extraFilled)=>{
extraFilled.forEach((item)=>{
try{
Object.keys(options.query).forEach((key)=>{
if(options.query[key]['$eq']){
item[key] = options.query[key]['$eq'];
}
if(options.query[key]['$in'] && Array.isArray(options.query[key]['$in'])){
let index = Math.round(Math.random() * (options.query[key]['$in'].length-1));
item[key] = options.query[key]['$in'][index];
}
if(options.query[key]['$lt'] || options.query[key]['$gt']){
if(Number.isInteger(options.query[key]['$lt'] || options.query[key]['$gt'])){
const lower = options.query[key]['$gt'] !== null?options.query[key]['$gt']:Number.MIN_SAFE_INTEGER;
const upper = options.query[key]['$lt'] || Number.MAX_SAFE_INTEGER;
const diff = upper - lower;
let result = Math.floor(Math.random()*diff)+ lower;
item[key] = result;
}else{
if( typeof (
options.query[key]['$lt'] ||
options.query[key]['$gt']
) === 'string' ){
const lower = new Date(options.query[key]['$gt'] || '01/01/1970 00:00:00 UTC');
const upper = new Date(options.query[key]['$lt']);
const lowerLimit = lower.getTime();
const diff = upper.getTime() - lower.getTime();
let result = Math.floor(Math.random() * diff) + lowerLimit;
let resultDate = new Date();
resultDate.setTime(result);
item[key] = resultDate.toString();
}else{
const lower = options.query[key]['$gt'] || Number.MIN_VALUE;
const upper = options.query[key]['$lt'] || Number.MAX_VALUE;
const diff = upper - lower;
let result = Math.random() * diff + lower;
item[key] = result;
}
}
}
});
if(options.persistGenerated){
ob.instances[item[identifier]] = item;
}
}catch(ex){
console.log('ERROR', ex);
}
set.push(item);
});
const fullSetTotal = filteredSetLength + options.generated;
callback(null, returnValue, set, null, writeResults, {total: fullSetTotal});
//writeResults(returnValue, set);
//returnContent(res, returnValue, errorConfig, config);
});
}else{
const fullSetTotal = seeds.length;
callback(null, returnValue, set, len, writeResults, {total: fullSetTotal});
//writeResults(returnValue, set, len);
//returnContent(res, returnValue, errorConfig, config);
}
});
}
});
};
const copyJSON = (ob)=>{
let copy = JSON.parse(JSON.stringify(ob));
Object.keys(copy).forEach((key)=>{
if(copy[key] === null) delete copy[key];
});
return copy;
};
const stringsToStructs = (strs, field, type)=>{
let map = strs.map((v)=>{
let res = { type };
res[field] = v;
return res;
});
return map;
};
module.exports = {
stringsToStructs,
copyJSON,
handleList,
getExpansions,
makeLookup,
handleBatch,
handleListPage
};