@resolveio/server-lib 22.2.14 → 22.2.16

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.
@@ -244,38 +244,80 @@ var MongoManager = /** @class */ (function () {
244
244
  MongoManager.prototype.oneTimeTransaction = function (fn) {
245
245
  var _this = this;
246
246
  return asyncLocalStorage.run({}, function () { return __awaiter(_this, void 0, void 0, function () {
247
- var session, result_1, store;
247
+ var attempt, maxRetries, _loop_1, this_1, state_1;
248
248
  var _this = this;
249
249
  return __generator(this, function (_a) {
250
250
  switch (_a.label) {
251
251
  case 0:
252
- session = resolveio_server_app_1.ResolveIOServer.getMongoConnection().startSession();
253
- asyncLocalStorage.getStore().session = session;
252
+ attempt = 0;
253
+ maxRetries = 4;
254
+ _loop_1 = function () {
255
+ var session, store, result_1, err_1, jitter, _b, activeStore;
256
+ return __generator(this, function (_c) {
257
+ switch (_c.label) {
258
+ case 0:
259
+ session = resolveio_server_app_1.ResolveIOServer.getMongoConnection().startSession();
260
+ store = asyncLocalStorage.getStore();
261
+ if (store) {
262
+ store.session = session;
263
+ }
264
+ _c.label = 1;
265
+ case 1:
266
+ _c.trys.push([1, 3, 5, 9]);
267
+ return [4 /*yield*/, session.withTransaction(function () { return __awaiter(_this, void 0, void 0, function () {
268
+ return __generator(this, function (_a) {
269
+ switch (_a.label) {
270
+ case 0: return [4 /*yield*/, fn()];
271
+ case 1:
272
+ result_1 = _a.sent();
273
+ return [2 /*return*/, result_1];
274
+ }
275
+ });
276
+ }); })];
277
+ case 2:
278
+ _c.sent();
279
+ return [2 /*return*/, { value: result_1 }];
280
+ case 3:
281
+ err_1 = _c.sent();
282
+ if (!this_1.shouldRetryTransactionError(err_1) || attempt >= maxRetries) {
283
+ throw err_1;
284
+ }
285
+ attempt += 1;
286
+ jitter = Math.floor(Math.random() * 60);
287
+ return [4 /*yield*/, this_1.delay((120 * attempt) + jitter)];
288
+ case 4:
289
+ _c.sent();
290
+ return [3 /*break*/, 9];
291
+ case 5:
292
+ _c.trys.push([5, 7, , 8]);
293
+ return [4 /*yield*/, session.endSession()];
294
+ case 6:
295
+ _c.sent();
296
+ return [3 /*break*/, 8];
297
+ case 7:
298
+ _b = _c.sent();
299
+ return [3 /*break*/, 8];
300
+ case 8:
301
+ activeStore = asyncLocalStorage.getStore();
302
+ if (activeStore) {
303
+ delete activeStore.session;
304
+ }
305
+ return [7 /*endfinally*/];
306
+ case 9: return [2 /*return*/];
307
+ }
308
+ });
309
+ };
310
+ this_1 = this;
254
311
  _a.label = 1;
255
312
  case 1:
256
- _a.trys.push([1, , 3, 5]);
257
- return [4 /*yield*/, session.withTransaction(function () { return __awaiter(_this, void 0, void 0, function () {
258
- return __generator(this, function (_a) {
259
- switch (_a.label) {
260
- case 0: return [4 /*yield*/, fn()];
261
- case 1:
262
- // Directly assign the result from fn()
263
- result_1 = _a.sent();
264
- return [2 /*return*/, result_1]; // Explicit return for transaction validation
265
- }
266
- });
267
- }); })];
313
+ if (!true) return [3 /*break*/, 3];
314
+ return [5 /*yield**/, _loop_1()];
268
315
  case 2:
269
- _a.sent();
270
- return [2 /*return*/, result_1]; // Transaction committed successfully
271
- case 3: return [4 /*yield*/, session.endSession()];
272
- case 4:
273
- _a.sent();
274
- store = asyncLocalStorage.getStore();
275
- if (store)
276
- delete store.session;
277
- return [7 /*endfinally*/];
278
- case 5: return [2 /*return*/];
316
+ state_1 = _a.sent();
317
+ if (typeof state_1 === "object")
318
+ return [2 /*return*/, state_1.value];
319
+ return [3 /*break*/, 1];
320
+ case 3: return [2 /*return*/];
279
321
  }
280
322
  });
281
323
  }); });
@@ -355,6 +397,43 @@ var MongoManager = /** @class */ (function () {
355
397
  });
356
398
  });
357
399
  };
