@resolveio/server-lib 4.5.8 → 4.5.10

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.
@@ -126,6 +126,82 @@ function loadCollectionMethods(methodManager) {
126
126
  });
127
127
  }
128
128
  },
129
+ // Insert 1 document
130
+ // @Inputs: collection, document
131
+ // @Outputs: res = _id of new document, err = not inserted
132
+ insertManyDocuments: {
133
+ check: new simpl_schema_1.default({
134
+ collection: {
135
+ type: String
136
+ },
137
+ documents: {
138
+ type: Array
139
+ },
140
+ 'documents.$': {
141
+ type: Object,
142
+ blackbox: true
143
+ }
144
+ }),
145
+ function: function (collection, documents) {
146
+ var _this = this;
147
+ return new Promise(function (res, rej) {
148
+ var promises = [];
149
+ var _loop_1 = function (i) {
150
+ var document_1 = documents[i];
151
+ promises.push(new Promise(function (resolve, reject) {
152
+ var schemaErrors = null;
153
+ try {
154
+ index_1.ResolveIOServer.getMainDB().model(collection)['simplschema'].validate(document_1);
155
+ }
156
+ catch (errors) {
157
+ schemaErrors = errors;
158
+ _this.callMethodInternal.call(_this, 'insertErrorLog', 'Schema Failed on insert - ' + collection, [schemaErrors, document_1]);
159
+ }
160
+ if (schemaErrors) {
161
+ console.log(schemaErrors);
162
+ reject('Schema Errors');
163
+ }
164
+ else {
165
+ if (common_1.getBinarySize(JSON.stringify(document_1)) < 200000) {
166
+ log_collection_1.Logs.create({
167
+ _id: mongoose_1.Types.ObjectId().toHexString(),
168
+ date: new Date(),
169
+ type: 'info',
170
+ title: 'Insert Document',
171
+ message: 'User: ' + _this.user + ', Collection Type: ' + collection,
172
+ payload: JSON.stringify(document_1, null, 2),
173
+ method: 'insertDocument',
174
+ user: _this.user,
175
+ messageId: ''
176
+ });
177
+ }
178
+ else {
179
+ log_collection_1.Logs.create({
180
+ _id: mongoose_1.Types.ObjectId().toHexString(),
181
+ date: new Date(),
182
+ type: 'info',
183
+ title: 'Insert Document',
184
+ message: 'User: ' + _this.user + ', Collection Type: ' + collection,
185
+ payload: 'Too Big',
186
+ method: 'insertDocument',
187
+ user: _this.user,
188
+ messageId: ''
189
+ });
190
+ }
191
+ var id_2 = mongoose_1.Types.ObjectId().toHexString();
192
+ document_1['_id'] = id_2;
193
+ document_1['__v'] = 0;
194
+ index_1.ResolveIOServer.getMainDB().model(collection).create(document_1).then(function () { return resolve(id_2); }, function (err) { return reject(err); });
195
+ }
196
+ }));
197
+ };
198
+ for (var i = 0; i < documents.length; i++) {
199
+ _loop_1(i);
200
+ }
201
+ Promise.all(promises).then(function (vals) { return res(vals); });
202
+ });
203
+ }
204
+ },
129
205
  // Replaces 1 document with new document (from _id)
130
206
  // @Inputs: collection, document id
131
207
  // @Outputs: res = 1 (updated 1), res = 0 (not updated), err = not updated/wrong version
