@resolveio/server-lib 20.7.131 → 20.7.133

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.
@@ -85,9 +85,13 @@ export declare class MongoManager {
85
85
  private _serverCollections;
86
86
  private _isWorkersEnabled;
87
87
  private _isWorkerInstance;
88
+ private _watchedDatabases;
89
+ private _mainDatabaseName;
88
90
  constructor();
89
91
  static create(): Promise<MongoManager>;
90
92
  private initialize;
93
+ private resolveWatchedDatabases;
94
+ getWatchedDatabases(): string[];
91
95
  oneTimeTransaction<T>(fn: () => Promise<T>): Promise<T>;
92
96
  getSession(): any;
93
97
  private setCacheLimit;
@@ -72,6 +72,17 @@ var __rest = (this && this.__rest) || function (s, e) {
72
72
  }
73
73
  return t;
74
74
  };
75
+ var __values = (this && this.__values) || function(o) {
76
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
77
+ if (m) return m.call(o);
78
+ if (o && typeof o.length === "number") return {
79
+ next: function () {
80
+ if (o && i >= o.length) o = void 0;
81
+ return { value: o && o[i++], done: !o };
82
+ }
83
+ };
84
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
85
+ };
75
86
  var __read = (this && this.__read) || function (o, n) {
76
87
  var m = typeof Symbol === "function" && o[Symbol.iterator];
77
88
  if (!m) return o;
@@ -97,17 +108,6 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
97
108
  }
98
109
  return to.concat(ar || Array.prototype.slice.call(from));
99
110
  };
100
- var __values = (this && this.__values) || function(o) {
101
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
102
- if (m) return m.call(o);
103
- if (o && typeof o.length === "number") return {
104
- next: function () {
105
- if (o && i >= o.length) o = void 0;
106
- return { value: o && o[i++], done: !o };
107
- }
108
- };
109
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
110
- };
111
111
  Object.defineProperty(exports, "__esModule", { value: true });
112
112
  exports.MongoManagerUserCollection = exports.MongoManagerCollection = exports.MongoManagerModel = exports.MongoManager = void 0;
113
113
  var NodeCache = require("node-cache");
