@resolveio/server-lib 4.3.15 → 4.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,476 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (_) try {
17
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ var index_1 = require("../index");
39
+ var mongoose_1 = require("mongoose");
40
+ var common_1 = require("./common");
41
+ function getReportLookupSchemas(collection_root) {
42
+ var lookupTrees = [];
43
+ lookupTrees.push({ collection_name: collection_root, tree: index_1.ResolveIOServer.getMainDB().model(collection_root)['rbSchema'], is_root: true });
44
+ var schemaObj = index_1.ResolveIOServer.getMainDB().model(collection_root)['rbSchema'];
45
+ var modelKeys = Object.keys(schemaObj);
46
+ modelKeys.forEach(function (field) {
47
+ if (schemaObj[field].lookup_collection) {
48
+ var collectionName_1 = schemaObj[field].lookup_collection;
49
+ var collectionModel = index_1.ResolveIOServer.getMainDB().model(collectionName_1);
50
+ if (!lookupTrees.filter(function (a) { return a.collection_name === collectionName_1; }).length) {
51
+ lookupTrees.push({ collection_name: collectionName_1, tree: collectionModel['rbSchema'], is_root: false });
52
+ }
53
+ }
54
+ });
55
+ return lookupTrees;
56
+ }
57
+ exports.getReportLookupSchemas = getReportLookupSchemas;
58
+ function buildRbSchema(rbSchema) {
59
+ Object.keys(rbSchema).forEach(function (key) {
60
+ if (rbSchema[key].type === Object) {
61
+ rbSchema[key].type = 'Object';
62
+ rbSchema[key].rbType = 'Object';
63
+ }
64
+ else if (rbSchema[key].type === Array) {
65
+ rbSchema[key].type = 'Array';
66
+ rbSchema[key].rbType = 'Array';
67
+ }
68
+ else if (rbSchema[key].type === String) {
69
+ rbSchema[key].type = 'String';
70
+ rbSchema[key].rbType = 'String';
71
+ }
72
+ else if (rbSchema[key].type === Date) {
73
+ rbSchema[key].type = 'Date';
74
+ rbSchema[key].rbType = 'Date';
75
+ }
76
+ else if (rbSchema[key].type === Number) {
77
+ rbSchema[key].type = 'Number';
78
+ rbSchema[key].rbType = 'Number';
79
+ }
80
+ else if (rbSchema[key].type === Boolean) {
81
+ rbSchema[key].rbType = 'Boolean';
82
+ rbSchema[key].type = 'Boolean';
83
+ }
84
+ });
85
+ var schema = new mongoose_1.Schema(rbSchema, {
86
+ skipVersioning: true,
87
+ versionKey: false,
88
+ timestamps: false,
89
+ id: false,
90
+ _id: false,
91
+ });
92
+ Object.keys(schema.obj).forEach(function (key) {
93
+ delete schema.obj[key].type;
94
+ });
95
+ return schema.obj;
96
+ }
97
+ exports.buildRbSchema = buildRbSchema;
98
+ /*** ye old regex pairs:
99
+ TEMP, PER FILE:
100
+ (['\s][^'\s]*id_[^'\s]+'?):\s*{
101
+ rbLookups.push({ lookup: 'AAA', local_key: $1 });
102
+ \s*rbLookups.push\s*\((\s*\{\s*lookup\:\s*'[^']*'\s*?\,?\s*)(local_key\:\s*'[^']*')\s*?\,?(\s*\})\)\;
103
+ $1$2,$3,\n
104
+ ***/
105
+ function buildRbLookups(rbLookups, defaultSchema, default_checked_array) {
106
+ // Remember: lkp.local_key will be removed later on, because it has 'id_'
107
+ if (default_checked_array === void 0) { default_checked_array = []; }
108
+ var rbSchema = {};
109
+ rbLookups.forEach(function (lkp) {
110
+ var errString = '';
111
+ var localKey, newPropertyName;
112
+ var localKeyDir = '';
113
+ var localKeyDirDot = '';
114
+ var collectionAlias = common_1.toTitleCase(lkp.lookup.replace(/\_/g, ' ')) + ' (Lookup)';
115
+ if (lkp.local_key.match(/\./)) {
116
+ localKey = lkp.local_key.replace(/\.\$/g, '');
117
+ localKeyDirDot = lkp.local_key.replace(/^(.+\.).*$/, '$1');
118
+ localKeyDir = localKeyDirDot.replace(/\.$/, '');
119
+ }
120
+ else {
121
+ // local_key is a root leaf node.
122
+ localKey = lkp.local_key;
123
+ }
124
+ newPropertyName = localKeyDirDot + collectionAlias;
125
+ if (!defaultSchema.path(lkp.local_key)) {
126
+ errString += '--local_key: \'' + lkp.local_key + '\' MUST be a valid schema path! ';
127
+ }
128
+ else if (!(defaultSchema.path(lkp.local_key) instanceof mongoose_1.SchemaTypes.String)) {
129
+ errString += '--local_key: \'' + lkp.local_key + '\' MUST be a String! ';
130
+ }
131
+ var lookupNode = {
132
+ lookup_collection: lkp.lookup,
133
+ lookup_collection_alias: collectionAlias,
134
+ local_key: localKey,
135
+ field_path: localKeyDir,
136
+ lookup_as: newPropertyName,
137
+ lookup_key: '_id',
138
+ type: 'Object',
139
+ };
140
+ rbSchema[newPropertyName] = lookupNode;
141
+ if (errString) {
142
+ throwErr(errString, lkp, lookupNode, newPropertyName);
143
+ }
144
+ });
145
+ for (var ii = 0; ii < default_checked_array.length; ii++) {
146
+ rbSchema[default_checked_array[ii]] = {};
147
+ rbSchema[default_checked_array[ii]]['default_checked'] = true;
148
+ Object.assign(rbSchema[default_checked_array[ii]], defaultSchema['tree'][default_checked_array[ii]]);
149
+ }
150
+ return rbSchema;
151
+ }
152
+ exports.buildRbLookups = buildRbLookups;
153
+ function throwErr(err, lkp, lookupNode, newPropertyName) {
154
+ if (newPropertyName === void 0) { newPropertyName = ''; }
155
+ // err could be from a try/throw/catch
156
+ // console.log('------- ERROR thrown by rbLookups: ');
157
+ // console.log(err);
158
+ // console.log('---item: ');
159
+ // console.log(lkp);
160
+ // console.log('---Attempted to create rbSchema[\'' + newPropertyName + '\']: ');
161
+ // console.log(lookupNode);
162
+ process.exit(0);
163
+ }
164
+ exports.throwErr = throwErr;
165
+ function buildTree(current_root, schemaTree, lookup) {
166
+ var res = [];
167
+ Object.keys(schemaTree).filter(function (a) { return a !== '_id' && a !== '__v' && !a.startsWith('id_') && a !== 'id' && !a.match(/.*\.id_/); }).forEach(function (key) {
168
+ if (!lookup || (lookup && !schemaTree[key].lookup_collection)) {
169
+ var treeItemSplit = key.split('.');
170
+ if (treeItemSplit.length === 1) {
171
+ if (schemaTree[key].rbType === 'String' || schemaTree[key].rbType === 'Number' || schemaTree[key].rbType === 'Boolean' || schemaTree[key].rbType === 'Date') {
172
+ res.push({
173
+ collection_name: current_root,
174
+ columnName: common_1.toTitleCase(key.replace(/\_/g, ' ')),
175
+ fieldName: key,
176
+ fieldType: schemaTree[key].rbType,
177
+ distinctFieldValues: [],
178
+ fieldPath: lookup ? lookup.lookup_as + '.$.' + key : key,
179
+ lookup_collection: lookup ? lookup.lookup_collection : (schemaTree[key].lookup_collection ? schemaTree[key].lookup_collection : ''),
180
+ lookup_local_key: lookup ? lookup.lookup_local_key : (schemaTree[key].lookup_collection ? schemaTree[key].local_key : ''),
181
+ lookup_foreign_key: lookup ? lookup.lookup_foreign_key : (schemaTree[key].lookup_collection ? schemaTree[key].lookup_key : ''),
182
+ lookup_as: lookup ? lookup.lookup_as : (schemaTree[key].lookup_collection ? schemaTree[key].lookup_as : ''),
183
+ text: common_1.toTitleCase(key.replace(/\_/g, ' ')),
184
+ value: key,
185
+ isLeaf: true,
186
+ isActive: false,
187
+ isSelected: false,
188
+ depth: 0,
189
+ leafType: '',
190
+ leafTypes: getLeafTypes(schemaTree[key], key)
191
+ });
192
+ }
193
+ else {
194
+ res.push({
195
+ collection_name: current_root,
196
+ columnName: common_1.toTitleCase(key.replace(/\_/g, ' ')),
197
+ fieldName: key,
198
+ fieldType: schemaTree[key].lookup_collection ? 'Lookup' : schemaTree[key].rbType,
199
+ distinctFieldValues: [],
200
+ fieldPath: lookup ? lookup.lookup_as + '.$.' + key : key,
201
+ lookup_collection: lookup ? lookup.lookup_collection : (schemaTree[key].lookup_collection ? schemaTree[key].lookup_collection : ''),
202
+ lookup_local_key: lookup ? lookup.lookup_local_key : (schemaTree[key].lookup_collection ? schemaTree[key].local_key : ''),
203
+ lookup_foreign_key: lookup ? lookup.lookup_foreign_key : (schemaTree[key].lookup_collection ? schemaTree[key].lookup_key : ''),
204
+ lookup_as: lookup ? lookup.lookup_as : (schemaTree[key].lookup_collection ? schemaTree[key].lookup_as : ''),
205
+ text: common_1.toTitleCase(key.replace(/\_/g, ' ')),
206
+ value: key,
207
+ isLeaf: false,
208
+ isActive: false,
209
+ isSelected: false,
210
+ depth: 0,
211
+ leafTypeValue: '',
212
+ children: []
213
+ });
214
+ }
215
+ res = res.sort(function (a, b) { return a.fieldName.localeCompare(b.fieldName); });
216
+ }
217
+ else {
218
+ var child_1 = null;
219
+ treeItemSplit.forEach(function (layer, index) {
220
+ if (!child_1) {
221
+ child_1 = res.filter(function (a) { return a.fieldName === layer; })[0];
222
+ }
223
+ else {
224
+ if (child_1.children.filter(function (a) { return a.fieldName === layer; })[0]) {
225
+ child_1 = child_1.children.filter(function (a) { return a.fieldName === layer; })[0];
226
+ }
227
+ else {
228
+ if (schemaTree[key].rbType === 'String' || schemaTree[key].rbType === 'Number' || schemaTree[key].rbType === 'Boolean' || schemaTree[key].rbType === 'Date') {
229
+ child_1.children.push({
230
+ collection_name: current_root,
231
+ columnName: common_1.toTitleCase(key.replace(/\_/g, ' ')),
232
+ fieldName: layer,
233
+ fieldType: schemaTree[key].rbType,
234
+ distinctFieldValues: [],
235
+ fieldPath: lookup ? lookup.lookup_as + '.$.' + key : key,
236
+ lookup_collection: lookup ? lookup.lookup_collection : (schemaTree[key].lookup_collection ? schemaTree[key].lookup_collection : ''),
237
+ lookup_local_key: lookup ? lookup.lookup_local_key : (schemaTree[key].lookup_collection ? schemaTree[key].local_key : ''),
238
+ lookup_foreign_key: lookup ? lookup.lookup_foreign_key : (schemaTree[key].lookup_collection ? schemaTree[key].lookup_key : ''),
239
+ lookup_as: lookup ? lookup.lookup_as : (schemaTree[key].lookup_collection ? schemaTree[key].lookup_as : ''),
240
+ text: common_1.toTitleCase(layer.replace(/\_/g, ' ')),
241
+ value: layer,
242
+ isLeaf: true,
243
+ isActive: false,
244
+ isSelected: false,
245
+ depth: index,
246
+ leafType: '',
247
+ leafTypes: getLeafTypes(schemaTree[key], key)
248
+ });
249
+ }
250
+ else {
251
+ child_1.children.push({
252
+ collection_name: current_root,
253
+ columnName: common_1.toTitleCase(key.replace(/\_/g, ' ')),
254
+ fieldName: layer,
255
+ fieldType: schemaTree[key].lookup_collection ? 'Lookup' : schemaTree[key].rbType,
256
+ distinctFieldValues: [],
257
+ fieldPath: lookup ? lookup.lookup_as + '.$.' + key : key,
258
+ lookup_collection: lookup ? lookup.lookup_collection : (schemaTree[key].lookup_collection ? schemaTree[key].lookup_collection : ''),
259
+ lookup_local_key: lookup ? lookup.lookup_local_key : (schemaTree[key].lookup_collection ? schemaTree[key].local_key : ''),
260
+ lookup_foreign_key: lookup ? lookup.lookup_foreign_key : (schemaTree[key].lookup_collection ? schemaTree[key].lookup_key : ''),
261
+ lookup_as: lookup ? lookup.lookup_as : (schemaTree[key].lookup_collection ? schemaTree[key].lookup_as : ''),
262
+ text: common_1.toTitleCase(layer.replace(/\_/g, ' ')),
263
+ value: layer,
264
+ isLeaf: false,
265
+ isActive: false,
266
+ isSelected: false,
267
+ depth: index,
268
+ leafTypeValue: '',
269
+ children: []
270
+ });
271
+ }
272
+ child_1.children = child_1.children.sort(function (a, b) { return a.fieldName.localeCompare(b.fieldName); });
273
+ }
274
+ }
275
+ });
276
+ }
277
+ }
278
+ });
279
+ return res;
280
+ }
281
+ exports.buildTree = buildTree;
282
+ function getLeafTypes(schemaTree, fieldPath) {
283
+ var leafTypes = [];
284
+ if (schemaTree.rbType === 'String') {
285
+ leafTypes = [
286
+ {
287
+ text: 'As Text',
288
+ value: 'String'
289
+ },
290
+ {
291
+ text: 'As Count',
292
+ value: 'Count'
293
+ },
294
+ {
295
+ text: 'First',
296
+ value: 'First'
297
+ },
298
+ {
299
+ text: 'Last',
300
+ value: 'Last'
301
+ }
302
+ ];
303
+ }
304
+ else if (schemaTree.rbType === 'Number') {
305
+ leafTypes = [
306
+ {
307
+ text: 'As Number',
308
+ value: 'Number'
309
+ },
310
+ {
311
+ text: 'As Currency',
312
+ value: 'Currency'
313
+ },
314
+ {
315
+ text: 'As Text',
316
+ value: 'String'
317
+ },
318
+ {
319
+ text: 'As Average',
320
+ value: 'Average'
321
+ },
322
+ {
323
+ text: 'As Count',
324
+ value: 'Count'
325
+ },
326
+ {
327
+ text: 'As Sum',
328
+ value: 'Sum'
329
+ },
330
+ {
331
+ text: 'Minimum',
332
+ value: 'Minimum'
333
+ },
334
+ {
335
+ text: 'Maximum',
336
+ value: 'Maximum'
337
+ },
338
+ {
339
+ text: 'First',
340
+ value: 'First'
341
+ },
342
+ {
343
+ text: 'Last',
344
+ value: 'Last'
345
+ }
346
+ ];
347
+ }
348
+ else if (schemaTree.rbType === 'Boolean') {
349
+ leafTypes = [
350
+ {
351
+ text: 'As Boolean (True/False)',
352
+ value: 'Boolean'
353
+ },
354
+ {
355
+ text: 'As Text (Yes/No)',
356
+ value: 'String'
357
+ },
358
+ {
359
+ text: 'As Number (1/0)',
360
+ value: 'Number'
361
+ },
362
+ {
363
+ text: 'As Count',
364
+ value: 'Count'
365
+ },
366
+ {
367
+ text: 'First',
368
+ value: 'First'
369
+ },
370
+ {
371
+ text: 'Last',
372
+ value: 'Last'
373
+ }
374
+ ];
375
+ }
376
+ else if (schemaTree.rbType === 'Date') {
377
+ leafTypes = [
378
+ {
379
+ text: 'As Date (Short Format)',
380
+ value: 'Date'
381
+ },
382
+ {
383
+ text: 'As Time (Short Format)',
384
+ value: 'Time'
385
+ },
386
+ {
387
+ text: 'As Date/Time (Short Format)',
388
+ value: 'DateTime'
389
+ },
390
+ {
391
+ text: 'As Date (Long Format)',
392
+ value: 'Date_long'
393
+ },
394
+ {
395
+ text: 'As Date/Time (Long Format)',
396
+ value: 'DateTime_long'
397
+ },
398
+ {
399
+ text: 'As Timestamp (MS since 1970)',
400
+ value: 'Number'
401
+ },
402
+ {
403
+ text: 'As Count',
404
+ value: 'Count'
405
+ },
406
+ {
407
+ text: 'Minimum',
408
+ value: 'Minimum'
409
+ },
410
+ {
411
+ text: 'Maximum',
412
+ value: 'Maximum'
413
+ },
414
+ {
415
+ text: 'First',
416
+ value: 'First'
417
+ },
418
+ {
419
+ text: 'Last',
420
+ value: 'Last'
421
+ }
422
+ ];
423
+ }
424
+ return leafTypes;
425
+ }
426
+ exports.getLeafTypes = getLeafTypes;
427
+ function mongoCommand(commandstr, collection, field) {
428
+ return __awaiter(this, void 0, void 0, function () {
429
+ var tmpObj, obj, obj, err_1;
430
+ return __generator(this, function (_a) {
431
+ switch (_a.label) {
432
+ case 0:
433
+ _a.trys.push([0, 6, , 7]);
434
+ if (!(commandstr === 'count')) return [3 /*break*/, 2];
435
+ obj = {
436
+ count: collection,
437
+ query: {}
438
+ };
439
+ obj.query[field] = { $ne: null };
440
+ return [4 /*yield*/, index_1.ResolveIOServer.getMainDB().db.command(obj)];
441
+ case 1:
442
+ tmpObj = _a.sent();
443
+ return [3 /*break*/, 5];
444
+ case 2:
445
+ if (!(commandstr === 'distinct')) return [3 /*break*/, 4];
446
+ obj = {
447
+ distinct: collection,
448
+ query: {},
449
+ key: field,
450
+ };
451
+ return [4 /*yield*/, index_1.ResolveIOServer.getMainDB().db.command(obj)];
452
+ case 3:
453
+ tmpObj = _a.sent();
454
+ return [3 /*break*/, 5];
455
+ case 4:
456
+ if (commandstr === 'listIndexes') {
457
+ // tmpObj = await this.mainDb.db[collection].getIndexes();
458
+ // let obj = { 'listIndexes': collection };
459
+ // tmpObj = await this.mainDb.db.getIndexes(obj);
460
+ }
461
+ _a.label = 5;
462
+ case 5: return [3 /*break*/, 7];
463
+ case 6:
464
+ err_1 = _a.sent();
465
+ // console.log('----------- mongoCommand err: ', err);
466
+ return [2 /*return*/, -1];
467
+ case 7:
468
+ if (tmpObj) {
469
+ return [2 /*return*/, tmpObj.values];
470
+ }
471
+ return [2 /*return*/];
472
+ }
473
+ });
474
+ });
475
+ }
476
+ exports.mongoCommand = mongoCommand;