@resolveio/server-lib 9.1.32 → 9.1.34

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,46 +283,53 @@ 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: '',
238
290
  messageId: 0,
239
291
  route: ''
240
292
  });
293
+ index_1.ResolveIOServer.getMongoManager().addToQueue({
294
+ _id: 0,
295
+ name_collection: _this.collectionName,
296
+ name_function: 'aggregate',
297
+ name_function_addt: 'toArray',
298
+ data_function: [pipeline, options],
299
+ data_function_addt: [],
300
+ cbs: [function (err, res, cached) {
301
+ if (_this.createLogs && allowLogs) {
302
+ log_collection_1.Logs.insertOne({
303
+ _id: objectIdHexString(),
304
+ type: 'queryResponse',
305
+ collection: _this.collectionName,
306
+ id_document: '',
307
+ payload: common_1.getBinarySize(JSON.stringify(['Is Cached: ' + cached, err, res])) < 200000 ? JSON.stringify(['Is Cached: ' + cached, err, res], null, 2) : 'Too Big',
308
+ method: 'aggregate',
309
+ id_user: '',
310
+ user: '',
311
+ messageId: 0,
312
+ route: ''
313
+ });
314
+ }
315
+ if (err) {
316
+ reject(err);
317
+ }
318
+ else {
319
+ resolve(res);
320
+ }
321
+ }],
322
+ cbs_next: [],
323
+ running: false,
324
+ cacheId: 0,
325
+ lookup_collections: pipeline.filter(function (a) { return a['$lookup'] && a['$lookup'].from; }).map(function (a) { return a['$lookup'].from; }).filter(function (elem, index, self) {
326
+ return index === self.indexOf(elem);
327
+ })
328
+ });
329
+ }
330
+ else {
331
+ index_1.ResolveIOServer.getMainDB().collection(_this.collectionName).aggregate(pipeline, options).toArray().then(function (res) { return resolve(res); }, function (err) { return reject(err); });
241
332
  }
242
- index_1.ResolveIOServer.getMongoManager().addToQueue({
243
- _id: 0,
244
- name_collection: _this.collectionName,
245
- name_function: 'aggregate',
246
- name_function_addt: 'toArray',
247
- data_function: [pipeline, options],
248
- data_function_addt: [],
249
- cbs: [function (err, res) {
250
- if (err) {
251
- reject(err);
252
- }
253
- else {
254
- resolve(res);
255
- }
256
- if (_this.createLogs && allowLogs) {
257
- log_collection_1.Logs.insertOne({
258
- _id: objectIdHexString(),
259
- type: 'queryResponse',
260
- collection: _this.collectionName,
261
- id_document: '',
262
- payload: common_1.getBinarySize(JSON.stringify([err, res])) < 200000 ? JSON.stringify([err, res], null, 2) : 'Too Big',
263
- method: 'aggregate',
264
- id_user: '',
265
- user: '',
266
- messageId: 0,
267
- route: ''
268
- });
269
- }
270
- }],
271
- cbs_next: [],
272
- running: false
273
- });
274
333
  });
275
334
  };
276
335
  Collection.prototype.aggregateCount = function (pipeline, options) {
@@ -283,7 +342,7 @@ var Collection = /** @class */ (function () {
283
342
  name_function_addt: 'count',
284
343
  data_function: [pipeline, options],
285
344
  data_function_addt: [],
286
- cbs: [function (err, res) {
345
+ cbs: [function (err, res, cached) {
287
346
  if (err) {
288
347
  reject(err);
289
348
  }
@@ -292,7 +351,11 @@ var Collection = /** @class */ (function () {
292
351
  }
293
352
  }],
294
353
  cbs_next: [],
295
- running: false
354
+ running: false,
355
+ cacheId: 0,
356
+ lookup_collections: pipeline.filter(function (a) { return a['$lookup'] && a['$lookup'].from; }).map(function (a) { return a['$lookup'].from; }).filter(function (elem, index, self) {
357
+ return index === self.indexOf(elem);
358
+ })
296
359
  });
297
360
  });
298
361
  };
@@ -445,46 +508,51 @@ var Collection = /** @class */ (function () {
445
508
  type: 'query',
446
509
  collection: this.collectionName,
447
510
  id_document: '',
448
- payload: common_1.getBinarySize(JSON.stringify([filter, options])) < 200000 ? JSON.stringify([filter, options], null, 2) : 'Too Big',
511
+ payload: JSON.stringify([filter, options], null, 2),
449
512
  method: 'find',
450
513
  id_user: '',
451
514
  user: '',
452
515
  messageId: 0,
453
516
  route: ''
454
517
  });
518
+ index_1.ResolveIOServer.getMongoManager().addToQueue({
519
+ _id: 0,
520
+ name_collection: this.collectionName,
521
+ name_function: 'find',
522
+ name_function_addt: 'toArray',
523
+ data_function: [filter, options],
524
+ data_function_addt: [],
525
+ cbs: [function (err, res, cached) {
526
+ if (_this.createLogs && !bypassLogs) {
527
+ log_collection_1.Logs.insertOne({
528
+ _id: objectIdHexString(),
529
+ type: 'queryResponse',
530
+ collection: _this.collectionName,
531
+ id_document: '',
532
+ payload: common_1.getBinarySize(JSON.stringify(['Is Cached: ' + cached, err, res])) < 200000 ? JSON.stringify(['Is Cached: ' + cached, err, res], null, 2) : 'Too Big',
533
+ method: 'find',
534
+ id_user: '',
535
+ user: '',
536
+ messageId: 0,
537
+ route: ''
538
+ });
539
+ }
540
+ if (err) {
541
+ reject(err);
542
+ }
543
+ else {
544
+ resolve(res);
545
+ }
546
+ }],
547
+ cbs_next: [],
548
+ running: false,
549
+ cacheId: 0,
550
+ lookup_collections: []
551
+ });
552
+ }
553
+ else {
554
+ index_1.ResolveIOServer.getMainDB().collection(this.collectionName).find(filter, options).toArray().then(function (res) { return resolve(res); }, function (err) { return reject(err); });
455
555
  }