@@ -132,6 +132,8 @@ var MongoManager = /** @class */ (function () {
132
132
  this._serverCollections = [];
133
133
  this._isWorkersEnabled = false;
134
134
  this._isWorkerInstance = false;
135
+ this._watchedDatabases = [];
136
+ this._mainDatabaseName = '';
135
137
  }
136
138
  MongoManager.create = function () {
137
139
  return __awaiter(this, void 0, void 0, function () {
@@ -157,6 +159,8 @@ var MongoManager = /** @class */ (function () {
157
159
  this._nodeCache = new NodeCache({ stdTTL: 0, checkperiod: 0 });
158
160
  this._isWorkersEnabled = process.env.IS_WORKERS_ENABLED === 'true';
159
161
  this._isWorkerInstance = process.env.IS_WORKER_INSTANCE === 'true';
162
+ this._mainDatabaseName = resolveio_server_app_1.ResolveIOServer.getServerConfig() ? resolveio_server_app_1.ResolveIOServer.getServerConfig()['DATABASE'] : '';
163
+ this._watchedDatabases = this.resolveWatchedDatabases();
160
164
  this.setCacheLimit();
161
165
  if (this._isWorkersEnabled && this._isWorkerInstance) {
162
166
  this.setupChangeStream();
@@ -170,6 +174,43 @@ var MongoManager = /** @class */ (function () {
170
174
  });
171
175
  });
172
176
  };
177
+ MongoManager.prototype.resolveWatchedDatabases = function () {
178
+ var e_1, _a;
179
+ var config = resolveio_server_app_1.ResolveIOServer.getServerConfig ? resolveio_server_app_1.ResolveIOServer.getServerConfig() : null;
180
+ var watchSet = new Set();
181
+ if (this._mainDatabaseName) {
182
+ watchSet.add(this._mainDatabaseName);
183
+ }
184
+ var configValue = config ? config['WATCH_DATABASES'] : null;
185
+ if (Array.isArray(configValue)) {
186
+ try {
187
+ for (var configValue_1 = __values(configValue), configValue_1_1 = configValue_1.next(); !configValue_1_1.done; configValue_1_1 = configValue_1.next()) {
188
+ var value = configValue_1_1.value;
189
+ if (typeof value === 'string' && value.trim()) {
190
+ watchSet.add(value.trim());
191
+ }
192
+ }
193
+ }
194
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
195
+ finally {
196
+ try {
197
+ if (configValue_1_1 && !configValue_1_1.done && (_a = configValue_1.return)) _a.call(configValue_1);
198
+ }
199
+ finally { if (e_1) throw e_1.error; }
200
+ }
201
+ }
202
+ else if (typeof configValue === 'string' && configValue.trim()) {
203
+ configValue.split(',').map(function (a) { return a.trim(); }).filter(function (a) { return a.length; }).forEach(function (a) { return watchSet.add(a); });
204
+ }
205
+ var envValue = process.env.MONGO_WATCH_DATABASES;
206
+ if (envValue) {
207
+ envValue.split(',').map(function (a) { return a.trim(); }).filter(function (a) { return a.length; }).forEach(function (a) { return watchSet.add(a); });
208
+ }
209
+ return Array.from(watchSet.values());
210
+ };
211
+ MongoManager.prototype.getWatchedDatabases = function () {
212
+ return this._watchedDatabases;
213
+ };
173
214
  MongoManager.prototype.oneTimeTransaction = function (fn) {
174
215
  var _this = this;
175
216
  return asyncLocalStorage.run({}, function () { return __awaiter(_this, void 0, void 0, function () {
@@ -615,7 +656,7 @@ var MongoManager = /** @class */ (function () {
615
656
  return undefined;
616
657
  };
617
658
  MongoManager.prototype.addToCache = function (collections, cacheKey, data) {
618
- var e_1, _a;
659
+ var e_2, _a;
619
660
  if ((0, common_1.getBinarySize)(JSON.stringify(data)) < 1000000 &&
620
661
  !collections.includes('logs') &&
621
662
  !collections.find(function (a) { return a.endsWith('.versions'); }) &&
@@ -635,12 +676,12 @@ var MongoManager = /** @class */ (function () {
635
676
  }
636
677
  }
637
678
  }
638
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
679
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
639
680
  finally {
640
681
  try {
641
682
  if (keys_1_1 && !keys_1_1.done && (_a = keys_1.return)) _a.call(keys_1);
642
683
  }
643
- finally { if (e_1) throw e_1.error; }
684
+ finally { if (e_2) throw e_2.error; }
644
685
  }
645
686
  console.log('Query Cache: ' + 'Too Big, - Deleted: ' + deleteCount + ' - ' + nodeCacheSize);
646
687
  }
@@ -649,7 +690,7 @@ var MongoManager = /** @class */ (function () {
649
690
  }
650
691
  };
651
692
  MongoManager.prototype.invalidateQueryCache = function (collection) {
652
- var e_2, _a, e_3, _b;
693
+ var e_3, _a, e_4, _b;
653
694
  var collectionCacheMap = this._cacheMap.filter(function (a) { return a.collections.includes(collection); });
654
695
  if (resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager() && resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()) {
655
696
  console.log(new Date(), 'Mongo Invalidate Cache', collection);
@@ -666,12 +707,12 @@ var MongoManager = /** @class */ (function () {
666
707
  }
667
708
  }
668
709
  }
669
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
710
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
670
711
  finally {
671
712
  try {
672
713
  if (collectionCacheMap_1_1 && !collectionCacheMap_1_1.done && (_a = collectionCacheMap_1.return)) _a.call(collectionCacheMap_1);
673
714
  }
674
- finally { if (e_2) throw e_2.error; }
715
+ finally { if (e_3) throw e_3.error; }
675
716
  }
676
717
  try {
677
718
  // eslint-disable-next-line no-unused-vars
@@ -685,22 +726,23 @@ var MongoManager = /** @class */ (function () {
685
726
  }
686
727
  }
687
728
  }
688
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
729
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
689
730
  finally {
690
731
  try {
691
732
  if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
692
733
  }
693
- finally { if (e_3) throw e_3.error; }
734
+ finally { if (e_4) throw e_4.error; }
694
735
  }
695
736
  this._cacheMap = this._cacheMap.filter(function (a) { return !a.collections.includes(collection); });
696
737
  };
697
738
  MongoManager.prototype.setupChangeStream = function () {
698
739
  var _this = this;
699
- var db = resolveio_server_app_1.ResolveIOServer.getMainDB();
740
+ var client = resolveio_server_app_1.ResolveIOServer.getMongoConnection();
741
+ var watchDatabases = this._watchedDatabases.length ? this._watchedDatabases : (this._mainDatabaseName ? [this._mainDatabaseName] : []);
700
742
  var pipeline = [
701
743
  {
702
744
  $match: {
703
- $and: [
745
+ $and: __spreadArray(__spreadArray([], __read((watchDatabases.length ? [{ 'ns.db': { $in: watchDatabases } }] : [])), false), [
704
746
  { 'ns.coll': { $nin: [
705
747
  'log-method-latencies',
706
748
  'log-subscriptions',
@@ -715,11 +757,11 @@ var MongoManager = /** @class */ (function () {
715
757
  ] } },
716
758
  { 'ns.coll': { $not: /.*\.versions$/ } },
717
759
  { 'ns.coll': { $not: /^monitor-/ } },
718
- ]
760
+ ], false)
719
761
  },
720
762
  },
721
763
  ];
722
- var changeStream = db.watch(pipeline, { fullDocument: 'updateLookup' });
764
+ var changeStream = client.watch(pipeline, { fullDocument: 'updateLookup' });
723
765
  changeStream.on('change', function (change) {
724
766
  if (change['ns'] && change['ns'].coll) {
725
767
  var collectionName = change['ns'].coll;
@@ -1031,7 +1073,7 @@ var MongoManagerCollection = /** @class */ (function () {
1031
1073
  if (bypassSession === void 0) { bypassSession = false; }
1032
1074
  return __awaiter(this, void 0, void 0, function () {
1033
1075
  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_7, bwOptions, affectedDocIds, pipeline, oldVersions, _d;
1034
- var e_4, _e;
1076
+ var e_5, _e;
1035
1077
  return __generator(this, function (_f) {
1036
1078
  switch (_f.label) {
1037
1079
  case 0:
@@ -1057,7 +1099,7 @@ var MongoManagerCollection = /** @class */ (function () {
1057
1099
  logs = [];
1058
1100
  versionOps = [];
1059
1101
  try {
1060
- for (batchOps_1 = (e_4 = void 0, __values(batchOps)), batchOps_1_1 = batchOps_1.next(); !batchOps_1_1.done; batchOps_1_1 = batchOps_1.next()) {
1102
+ for (batchOps_1 = (e_5 = void 0, __values(batchOps)), batchOps_1_1 = batchOps_1.next(); !batchOps_1_1.done; batchOps_1_1 = batchOps_1.next()) {
1061
1103
  op = batchOps_1_1.value;
1062
1104
  opType = Object.keys(op)[0];
1063
1105
  doc = op[opType];
@@ -1227,12 +1269,12 @@ var MongoManagerCollection = /** @class */ (function () {
1227
1269
  }
1228
1270
  }
1229
1271
  }
1230
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
1272
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
1231
1273
  finally {
1232
1274
  try {
1233
1275
  if (batchOps_1_1 && !batchOps_1_1.done && (_e = batchOps_1.return)) _e.call(batchOps_1);
1234
1276
  }
1235
- finally { if (e_4) throw e_4.error; }
1277
+ finally { if (e_5) throw e_5.error; }
1236
1278
  }
1237
1279
  affectedDocs = [];
1238
1280
  if (!(!bypassVersions && this.useVersions && versionOps.length > 0)) return [3 /*break*/, 10];
@@ -2630,8 +2672,8 @@ var MongoManagerCollection = /** @class */ (function () {
2630
2672
  if (bypassMonitor === void 0) { bypassMonitor = false; }
2631
2673
  if (bypassSession === void 0) { bypassSession = false; }
2632
2674
  return __awaiter(this, void 0, void 0, function () {
2633
- var validationResults, docs_1, docs_1_1, doc, validation, isValid, e_5_1, validDocs, mongoSession, _a, session, safeOptions, i, doc, date, monitor, err_25;
2634
- var e_5, _b;
2675
+ var validationResults, docs_1, docs_1_1, doc, validation, isValid, e_6_1, validDocs, mongoSession, _a, session, safeOptions, i, doc, date, monitor, err_25;
2676
+ var e_6, _b;
2635
2677
  return __generator(this, function (_c) {
2636
2678
  switch (_c.label) {
2637
2679
  case 0:
@@ -2665,14 +2707,14 @@ var MongoManagerCollection = /** @class */ (function () {
2665
2707
  return [3 /*break*/, 2];
2666
2708
  case 6: return [3 /*break*/, 9];
2667
2709
  case 7:
2668
- e_5_1 = _c.sent();
2669
- e_5 = { error: e_5_1 };
2710
+ e_6_1 = _c.sent();
2711
+ e_6 = { error: e_6_1 };
2670
2712
  return [3 /*break*/, 9];
2671
2713
  case 8:
2672
2714
  try {
2673
2715
  if (docs_1_1 && !docs_1_1.done && (_b = docs_1.return)) _b.call(docs_1);
2674
2716
  }
2675
- finally { if (e_5) throw e_5.error; }
2717
+ finally { if (e_6) throw e_6.error; }
2676
2718
  return [7 /*endfinally*/];
2677
2719
  case 9:
2678
2720
  validDocs = this.checkSchema ? docs.filter(function (a, idx) { return validationResults[idx]; }) : docs;