@nextage/nx-frame-be 1.0.1

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.
Files changed (119) hide show
  1. package/README.md +1 -0
  2. package/README.md.old +45 -0
  3. package/build/app.d.ts +0 -0
  4. package/build/app.js +1 -0
  5. package/build/aws/aws-cluster-manager.d.ts +28 -0
  6. package/build/aws/aws-cluster-manager.js +105 -0
  7. package/build/aws/aws-email-manager.d.ts +24 -0
  8. package/build/aws/aws-email-manager.js +57 -0
  9. package/build/aws/aws-queue-manager.d.ts +37 -0
  10. package/build/aws/aws-queue-manager.js +100 -0
  11. package/build/aws/aws.interfaces.d.ts +39 -0
  12. package/build/aws/aws.interfaces.js +2 -0
  13. package/build/aws/index.d.ts +4 -0
  14. package/build/aws/index.js +20 -0
  15. package/build/common/base/__test__/base.model.test.d.ts +1 -0
  16. package/build/common/base/__test__/base.model.test.js +283 -0
  17. package/build/common/base/base.controller.d.ts +124 -0
  18. package/build/common/base/base.controller.js +134 -0
  19. package/build/common/base/base.interfaces.d.ts +81 -0
  20. package/build/common/base/base.interfaces.js +2 -0
  21. package/build/common/base/base.model.d.ts +384 -0
  22. package/build/common/base/base.model.js +730 -0
  23. package/build/common/base/folded.model.d.ts +41 -0
  24. package/build/common/base/folded.model.js +215 -0
  25. package/build/common/base/mongo-utils.d.ts +122 -0
  26. package/build/common/base/mongo-utils.js +242 -0
  27. package/build/common/base/mongo.types.d.ts +20 -0
  28. package/build/common/base/mongo.types.js +2 -0
  29. package/build/common/base/sql-utils.d.ts +20 -0
  30. package/build/common/base/sql-utils.js +69 -0
  31. package/build/common/constants.d.ts +27 -0
  32. package/build/common/constants.js +70 -0
  33. package/build/common/crypto/crypto.classes.d.ts +100 -0
  34. package/build/common/crypto/crypto.classes.js +146 -0
  35. package/build/common/crypto/crypto.constants.d.ts +68 -0
  36. package/build/common/crypto/crypto.constants.js +57 -0
  37. package/build/common/crypto/crypto.error.d.ts +4 -0
  38. package/build/common/crypto/crypto.error.js +10 -0
  39. package/build/common/crypto/crypto.interfaces.d.ts +46 -0
  40. package/build/common/crypto/crypto.interfaces.js +2 -0
  41. package/build/common/crypto/crypto.utils.d.ts +139 -0
  42. package/build/common/crypto/crypto.utils.js +380 -0
  43. package/build/common/crypto/index.d.ts +3 -0
  44. package/build/common/crypto/index.js +19 -0
  45. package/build/common/enums.d.ts +61 -0
  46. package/build/common/enums.js +75 -0
  47. package/build/common/errors.d.ts +13 -0
  48. package/build/common/errors.js +24 -0
  49. package/build/common/express/express.interfaces.d.ts +20 -0
  50. package/build/common/express/express.interfaces.js +2 -0
  51. package/build/common/express/express.utils.d.ts +31 -0
  52. package/build/common/express/express.utils.js +85 -0
  53. package/build/common/express/index.d.ts +2 -0
  54. package/build/common/express/index.js +18 -0
  55. package/build/common/graphql/__test__/generator.schema.test.d.ts +1 -0
  56. package/build/common/graphql/__test__/generator.schema.test.js +391 -0
  57. package/build/common/graphql/directives.d.ts +18 -0
  58. package/build/common/graphql/directives.js +56 -0
  59. package/build/common/graphql/generator copy.d.ts +41 -0
  60. package/build/common/graphql/generator copy.js +385 -0
  61. package/build/common/graphql/generator.d.ts +42 -0
  62. package/build/common/graphql/generator.js +385 -0
  63. package/build/common/graphql/index.d.ts +4 -0
  64. package/build/common/graphql/index.js +21 -0
  65. package/build/common/graphql/interfaces.d.ts +67 -0
  66. package/build/common/graphql/interfaces.js +2 -0
  67. package/build/common/graphql/loader.d.ts +22 -0
  68. package/build/common/graphql/loader.js +186 -0
  69. package/build/common/graphql/scalars/index.d.ts +10 -0
  70. package/build/common/graphql/scalars/index.js +6 -0
  71. package/build/common/graphql/scalars/resolvers.d.ts +8 -0
  72. package/build/common/graphql/scalars/resolvers.js +71 -0
  73. package/build/common/graphql/scalars/schema.d.ts +1 -0
  74. package/build/common/graphql/scalars/schema.js +12 -0
  75. package/build/common/graphql/types/index.d.ts +3 -0
  76. package/build/common/graphql/types/index.js +6 -0
  77. package/build/common/graphql/types/schema.d.ts +1 -0
  78. package/build/common/graphql/types/schema.js +98 -0
  79. package/build/common/graphql/utils.d.ts +18 -0
  80. package/build/common/graphql/utils.js +40 -0
  81. package/build/common/grid-fs/gridfs-base.model.d.ts +53 -0
  82. package/build/common/grid-fs/gridfs-base.model.js +95 -0
  83. package/build/common/grid-fs/gridfs.interfaces.d.ts +41 -0
  84. package/build/common/grid-fs/gridfs.interfaces.js +2 -0
  85. package/build/common/index.d.ts +10 -0
  86. package/build/common/index.js +32 -0
  87. package/build/common/interfaces.d.ts +26 -0
  88. package/build/common/interfaces.js +10 -0
  89. package/build/common/manager/bulk-manager.d.ts +59 -0
  90. package/build/common/manager/bulk-manager.js +103 -0
  91. package/build/common/manager/crypto-manager.d.ts +88 -0
  92. package/build/common/manager/crypto-manager.js +200 -0
  93. package/build/common/manager/index.d.ts +5 -0
  94. package/build/common/manager/index.js +21 -0
  95. package/build/common/manager/pubsub-manager.d.ts +20 -0
  96. package/build/common/manager/pubsub-manager.js +38 -0
  97. package/build/common/manager/shutdown-manager.d.ts +27 -0
  98. package/build/common/manager/shutdown-manager.js +118 -0
  99. package/build/common/manager/socket-io-cli-manager.d.ts +53 -0
  100. package/build/common/manager/socket-io-cli-manager.js +91 -0
  101. package/build/common/manager/tunnel-manager.d.ts +28 -0
  102. package/build/common/manager/tunnel-manager.js +49 -0
  103. package/build/common/models/coded-entity.controller.d.ts +5 -0
  104. package/build/common/models/coded-entity.controller.js +9 -0
  105. package/build/common/models/coded-entity.model.d.ts +25 -0
  106. package/build/common/models/coded-entity.model.js +51 -0
  107. package/build/common/models/user.model.d.ts +56 -0
  108. package/build/common/models/user.model.js +72 -0
  109. package/build/common/types.d.ts +136 -0
  110. package/build/common/types.js +16 -0
  111. package/build/common/utils.d.ts +552 -0
  112. package/build/common/utils.js +1100 -0
  113. package/build/index.d.ts +2 -0
  114. package/build/index.js +18 -0
  115. package/build/test/env.d.ts +0 -0
  116. package/build/test/env.js +5 -0
  117. package/build/test/setup.d.ts +6 -0
  118. package/build/test/setup.js +59 -0
  119. package/package.json +90 -0
