@resolveio/server-lib 9.0.45 → 9.1.1

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.
@@ -289,20 +289,23 @@ var Collection = /** @class */ (function () {
289
289
  if (filter === void 0) { filter = {}; }
290
290
  if (bypassLogs === void 0) { bypassLogs = false; }
291
291
  return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
292
- var docs;
293
- var _this = this;
292
+ var docs, i, doc;
294
293
  return __generator(this, function (_a) {
295
294
  switch (_a.label) {
296
295
  case 0:
297
- if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 2];
296
+ if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 6];
298
297
  return [4 /*yield*/, this.find(filter)];
299
298
  case 1:
300
299
  docs = _a.sent();
301
- docs.forEach(function (doc) {
302
- log_collection_1.Logs.insertOne({
300
+ i = 0;
301
+ _a.label = 2;
302
+ case 2:
303
+ if (!(i < docs.length)) return [3 /*break*/, 6];
304
+ doc = docs[i];
305
+ return [4 /*yield*/, log_collection_1.Logs.insertOne({
303
306
  _id: objectIdHexString(),
304
307
  type: 'document',
305
- collection: _this.collectionName,
308
+ collection: this.collectionName,
306
309
  id_document: doc['_id'],
307
310
  payload: common_1.getBinarySize(JSON.stringify([doc, filter, options])) < 200000 ? JSON.stringify([doc, filter, options], null, 2) : 'Too Big',
308
311
  method: 'deleteMany',
@@ -310,14 +313,19 @@ var Collection = /** @class */ (function () {
310
313
  user: '',
311
314
  messageId: 0,
312
315
  route: ''
313
- });
314
- if (_this.useVersions) {
315
- doc['_id'] = { _id: doc['_id'], __v: doc['__v'] };
316
- index_1.ResolveIOServer.getMongoManager().collection(_this.versionCollection).insertOne(doc);
317
- }
318
- });
319
- _a.label = 2;
320
- case 2:
316
+ })];
317
+ case 3:
318
+ _a.sent();
319
+ if (!this.useVersions) return [3 /*break*/, 5];
320
+ doc['_id'] = { _id: doc['_id'], __v: doc['__v'] };
321
+ return [4 /*yield*/, index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).insertOne(doc)];
322
+ case 4:
323
+ _a.sent();
324
+ _a.label = 5;
325
+ case 5:
326
+ i++;
327
+ return [3 /*break*/, 2];
328
+ case 6:
321
329
  index_1.ResolveIOServer.getMainDB().collection(this.collectionName).deleteMany(filter, options).then(function (res) {
322
330
  if (res.result.ok) {
323
331
  resolve(res.deletedCount);
@@ -340,12 +348,12 @@ var Collection = /** @class */ (function () {
340
348
  return __generator(this, function (_a) {
341
349
  switch (_a.label) {
342
350
  case 0:
343
- if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 2];
351
+ if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 4];
344
352
  return [4 /*yield*/, this.findOne(filter)];
345
353
  case 1:
346
354
  doc = _a.sent();
347
- if (doc) {
348
- log_collection_1.Logs.insertOne({
355
+ if (!doc) return [3 /*break*/, 4];
356
+ return [4 /*yield*/, log_collection_1.Logs.insertOne({
349
357
  _id: objectIdHexString(),
350
358
  type: 'document',
351
359
  collection: this.collectionName,
@@ -356,14 +364,16 @@ var Collection = /** @class */ (function () {
356
364
  user: '',
357
365
  messageId: 0,
358
366
  route: ''
359
- });
360
- if (this.useVersions) {
361
- doc['_id'] = { _id: doc['_id'], __v: doc['__v'] };
362
- index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).insertOne(doc);
363
- }
364
- }
365
- _a.label = 2;
367
+ })];
366
368
  case 2:
369
+ _a.sent();
370
+ if (!this.useVersions) return [3 /*break*/, 4];
371
+ doc['_id'] = { _id: doc['_id'], __v: doc['__v'] };
372
+ return [4 /*yield*/, index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).insertOne(doc)];
373
+ case 3:
374
+ _a.sent();
375
+ _a.label = 4;
376
+ case 4:
367
377
  index_1.ResolveIOServer.getMainDB().collection(this.collectionName).deleteOne(filter, options).then(function (res) {
368
378
  if (res.result.ok) {
369
379
  resolve(res.deletedCount);
@@ -393,40 +403,49 @@ var Collection = /** @class */ (function () {
393
403
  var _this = this;
394
404
  if (filter === void 0) { filter = {}; }
395
405
  if (bypassLogs === void 0) { bypassLogs = false; }
396
- return new Promise(function (resolve, reject) {
397
- if (_this.createLogs && !bypassLogs) {
398
- log_collection_1.Logs.insertOne({
399
- _id: objectIdHexString(),
400
- type: 'query',
401
- collection: _this.collectionName,
402
- id_document: '',
403
- payload: common_1.getBinarySize(JSON.stringify([filter, options])) < 200000 ? JSON.stringify([filter, options], null, 2) : 'Too Big',
404
- method: 'find',
405
- id_user: '',
406
- user: '',
407
- messageId: 0,
408
- route: ''
409
- });
410
- }
411
- index_1.ResolveIOServer.getMongoManager().addToQueue({
412
- _id: 0,
413
- name_collection: _this.collectionName,
414
- name_function: 'find',
415
- name_function_addt: 'toArray',
416
- data_function: [filter, options],
417
- data_function_addt: [],
418
- cbs: [function (err, res) {
419
- if (err) {
420
- reject(err);
421
- }
422
- else {
423
- resolve(res);
424
- }
425
- }],
426
- cbs_next: [],
427
- running: false
406
+ return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
407
+ return __generator(this, function (_a) {
408
+ switch (_a.label) {
409
+ case 0:
410
+ if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 2];
411
+ return [4 /*yield*/, log_collection_1.Logs.insertOne({
412
+ _id: objectIdHexString(),
413
+ type: 'query',
414
+ collection: this.collectionName,
415
+ id_document: '',
416
+ payload: common_1.getBinarySize(JSON.stringify([filter, options])) < 200000 ? JSON.stringify([filter, options], null, 2) : 'Too Big',
417
+ method: 'find',
418
+ id_user: '',
419
+ user: '',
420
+ messageId: 0,
421
+ route: ''
422
+ })];
423
+ case 1:
424
+ _a.sent();
425
+ _a.label = 2;
426
+ case 2:
427
+ index_1.ResolveIOServer.getMongoManager().addToQueue({
428
+ _id: 0,
429
+ name_collection: this.collectionName,
430
+ name_function: 'find',
431
+ name_function_addt: 'toArray',
432
+ data_function: [filter, options],
433
+ data_function_addt: [],
434
+ cbs: [function (err, res) {
435
+ if (err) {
436
+ reject(err);
437
+ }
438
+ else {
439
+ resolve(res);
440
+ }
441
+ }],
442
+ cbs_next: [],
443
+ running: false
444
+ });
445
+ return [2 /*return*/];
446
+ }
428
447
  });
429
- });
448
+ }); });
430
449
  };
431
450
  Collection.prototype.findById = function (id, options) {
432
451
  return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).findOne({ _id: id }, options);
@@ -506,12 +525,12 @@ var Collection = /** @class */ (function () {
506
525
  return __generator(this, function (_a) {
507
526
  switch (_a.label) {
508
527
  case 0:
509
- if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 2];
528
+ if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 3];
510
529
  return [4 /*yield*/, this.findOne(filter)];
511
530
  case 1:
512
531
  doc = _a.sent();
513
- if (doc) {
514
- log_collection_1.Logs.insertOne({
532
+ if (!doc) return [3 /*break*/, 3];
533
+ return [4 /*yield*/, log_collection_1.Logs.insertOne({
515
534
  _id: objectIdHexString(),
516
535
  type: 'document',
517
536
  collection: this.collectionName,
@@ -522,10 +541,11 @@ var Collection = /** @class */ (function () {
522
541
  user: '',
523
542
  messageId: 0,
524
543
  route: ''
525
- });
526
- }
527
- _a.label = 2;
544
+ })];
528
545
  case 2:
546
+ _a.sent();
547
+ _a.label = 3;
548
+ case 3:
529
549
  index_1.ResolveIOServer.getMainDB().collection(this.collectionName).findOneAndDelete(filter, options).then(function (res) {
530
550
  if (res.ok) {
531
551
  resolve(res.value);
@@ -554,17 +574,17 @@ var Collection = /** @class */ (function () {
554
574
  console.log(new Date(), this.collectionName, 'Schema Errors - findOneAndReplace', validation.validationErrors());
555
575
  index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal('insertErrorLog', 'Schema Failed on findOneAndReplace - ' + this.collectionName, [validation.validationErrors(), replacement]);
556
576
  reject(validation.validationErrors());
557
- return [3 /*break*/, 4];
577
+ return [3 /*break*/, 5];
558
578
  case 1:
559
579
  if (this.timestamps) {
560
580
  replacement['updatedAt'] = new Date();
561
581
  }
562
- if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 3];
582
+ if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 4];
563
583
  return [4 /*yield*/, this.findOne(filter)];
564
584
  case 2:
565
585
  doc = _a.sent();
566
- if (doc) {
567
- log_collection_1.Logs.insertOne({
586
+ if (!doc) return [3 /*break*/, 4];
587
+ return [4 /*yield*/, log_collection_1.Logs.insertOne({
568
588
  _id: objectIdHexString(),
569
589
  type: 'document',
570
590
  collection: this.collectionName,
@@ -575,10 +595,11 @@ var Collection = /** @class */ (function () {
575
595
  user: '',
576
596
  messageId: 0,
577
597
  route: ''
578
- });
579
- }
580
- _a.label = 3;
598
+ })];
581
599
  case 3:
600
+ _a.sent();
601
+ _a.label = 4;
602
+ case 4:
582
603
  index_1.ResolveIOServer.getMainDB().collection(this.collectionName).findOneAndReplace(filter, replacement, options).then(function (res) {
583
604
  if (res.ok) {
584
605
  resolve(res.value);
@@ -587,8 +608,8 @@ var Collection = /** @class */ (function () {
587
608
  reject(res.lastErrorObject);
588
609
  }
589
610
  }, function (err) { return reject(err); });
590
- _a.label = 4;
591
- case 4: return [2 /*return*/];
611
+ _a.label = 5;
612
+ case 5: return [2 /*return*/];
592
613
  }
593
614
  });
594
615
  }); });
@@ -608,7 +629,7 @@ var Collection = /** @class */ (function () {
608
629
  console.log(new Date(), this.collectionName, 'Schema Errors - findOneAndUpdate', validation.validationErrors());
609
630
  index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal('insertErrorLog', 'Schema Failed on findOneAndUpdate - ' + this.collectionName, [validation.validationErrors(), update]);
610
631
  reject(validation.validationErrors());
611
- return [3 /*break*/, 4];
632
+ return [3 /*break*/, 5];
612
633
  case 1:
613
634
  if (this.timestamps) {
614
635
  if (!update['$set']) {
@@ -618,12 +639,12 @@ var Collection = /** @class */ (function () {
618
639
  update['$set']['updatedAt'] = new Date();
619
640
  }
620
641
  }
621
- if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 3];
642
+ if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 4];
622
643
  return [4 /*yield*/, this.findOne(filter)];
623
644
  case 2:
624
645
  doc = _a.sent();
625
- if (doc) {
626
- log_collection_1.Logs.insertOne({
646
+ if (!doc) return [3 /*break*/, 4];
647
+ return [4 /*yield*/, log_collection_1.Logs.insertOne({
627
648
  _id: objectIdHexString(),
628
649
  type: 'document',
629
650
  collection: this.collectionName,
@@ -634,10 +655,11 @@ var Collection = /** @class */ (function () {
634
655
  user: '',
635
656
  messageId: 0,
636
657
  route: ''
637
- });
638
- }
639
- _a.label = 3;
658
+ })];
640
659
  case 3:
660
+ _a.sent();
661
+ _a.label = 4;
662
+ case 4:
641
663
  index_1.ResolveIOServer.getMainDB().collection(this.collectionName).findOneAndUpdate(filter, update, options).then(function (res) {
642
664
  if (res.ok) {
643
665
  resolve(res.value);
@@ -646,8 +668,8 @@ var Collection = /** @class */ (function () {
646
668
  reject(res.lastErrorObject);
647
669
  }
648
670
  }, function (err) { return reject(err); });
649
- _a.label = 4;
650
- case 4: return [2 /*return*/];
671
+ _a.label = 5;
672
+ case 5: return [2 /*return*/];
651
673
  }
652
674
  });
653
675
  }); });
@@ -661,104 +683,131 @@ var Collection = /** @class */ (function () {
661
683
  Collection.prototype.insertMany = function (docs, options, bypassLogs) {
662
684
  var _this = this;
663
685
  if (bypassLogs === void 0) { bypassLogs = false; }
664
- return new Promise(function (resolve, reject) {
665
- if (!docs.length) {
666
- reject('No Documents');
667
- return;
668
- }
669
- var validationResults = [];
670
- docs.forEach(function (doc) {
671
- var validation = _this.simplschema.newContext();
672
- var isValid = validation.validate(doc);
673
- if (!isValid) {
674
- console.log(new Date(), _this.collectionName, 'Schema Errors - insertMany', validation.validationErrors());
675
- index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal('insertErrorLog', 'Schema Failed on insertMany - ' + _this.collectionName, [validation.validationErrors(), doc]);
676
- validationResults.push(false);
677
- }
678
- else {
679
- validationResults.push(true);
680
- }
681
- });
682
- var validDocs = docs.filter(function (a, idx) { return validationResults[idx]; });
683
- validDocs.forEach(function (doc) {
684
- if (_this.timestamps) {
685
- doc['createdAt'] = new Date();
686
- doc['updatedAt'] = new Date();
687
- }
688
- if (_this.createLogs && !bypassLogs) {
689
- log_collection_1.Logs.insertOne({
690
- _id: objectIdHexString(),
691
- type: 'document',
692
- collection: _this.collectionName,
693
- id_document: doc['_id'],
694
- payload: common_1.getBinarySize(JSON.stringify([doc, options])) < 200000 ? JSON.stringify([doc, options], null, 2) : 'Too Big',
695
- method: 'insertMany',
696
- id_user: '',
697
- user: '',
698
- messageId: 0,
699
- route: ''
700
- });
686
+ return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
687
+ var validationResults, validDocs, i, doc;
688
+ var _this = this;
689
+ return __generator(this, function (_a) {
690
+ switch (_a.label) {
691
+ case 0:
692
+ if (!docs.length) {
693
+ reject('No Documents');
694
+ return [2 /*return*/];
695
+ }
696
+ validationResults = [];
697
+ docs.forEach(function (doc) {
698
+ var validation = _this.simplschema.newContext();
699
+ var isValid = validation.validate(doc);
700
+ if (!isValid) {
701
+ console.log(new Date(), _this.collectionName, 'Schema Errors - insertMany', validation.validationErrors());
702
+ index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal('insertErrorLog', 'Schema Failed on insertMany - ' + _this.collectionName, [validation.validationErrors(), doc]);
703
+ validationResults.push(false);
704
+ }
705
+ else {
706
+ validationResults.push(true);
707
+ }
708
+ });
709
+ validDocs = docs.filter(function (a, idx) { return validationResults[idx]; });
710
+ i = 0;
711
+ _a.label = 1;
712
+ case 1:
713
+ if (!(i < validDocs.length)) return [3 /*break*/, 4];
714
+ doc = validDocs[i];
715
+ if (this.timestamps) {
716
+ doc['createdAt'] = new Date();
717
+ doc['updatedAt'] = new Date();
718
+ }
719
+ if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 3];
720
+ return [4 /*yield*/, log_collection_1.Logs.insertOne({
721
+ _id: objectIdHexString(),
722
+ type: 'document',
723
+ collection: this.collectionName,
724
+ id_document: doc['_id'],
725
+ payload: common_1.getBinarySize(JSON.stringify([doc, options])) < 200000 ? JSON.stringify([doc, options], null, 2) : 'Too Big',
726
+ method: 'insertMany',
727
+ id_user: '',
728
+ user: '',
729
+ messageId: 0,
730
+ route: ''
731
+ })];
732
+ case 2:
733
+ _a.sent();
734
+ _a.label = 3;
735
+ case 3:
736
+ i++;
737
+ return [3 /*break*/, 1];
738
+ case 4:
739
+ if (validDocs.length) {
740
+ index_1.ResolveIOServer.getMainDB().collection(this.collectionName).insertMany(validDocs, options).then(function (res) {
741
+ if (res.result.ok) {
742
+ resolve(validDocs);
743
+ }
744
+ else {
745
+ reject(res.result.ok);
746
+ }
747
+ }, function (err) {
748
+ reject(err);
749
+ });
750
+ }
751
+ return [2 /*return*/];
701
752
  }
702
753
  });
703
- if (validDocs.length) {
704
- index_1.ResolveIOServer.getMainDB().collection(_this.collectionName).insertMany(validDocs, options).then(function (res) {
705
- if (res.result.ok) {
706
- resolve(validDocs);
707
- }
708
- else {
709
- reject(res.result.ok);
710
- }
711
- }, function (err) {
712
- reject(err);
713
- });
714
- }
715
- });
754
+ }); });
716
755
  };
717
756
  Collection.prototype.insertOne = function (doc, options, bypassLogs) {
718
757
  var _this = this;
719
758
  if (bypassLogs === void 0) { bypassLogs = false; }
720
- return new Promise(function (resolve, reject) {
721
- var validation = _this.simplschema.newContext();
722
- var isValid = validation.validate(doc);
723
- if (!isValid) {
724
- console.log(new Date(), _this.collectionName, 'Schema Errors - insertOne', validation.validationErrors());
725
- index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal('insertErrorLog', 'Schema Failed on insertOne - ' + _this.collectionName, [validation.validationErrors(), doc]);
726
- reject(validation.validationErrors());
727
- }
728
- else {
729
- if (_this.timestamps) {
730
- doc['createdAt'] = new Date();
731
- doc['updatedAt'] = new Date();
732
- }
733
- if (_this.createLogs && !bypassLogs) {
734
- log_collection_1.Logs.insertOne({
735
- _id: objectIdHexString(),
736
- type: 'document',
737
- collection: _this.collectionName,
738
- id_document: doc['_id'],
739
- payload: common_1.getBinarySize(JSON.stringify([doc, options])) < 200000 ? JSON.stringify([doc, options], null, 2) : 'Too Big',
740
- method: 'insertOne',
741
- id_user: '',
742
- user: '',
743
- messageId: 0,
744
- route: ''
745
- });
746
- }
747
- if (_this.useVersions && !doc.hasOwnProperty('__v')) {
748
- doc['__v'] = 0;
759
+ return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
760
+ var validation, isValid;
761
+ return __generator(this, function (_a) {
762
+ switch (_a.label) {
763
+ case 0:
764
+ validation = this.simplschema.newContext();
765
+ isValid = validation.validate(doc);
766
+ if (!!isValid) return [3 /*break*/, 1];
767
+ console.log(new Date(), this.collectionName, 'Schema Errors - insertOne', validation.validationErrors());
768
+ index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal('insertErrorLog', 'Schema Failed on insertOne - ' + this.collectionName, [validation.validationErrors(), doc]);
769
+ reject(validation.validationErrors());
770
+ return [3 /*break*/, 4];
771
+ case 1:
772
+ if (this.timestamps) {
773
+ doc['createdAt'] = new Date();
774
+ doc['updatedAt'] = new Date();
775
+ }
776
+ if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 3];
777
+ return [4 /*yield*/, log_collection_1.Logs.insertOne({
778
+ _id: objectIdHexString(),
779
+ type: 'document',
780
+ collection: this.collectionName,
781
+ id_document: doc['_id'],
782
+ payload: common_1.getBinarySize(JSON.stringify([doc, options])) < 200000 ? JSON.stringify([doc, options], null, 2) : 'Too Big',
783
+ method: 'insertOne',
784
+ id_user: '',
785
+ user: '',
786
+ messageId: 0,
787
+ route: ''
788
+ })];
789
+ case 2:
790
+ _a.sent();
791
+ _a.label = 3;
792
+ case 3:
793
+ if (this.useVersions && !doc.hasOwnProperty('__v')) {
794
+ doc['__v'] = 0;
795
+ }
796
+ index_1.ResolveIOServer.getMainDB().collection(this.collectionName).insertOne(doc, options).then(function (res) {
797
+ if (res.result.ok) {
798
+ resolve(doc);
799
+ }
800
+ else {
801
+ reject(res.result.ok);
802
+ }
803
+ }, function (err) {
804
+ reject(err);
805
+ });
806
+ _a.label = 4;
807
+ case 4: return [2 /*return*/];
749
808
  }
750
- index_1.ResolveIOServer.getMainDB().collection(_this.collectionName).insertOne(doc, options).then(function (res) {
751
- if (res.result.ok) {
752
- resolve(doc);
753
- }
754
- else {
755
- reject(res.result.ok);
756
- }
757
- }, function (err) {
758
- reject(err);
759
- });
760
- }
761
- });
809
+ });
810
+ }); });
762
811
  };
763
812
  Collection.prototype.listIndexes = function (options) {
764
813
  return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).listIndexes(options);
@@ -784,7 +833,7 @@ var Collection = /** @class */ (function () {
784
833
  console.log(new Date(), this.collectionName, 'Schema Errors - replaceOne', validation.validationErrors());
785
834
  index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal('insertErrorLog', 'Schema Failed on replaceOne - ' + this.collectionName, [validation.validationErrors(), replacement]);
786
835
  reject(validation.validationErrors());
787
- return [3 /*break*/, 10];
836
+ return [3 /*break*/, 15];
788
837
  case 1:
789
838
  if (this.timestamps) {
790
839
  replacement['updatedAt'] = new Date();
@@ -795,9 +844,9 @@ var Collection = /** @class */ (function () {
795
844
  doc = _a.sent();
796
845
  _a.label = 3;
797
846
  case 3:
798
- if (!doc) return [3 /*break*/, 9];
799
- if (this.createLogs && !bypassLogs) {
800
- log_collection_1.Logs.insertOne({
847
+ if (!doc) return [3 /*break*/, 14];
848
+ if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 5];
849
+ return [4 /*yield*/, log_collection_1.Logs.insertOne({
801
850
  _id: objectIdHexString(),
802
851
  type: 'document',
803
852
  collection: this.collectionName,
@@ -808,20 +857,28 @@ var Collection = /** @class */ (function () {
808
857
  user: '',
809
858
  messageId: 0,
810
859
  route: ''
811
- });
812
- }
813
- if (!this.useVersions) return [3 /*break*/, 7];
860
+ })];
861
+ case 4:
862
+ _a.sent();
863
+ _a.label = 5;
864
+ case 5:
865
+ if (!this.useVersions) return [3 /*break*/, 12];
866
+ if (!(doc['__v'] === replacement['__v'])) return [3 /*break*/, 9];
867
+ replacement['__v'] += 1;
814
868
  versionDoc = common_1.deepCopy(doc);
815
869
  versionDoc['_id'] = { _id: doc['_id'], __v: doc['__v'] };
816
- index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).insertOne(versionDoc);
817
- if (doc['__v'] >= 4) {
818
- index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({ $and: [
870
+ return [4 /*yield*/, index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).insertOne(versionDoc)];
871
+ case 6:
872
+ _a.sent();
873
+ if (!(doc['__v'] >= 4)) return [3 /*break*/, 8];
874
+ return [4 /*yield*/, index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({ $and: [
819
875
  { '_id._id': doc['_id'] },
820
876
  { '_id.__v': { $lte: doc['__v'] - 4 } }
821
- ] });
822
- }
823
- if (!(doc['__v'] === replacement['__v'])) return [3 /*break*/, 4];
824
- replacement['__v'] += 1;
877
+ ] })];
878
+ case 7:
879
+ _a.sent();
880
+ _a.label = 8;
881
+ case 8:
825
882
  index_1.ResolveIOServer.getMainDB().collection(this.collectionName).replaceOne(filter, replacement, options).then(function (res) {
826
883
  if (res.result.ok) {
827
884
  resolve(res.result.nModified);
@@ -832,8 +889,8 @@ var Collection = /** @class */ (function () {
832
889
  }, function (err) {
833
890
  reject(err);
834
891
  });
835
- return [3 /*break*/, 6];
836
- case 4:
892
+ return [3 /*break*/, 11];
893
+ case 9:
837
894
  console.log('invalid version - ' + this.collectionName, doc['__v'], replacement['__v']);
838
895
  return [4 /*yield*/, index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).findOne({
839
896
  $and: [
@@ -841,7 +898,7 @@ var Collection = /** @class */ (function () {
841
898
  { '_id.__v': replacement['__v'] }
842
899
  ]
843
900
  })];
844
- case 5:
901
+ case 10:
845
902
  prevDoc = _a.sent();
846
903
  if (prevDoc) {
847
904
  docId = doc['_id'];
@@ -863,9 +920,9 @@ var Collection = /** @class */ (function () {
863
920
  else {
864
921
  reject('Invalid Version And Could Not Find History - DB: ' + doc['__v'] + ', Trying to update with :' + replacement['__v']);
865
922
  }
866
- _a.label = 6;
867
- case 6: return [3 /*break*/, 8];
868
- case 7:
923
+ _a.label = 11;
924
+ case 11: return [3 /*break*/, 13];
925
+ case 12:
869
926
  index_1.ResolveIOServer.getMainDB().collection(this.collectionName).replaceOne(filter, replacement, options).then(function (res) {
870
927
  if (res.result.ok) {
871
928
  resolve(res.result.nModified);
@@ -876,12 +933,12 @@ var Collection = /** @class */ (function () {
876
933
  }, function (err) {
877
934
  reject(err);
878
935
  });
879
- _a.label = 8;
880
- case 8: return [3 /*break*/, 10];
881
- case 9:
936
+ _a.label = 13;
937
+ case 13: return [3 /*break*/, 15];
938
+ case 14:
882
939
  reject('No Document');
883
- _a.label = 10;
884
- case 10: return [2 /*return*/];
940
+ _a.label = 15;
941
+ case 15: return [2 /*return*/];
885
942
  }
886
943
  });
887
944
  }); });
@@ -893,8 +950,7 @@ var Collection = /** @class */ (function () {
893
950
  var _this = this;
894
951
  if (bypassLogs === void 0) { bypassLogs = false; }
895
952
  return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
896
- var validation, isValid, docs;
897
- var _this = this;
953
+ var validation, isValid, docs, i, doc;
898
954
  return __generator(this, function (_a) {
899
955
  switch (_a.label) {
900
956
  case 0:
@@ -912,17 +968,21 @@ var Collection = /** @class */ (function () {
912
968
  console.log(new Date(), this.collectionName, 'Schema Errors - updateMany', validation.validationErrors());
913
969
  index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal('insertErrorLog', 'Schema Failed on updateMany - ' + this.collectionName, [validation.validationErrors(), update]);
914
970
  reject(validation.validationErrors());
915
- return [3 /*break*/, 4];
971
+ return [3 /*break*/, 7];
916
972
  case 1:
917
- if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 3];
973
+ if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 6];
918
974
  return [4 /*yield*/, this.find(filter)];
919
975
  case 2:
920
976
  docs = _a.sent();
921
- docs.forEach(function (doc) {
922
- log_collection_1.Logs.insertOne({
977
+ i = 0;
978
+ _a.label = 3;
979
+ case 3:
980
+ if (!(i < docs.length)) return [3 /*break*/, 6];
981
+ doc = docs[i];
982
+ return [4 /*yield*/, log_collection_1.Logs.insertOne({
923
983
  _id: objectIdHexString(),
924
984
  type: 'document',
925
- collection: _this.collectionName,
985
+ collection: this.collectionName,
926
986
  id_document: doc['_id'],
927
987
  payload: common_1.getBinarySize(JSON.stringify([doc, filter, update, options])) < 200000 ? JSON.stringify([doc, filter, update, options], null, 2) : 'Too Big',
928
988
  method: 'updateMany',
@@ -930,10 +990,14 @@ var Collection = /** @class */ (function () {
930
990
  user: '',
931
991
  messageId: 0,
932
992
  route: ''
933
- });
934
- });
935
- _a.label = 3;
936
- case 3:
993
+ })];
994
+ case 4:
995
+ _a.sent();
996
+ _a.label = 5;
997
+ case 5:
998
+ i++;
999
+ return [3 /*break*/, 3];
1000
+ case 6:
937
1001
  index_1.ResolveIOServer.getMainDB().collection(this.collectionName).updateMany(filter, update, options).then(function (res) {
938
1002
  if (res.result.ok) {
939
1003
  resolve(res.result.nModified);
@@ -942,8 +1006,8 @@ var Collection = /** @class */ (function () {
942
1006
  reject(res.result.ok);
943
1007
  }
944
1008
  }, function (err) { return reject(err); });
945
- _a.label = 4;
946
- case 4: return [2 /*return*/];
1009
+ _a.label = 7;
1010
+ case 7: return [2 /*return*/];
947
1011
  }
948
1012
  });
949
1013
  }); });
@@ -970,68 +1034,75 @@ var Collection = /** @class */ (function () {
970
1034
  console.log(new Date(), this.collectionName, 'Schema Errors - updateOne', validation.validationErrors());
971
1035
  index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal('insertErrorLog', 'Schema Failed on updateOne - ' + this.collectionName, [validation.validationErrors(), update]);
972
1036
  reject(validation.validationErrors());
973
- return [3 /*break*/, 3];
1037
+ return [3 /*break*/, 11];
974
1038
  case 1: return [4 /*yield*/, this.findOne(filter)];
975
1039
  case 2:
976
1040
  doc = _a.sent();
977
- if (doc) {
978
- if (this.createLogs && !bypassLogs) {
979
- log_collection_1.Logs.insertOne({
980
- _id: objectIdHexString(),
981
- type: 'document',
982
- collection: this.collectionName,
983
- id_document: doc['_id'],
984
- payload: common_1.getBinarySize(JSON.stringify([doc, filter, update, options])) < 200000 ? JSON.stringify([doc, filter, update, options], null, 2) : 'Too Big',
985
- method: 'updateOne',
986
- id_user: '',
987
- user: '',
988
- messageId: 0,
989
- route: ''
990
- });
1041
+ if (!doc) return [3 /*break*/, 10];
1042
+ if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 4];
1043
+ return [4 /*yield*/, log_collection_1.Logs.insertOne({
1044
+ _id: objectIdHexString(),
1045
+ type: 'document',
1046
+ collection: this.collectionName,
1047
+ id_document: doc['_id'],
1048
+ payload: common_1.getBinarySize(JSON.stringify([doc, filter, update, options])) < 200000 ? JSON.stringify([doc, filter, update, options], null, 2) : 'Too Big',
1049
+ method: 'updateOne',
1050
+ id_user: '',
1051
+ user: '',
1052
+ messageId: 0,
1053
+ route: ''
1054
+ })];
1055
+ case 3:
1056
+ _a.sent();
1057
+ _a.label = 4;
1058
+ case 4:
1059
+ if (!this.useVersions) return [3 /*break*/, 8];
1060
+ versionDoc = common_1.deepCopy(doc);
1061
+ versionDoc['_id'] = { _id: doc['_id'], __v: doc['__v'] };
1062
+ return [4 /*yield*/, index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).insertOne(versionDoc)];
1063
+ case 5:
1064
+ _a.sent();
1065
+ if (!(doc['__v'] >= 4)) return [3 /*break*/, 7];
1066
+ return [4 /*yield*/, index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({ $and: [
1067
+ { '_id._id': doc['_id'] },
1068
+ { '_id.__v': { $lte: doc['__v'] - 4 } }
1069
+ ] })];
1070
+ case 6:
1071
+ _a.sent();
1072
+ _a.label = 7;
1073
+ case 7:
1074
+ if (!objectContainsPropertyDeep(update, '__v')) {
1075
+ if (!update.$inc) {
1076
+ update.$inc = { __v: 1 };
991
1077
  }
992
- if (this.useVersions) {
993
- versionDoc = common_1.deepCopy(doc);
994
- versionDoc['_id'] = { _id: doc['_id'], __v: doc['__v'] };
995
- index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).insertOne(versionDoc);
996
- if (doc['__v'] >= 4) {
997
- index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({ $and: [
998
- { '_id._id': doc['_id'] },
999
- { '_id.__v': { $lte: doc['__v'] - 4 } }
1000
- ] });
1001
- }
1002
- if (!objectContainsPropertyDeep(update, '__v')) {
1003
- if (!update.$inc) {
1004
- update.$inc = { __v: 1 };
1005
- }
1006
- else if (!update.$inc.__v) {
1007
- update.$inc.__v = 1;
1008
- }
1009
- }
1010
- index_1.ResolveIOServer.getMainDB().collection(this.collectionName).updateOne(filter, update, options).then(function (res) {
1011
- if (res.result.ok) {
1012
- resolve(res.result.nModified);
1013
- }
1014
- else {
1015
- reject(res.result.ok);
1016
- }
1017
- }, function (err) { return reject(err); });
1078
+ else if (!update.$inc.__v) {
1079
+ update.$inc.__v = 1;
1080
+ }
1081
+ }
1082
+ index_1.ResolveIOServer.getMainDB().collection(this.collectionName).updateOne(filter, update, options).then(function (res) {
1083
+ if (res.result.ok) {
1084
+ resolve(res.result.nModified);
1018
1085
  }
1019
1086
  else {
1020
- index_1.ResolveIOServer.getMainDB().collection(this.collectionName).updateOne(filter, update, options).then(function (res) {
1021
- if (res.result.ok) {
1022
- resolve(res.result.nModified);
1023
- }
1024
- else {
1025
- reject(res.result.ok);
1026
- }
1027
- }, function (err) { return reject(err); });
1087
+ reject(res.result.ok);
1028
1088
  }
1029
- }
1030
- else {
1031
- reject('No Document');
1032
- }
1033
- _a.label = 3;
1034
- case 3: return [2 /*return*/];
1089
+ }, function (err) { return reject(err); });
1090
+ return [3 /*break*/, 9];
1091
+ case 8:
1092
+ index_1.ResolveIOServer.getMainDB().collection(this.collectionName).updateOne(filter, update, options).then(function (res) {
1093
+ if (res.result.ok) {
1094
+ resolve(res.result.nModified);
1095
+ }
1096
+ else {
1097
+ reject(res.result.ok);
1098
+ }
1099
+ }, function (err) { return reject(err); });
1100
+ _a.label = 9;
1101
+ case 9: return [3 /*break*/, 11];
1102
+ case 10:
1103
+ reject('No Document');
1104
+ _a.label = 11;
1105
+ case 11: return [2 /*return*/];
1035
1106
  }
1036
1107
  });
1037
1108
  }); });