@resolveio/server-lib 9.0.33 → 9.0.35

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.
@@ -170,6 +170,7 @@ var Model = /** @class */ (function () {
170
170
  this.collection_main = new Collection(collectionName, schema, useReportBuilder, reportBuilderLookupTables, timestamps, createLogs);
171
171
  }
172
172
  if (useVersionCollection) {
173
+ this.collection_main.useVersions = true;
173
174
  var versionSchema = common_1.deepCopy(schema);
174
175
  versionSchema['_id'].type = 'Object';
175
176
  versionSchema['_id'].blackbox = true;
@@ -193,7 +194,7 @@ var Collection = /** @class */ (function () {
193
194
  this.simplschema = null;
194
195
  this.rbSchema = null;
195
196
  this.timestamps = false;
196
- this.versionKey = '';
197
+ this.useVersions = false;
197
198
  this.versionCollection = '';
198
199
  this.createLogs = true;
199
200
  this.useRB = false;
@@ -283,21 +284,22 @@ var Collection = /** @class */ (function () {
283
284
  Collection.prototype.createIndexes = function (indexSpecs, options) {
284
285
  return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).createIndexes(indexSpecs, options);
285
286
  };
286
- Collection.prototype.deleteMany = function (filter, options) {
287
+ Collection.prototype.deleteMany = function (filter, options, bypassLogs) {
287
288
  var _this = this;
288
289
  if (filter === void 0) { filter = {}; }
290
+ if (bypassLogs === void 0) { bypassLogs = false; }
289
291
  return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
290
292
  var docs;
291
293
  var _this = this;
292
294
  return __generator(this, function (_a) {
293
295
  switch (_a.label) {
294
296
  case 0:
295
- if (!this.createLogs) return [3 /*break*/, 2];
297
+ if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 2];
296
298
  return [4 /*yield*/, this.find(filter)];
297
299
  case 1:
298
300
  docs = _a.sent();
299
301
  docs.forEach(function (doc) {
300
- log_collection_1.Logs.create({
302
+ log_collection_1.Logs.insertOne({
301
303
  _id: objectIdHexString(),
302
304
  type: 'document',
303
305
  collection: _this.collectionName,
@@ -309,6 +311,10 @@ var Collection = /** @class */ (function () {
309
311
  messageId: '',
310
312
  route: ''
311
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
+ }
312
318
  });
313
319
  _a.label = 2;
314
320
  case 2:
@@ -325,20 +331,21 @@ var Collection = /** @class */ (function () {
325
331
  });
326
332
  }); });
327
333
  };
328
- Collection.prototype.deleteOne = function (filter, options) {
334
+ Collection.prototype.deleteOne = function (filter, options, bypassLogs) {
329
335
  var _this = this;
330
336
  if (filter === void 0) { filter = {}; }
337
+ if (bypassLogs === void 0) { bypassLogs = false; }
331
338
  return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
332
339
  var doc;
333
340
  return __generator(this, function (_a) {
334
341
  switch (_a.label) {
335
342
  case 0:
336
- if (!this.createLogs) return [3 /*break*/, 2];
343
+ if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 2];
337
344
  return [4 /*yield*/, this.findOne(filter)];
338
345
  case 1:
339
346
  doc = _a.sent();
340
347
  if (doc) {
341
- log_collection_1.Logs.create({
348
+ log_collection_1.Logs.insertOne({
342
349
  _id: objectIdHexString(),
343
350
  type: 'document',
344
351
  collection: this.collectionName,
@@ -350,6 +357,10 @@ var Collection = /** @class */ (function () {
350
357
  messageId: '',
351
358
  route: ''
352
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
+ }
353
364
  }
354
365
  _a.label = 2;
355
366
  case 2:
@@ -378,12 +389,13 @@ var Collection = /** @class */ (function () {
378
389
  Collection.prototype.dropIndexes = function (options) {
379
390
  return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).dropIndexes(options);
380
391
  };
381
- Collection.prototype.find = function (filter, options) {
392
+ Collection.prototype.find = function (filter, options, bypassLogs) {
382
393
  var _this = this;
383
394
  if (filter === void 0) { filter = {}; }
395
+ if (bypassLogs === void 0) { bypassLogs = false; }
384
396
  return new Promise(function (resolve, reject) {
385
- if (_this.createLogs) {
386
- log_collection_1.Logs.create({
397
+ if (_this.createLogs && !bypassLogs) {
398
+ log_collection_1.Logs.insertOne({
387
399
  _id: objectIdHexString(),
388
400
  type: 'query',
389
401
  collection: _this.collectionName,
@@ -419,10 +431,11 @@ var Collection = /** @class */ (function () {
419
431
  Collection.prototype.findById = function (id, options) {
420
432
  return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).findOne({ _id: id }, options);
421
433
  };
422
- Collection.prototype.findCount = function (filter, options) {
434
+ Collection.prototype.findCount = function (filter, options, bypassLogs) {
423
435
  if (filter === void 0) { filter = {}; }
424
- if (this.createLogs) {
425
- log_collection_1.Logs.create({
436
+ if (bypassLogs === void 0) { bypassLogs = false; }
437
+ if (this.createLogs && !bypassLogs) {
438
+ log_collection_1.Logs.insertOne({
426
439
  _id: objectIdHexString(),
427
440
  type: 'query',
428
441
  collection: this.collectionName,
@@ -445,12 +458,13 @@ var Collection = /** @class */ (function () {
445
458
  if (filter === void 0) { filter = {}; }
446
459
  return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).find(filter, options).stream(streamOptions);
447
460
  };
448
- Collection.prototype.findOne = function (filter, options) {
461
+ Collection.prototype.findOne = function (filter, options, bypassLogs) {
449
462
  var _this = this;
450
463
  if (filter === void 0) { filter = {}; }
464
+ if (bypassLogs === void 0) { bypassLogs = false; }
451
465
  return new Promise(function (resolve, reject) {
452
- if (_this.createLogs) {
453
- log_collection_1.Logs.create({
466
+ if (_this.createLogs && !bypassLogs) {
467
+ log_collection_1.Logs.insertOne({
454
468
  _id: objectIdHexString(),
455
469
  type: 'query',
456
470
  collection: _this.collectionName,
@@ -483,20 +497,21 @@ var Collection = /** @class */ (function () {
483
497
  });
484
498
  });
485
499
  };
486
- Collection.prototype.findOneAndDelete = function (filter, options) {
500
+ Collection.prototype.findOneAndDelete = function (filter, options, bypassLogs) {
487
501
  var _this = this;
488
502
  if (filter === void 0) { filter = {}; }
503
+ if (bypassLogs === void 0) { bypassLogs = false; }
489
504
  return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
490
505
  var doc;
491
506
  return __generator(this, function (_a) {
492
507
  switch (_a.label) {
493
508
  case 0:
494
- if (!this.createLogs) return [3 /*break*/, 2];
509
+ if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 2];
495
510
  return [4 /*yield*/, this.findOne(filter)];
496
511
  case 1:
497
512
  doc = _a.sent();
498
513
  if (doc) {
499
- log_collection_1.Logs.create({
514
+ log_collection_1.Logs.insertOne({
500
515
  _id: objectIdHexString(),
501
516
  type: 'document',
502
517
  collection: this.collectionName,
@@ -524,21 +539,31 @@ var Collection = /** @class */ (function () {
524
539
  });
525
540
  }); });
526
541
  };
527
- Collection.prototype.findOneAndReplace = function (filter, replacement, options) {
542
+ Collection.prototype.findOneAndReplace = function (filter, replacement, options, bypassLogs) {
528
543
  var _this = this;
529
544
  if (filter === void 0) { filter = {}; }
545
+ if (bypassLogs === void 0) { bypassLogs = false; }
530
546
  return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
531
- var doc;
547
+ var validation, doc;
532
548
  return __generator(this, function (_a) {
533
549
  switch (_a.label) {
534
550
  case 0:
535
- replacement['updatedAt'] = new Date();
536
- if (!this.createLogs) return [3 /*break*/, 2];
537
- return [4 /*yield*/, this.findOne(filter)];
551
+ validation = this.simplschema.newContext().validate(replacement);
552
+ if (!!validation.isValid()) return [3 /*break*/, 1];
553
+ console.log(new Date(), this.collectionName, 'Schema Errors', validation.validationErrors());
554
+ index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal('insertErrorLog', 'Schema Failed on findOneAndReplace - ' + this.collectionName, [validation.validationErrors(), replacement]);
555
+ reject(validation.validationErrors());
556
+ return [3 /*break*/, 4];
538
557
  case 1:
558
+ if (this.timestamps) {
559
+ replacement['updatedAt'] = new Date();
560
+ }
561
+ if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 3];
562
+ return [4 /*yield*/, this.findOne(filter)];
563
+ case 2:
539
564
  doc = _a.sent();
540
565
  if (doc) {
541
- log_collection_1.Logs.create({
566
+ log_collection_1.Logs.insertOne({
542
567
  _id: objectIdHexString(),
543
568
  type: 'document',
544
569
  collection: this.collectionName,
@@ -551,8 +576,8 @@ var Collection = /** @class */ (function () {
551
576
  route: ''
552
577
  });
553
578
  }
554
- _a.label = 2;
555
- case 2:
579
+ _a.label = 3;
580
+ case 3:
556
581
  index_1.ResolveIOServer.getMainDB().collection(this.collectionName).findOneAndReplace(filter, replacement, options).then(function (res) {
557
582
  if (res.ok) {
558
583
  resolve(res.value);
@@ -561,31 +586,42 @@ var Collection = /** @class */ (function () {
561
586
  reject(res.lastErrorObject);
562
587
  }
563
588
  }, function (err) { return reject(err); });
564
- return [2 /*return*/];
589
+ _a.label = 4;
590
+ case 4: return [2 /*return*/];
565
591
  }
566
592
  });
567
593
  }); });
568
594
  };
569
- Collection.prototype.findOneAndUpdate = function (filter, update, options) {
595
+ Collection.prototype.findOneAndUpdate = function (filter, update, options, bypassLogs) {
570
596
  var _this = this;
571
597
  if (filter === void 0) { filter = {}; }
598
+ if (bypassLogs === void 0) { bypassLogs = false; }
572
599
  return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
573
- var doc;
600
+ var validation, doc;
574
601
  return __generator(this, function (_a) {
575
602
  switch (_a.label) {
576
603
  case 0:
577
- if (!update['$set']) {
578
- update['$set'] = { updatedAt: new Date() };
579
- }
580
- else {
581
- update['$set']['updatedAt'] = new Date();
604
+ validation = this.simplschema.newContext().validate(update, { modifier: true });
605
+ if (!!validation.isValid()) return [3 /*break*/, 1];
606
+ console.log(new Date(), this.collectionName, 'Schema Errors', validation.validationErrors());
607
+ index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal('insertErrorLog', 'Schema Failed on findOneAndUpdate - ' + this.collectionName, [validation.validationErrors(), update]);
608
+ reject(validation.validationErrors());
609
+ return [3 /*break*/, 4];
610
+ case 1:
611
+ if (this.timestamps) {
612
+ if (!update['$set']) {
613
+ update['$set'] = { updatedAt: new Date() };
614
+ }
615
+ else {
616
+ update['$set']['updatedAt'] = new Date();
617
+ }
582
618
  }
583
- if (!this.createLogs) return [3 /*break*/, 2];
619
+ if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 3];
584
620
  return [4 /*yield*/, this.findOne(filter)];
585
- case 1:
621
+ case 2:
586
622
  doc = _a.sent();
587
623
  if (doc) {
588
- log_collection_1.Logs.create({
624
+ log_collection_1.Logs.insertOne({
589
625
  _id: objectIdHexString(),
590
626
  type: 'document',
591
627
  collection: this.collectionName,
@@ -598,8 +634,8 @@ var Collection = /** @class */ (function () {
598
634
  route: ''
599
635
  });
600
636
  }
601
- _a.label = 2;
602
- case 2:
637
+ _a.label = 3;
638
+ case 3:
603
639
  index_1.ResolveIOServer.getMainDB().collection(this.collectionName).findOneAndUpdate(filter, update, options).then(function (res) {
604
640
  if (res.ok) {
605
641
  resolve(res.value);
@@ -608,7 +644,8 @@ var Collection = /** @class */ (function () {
608
644
  reject(res.lastErrorObject);
609
645
  }
610
646
  }, function (err) { return reject(err); });
611
- return [2 /*return*/];
647
+ _a.label = 4;
648
+ case 4: return [2 /*return*/];
612
649
  }
613
650
  });
614
651
  }); });
@@ -619,22 +656,34 @@ var Collection = /** @class */ (function () {
619
656
  Collection.prototype.indexExists = function (indexes, options) {
620
657
  return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).indexExists(indexes, options);
621
658
  };
622
- Collection.prototype.insertMany = function (docs, options) {
659
+ Collection.prototype.insertMany = function (docs, options, bypassLogs) {
623
660
  var _this = this;
661
+ if (bypassLogs === void 0) { bypassLogs = false; }
624
662
  return new Promise(function (resolve, reject) {
625
- if (!docs || !docs.length) {
626
- reject('No documents');
663
+ if (!docs.length) {
664
+ reject('No Documents');
627
665
  return;
628
666
  }
629
- if (_this.timestamps) {
630
- docs.forEach(function (doc) {
667
+ var validationResults = [];
668
+ docs.forEach(function (doc) {
669
+ var validation = _this.simplschema.newContext().validate(doc);
670
+ if (!validation.isValid()) {
671
+ console.log(new Date(), _this.collectionName, 'Schema Errors', validation.validationErrors());
672
+ index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal('insertErrorLog', 'Schema Failed on insertMany - ' + _this.collectionName, [validation.validationErrors(), doc]);
673
+ validationResults.push(false);
674
+ }
675
+ else {
676
+ validationResults.push(true);
677
+ }
678
+ });
679
+ var validDocs = docs.filter(function (a, idx) { return validationResults[idx]; });
680
+ validDocs.forEach(function (doc) {
681
+ if (_this.timestamps) {
631
682
  doc['createdAt'] = new Date();
632
683
  doc['updatedAt'] = new Date();
633
- });
634
- }
635
- if (_this.createLogs) {
636
- docs.forEach(function (doc) {
637
- log_collection_1.Logs.create({
684
+ }
685
+ if (_this.createLogs && !bypassLogs) {
686
+ log_collection_1.Logs.insertOne({
638
687
  _id: objectIdHexString(),
639
688
  type: 'document',
640
689
  collection: _this.collectionName,
@@ -646,55 +695,62 @@ var Collection = /** @class */ (function () {
646
695
  messageId: '',
647
696
  route: ''
648
697
  });
649
- });
650
- }
651
- index_1.ResolveIOServer.getMainDB().collection(_this.collectionName).insertMany(docs, options).then(function (res) {
652
- if (res.result.ok) {
653
- resolve(docs);
654
- }
655
- else {
656
- reject(res.result.ok);
657
698
  }
658
- }, function (err) {
659
- reject(err);
660
699
  });
700
+ if (validDocs.length) {
701
+ index_1.ResolveIOServer.getMainDB().collection(_this.collectionName).insertMany(validDocs, options).then(function (res) {
702
+ if (res.result.ok) {
703
+ resolve(validDocs);
704
+ }
705
+ else {
706
+ reject(res.result.ok);
707
+ }
708
+ }, function (err) {
709
+ reject(err);
710
+ });
711
+ }
661
712
  });
662
713
  };
663
- Collection.prototype.insertOne = function (doc, options) {
714
+ Collection.prototype.insertOne = function (doc, options, bypassLogs) {
664
715
  var _this = this;
716
+ if (bypassLogs === void 0) { bypassLogs = false; }
665
717
  return new Promise(function (resolve, reject) {
666
- if (!doc) {
667
- reject('No document');
668
- return;
669
- }
670
- if (_this.timestamps) {
671
- doc['createdAt'] = new Date();
672
- doc['updatedAt'] = new Date();
718
+ var validation = _this.simplschema.newContext().validate(doc);
719
+ if (!validation.isValid()) {
720
+ console.log(new Date(), _this.collectionName, 'Schema Errors', validation.validationErrors());
721
+ index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal('insertErrorLog', 'Schema Failed on insertOne - ' + _this.collectionName, [validation.validationErrors(), doc]);
722
+ reject(validation.validationErrors());
673
723
  }
674
- if (_this.createLogs) {
675
- log_collection_1.Logs.create({
676
- _id: objectIdHexString(),
677
- type: 'document',
678
- collection: _this.collectionName,
679
- id_document: doc['_id'],
680
- payload: common_1.getBinarySize(JSON.stringify([doc, options])) < 200000 ? JSON.stringify([doc, options], null, 2) : 'Too Big',
681
- method: 'insertOne',
682
- id_user: '',
683
- user: '',
684
- messageId: '',
685
- route: ''
686
- });
687
- }
688
- index_1.ResolveIOServer.getMainDB().collection(_this.collectionName).insertOne(doc, options).then(function (res) {
689
- if (res.result.ok) {
690
- resolve(doc);
724
+ else {
725
+ if (_this.timestamps) {
726
+ doc['createdAt'] = new Date();
727
+ doc['updatedAt'] = new Date();
691
728
  }
692
- else {
693
- reject(res.result.ok);
729
+ if (_this.createLogs && !bypassLogs) {
730
+ log_collection_1.Logs.insertOne({
731
+ _id: objectIdHexString(),
732
+ type: 'document',
733
+ collection: _this.collectionName,
734
+ id_document: doc['_id'],
735
+ payload: common_1.getBinarySize(JSON.stringify([doc, options])) < 200000 ? JSON.stringify([doc, options], null, 2) : 'Too Big',
736
+ method: 'insertOne',
737
+ id_user: '',
738
+ user: '',
739
+ messageId: '',
740
+ route: ''
741
+ });
694
742
  }
695
- }, function (err) {
696
- reject(err);
697
- });
743
+ index_1.ResolveIOServer.getMainDB().collection(_this.collectionName).insertOne(doc, options).then(function (res) {
744
+ if (res.result.ok) {
745
+ resolve(doc);
746
+ }
747
+ else {
748
+ reject(res.result.ok);
749
+ }
750
+ }, function (err) {
751
+ reject(err);
752
+ });
753
+ }
698
754
  });
699
755
  };
700
756
  Collection.prototype.listIndexes = function (options) {
@@ -706,19 +762,32 @@ var Collection = /** @class */ (function () {
706
762
  Collection.prototype.rename = function (newName, options) {
707
763
  return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).rename(newName, options);
708
764
  };
709
- Collection.prototype.replaceOne = function (filter, replacement, options) {
765
+ Collection.prototype.replaceOne = function (filter, replacement, options, bypassLogs, bypassDocument) {
710
766
  var _this = this;
767
+ if (bypassLogs === void 0) { bypassLogs = false; }
768
+ if (bypassDocument === void 0) { bypassDocument = false; }
711
769
  return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
712
- var doc;
770
+ var validation, doc, versionDoc, prevDoc, docId, docVersion, updatedDoc;
713
771
  return __generator(this, function (_a) {
714
772
  switch (_a.label) {
715
773
  case 0:
716
- if (!this.createLogs) return [3 /*break*/, 2];
717
- return [4 /*yield*/, this.findOne(filter)];
774
+ validation = this.simplschema.newContext().validate(replacement);
775
+ if (!!validation.isValid()) return [3 /*break*/, 1];
776
+ console.log(new Date(), this.collectionName, 'Schema Errors', validation.validationErrors());
777
+ index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal('insertErrorLog', 'Schema Failed on replaceOne - ' + this.collectionName, [validation.validationErrors(), replacement]);
778
+ reject(validation.validationErrors());
779
+ return [3 /*break*/, 11];
718
780
  case 1:
781
+ if (this.timestamps) {
782
+ replacement['updatedAt'] = new Date();
783
+ }
784
+ if (!!bypassDocument) return [3 /*break*/, 10];
785
+ return [4 /*yield*/, this.findOne(filter)];
786
+ case 2:
719
787
  doc = _a.sent();
720
- if (doc) {
721
- log_collection_1.Logs.create({
788
+ if (!doc) return [3 /*break*/, 8];
789
+ if (this.createLogs && !bypassLogs) {
790
+ log_collection_1.Logs.insertOne({
722
791
  _id: objectIdHexString(),
723
792
  type: 'document',
724
793
  collection: this.collectionName,
@@ -731,8 +800,18 @@ var Collection = /** @class */ (function () {
731
800
  route: ''
732
801
  });
733
802
  }
734
- _a.label = 2;
735
- case 2:
803
+ if (!this.useVersions) return [3 /*break*/, 6];
804
+ versionDoc = common_1.deepCopy(doc);
805
+ versionDoc['_id'] = { _id: doc['_id'], __v: doc['__v'] };
806
+ index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).insertOne(versionDoc);
807
+ if (doc['__v'] >= 4) {
808
+ index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({ $and: [
809
+ { '_id._id': doc['_id'] },
810
+ { '_id.__v': { $lte: doc['__v'] - 4 } }
811
+ ] });
812
+ }
813
+ if (!(doc['__v'] === replacement['__v'])) return [3 /*break*/, 3];
814
+ replacement['__v'] += 1;
736
815
  index_1.ResolveIOServer.getMainDB().collection(this.collectionName).replaceOne(filter, replacement, options).then(function (res) {
737
816
  if (res.result.ok) {
738
817
  resolve(res.result.nModified);
@@ -743,7 +822,70 @@ var Collection = /** @class */ (function () {
743
822
  }, function (err) {
744
823
  reject(err);
745
824
  });
746
- return [2 /*return*/];
825
+ return [3 /*break*/, 5];
826
+ case 3:
827
+ console.log('invalid version - ' + this.collectionName, doc['__v'], replacement['__v']);
828
+ return [4 /*yield*/, index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).findOne({
829
+ $and: [
830
+ { '_id._id': doc['_id'] },
831
+ { '_id.__v': replacement['__v'] }
832
+ ]
833
+ })];
834
+ case 4:
835
+ prevDoc = _a.sent();
836
+ if (prevDoc) {
837
+ docId = doc['_id'];
838
+ docVersion = doc['__v'];
839
+ updatedDoc = common_1.getMongoMergeUpdatedDoc(replacement, doc, prevDoc);
840
+ updatedDoc['_id'] = docId;
841
+ updatedDoc['__v'] = docVersion + 1;
842
+ updatedDoc['updatedAt'] = new Date();
843
+ index_1.ResolveIOServer.getMainDB().collection(this.collectionName).replaceOne(filter, updatedDoc, options).then(function (res) {
844
+ if (res.result.ok) {
845
+ resolve(res.result.nModified);
846
+ }
847
+ else {
848
+ reject(res.result.ok);
849
+ }
850
+ }, function (err) {
851
+ reject(err);
852
+ });
853
+ }
854
+ else {
855
+ reject('Invalid Version And Could Not Find History - DB: ' + doc['__v'] + ', Trying to update with :' + replacement['__v']);
856
+ }
857
+ _a.label = 5;
858
+ case 5: return [3 /*break*/, 7];
859
+ case 6:
860
+ index_1.ResolveIOServer.getMainDB().collection(this.collectionName).replaceOne(filter, replacement, options).then(function (res) {
861
+ if (res.result.ok) {
862
+ resolve(res.result.nModified);
863
+ }
864
+ else {
865
+ reject(res.result.ok);
866
+ }
867
+ }, function (err) {
868
+ reject(err);
869
+ });
870
+ _a.label = 7;
871
+ case 7: return [3 /*break*/, 9];
872
+ case 8:
873
+ reject('No Document');
874
+ _a.label = 9;
875
+ case 9: return [3 /*break*/, 11];
876
+ case 10:
877
+ index_1.ResolveIOServer.getMainDB().collection(this.collectionName).replaceOne(filter, replacement, options).then(function (res) {
878
+ if (res.result.ok) {
879
+ resolve(res.result.nModified);
880
+ }
881
+ else {
882
+ reject(res.result.ok);
883
+ }
884
+ }, function (err) {
885
+ reject(err);
886
+ });
887
+ _a.label = 11;
888
+ case 11: return [2 /*return*/];
747
889
  }
748
890
  });
749
891
  }); });
@@ -751,26 +893,36 @@ var Collection = /** @class */ (function () {
751
893
  Collection.prototype.stats = function (options) {
752
894
  return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).stats(options);
753
895
  };
754
- Collection.prototype.updateMany = function (filter, update, options) {
896
+ Collection.prototype.updateMany = function (filter, update, options, bypassLogs) {
755
897
  var _this = this;
898
+ if (bypassLogs === void 0) { bypassLogs = false; }
756
899
  return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
757
- var docs;
900
+ var validation, docs;
758
901
  var _this = this;
759
902
  return __generator(this, function (_a) {
760
903
  switch (_a.label) {
761
904
  case 0:
762
- if (!update['$set']) {
763
- update['$set'] = { updatedAt: new Date() };
764
- }
765
- else {
766
- update['$set']['updatedAt'] = new Date();
905
+ if (this.timestamps) {
906
+ if (!update.$set) {
907
+ update.$set = { updatedAt: new Date() };
908
+ }
909
+ else {
910
+ update.$set['updatedAt'] = new Date();
911
+ }
767
912
  }
768
- if (!this.createLogs) return [3 /*break*/, 2];
769
- return [4 /*yield*/, this.find(filter)];
913
+ validation = this.simplschema.newContext().validate(update, { modifier: true });
914
+ if (!!validation.isValid()) return [3 /*break*/, 1];
915
+ console.log(new Date(), this.collectionName, 'Schema Errors', validation.validationErrors());
916
+ index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal('insertErrorLog', 'Schema Failed on updateMany - ' + this.collectionName, [validation.validationErrors(), update]);
917
+ reject(validation.validationErrors());
918
+ return [3 /*break*/, 4];
770
919
  case 1:
920
+ if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 3];
921
+ return [4 /*yield*/, this.find(filter)];
922
+ case 2:
771
923
  docs = _a.sent();
772
924
  docs.forEach(function (doc) {
773
- log_collection_1.Logs.create({
925
+ log_collection_1.Logs.insertOne({
774
926
  _id: objectIdHexString(),
775
927
  type: 'document',
776
928
  collection: _this.collectionName,
@@ -783,8 +935,8 @@ var Collection = /** @class */ (function () {
783
935
  route: ''
784
936
  });
785
937
  });
786
- _a.label = 2;
787
- case 2:
938
+ _a.label = 3;
939
+ case 3:
788
940
  index_1.ResolveIOServer.getMainDB().collection(this.collectionName).updateMany(filter, update, options).then(function (res) {
789
941
  if (res.result.ok) {
790
942
  resolve(res.result.nModified);
@@ -793,54 +945,93 @@ var Collection = /** @class */ (function () {
793
945
  reject(res.result.ok);
794
946
  }
795
947
  }, function (err) { return reject(err); });
796
- return [2 /*return*/];
948
+ _a.label = 4;
949
+ case 4: return [2 /*return*/];
797
950
  }
798
951
  });
799
952
  }); });
800
- return;
801
953
  };
802
- Collection.prototype.updateOne = function (filter, update, options) {
954
+ Collection.prototype.updateOne = function (filter, update, options, bypassLogs) {
803
955
  var _this = this;
956
+ if (bypassLogs === void 0) { bypassLogs = false; }
804
957
  return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
805
- var doc;
958
+ var validation, doc, versionDoc;
806
959
  return __generator(this, function (_a) {
807
960
  switch (_a.label) {
808
961
  case 0:
809
- if (!update['$set']) {
810
- update['$set'] = { updatedAt: new Date() };
811
- }
812
- else {
813
- update['$set']['updatedAt'] = new Date();
962
+ if (this.timestamps) {
963
+ if (!update.$set) {
964
+ update.$set = { updatedAt: new Date() };
965
+ }
966
+ else {
967
+ update.$set['updatedAt'] = new Date();
968
+ }
814
969
  }
815
- if (!this.createLogs) return [3 /*break*/, 2];
816
- return [4 /*yield*/, this.findOne(filter)];
817
- case 1:
970
+ validation = this.simplschema.newContext().validate(update, { modifier: true });
971
+ if (!!validation.isValid()) return [3 /*break*/, 1];
972
+ console.log(new Date(), this.collectionName, 'Schema Errors', validation.validationErrors());
973
+ index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal('insertErrorLog', 'Schema Failed on updateOne - ' + this.collectionName, [validation.validationErrors(), update]);
974
+ reject(validation.validationErrors());
975
+ return [3 /*break*/, 3];
976
+ case 1: return [4 /*yield*/, this.findOne(filter)];
977
+ case 2:
818
978
  doc = _a.sent();
819
979
  if (doc) {
820
- log_collection_1.Logs.create({
821
- _id: objectIdHexString(),
822
- type: 'document',
823
- collection: this.collectionName,
824
- id_document: doc['_id'],
825
- payload: common_1.getBinarySize(JSON.stringify([doc, filter, update, options])) < 200000 ? JSON.stringify([doc, filter, update, options], null, 2) : 'Too Big',
826
- method: 'updateOne',
827
- id_user: '',
828
- user: '',
829
- messageId: '',
830
- route: ''
831
- });
832
- }
833
- _a.label = 2;
834
- case 2:
835
- index_1.ResolveIOServer.getMainDB().collection(this.collectionName).updateOne(filter, update, options).then(function (res) {
836
- if (res.result.ok) {
837
- resolve(res.result.nModified);
980
+ if (this.createLogs && !bypassLogs) {
981
+ log_collection_1.Logs.insertOne({
982
+ _id: objectIdHexString(),
983
+ type: 'document',
984
+ collection: this.collectionName,
985
+ id_document: doc['_id'],
986
+ payload: common_1.getBinarySize(JSON.stringify([doc, filter, update, options])) < 200000 ? JSON.stringify([doc, filter, update, options], null, 2) : 'Too Big',
987
+ method: 'updateOne',
988
+ id_user: '',
989
+ user: '',
990
+ messageId: '',
991
+ route: ''
992
+ });
993
+ }
994
+ if (this.useVersions) {
995
+ versionDoc = common_1.deepCopy(doc);
996
+ versionDoc['_id'] = { _id: doc['_id'], __v: doc['__v'] };
997
+ index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).insertOne(versionDoc);
998
+ if (doc['__v'] >= 4) {
999
+ index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({ $and: [
1000
+ { '_id._id': doc['_id'] },
1001
+ { '_id.__v': { $lte: doc['__v'] - 4 } }
1002
+ ] });
1003
+ }
1004
+ if (!update.$inc) {
1005
+ update.$inc = { __v: 1 };
1006
+ }
1007
+ else if (!update.$inc.__v) {
1008
+ update.$inc.__v = 1;
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); });
838
1018
  }
839
1019
  else {
840
- reject(res.result.ok);
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); });
841
1028
  }
842
- }, function (err) { return reject(err); });
843
- return [2 /*return*/];
1029
+ }
1030
+ else {
1031
+ reject('No Document');
1032
+ }
1033
+ _a.label = 3;
1034
+ case 3: return [2 /*return*/];
844
1035
  }
845
1036
  });
846
1037
  }); });