456
- index_1.ResolveIOServer.getMongoManager().addToQueue({
457
- _id: 0,
458
- name_collection: this.collectionName,
459
- name_function: 'find',
460
- name_function_addt: 'toArray',
461
- data_function: [filter, options],
462
- data_function_addt: [],
463
- cbs: [function (err, res) {
464
- if (err) {
465
- reject(err);
466
- }
467
- else {
468
- resolve(res);
469
- }
470
- if (_this.createLogs && !bypassLogs) {
471
- log_collection_1.Logs.insertOne({
472
- _id: objectIdHexString(),
473
- type: 'queryResponse',
474
- collection: _this.collectionName,
475
- id_document: '',
476
- payload: common_1.getBinarySize(JSON.stringify([err, res])) < 200000 ? JSON.stringify([err, res], null, 2) : 'Too Big',
477
- method: 'find',
478
- id_user: '',
479
- user: '',
480
- messageId: 0,
481
- route: ''
482
- });
483
- }
484
- }],
485
- cbs_next: [],
486
- running: false
487
- });
488
556
  return [2 /*return*/];
489
557
  });
490
558
  }); });
@@ -501,7 +569,7 @@ var Collection = /** @class */ (function () {
501
569
  type: 'query',
502
570
  collection: this.collectionName,
503
571
  id_document: '',
504
- payload: common_1.getBinarySize(JSON.stringify([filter, options])) < 200000 ? JSON.stringify([filter, options], null, 2) : 'Too Big',
572
+ payload: JSON.stringify([filter, options], null, 2),
505
573
  method: 'findCount',
506
574
  id_user: '',
507
575
  user: '',
@@ -530,46 +598,51 @@ var Collection = /** @class */ (function () {
530
598
  type: 'query',
531
599
  collection: _this.collectionName,
532
600
  id_document: '',
533
- payload: common_1.getBinarySize(JSON.stringify([filter, options])) < 200000 ? JSON.stringify([filter, options], null, 2) : 'Too Big',
601
+ payload: JSON.stringify([filter, options], null, 2),
534
602
  method: 'findOne',
535
603
  id_user: '',
536
604
  user: '',
537
605
  messageId: 0,
538
606
  route: ''
539
607
  });
608
+ index_1.ResolveIOServer.getMongoManager().addToQueue({
609
+ _id: 0,
610
+ name_collection: _this.collectionName,
611
+ name_function: 'findOne',
612
+ name_function_addt: '',
613
+ data_function: [filter, options],
614
+ data_function_addt: [],
615
+ cbs: [function (err, res, cached) {
616
+ if (err) {
617
+ reject(err);
618
+ }
619
+ else {
620
+ resolve(res);
621
+ }
622
+ if (_this.createLogs && !bypassLogs) {
623
+ log_collection_1.Logs.insertOne({
624
+ _id: objectIdHexString(),
625
+ type: 'queryResponse',
626
+ collection: _this.collectionName,
627
+ id_document: '',
628
+ payload: common_1.getBinarySize(JSON.stringify(['Is Cached: ' + cached, err, res])) < 200000 ? JSON.stringify(['Is Cached: ' + cached, err, res], null, 2) : 'Too Big',
629
+ method: 'findOne',
630
+ id_user: '',
631
+ user: '',
632
+ messageId: 0,
633
+ route: ''
634
+ });
635
+ }
636
+ }],
637
+ cbs_next: [],
638
+ running: false,
639
+ cacheId: 0,
640
+ lookup_collections: []
641
+ });
642
+ }
643
+ else {
644
+ index_1.ResolveIOServer.getMainDB().collection(_this.collectionName).findOne(filter, options).then(function (res) { return resolve(res); }, function (err) { return reject(err); });
540
645
  }
541
- index_1.ResolveIOServer.getMongoManager().addToQueue({
542
- _id: 0,
543
- name_collection: _this.collectionName,
544
- name_function: 'findOne',
545
- name_function_addt: '',
546
- data_function: [filter, options],
547
- data_function_addt: [],
548
- cbs: [function (err, res) {
549
- if (err) {
550
- reject(err);
551
- }
552
- else {
553
- resolve(res);
554
- }
555
- if (_this.createLogs && !bypassLogs) {
556
- log_collection_1.Logs.insertOne({
557
- _id: objectIdHexString(),
558
- type: 'queryResponse',
559
- collection: _this.collectionName,
560
- id_document: '',
561
- payload: common_1.getBinarySize(JSON.stringify([err, res])) < 200000 ? JSON.stringify([err, res], null, 2) : 'Too Big',
562
- method: 'findOne',
563
- id_user: '',
564
- user: '',
565
- messageId: 0,
566
- route: ''
567
- });
568
- }
569
- }],
570
- cbs_next: [],
571
- running: false
572
- });
573
646
  });
574
647
  };
575
648
  Collection.prototype.findOneAndDelete = function (filter, options, bypassLogs) {