@@ -196,7 +196,7 @@ function loadReportBuilderMethods(methodManager) {
196
196
  if (date_field === void 0) { date_field = ''; }
197
197
  if (date_interval === void 0) { date_interval = ''; }
198
198
  return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
199
- var _a, uniqueSelectedFieldCollections, modelCollection, queryMatchCondition, queryLookups, queryGroupUnwindCollectionPaths, queryUnwindCollectionPaths, initialProjection, queryGroups, queryGroupUnwind, initialGroup, secondInitialGroup, zipLayers, initialNonSumFilterProj, initialNonSumElemAtProj, initialNonSumValueProj, cnt, _loop_1, group, correctSort_1, _loop_2, i, queryProjection, queryTotals, query, i, res, err_1;
199
+ var _a, uniqueSelectedFieldCollections, modelCollection, queryMatchCondition, initialLookups, initialProjection, tmpIndex_1, initialTotals, queryGroups, queryGroupUnwind, initialGroup, secondInitialGroup, secondInitialLookup, zipLayers, initialNonSumFilterProj, initialNonSumElemAtProj, initialNonSumValueProj, cnt, _loop_1, group, correctSort_1, _loop_2, i, queryProjection, queryTotals, query, i, res, err_1;
200
200
  return __generator(this, function (_b) {
201
201
  switch (_b.label) {
202
202
  case 0:
@@ -236,11 +236,11 @@ function loadReportBuilderMethods(methodManager) {
236
236
  queryMatchCondition['$and'].push(filter);
237
237
  // queryMatchConditionLookup['$and'].push(filter);
238
238
  });
239
- queryLookups = [];
239
+ initialLookups = [];
240
240
  // Lookup Groups
241
- groupsRow.filter(function (a) { return a.treeItem.collection_name !== rootCollectionName; }).forEach(function (group) {
242
- if (!queryLookups.filter(function (a) { return a.as === group.treeItem.lookup_as.replace(/\.\$/g, ''); }).length) {
243
- queryLookups.push({
241
+ groupsRow.filter(function (a) { return a.treeItem.collection_name !== rootCollectionName && a.treeItem.fieldPath.split('.$').length === 2; }).forEach(function (group) {
242
+ if (!initialLookups.filter(function (a) { return a.as === group.treeItem.lookup_as.replace(/\.\$/g, ''); }).length) {
243
+ initialLookups.push({
244
244
  from: group.treeItem.lookup_collection,
245
245
  localField: group.treeItem.lookup_local_key,
246
246
  foreignField: group.treeItem.lookup_foreign_key,
@@ -251,8 +251,8 @@ function loadReportBuilderMethods(methodManager) {
251
251
  // Lookup Selected Fields
252
252
  uniqueSelectedFieldCollections.filter(function (a) { return a !== rootCollectionName; }).forEach(function (uniqueCollection) {
253
253
  selectedFields.filter(function (a) { return a.collection_name === uniqueCollection; }).forEach(function (field) {
254
- if (!queryLookups.filter(function (a) { return a.as === field.lookup_as.replace(/\.\$/g, ''); }).length) {
255
- queryLookups.push({
254
+ if (!initialLookups.filter(function (a) { return a.as === field.lookup_as.replace(/\.\$/g, ''); }).length && field.fieldPath.split('.$').length === 2) {
255
+ initialLookups.push({
256
256
  from: field.lookup_collection,
257
257
  localField: field.lookup_local_key,
258
258
  foreignField: field.lookup_foreign_key,
@@ -261,37 +261,12 @@ function loadReportBuilderMethods(methodManager) {
261
261
  }
262
262
  });
263
263
  });
264
- queryGroupUnwindCollectionPaths = [];
265
- groupsRow.filter(function (a) { return a.treeItem.fieldPath.includes('$'); }).forEach(function (field) {
266
- var arrayPartData = field.treeItem.fieldPath.split('.$');
267
- var path = '$';
268
- for (var i = 0; i < arrayPartData.length - 1; i++) {
269
- path += arrayPartData[i];
270
- if (!queryGroupUnwindCollectionPaths.filter(function (a) { return a === path; })[0]) {
271
- queryGroupUnwindCollectionPaths.push(path);
272
- }
273
- }
274
- });
275
- queryUnwindCollectionPaths = [];
276
- selectedFields.filter(function (a) { return a.fieldPath.includes('$'); }).forEach(function (field) {
277
- var arrayPartData = field.fieldPath.split('.$');
278
- var path = '$';
279
- for (var i = 0; i < arrayPartData.length - 1; i++) {
280
- path += arrayPartData[i];
281
- if (!queryUnwindCollectionPaths.some(function (a) { return a === path; }) && !queryGroupUnwindCollectionPaths.some(function (a) { return a === path; })) {
282
- queryUnwindCollectionPaths.push(path);
283
- }
284
- }
285
- });
286
264
  initialProjection = { _id: 1, count: { $sum: 1 } };
287
265
  // Projection
288
- groupsRow.filter(function (a) { return !a.treeItem.fieldPath.includes('$'); }).forEach(function (row) {
289
- initialProjection[row.treeItem.fieldPath] = 1;
290
- });
291
- selectedFields.filter(function (a) { return !a.fieldPath.includes('$'); }).forEach(function (field) {
292
- initialProjection[field.fieldPath] = 1;
266
+ groupsRow.forEach(function (row) {
267
+ initialProjection[row.treeItem.fieldPath.split('.$')[0]] = 1;
293
268
  });
294
- selectedFields.filter(function (a) { return a.fieldPath.includes('$'); }).forEach(function (field) {
269
+ selectedFields.forEach(function (field) {
295
270
  initialProjection[field.fieldPath.split('.$')[0]] = 1;
296
271
  });
297
272
  if (selectedFields.some(function (a) { return a.fieldPath.includes('$'); }) || groupsRow.some(function (a) { return a.field.includes('$') || a.field.includes('(Lookup)'); })) {
@@ -330,27 +305,42 @@ function loadReportBuilderMethods(methodManager) {
330
305
  }
331
306
  }
332
307
  };
333
- groupsRow.filter(function (a) { return a.field.includes('$'); }).forEach(function (field, index) {
308
+ tmpIndex_1 = 0;
309
+ groupsRow.filter(function (a) { return a.field.includes('$'); }).forEach(function (field) {
334
310
  if (!initialProjection['rb_arrays_0'].$map.input.$zip.inputs.some(function (a) { return a === '$' + field.field.split('.$')[0]; })) {
335
311
  initialProjection['rb_arrays_0'].$map.input.$zip.inputs.push('$' + field.field.split('.$')[0]);
336
- initialProjection['rb_arrays_0'].$map.in[field.field.split('.$')[0]] = {
337
- $arrayElemAt: ['$$zipped', index]
312
+ initialProjection['rb_arrays_0'].$map.in[field.field.split('.$')[0].replace(new RegExp(/\./g), '_')] = {
313
+ $arrayElemAt: ['$$zipped', tmpIndex_1++]
338
314
  };
339
315
  }
340
316
  });
341
- selectedFields.filter(function (a) { return a.fieldPath.includes('$'); }).forEach(function (field, index) {
317
+ selectedFields.filter(function (a) { return a.fieldPath.includes('$'); }).forEach(function (field) {
342
318
  if (!initialProjection['rb_arrays_0'].$map.input.$zip.inputs.some(function (a) { return a === '$' + field.fieldPath.split('.$')[0]; })) {
343
319
  initialProjection['rb_arrays_0'].$map.input.$zip.inputs.push('$' + field.fieldPath.split('.$')[0]);
344
- initialProjection['rb_arrays_0'].$map.in[field.fieldPath.split('.$')[0]] = {
345
- $arrayElemAt: ['$$zipped', index]
320
+ initialProjection['rb_arrays_0'].$map.in[field.fieldPath.split('.$')[0].replace(new RegExp(/\./g), '_')] = {
321
+ $arrayElemAt: ['$$zipped', tmpIndex_1++]
346
322
  };
347
323
  }
348
324
  });
349
325
  }
326
+ initialTotals = null;
327
+ fieldsTotal.forEach(function (total) {
328
+ selectedFields.filter(function (a) { return a.fieldType === 'Number' && !a.fieldPath.includes('$') && !a.fieldPath.includes('(Lookup)'); }).forEach(function (field) {
329
+ if (!initialTotals) {
330
+ initialTotals = {
331
+ $addFields: {}
332
+ };
333
+ }
334
+ initialTotals.$addFields[total.id + '_' + field.id] = {
335
+ $divide: ['$' + field.fieldPath, { $max: [1, { $size: '$rb_arrays_0' }] }]
336
+ };
337
+ });
338
+ });
350
339
  queryGroups = [];
351
340
  queryGroupUnwind = [];
352
341
  initialGroup = null;
353
342
  secondInitialGroup = null;
343
+ secondInitialLookup = [];
354
344
  zipLayers = [];
355
345
  if (groupsRow.length && !groupsRow.some(function (a) { return a.treeItem.fieldPath.includes('$') || a.treeItem.collection_name !== rootCollectionName; })) {
356
346
  initialGroup = { _id: {} };
@@ -389,13 +379,31 @@ function loadReportBuilderMethods(methodManager) {
389
379
  fieldsTotal.forEach(function (total) {
390
380
  selectedFields.filter(function (a) { return a.fieldType === 'Number' && !a.fieldPath.includes('$'); }).forEach(function (field) {
391
381
  var _a;
392
- initialGroup[total.id + '_' + field.id] = (_a = {}, _a['$' + total.type] = '$' + field.fieldPath.replace(/\.\$/g, ''), _a);
382
+ initialGroup[total.id + '_' + field.id] = (_a = {}, _a['$' + total.type] = '$' + total.id + '_' + field.id, _a);
393
383
  });
394
384
  });
395
385
  }
396
386
  }
397
387
  else if (groupsRow.length) { //group has array or lookup
398
388
  // project next layers
389
+ groupsRow.forEach(function (group) {
390
+ if (groupsRow.some(function (a) { return a.treeItem.collection_name !== rootCollectionName; })) {
391
+ if (!secondInitialLookup.filter(function (a) { return a.as === group.treeItem.lookup_as.replace(/\.\$/g, ''); }).length) {
392
+ secondInitialLookup.push({ $lookup: {
393
+ from: group.treeItem.lookup_collection,
394
+ localField: 'rb_arrays_0.' + group.treeItem.lookup_local_key,
395
+ foreignField: group.treeItem.lookup_foreign_key,
396
+ as: 'rb_arrays_0.' + group.treeItem.lookup_as.replace(/\.\$/g, '')
397
+ } });
398
+ secondInitialLookup.push({
399
+ '$unwind': {
400
+ "path": '$rb_arrays_0.' + group.treeItem.lookup_local_key,
401
+ "preserveNullAndEmptyArrays": true
402
+ }
403
+ });
404
+ }
405
+ }
406
+ });
399
407
  secondInitialGroup = { _id: {} };
400
408
  groupsRow.forEach(function (row) {
401
409
  secondInitialGroup._id[row.id] = '$rb_arrays_0.' + row.field.replace(/\.\$/g, '');
@@ -432,7 +440,7 @@ function loadReportBuilderMethods(methodManager) {
432
440
  fieldsTotal.forEach(function (total) {
433
441
  selectedFields.filter(function (a) { return a.fieldType === 'Number' && !a.fieldPath.includes('$'); }).forEach(function (field) {
434
442
  var _a;
435
- secondInitialGroup[total.id + '_' + field.id] = (_a = {}, _a['$' + total.type] = '$' + field.fieldPath.replace(/\.\$/g, ''), _a);
443
+ secondInitialGroup[total.id + '_' + field.id] = (_a = {}, _a['$' + total.type] = '$' + total.id + '_' + field.id, _a);
436
444
  });
437
445
  });
438
446
  }
@@ -478,7 +486,7 @@ function loadReportBuilderMethods(methodManager) {
478
486
  initialNonSumElemAtProj = { $addFields: {} };
479
487
  initialNonSumValueProj = { $addFields: {} };
480
488
  selectedFields.filter(function (a) { return a.fieldPath.split('.$').length === 2; }).forEach(function (field) {
481
- initialNonSumElemAtProj.$addFields['rb_arrays_0_' + field.id] = '$rb_arrays_0_' + field.id + '.' + field.fieldPath.split('.$')[0];
489
+ initialNonSumElemAtProj.$addFields['rb_arrays_0_' + field.id] = '$rb_arrays_0_' + field.id + '.' + field.fieldPath.split('.$')[0].replace(new RegExp(/\./g), '_');
482
490
  });
483
491
  selectedFields.filter(function (a) { return a.fieldPath.split('.$').length === 2; }).forEach(function (field) {
484
492
  if (field.leafValueType === 'Count') {
@@ -503,6 +511,35 @@ function loadReportBuilderMethods(methodManager) {
503
511
  }
504
512
  cnt = 1;
505
513
  _loop_1 = function () {
514
+ // Lookups
515
+ var queryLookups = [];
516
+ // Lookup Groups
517
+ groupsRow.filter(function (a) { return a.treeItem.collection_name !== rootCollectionName && a.treeItem.fieldPath.split('.$').length === cnt + 3; }).forEach(function (group) {
518
+ if (!queryLookups.filter(function (a) { return a.as === group.treeItem.lookup_as.replace(/\.\$/g, ''); }).length) {
519
+ queryLookups.push({
520
+ from: group.treeItem.lookup_collection,
521
+ localField: 'rb_arrays_' + (cnt - 1) + '.' + group.treeItem.lookup_local_key,
522
+ foreignField: group.treeItem.lookup_foreign_key,
523
+ as: 'rb_arrays_' + (cnt - 1) + '.' + group.treeItem.lookup_as.replace(/\.\$/g, '')
524
+ });
525
+ }
526
+ });
527
+ // Lookup Selected Fields
528
+ uniqueSelectedFieldCollections.filter(function (a) { return a !== rootCollectionName; }).forEach(function (uniqueCollection) {
529
+ selectedFields.filter(function (a) { return a.collection_name === uniqueCollection; }).forEach(function (field) {
530
+ if (!queryLookups.filter(function (a) { return a.as === field.lookup_as.replace(/\.\$/g, ''); }).length && field.fieldPath.split('.$').length === cnt + 2) {
531
+ queryLookups.push({
532
+ from: field.lookup_collection,
533
+ localField: 'rb_arrays_' + (cnt - 1) + '.' + field.lookup_local_key,
534
+ foreignField: field.lookup_foreign_key,
535
+ as: 'rb_arrays_' + (cnt - 1) + '.' + field.lookup_as.replace(/\.\$/g, '')
536
+ });
537
+ }
538
+ });
539
+ });
540
+ queryLookups.forEach(function (lookup) {
541
+ zipLayers.push({ $lookup: lookup });
542
+ });
506
543
  var proj = { $addFields: {} };
507
544
  if (selectedFields.some(function (a) { return a.fieldPath.split('.$').length === cnt + 2; })) {
508
545
  proj.$addFields['rb_arrays_' + cnt] = {
@@ -517,7 +554,8 @@ function loadReportBuilderMethods(methodManager) {
517
554
  in: {}
518
555
  }
519
556
  };
520
- selectedFields.filter(function (a) { return a.fieldPath.split('.$').length === cnt + 2; }).forEach(function (field, index) {
557
+ var indexCnt_1 = 0;
558
+ selectedFields.filter(function (a) { return a.fieldPath.split('.$').length === cnt + 2; }).forEach(function (field) {
521
559
  var fieldPath = field.fieldPath.split('.$')[0];
522
560
  for (var i = 1; i < cnt + 1; i++) {
523
561
  fieldPath += field.fieldPath.split('.$')[i];
@@ -525,8 +563,8 @@ function loadReportBuilderMethods(methodManager) {
525
563
  if (!proj.$addFields['rb_arrays_' + cnt].$map.input.$zip.inputs.some(function (a) { return a === '$' + fieldPath; })) {
526
564
  if (!proj.$addFields['rb_arrays_' + cnt].$map.input.$zip.inputs.some(function (a) { return a === '$rb_arrays_' + (cnt - 1) + '.' + fieldPath; })) {
527
565
  proj.$addFields['rb_arrays_' + cnt].$map.input.$zip.inputs.push('$rb_arrays_' + (cnt - 1) + '.' + fieldPath);
528
- proj.$addFields['rb_arrays_' + cnt].$map.in[field.fieldPath.split('.$.')[cnt]] = {
529
- $arrayElemAt: ['$$zipped', index]
566
+ proj.$addFields['rb_arrays_' + cnt].$map.in[field.fieldPath.split('.$.')[cnt].replace(new RegExp(/\./g), '_')] = {
567
+ $arrayElemAt: ['$$zipped', indexCnt_1++]
530
568
  };
531
569
  }
532
570
  }
@@ -839,7 +877,7 @@ function loadReportBuilderMethods(methodManager) {
839
877
  if (!queryTotals) {
840
878
  queryTotals = {};
841
879
  }
842
- queryTotals[total.id + '_' + field.id] = (_a = {}, _a['$' + total.type] = '$results.' + field.id, _a);
880
+ queryTotals[total.id + '_' + field.id] = (_a = {}, _a['$' + total.type] = '$results.' + total.id + '_' + field.id, _a);
843
881
  });
844
882
  });
845
883
  }
@@ -850,10 +888,16 @@ function loadReportBuilderMethods(methodManager) {
850
888
  $match: queryMatchCondition
851
889
  });
852
890
  }
891
+ initialLookups.forEach(function (lookup) {
892
+ query.push({ $lookup: lookup });
893
+ });
853
894
  // Group w/ root properties & all arrays needed
854
895
  query.push({
855
896
  $project: initialProjection
856
897
  });
898
+ if (initialTotals) {
899
+ query.push(initialTotals);
900
+ }
857
901
  if (initialGroup) {
858
902
  query.push({
859
903
  $group: initialGroup
@@ -866,6 +910,9 @@ function loadReportBuilderMethods(methodManager) {
866
910
  "preserveNullAndEmptyArrays": true
867
911
  }
868
912
  });
913
+ secondInitialLookup.forEach(function (lookup) {
914
+ query.push(lookup);
915
+ });
869
916
  query.push({
870
917
  $group: secondInitialGroup
871
918
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@resolveio/server-lib",
3
- "version": "4.5.8",
3
+ "version": "4.5.10",
4
4
  "description": "",
5
5
  "main": "index.ts",
6
6
  "scripts": {