@resolveio/server-lib 9.1.32 → 9.1.33

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.
@@ -4,6 +4,7 @@ import { UserModel } from '../models/user.model';
4
4
  interface MongoQueueModel {
5
5
  _id: number;
6
6
  name_collection: string;
7
+ lookup_collections: string[];
7
8
  name_function: string;
8
9
  data_function: any[];
9
10
  name_function_addt: string;
@@ -11,17 +12,22 @@ interface MongoQueueModel {
11
12
  cbs: Function[];
12
13
  cbs_next: Function[];
13
14
  running: boolean;
15
+ cacheId: number;
14
16
  }
15
17
  export declare class MongoManager {
16
18
  private _collections;
17
19
  private _mongoQueue;
18
20
  private _mongoQueueId;
21
+ private _nodeCache;
22
+ private _mongoQueueCacheId;
19
23
  constructor();
20
24
  registerCollection(collection: Collection<any>): void;
21
25
  collections(): Collection<any>[];
22
26
  collection(collectionName: string): Collection<any> | UserCollection<any>;
23
27
  addToQueue(mongoQueue: MongoQueueModel): void;
24
28
  private runQueue;
29
+ private addToCache;
30
+ invalidateQueryCache(collection: string): void;
25
31
  }
26
32
  export declare class Model<T> {
27
33
  collection_main: Collection<T> | UserCollection<T>;
@@ -56,6 +56,7 @@ var index_1 = require("../index");
56
56
  var user_collection_1 = require("../collections/user.collection");
57
57
  var common_1 = require("../util/common");
58
58
  var log_collection_1 = require("../collections/log.collection");
59
+ var NodeCache = require("node-cache");
59
60
  var crypto = require('crypto');
60
61
  var scmp = require('scmp');
61
62
  var MongoManager = /** @class */ (function () {
@@ -63,6 +64,8 @@ var MongoManager = /** @class */ (function () {
63
64
  this._collections = [];
64
65
  this._mongoQueue = [];
65
66
  this._mongoQueueId = 0;
67
+ this._mongoQueueCacheId = 1;
68
+ this._nodeCache = new NodeCache({ stdTTL: 0, checkperiod: 0 });
66
69
  this.runQueue();
67
70
  }
68
71
  MongoManager.prototype.registerCollection = function (collection) {
@@ -95,62 +98,109 @@ var MongoManager = /** @class */ (function () {
95
98
  var _this = this;
96
99
  return __generator(this, function (_a) {
97
100
  setInterval(function () {
98
- var _a, _b, _c;
99
- if (_this._mongoQueue.filter(function (a) { return !a.running; }).length) {
100
- var mongoQueue_1 = _this._mongoQueue.filter(function (a) { return !a.running; })[0];
101
- mongoQueue_1.running = true;
102
- if (!mongoQueue_1.name_function_addt) {
103
- (_a = index_1.ResolveIOServer.getMainDB().collection(mongoQueue_1.name_collection))[mongoQueue_1.name_function].apply(_a, mongoQueue_1.data_function).then(function (res) {
104
- mongoQueue_1.cbs.forEach(function (cb) {
105
- cb(null, res);
101
+ var queueArr = _this._mongoQueue.filter(function (a) { return !a.running && a.cbs.length; });
102
+ if (queueArr.length) {
103
+ var _loop_1 = function (i) {
104
+ var _a, _b, _c;
105
+ var mongoQueue = queueArr[i];
106
+ mongoQueue.running = true;
107
+ if (mongoQueue.cacheId) {
108
+ var res_1 = _this._nodeCache.get(mongoQueue.cacheId);
109
+ mongoQueue.cbs.forEach(function (cb) {
110
+ cb(null, res_1, true);
106
111
  });
107
- if (mongoQueue_1.cbs_next.length) {
108
- mongoQueue_1.running = false;
109
- mongoQueue_1.cbs = mongoQueue_1.cbs_next;
110
- mongoQueue_1.cbs_next = [];
111
- }
112
- else {
113
- _this._mongoQueue.splice(_this._mongoQueue.map(function (a) { return a._id; }).indexOf(mongoQueue_1._id), 1);
114
- }
115
- }, function (err) {
116
- mongoQueue_1.cbs.forEach(function (cb) {
117
- cb(err, null);
118
- });
119
- mongoQueue_1.cbs_next.forEach(function (cb) {
120
- cb(err, null);
121
- });
122
- _this._mongoQueue.splice(_this._mongoQueue.map(function (a) { return a._id; }).indexOf(mongoQueue_1._id), 1);
123
- });
124
- }
125
- else {
126
- (_b = (_c = index_1.ResolveIOServer.getMainDB().collection(mongoQueue_1.name_collection))[mongoQueue_1.name_function].apply(_c, mongoQueue_1.data_function))[mongoQueue_1.name_function_addt].apply(_b, mongoQueue_1.data_function_addt).then(function (res) {
127
- mongoQueue_1.cbs.forEach(function (cb) {
128
- cb(null, res);
129
- });
130
- if (mongoQueue_1.cbs_next.length) {
131
- mongoQueue_1.running = false;
132
- mongoQueue_1.cbs = mongoQueue_1.cbs_next;
133
- mongoQueue_1.cbs_next = [];
112
+ mongoQueue.cbs = [];
113
+ mongoQueue.running = false;
114
+ }
115
+ else {
116
+ if (!mongoQueue.name_function_addt) {
117
+ (_a = index_1.ResolveIOServer.getMainDB().collection(mongoQueue.name_collection))[mongoQueue.name_function].apply(_a, mongoQueue.data_function).then(function (res) {
118
+ mongoQueue.cacheId = _this._mongoQueueCacheId;
119
+ _this._mongoQueueCacheId += 1;
120
+ _this.addToCache(mongoQueue.cacheId, res);
121
+ mongoQueue.cbs.forEach(function (cb) {
122
+ cb(null, res, false);
123
+ });
124
+ if (mongoQueue.cbs_next.length) {
125
+ mongoQueue.cbs = mongoQueue.cbs_next;
126
+ mongoQueue.cbs_next = [];
127
+ }
128
+ else {
129
+ mongoQueue.cbs = [];
130
+ }
131
+ mongoQueue.running = false;
132
+ }, function (err) {
133
+ mongoQueue.cbs.forEach(function (cb) {
134
+ cb(err, null, false);
135
+ });
136
+ mongoQueue.cbs_next.forEach(function (cb) {
137
+ cb(err, null, false);
138
+ });
139
+ _this._mongoQueue.splice(_this._mongoQueue.map(function (a) { return a._id; }).indexOf(mongoQueue._id), 1);
140
+ });
134
141
  }
135
142
  else {
136
- _this._mongoQueue.splice(_this._mongoQueue.map(function (a) { return a._id; }).indexOf(mongoQueue_1._id), 1);
143
+ (_b = (_c = index_1.ResolveIOServer.getMainDB().collection(mongoQueue.name_collection))[mongoQueue.name_function].apply(_c, mongoQueue.data_function))[mongoQueue.name_function_addt].apply(_b, mongoQueue.data_function_addt).then(function (res) {
144
+ mongoQueue.cacheId = _this._mongoQueueCacheId;
145
+ _this._mongoQueueCacheId += 1;
146
+ _this.addToCache(mongoQueue.cacheId, res);
147
+ mongoQueue.cbs.forEach(function (cb) {
148
+ cb(null, res, false);
149
+ });
150
+ if (mongoQueue.cbs_next.length) {
151
+ mongoQueue.cbs = mongoQueue.cbs_next;
152
+ mongoQueue.cbs_next = [];
153
+ }
154
+ else {
155
+ mongoQueue.cbs = [];
156
+ }
157
+ mongoQueue.running = false;
158
+ }, function (err) {
159
+ mongoQueue.cbs.forEach(function (cb) {
160
+ cb(err, null, false);
161
+ });
162
+ mongoQueue.cbs_next.forEach(function (cb) {
163
+ cb(err, null, false);
164
+ });
165
+ _this._mongoQueue.splice(_this._mongoQueue.map(function (a) { return a._id; }).indexOf(mongoQueue._id), 1);
166
+ });
137
167
  }
138
- }, function (err) {
139
- mongoQueue_1.cbs.forEach(function (cb) {
140
- cb(err, null);
141
- });
142
- mongoQueue_1.cbs_next.forEach(function (cb) {
143
- cb(err, null);
144
- });
145
- _this._mongoQueue.splice(_this._mongoQueue.map(function (a) { return a._id; }).indexOf(mongoQueue_1._id), 1);
146
- });
168
+ }
169
+ };
170
+ for (var i = 0; i < queueArr.length; i++) {
171
+ _loop_1(i);
147
172
  }
148
173
  }
149
- }, 10);
174
+ }, 1);
150
175
  return [2 /*return*/];
151
176
  });
152
177
  });
153
178
  };
179
+ MongoManager.prototype.addToCache = function (id, data) {
180
+ var nodeCacheSize = this._nodeCache.getStats().vsize;
181
+ if (nodeCacheSize > 250000000) {
182
+ var deleteCount = 0;
183
+ var queueArr = this._mongoQueue.filter(function (a) { return a.cacheId; });
184
+ for (var zz = 0; zz < queueArr.length; zz++) {
185
+ this._nodeCache.del(queueArr[zz].cacheId);
186
+ queueArr[zz].cacheId = 0;
187
+ deleteCount += 1;
188
+ nodeCacheSize = this._nodeCache.getStats().vsize;
189
+ if (nodeCacheSize < 250000000) {
190
+ break;
191
+ }
192
+ }
193
+ console.log('Query Cache: ' + 'Too Big, - Deleted: ' + deleteCount + ' - ' + nodeCacheSize);
194
+ }
195
+ this._nodeCache.set(id, data);
196
+ };
197
+ MongoManager.prototype.invalidateQueryCache = function (collection) {
198
+ var _this = this;
199
+ this._mongoQueue.filter(function (a) { return (a.name_collection === collection || a.lookup_collections.includes(collection)) && a.cacheId; }).forEach(function (queue) {
200
+ _this._nodeCache.del(queue.cacheId);
201
+ queue.cacheId = 0;
202
+ });
203
+ };
154
204
  return MongoManager;
155
205
  }());
156
206
  exports.MongoManager = MongoManager;
@@ -181,11 +231,13 @@ var Model = /** @class */ (function () {
181
231
  else {
182
232
  this.collection_version = new Collection(collectionName + '.versions', versionSchema, false, [], true, false, false);
183
233
  }
184
- // if (this.collection_version.indexExists('_id.__v_1__id._id_1')) {
185
- // this.collection_version.dropIndex('_id.__v_1__id._id_1');
186
- // }
187
- this.collection_version.createIndex({ '_id._id': 1, '_id.__v': 1 });
188
- this.collection_main.versionCollection = collectionName + '.versions';
234
+ if (this.collection_version.indexExists('_id.__v_1__id._id_1')) {
235
+ this.collection_version.dropIndex('_id.__v_1__id._id_1');
236
+ }
237
+ if (!this.collection_version.indexExists('_id._id_1__id.__v_1')) {
238
+ this.collection_version.createIndex({ '_id._id': 1, '_id.__v': 1 });
239
+ this.collection_main.versionCollection = collectionName + '.versions';
240
+ }
189
241
  }
190
242
  }
191
243
  return Model;
@@ -231,7 +283,7 @@ var Collection = /** @class */ (function () {
231
283
  type: 'query',
232
284
  collection: _this.collectionName,
233
285
  id_document: '',
234
- payload: common_1.getBinarySize(JSON.stringify([pipeline, options])) < 200000 ? JSON.stringify([pipeline, options], null, 2) : 'Too Big',
286
+ payload: JSON.stringify([pipeline, options], null, 2),
235
287
  method: 'aggregate',
236
288
  id_user: '',
237
289
  user: '',
@@ -246,20 +298,14 @@ var Collection = /** @class */ (function () {
246
298
  name_function_addt: 'toArray',
247
299
  data_function: [pipeline, options],
248
300
  data_function_addt: [],
249
- cbs: [function (err, res) {
250
- if (err) {
251
- reject(err);
252
- }
253
- else {
254
- resolve(res);
255
- }
301
+ cbs: [function (err, res, cached) {
256
302
  if (_this.createLogs && allowLogs) {
257
303
  log_collection_1.Logs.insertOne({
258
304
  _id: objectIdHexString(),
259
305
  type: 'queryResponse',
260
306
  collection: _this.collectionName,
261
307
  id_document: '',
262
- payload: common_1.getBinarySize(JSON.stringify([err, res])) < 200000 ? JSON.stringify([err, res], null, 2) : 'Too Big',
308
+ payload: common_1.getBinarySize(JSON.stringify(['Is Cached: ' + cached, err, res])) < 200000 ? JSON.stringify(['Is Cached: ' + cached, err, res], null, 2) : 'Too Big',
263
309
  method: 'aggregate',
264
310
  id_user: '',
265
311
  user: '',
@@ -267,9 +313,19 @@ var Collection = /** @class */ (function () {
267
313
  route: ''
268
314
  });
269
315
  }
316
+ if (err) {
317
+ reject(err);
318
+ }
319
+ else {
320
+ resolve(res);
321
+ }
270
322
  }],
271
323
  cbs_next: [],
272
- running: false
324
+ running: false,
325
+ cacheId: 0,
326
+ lookup_collections: pipeline.filter(function (a) { return a['$lookup'] && a['$lookup'].from; }).map(function (a) { return a['$lookup'].from; }).filter(function (elem, index, self) {
327
+ return index === self.indexOf(elem);
328
+ })
273
329
  });
274
330
  });
275
331
  };
@@ -283,7 +339,7 @@ var Collection = /** @class */ (function () {
283
339
  name_function_addt: 'count',
284
340
  data_function: [pipeline, options],
285
341
  data_function_addt: [],
286
- cbs: [function (err, res) {
342
+ cbs: [function (err, res, cached) {
287
343
  if (err) {
288
344
  reject(err);
289
345
  }
@@ -292,7 +348,11 @@ var Collection = /** @class */ (function () {
292
348
  }
293
349
  }],
294
350
  cbs_next: [],
295
- running: false
351
+ running: false,
352
+ cacheId: 0,
353
+ lookup_collections: pipeline.filter(function (a) { return a['$lookup'] && a['$lookup'].from; }).map(function (a) { return a['$lookup'].from; }).filter(function (elem, index, self) {
354
+ return index === self.indexOf(elem);
355
+ })
296
356
  });
297
357
  });
298
358
  };
@@ -445,7 +505,7 @@ var Collection = /** @class */ (function () {
445
505
  type: 'query',
446
506
  collection: this.collectionName,
447
507
  id_document: '',
448
- payload: common_1.getBinarySize(JSON.stringify([filter, options])) < 200000 ? JSON.stringify([filter, options], null, 2) : 'Too Big',
508
+ payload: JSON.stringify([filter, options], null, 2),
449
509
  method: 'find',
450
510
  id_user: '',
451
511
  user: '',
@@ -460,20 +520,14 @@ var Collection = /** @class */ (function () {
460
520
  name_function_addt: 'toArray',
461
521
  data_function: [filter, options],
462
522
  data_function_addt: [],
463
- cbs: [function (err, res) {
464
- if (err) {
465
- reject(err);
466
- }
467
- else {
468
- resolve(res);
469
- }
523
+ cbs: [function (err, res, cached) {
470
524
  if (_this.createLogs && !bypassLogs) {
471
525
  log_collection_1.Logs.insertOne({
472
526
  _id: objectIdHexString(),
473
527
  type: 'queryResponse',
474
528
  collection: _this.collectionName,
475
529
  id_document: '',
476
- payload: common_1.getBinarySize(JSON.stringify([err, res])) < 200000 ? JSON.stringify([err, res], null, 2) : 'Too Big',
530
+ payload: common_1.getBinarySize(JSON.stringify(['Is Cached: ' + cached, err, res])) < 200000 ? JSON.stringify(['Is Cached: ' + cached, err, res], null, 2) : 'Too Big',
477
531
  method: 'find',
478
532
  id_user: '',
479
533
  user: '',
@@ -481,9 +535,17 @@ var Collection = /** @class */ (function () {
481
535
  route: ''
482
536
  });
483
537
  }
538
+ if (err) {
539
+ reject(err);
540
+ }
541
+ else {
542
+ resolve(res);
543
+ }
484
544
  }],
485
545
  cbs_next: [],
486
- running: false
546
+ running: false,
547
+ cacheId: 0,
548
+ lookup_collections: []
487
549
  });
488
550
  return [2 /*return*/];
489
551
  });
@@ -501,7 +563,7 @@ var Collection = /** @class */ (function () {
501
563
  type: 'query',
502
564
  collection: this.collectionName,
503
565
  id_document: '',
504
- payload: common_1.getBinarySize(JSON.stringify([filter, options])) < 200000 ? JSON.stringify([filter, options], null, 2) : 'Too Big',
566
+ payload: JSON.stringify([filter, options], null, 2),
505
567
  method: 'findCount',
506
568
  id_user: '',
507
569
  user: '',
@@ -530,7 +592,7 @@ var Collection = /** @class */ (function () {
530
592
  type: 'query',
531
593
  collection: _this.collectionName,
532
594
  id_document: '',
533
- payload: common_1.getBinarySize(JSON.stringify([filter, options])) < 200000 ? JSON.stringify([filter, options], null, 2) : 'Too Big',
595
+ payload: JSON.stringify([filter, options], null, 2),
534
596
  method: 'findOne',
535
597
  id_user: '',
536
598
  user: '',
@@ -545,7 +607,7 @@ var Collection = /** @class */ (function () {
545
607
  name_function_addt: '',
546
608
  data_function: [filter, options],
547
609
  data_function_addt: [],
548
- cbs: [function (err, res) {
610
+ cbs: [function (err, res, cached) {
549
611
  if (err) {
550
612
  reject(err);
551
613
  }
@@ -558,7 +620,7 @@ var Collection = /** @class */ (function () {
558
620
  type: 'queryResponse',
559
621
  collection: _this.collectionName,
560
622
  id_document: '',
561
- payload: common_1.getBinarySize(JSON.stringify([err, res])) < 200000 ? JSON.stringify([err, res], null, 2) : 'Too Big',
623
+ payload: common_1.getBinarySize(JSON.stringify(['Is Cached: ' + cached, err, res])) < 200000 ? JSON.stringify(['Is Cached: ' + cached, err, res], null, 2) : 'Too Big',
562
624
  method: 'findOne',
563
625
  id_user: '',
564
626
  user: '',
@@ -568,7 +630,9 @@ var Collection = /** @class */ (function () {
568
630
  }
569
631
  }],
570
632
  cbs_next: [],
571
- running: false
633
+ running: false,
634
+ cacheId: 0,
635
+ lookup_collections: []
572
636
  });
573
637
  });
574
638
  };