@@ -0,0 +1,730 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.BaseModel = void 0;
13
+ const events_1 = require("events");
14
+ const utils_1 = require("../utils");
15
+ const constants_1 = require("../constants");
16
+ const enums_1 = require("../enums");
17
+ const errors_1 = require("../errors");
18
+ const mongo_utils_1 = require("./mongo-utils");
19
+ class BaseModel extends events_1.EventEmitter {
20
+ constructor({ name, model, params, logger }) {
21
+ super();
22
+ this.defaultParams = {};
23
+ this.logger = constants_1.APP.logger;
24
+ this.defaultParams = params || {};
25
+ this.name = name || 'Base';
26
+ this.mgModel = model;
27
+ this.logger = logger || constants_1.APP.logger;
28
+ this.events = {};
29
+ this.init();
30
+ }
31
+ /**
32
+ *
33
+ */
34
+ get schema() {
35
+ return this.mgModel.schema.obj;
36
+ }
37
+ /**
38
+ * Get mongodb collection name
39
+ */
40
+ get collectionName() {
41
+ if (!this.mgModel)
42
+ return null;
43
+ return this.mgModel.collection.collectionName;
44
+ }
45
+ /**
46
+ * Get Mongoose model name
47
+ */
48
+ get modelName() {
49
+ if (!this.mgModel)
50
+ return null;
51
+ return this.mgModel.modelName;
52
+ }
53
+ /**
54
+ * Get default parameters
55
+ */
56
+ get modelParams() {
57
+ if (!this.defaultParams)
58
+ return null;
59
+ return this.defaultParams;
60
+ }
61
+ /**
62
+ *
63
+ */
64
+ init() {
65
+ }
66
+ /**
67
+ *
68
+ * @param {*} options
69
+ */
70
+ initBulk(options) {
71
+ return this.mgModel.collection.initializeOrderedBulkOp(options);
72
+ }
73
+ /**
74
+ *
75
+ * @param {*} pipeline
76
+ * @param {*} params
77
+ * @param {*} context
78
+ */
79
+ aggregate(pipeline_1) {
80
+ return __awaiter(this, arguments, void 0, function* (pipeline, params = {}, context = {}) {
81
+ try {
82
+ const filter = yield this.updateFilters(params, context);
83
+ if (!filter)
84
+ return [];
85
+ if (Object.keys(filter).length)
86
+ pipeline.unshift({ $match: filter });
87
+ return yield this.mgModel.aggregate(pipeline).allowDiskUse(true);
88
+ }
89
+ catch (e) {
90
+ return [];
91
+ }
92
+ });
93
+ }
94
+ /**
95
+ *
96
+ * @param {*} pipeline
97
+ * @param {*} params
98
+ * @param {*} context
99
+ */
100
+ aggregateNativeFiltered(pipeline_1) {
101
+ return __awaiter(this, arguments, void 0, function* (pipeline, params = {}, context) {
102
+ try {
103
+ const filter = yield this.updateFilters(params, context);
104
+ if (!filter)
105
+ return Promise.resolve(null);
106
+ if (Object.keys(filter).length)
107
+ pipeline.unshift({ $match: filter });
108
+ return this.mgModel.collection.aggregate(pipeline, { allowDiskUse: true });
109
+ }
110
+ catch (e) {
111
+ return Promise.resolve(null);
112
+ }
113
+ });
114
+ }
115
+ /**
116
+ *
117
+ * @param {*} pipeline
118
+ * @param {*} params
119
+ * @param {*} context
120
+ */
121
+ aggregateNative(pipeline) {
122
+ return this.mgModel.collection.aggregate(pipeline, { allowDiskUse: true });
123
+ }
124
+ /**
125
+ * Simple Find wrapper
126
+ * @param {*} params
127
+ * @param {*} fields
128
+ * @param {*} options
129
+ */
130
+ find(params, fields, options) {
131
+ return __awaiter(this, void 0, void 0, function* () {
132
+ return this.mgModel.find(params, fields, options);
133
+ });
134
+ }
135
+ /**
136
+ * Get documents using Moongose wrapper method
137
+ *
138
+ * @param {*} params
139
+ */
140
+ list() {
141
+ return __awaiter(this, arguments, void 0, function* (params = {}, fields = {}, { offset, limit, sort } = {}, p4 = {}, p5) {
142
+ let context;
143
+ let queryActions = {};
144
+ if (p5) {
145
+ queryActions = p4;
146
+ context = p5;
147
+ }
148
+ else
149
+ context = p4;
150
+ const filter = yield this.updateFilters(params, context);
151
+ if (!filter)
152
+ return [];
153
+ let options = {};
154
+ if (offset || limit)
155
+ options = { skip: offset, limit: limit };
156
+ if (sort || this.defaultParams.sort)
157
+ options.sort = this.parseSortObject(sort || this.defaultParams.sort);
158
+ // return await this.mgModel.find(filter, fields, options).sort(this.parseSortObject(sort || this.defaultParams.sort!));
159
+ let query = this.mgModel.find(filter, fields, options);
160
+ if (Array.isArray(queryActions.populate))
161
+ queryActions.populate.forEach(ref => query = query.populate(ref));
162
+ return query;
163
+ });
164
+ }
165
+ /**
166
+ * Get documents using mongodb native method
167
+ *
168
+ * @param {*} params
169
+ * @param {*} callback
170
+ */
171
+ listNative(params = {}, options = {}) {
172
+ let cursor = this.mgModel.collection.find(params, options);
173
+ return cursor;
174
+ //return mongodb.db.collection(this.collectionName).find(params).toArray(callback);
175
+ }
176
+ /**
177
+ *
178
+ * @param params
179
+ * @param context
180
+ * @returns
181
+ */
182
+ count() {
183
+ return __awaiter(this, arguments, void 0, function* (params = {}, context = {}) {
184
+ const filter = yield this.updateFilters(params, context);
185
+ if (!filter)
186
+ return 0;
187
+ return this.mgModel.countDocuments(filter);
188
+ });
189
+ }
190
+ /**
191
+ * Get document by ID using Moongose wrapper method
192
+ *
193
+ * @param {*} id
194
+ * @param context
195
+ */
196
+ get(id_1) {
197
+ return __awaiter(this, arguments, void 0, function* (id, context = {}) {
198
+ if (!id)
199
+ return Promise.resolve(null);
200
+ return this.mgModel.findById(id);
201
+ });
202
+ }
203
+ /**
204
+ *
205
+ * @param {*} item
206
+ * @param {*} context
207
+ */
208
+ checkContextPermission(item, context = {}) {
209
+ return Promise.resolve((context.user) ? item : null);
210
+ }
211
+ /**
212
+ *
213
+ * @param {*} context
214
+ */
215
+ checkChannelSubscriber(payloadId, context = {}) {
216
+ return Promise.resolve(!!(context.user));
217
+ }
218
+ /**
219
+ * Get document using Moongose wrapper method
220
+ *
221
+ * @param {*} params
222
+ * @param {*} context
223
+ */
224
+ findOne(params_1) {
225
+ return __awaiter(this, arguments, void 0, function* (params, context = {}) {
226
+ const filter = yield this.updateFilters(params, context);
227
+ if (!filter)
228
+ return Promise.resolve(null);
229
+ return this.mgModel.findOne(filter);
230
+ });
231
+ }
232
+ /**
233
+ * Get document and update using Moongose wrapper method
234
+ *
235
+ * @param {*} filter
236
+ * @param {*} update
237
+ * @param {*} options
238
+ */
239
+ findOneAndUpdate(filter, update, options) {
240
+ return this.mgModel.findOneAndUpdate(filter, update, options);
241
+ }
242
+ /**
243
+ *
244
+ * @param {*} params
245
+ */
246
+ findOneNative(params) {
247
+ return this.mgModel.collection.findOne(params);
248
+ }
249
+ /**
250
+ *
251
+ * @param {*} item
252
+ */
253
+ beforeCreate(item, context) {
254
+ return Promise.resolve(item);
255
+ }
256
+ /**
257
+ *
258
+ * @param {*} item
259
+ */
260
+ beforeUpdate(item, context) {
261
+ return Promise.resolve(item);
262
+ }
263
+ /**
264
+ *
265
+ * @param {*} item
266
+ */
267
+ beforeCopy(item, context) {
268
+ return Promise.resolve(item);
269
+ }
270
+ /**
271
+ *
272
+ * @param {*} id
273
+ */
274
+ beforeRemove(id, context) {
275
+ return Promise.resolve(id);
276
+ }
277
+ /**
278
+ *
279
+ * @param {*} item
280
+ */
281
+ afterCreate(item, context) {
282
+ return Promise.resolve(item);
283
+ }
284
+ /**
285
+ *
286
+ * @param {*} item
287
+ */
288
+ afterUpdate(item, context) {
289
+ return Promise.resolve(item);
290
+ }
291
+ /**
292
+ *
293
+ * @param {*} item
294
+ */
295
+ afterCopy(item, context) {
296
+ return Promise.resolve(item);
297
+ }
298
+ /**
299
+ *
300
+ * @param {*} item
301
+ */
302
+ afterRemove(item, context) {
303
+ return Promise.resolve(item);
304
+ }
305
+ /**
306
+ *
307
+ * @param {*} params
308
+ */
309
+ manageFilterArrayParams(params) {
310
+ for (let k in params) {
311
+ if (!k.startsWith('$') && Array.isArray(params[k])) {
312
+ if (k === '_id' || k === 'id')
313
+ params[k] = params[k].map((p) => (0, mongo_utils_1.toObjectId)(p));
314
+ params[k] = { $in: params[k] };
315
+ }
316
+ }
317
+ }
318
+ /**
319
+ *
320
+ * @param {*} params
321
+ * @param {*} context
322
+ */
323
+ updateFilters(params, context) {
324
+ return __awaiter(this, void 0, void 0, function* () {
325
+ var _a;
326
+ if (!params)
327
+ params = {};
328
+ this.manageFilterArrayParams(params);
329
+ (_a = params.rangeDate) !== null && _a !== void 0 ? _a : (params.rangeDate = {});
330
+ let and = [];
331
+ if (params.rangeDate.eq) {
332
+ params.rangeDate.from = params.rangeDate.eq;
333
+ params.rangeDate.to = params.rangeDate.eq;
334
+ }
335
+ if (params.rangeDate.from)
336
+ and.push({ $or: [{ endDate: { $gte: params.rangeDate.from } }, { endDate: { $exists: false } }] });
337
+ if (params.rangeDate.to)
338
+ and.push({ $or: [{ startDate: { $lte: params.rangeDate.to } }, { startDate: { $exists: false } }] });
339
+ if (and.length)
340
+ params.$and = and;
341
+ delete params.rangeDate;
342
+ if (params.text)
343
+ params.$text = { $search: params.text };
344
+ delete params.text;
345
+ // TMP evaluate if useful
346
+ // for (let key in params)
347
+ // params[key] = parseTypedString(params[key]);
348
+ return params;
349
+ });
350
+ }
351
+ /**
352
+ *
353
+ * @param {*} sort
354
+ */
355
+ parseSortObject(sort) {
356
+ for (let k in sort) {
357
+ if (sort[k] === 0)
358
+ sort[k] = -1;
359
+ }
360
+ // fix to prevent error of skip limit occurring when results have a lot
361
+ // of items whith equal order
362
+ sort._id = 1;
363
+ return sort;
364
+ }
365
+ /**
366
+ * Create document using Moongose wrapper method
367
+ *
368
+ * @param {*} item
369
+ */
370
+ create(item, context) {
371
+ return __awaiter(this, void 0, void 0, function* () {
372
+ item = yield this.beforeCreate(item, context);
373
+ const itemDoc = this.mgModel.build(item);
374
+ const res = yield itemDoc.save(); //await this.mgModel.create(item);
375
+ if (context)
376
+ context.evtItem = res;
377
+ this.publishCRUDEvent({ type: enums_1.ModelCrudEventType.create, data: { uid: res.id, model: this.name } }, context);
378
+ return yield this.afterCreate(res, context);
379
+ });
380
+ }
381
+ /**
382
+ * Call insertOne mongodb native method
383
+ *
384
+ * @param {*} items
385
+ * @param {*} callback
386
+ */
387
+ insertOneNative(item, context) {
388
+ return __awaiter(this, void 0, void 0, function* () {
389
+ item = yield this.beforeCreate(item, context);
390
+ return this.mgModel.collection.insertOne(item);
391
+ });
392
+ }
393
+ /**
394
+ * Call insertMany Mongoose method
395
+ *
396
+ * @param {*} items
397
+ * @param {*} options
398
+ */
399
+ insertMany(items, options) {
400
+ return this.mgModel.insertMany(items, options);
401
+ }
402
+ /**
403
+ * Call insertMany mongodb native method
404
+ *
405
+ * @param {*} items
406
+ * @param {*} callback
407
+ */
408
+ insertManyNative(items, options) {
409
+ return this.mgModel.collection.insertMany(items, options);
410
+ }
411
+ /**
412
+ * Update document by ID using Mongoose wrapper method
413
+ *
414
+ * @param {*} id
415
+ * @param {*} item
416
+ */
417
+ update(id, item, param3, param4) {
418
+ return __awaiter(this, void 0, void 0, function* () {
419
+ let mergeItem = false;
420
+ let context = {};
421
+ if (!param4) {
422
+ context = param3;
423
+ }
424
+ else if (typeof param3 === 'boolean') {
425
+ mergeItem = param3;
426
+ context = param4;
427
+ }
428
+ item = yield this.beforeUpdate(item, context);
429
+ //const res = await this.mgModel.findByIdAndUpdate(id, { $set: item }, { new: true });
430
+ //const itemDoc = this.mgModel.build(Object.assign({ id, item }));
431
+ const itemDoc = yield this.get(id);
432
+ if (!itemDoc)
433
+ throw new errors_1.AppError('messages.notFound');
434
+ // complete merge
435
+ if (mergeItem)
436
+ item = (0, utils_1.merge)(itemDoc.toObject(), item); // Merge with existing doc
437
+ itemDoc === null || itemDoc === void 0 ? void 0 : itemDoc.set(item);
438
+ const res = yield itemDoc.save();
439
+ if (context)
440
+ context.evtItem = res;
441
+ this.publishCRUDEvent({ type: enums_1.ModelCrudEventType.update, data: { uid: id, model: this.name } }, context);
442
+ return yield this.afterUpdate(res, context);
443
+ });
444
+ }
445
+ /**
446
+ * Update document using mongodb generic method
447
+ *
448
+ * @param {*} filter
449
+ * @param {*} update
450
+ * @param {*} options
451
+ */
452
+ updateOne(filter, update, options) {
453
+ return this.mgModel.findOneAndUpdate(filter, update, options);
454
+ }
455
+ /**
456
+ * Update document using mongodb native method
457
+ *
458
+ * @param {*} filter
459
+ * @param {*} update
460
+ * @param {*} options
461
+ * @param {*} callback
462
+ */
463
+ updateNative(filter, update, options) {
464
+ return this.mgModel.collection.updateOne(filter, update, options);
465
+ }
466
+ /**
467
+ * Update many documents using mongodb native method
468
+ *
469
+ * @param {*} filter
470
+ * @param {*} update
471
+ * @param {*} options
472
+ */
473
+ updateManyNative(filter, update, options) {
474
+ return this.mgModel.collection.updateMany(filter, update, options);
475
+ }
476
+ /**
477
+ * Update document using Mongoose wrapper method
478
+ *
479
+ * @param {*} id
480
+ * @param {*} update
481
+ * @param {*} options
482
+ */
483
+ updateRaw(id, update, options) {
484
+ return this.mgModel.findByIdAndUpdate(id, update, options || { new: true });
485
+ }
486
+ /**
487
+ * Copy document by ID
488
+ *
489
+ * @param {*} id
490
+ */
491
+ copy(id, item, context) {
492
+ return __awaiter(this, void 0, void 0, function* () {
493
+ const foundItem = yield this.mgModel.findOne({ _id: (0, mongo_utils_1.toObjectId)(id) });
494
+ if (!foundItem)
495
+ return null;
496
+ const fiAttrs = foundItem.toObject();
497
+ delete fiAttrs.id;
498
+ delete fiAttrs._id;
499
+ item = Object.assign(fiAttrs, item);
500
+ item = yield this.beforeCopy(item, context);
501
+ const newItem = this.mgModel.build(item);
502
+ const res = yield (newItem === null || newItem === void 0 ? void 0 : newItem.save());
503
+ if (context)
504
+ context.evtItem = res;
505
+ this.publishCRUDEvent({ type: enums_1.ModelCrudEventType.create, data: { uid: res === null || res === void 0 ? void 0 : res.id, model: this.name } }, context);
506
+ return yield this.afterCopy(res, context);
507
+ });
508
+ }
509
+ /**
510
+ *
511
+ * @param {*} id
512
+ * @param {*} param1
513
+ * @param {*} options
514
+ */
515
+ addArrayItem(id_1, _a) {
516
+ return __awaiter(this, arguments, void 0, function* (id, { arrayPath, item }, options = { new: true }, context) {
517
+ var _b;
518
+ let update = { $addToSet: {} };
519
+ update.$addToSet[arrayPath] = item;
520
+ (_b = options.new) !== null && _b !== void 0 ? _b : (options.new = true);
521
+ const res = yield this.mgModel.findByIdAndUpdate(id, update, options);
522
+ this.publishCRUDEvent({ type: enums_1.ModelCrudEventType.addArrayItem, data: { uid: id, model: this.name, path: arrayPath } }, context);
523
+ return res;
524
+ });
525
+ }
526
+ /**
527
+ *
528
+ * @param {*} id
529
+ * @param {*} param1
530
+ * @param {*} options
531
+ * @param {*} callback
532
+ */
533
+ addArrayItemNative(id, { arrayPath, item }, options) {
534
+ let filter = { _id: id }, update = { $addToSet: {} };
535
+ update.$addToSet[arrayPath] = item;
536
+ return this.mgModel.collection.updateOne(filter, update, options);
537
+ }
538
+ /**
539
+ *
540
+ * @param {*} id
541
+ * @param {*} updateParams
542
+ * @param {*} options
543
+ */
544
+ updateArrayItem(id_1, _a) {
545
+ return __awaiter(this, arguments, void 0, function* (id, { arrayPath, arrayFilter, updateAttr, partial, item }, options = {}, context) {
546
+ var _b;
547
+ let filter = { _id: id }, update = { $set: {} };
548
+ filter[arrayPath] = { $elemMatch: arrayFilter };
549
+ if (updateAttr)
550
+ update.$set[`${arrayPath}.$.${updateAttr}`] = item[updateAttr];
551
+ else if (partial) {
552
+ for (let key in item)
553
+ update.$set[`${arrayPath}.$.${key}`] = item[key];
554
+ }
555
+ else {
556
+ update.$set[`${arrayPath}.$`] = item;
557
+ }
558
+ (_b = options.new) !== null && _b !== void 0 ? _b : (options.new = true);
559
+ const res = yield this.mgModel.findOneAndUpdate(filter, update, options);
560
+ this.publishCRUDEvent({ type: enums_1.ModelCrudEventType.updateArrayItem, data: { uid: id, model: this.name, path: arrayPath } }, context);
561
+ return res;
562
+ });
563
+ }
564
+ /**
565
+ *
566
+ * @param {*} id
567
+ * @param {*} updateParams
568
+ * @param {*} options
569
+ */
570
+ updateArrayItemNative(id, { arrayPath, arrayFilter, updateAttr, item }, options) {
571
+ let filter = { _id: id }, update = { $set: {} };
572
+ filter[arrayPath] = { $elemMatch: arrayFilter };
573
+ if (updateAttr)
574
+ update.$set[`${arrayPath}.$.${updateAttr}`] = item[updateAttr];
575
+ else
576
+ update.$set[`${arrayPath}.$`] = item;
577
+ return this.mgModel.collection.updateOne(filter, update, options);
578
+ }
579
+ /**
580
+ *
581
+ * @param {*} id
582
+ * @param {*} updateParams
583
+ * @param {*} options
584
+ */
585
+ updateArrayItemManyNative(filter, { arrayPath, arrayFilter, updateAttr, item }, options) {
586
+ let update = { $set: {} };
587
+ filter[arrayPath] = { $elemMatch: arrayFilter };
588
+ if (updateAttr)
589
+ update.$set[`${arrayPath}.$.${updateAttr}`] = item[updateAttr];
590
+ else
591
+ update.$set[`${arrayPath}.$`] = item;
592
+ return this.mgModel.collection.updateMany(filter, update, options);
593
+ }
594
+ /**
595
+ *
596
+ * @param {*} id
597
+ * @param {*} param1
598
+ * @param {*} options
599
+ */
600
+ removeArrayItem(id_1, _a) {
601
+ return __awaiter(this, arguments, void 0, function* (id, { arrayPath, item }, options = { new: true }, context) {
602
+ var _b;
603
+ let update = { $pull: {} };
604
+ update.$pull[arrayPath] = item;
605
+ (_b = options.new) !== null && _b !== void 0 ? _b : (options.new = true);
606
+ const res = yield this.mgModel.findByIdAndUpdate(id, update, options);
607
+ this.publishCRUDEvent({ type: enums_1.ModelCrudEventType.removeArrayItem, data: { uid: id, model: this.name, path: arrayPath } }, context);
608
+ return res;
609
+ });
610
+ }
611
+ /**
612
+ *
613
+ * @param {*} id
614
+ * @param {*} param1
615
+ * @param {*} options
616
+ */
617
+ removeArrayItemNative(id, { arrayPath, item }, options) {
618
+ let filter = { _id: id }, update = { $pull: {} };
619
+ update.$pull[arrayPath] = item;
620
+ return this.mgModel.collection.updateOne(filter, update, options);
621
+ }
622
+ /**
623
+ * Delete document using Mongoose wrapper method
624
+ *
625
+ * @param {*} id
626
+ */
627
+ remove(id, context) {
628
+ return __awaiter(this, void 0, void 0, function* () {
629
+ if (!id)
630
+ throw new Error('missing id');
631
+ id = yield this.beforeRemove(id, context);
632
+ const res = yield this.mgModel.findByIdAndDelete(id);
633
+ this.publishCRUDEvent({ type: enums_1.ModelCrudEventType.remove, data: { uid: id, model: this.name } }, context);
634
+ return this.afterRemove(res, context);
635
+ });
636
+ }
637
+ /**
638
+ *
639
+ * @param {*} filter
640
+ * @param {*} callback
641
+ */
642
+ removeNative(filter, options) {
643
+ return this.mgModel.collection.deleteMany(filter, options);
644
+ }
645
+ /**
646
+ *
647
+ * @param items
648
+ * @param context
649
+ * @returns
650
+ */
651
+ bulkCreate(items, context) {
652
+ if (!items)
653
+ return Promise.resolve([]);
654
+ ;
655
+ let promises = items.map(item => this.create(item, context));
656
+ return Promise.all(promises);
657
+ }
658
+ /**
659
+ *
660
+ * @param ids
661
+ * @param items
662
+ * @param context
663
+ * @returns
664
+ */
665
+ bulkUpdate(ids, items, context) {
666
+ if (!items)
667
+ return Promise.resolve([]);
668
+ let promises = ids.map((id, index) => this.update(id, Array.isArray(items) ? items[index] : items, context));
669
+ return Promise.all(promises);
670
+ }
671
+ /**
672
+ *
673
+ * @param ids
674
+ * @param context
675
+ * @returns
676
+ */
677
+ bulkRemove(ids, context) {
678
+ if (!Array.isArray(ids))
679
+ return Promise.resolve([]);
680
+ ;
681
+ let promises = ids.map(id => this.remove(id, context));
682
+ return Promise.all(promises);
683
+ }
684
+ /**
685
+ *
686
+ * @param {*} evt
687
+ * @param {*} context
688
+ */
689
+ publishCRUDEvent(evt, context = {}) {
690
+ if (!this.defaultParams.CRUDEvents)
691
+ return;
692
+ if (context === null || context === void 0 ? void 0 : context.evtType)
693
+ evt.type = context.evtType;
694
+ constants_1.APP.nxPubSub.publish(constants_1.MODEL_CRUD_EVENT, evt, context);
695
+ }
696
+ /**
697
+ *
698
+ * @param {*} name Name of new stream to be created
699
+ * @param {*} pipeline Filter pipeline
700
+ * @param {*} callback stream event callback
701
+ * @param {*} duration time in ms to terminate the stream
702
+ * @returns change stream data { name, duration, stream }
703
+ */
704
+ addChangeStream(name_1) {
705
+ return __awaiter(this, arguments, void 0, function* (name, pipeline = [], callback = () => { }, duration) {
706
+ constants_1.APP.logger.info(`Adding new change stream [${name}] - duration ${duration || 'infinte'}`, (0, utils_1.logClassCtx)(this, 'addChangeStream'));
707
+ const csData = { name, duration, stream: this.mgModel.watch(pipeline) };
708
+ csData.stream.on('change', (next) => callback(name, next));
709
+ if (duration)
710
+ yield this.closeChangeStream(csData);
711
+ return csData;
712
+ });
713
+ }
714
+ /**
715
+ *
716
+ * @param {*} csData change stream data { name, duration, stream }
717
+ * @returns
718
+ */
719
+ closeChangeStream({ name, stream, duration }) {
720
+ return new Promise((resolve) => {
721
+ setTimeout(() => {
722
+ constants_1.APP.logger.info(`Closing change stream [${name}]`, (0, utils_1.logClassCtx)(this, 'closeChangeStream'));
723
+ stream.close();
724
+ resolve();
725
+ }, duration || 60000);
726
+ });
727
+ }
728
+ ;
729
+ }
730
+ exports.BaseModel = BaseModel;