400
+ MongoManager.prototype.hasMongoErrorLabel = function (err, label) {
401
+ if (!err || !label) {
402
+ return false;
403
+ }
404
+ if (typeof err.hasErrorLabel === 'function') {
405
+ try {
406
+ if (err.hasErrorLabel(label)) {
407
+ return true;
408
+ }
409
+ }
410
+ catch (_a) { }
411
+ }
412
+ if (Array.isArray(err.errorLabels) && err.errorLabels.includes(label)) {
413
+ return true;
414
+ }
415
+ return false;
416
+ };
417
+ MongoManager.prototype.shouldRetryWriteConflict = function (err) {
418
+ if (!err) {
419
+ return false;
420
+ }
421
+ if (err.code === 112 || err.codeName === 'WriteConflict') {
422
+ return true;
423
+ }
424
+ var message = String(err.message || '');
425
+ return /write conflict/i.test(message) || /yielding is disabled/i.test(message);
426
+ };
427
+ MongoManager.prototype.shouldRetryTransactionError = function (err) {
428
+ if (!err) {
429
+ return false;
430
+ }
431
+ if (this.shouldRetryWriteConflict(err) || this.shouldRetryWaitQueueTimeout(err)) {
432
+ return true;
433
+ }
434
+ return this.hasMongoErrorLabel(err, 'TransientTransactionError')
435
+ || this.hasMongoErrorLabel(err, 'UnknownTransactionCommitResult');
436
+ };
358
437
  MongoManager.prototype.shouldRetryWaitQueueTimeout = function (err) {
359
438
  if (!err) {
360
439
  return false;
@@ -366,7 +445,7 @@ var MongoManager = /** @class */ (function () {
366
445
  };
367
446
  MongoManager.prototype.retryRead = function (operation, options) {
368
447
  return __awaiter(this, void 0, void 0, function () {
369
- var attempt, maxRetries, err_1;
448
+ var attempt, maxRetries, err_2;
370
449
  return __generator(this, function (_a) {
371
450
  switch (_a.label) {
372
451
  case 0:
@@ -384,9 +463,9 @@ var MongoManager = /** @class */ (function () {
384
463
  return [4 /*yield*/, operation()];
385
464
  case 3: return [2 /*return*/, _a.sent()];
386
465
  case 4:
387
- err_1 = _a.sent();
388
- if (!this.shouldRetryWaitQueueTimeout(err_1) || attempt >= maxRetries) {
389
- throw err_1;
466
+ err_2 = _a.sent();
467
+ if (!this.shouldRetryWaitQueueTimeout(err_2) || attempt >= maxRetries) {
468
+ throw err_2;
390
469
  }
391
470
  attempt += 1;
392
471
  return [4 /*yield*/, this.delay(100 * attempt)];
@@ -447,7 +526,7 @@ var MongoManager = /** @class */ (function () {
447
526
  };
448
527
  MongoManager.prototype.executeFind = function (collectionName, filter, options, cacheKey) {
449
528
  return __awaiter(this, void 0, void 0, function () {
450
- var result, collection, _a, session, safeOptions, pubContext, monitor, queryStart, err_2, durationMs;
529
+ var result, collection, _a, session, safeOptions, pubContext, monitor, queryStart, err_3, durationMs;
451
530
  return __generator(this, function (_b) {
452
531
  switch (_b.label) {
453
532
  case 0:
@@ -464,15 +543,15 @@ var MongoManager = /** @class */ (function () {
464
543
  result = _b.sent();
465
544
  return [3 /*break*/, 6];
466
545
  case 3:
467
- err_2 = _b.sent();
546
+ err_3 = _b.sent();
468
547
  console.log(JSON.stringify([new Date(), 'Error Execute Find', collectionName, filter, safeOptions, {
469
- code: err_2.code,
470
- codeName: err_2.codeName,
471
- message: err_2.message,
472
- stack: err_2.stack
548
+ code: err_3.code,
549
+ codeName: err_3.codeName,
550
+ message: err_3.message,
551
+ stack: err_3.stack
473
552
  }], null, 2));
474
- err_2.message = "Error in Execute Find: ".concat(collectionName, " => ").concat(err_2.codeName || 'NoCodeName', " => ").concat(err_2.message);
475
- throw err_2;
553
+ err_3.message = "Error in Execute Find: ".concat(collectionName, " => ").concat(err_3.codeName || 'NoCodeName', " => ").concat(err_3.message);
554
+ throw err_3;
476
555
  case 4:
477
556
  durationMs = Date.now() - queryStart;
478
557
  return [4 /*yield*/, monitor.finish()];
@@ -549,7 +628,7 @@ var MongoManager = /** @class */ (function () {
549
628
  };
550
629
  MongoManager.prototype.executeFindOne = function (collectionName, filter, options, cacheKey) {
551
630
  return __awaiter(this, void 0, void 0, function () {
552
- var result, collection, _a, session, safeOptions, pubContext, monitor, queryStart, err_3, durationMs;
631
+ var result, collection, _a, session, safeOptions, pubContext, monitor, queryStart, err_4, durationMs;
553
632
  return __generator(this, function (_b) {
554
633
  switch (_b.label) {
555
634
  case 0:
@@ -566,15 +645,15 @@ var MongoManager = /** @class */ (function () {
566
645
  result = _b.sent();
567
646
  return [3 /*break*/, 6];
568
647
  case 3:
569
- err_3 = _b.sent();
648
+ err_4 = _b.sent();
570
649
  console.log(JSON.stringify([new Date(), 'Error Execute Find One', collectionName, filter, safeOptions, {
571
- code: err_3.code,
572
- codeName: err_3.codeName,
573
- message: err_3.message,
574
- stack: err_3.stack
650
+ code: err_4.code,
651
+ codeName: err_4.codeName,
652
+ message: err_4.message,
653
+ stack: err_4.stack
575
654
  }], null, 2));
576
- err_3.message = "Error in Execute Find One: ".concat(collectionName, " => ").concat(err_3.codeName || 'NoCodeName', " => ").concat(err_3.message);
577
- throw err_3;
655
+ err_4.message = "Error in Execute Find One: ".concat(collectionName, " => ").concat(err_4.codeName || 'NoCodeName', " => ").concat(err_4.message);
656
+ throw err_4;
578
657
  case 4:
579
658
  durationMs = Date.now() - queryStart;
580
659
  return [4 /*yield*/, monitor.finish()];
@@ -651,7 +730,7 @@ var MongoManager = /** @class */ (function () {
651
730
  };
652
731
  MongoManager.prototype.executeAggregate = function (collectionName, pipeline, options, cacheKey, collections) {
653
732
  return __awaiter(this, void 0, void 0, function () {
654
- var result, collection, _a, session, safeOptions, pubContext, monitor, queryStart, err_4, durationMs;
733
+ var result, collection, _a, session, safeOptions, pubContext, monitor, queryStart, err_5, durationMs;
655
734
  return __generator(this, function (_b) {
656
735
  switch (_b.label) {
657
736
  case 0:
@@ -668,15 +747,15 @@ var MongoManager = /** @class */ (function () {
668
747
  result = _b.sent();
669
748
  return [3 /*break*/, 6];
670
749
  case 3:
671
- err_4 = _b.sent();
750
+ err_5 = _b.sent();
672
751
  console.log(JSON.stringify([new Date(), 'Error Execute Aggregate', collectionName, pipeline, collections, safeOptions, {
673
- code: err_4.code,
674
- codeName: err_4.codeName,
675
- message: err_4.message,
676
- stack: err_4.stack
752
+ code: err_5.code,
753
+ codeName: err_5.codeName,
754
+ message: err_5.message,
755
+ stack: err_5.stack
677
756
  }], null, 2));
678
- err_4.message = "Error in Execute Aggregate: ".concat(collectionName, " => ").concat(err_4.codeName || 'NoCodeName', " => ").concat(err_4.message);
679
- throw err_4;
757
+ err_5.message = "Error in Execute Aggregate: ".concat(collectionName, " => ").concat(err_5.codeName || 'NoCodeName', " => ").concat(err_5.message);
758
+ throw err_5;
680
759
  case 4:
681
760
  durationMs = Date.now() - queryStart;
682
761
  return [4 /*yield*/, monitor.finish()];
@@ -738,7 +817,7 @@ var MongoManager = /** @class */ (function () {
738
817
  };
739
818
  MongoManager.prototype.executeCountDocuments = function (collectionName, filter, options, cacheKey) {
740
819
  return __awaiter(this, void 0, void 0, function () {
741
- var result, collection, _a, session, safeOptions, pubContext, monitor, queryStart, err_5, durationMs;
820
+ var result, collection, _a, session, safeOptions, pubContext, monitor, queryStart, err_6, durationMs;
742
821
  return __generator(this, function (_b) {
743
822
  switch (_b.label) {
744
823
  case 0:
@@ -755,15 +834,15 @@ var MongoManager = /** @class */ (function () {
755
834
  result = _b.sent();
756
835
  return [3 /*break*/, 6];
757
836
  case 3:
758
- err_5 = _b.sent();
837
+ err_6 = _b.sent();
759
838
  console.log(JSON.stringify([new Date(), 'Error Execute Count Documents', collectionName, filter, safeOptions, {
760
- code: err_5.code,
761
- codeName: err_5.codeName,
762
- message: err_5.message,
763
- stack: err_5.stack
839
+ code: err_6.code,
840
+ codeName: err_6.codeName,
841
+ message: err_6.message,
842
+ stack: err_6.stack
764
843
  }], null, 2));
765
- err_5.message = "Error in Execute Count Documents: ".concat(collectionName, " => ").concat(err_5.codeName || 'NoCodeName', " => ").concat(err_5.message);
766
- throw err_5;
844
+ err_6.message = "Error in Execute Count Documents: ".concat(collectionName, " => ").concat(err_6.codeName || 'NoCodeName', " => ").concat(err_6.message);
845
+ throw err_6;
767
846
  case 4:
768
847
  durationMs = Date.now() - queryStart;
769
848
  return [4 /*yield*/, monitor.finish()];
@@ -1011,7 +1090,7 @@ var MongoManager = /** @class */ (function () {
1011
1090
  };
1012
1091
  MongoManager.prototype.teardownChangeStream = function (forceClose) {
1013
1092
  return __awaiter(this, void 0, void 0, function () {
1014
- var currentStream, err_6;
1093
+ var currentStream, err_7;
1015
1094
  return __generator(this, function (_a) {
1016
1095
  switch (_a.label) {
1017
1096
  case 0:
@@ -1030,8 +1109,8 @@ var MongoManager = /** @class */ (function () {
1030
1109
  _a.sent();
1031
1110
  return [3 /*break*/, 4];
1032
1111
  case 3:
1033
- err_6 = _a.sent();
1034
- console.log(new Date(), 'Mongo change stream close failed', err_6);
1112
+ err_7 = _a.sent();
1113
+ console.log(new Date(), 'Mongo change stream close failed', err_7);
1035
1114
  return [3 /*break*/, 4];
1036
1115
  case 4: return [2 /*return*/];
1037
1116
  }
@@ -1069,7 +1148,7 @@ var MongoManager = /** @class */ (function () {
1069
1148
  return;
1070
1149
  }
1071
1150
  this._changeStreamRestartTimeout = setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
1072
- var err_7;
1151
+ var err_8;
1073
1152
  return __generator(this, function (_a) {
1074
1153
  switch (_a.label) {
1075
1154
  case 0:
@@ -1082,8 +1161,8 @@ var MongoManager = /** @class */ (function () {
1082
1161
  _a.sent();
1083
1162
  return [3 /*break*/, 4];
1084
1163
  case 3:
1085
- err_7 = _a.sent();
1086
- console.log(new Date(), 'Mongo change stream restart failed. Retrying...', err_7);
1164
+ err_8 = _a.sent();
1165
+ console.log(new Date(), 'Mongo change stream restart failed. Retrying...', err_8);
1087
1166
  this.scheduleChangeStreamRestart(Math.min(delayMs * 2, 60000));
1088
1167
  return [3 /*break*/, 4];
1089
1168
  case 4: return [2 /*return*/];
@@ -1223,7 +1302,8 @@ var MongoManagerCollection = /** @class */ (function () {
1223
1302
  if (err.code === 112 || err.codeName === 'WriteConflict') {
1224
1303
  return true;
1225
1304
  }
1226
- return typeof err.message === 'string' && err.message.includes('Write conflict');
1305
+ var message = String(err.message || '');
1306
+ return /write conflict/i.test(message) || /yielding is disabled/i.test(message);
1227
1307
  };
1228
1308
  MongoManagerCollection.prototype.shouldRetryWaitQueueTimeout = function (err) {
1229
1309
  if (!err) {
@@ -1244,7 +1324,7 @@ var MongoManagerCollection = /** @class */ (function () {
1244
1324
  };
1245
1325
  MongoManagerCollection.prototype.retryWrite = function (operation, options) {
1246
1326
  return __awaiter(this, void 0, void 0, function () {
1247
- var attempt, maxRetries, err_8, shouldRetry;
1327
+ var attempt, configuredMaxRetries, maxRetries, configuredBaseDelay, baseDelayMs, err_9, shouldRetry, backoff, jitter;
1248
1328
  return __generator(this, function (_a) {
1249
1329
  switch (_a.label) {
1250
1330
  case 0:
@@ -1252,7 +1332,14 @@ var MongoManagerCollection = /** @class */ (function () {
1252
1332
  return [2 /*return*/, operation()];
1253
1333
  }
1254
1334
  attempt = 0;
1255
- maxRetries = 5;
1335
+ configuredMaxRetries = Number(process.env.MONGO_WRITE_RETRY_MAX || 8);
1336
+ maxRetries = Number.isFinite(configuredMaxRetries) && configuredMaxRetries >= 0
1337
+ ? Math.min(Math.floor(configuredMaxRetries), 20)
1338
+ : 8;
1339
+ configuredBaseDelay = Number(process.env.MONGO_WRITE_RETRY_BASE_MS || 120);
1340
+ baseDelayMs = Number.isFinite(configuredBaseDelay) && configuredBaseDelay > 0
1341
+ ? Math.min(Math.floor(configuredBaseDelay), 5000)
1342
+ : 120;
1256
1343
  _a.label = 1;
1257
1344
  case 1:
1258
1345
  if (!true) return [3 /*break*/, 7];
@@ -1262,13 +1349,15 @@ var MongoManagerCollection = /** @class */ (function () {
1262
1349
  return [4 /*yield*/, operation()];
1263
1350
  case 3: return [2 /*return*/, _a.sent()];
1264
1351
  case 4:
1265
- err_8 = _a.sent();
1266
- shouldRetry = this.shouldRetryWriteConflict(err_8) || this.shouldRetryWaitQueueTimeout(err_8);
1352
+ err_9 = _a.sent();
1353
+ shouldRetry = this.shouldRetryWriteConflict(err_9) || this.shouldRetryWaitQueueTimeout(err_9);
1267
1354
  if (!shouldRetry || attempt >= maxRetries) {
1268
- throw err_8;
1355
+ throw err_9;
1269
1356
  }
1270
1357
  attempt += 1;
1271
- return [4 /*yield*/, this.delay(75 * attempt)];
1358
+ backoff = Math.min(5000, baseDelayMs * (Math.pow(2, (attempt - 1))));
1359
+ jitter = Math.floor(Math.random() * 90);
1360
+ return [4 /*yield*/, this.delay(backoff + jitter)];
1272
1361
  case 5:
1273
1362
  _a.sent();
1274
1363
  return [3 /*break*/, 6];
@@ -1280,7 +1369,7 @@ var MongoManagerCollection = /** @class */ (function () {
1280
1369
  };
1281
1370
  MongoManagerCollection.prototype.retryRead = function (operation, options) {
1282
1371
  return __awaiter(this, void 0, void 0, function () {
1283
- var attempt, maxRetries, err_9;
1372
+ var attempt, maxRetries, err_10;
1284
1373
  return __generator(this, function (_a) {
1285
1374
  switch (_a.label) {
1286
1375
  case 0:
@@ -1298,9 +1387,9 @@ var MongoManagerCollection = /** @class */ (function () {
1298
1387
  return [4 /*yield*/, operation()];
1299
1388
  case 3: return [2 /*return*/, _a.sent()];
1300
1389
  case 4:
1301
- err_9 = _a.sent();
1302
- if (!this.shouldRetryWaitQueueTimeout(err_9) || attempt >= maxRetries) {
1303
- throw err_9;
1390
+ err_10 = _a.sent();
1391
+ if (!this.shouldRetryWaitQueueTimeout(err_10) || attempt >= maxRetries) {
1392
+ throw err_10;
1304
1393
  }
1305
1394
  attempt += 1;
1306
1395
  return [4 /*yield*/, this.delay(100 * attempt)];
@@ -1315,7 +1404,7 @@ var MongoManagerCollection = /** @class */ (function () {
1315
1404
  };
1316
1405
  MongoManagerCollection.prototype.aggregate = function (pipeline_1, options_1) {
1317
1406
  return __awaiter(this, arguments, void 0, function (pipeline, options, skipCache, bypassSession) {
1318
- var mongoSession, _a, session, safeOptions, monitor, res, err_10;
1407
+ var mongoSession, _a, session, safeOptions, monitor, res, err_11;
1319
1408
  if (skipCache === void 0) { skipCache = false; }
1320
1409
  if (bypassSession === void 0) { bypassSession = false; }
1321
1410
  return __generator(this, function (_b) {
@@ -1346,15 +1435,15 @@ var MongoManagerCollection = /** @class */ (function () {
1346
1435
  (0, subscription_dependency_context_1.recordAggregateDependencies)(this.collectionName, pipeline, res);
1347
1436
  return [2 /*return*/, res];
1348
1437
  case 4:
1349
- err_10 = _b.sent();
1438
+ err_11 = _b.sent();
1350
1439
  console.log(JSON.stringify([new Date(), 'Error Aggregate', this.collectionName, pipeline, safeOptions, {
1351
- code: err_10.code,
1352
- codeName: err_10.codeName,
1353
- message: err_10.message,
1354
- stack: err_10.stack
1440
+ code: err_11.code,
1441
+ codeName: err_11.codeName,
1442
+ message: err_11.message,
1443
+ stack: err_11.stack
1355
1444
  }], null, 2));
1356
- err_10.message = "Error in Aggregate: ".concat(this.collectionName, " => ").concat(err_10.codeName || 'NoCodeName', " => ").concat(err_10.message);
1357
- throw err_10;
1445
+ err_11.message = "Error in Aggregate: ".concat(this.collectionName, " => ").concat(err_11.codeName || 'NoCodeName', " => ").concat(err_11.message);
1446
+ throw err_11;
1358
1447
  case 5: return [4 /*yield*/, monitor.finish()];
1359
1448
  case 6:
1360
1449
  _b.sent();
@@ -1366,7 +1455,7 @@ var MongoManagerCollection = /** @class */ (function () {
1366
1455
  };
1367
1456
  MongoManagerCollection.prototype.aggregateCount = function (pipeline_1, options_1) {
1368
1457
  return __awaiter(this, arguments, void 0, function (pipeline, options, bypassSession) {
1369
- var mongoSession, _a, session, safeOptions, monitor, res, err_11;
1458
+ var mongoSession, _a, session, safeOptions, monitor, res, err_12;
1370
1459
  if (bypassSession === void 0) { bypassSession = false; }
1371
1460
  return __generator(this, function (_b) {
1372
1461
  switch (_b.label) {
@@ -1393,15 +1482,15 @@ var MongoManagerCollection = /** @class */ (function () {
1393
1482
  (0, subscription_dependency_context_1.recordAggregateDependencies)(this.collectionName, pipeline, res);
1394
1483
  return [2 /*return*/, res.length];
1395
1484
  case 3:
1396
- err_11 = _b.sent();
1485
+ err_12 = _b.sent();
1397
1486
  console.log(JSON.stringify([new Date(), 'Error Aggregate Count', this.collectionName, pipeline, safeOptions, {
1398
- code: err_11.code,
1399
- codeName: err_11.codeName,
1400
- message: err_11.message,
1401
- stack: err_11.stack
1487
+ code: err_12.code,
1488
+ codeName: err_12.codeName,
1489
+ message: err_12.message,
1490
+ stack: err_12.stack
1402
1491
  }], null, 2));
1403
- err_11.message = "Error in Aggregate Count: ".concat(this.collectionName, " => ").concat(err_11.codeName || 'NoCodeName', " => ").concat(err_11.message);
1404
- throw err_11;
1492
+ err_12.message = "Error in Aggregate Count: ".concat(this.collectionName, " => ").concat(err_12.codeName || 'NoCodeName', " => ").concat(err_12.message);
1493
+ throw err_12;
1405
1494
  case 4: return [4 /*yield*/, monitor.finish()];
1406
1495
  case 5:
1407
1496
  _b.sent();
@@ -1472,14 +1561,14 @@ var MongoManagerCollection = /** @class */ (function () {
1472
1561
  };
1473
1562
  MongoManagerCollection.prototype.bulkWrite = function (operations_1, options_1) {
1474
1563
  return __awaiter(this, arguments, void 0, function (operations, options, bypassCheckSchema, bypassLogs, bypassVersions, bypassSession) {
1475
- var BATCH_SIZE, opIndex, mongoSession, _a, session, safeOptions, batchOps, logs, versionOps, batchOps_1, batchOps_1_1, op, opType, doc, validationContext, isValid, affectedDocs, versionFilters, versionInserts, _b, _c, monitor, err_12, bwOptions, affectedDocIds, pipeline, oldVersions, _d;
1476
- var e_6, _e;
1564
+ var BATCH_SIZE, opIndex, mongoSession, _a, session, safeOptions, _loop_2, this_2;
1565
+ var _this = this;
1477
1566
  if (bypassCheckSchema === void 0) { bypassCheckSchema = false; }
1478
1567
  if (bypassLogs === void 0) { bypassLogs = false; }
1479
1568
  if (bypassVersions === void 0) { bypassVersions = false; }
1480
1569
  if (bypassSession === void 0) { bypassSession = false; }
1481
- return __generator(this, function (_f) {
1482
- switch (_f.label) {
1570
+ return __generator(this, function (_b) {
1571
+ switch (_b.label) {
1483
1572
  case 0:
1484
1573
  if (!operations.length) {
1485
1574
  return [2 /*return*/, null];
@@ -1496,334 +1585,348 @@ var MongoManagerCollection = /** @class */ (function () {
1496
1585
  }
1497
1586
  }
1498
1587
  _a = options || {}, session = _a.session, safeOptions = __rest(_a, ["session"]);
1499
- _f.label = 1;
1500
- case 1:
1501
- if (!(opIndex < operations.length)) return [3 /*break*/, 27];
1502
- batchOps = operations.slice(opIndex, opIndex + BATCH_SIZE);
1503
- logs = [];
1504
- versionOps = [];
1505
- try {
1506
- for (batchOps_1 = (e_6 = void 0, __values(batchOps)), batchOps_1_1 = batchOps_1.next(); !batchOps_1_1.done; batchOps_1_1 = batchOps_1.next()) {
1507
- op = batchOps_1_1.value;
1508
- opType = Object.keys(op)[0];
1509
- doc = op[opType];
1510
- // Skip validation if bypassCheckSchema is true
1511
- if (this.checkSchema && !bypassCheckSchema) {
1512
- validationContext = this.simplschema.newContext();
1513
- isValid = void 0;
1514
- if (opType === 'insertOne') {
1515
- isValid = validationContext.validate(doc.document);
1516
- }
1517
- else if (opType === 'replaceOne') {
1518
- isValid = validationContext.validate(doc.replacement);
1519
- }
1520
- else if (opType === 'updateOne' || opType === 'updateMany') {
1521
- // For updates, validate the update modifier
1522
- isValid = validationContext.validate(doc.update, { modifier: true });
1523
- }
1524
- else if (opType === 'deleteOne' || opType === 'deleteMany') {
1525
- // No validation needed for deletes
1526
- isValid = true;
1527
- }
1528
- else {
1529
- throw new Error("Unsupported operation type: ".concat(opType));
1530
- }
1531
- if (!isValid) {
1532
- throw new Error("Schema validation failed for ".concat(opType, ": ").concat(JSON.stringify(validationContext.validationErrors(), null, 2)));
1533
- }
1534
- }
1535
- // Now proceed to process the operations
1536
- if (opType === 'insertOne') {
1537
- // If this.timestamps is true, set createdAt and updatedAt
1538
- if (this.timestamps) {
1539
- if (!doc.document.createdAt) {
1540
- doc.document.createdAt = new Date();
1588
+ _loop_2 = function () {
1589
+ var batchOps, logs, versionOps, batchOps_1, batchOps_1_1, op, opType, doc, validationContext, isValid, affectedDocs, versionFilters, versionInserts, _c, _d, monitor, err_13, bwOptions, affectedDocIds, pipeline, oldVersions, _e;
1590
+ var e_6, _f;
1591
+ return __generator(this, function (_g) {
1592
+ switch (_g.label) {
1593
+ case 0:
1594
+ batchOps = operations.slice(opIndex, opIndex + BATCH_SIZE);
1595
+ logs = [];
1596
+ versionOps = [];
1597
+ try {
1598
+ for (batchOps_1 = (e_6 = void 0, __values(batchOps)), batchOps_1_1 = batchOps_1.next(); !batchOps_1_1.done; batchOps_1_1 = batchOps_1.next()) {
1599
+ op = batchOps_1_1.value;
1600
+ opType = Object.keys(op)[0];
1601
+ doc = op[opType];
1602
+ // Skip validation if bypassCheckSchema is true
1603
+ if (this_2.checkSchema && !bypassCheckSchema) {
1604
+ validationContext = this_2.simplschema.newContext();
1605
+ isValid = void 0;
1606
+ if (opType === 'insertOne') {
1607
+ isValid = validationContext.validate(doc.document);
1608
+ }
1609
+ else if (opType === 'replaceOne') {
1610
+ isValid = validationContext.validate(doc.replacement);
1611
+ }
1612
+ else if (opType === 'updateOne' || opType === 'updateMany') {
1613
+ // For updates, validate the update modifier
1614
+ isValid = validationContext.validate(doc.update, { modifier: true });
1615
+ }
1616
+ else if (opType === 'deleteOne' || opType === 'deleteMany') {
1617
+ // No validation needed for deletes
1618
+ isValid = true;
1619
+ }
1620
+ else {
1621
+ throw new Error("Unsupported operation type: ".concat(opType));
1622
+ }
1623
+ if (!isValid) {
1624
+ throw new Error("Schema validation failed for ".concat(opType, ": ").concat(JSON.stringify(validationContext.validationErrors(), null, 2)));
1625
+ }
1626
+ }
1627
+ // Now proceed to process the operations
1628
+ if (opType === 'insertOne') {
1629
+ // If this.timestamps is true, set createdAt and updatedAt
1630
+ if (this_2.timestamps) {
1631
+ if (!doc.document.createdAt) {
1632
+ doc.document.createdAt = new Date();
1633
+ }
1634
+ if (!doc.document.updatedAt) {
1635
+ doc.document.updatedAt = new Date();
1636
+ }
1637
+ }
1638
+ // Prepare log entry
1639
+ if (!bypassLogs && this_2.createLogs) {
1640
+ logs.push({
1641
+ _id: (0, common_1.objectIdHexString)(),
1642
+ type: 'document',
1643
+ collection: this_2.collectionName,
1644
+ id_document: doc.document._id || (0, common_1.objectIdHexString)(),
1645
+ payload: (0, common_1.getBinarySize)(JSON.stringify(doc.document)) < 1000000
1646
+ ? JSON.stringify(doc.document, null, 2)
1647
+ : 'Too Big',
1648
+ method: 'insertOne',
1649
+ id_user: '', // Add user info if available
1650
+ user: '',
1651
+ messageId: 0,
1652
+ route: '',
1653
+ createdAt: new Date(),
1654
+ });
1655
+ }
1656
+ // Since we might have modified doc.document, update the operation
1657
+ op[opType].document = doc.document;
1658
+ }
1659
+ else if (opType === 'updateOne' || opType === 'updateMany') {
1660
+ // If this.timestamps is true, add updatedAt
1661
+ if (this_2.timestamps) {
1662
+ if (!doc.update.$set) {
1663
+ doc.update.$set = {};
1664
+ }
1665
+ doc.update.$set.updatedAt = new Date();
1666
+ }
1667
+ // If this.useVersions is true, increment __v
1668
+ if (!bypassVersions && this_2.useVersions) {
1669
+ if (!doc.update.$inc) {
1670
+ doc.update.$inc = {};
1671
+ }
1672
+ doc.update.$inc.__v = 1;
1673
+ }
1674
+ // Prepare log entry
1675
+ if (!bypassLogs && this_2.createLogs) {
1676
+ logs.push({
1677
+ _id: (0, common_1.objectIdHexString)(),
1678
+ type: 'document',
1679
+ collection: this_2.collectionName,
1680
+ id_document: doc.filter._id || null,
1681
+ payload: (0, common_1.getBinarySize)(JSON.stringify(doc.update)) < 1000000
1682
+ ? JSON.stringify(doc.update, null, 2)
1683
+ : 'Too Big',
1684
+ method: opType,
1685
+ id_user: '', // Add user info if available
1686
+ user: '',
1687
+ messageId: 0,
1688
+ route: '',
1689
+ instance_index: process.env.NODE_APP_INSTANCE || '0',
1690
+ createdAt: new Date()
1691
+ });
1692
+ }
1693
+ // For versioning, store the filter to fetch the document later
1694
+ if (!bypassVersions && this_2.useVersions) {
1695
+ versionOps.push({
1696
+ filter: doc.filter,
1697
+ });
1698
+ }
1699
+ // No need to modify the operation further
1700
+ }
1701
+ else if (opType === 'replaceOne') {
1702
+ // If this.timestamps is true, set updatedAt
1703
+ if (this_2.timestamps) {
1704
+ doc.replacement.updatedAt = new Date();
1705
+ }
1706
+ // If this.useVersions is true, increment __v
1707
+ if (!bypassVersions && this_2.useVersions) {
1708
+ doc.replacement.__v = (doc.replacement.__v || 0) + 1;
1709
+ }
1710
+ // Prepare log entry
1711
+ if (!bypassLogs && this_2.createLogs) {
1712
+ logs.push({
1713
+ _id: (0, common_1.objectIdHexString)(),
1714
+ type: 'document',
1715
+ collection: this_2.collectionName,
1716
+ id_document: doc.filter._id || null,
1717
+ payload: (0, common_1.getBinarySize)(JSON.stringify(doc.replacement)) < 1000000
1718
+ ? JSON.stringify(doc.replacement, null, 2)
1719
+ : 'Too Big',
1720
+ method: 'replaceOne',
1721
+ id_user: '', // Add user info if available
1722
+ user: '',
1723
+ messageId: 0,
1724
+ route: '',
1725
+ createdAt: new Date(),
1726
+ });
1727
+ }
1728
+ // For versioning, store the filter to fetch the document later
1729
+ if (!bypassVersions && this_2.useVersions) {
1730
+ versionOps.push({
1731
+ filter: doc.filter,
1732
+ });
1733
+ }
1734
+ // Since we might have modified doc.replacement, update the operation
1735
+ op[opType].replacement = doc.replacement;
1736
+ }
1737
+ else if (opType === 'deleteOne' || opType === 'deleteMany') {
1738
+ // Prepare log entry
1739
+ if (!bypassLogs && this_2.createLogs) {
1740
+ logs.push({
1741
+ _id: (0, common_1.objectIdHexString)(),
1742
+ type: 'document',
1743
+ collection: this_2.collectionName,
1744
+ id_document: doc.filter._id || null,
1745
+ payload: (0, common_1.getBinarySize)(JSON.stringify(doc.filter)) < 1000000
1746
+ ? JSON.stringify(doc.filter, null, 2)
1747
+ : 'Too Big',
1748
+ method: opType,
1749
+ id_user: '', // Add user info if available
1750
+ user: '',
1751
+ messageId: 0,
1752
+ route: '',
1753
+ createdAt: new Date(),
1754
+ });
1755
+ }
1756
+ // For versioning, store the filter to fetch the document later
1757
+ if (!bypassVersions && this_2.useVersions) {
1758
+ versionOps.push({
1759
+ filter: doc.filter,
1760
+ });
1761
+ }
1762
+ // No need to modify the operation
1763
+ }
1764
+ else {
1765
+ throw new Error("Unsupported operation type: ".concat(opType));
1766
+ }
1767
+ }
1541
1768
  }
1542
- if (!doc.document.updatedAt) {
1543
- doc.document.updatedAt = new Date();
1769
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
1770
+ finally {
1771
+ try {
1772
+ if (batchOps_1_1 && !batchOps_1_1.done && (_f = batchOps_1.return)) _f.call(batchOps_1);
1773
+ }
1774
+ finally { if (e_6) throw e_6.error; }
1544
1775
  }
1545
- }
1546
- // Prepare log entry
1547
- if (!bypassLogs && this.createLogs) {
1548
- logs.push({
1549
- _id: (0, common_1.objectIdHexString)(),
1550
- type: 'document',
1551
- collection: this.collectionName,
1552
- id_document: doc.document._id || (0, common_1.objectIdHexString)(),
1553
- payload: (0, common_1.getBinarySize)(JSON.stringify(doc.document)) < 1000000
1554
- ? JSON.stringify(doc.document, null, 2)
1555
- : 'Too Big',
1556
- method: 'insertOne',
1557
- id_user: '', // Add user info if available
1558
- user: '',
1559
- messageId: 0,
1560
- route: '',
1561
- createdAt: new Date(),
1562
- });
1563
- }
1564
- // Since we might have modified doc.document, update the operation
1565
- op[opType].document = doc.document;
1566
- }
1567
- else if (opType === 'updateOne' || opType === 'updateMany') {
1568
- // If this.timestamps is true, add updatedAt
1569
- if (this.timestamps) {
1570
- if (!doc.update.$set) {
1571
- doc.update.$set = {};
1572
- }
1573
- doc.update.$set.updatedAt = new Date();
1574
- }
1575
- // If this.useVersions is true, increment __v
1576
- if (!bypassVersions && this.useVersions) {
1577
- if (!doc.update.$inc) {
1578
- doc.update.$inc = {};
1776
+ affectedDocs = [];
1777
+ if (!(!bypassVersions && this_2.useVersions && versionOps.length > 0)) return [3 /*break*/, 9];
1778
+ _g.label = 1;
1779
+ case 1:
1780
+ _g.trys.push([1, 8, , 9]);
1781
+ versionFilters = versionOps.map(function (v) { return v.filter; });
1782
+ if (!versionFilters.length) return [3 /*break*/, 3];
1783
+ return [4 /*yield*/, resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this_2.collectionName).find({ $or: versionFilters }, { session: session }).toArray()];
1784
+ case 2:
1785
+ affectedDocs = _g.sent();
1786
+ _g.label = 3;
1787
+ case 3:
1788
+ versionInserts = affectedDocs.map(function (doc) { return ({
1789
+ insertOne: {
1790
+ document: __assign(__assign({}, doc), { _id: { _id: doc._id, __v: doc.__v } }),
1791
+ },
1792
+ }); });
1793
+ if (!(versionInserts.length > 0)) return [3 /*break*/, 7];
1794
+ _g.label = 4;
1795
+ case 4:
1796
+ _g.trys.push([4, 6, , 7]);
1797
+ return [4 /*yield*/, resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this_2.versionCollection).bulkWrite(versionInserts)];
1798
+ case 5:
1799
+ _g.sent();
1800
+ return [3 /*break*/, 7];
1801
+ case 6:
1802
+ _c = _g.sent();
1803
+ return [3 /*break*/, 7];
1804
+ case 7: return [3 /*break*/, 9];
1805
+ case 8:
1806
+ _d = _g.sent();
1807
+ return [3 /*break*/, 9];
1808
+ case 9:
1809
+ monitor = monitor_manager_1.MonitorMongo.create('bulkWrite', this_2.collectionName, JSON.stringify([batchOps, safeOptions]));
1810
+ _g.label = 10;
1811
+ case 10:
1812
+ _g.trys.push([10, 12, 13, 15]);
1813
+ return [4 /*yield*/, this_2.retryWrite(function () { return resolveio_server_app_1.ResolveIOServer.getMainDB().collection(_this.collectionName).bulkWrite(batchOps, options || undefined); }, options)];
1814
+ case 11:
1815
+ _g.sent();
1816
+ return [3 /*break*/, 15];
1817
+ case 12:
1818
+ err_13 = _g.sent();
1819
+ console.log(JSON.stringify([new Date(), 'Error Bulk Write', this_2.collectionName, operations, safeOptions, {
1820
+ code: err_13.code,
1821
+ codeName: err_13.codeName,
1822
+ message: err_13.message,
1823
+ stack: err_13.stack
1824
+ }], null, 2));
1825
+ err_13.message = "Error in Bulk Write: ".concat(this_2.collectionName, " => ").concat(err_13.codeName || 'NoCodeName', " => ").concat(err_13.message);
1826
+ throw err_13;
1827
+ case 13: return [4 /*yield*/, monitor.finish()];
1828
+ case 14:
1829
+ _g.sent();
1830
+ return [7 /*endfinally*/];
1831
+ case 15:
1832
+ if (!(!bypassLogs && this_2.createLogs && logs.length > 0)) return [3 /*break*/, 18];
1833
+ if (!resolveio_server_app_1.ResolveIOServer.shouldWriteLogsOffline()) return [3 /*break*/, 16];
1834
+ resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLogs(logs.map(function (a) {
1835
+ return {
1836
+ type: 'log',
1837
+ data: a
1838
+ };
1839
+ }));
1840
+ return [3 /*break*/, 18];
1841
+ case 16:
1842
+ bwOptions = null;
1843
+ bwOptions = { session: session, ordered: false };
1844
+ return [4 /*yield*/, resolveio_server_app_1.ResolveIOServer.getMainDB().collection('logs').bulkWrite(logs.map(function (a) {
1845
+ a.client = 'ResolveIO';
1846
+ a.instance = resolveio_server_app_1.ResolveIOServer.getInstanceHost();
1847
+ return {
1848
+ insertOne: {
1849
+ document: a
1850
+ }
1851
+ };
1852
+ }), bwOptions)];
1853
+ case 17:
1854
+ _g.sent();
1855
+ _g.label = 18;
1856
+ case 18:
1857
+ if (!(!bypassVersions && this_2.useVersions)) return [3 /*break*/, 24];
1858
+ _g.label = 19;
1859
+ case 19:
1860
+ _g.trys.push([19, 23, , 24]);
1861
+ affectedDocIds = affectedDocs.map(function (doc) { return doc._id; });
1862
+ pipeline = [
1863
+ {
1864
+ $match: { '_id._id': { $in: affectedDocIds } },
1865
+ },
1866
+ {
1867
+ $sort: { '_id._id': 1, '_id.__v': -1 },
1868
+ },
1869
+ {
1870
+ $group: {
1871
+ _id: '$_id._id',
1872
+ versions: { $push: '$_id' },
1873
+ },
1874
+ },
1875
+ {
1876
+ $project: {
1877
+ versionsToDelete: { $slice: ['$versions', 5, { $size: '$versions' }] },
1878
+ },
1879
+ },
1880
+ { $unwind: '$versionsToDelete' },
1881
+ {
1882
+ $replaceRoot: { newRoot: { _id: '$versionsToDelete' } },
1883
+ },
1884
+ ];
1885
+ return [4 /*yield*/, resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this_2.versionCollection).aggregate(pipeline).toArray()];
1886
+ case 20:
1887
+ oldVersions = _g.sent();
1888
+ if (!(oldVersions.length > 0)) return [3 /*break*/, 22];
1889
+ return [4 /*yield*/, resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this_2.versionCollection).deleteMany({ _id: { $in: oldVersions.map(function (v) { return v._id; }) } })];
1890
+ case 21:
1891
+ _g.sent();
1892
+ _g.label = 22;
1893
+ case 22: return [3 /*break*/, 24];
1894
+ case 23:
1895
+ _e = _g.sent();
1896
+ return [3 /*break*/, 24];
1897
+ case 24:
1898
+ opIndex += BATCH_SIZE;
1899
+ // Yield to the event loop
1900
+ // eslint-disable-next-line no-restricted-syntax
1901
+ return [4 /*yield*/, new Promise(function (resolve) { return setImmediate(resolve); })];
1902
+ case 25:
1903
+ // Yield to the event loop
1904
+ // eslint-disable-next-line no-restricted-syntax
1905
+ _g.sent();
1906
+ // Invalidate cache
1907
+ if (!this_2.skipCache) {
1908
+ resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this_2.collectionName);
1579
1909
  }
1580
- doc.update.$inc.__v = 1;
1581
- }
1582
- // Prepare log entry
1583
- if (!bypassLogs && this.createLogs) {
1584
- logs.push({
1585
- _id: (0, common_1.objectIdHexString)(),
1586
- type: 'document',
1587
- collection: this.collectionName,
1588
- id_document: doc.filter._id || null,
1589
- payload: (0, common_1.getBinarySize)(JSON.stringify(doc.update)) < 1000000
1590
- ? JSON.stringify(doc.update, null, 2)
1591
- : 'Too Big',
1592
- method: opType,
1593
- id_user: '', // Add user info if available
1594
- user: '',
1595
- messageId: 0,
1596
- route: '',
1597
- instance_index: process.env.NODE_APP_INSTANCE || '0',
1598
- createdAt: new Date()
1599
- });
1600
- }
1601
- // For versioning, store the filter to fetch the document later
1602
- if (!bypassVersions && this.useVersions) {
1603
- versionOps.push({
1604
- filter: doc.filter,
1605
- });
1606
- }
1607
- // No need to modify the operation further
1608
- }
1609
- else if (opType === 'replaceOne') {
1610
- // If this.timestamps is true, set updatedAt
1611
- if (this.timestamps) {
1612
- doc.replacement.updatedAt = new Date();
1613
- }
1614
- // If this.useVersions is true, increment __v
1615
- if (!bypassVersions && this.useVersions) {
1616
- doc.replacement.__v = (doc.replacement.__v || 0) + 1;
1617
- }
1618
- // Prepare log entry
1619
- if (!bypassLogs && this.createLogs) {
1620
- logs.push({
1621
- _id: (0, common_1.objectIdHexString)(),
1622
- type: 'document',
1623
- collection: this.collectionName,
1624
- id_document: doc.filter._id || null,
1625
- payload: (0, common_1.getBinarySize)(JSON.stringify(doc.replacement)) < 1000000
1626
- ? JSON.stringify(doc.replacement, null, 2)
1627
- : 'Too Big',
1628
- method: 'replaceOne',
1629
- id_user: '', // Add user info if available
1630
- user: '',
1631
- messageId: 0,
1632
- route: '',
1633
- createdAt: new Date(),
1634
- });
1635
- }
1636
- // For versioning, store the filter to fetch the document later
1637
- if (!bypassVersions && this.useVersions) {
1638
- versionOps.push({
1639
- filter: doc.filter,
1640
- });
1641
- }
1642
- // Since we might have modified doc.replacement, update the operation
1643
- op[opType].replacement = doc.replacement;
1644
- }
1645
- else if (opType === 'deleteOne' || opType === 'deleteMany') {
1646
- // Prepare log entry
1647
- if (!bypassLogs && this.createLogs) {
1648
- logs.push({
1649
- _id: (0, common_1.objectIdHexString)(),
1650
- type: 'document',
1651
- collection: this.collectionName,
1652
- id_document: doc.filter._id || null,
1653
- payload: (0, common_1.getBinarySize)(JSON.stringify(doc.filter)) < 1000000
1654
- ? JSON.stringify(doc.filter, null, 2)
1655
- : 'Too Big',
1656
- method: opType,
1657
- id_user: '', // Add user info if available
1658
- user: '',
1659
- messageId: 0,
1660
- route: '',
1661
- createdAt: new Date(),
1662
- });
1663
- }
1664
- // For versioning, store the filter to fetch the document later
1665
- if (!bypassVersions && this.useVersions) {
1666
- versionOps.push({
1667
- filter: doc.filter,
1668
- });
1669
- }
1670
- // No need to modify the operation
1671
- }
1672
- else {
1673
- throw new Error("Unsupported operation type: ".concat(opType));
1910
+ return [2 /*return*/];
1674
1911
  }
1675
- }
1676
- }
1677
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
1678
- finally {
1679
- try {
1680
- if (batchOps_1_1 && !batchOps_1_1.done && (_e = batchOps_1.return)) _e.call(batchOps_1);
1681
- }
1682
- finally { if (e_6) throw e_6.error; }
1683
- }
1684
- affectedDocs = [];
1685
- if (!(!bypassVersions && this.useVersions && versionOps.length > 0)) return [3 /*break*/, 10];
1686
- _f.label = 2;
1912
+ });
1913
+ };
1914
+ this_2 = this;
1915
+ _b.label = 1;
1916
+ case 1:
1917
+ if (!(opIndex < operations.length)) return [3 /*break*/, 3];
1918
+ return [5 /*yield**/, _loop_2()];
1687
1919
  case 2:
1688
- _f.trys.push([2, 9, , 10]);
1689
- versionFilters = versionOps.map(function (v) { return v.filter; });
1690
- if (!versionFilters.length) return [3 /*break*/, 4];
1691
- return [4 /*yield*/, resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName).find({ $or: versionFilters }, { session: session }).toArray()];
1692
- case 3:
1693
- affectedDocs = _f.sent();
1694
- _f.label = 4;
1695
- case 4:
1696
- versionInserts = affectedDocs.map(function (doc) { return ({
1697
- insertOne: {
1698
- document: __assign(__assign({}, doc), { _id: { _id: doc._id, __v: doc.__v } }),
1699
- },
1700
- }); });
1701
- if (!(versionInserts.length > 0)) return [3 /*break*/, 8];
1702
- _f.label = 5;
1703
- case 5:
1704
- _f.trys.push([5, 7, , 8]);
1705
- return [4 /*yield*/, resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.versionCollection).bulkWrite(versionInserts)];
1706
- case 6:
1707
- _f.sent();
1708
- return [3 /*break*/, 8];
1709
- case 7:
1710
- _b = _f.sent();
1711
- return [3 /*break*/, 8];
1712
- case 8: return [3 /*break*/, 10];
1713
- case 9:
1714
- _c = _f.sent();
1715
- return [3 /*break*/, 10];
1716
- case 10:
1717
- monitor = monitor_manager_1.MonitorMongo.create('bulkWrite', this.collectionName, JSON.stringify([batchOps, safeOptions]));
1718
- _f.label = 11;
1719
- case 11:
1720
- _f.trys.push([11, 13, 14, 16]);
1721
- return [4 /*yield*/, resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName).bulkWrite(batchOps, options || undefined)];
1722
- case 12:
1723
- _f.sent();
1724
- return [3 /*break*/, 16];
1725
- case 13:
1726
- err_12 = _f.sent();
1727
- console.log(JSON.stringify([new Date(), 'Error Bulk Write', this.collectionName, operations, safeOptions, {
1728
- code: err_12.code,
1729
- codeName: err_12.codeName,
1730
- message: err_12.message,
1731
- stack: err_12.stack
1732
- }], null, 2));
1733
- err_12.message = "Error in Bulk Write: ".concat(this.collectionName, " => ").concat(err_12.codeName || 'NoCodeName', " => ").concat(err_12.message);
1734
- throw err_12;
1735
- case 14: return [4 /*yield*/, monitor.finish()];
1736
- case 15:
1737
- _f.sent();
1738
- return [7 /*endfinally*/];
1739
- case 16:
1740
- if (!(!bypassLogs && this.createLogs && logs.length > 0)) return [3 /*break*/, 19];
1741
- if (!resolveio_server_app_1.ResolveIOServer.shouldWriteLogsOffline()) return [3 /*break*/, 17];
1742
- resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLogs(logs.map(function (a) {
1743
- return {
1744
- type: 'log',
1745
- data: a
1746
- };
1747
- }));
1748
- return [3 /*break*/, 19];
1749
- case 17:
1750
- bwOptions = null;
1751
- bwOptions = { session: session, ordered: false };
1752
- return [4 /*yield*/, resolveio_server_app_1.ResolveIOServer.getMainDB().collection('logs').bulkWrite(logs.map(function (a) {
1753
- a.client = 'ResolveIO';
1754
- a.instance = resolveio_server_app_1.ResolveIOServer.getInstanceHost();
1755
- return {
1756
- insertOne: {
1757
- document: a
1758
- }
1759
- };
1760
- }), bwOptions)];
1761
- case 18:
1762
- _f.sent();
1763
- _f.label = 19;
1764
- case 19:
1765
- if (!(!bypassVersions && this.useVersions)) return [3 /*break*/, 25];
1766
- _f.label = 20;
1767
- case 20:
1768
- _f.trys.push([20, 24, , 25]);
1769
- affectedDocIds = affectedDocs.map(function (doc) { return doc._id; });
1770
- pipeline = [
1771
- {
1772
- $match: { '_id._id': { $in: affectedDocIds } },
1773
- },
1774
- {
1775
- $sort: { '_id._id': 1, '_id.__v': -1 },
1776
- },
1777
- {
1778
- $group: {
1779
- _id: '$_id._id',
1780
- versions: { $push: '$_id' },
1781
- },
1782
- },
1783
- {
1784
- $project: {
1785
- versionsToDelete: { $slice: ['$versions', 5, { $size: '$versions' }] },
1786
- },
1787
- },
1788
- { $unwind: '$versionsToDelete' },
1789
- {
1790
- $replaceRoot: { newRoot: { _id: '$versionsToDelete' } },
1791
- },
1792
- ];
1793
- return [4 /*yield*/, resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.versionCollection).aggregate(pipeline).toArray()];
1794
- case 21:
1795
- oldVersions = _f.sent();
1796
- if (!(oldVersions.length > 0)) return [3 /*break*/, 23];
1797
- return [4 /*yield*/, resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.versionCollection).deleteMany({ _id: { $in: oldVersions.map(function (v) { return v._id; }) } })];
1798
- case 22:
1799
- _f.sent();
1800
- _f.label = 23;
1801
- case 23: return [3 /*break*/, 25];
1802
- case 24:
1803
- _d = _f.sent();
1804
- return [3 /*break*/, 25];
1805
- case 25:
1806
- opIndex += BATCH_SIZE;
1807
- // Yield to the event loop
1808
- // eslint-disable-next-line no-restricted-syntax
1809
- return [4 /*yield*/, new Promise(function (resolve) { return setImmediate(resolve); })];
1810
- case 26:
1811
- // Yield to the event loop
1812
- // eslint-disable-next-line no-restricted-syntax
1813
- _f.sent();
1814
- // Invalidate cache
1815
- if (!this.skipCache) {
1816
- resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName);
1817
- }
1920
+ _b.sent();
1818
1921
  return [3 /*break*/, 1];
1819
- case 27: return [2 /*return*/, { ok: 1 }]; // Adjust return value as per your requirements
1922
+ case 3: return [2 /*return*/, { ok: 1 }]; // Adjust return value as per your requirements
1820
1923
  }
1821
1924
  });
1822
1925
  });
1823
1926
  };
1824
1927
  MongoManagerCollection.prototype.countDocuments = function () {
1825
1928
  return __awaiter(this, arguments, void 0, function (filter, options, skipCache) {
1826
- var _a, session, safeOptions, isUnfiltered, monitor, res, err_13;
1929
+ var _a, session, safeOptions, isUnfiltered, monitor, res, err_14;
1827
1930
  if (filter === void 0) { filter = {}; }
1828
1931
  if (skipCache === void 0) { skipCache = false; }
1829
1932
  return __generator(this, function (_b) {
@@ -1848,15 +1951,15 @@ var MongoManagerCollection = /** @class */ (function () {
1848
1951
  res = _b.sent();
1849
1952
  return [2 /*return*/, res];
1850
1953
  case 5:
1851
- err_13 = _b.sent();
1954
+ err_14 = _b.sent();
1852
1955
  console.log(JSON.stringify([new Date(), 'Error Count Documents', this.collectionName, filter, safeOptions, {
1853
- code: err_13.code,
1854
- codeName: err_13.codeName,
1855
- message: err_13.message,
1856
- stack: err_13.stack
1956
+ code: err_14.code,
1957
+ codeName: err_14.codeName,
1958
+ message: err_14.message,
1959
+ stack: err_14.stack
1857
1960
  }], null, 2));
1858
- err_13.message = "Error in Count Documents: ".concat(this.collectionName, " => ").concat(err_13.codeName || 'NoCodeName', " => ").concat(err_13.message);
1859
- throw err_13;
1961
+ err_14.message = "Error in Count Documents: ".concat(this.collectionName, " => ").concat(err_14.codeName || 'NoCodeName', " => ").concat(err_14.message);
1962
+ throw err_14;
1860
1963
  case 6: return [4 /*yield*/, monitor.finish()];
1861
1964
  case 7:
1862
1965
  _b.sent();
@@ -1882,7 +1985,7 @@ var MongoManagerCollection = /** @class */ (function () {
1882
1985
  return __generator(this, function (_b) {
1883
1986
  switch (_b.label) {
1884
1987
  case 0:
1885
- _b.trys.push([0, 2, , 3]);
1988
+ _b.trys.push([0, 3, , 4]);
1886
1989
  dbCollection = resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions);
1887
1990
  return [4 /*yield*/, dbCollection.indexes()];
1888
1991
  case 1:
@@ -1893,11 +1996,12 @@ var MongoManagerCollection = /** @class */ (function () {
1893
1996
  if (indexExists) {
1894
1997
  return [2 /*return*/, 'Index already exists']; // Skip creation if index exists
1895
1998
  }
1896
- return [2 /*return*/, dbCollection.createIndex(fieldOrSpec, options || undefined)];
1897
- case 2:
1999
+ return [4 /*yield*/, dbCollection.createIndex(fieldOrSpec, options || undefined)];
2000
+ case 2: return [2 /*return*/, _b.sent()];
2001
+ case 3:
1898
2002
  _a = _b.sent();
1899
- return [3 /*break*/, 3];
1900
- case 3: return [2 /*return*/, 'Index creation queued'];
2003
+ return [3 /*break*/, 4];
2004
+ case 4: return [2 /*return*/, 'Index creation queued'];
1901
2005
  }
1902
2006
  });
1903
2007
  });
@@ -1908,7 +2012,7 @@ var MongoManagerCollection = /** @class */ (function () {
1908
2012
  return __generator(this, function (_b) {
1909
2013
  switch (_b.label) {
1910
2014
  case 0:
1911
- _b.trys.push([0, 2, , 3]);
2015
+ _b.trys.push([0, 3, , 4]);
1912
2016
  dbCollection = resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions);
1913
2017
  return [4 /*yield*/, dbCollection.indexes()];
1914
2018
  case 1:
@@ -1922,18 +2026,19 @@ var MongoManagerCollection = /** @class */ (function () {
1922
2026
  if (indexesToCreate.length === 0) {
1923
2027
  return [2 /*return*/, ['All indexes already exist']];
1924
2028
  }
1925
- return [2 /*return*/, dbCollection.createIndexes(indexSpecs, options || undefined)];
1926
- case 2:
2029
+ return [4 /*yield*/, dbCollection.createIndexes(indexesToCreate, options || undefined)];
2030
+ case 2: return [2 /*return*/, _b.sent()];
2031
+ case 3:
1927
2032
  _a = _b.sent();
1928
- return [3 /*break*/, 3];
1929
- case 3: return [2 /*return*/, ['Index creation queued']];
2033
+ return [3 /*break*/, 4];
2034
+ case 4: return [2 /*return*/, ['Index creation queued']];
1930
2035
  }
1931
2036
  });
1932
2037
  });
1933
2038
  };
1934
2039
  MongoManagerCollection.prototype.deleteMany = function () {
1935
2040
  return __awaiter(this, arguments, void 0, function (filter, options, bypassLogs, bypassSession) {
1936
- var mongoSession, _a, session, safeOptions, docs, i, doc, versionDoc, _b, monitor, res, err_14;
2041
+ var mongoSession, _a, session, safeOptions, docs, i, doc, versionDoc, _b, monitor, res, err_15;
1937
2042
  if (filter === void 0) { filter = {}; }
1938
2043
  if (bypassLogs === void 0) { bypassLogs = false; }
1939
2044
  if (bypassSession === void 0) { bypassSession = false; }
@@ -2031,15 +2136,15 @@ var MongoManagerCollection = /** @class */ (function () {
2031
2136
  }
2032
2137
  return [2 /*return*/, res];
2033
2138
  case 14:
2034
- err_14 = _c.sent();
2139
+ err_15 = _c.sent();
2035
2140
  console.log(JSON.stringify([new Date(), 'Error Delete Many', this.collectionName, filter, safeOptions, {
2036
- code: err_14.code,
2037
- codeName: err_14.codeName,
2038
- message: err_14.message,
2039
- stack: err_14.stack
2141
+ code: err_15.code,
2142
+ codeName: err_15.codeName,
2143
+ message: err_15.message,
2144
+ stack: err_15.stack
2040
2145
  }], null, 2));
2041
- err_14.message = "Error in Delete Many: ".concat(this.collectionName, " => ").concat(err_14.codeName || 'NoCodeName', " => ").concat(err_14.message);
2042
- throw err_14;
2146
+ err_15.message = "Error in Delete Many: ".concat(this.collectionName, " => ").concat(err_15.codeName || 'NoCodeName', " => ").concat(err_15.message);
2147
+ throw err_15;
2043
2148
  case 15: return [4 /*yield*/, monitor.finish()];
2044
2149
  case 16:
2045
2150
  _c.sent();
@@ -2051,7 +2156,7 @@ var MongoManagerCollection = /** @class */ (function () {
2051
2156
  };
2052
2157
  MongoManagerCollection.prototype.deleteOne = function () {
2053
2158
  return __awaiter(this, arguments, void 0, function (filter, options, bypassLogs, bypassSession) {
2054
- var mongoSession, _a, session, safeOptions, monitor, res, doc, versionDoc, _b, err_15, monitor, res, err_16;
2159
+ var mongoSession, _a, session, safeOptions, monitor, res, doc, versionDoc, _b, err_16, monitor, res, err_17;
2055
2160
  if (filter === void 0) { filter = {}; }
2056
2161
  if (bypassLogs === void 0) { bypassLogs = false; }
2057
2162
  if (bypassSession === void 0) { bypassSession = false; }
@@ -2143,15 +2248,15 @@ var MongoManagerCollection = /** @class */ (function () {
2143
2248
  _c.label = 12;
2144
2249
  case 12: return [2 /*return*/, res];
2145
2250
  case 13:
2146
- err_15 = _c.sent();
2251
+ err_16 = _c.sent();
2147
2252
  console.log(JSON.stringify([new Date(), 'Error Delete One (Find One And Delete)', this.collectionName, filter, safeOptions, {
2148
- code: err_15.code,
2149
- codeName: err_15.codeName,
2150
- message: err_15.message,
2151
- stack: err_15.stack
2253
+ code: err_16.code,
2254
+ codeName: err_16.codeName,
2255
+ message: err_16.message,
2256
+ stack: err_16.stack
2152
2257
  }], null, 2));
2153
- err_15.message = "Error in Delete One (Find One And Delete): ".concat(this.collectionName, " => ").concat(err_15.codeName || 'NoCodeName', " => ").concat(err_15.message);
2154
- throw err_15;
2258
+ err_16.message = "Error in Delete One (Find One And Delete): ".concat(this.collectionName, " => ").concat(err_16.codeName || 'NoCodeName', " => ").concat(err_16.message);
2259
+ throw err_16;
2155
2260
  case 14: return [4 /*yield*/, monitor.finish()];
2156
2261
  case 15:
2157
2262
  _c.sent();
@@ -2170,15 +2275,15 @@ var MongoManagerCollection = /** @class */ (function () {
2170
2275
  }
2171
2276
  return [2 /*return*/, res];
2172
2277
  case 20:
2173
- err_16 = _c.sent();
2278
+ err_17 = _c.sent();
2174
2279
  console.log(JSON.stringify([new Date(), 'Error Delete One', this.collectionName, filter, safeOptions, {
2175
- code: err_16.code,
2176
- codeName: err_16.codeName,
2177
- message: err_16.message,
2178
- stack: err_16.stack
2280
+ code: err_17.code,
2281
+ codeName: err_17.codeName,
2282
+ message: err_17.message,
2283
+ stack: err_17.stack
2179
2284
  }], null, 2));
2180
- err_16.message = "Error in Delete One: ".concat(this.collectionName, " => ").concat(err_16.codeName || 'NoCodeName', " => ").concat(err_16.message);
2181
- throw err_16;
2285
+ err_17.message = "Error in Delete One: ".concat(this.collectionName, " => ").concat(err_17.codeName || 'NoCodeName', " => ").concat(err_17.message);
2286
+ throw err_17;
2182
2287
  case 21: return [4 /*yield*/, monitor.finish()];
2183
2288
  case 22:
2184
2289
  _c.sent();
@@ -2190,7 +2295,7 @@ var MongoManagerCollection = /** @class */ (function () {
2190
2295
  };
2191
2296
  MongoManagerCollection.prototype.distinct = function (key_1, filter_1, options_1) {
2192
2297
  return __awaiter(this, arguments, void 0, function (key, filter, options, bypassSession) {
2193
- var mongoSession, _a, session, safeOptions, monitor, res, err_17;
2298
+ var mongoSession, _a, session, safeOptions, monitor, res, err_18;
2194
2299
  if (bypassSession === void 0) { bypassSession = false; }
2195
2300
  return __generator(this, function (_b) {
2196
2301
  switch (_b.label) {
@@ -2216,15 +2321,15 @@ var MongoManagerCollection = /** @class */ (function () {
2216
2321
  res = _b.sent();
2217
2322
  return [2 /*return*/, res];
2218
2323
  case 3:
2219
- err_17 = _b.sent();
2324
+ err_18 = _b.sent();
2220
2325
  console.log(JSON.stringify([new Date(), 'Error Distinct', this.collectionName, filter, safeOptions, {
2221
- code: err_17.code,
2222
- codeName: err_17.codeName,
2223
- message: err_17.message,
2224
- stack: err_17.stack
2326
+ code: err_18.code,
2327
+ codeName: err_18.codeName,
2328
+ message: err_18.message,
2329
+ stack: err_18.stack
2225
2330
  }], null, 2));
2226
- err_17.message = "Error in Distinct: ".concat(this.collectionName, " => ").concat(err_17.codeName || 'NoCodeName', " => ").concat(err_17.message);
2227
- throw err_17;
2331
+ err_18.message = "Error in Distinct: ".concat(this.collectionName, " => ").concat(err_18.codeName || 'NoCodeName', " => ").concat(err_18.message);
2332
+ throw err_18;
2228
2333
  case 4: return [4 /*yield*/, monitor.finish()];
2229
2334
  case 5:
2230
2335
  _b.sent();
@@ -2236,7 +2341,7 @@ var MongoManagerCollection = /** @class */ (function () {
2236
2341
  };
2237
2342
  MongoManagerCollection.prototype.drop = function (options_1) {
2238
2343
  return __awaiter(this, arguments, void 0, function (options, bypassSession) {
2239
- var mongoSession, _a, session, safeOptions, monitor, res, err_18;
2344
+ var mongoSession, _a, session, safeOptions, monitor, res, err_19;
2240
2345
  if (bypassSession === void 0) { bypassSession = false; }
2241
2346
  return __generator(this, function (_b) {
2242
2347
  switch (_b.label) {
@@ -2262,15 +2367,15 @@ var MongoManagerCollection = /** @class */ (function () {
2262
2367
  res = _b.sent();
2263
2368
  return [2 /*return*/, res];
2264
2369
  case 3:
2265
- err_18 = _b.sent();
2370
+ err_19 = _b.sent();
2266
2371
  console.log(JSON.stringify([new Date(), 'Error Drop Collection', this.collectionName, safeOptions, {
2267
- code: err_18.code,
2268
- codeName: err_18.codeName,
2269
- message: err_18.message,
2270
- stack: err_18.stack
2372
+ code: err_19.code,
2373
+ codeName: err_19.codeName,
2374
+ message: err_19.message,
2375
+ stack: err_19.stack
2271
2376
  }], null, 2));
2272
- err_18.message = "Error in Drop Collection: ".concat(this.collectionName, " => ").concat(err_18.codeName || 'NoCodeName', " => ").concat(err_18.message);
2273
- throw err_18;
2377
+ err_19.message = "Error in Drop Collection: ".concat(this.collectionName, " => ").concat(err_19.codeName || 'NoCodeName', " => ").concat(err_19.message);
2378
+ throw err_19;
2274
2379
  case 4: return [4 /*yield*/, monitor.finish()];
2275
2380
  case 5:
2276
2381
  _b.sent();
@@ -2282,7 +2387,7 @@ var MongoManagerCollection = /** @class */ (function () {
2282
2387
  };
2283
2388
  MongoManagerCollection.prototype.dropIndex = function (indexName_1, options_1) {
2284
2389
  return __awaiter(this, arguments, void 0, function (indexName, options, bypassSession) {
2285
- var mongoSession, _a, session, safeOptions, monitor, res, err_19;
2390
+ var mongoSession, _a, session, safeOptions, monitor, res, err_20;
2286
2391
  if (bypassSession === void 0) { bypassSession = false; }
2287
2392
  return __generator(this, function (_b) {
2288
2393
  switch (_b.label) {
@@ -2308,15 +2413,15 @@ var MongoManagerCollection = /** @class */ (function () {
2308
2413
  res = _b.sent();
2309
2414
  return [2 /*return*/, res];
2310
2415
  case 3:
2311
- err_19 = _b.sent();
2416
+ err_20 = _b.sent();
2312
2417
  console.log(JSON.stringify([new Date(), 'Error Drop Index', this.collectionName, indexName, safeOptions, {
2313
- code: err_19.code,
2314
- codeName: err_19.codeName,
2315
- message: err_19.message,
2316
- stack: err_19.stack
2418
+ code: err_20.code,
2419
+ codeName: err_20.codeName,
2420
+ message: err_20.message,
2421
+ stack: err_20.stack
2317
2422
  }], null, 2));
2318
- err_19.message = "Error in Drop Index: ".concat(this.collectionName, " => ").concat(err_19.codeName || 'NoCodeName', " => ").concat(err_19.message);
2319
- throw err_19;
2423
+ err_20.message = "Error in Drop Index: ".concat(this.collectionName, " => ").concat(err_20.codeName || 'NoCodeName', " => ").concat(err_20.message);
2424
+ throw err_20;
2320
2425
  case 4: return [4 /*yield*/, monitor.finish()];
2321
2426
  case 5:
2322
2427
  _b.sent();
@@ -2328,7 +2433,7 @@ var MongoManagerCollection = /** @class */ (function () {
2328
2433
  };
2329
2434
  MongoManagerCollection.prototype.dropIndexes = function (options_1) {
2330
2435
  return __awaiter(this, arguments, void 0, function (options, bypassSession) {
2331
- var mongoSession, _a, session, safeOptions, monitor, res, err_20;
2436
+ var mongoSession, _a, session, safeOptions, monitor, res, err_21;
2332
2437
  if (bypassSession === void 0) { bypassSession = false; }
2333
2438
  return __generator(this, function (_b) {
2334
2439
  switch (_b.label) {
@@ -2354,15 +2459,15 @@ var MongoManagerCollection = /** @class */ (function () {
2354
2459
  res = _b.sent();
2355
2460
  return [2 /*return*/, res];
2356
2461
  case 3:
2357
- err_20 = _b.sent();
2462
+ err_21 = _b.sent();
2358
2463
  console.log(JSON.stringify([new Date(), 'Error Drop Indexes', this.collectionName, safeOptions, {
2359
- code: err_20.code,
2360
- codeName: err_20.codeName,
2361
- message: err_20.message,
2362
- stack: err_20.stack
2464
+ code: err_21.code,
2465
+ codeName: err_21.codeName,
2466
+ message: err_21.message,
2467
+ stack: err_21.stack
2363
2468
  }], null, 2));
2364
- err_20.message = "Error in Drop Indexes: ".concat(this.collectionName, " => ").concat(err_20.codeName || 'NoCodeName', " => ").concat(err_20.message);
2365
- throw err_20;
2469
+ err_21.message = "Error in Drop Indexes: ".concat(this.collectionName, " => ").concat(err_21.codeName || 'NoCodeName', " => ").concat(err_21.message);
2470
+ throw err_21;
2366
2471
  case 4: return [4 /*yield*/, monitor.finish()];
2367
2472
  case 5:
2368
2473
  _b.sent();
@@ -2374,7 +2479,7 @@ var MongoManagerCollection = /** @class */ (function () {
2374
2479
  };
2375
2480
  MongoManagerCollection.prototype.find = function () {
2376
2481
  return __awaiter(this, arguments, void 0, function (filter, options, skipCache, bypassSession) {
2377
- var mongoSession, _a, session, safeOptions, monitor, res, err_21;
2482
+ var mongoSession, _a, session, safeOptions, monitor, res, err_22;
2378
2483
  if (filter === void 0) { filter = {}; }
2379
2484
  if (skipCache === void 0) { skipCache = false; }
2380
2485
  if (bypassSession === void 0) { bypassSession = false; }
@@ -2406,15 +2511,15 @@ var MongoManagerCollection = /** @class */ (function () {
2406
2511
  (0, subscription_dependency_context_1.recordDependencyResult)(this.collectionName, res, { filter: filter, meta: buildQueryMetaFromFindOptions(safeOptions) });
2407
2512
  return [2 /*return*/, res];
2408
2513
  case 4:
2409
- err_21 = _b.sent();
2514
+ err_22 = _b.sent();
2410
2515
  console.log(JSON.stringify([new Date(), 'Error Find', this.collectionName, filter, safeOptions, {
2411
- code: err_21.code,
2412
- codeName: err_21.codeName,
2413
- message: err_21.message,
2414
- stack: err_21.stack
2516
+ code: err_22.code,
2517
+ codeName: err_22.codeName,
2518
+ message: err_22.message,
2519
+ stack: err_22.stack
2415
2520
  }], null, 2));
2416
- err_21.message = "Error in Find: ".concat(this.collectionName, " => ").concat(err_21.codeName || 'NoCodeName', " => ").concat(err_21.message);
2417
- throw err_21;
2521
+ err_22.message = "Error in Find: ".concat(this.collectionName, " => ").concat(err_22.codeName || 'NoCodeName', " => ").concat(err_22.message);
2522
+ throw err_22;
2418
2523
  case 5: return [4 /*yield*/, monitor.finish()];
2419
2524
  case 6:
2420
2525
  _b.sent();
@@ -2426,7 +2531,7 @@ var MongoManagerCollection = /** @class */ (function () {
2426
2531
  };
2427
2532
  MongoManagerCollection.prototype.findById = function (id_1, options_1) {
2428
2533
  return __awaiter(this, arguments, void 0, function (id, options, skipCache, bypassSession) {
2429
- var mongoSession, _a, session, safeOptions, monitor, res, err_22;
2534
+ var mongoSession, _a, session, safeOptions, monitor, res, err_23;
2430
2535
  if (skipCache === void 0) { skipCache = false; }
2431
2536
  if (bypassSession === void 0) { bypassSession = false; }
2432
2537
  return __generator(this, function (_b) {
@@ -2457,15 +2562,15 @@ var MongoManagerCollection = /** @class */ (function () {
2457
2562
  (0, subscription_dependency_context_1.recordDependencyResult)(this.collectionName, res, { filter: { _id: id }, meta: buildQueryMetaFromFindOptions(safeOptions) });
2458
2563
  return [2 /*return*/, res];
2459
2564
  case 4:
2460
- err_22 = _b.sent();
2565
+ err_23 = _b.sent();
2461
2566
  console.log(JSON.stringify([new Date(), 'Error Find By ID', this.collectionName, id, safeOptions, {
2462
- code: err_22.code,
2463
- codeName: err_22.codeName,
2464
- message: err_22.message,
2465
- stack: err_22.stack
2567
+ code: err_23.code,
2568
+ codeName: err_23.codeName,
2569
+ message: err_23.message,
2570
+ stack: err_23.stack
2466
2571
  }], null, 2));
2467
- err_22.message = "Error in Find By ID: ".concat(this.collectionName, " => ").concat(err_22.codeName || 'NoCodeName', " => ").concat(err_22.message);
2468
- throw err_22;
2572
+ err_23.message = "Error in Find By ID: ".concat(this.collectionName, " => ").concat(err_23.codeName || 'NoCodeName', " => ").concat(err_23.message);
2573
+ throw err_23;
2469
2574
  case 5: return [4 /*yield*/, monitor.finish()];
2470
2575
  case 6:
2471
2576
  _b.sent();
@@ -2477,7 +2582,7 @@ var MongoManagerCollection = /** @class */ (function () {
2477
2582
  };
2478
2583
  MongoManagerCollection.prototype.findCount = function () {
2479
2584
  return __awaiter(this, arguments, void 0, function (filter, options, bypassSession) {
2480
- var mongoSession, _a, session, safeOptions, monitor, res, err_23;
2585
+ var mongoSession, _a, session, safeOptions, monitor, res, err_24;
2481
2586
  if (filter === void 0) { filter = {}; }
2482
2587
  if (bypassSession === void 0) { bypassSession = false; }
2483
2588
  return __generator(this, function (_b) {
@@ -2505,15 +2610,15 @@ var MongoManagerCollection = /** @class */ (function () {
2505
2610
  (0, subscription_dependency_context_1.recordDependencyResult)(this.collectionName, res, { filter: filter });
2506
2611
  return [2 /*return*/, res];
2507
2612
  case 3:
2508
- err_23 = _b.sent();
2613
+ err_24 = _b.sent();
2509
2614
  console.log(JSON.stringify([new Date(), 'Error Find Count', this.collectionName, filter, safeOptions, {
2510
- code: err_23.code,
2511
- codeName: err_23.codeName,
2512
- message: err_23.message,
2513
- stack: err_23.stack
2615
+ code: err_24.code,
2616
+ codeName: err_24.codeName,
2617
+ message: err_24.message,
2618
+ stack: err_24.stack
2514
2619
  }], null, 2));
2515
- err_23.message = "Error in Find Count: ".concat(this.collectionName, " => ").concat(err_23.codeName || 'NoCodeName', " => ").concat(err_23.message);
2516
- throw err_23;
2620
+ err_24.message = "Error in Find Count: ".concat(this.collectionName, " => ").concat(err_24.codeName || 'NoCodeName', " => ").concat(err_24.message);
2621
+ throw err_24;
2517
2622
  case 4: return [4 /*yield*/, monitor.finish()];
2518
2623
  case 5:
2519
2624
  _b.sent();
@@ -2583,7 +2688,7 @@ var MongoManagerCollection = /** @class */ (function () {
2583
2688
  };
2584
2689
  MongoManagerCollection.prototype.findOne = function () {
2585
2690
  return __awaiter(this, arguments, void 0, function (filter, options, skipCache, bypassSession) {
2586
- var mongoSession, _a, session, safeOptions, monitor, res, err_24;
2691
+ var mongoSession, _a, session, safeOptions, monitor, res, err_25;
2587
2692
  if (filter === void 0) { filter = {}; }
2588
2693
  if (skipCache === void 0) { skipCache = false; }
2589
2694
  if (bypassSession === void 0) { bypassSession = false; }
@@ -2615,15 +2720,15 @@ var MongoManagerCollection = /** @class */ (function () {
2615
2720
  (0, subscription_dependency_context_1.recordDependencyResult)(this.collectionName, res, { filter: filter });
2616
2721
  return [2 /*return*/, res];
2617
2722
  case 4:
2618
- err_24 = _b.sent();
2723
+ err_25 = _b.sent();
2619
2724
  console.log(JSON.stringify([new Date(), 'Error Find One', this.collectionName, filter, safeOptions, {
2620
- code: err_24.code,
2621
- codeName: err_24.codeName,
2622
- message: err_24.message,
2623
- stack: err_24.stack
2725
+ code: err_25.code,
2726
+ codeName: err_25.codeName,
2727
+ message: err_25.message,
2728
+ stack: err_25.stack
2624
2729
  }], null, 2));
2625
- err_24.message = "Error in Find One: ".concat(this.collectionName, " => ").concat(err_24.codeName || 'NoCodeName', " => ").concat(err_24.message);
2626
- throw err_24;
2730
+ err_25.message = "Error in Find One: ".concat(this.collectionName, " => ").concat(err_25.codeName || 'NoCodeName', " => ").concat(err_25.message);
2731
+ throw err_25;
2627
2732
  case 5: return [4 /*yield*/, monitor.finish()];
2628
2733
  case 6:
2629
2734
  _b.sent();
@@ -2635,7 +2740,7 @@ var MongoManagerCollection = /** @class */ (function () {
2635
2740
  };
2636
2741
  MongoManagerCollection.prototype.findOneAndDelete = function () {
2637
2742
  return __awaiter(this, arguments, void 0, function (filter, options, bypassLogs, bypassSession) {
2638
- var mongoSession, _a, session, safeOptions, monitor, doc, versionDoc, _b, err_25;
2743
+ var mongoSession, _a, session, safeOptions, monitor, doc, versionDoc, _b, err_26;
2639
2744
  if (filter === void 0) { filter = {}; }
2640
2745
  if (bypassLogs === void 0) { bypassLogs = false; }
2641
2746
  if (bypassSession === void 0) { bypassSession = false; }
@@ -2721,15 +2826,15 @@ var MongoManagerCollection = /** @class */ (function () {
2721
2826
  case 10: return [2 /*return*/, null];
2722
2827
  case 11: return [3 /*break*/, 15];
2723
2828
  case 12:
2724
- err_25 = _c.sent();
2829
+ err_26 = _c.sent();
2725
2830
  console.log(JSON.stringify([new Date(), 'Error Find One And Delete', this.collectionName, filter, safeOptions, {
2726
- code: err_25.code,
2727
- codeName: err_25.codeName,
2728
- message: err_25.message,
2729
- stack: err_25.stack
2831
+ code: err_26.code,
2832
+ codeName: err_26.codeName,
2833
+ message: err_26.message,
2834
+ stack: err_26.stack
2730
2835
  }], null, 2));
2731
- err_25.message = "Error in Find One And Delete: ".concat(this.collectionName, " => ").concat(err_25.codeName || 'NoCodeName', " => ").concat(err_25.message);
2732
- throw err_25;
2836
+ err_26.message = "Error in Find One And Delete: ".concat(this.collectionName, " => ").concat(err_26.codeName || 'NoCodeName', " => ").concat(err_26.message);
2837
+ throw err_26;
2733
2838
  case 13: return [4 /*yield*/, monitor.finish()];
2734
2839
  case 14:
2735
2840
  _c.sent();
@@ -2741,7 +2846,7 @@ var MongoManagerCollection = /** @class */ (function () {
2741
2846
  };
2742
2847
  MongoManagerCollection.prototype.findOneAndReplace = function () {
2743
2848
  return __awaiter(this, arguments, void 0, function (filter, replacement, options, bypassLogs, bypassCheckSchema, bypassSession) {
2744
- var validation, isValid, date, mongoSession, _a, session, safeOptions, monitor, doc, versionDoc, _b, err_26;
2849
+ var validation, isValid, date, mongoSession, _a, session, safeOptions, monitor, doc, versionDoc, _b, err_27;
2745
2850
  if (filter === void 0) { filter = {}; }
2746
2851
  if (bypassLogs === void 0) { bypassLogs = false; }
2747
2852
  if (bypassCheckSchema === void 0) { bypassCheckSchema = false; }
@@ -2853,15 +2958,15 @@ var MongoManagerCollection = /** @class */ (function () {
2853
2958
  case 12: return [2 /*return*/, null];
2854
2959
  case 13: return [3 /*break*/, 17];
2855
2960
  case 14:
2856
- err_26 = _c.sent();
2961
+ err_27 = _c.sent();
2857
2962
  console.log(JSON.stringify([new Date(), 'Error Find One And Replace', this.collectionName, filter, safeOptions, {
2858
- code: err_26.code,
2859
- codeName: err_26.codeName,
2860
- message: err_26.message,
2861
- stack: err_26.stack
2963
+ code: err_27.code,
2964
+ codeName: err_27.codeName,
2965
+ message: err_27.message,
2966
+ stack: err_27.stack
2862
2967
  }], null, 2));
2863
- err_26.message = "Error in Find One And Replace: ".concat(this.collectionName, " => ").concat(err_26.codeName || 'NoCodeName', " => ").concat(err_26.message);
2864
- throw err_26;
2968
+ err_27.message = "Error in Find One And Replace: ".concat(this.collectionName, " => ").concat(err_27.codeName || 'NoCodeName', " => ").concat(err_27.message);
2969
+ throw err_27;
2865
2970
  case 15: return [4 /*yield*/, monitor.finish()];
2866
2971
  case 16:
2867
2972
  _c.sent();
@@ -2873,7 +2978,7 @@ var MongoManagerCollection = /** @class */ (function () {
2873
2978
  };
2874
2979
  MongoManagerCollection.prototype.findOneAndUpdate = function () {
2875
2980
  return __awaiter(this, arguments, void 0, function (filter, update, options, bypassLogs, bypassCheckSchema, bypassSession) {
2876
- var validation, isValid, date, mongoSession, _a, session, safeOptions, monitor, doc, versionDoc, _b, err_27;
2981
+ var validation, isValid, date, mongoSession, _a, session, safeOptions, monitor, doc, versionDoc, _b, err_28;
2877
2982
  if (filter === void 0) { filter = {}; }
2878
2983
  if (bypassLogs === void 0) { bypassLogs = false; }
2879
2984
  if (bypassCheckSchema === void 0) { bypassCheckSchema = false; }
@@ -3002,15 +3107,15 @@ var MongoManagerCollection = /** @class */ (function () {
3002
3107
  case 12: return [2 /*return*/, null];
3003
3108
  case 13: return [3 /*break*/, 17];
3004
3109
  case 14:
3005
- err_27 = _c.sent();
3110
+ err_28 = _c.sent();
3006
3111
  console.log(JSON.stringify([new Date(), 'Error Find One And Update', this.collectionName, filter, safeOptions, {
3007
- code: err_27.code,
3008
- codeName: err_27.codeName,
3009
- message: err_27.message,
3010
- stack: err_27.stack
3112
+ code: err_28.code,
3113
+ codeName: err_28.codeName,
3114
+ message: err_28.message,
3115
+ stack: err_28.stack
3011
3116
  }], null, 2));
3012
- err_27.message = "Error in Find One And Update: ".concat(this.collectionName, " => ").concat(err_27.codeName || 'NoCodeName', " => ").concat(err_27.message);
3013
- throw err_27;
3117
+ err_28.message = "Error in Find One And Update: ".concat(this.collectionName, " => ").concat(err_28.codeName || 'NoCodeName', " => ").concat(err_28.message);
3118
+ throw err_28;
3014
3119
  case 15: return [4 /*yield*/, monitor.finish()];
3015
3120
  case 16:
3016
3121
  _c.sent();
@@ -3022,7 +3127,7 @@ var MongoManagerCollection = /** @class */ (function () {
3022
3127
  };
3023
3128
  MongoManagerCollection.prototype.indexes = function (options) {
3024
3129
  return __awaiter(this, void 0, void 0, function () {
3025
- var _a, session, safeOptions, monitor, res, err_28;
3130
+ var _a, session, safeOptions, monitor, res, err_29;
3026
3131
  return __generator(this, function (_b) {
3027
3132
  switch (_b.label) {
3028
3133
  case 0:
@@ -3036,15 +3141,15 @@ var MongoManagerCollection = /** @class */ (function () {
3036
3141
  res = _b.sent();
3037
3142
  return [2 /*return*/, res];
3038
3143
  case 3:
3039
- err_28 = _b.sent();
3144
+ err_29 = _b.sent();
3040
3145
  console.log(JSON.stringify([new Date(), 'Error Indexes', this.collectionName, safeOptions, {
3041
- code: err_28.code,
3042
- codeName: err_28.codeName,
3043
- message: err_28.message,
3044
- stack: err_28.stack
3146
+ code: err_29.code,
3147
+ codeName: err_29.codeName,
3148
+ message: err_29.message,
3149
+ stack: err_29.stack
3045
3150
  }], null, 2));
3046
- err_28.message = "Error in Indexes: ".concat(this.collectionName, " => ").concat(err_28.codeName || 'NoCodeName', " => ").concat(err_28.message);
3047
- throw err_28;
3151
+ err_29.message = "Error in Indexes: ".concat(this.collectionName, " => ").concat(err_29.codeName || 'NoCodeName', " => ").concat(err_29.message);
3152
+ throw err_29;
3048
3153
  case 4: return [4 /*yield*/, monitor.finish()];
3049
3154
  case 5:
3050
3155
  _b.sent();
@@ -3056,7 +3161,7 @@ var MongoManagerCollection = /** @class */ (function () {
3056
3161
  };
3057
3162
  MongoManagerCollection.prototype.indexExists = function (indexes, options) {
3058
3163
  return __awaiter(this, void 0, void 0, function () {
3059
- var _a, session, safeOptions, monitor, res, err_29;
3164
+ var _a, session, safeOptions, monitor, res, err_30;
3060
3165
  return __generator(this, function (_b) {
3061
3166
  switch (_b.label) {
3062
3167
  case 0:
@@ -3070,15 +3175,15 @@ var MongoManagerCollection = /** @class */ (function () {
3070
3175
  res = _b.sent();
3071
3176
  return [2 /*return*/, res];
3072
3177
  case 3:
3073
- err_29 = _b.sent();
3178
+ err_30 = _b.sent();
3074
3179
  console.log(JSON.stringify([new Date(), 'Error Index Exists', this.collectionName, indexes, safeOptions, {
3075
- code: err_29.code,
3076
- codeName: err_29.codeName,
3077
- message: err_29.message,
3078
- stack: err_29.stack
3180
+ code: err_30.code,
3181
+ codeName: err_30.codeName,
3182
+ message: err_30.message,
3183
+ stack: err_30.stack
3079
3184
  }], null, 2));
3080
- err_29.message = "Error in Index Exists: ".concat(this.collectionName, " => ").concat(err_29.codeName || 'NoCodeName', " => ").concat(err_29.message);
3081
- throw err_29;
3185
+ err_30.message = "Error in Index Exists: ".concat(this.collectionName, " => ").concat(err_30.codeName || 'NoCodeName', " => ").concat(err_30.message);
3186
+ throw err_30;
3082
3187
  case 4: return [4 /*yield*/, monitor.finish()];
3083
3188
  case 5:
3084
3189
  _b.sent();
@@ -3090,7 +3195,7 @@ var MongoManagerCollection = /** @class */ (function () {
3090
3195
  };
3091
3196
  MongoManagerCollection.prototype.insertMany = function (docs_1, options_1) {
3092
3197
  return __awaiter(this, arguments, void 0, function (docs, options, bypassLogs, bypassCheckSchema, bypassMonitor, bypassSession) {
3093
- var validationResults, docs_2, docs_2_1, doc, validation, isValid, e_7_1, validDocs, mongoSession, _a, session, safeOptions, i, doc, date, monitor, err_30;
3198
+ var validationResults, docs_2, docs_2_1, doc, validation, isValid, e_7_1, validDocs, mongoSession, _a, session, safeOptions, i, doc, date, monitor, err_31;
3094
3199
  var e_7, _b;
3095
3200
  var _this = this;
3096
3201
  if (bypassLogs === void 0) { bypassLogs = false; }
@@ -3230,15 +3335,15 @@ var MongoManagerCollection = /** @class */ (function () {
3230
3335
  }
3231
3336
  return [2 /*return*/, validDocs];
3232
3337
  case 17:
3233
- err_30 = _c.sent();
3338
+ err_31 = _c.sent();
3234
3339
  console.log(JSON.stringify([new Date(), 'Error Insert Many', this.collectionName, docs, safeOptions, {
3235
- code: err_30.code,
3236
- codeName: err_30.codeName,
3237
- message: err_30.message,
3238
- stack: err_30.stack
3340
+ code: err_31.code,
3341
+ codeName: err_31.codeName,
3342
+ message: err_31.message,
3343
+ stack: err_31.stack
3239
3344
  }], null, 2));
3240
- err_30.message = "Error in Insert Many: ".concat(this.collectionName, " => ").concat(err_30.codeName || 'NoCodeName', " => ").concat(err_30.message);
3241
- throw err_30;
3345
+ err_31.message = "Error in Insert Many: ".concat(this.collectionName, " => ").concat(err_31.codeName || 'NoCodeName', " => ").concat(err_31.message);
3346
+ throw err_31;
3242
3347
  case 18:
3243
3348
  if (monitor) {
3244
3349
  monitor.finish();
@@ -3251,7 +3356,7 @@ var MongoManagerCollection = /** @class */ (function () {
3251
3356
  };
3252
3357
  MongoManagerCollection.prototype.insertOne = function (doc_1, options_1) {
3253
3358
  return __awaiter(this, arguments, void 0, function (doc, options, bypassLogs, bypassCheckSchema, bypassSession) {
3254
- var validation, isValid, date, mongoSession, _a, session, safeOptions, monitor, err_31;
3359
+ var validation, isValid, date, mongoSession, _a, session, safeOptions, monitor, err_32;
3255
3360
  var _this = this;
3256
3361
  if (bypassLogs === void 0) { bypassLogs = false; }
3257
3362
  if (bypassCheckSchema === void 0) { bypassCheckSchema = false; }
@@ -3346,15 +3451,15 @@ var MongoManagerCollection = /** @class */ (function () {
3346
3451
  }
3347
3452
  return [2 /*return*/, doc];
3348
3453
  case 8:
3349
- err_31 = _b.sent();
3454
+ err_32 = _b.sent();
3350
3455
  console.log(JSON.stringify([new Date(), 'Error Insert One', this.collectionName, doc, safeOptions, {
3351
- code: err_31.code,
3352
- codeName: err_31.codeName,
3353
- message: err_31.message,
3354
- stack: err_31.stack
3456
+ code: err_32.code,
3457
+ codeName: err_32.codeName,
3458
+ message: err_32.message,
3459
+ stack: err_32.stack
3355
3460
  }], null, 2));
3356
- err_31.message = "Error in Insert One: ".concat(this.collectionName, " => ").concat(err_31.codeName || 'NoCodeName', " => ").concat(err_31.message);
3357
- throw err_31;
3461
+ err_32.message = "Error in Insert One: ".concat(this.collectionName, " => ").concat(err_32.codeName || 'NoCodeName', " => ").concat(err_32.message);
3462
+ throw err_32;
3358
3463
  case 9: return [4 /*yield*/, monitor.finish()];
3359
3464
  case 10:
3360
3465
  _b.sent();
@@ -3394,7 +3499,7 @@ var MongoManagerCollection = /** @class */ (function () {
3394
3499
  };
3395
3500
  MongoManagerCollection.prototype.rename = function (newName_1, options_1) {
3396
3501
  return __awaiter(this, arguments, void 0, function (newName, options, bypassSession) {
3397
- var mongoSession, _a, session, safeOptions, monitor, res, err_32;
3502
+ var mongoSession, _a, session, safeOptions, monitor, res, err_33;
3398
3503
  if (bypassSession === void 0) { bypassSession = false; }
3399
3504
  return __generator(this, function (_b) {
3400
3505
  switch (_b.label) {
@@ -3420,15 +3525,15 @@ var MongoManagerCollection = /** @class */ (function () {
3420
3525
  res = _b.sent();
3421
3526
  return [2 /*return*/, res];
3422
3527
  case 3:
3423
- err_32 = _b.sent();
3528
+ err_33 = _b.sent();
3424
3529
  console.log(JSON.stringify([new Date(), 'Error Rename', this.collectionName, newName, safeOptions, {
3425
- code: err_32.code,
3426
- codeName: err_32.codeName,
3427
- message: err_32.message,
3428
- stack: err_32.stack
3530
+ code: err_33.code,
3531
+ codeName: err_33.codeName,
3532
+ message: err_33.message,
3533
+ stack: err_33.stack
3429
3534
  }], null, 2));
3430
- err_32.message = "Error in Rename: ".concat(this.collectionName, " => ").concat(err_32.codeName || 'NoCodeName', " => ").concat(err_32.message);
3431
- throw err_32;
3535
+ err_33.message = "Error in Rename: ".concat(this.collectionName, " => ").concat(err_33.codeName || 'NoCodeName', " => ").concat(err_33.message);
3536
+ throw err_33;
3432
3537
  case 4: return [4 /*yield*/, monitor.finish()];
3433
3538
  case 5:
3434
3539
  _b.sent();
@@ -3440,7 +3545,7 @@ var MongoManagerCollection = /** @class */ (function () {
3440
3545
  };
3441
3546
  MongoManagerCollection.prototype.replaceOne = function (filter_1, replacement_1, options_1) {
3442
3547
  return __awaiter(this, arguments, void 0, function (filter, replacement, options, bypassLogs, bypassCheckSchema, doc, bypassSession) {
3443
- var validation, isValid, date, mongoSession, _a, session, safeOptions, versionDoc, _b, monitor, res, err_33, prevDoc, docId, docVersion, updatedDoc_1, monitor, res, err_34, monitor, res, err_35, _c, session_1, safeOptions_1, monitor, res, doc_1, err_36, monitor, res, err_37;
3548
+ var validation, isValid, date, mongoSession, _a, session, safeOptions, versionDoc, _b, monitor, res, err_34, prevDoc, docId, docVersion, updatedDoc_1, monitor, res, err_35, monitor, res, err_36, _c, session_1, safeOptions_1, monitor, res, doc_1, err_37, monitor, res, err_38;
3444
3549
  var _this = this;
3445
3550
  if (bypassLogs === void 0) { bypassLogs = false; }
3446
3551
  if (bypassCheckSchema === void 0) { bypassCheckSchema = false; }
@@ -3560,15 +3665,15 @@ var MongoManagerCollection = /** @class */ (function () {
3560
3665
  }
3561
3666
  return [2 /*return*/, res];
3562
3667
  case 16:
3563
- err_33 = _d.sent();
3668
+ err_34 = _d.sent();
3564
3669
  console.log(JSON.stringify([new Date(), 'Error Replace One', this.collectionName, filter, replacement, safeOptions, {
3565
- code: err_33.code,
3566
- codeName: err_33.codeName,
3567
- message: err_33.message,
3568
- stack: err_33.stack
3670
+ code: err_34.code,
3671
+ codeName: err_34.codeName,
3672
+ message: err_34.message,
3673
+ stack: err_34.stack
3569
3674
  }], null, 2));
3570
- err_33.message = "Error in Replace One: ".concat(this.collectionName, " => ").concat(err_33.codeName || 'NoCodeName', " => ").concat(err_33.message);
3571
- throw err_33;
3675
+ err_34.message = "Error in Replace One: ".concat(this.collectionName, " => ").concat(err_34.codeName || 'NoCodeName', " => ").concat(err_34.message);
3676
+ throw err_34;
3572
3677
  case 17: return [4 /*yield*/, monitor.finish()];
3573
3678
  case 18:
3574
3679
  _d.sent();
@@ -3642,15 +3747,15 @@ var MongoManagerCollection = /** @class */ (function () {
3642
3747
  }
3643
3748
  return [2 /*return*/, res];
3644
3749
  case 27:
3645
- err_34 = _d.sent();
3750
+ err_35 = _d.sent();
3646
3751
  console.log(JSON.stringify([new Date(), 'Error Replace One', this.collectionName, filter, replacement, safeOptions, {
3647
- code: err_34.code,
3648
- codeName: err_34.codeName,
3649
- message: err_34.message,
3650
- stack: err_34.stack
3752
+ code: err_35.code,
3753
+ codeName: err_35.codeName,
3754
+ message: err_35.message,
3755
+ stack: err_35.stack
3651
3756
  }], null, 2));
3652
- err_34.message = "Error in Replace One: ".concat(this.collectionName, " => ").concat(err_34.codeName || 'NoCodeName', " => ").concat(err_34.message);
3653
- throw err_34;
3757
+ err_35.message = "Error in Replace One: ".concat(this.collectionName, " => ").concat(err_35.codeName || 'NoCodeName', " => ").concat(err_35.message);
3758
+ throw err_35;
3654
3759
  case 28: return [4 /*yield*/, monitor.finish()];
3655
3760
  case 29:
3656
3761
  _d.sent();
@@ -3719,15 +3824,15 @@ var MongoManagerCollection = /** @class */ (function () {
3719
3824
  }
3720
3825
  return [2 /*return*/, res];
3721
3826
  case 39:
3722
- err_35 = _d.sent();
3827
+ err_36 = _d.sent();
3723
3828
  console.log(JSON.stringify([new Date(), 'Error Replace One', this.collectionName, filter, replacement, safeOptions, {
3724
- code: err_35.code,
3725
- codeName: err_35.codeName,
3726
- message: err_35.message,
3727
- stack: err_35.stack
3829
+ code: err_36.code,
3830
+ codeName: err_36.codeName,
3831
+ message: err_36.message,
3832
+ stack: err_36.stack
3728
3833
  }], null, 2));
3729
- err_35.message = "Error in Replace One: ".concat(this.collectionName, " => ").concat(err_35.codeName || 'NoCodeName', " => ").concat(err_35.message);
3730
- throw err_35;
3834
+ err_36.message = "Error in Replace One: ".concat(this.collectionName, " => ").concat(err_36.codeName || 'NoCodeName', " => ").concat(err_36.message);
3835
+ throw err_36;
3731
3836
  case 40: return [4 /*yield*/, monitor.finish()];
3732
3837
  case 41:
3733
3838
  _d.sent();
@@ -3854,15 +3959,15 @@ var MongoManagerCollection = /** @class */ (function () {
3854
3959
  }
3855
3960
  return [2 /*return*/, res];
3856
3961
  case 56:
3857
- err_36 = _d.sent();
3962
+ err_37 = _d.sent();
3858
3963
  console.log(JSON.stringify([new Date(), 'Error Replace One (Find One And Replace)', this.collectionName, filter, safeOptions_1, {
3859
- code: err_36.code,
3860
- codeName: err_36.codeName,
3861
- message: err_36.message,
3862
- stack: err_36.stack
3964
+ code: err_37.code,
3965
+ codeName: err_37.codeName,
3966
+ message: err_37.message,
3967
+ stack: err_37.stack
3863
3968
  }], null, 2));
3864
- err_36.message = "Error in Replace One (Find One And Replace): ".concat(this.collectionName, " => ").concat(err_36.codeName || 'NoCodeName', " => ").concat(err_36.message);
3865
- throw err_36;
3969
+ err_37.message = "Error in Replace One (Find One And Replace): ".concat(this.collectionName, " => ").concat(err_37.codeName || 'NoCodeName', " => ").concat(err_37.message);
3970
+ throw err_37;
3866
3971
  case 57: return [4 /*yield*/, monitor.finish()];
3867
3972
  case 58:
3868
3973
  _d.sent();
@@ -3881,15 +3986,15 @@ var MongoManagerCollection = /** @class */ (function () {
3881
3986
  }
3882
3987
  return [2 /*return*/, res];
3883
3988
  case 63:
3884
- err_37 = _d.sent();
3989
+ err_38 = _d.sent();
3885
3990
  console.log(JSON.stringify([new Date(), 'Error Replace One', this.collectionName, filter, replacement, safeOptions, {
3886
- code: err_37.code,
3887
- codeName: err_37.codeName,
3888
- message: err_37.message,
3889
- stack: err_37.stack
3991
+ code: err_38.code,
3992
+ codeName: err_38.codeName,
3993
+ message: err_38.message,
3994
+ stack: err_38.stack
3890
3995
  }], null, 2));
3891
- err_37.message = "Error in Replace One: ".concat(this.collectionName, " => ").concat(err_37.codeName || 'NoCodeName', " => ").concat(err_37.message);
3892
- throw err_37;
3996
+ err_38.message = "Error in Replace One: ".concat(this.collectionName, " => ").concat(err_38.codeName || 'NoCodeName', " => ").concat(err_38.message);
3997
+ throw err_38;
3893
3998
  case 64: return [4 /*yield*/, monitor.finish()];
3894
3999
  case 65:
3895
4000
  _d.sent();
@@ -3901,7 +4006,7 @@ var MongoManagerCollection = /** @class */ (function () {
3901
4006
  };
3902
4007
  MongoManagerCollection.prototype.updateMany = function (filter_1, update_1, options_1) {
3903
4008
  return __awaiter(this, arguments, void 0, function (filter, update, options, bypassLogs, bypassCheckSchema, bypassVersions, bypassSession) {
3904
- var isEmptyUpdate, allEmpty, date, validation, isValid, mongoSession, _a, session, safeOptions, docs, i, doc, versionDoc, _b, monitor, res, err_38;
4009
+ var isEmptyUpdate, allEmpty, date, validation, isValid, mongoSession, _a, session, safeOptions, docs, i, doc, versionDoc, _b, monitor, res, err_39;
3905
4010
  var _this = this;
3906
4011
  if (bypassLogs === void 0) { bypassLogs = false; }
3907
4012
  if (bypassCheckSchema === void 0) { bypassCheckSchema = false; }
@@ -4059,15 +4164,15 @@ var MongoManagerCollection = /** @class */ (function () {
4059
4164
  }
4060
4165
  return [2 /*return*/, res];
4061
4166
  case 18:
4062
- err_38 = _c.sent();
4167
+ err_39 = _c.sent();
4063
4168
  console.log(JSON.stringify([new Date(), 'Error Update Many', this.collectionName, filter, update, safeOptions, {
4064
- code: err_38.code,
4065
- codeName: err_38.codeName,
4066
- message: err_38.message,
4067
- stack: err_38.stack
4169
+ code: err_39.code,
4170
+ codeName: err_39.codeName,
4171
+ message: err_39.message,
4172
+ stack: err_39.stack
4068
4173
  }], null, 2));
4069
- err_38.message = "Error in Update Many: ".concat(this.collectionName, " => ").concat(err_38.codeName || 'NoCodeName', " => ").concat(err_38.message);
4070
- throw err_38;
4174
+ err_39.message = "Error in Update Many: ".concat(this.collectionName, " => ").concat(err_39.codeName || 'NoCodeName', " => ").concat(err_39.message);
4175
+ throw err_39;
4071
4176
  case 19: return [4 /*yield*/, monitor.finish()];
4072
4177
  case 20:
4073
4178
  _c.sent();
@@ -4079,7 +4184,7 @@ var MongoManagerCollection = /** @class */ (function () {
4079
4184
  };
4080
4185
  MongoManagerCollection.prototype.updateOne = function (filter_1, update_1, options_1) {
4081
4186
  return __awaiter(this, arguments, void 0, function (filter, update, options, bypassLogs, bypassCheckSchema, bypassSession) {
4082
- var validation, isValid, isEmptyUpdate, allEmpty, date, mongoSession, _a, session, safeOptions, doc, versionDoc, _b, monitor, res, err_39, monitor, res, err_40, monitor, res, doc, err_41, monitor, res, err_42;
4187
+ var validation, isValid, isEmptyUpdate, allEmpty, date, mongoSession, _a, session, safeOptions, doc, versionDoc, _b, monitor, res, err_40, monitor, res, err_41, monitor, res, doc, err_42, monitor, res, err_43;
4083
4188
  var _this = this;
4084
4189
  if (bypassLogs === void 0) { bypassLogs = false; }
4085
4190
  if (bypassCheckSchema === void 0) { bypassCheckSchema = false; }
@@ -4228,15 +4333,15 @@ var MongoManagerCollection = /** @class */ (function () {
4228
4333
  }
4229
4334
  return [2 /*return*/, res];
4230
4335
  case 14:
4231
- err_39 = _c.sent();
4336
+ err_40 = _c.sent();
4232
4337
  console.log(JSON.stringify([new Date(), 'Error Update One', this.collectionName, filter, update, safeOptions, {
4233
- code: err_39.code,
4234
- codeName: err_39.codeName,
4235
- message: err_39.message,
4236
- stack: err_39.stack
4338
+ code: err_40.code,
4339
+ codeName: err_40.codeName,
4340
+ message: err_40.message,
4341
+ stack: err_40.stack
4237
4342
  }], null, 2));
4238
- err_39.message = "Error in Update One: ".concat(this.collectionName, " => ").concat(err_39.codeName || 'NoCodeName', " => ").concat(err_39.message);
4239
- throw err_39;
4343
+ err_40.message = "Error in Update One: ".concat(this.collectionName, " => ").concat(err_40.codeName || 'NoCodeName', " => ").concat(err_40.message);
4344
+ throw err_40;
4240
4345
  case 15: return [4 /*yield*/, monitor.finish()];
4241
4346
  case 16:
4242
4347
  _c.sent();
@@ -4319,15 +4424,15 @@ var MongoManagerCollection = /** @class */ (function () {
4319
4424
  }
4320
4425
  return [2 /*return*/, res];
4321
4426
  case 24:
4322
- err_40 = _c.sent();
4427
+ err_41 = _c.sent();
4323
4428
  console.log(JSON.stringify([new Date(), 'Error Update One', this.collectionName, filter, safeOptions, {
4324
- code: err_40.code,
4325
- codeName: err_40.codeName,
4326
- message: err_40.message,
4327
- stack: err_40.stack
4429
+ code: err_41.code,
4430
+ codeName: err_41.codeName,
4431
+ message: err_41.message,
4432
+ stack: err_41.stack
4328
4433
  }], null, 2));
4329
- err_40.message = "Error in Update One: ".concat(this.collectionName, " => ").concat(err_40.codeName || 'NoCodeName', " => ").concat(err_40.message);
4330
- throw err_40;
4434
+ err_41.message = "Error in Update One: ".concat(this.collectionName, " => ").concat(err_41.codeName || 'NoCodeName', " => ").concat(err_41.message);
4435
+ throw err_41;
4331
4436
  case 25: return [4 /*yield*/, monitor.finish()];
4332
4437
  case 26:
4333
4438
  _c.sent();
@@ -4469,15 +4574,15 @@ var MongoManagerCollection = /** @class */ (function () {
4469
4574
  _c.label = 41;
4470
4575
  case 41: return [2 /*return*/, res];
4471
4576
  case 42:
4472
- err_41 = _c.sent();
4577
+ err_42 = _c.sent();
4473
4578
  console.log(JSON.stringify([new Date(), 'Error Update One (Find One And Update)', this.collectionName, filter, safeOptions, {
4474
- code: err_41.code,
4475
- codeName: err_41.codeName,
4476
- message: err_41.message,
4477
- stack: err_41.stack
4579
+ code: err_42.code,
4580
+ codeName: err_42.codeName,
4581
+ message: err_42.message,
4582
+ stack: err_42.stack
4478
4583
  }], null, 2));
4479
- err_41.message = "Error in Update One (Find One And Update): ".concat(this.collectionName, " => ").concat(err_41.codeName || 'NoCodeName', " => ").concat(err_41.message);
4480
- throw err_41;
4584
+ err_42.message = "Error in Update One (Find One And Update): ".concat(this.collectionName, " => ").concat(err_42.codeName || 'NoCodeName', " => ").concat(err_42.message);
4585
+ throw err_42;
4481
4586
  case 43: return [4 /*yield*/, monitor.finish()];
4482
4587
  case 44:
4483
4588
  _c.sent();
@@ -4496,15 +4601,15 @@ var MongoManagerCollection = /** @class */ (function () {
4496
4601
  }
4497
4602
  return [2 /*return*/, res];
4498
4603
  case 49:
4499
- err_42 = _c.sent();
4604
+ err_43 = _c.sent();
4500
4605
  console.log(JSON.stringify([new Date(), 'Error Update One', this.collectionName, filter, update, safeOptions, {
4501
- code: err_42.code,
4502
- codeName: err_42.codeName,
4503
- message: err_42.message,
4504
- stack: err_42.stack
4606
+ code: err_43.code,
4607
+ codeName: err_43.codeName,
4608
+ message: err_43.message,
4609
+ stack: err_43.stack
4505
4610
  }], null, 2));
4506
- err_42.message = "Error in Update One: ".concat(this.collectionName, " => ").concat(err_42.codeName || 'NoCodeName', " => ").concat(err_42.message);
4507
- throw err_42;
4611
+ err_43.message = "Error in Update One: ".concat(this.collectionName, " => ").concat(err_43.codeName || 'NoCodeName', " => ").concat(err_43.message);
4612
+ throw err_43;
4508
4613
  case 50: return [4 /*yield*/, monitor.finish()];
4509
4614
  case 51:
4510
4615
  _c.sent();
@@ -4544,7 +4649,7 @@ var MongoManagerUserCollection = /** @class */ (function (_super) {
4544
4649
  };
4545
4650
  MongoManagerUserCollection.prototype.authenticate = function (user, password) {
4546
4651
  return __awaiter(this, void 0, void 0, function () {
4547
- var attemptsInterval, calculatedInterval, hashBuffer, err_43;
4652
+ var attemptsInterval, calculatedInterval, hashBuffer, err_44;
4548
4653
  return __generator(this, function (_a) {
4549
4654
  switch (_a.label) {
4550
4655
  case 0:
@@ -4618,9 +4723,9 @@ var MongoManagerUserCollection = /** @class */ (function (_super) {
4618
4723
  _a.label = 10;
4619
4724
  case 10: return [3 /*break*/, 12];
4620
4725
  case 11:
4621
- err_43 = _a.sent();
4622
- err_43.message = "Error in User Authenticate: ".concat(JSON.stringify(user, null, 2), " - ").concat(err_43.message);
4623
- throw err_43;
4726
+ err_44 = _a.sent();
4727
+ err_44.message = "Error in User Authenticate: ".concat(JSON.stringify(user, null, 2), " - ").concat(err_44.message);
4728
+ throw err_44;
4624
4729
  case 12: return [2 /*return*/];
4625
4730
  }
4626
4731
  });
@@ -4641,7 +4746,7 @@ var MongoManagerUserCollection = /** @class */ (function (_super) {
4641
4746
  };
4642
4747
  MongoManagerUserCollection.prototype.setPassword = function (user, password) {
4643
4748
  return __awaiter(this, void 0, void 0, function () {
4644
- var saltBuffer, salt, hashRaw, hash, err_44;
4749
+ var saltBuffer, salt, hashRaw, hash, err_45;
4645
4750
  return __generator(this, function (_a) {
4646
4751
  switch (_a.label) {
4647
4752
  case 0:
@@ -4667,9 +4772,9 @@ var MongoManagerUserCollection = /** @class */ (function (_super) {
4667
4772
  hash = Buffer.from(hashRaw, 'binary').toString('hex');
4668
4773
  return [2 /*return*/, user_collection_1.Users.updateOne({ _id: user._id }, { $set: { hash: hash, salt: salt, services: {}, attempts: 0 } })];
4669
4774
  case 6:
4670
- err_44 = _a.sent();
4671
- err_44.message = "Error in User Set Password: ".concat(JSON.stringify(user, null, 2), " - ").concat(err_44.message);
4672
- throw err_44;
4775
+ err_45 = _a.sent();
4776
+ err_45.message = "Error in User Set Password: ".concat(JSON.stringify(user, null, 2), " - ").concat(err_45.message);
4777
+ throw err_45;
4673
4778
  case 7: return [2 /*return*/];
4674
4779
  }
4675
4780
  });