@resolveio/server-lib 4.5.9 → 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, initalLookups, initialProjection, tmpIndex_1, 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
- initalLookups = [];
239
+ initialLookups = [];
240
240
  // Lookup Groups
241
241
  groupsRow.filter(function (a) { return a.treeItem.collection_name !== rootCollectionName && a.treeItem.fieldPath.split('.$').length === 2; }).forEach(function (group) {
242
- if (!initalLookups.filter(function (a) { return a.as === group.treeItem.lookup_as.replace(/\.\$/g, ''); }).length) {
243
- initalLookups.push({
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 (!initalLookups.filter(function (a) { return a.as === field.lookup_as.replace(/\.\$/g, ''); }).length && field.fieldPath.split('.$').length === 2) {
255
- initalLookups.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,
@@ -314,7 +314,6 @@ function loadReportBuilderMethods(methodManager) {
314
314
  };
315
315
  }
316
316
  });
317
- tmpIndex_1 = 0;
318
317
  selectedFields.filter(function (a) { return a.fieldPath.includes('$'); }).forEach(function (field) {
319
318
  if (!initialProjection['rb_arrays_0'].$map.input.$zip.inputs.some(function (a) { return a === '$' + field.fieldPath.split('.$')[0]; })) {
320
319
  initialProjection['rb_arrays_0'].$map.input.$zip.inputs.push('$' + field.fieldPath.split('.$')[0]);
@@ -324,10 +323,24 @@ function loadReportBuilderMethods(methodManager) {
324
323
  }
325
324
  });
326
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
+ });
327
339
  queryGroups = [];
328
340
  queryGroupUnwind = [];
329
341
  initialGroup = null;
330
342
  secondInitialGroup = null;
343
+ secondInitialLookup = [];
331
344
  zipLayers = [];
332
345
  if (groupsRow.length && !groupsRow.some(function (a) { return a.treeItem.fieldPath.includes('$') || a.treeItem.collection_name !== rootCollectionName; })) {
333
346
  initialGroup = { _id: {} };
@@ -366,13 +379,31 @@ function loadReportBuilderMethods(methodManager) {
366
379
  fieldsTotal.forEach(function (total) {
367
380
  selectedFields.filter(function (a) { return a.fieldType === 'Number' && !a.fieldPath.includes('$'); }).forEach(function (field) {
368
381
  var _a;
369
- 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);
370
383
  });
371
384
  });
372
385
  }
373
386
  }
374
387
  else if (groupsRow.length) { //group has array or lookup
375
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
+ });
376
407
  secondInitialGroup = { _id: {} };
377
408
  groupsRow.forEach(function (row) {
378
409
  secondInitialGroup._id[row.id] = '$rb_arrays_0.' + row.field.replace(/\.\$/g, '');
@@ -409,7 +440,7 @@ function loadReportBuilderMethods(methodManager) {
409
440
  fieldsTotal.forEach(function (total) {
410
441
  selectedFields.filter(function (a) { return a.fieldType === 'Number' && !a.fieldPath.includes('$'); }).forEach(function (field) {
411
442
  var _a;
412
- 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);
413
444
  });
414
445
  });
415
446
  }
@@ -483,7 +514,7 @@ function loadReportBuilderMethods(methodManager) {
483
514
  // Lookups
484
515
  var queryLookups = [];
485
516
  // Lookup Groups
486
- groupsRow.filter(function (a) { return a.treeItem.collection_name !== rootCollectionName && a.treeItem.fieldPath.split('.$').length === cnt + 2; }).forEach(function (group) {
517
+ groupsRow.filter(function (a) { return a.treeItem.collection_name !== rootCollectionName && a.treeItem.fieldPath.split('.$').length === cnt + 3; }).forEach(function (group) {
487
518
  if (!queryLookups.filter(function (a) { return a.as === group.treeItem.lookup_as.replace(/\.\$/g, ''); }).length) {
488
519
  queryLookups.push({
489
520
  from: group.treeItem.lookup_collection,
@@ -846,7 +877,7 @@ function loadReportBuilderMethods(methodManager) {
846
877
  if (!queryTotals) {
847
878
  queryTotals = {};
848
879
  }
849
- 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);
850
881
  });
851
882
  });
852
883
  }
@@ -857,13 +888,16 @@ function loadReportBuilderMethods(methodManager) {
857
888
  $match: queryMatchCondition
858
889
  });
859
890
  }
860
- initalLookups.forEach(function (lookup) {
891
+ initialLookups.forEach(function (lookup) {
861
892
  query.push({ $lookup: lookup });
862
893
  });
863
894
  // Group w/ root properties & all arrays needed
864
895
  query.push({
865
896
  $project: initialProjection
866
897
  });
898
+ if (initialTotals) {
899
+ query.push(initialTotals);
900
+ }
867
901
  if (initialGroup) {
868
902
  query.push({
869
903
  $group: initialGroup
@@ -876,6 +910,9 @@ function loadReportBuilderMethods(methodManager) {
876
910
  "preserveNullAndEmptyArrays": true
877
911
  }
878
912
  });
913
+ secondInitialLookup.forEach(function (lookup) {
914
+ query.push(lookup);
915
+ });
879
916
  query.push({
880
917
  $group: secondInitialGroup
881
918
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@resolveio/server-lib",
3
- "version": "4.5.9",
3
+ "version": "4.5.10",
4
4
  "description": "",
5
5
  "main": "index.ts",
6
6
  "scripts": {