@resolveio/server-lib 9.0.32 → 9.0.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.
Files changed (193) hide show
  1. package/client-server-app.js +7 -48
  2. package/client-server-app.js.map +1 -1
  3. package/collections/app-status.collection.js +8 -11
  4. package/collections/app-status.collection.js.map +1 -1
  5. package/collections/app-version.collection.js +8 -11
  6. package/collections/app-version.collection.js.map +1 -1
  7. package/collections/counter.collection.js +8 -11
  8. package/collections/counter.collection.js.map +1 -1
  9. package/collections/cron-job-history.collection.js +9 -12
  10. package/collections/cron-job-history.collection.js.map +1 -1
  11. package/collections/cron-job.collection.js +8 -11
  12. package/collections/cron-job.collection.js.map +1 -1
  13. package/collections/email-history.collection.js +8 -11
  14. package/collections/email-history.collection.js.map +1 -1
  15. package/collections/email-verified.collection.js +9 -12
  16. package/collections/email-verified.collection.js.map +1 -1
  17. package/collections/file.collection.js +8 -11
  18. package/collections/file.collection.js.map +1 -1
  19. package/collections/flag.collection.js +8 -11
  20. package/collections/flag.collection.js.map +1 -1
  21. package/collections/log-method-latency.collection.js +11 -14
  22. package/collections/log-method-latency.collection.js.map +1 -1
  23. package/collections/log-subscription.collection.js +13 -16
  24. package/collections/log-subscription.collection.js.map +1 -1
  25. package/collections/log.collection.js +16 -19
  26. package/collections/log.collection.js.map +1 -1
  27. package/collections/logged-in-users.collection.js +9 -12
  28. package/collections/logged-in-users.collection.js.map +1 -1
  29. package/collections/method-call.collection.js +10 -13
  30. package/collections/method-call.collection.js.map +1 -1
  31. package/collections/method-response.collection.js +10 -13
  32. package/collections/method-response.collection.js.map +1 -1
  33. package/collections/monitor-cpu.collection.js +13 -16
  34. package/collections/monitor-cpu.collection.js.map +1 -1
  35. package/collections/monitor-https-inbound.collection.js +13 -16
  36. package/collections/monitor-https-inbound.collection.js.map +1 -1
  37. package/collections/monitor-https-outbound.collection.js +15 -18
  38. package/collections/monitor-https-outbound.collection.js.map +1 -1
  39. package/collections/monitor-memory.collection.js +12 -15
  40. package/collections/monitor-memory.collection.js.map +1 -1
  41. package/collections/monitor-mongo.collection.js +12 -15
  42. package/collections/monitor-mongo.collection.js.map +1 -1
  43. package/collections/notification.collection.js +8 -11
  44. package/collections/notification.collection.js.map +1 -1
  45. package/collections/queue-flag.collection.js +9 -12
  46. package/collections/queue-flag.collection.js.map +1 -1
  47. package/collections/queue-method.collection.js +9 -12
  48. package/collections/queue-method.collection.js.map +1 -1
  49. package/collections/queue-response.collection.js +9 -12
  50. package/collections/queue-response.collection.js.map +1 -1
  51. package/collections/report-builder-dashboard-builder.collection.js +10 -13
  52. package/collections/report-builder-dashboard-builder.collection.js.map +1 -1
  53. package/collections/report-builder-library.collection.js +11 -14
  54. package/collections/report-builder-library.collection.js.map +1 -1
  55. package/collections/report-builder-report.collection.js +11 -14
  56. package/collections/report-builder-report.collection.js.map +1 -1
  57. package/collections/user-group.collection.js +11 -14
  58. package/collections/user-group.collection.js.map +1 -1
  59. package/collections/user-guide.collection.js +8 -11
  60. package/collections/user-guide.collection.js.map +1 -1
  61. package/collections/user.collection.js +12 -15
  62. package/collections/user.collection.js.map +1 -1
  63. package/cron/cron.js +25 -25
  64. package/cron/cron.js.map +1 -1
  65. package/fixtures/cron-jobs.js +30 -80
  66. package/fixtures/cron-jobs.js.map +1 -1
  67. package/fixtures/init.js +9 -59
  68. package/fixtures/init.js.map +1 -1
  69. package/http/auth.js +344 -460
  70. package/http/auth.js.map +1 -1
  71. package/http/health.js +1 -5
  72. package/http/health.js.map +1 -1
  73. package/http/home.js +7 -11
  74. package/http/home.js.map +1 -1
  75. package/index.js +36 -98
  76. package/index.js.map +1 -1
  77. package/managers/cron.manager.js +78 -157
  78. package/managers/cron.manager.js.map +1 -1
  79. package/managers/method.manager.js +323 -303
  80. package/managers/method.manager.js.map +1 -1
  81. package/managers/mongo.manager.d.ts +14 -14
  82. package/managers/mongo.manager.js +794 -865
  83. package/managers/mongo.manager.js.map +1 -1
  84. package/managers/monitor.manager.js +171 -254
  85. package/managers/monitor.manager.js.map +1 -1
  86. package/managers/queue-method.manager.js +69 -166
  87. package/managers/queue-method.manager.js.map +1 -1
  88. package/managers/subscription.manager.js +515 -708
  89. package/managers/subscription.manager.js.map +1 -1
  90. package/methods/accounts.js +111 -183
  91. package/methods/accounts.js.map +1 -1
  92. package/methods/aws.js +156 -262
  93. package/methods/aws.js.map +1 -1
  94. package/methods/cloudconvert.js +100 -180
  95. package/methods/cloudconvert.js.map +1 -1
  96. package/methods/collections.js +114 -530
  97. package/methods/collections.js.map +1 -1
  98. package/methods/counters.js +29 -79
  99. package/methods/counters.js.map +1 -1
  100. package/methods/cron-jobs.js +819 -884
  101. package/methods/cron-jobs.js.map +1 -1
  102. package/methods/flags.js +1 -5
  103. package/methods/flags.js.map +1 -1
  104. package/methods/logs.js +258 -313
  105. package/methods/logs.js.map +1 -1
  106. package/methods/monitor.js +289 -348
  107. package/methods/monitor.js.map +1 -1
  108. package/methods/pdf.js +341 -340
  109. package/methods/pdf.js.map +1 -1
  110. package/methods/report-builder.js +565 -657
  111. package/methods/report-builder.js.map +1 -1
  112. package/methods/support.js +67 -185
  113. package/methods/support.js.map +1 -1
  114. package/models/app-status.model.js +1 -2
  115. package/models/app-version.model.js +1 -2
  116. package/models/billing-logged-in-users.model.js +1 -2
  117. package/models/collection-document.model.js +1 -2
  118. package/models/counter.model.js +1 -2
  119. package/models/cron-job-history.model.js +1 -2
  120. package/models/cron-job.model.js +1 -2
  121. package/models/dialog.model.js +1 -2
  122. package/models/email-history.model.js +1 -2
  123. package/models/email-verified.model.js +1 -2
  124. package/models/file.model.js +1 -2
  125. package/models/flag.model.js +1 -2
  126. package/models/log-method-latency.model.js +1 -2
  127. package/models/log-subscription.model.js +1 -2
  128. package/models/log.model.js +1 -2
  129. package/models/logged-in-users.model.js +1 -2
  130. package/models/method-call.model.js +1 -2
  131. package/models/method-response.model.js +1 -2
  132. package/models/method.model.js +1 -2
  133. package/models/monitor-cpu.model.js +1 -2
  134. package/models/monitor-https-inbound.model.js +1 -2
  135. package/models/monitor-https-outbound.model.js +1 -2
  136. package/models/monitor-memory.model.js +1 -2
  137. package/models/monitor-mongo.model.js +1 -2
  138. package/models/notification.model.js +1 -2
  139. package/models/pagination.model.js +2 -5
  140. package/models/pagination.model.js.map +1 -1
  141. package/models/permission.model.js +1 -2
  142. package/models/queue-method.model.js +1 -2
  143. package/models/queue-response.model.js +1 -2
  144. package/models/report-builder-dashboard-builder.model.js +1 -2
  145. package/models/report-builder-library.model.js +1 -2
  146. package/models/report-builder-report.model.js +1 -2
  147. package/models/report-builder.model.js +1 -2
  148. package/models/select-data-label.model.js +1 -2
  149. package/models/server-response.model.js +1 -2
  150. package/models/subscription.model.js +1 -2
  151. package/models/support-ticket.model.js +1 -2
  152. package/models/user-group.model.js +1 -2
  153. package/models/user-guide.model.js +1 -2
  154. package/models/user.model.js +1 -2
  155. package/package.json +1 -1
  156. package/public_api.js +70 -82
  157. package/public_api.js.map +1 -1
  158. package/publications/app-status.js +4 -8
  159. package/publications/app-status.js.map +1 -1
  160. package/publications/app-version.js +4 -8
  161. package/publications/app-version.js.map +1 -1
  162. package/publications/cron-jobs.js +9 -13
  163. package/publications/cron-jobs.js.map +1 -1
  164. package/publications/files.js +11 -15
  165. package/publications/files.js.map +1 -1
  166. package/publications/flags.js +7 -11
  167. package/publications/flags.js.map +1 -1
  168. package/publications/logs.js +31 -35
  169. package/publications/logs.js.map +1 -1
  170. package/publications/method-calls.js +4 -8
  171. package/publications/method-calls.js.map +1 -1
  172. package/publications/method-responses.js +4 -8
  173. package/publications/method-responses.js.map +1 -1
  174. package/publications/notifications.js +4 -8
  175. package/publications/notifications.js.map +1 -1
  176. package/publications/report-builder-dashboard-builders.js +8 -12
  177. package/publications/report-builder-dashboard-builders.js.map +1 -1
  178. package/publications/report-builder-libraries.js +16 -66
  179. package/publications/report-builder-libraries.js.map +1 -1
  180. package/publications/report-builder-reports.js +17 -21
  181. package/publications/report-builder-reports.js.map +1 -1
  182. package/publications/super-admin.js +3 -7
  183. package/publications/super-admin.js.map +1 -1
  184. package/publications/user-groups.js +4 -8
  185. package/publications/user-groups.js.map +1 -1
  186. package/publications/user-guides.js +4 -8
  187. package/publications/user-guides.js.map +1 -1
  188. package/server-app.js +393 -534
  189. package/server-app.js.map +1 -1
  190. package/util/common.js +55 -92
  191. package/util/common.js.map +1 -1
  192. package/util/schema-report-builder.js +76 -134
  193. package/util/schema-report-builder.js.map +1 -1
@@ -1,81 +1,29 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- extendStatics(d, b);
11
- function __() { this.constructor = d; }
12
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
- };
14
- })();
15
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
16
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
17
- return new (P || (P = Promise))(function (resolve, reject) {
18
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
19
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
20
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
21
- step((generator = generator.apply(thisArg, _arguments || [])).next());
22
- });
23
- };
24
- var __generator = (this && this.__generator) || function (thisArg, body) {
25
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
26
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
27
- function verb(n) { return function (v) { return step([n, v]); }; }
28
- function step(op) {
29
- if (f) throw new TypeError("Generator is already executing.");
30
- while (_) try {
31
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
32
- if (y = 0, t) op = [op[0] & 2, t.value];
33
- switch (op[0]) {
34
- case 0: case 1: t = op; break;
35
- case 4: _.label++; return { value: op[1], done: false };
36
- case 5: _.label++; y = op[1]; op = [0]; continue;
37
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
38
- default:
39
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
40
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
41
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
42
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
43
- if (t[2]) _.ops.pop();
44
- _.trys.pop(); continue;
45
- }
46
- op = body.call(thisArg, _);
47
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
48
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
49
- }
50
- };
51
- Object.defineProperty(exports, "__esModule", { value: true });
52
- exports.objectIdHexString = exports.UserCollection = exports.Collection = exports.Model = exports.MongoManager = void 0;
53
- var simpl_schema_1 = require("simpl-schema");
54
- var mongodb_1 = require("mongodb");
55
- var index_1 = require("../index");
56
- var user_collection_1 = require("../collections/user.collection");
57
- var common_1 = require("../util/common");
58
- var log_collection_1 = require("../collections/log.collection");
59
- var crypto = require('crypto');
60
- var scmp = require('scmp');
61
- var MongoManager = /** @class */ (function () {
62
- function MongoManager() {
1
+ import SimpleSchema from 'simpl-schema';
2
+ import { ObjectId } from 'mongodb';
3
+ import { ResolveIOServer } from '../index';
4
+ import { Users } from '../collections/user.collection';
5
+ import { buildRbLookups, buildRbSchema, deepCopy, getBinarySize, getMongoMergeUpdatedDoc } from '../util/common';
6
+ import { Logs } from '../collections/log.collection';
7
+ const crypto = require('crypto');
8
+ const scmp = require('scmp');
9
+ export class MongoManager {
10
+ constructor() {
63
11
  this._collections = [];
64
12
  this._mongoQueue = [];
65
13
  this._mongoQueueId = 0;
66
14
  this.runQueue();
67
15
  }
68
- MongoManager.prototype.registerCollection = function (collection) {
16
+ registerCollection(collection) {
69
17
  this._collections.push(collection);
70
- };
71
- MongoManager.prototype.collections = function () {
18
+ }
19
+ collections() {
72
20
  return this._collections;
73
- };
74
- MongoManager.prototype.collection = function (collectionName) {
75
- return this._collections.filter(function (a) { return a.collectionName === collectionName; })[0];
76
- };
77
- MongoManager.prototype.addToQueue = function (mongoQueue) {
78
- var _mongoQueue = this._mongoQueue.filter(function (a) { return a.name_collection === mongoQueue.name_collection && a.name_function === mongoQueue.name_function && a.name_function_addt === mongoQueue.name_function_addt && JSON.stringify(a.data_function) === JSON.stringify(mongoQueue.data_function) && JSON.stringify(a.data_function_addt) === JSON.stringify(mongoQueue.data_function_addt); })[0];
21
+ }
22
+ collection(collectionName) {
23
+ return this._collections.filter(a => a.collectionName === collectionName)[0];
24
+ }
25
+ addToQueue(mongoQueue) {
26
+ let _mongoQueue = this._mongoQueue.filter(a => a.name_collection === mongoQueue.name_collection && a.name_function === mongoQueue.name_function && a.name_function_addt === mongoQueue.name_function_addt && JSON.stringify(a.data_function) === JSON.stringify(mongoQueue.data_function) && JSON.stringify(a.data_function_addt) === JSON.stringify(mongoQueue.data_function_addt))[0];
79
27
  if (_mongoQueue) {
80
28
  if (!_mongoQueue.running) {
81
29
  _mongoQueue.cbs.push(mongoQueue.cbs[0]);
@@ -89,78 +37,64 @@ var MongoManager = /** @class */ (function () {
89
37
  this._mongoQueueId += 1;
90
38
  this._mongoQueue.push(mongoQueue);
91
39
  }
92
- };
93
- MongoManager.prototype.runQueue = function () {
94
- return __awaiter(this, void 0, void 0, function () {
95
- var _this = this;
96
- return __generator(this, function (_a) {
97
- 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);
106
- });
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
- });
40
+ }
41
+ async runQueue() {
42
+ setInterval(() => {
43
+ if (this._mongoQueue.filter(a => !a.running).length) {
44
+ let mongoQueue = this._mongoQueue.filter(a => !a.running)[0];
45
+ mongoQueue.running = true;
46
+ if (!mongoQueue.name_function_addt) {
47
+ ResolveIOServer.getMainDB().collection(mongoQueue.name_collection)[mongoQueue.name_function](...mongoQueue.data_function).then(res => {
48
+ mongoQueue.cbs.forEach(cb => {
49
+ cb(null, res);
50
+ });
51
+ if (mongoQueue.cbs_next.length) {
52
+ mongoQueue.running = false;
53
+ mongoQueue.cbs = mongoQueue.cbs_next;
54
+ mongoQueue.cbs_next = [];
124
55
  }
125
56
  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 = []; // BUG???
134
- }
135
- else {
136
- _this._mongoQueue.splice(_this._mongoQueue.map(function (a) { return a._id; }).indexOf(mongoQueue_1._id), 1);
137
- }
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
- });
57
+ this._mongoQueue.splice(this._mongoQueue.map(a => a._id).indexOf(mongoQueue._id), 1);
147
58
  }
148
- }
149
- }, 10);
150
- return [2 /*return*/];
151
- });
152
- });
153
- };
154
- return MongoManager;
155
- }());
156
- exports.MongoManager = MongoManager;
157
- var Model = /** @class */ (function () {
158
- function Model(collectionName, schema, useVersionCollection, useReportBuilder, reportBuilderLookupTables, timestamps, createLogs) {
159
- if (useVersionCollection === void 0) { useVersionCollection = false; }
160
- if (useReportBuilder === void 0) { useReportBuilder = true; }
161
- if (reportBuilderLookupTables === void 0) { reportBuilderLookupTables = []; }
162
- if (timestamps === void 0) { timestamps = true; }
163
- if (createLogs === void 0) { createLogs = true; }
59
+ }, err => {
60
+ mongoQueue.cbs.forEach(cb => {
61
+ cb(err, null);
62
+ });
63
+ mongoQueue.cbs_next.forEach(cb => {
64
+ cb(err, null);
65
+ });
66
+ this._mongoQueue.splice(this._mongoQueue.map(a => a._id).indexOf(mongoQueue._id), 1);
67
+ });
68
+ }
69
+ else {
70
+ ResolveIOServer.getMainDB().collection(mongoQueue.name_collection)[mongoQueue.name_function](...mongoQueue.data_function)[mongoQueue.name_function_addt](...mongoQueue.data_function_addt).then(res => {
71
+ mongoQueue.cbs.forEach(cb => {
72
+ cb(null, res);
73
+ });
74
+ if (mongoQueue.cbs_next.length) {
75
+ mongoQueue.running = false;
76
+ mongoQueue.cbs = mongoQueue.cbs_next;
77
+ mongoQueue.cbs_next = []; // BUG???
78
+ }
79
+ else {
80
+ this._mongoQueue.splice(this._mongoQueue.map(a => a._id).indexOf(mongoQueue._id), 1);
81
+ }
82
+ }, err => {
83
+ mongoQueue.cbs.forEach(cb => {
84
+ cb(err, null);
85
+ });
86
+ mongoQueue.cbs_next.forEach(cb => {
87
+ cb(err, null);
88
+ });
89
+ this._mongoQueue.splice(this._mongoQueue.map(a => a._id).indexOf(mongoQueue._id), 1);
90
+ });
91
+ }
92
+ }
93
+ }, 10);
94
+ }
95
+ }
96
+ export class Model {
97
+ constructor(collectionName, schema, useVersionCollection = false, useReportBuilder = true, reportBuilderLookupTables = [], timestamps = true, createLogs = true) {
164
98
  this.collection_main = null;
165
99
  this.collection_version = null;
166
100
  if (collectionName === 'users') {
@@ -170,7 +104,8 @@ var Model = /** @class */ (function () {
170
104
  this.collection_main = new Collection(collectionName, schema, useReportBuilder, reportBuilderLookupTables, timestamps, createLogs);
171
105
  }
172
106
  if (useVersionCollection) {
173
- var versionSchema = common_1.deepCopy(schema);
107
+ this.collection_main.useVersions = true;
108
+ let versionSchema = deepCopy(schema);
174
109
  versionSchema['_id'].type = 'Object';
175
110
  versionSchema['_id'].blackbox = true;
176
111
  if (collectionName === 'users') {
@@ -183,48 +118,44 @@ var Model = /** @class */ (function () {
183
118
  this.collection_main.versionCollection = collectionName + '.versions';
184
119
  }
185
120
  }
186
- return Model;
187
- }());
188
- exports.Model = Model;
189
- var Collection = /** @class */ (function () {
190
- function Collection(collectionName, schema, useReportBuilder, reportBuilderLookupTables, timestamps, createLogs) {
191
- var _this = this;
121
+ }
122
+ export class Collection {
123
+ constructor(collectionName, schema, useReportBuilder, reportBuilderLookupTables, timestamps, createLogs) {
192
124
  this.collectionName = '';
193
125
  this.simplschema = null;
194
126
  this.rbSchema = null;
195
127
  this.timestamps = false;
196
- this.versionKey = '';
128
+ this.useVersions = false;
197
129
  this.versionCollection = '';
198
130
  this.createLogs = true;
199
131
  this.useRB = false;
200
132
  this.collectionName = collectionName;
201
- this.simplschema = new simpl_schema_1.default(schema);
133
+ this.simplschema = new SimpleSchema(schema);
202
134
  this.timestamps = timestamps;
203
135
  this.createLogs = createLogs;
204
136
  if (useReportBuilder) {
205
137
  this.useRB = true;
206
- var schemaCopy = common_1.deepCopy(schema);
207
- var rbSchema = common_1.buildRbLookups(reportBuilderLookupTables, schemaCopy, []);
208
- this.rbSchema = common_1.buildRbSchema(rbSchema);
138
+ let schemaCopy = deepCopy(schema);
139
+ let rbSchema = buildRbLookups(reportBuilderLookupTables, schemaCopy, []);
140
+ this.rbSchema = buildRbSchema(rbSchema);
209
141
  }
210
- var interval = setInterval(function () {
211
- if (index_1.ResolveIOServer && index_1.ResolveIOServer.getMainServer() && index_1.ResolveIOServer.getMongoManager()) {
212
- index_1.ResolveIOServer.getMongoManager().registerCollection(_this);
142
+ let interval = setInterval(() => {
143
+ if (ResolveIOServer && ResolveIOServer.getMainServer() && ResolveIOServer.getMongoManager()) {
144
+ ResolveIOServer.getMongoManager().registerCollection(this);
213
145
  clearInterval(interval);
214
146
  }
215
147
  }, 1);
216
148
  }
217
- Collection.prototype.aggregate = function (pipeline, options) {
218
- var _this = this;
219
- return new Promise(function (resolve, reject) {
220
- index_1.ResolveIOServer.getMongoManager().addToQueue({
149
+ aggregate(pipeline, options) {
150
+ return new Promise((resolve, reject) => {
151
+ ResolveIOServer.getMongoManager().addToQueue({
221
152
  _id: 0,
222
- name_collection: _this.collectionName,
153
+ name_collection: this.collectionName,
223
154
  name_function: 'aggregate',
224
155
  name_function_addt: 'toArray',
225
156
  data_function: [pipeline, options],
226
157
  data_function_addt: [],
227
- cbs: [function (err, res) {
158
+ cbs: [(err, res) => {
228
159
  if (err) {
229
160
  reject(err);
230
161
  }
@@ -236,18 +167,17 @@ var Collection = /** @class */ (function () {
236
167
  running: false
237
168
  });
238
169
  });
239
- };
240
- Collection.prototype.aggregateCount = function (pipeline, options) {
241
- var _this = this;
242
- return new Promise(function (resolve, reject) {
243
- index_1.ResolveIOServer.getMongoManager().addToQueue({
170
+ }
171
+ aggregateCount(pipeline, options) {
172
+ return new Promise((resolve, reject) => {
173
+ ResolveIOServer.getMongoManager().addToQueue({
244
174
  _id: 0,
245
- name_collection: _this.collectionName,
175
+ name_collection: this.collectionName,
246
176
  name_function: 'aggregate',
247
177
  name_function_addt: 'count',
248
178
  data_function: [pipeline, options],
249
179
  data_function_addt: [],
250
- cbs: [function (err, res) {
180
+ cbs: [(err, res) => {
251
181
  if (err) {
252
182
  reject(err);
253
183
  }
@@ -259,136 +189,116 @@ var Collection = /** @class */ (function () {
259
189
  running: false
260
190
  });
261
191
  });
262
- };
263
- Collection.prototype.aggregateCursor = function (pipeline, options) {
264
- return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).aggregate(pipeline, options);
265
- };
266
- Collection.prototype.aggregateStream = function (pipeline, options) {
267
- return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).aggregate(pipeline, options).stream();
268
- };
269
- Collection.prototype.countDocuments = function (filter, options) {
270
- if (filter === void 0) { filter = {}; }
271
- return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).countDocuments(filter, options);
272
- };
273
- Collection.prototype.create = function (f_docs, options) {
274
- var docs = [];
192
+ }
193
+ aggregateCursor(pipeline, options) {
194
+ return ResolveIOServer.getMainDB().collection(this.collectionName).aggregate(pipeline, options);
195
+ }
196
+ aggregateStream(pipeline, options) {
197
+ return ResolveIOServer.getMainDB().collection(this.collectionName).aggregate(pipeline, options).stream();
198
+ }
199
+ countDocuments(filter = {}, options) {
200
+ return ResolveIOServer.getMainDB().collection(this.collectionName).countDocuments(filter, options);
201
+ }
202
+ create(f_docs, options) {
203
+ let docs = [];
275
204
  if (!Array.isArray(f_docs)) {
276
205
  return this.insertOne(f_docs, options);
277
206
  }
278
207
  return this.insertMany(f_docs, options);
279
- };
280
- Collection.prototype.createIndex = function (fieldOrSpec, options) {
281
- return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).createIndex(fieldOrSpec, options);
282
- };
283
- Collection.prototype.createIndexes = function (indexSpecs, options) {
284
- return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).createIndexes(indexSpecs, options);
285
- };
286
- Collection.prototype.deleteMany = function (filter, options) {
287
- var _this = this;
288
- if (filter === void 0) { filter = {}; }
289
- return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
290
- var docs;
291
- var _this = this;
292
- return __generator(this, function (_a) {
293
- switch (_a.label) {
294
- case 0:
295
- if (!this.createLogs) return [3 /*break*/, 2];
296
- return [4 /*yield*/, this.find(filter)];
297
- case 1:
298
- docs = _a.sent();
299
- docs.forEach(function (doc) {
300
- log_collection_1.Logs.create({
301
- _id: objectIdHexString(),
302
- type: 'document',
303
- collection: _this.collectionName,
304
- id_document: doc['_id'],
305
- payload: common_1.getBinarySize(JSON.stringify([doc, filter, options])) < 200000 ? JSON.stringify([doc, filter, options], null, 2) : 'Too Big',
306
- method: 'deleteMany',
307
- id_user: '',
308
- user: '',
309
- messageId: '',
310
- route: ''
311
- });
312
- });
313
- _a.label = 2;
314
- case 2:
315
- index_1.ResolveIOServer.getMainDB().collection(this.collectionName).deleteMany(filter, options).then(function (res) {
316
- if (res.result.ok) {
317
- resolve(res.deletedCount);
318
- }
319
- else {
320
- reject(res.result.ok);
321
- }
322
- }, function (err) { return reject(err); });
323
- return [2 /*return*/];
208
+ }
209
+ createIndex(fieldOrSpec, options) {
210
+ return ResolveIOServer.getMainDB().collection(this.collectionName).createIndex(fieldOrSpec, options);
211
+ }
212
+ createIndexes(indexSpecs, options) {
213
+ return ResolveIOServer.getMainDB().collection(this.collectionName).createIndexes(indexSpecs, options);
214
+ }
215
+ deleteMany(filter = {}, options, bypassLogs = false) {
216
+ return new Promise(async (resolve, reject) => {
217
+ if (this.createLogs && !bypassLogs) {
218
+ let docs = await this.find(filter);
219
+ docs.forEach(doc => {
220
+ Logs.insertOne({
221
+ _id: objectIdHexString(),
222
+ type: 'document',
223
+ collection: this.collectionName,
224
+ id_document: doc['_id'],
225
+ payload: getBinarySize(JSON.stringify([doc, filter, options])) < 200000 ? JSON.stringify([doc, filter, options], null, 2) : 'Too Big',
226
+ method: 'deleteMany',
227
+ id_user: '',
228
+ user: '',
229
+ messageId: '',
230
+ route: ''
231
+ });
232
+ if (this.useVersions) {
233
+ doc['_id'] = { _id: doc['_id'], __v: doc['__v'] };
234
+ ResolveIOServer.getMongoManager().collection(this.versionCollection).insertOne(doc);
235
+ }
236
+ });
237
+ }
238
+ ResolveIOServer.getMainDB().collection(this.collectionName).deleteMany(filter, options).then(res => {
239
+ if (res.result.ok) {
240
+ resolve(res.deletedCount);
324
241
  }
325
- });
326
- }); });
327
- };
328
- Collection.prototype.deleteOne = function (filter, options) {
329
- var _this = this;
330
- if (filter === void 0) { filter = {}; }
331
- return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
332
- var doc;
333
- return __generator(this, function (_a) {
334
- switch (_a.label) {
335
- case 0:
336
- if (!this.createLogs) return [3 /*break*/, 2];
337
- return [4 /*yield*/, this.findOne(filter)];
338
- case 1:
339
- doc = _a.sent();
340
- if (doc) {
341
- log_collection_1.Logs.create({
342
- _id: objectIdHexString(),
343
- type: 'document',
344
- collection: this.collectionName,
345
- id_document: doc['_id'],
346
- payload: common_1.getBinarySize(JSON.stringify([doc, filter, options])) < 200000 ? JSON.stringify([doc, filter, options], null, 2) : 'Too Big',
347
- method: 'deleteOne',
348
- id_user: '',
349
- user: '',
350
- messageId: '',
351
- route: ''
352
- });
353
- }
354
- _a.label = 2;
355
- case 2:
356
- index_1.ResolveIOServer.getMainDB().collection(this.collectionName).deleteOne(filter, options).then(function (res) {
357
- if (res.result.ok) {
358
- resolve(res.deletedCount);
359
- }
360
- else {
361
- reject(res.result.ok);
362
- }
363
- }, function (err) { return reject(err); });
364
- return [2 /*return*/];
242
+ else {
243
+ reject(res.result.ok);
365
244
  }
366
- });
367
- }); });
368
- };
369
- Collection.prototype.distinct = function (key, filter, options) {
370
- return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).distinct(key, filter, options);
371
- };
372
- Collection.prototype.drop = function (options) {
373
- return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).drop(options);
374
- };
375
- Collection.prototype.dropIndex = function (indexName, options) {
376
- return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).dropIndex(indexName, options);
377
- };
378
- Collection.prototype.dropIndexes = function (options) {
379
- return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).dropIndexes(options);
380
- };
381
- Collection.prototype.find = function (filter, options) {
382
- var _this = this;
383
- if (filter === void 0) { filter = {}; }
384
- return new Promise(function (resolve, reject) {
385
- if (_this.createLogs) {
386
- log_collection_1.Logs.create({
245
+ }, err => reject(err));
246
+ });
247
+ }
248
+ deleteOne(filter = {}, options, bypassLogs = false) {
249
+ return new Promise(async (resolve, reject) => {
250
+ if (this.createLogs && !bypassLogs) {
251
+ let doc = await this.findOne(filter);
252
+ if (doc) {
253
+ Logs.insertOne({
254
+ _id: objectIdHexString(),
255
+ type: 'document',
256
+ collection: this.collectionName,
257
+ id_document: doc['_id'],
258
+ payload: getBinarySize(JSON.stringify([doc, filter, options])) < 200000 ? JSON.stringify([doc, filter, options], null, 2) : 'Too Big',
259
+ method: 'deleteOne',
260
+ id_user: '',
261
+ user: '',
262
+ messageId: '',
263
+ route: ''
264
+ });
265
+ if (this.useVersions) {
266
+ doc['_id'] = { _id: doc['_id'], __v: doc['__v'] };
267
+ ResolveIOServer.getMongoManager().collection(this.versionCollection).insertOne(doc);
268
+ }
269
+ }
270
+ }
271
+ ResolveIOServer.getMainDB().collection(this.collectionName).deleteOne(filter, options).then(res => {
272
+ if (res.result.ok) {
273
+ resolve(res.deletedCount);
274
+ }
275
+ else {
276
+ reject(res.result.ok);
277
+ }
278
+ }, err => reject(err));
279
+ });
280
+ }
281
+ distinct(key, filter, options) {
282
+ return ResolveIOServer.getMainDB().collection(this.collectionName).distinct(key, filter, options);
283
+ }
284
+ drop(options) {
285
+ return ResolveIOServer.getMainDB().collection(this.collectionName).drop(options);
286
+ }
287
+ dropIndex(indexName, options) {
288
+ return ResolveIOServer.getMainDB().collection(this.collectionName).dropIndex(indexName, options);
289
+ }
290
+ dropIndexes(options) {
291
+ return ResolveIOServer.getMainDB().collection(this.collectionName).dropIndexes(options);
292
+ }
293
+ find(filter = {}, options, bypassLogs = false) {
294
+ return new Promise((resolve, reject) => {
295
+ if (this.createLogs && !bypassLogs) {
296
+ Logs.insertOne({
387
297
  _id: objectIdHexString(),
388
298
  type: 'query',
389
- collection: _this.collectionName,
299
+ collection: this.collectionName,
390
300
  id_document: '',
391
- payload: common_1.getBinarySize(JSON.stringify([filter, options])) < 200000 ? JSON.stringify([filter, options], null, 2) : 'Too Big',
301
+ payload: getBinarySize(JSON.stringify([filter, options])) < 200000 ? JSON.stringify([filter, options], null, 2) : 'Too Big',
392
302
  method: 'find',
393
303
  id_user: '',
394
304
  user: '',
@@ -396,14 +306,14 @@ var Collection = /** @class */ (function () {
396
306
  route: ''
397
307
  });
398
308
  }
399
- index_1.ResolveIOServer.getMongoManager().addToQueue({
309
+ ResolveIOServer.getMongoManager().addToQueue({
400
310
  _id: 0,
401
- name_collection: _this.collectionName,
311
+ name_collection: this.collectionName,
402
312
  name_function: 'find',
403
313
  name_function_addt: 'toArray',
404
314
  data_function: [filter, options],
405
315
  data_function_addt: [],
406
- cbs: [function (err, res) {
316
+ cbs: [(err, res) => {
407
317
  if (err) {
408
318
  reject(err);
409
319
  }
@@ -415,19 +325,18 @@ var Collection = /** @class */ (function () {
415
325
  running: false
416
326
  });
417
327
  });
418
- };
419
- Collection.prototype.findById = function (id, options) {
420
- return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).findOne({ _id: id }, options);
421
- };
422
- Collection.prototype.findCount = function (filter, options) {
423
- if (filter === void 0) { filter = {}; }
424
- if (this.createLogs) {
425
- log_collection_1.Logs.create({
328
+ }
329
+ findById(id, options) {
330
+ return ResolveIOServer.getMainDB().collection(this.collectionName).findOne({ _id: id }, options);
331
+ }
332
+ findCount(filter = {}, options, bypassLogs = false) {
333
+ if (this.createLogs && !bypassLogs) {
334
+ Logs.insertOne({
426
335
  _id: objectIdHexString(),
427
336
  type: 'query',
428
337
  collection: this.collectionName,
429
338
  id_document: '',
430
- payload: common_1.getBinarySize(JSON.stringify([filter, options])) < 200000 ? JSON.stringify([filter, options], null, 2) : 'Too Big',
339
+ payload: getBinarySize(JSON.stringify([filter, options])) < 200000 ? JSON.stringify([filter, options], null, 2) : 'Too Big',
431
340
  method: 'findCount',
432
341
  id_user: '',
433
342
  user: '',
@@ -435,27 +344,23 @@ var Collection = /** @class */ (function () {
435
344
  route: ''
436
345
  });
437
346
  }
438
- return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).find(filter, options).count();
439
- };
440
- Collection.prototype.findCursor = function (filter, options) {
441
- if (filter === void 0) { filter = {}; }
442
- return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).find(filter, options);
443
- };
444
- Collection.prototype.findStream = function (filter, options, streamOptions) {
445
- if (filter === void 0) { filter = {}; }
446
- return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).find(filter, options).stream(streamOptions);
447
- };
448
- Collection.prototype.findOne = function (filter, options) {
449
- var _this = this;
450
- if (filter === void 0) { filter = {}; }
451
- return new Promise(function (resolve, reject) {
452
- if (_this.createLogs) {
453
- log_collection_1.Logs.create({
347
+ return ResolveIOServer.getMainDB().collection(this.collectionName).find(filter, options).count();
348
+ }
349
+ findCursor(filter = {}, options) {
350
+ return ResolveIOServer.getMainDB().collection(this.collectionName).find(filter, options);
351
+ }
352
+ findStream(filter = {}, options, streamOptions) {
353
+ return ResolveIOServer.getMainDB().collection(this.collectionName).find(filter, options).stream(streamOptions);
354
+ }
355
+ findOne(filter = {}, options, bypassLogs = false) {
356
+ return new Promise((resolve, reject) => {
357
+ if (this.createLogs && !bypassLogs) {
358
+ Logs.insertOne({
454
359
  _id: objectIdHexString(),
455
360
  type: 'query',
456
- collection: _this.collectionName,
361
+ collection: this.collectionName,
457
362
  id_document: '',
458
- payload: common_1.getBinarySize(JSON.stringify([filter, options])) < 200000 ? JSON.stringify([filter, options], null, 2) : 'Too Big',
363
+ payload: getBinarySize(JSON.stringify([filter, options])) < 200000 ? JSON.stringify([filter, options], null, 2) : 'Too Big',
459
364
  method: 'findOne',
460
365
  id_user: '',
461
366
  user: '',
@@ -463,14 +368,14 @@ var Collection = /** @class */ (function () {
463
368
  route: ''
464
369
  });
465
370
  }
466
- index_1.ResolveIOServer.getMongoManager().addToQueue({
371
+ ResolveIOServer.getMongoManager().addToQueue({
467
372
  _id: 0,
468
- name_collection: _this.collectionName,
373
+ name_collection: this.collectionName,
469
374
  name_function: 'findOne',
470
375
  name_function_addt: '',
471
376
  data_function: [filter, options],
472
377
  data_function_addt: [],
473
- cbs: [function (err, res) {
378
+ cbs: [(err, res) => {
474
379
  if (err) {
475
380
  reject(err);
476
381
  }
@@ -482,248 +387,251 @@ var Collection = /** @class */ (function () {
482
387
  running: false
483
388
  });
484
389
  });
485
- };
486
- Collection.prototype.findOneAndDelete = function (filter, options) {
487
- var _this = this;
488
- if (filter === void 0) { filter = {}; }
489
- return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
490
- var doc;
491
- return __generator(this, function (_a) {
492
- switch (_a.label) {
493
- case 0:
494
- if (!this.createLogs) return [3 /*break*/, 2];
495
- return [4 /*yield*/, this.findOne(filter)];
496
- case 1:
497
- doc = _a.sent();
498
- if (doc) {
499
- log_collection_1.Logs.create({
500
- _id: objectIdHexString(),
501
- type: 'document',
502
- collection: this.collectionName,
503
- id_document: doc['_id'],
504
- payload: common_1.getBinarySize(JSON.stringify([doc, filter, options])) < 200000 ? JSON.stringify([doc, filter, options], null, 2) : 'Too Big',
505
- method: 'findOneAndDelete',
506
- id_user: '',
507
- user: '',
508
- messageId: '',
509
- route: ''
510
- });
511
- }
512
- _a.label = 2;
513
- case 2:
514
- index_1.ResolveIOServer.getMainDB().collection(this.collectionName).findOneAndDelete(filter, options).then(function (res) {
515
- if (res.ok) {
516
- resolve(res.value);
517
- }
518
- else {
519
- reject(res.lastErrorObject);
520
- }
521
- }, function (err) { return reject(err); });
522
- return [2 /*return*/];
390
+ }
391
+ findOneAndDelete(filter = {}, options, bypassLogs = false) {
392
+ return new Promise(async (resolve, reject) => {
393
+ if (this.createLogs && !bypassLogs) {
394
+ let doc = await this.findOne(filter);
395
+ if (doc) {
396
+ Logs.insertOne({
397
+ _id: objectIdHexString(),
398
+ type: 'document',
399
+ collection: this.collectionName,
400
+ id_document: doc['_id'],
401
+ payload: getBinarySize(JSON.stringify([doc, filter, options])) < 200000 ? JSON.stringify([doc, filter, options], null, 2) : 'Too Big',
402
+ method: 'findOneAndDelete',
403
+ id_user: '',
404
+ user: '',
405
+ messageId: '',
406
+ route: ''
407
+ });
523
408
  }
524
- });
525
- }); });
526
- };
527
- Collection.prototype.findOneAndReplace = function (filter, replacement, options) {
528
- var _this = this;
529
- if (filter === void 0) { filter = {}; }
530
- return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
531
- var doc;
532
- return __generator(this, function (_a) {
533
- switch (_a.label) {
534
- case 0:
535
- replacement['updatedAt'] = new Date();
536
- if (!this.createLogs) return [3 /*break*/, 2];
537
- return [4 /*yield*/, this.findOne(filter)];
538
- case 1:
539
- doc = _a.sent();
540
- if (doc) {
541
- log_collection_1.Logs.create({
542
- _id: objectIdHexString(),
543
- type: 'document',
544
- collection: this.collectionName,
545
- id_document: doc['_id'],
546
- payload: common_1.getBinarySize(JSON.stringify([doc, filter, replacement, options])) < 200000 ? JSON.stringify([doc, filter, replacement, options], null, 2) : 'Too Big',
547
- method: 'findOneAndReplace',
548
- id_user: '',
549
- user: '',
550
- messageId: '',
551
- route: ''
552
- });
553
- }
554
- _a.label = 2;
555
- case 2:
556
- index_1.ResolveIOServer.getMainDB().collection(this.collectionName).findOneAndReplace(filter, replacement, options).then(function (res) {
557
- if (res.ok) {
558
- resolve(res.value);
559
- }
560
- else {
561
- reject(res.lastErrorObject);
562
- }
563
- }, function (err) { return reject(err); });
564
- return [2 /*return*/];
409
+ }
410
+ ResolveIOServer.getMainDB().collection(this.collectionName).findOneAndDelete(filter, options).then(res => {
411
+ if (res.ok) {
412
+ resolve(res.value);
565
413
  }
566
- });
567
- }); });
568
- };
569
- Collection.prototype.findOneAndUpdate = function (filter, update, options) {
570
- var _this = this;
571
- if (filter === void 0) { filter = {}; }
572
- return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
573
- var doc;
574
- return __generator(this, function (_a) {
575
- switch (_a.label) {
576
- case 0:
577
- if (!update['$set']) {
578
- update['$set'] = { updatedAt: new Date() };
579
- }
580
- else {
581
- update['$set']['updatedAt'] = new Date();
582
- }
583
- if (!this.createLogs) return [3 /*break*/, 2];
584
- return [4 /*yield*/, this.findOne(filter)];
585
- case 1:
586
- doc = _a.sent();
587
- if (doc) {
588
- log_collection_1.Logs.create({
589
- _id: objectIdHexString(),
590
- type: 'document',
591
- collection: this.collectionName,
592
- id_document: doc['_id'],
593
- payload: common_1.getBinarySize(JSON.stringify([doc, filter, update, options])) < 200000 ? JSON.stringify([doc, filter, update, options], null, 2) : 'Too Big',
594
- method: 'findOneAndUpdate',
595
- id_user: '',
596
- user: '',
597
- messageId: '',
598
- route: ''
599
- });
600
- }
601
- _a.label = 2;
602
- case 2:
603
- index_1.ResolveIOServer.getMainDB().collection(this.collectionName).findOneAndUpdate(filter, update, options).then(function (res) {
604
- if (res.ok) {
605
- resolve(res.value);
606
- }
607
- else {
608
- reject(res.lastErrorObject);
609
- }
610
- }, function (err) { return reject(err); });
611
- return [2 /*return*/];
414
+ else {
415
+ reject(res.lastErrorObject);
612
416
  }
613
- });
614
- }); });
615
- };
616
- Collection.prototype.indexes = function (options) {
617
- return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).indexes(options);
618
- };
619
- Collection.prototype.indexExists = function (indexes, options) {
620
- return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).indexExists(indexes, options);
621
- };
622
- Collection.prototype.insertMany = function (docs, options) {
623
- var _this = this;
624
- return new Promise(function (resolve, reject) {
625
- if (!docs || !docs.length) {
626
- reject('No documents');
417
+ }, err => reject(err));
418
+ });
419
+ }
420
+ findOneAndReplace(filter = {}, replacement, options, bypassLogs = false) {
421
+ return new Promise(async (resolve, reject) => {
422
+ const validation = this.simplschema.newContext().validate(replacement);
423
+ if (!validation.isValid()) {
424
+ console.log(new Date(), this.collectionName, 'Schema Errors', validation.validationErrors());
425
+ ResolveIOServer.getMainServer().getMethodManager().callMethodInternal('insertErrorLog', 'Schema Failed on findOneAndReplace - ' + this.collectionName, [validation.validationErrors(), replacement]);
426
+ reject(validation.validationErrors());
427
+ }
428
+ else {
429
+ if (this.timestamps) {
430
+ replacement['updatedAt'] = new Date();
431
+ }
432
+ if (this.createLogs && !bypassLogs) {
433
+ let doc = await this.findOne(filter);
434
+ if (doc) {
435
+ Logs.insertOne({
436
+ _id: objectIdHexString(),
437
+ type: 'document',
438
+ collection: this.collectionName,
439
+ id_document: doc['_id'],
440
+ payload: getBinarySize(JSON.stringify([doc, filter, replacement, options])) < 200000 ? JSON.stringify([doc, filter, replacement, options], null, 2) : 'Too Big',
441
+ method: 'findOneAndReplace',
442
+ id_user: '',
443
+ user: '',
444
+ messageId: '',
445
+ route: ''
446
+ });
447
+ }
448
+ }
449
+ ResolveIOServer.getMainDB().collection(this.collectionName).findOneAndReplace(filter, replacement, options).then(res => {
450
+ if (res.ok) {
451
+ resolve(res.value);
452
+ }
453
+ else {
454
+ reject(res.lastErrorObject);
455
+ }
456
+ }, err => reject(err));
457
+ }
458
+ });
459
+ }
460
+ findOneAndUpdate(filter = {}, update, options, bypassLogs = false) {
461
+ return new Promise(async (resolve, reject) => {
462
+ const validation = this.simplschema.newContext().validate(update, { modifier: true });
463
+ if (!validation.isValid()) {
464
+ console.log(new Date(), this.collectionName, 'Schema Errors', validation.validationErrors());
465
+ ResolveIOServer.getMainServer().getMethodManager().callMethodInternal('insertErrorLog', 'Schema Failed on findOneAndUpdate - ' + this.collectionName, [validation.validationErrors(), update]);
466
+ reject(validation.validationErrors());
467
+ }
468
+ else {
469
+ if (this.timestamps) {
470
+ if (!update['$set']) {
471
+ update['$set'] = { updatedAt: new Date() };
472
+ }
473
+ else {
474
+ update['$set']['updatedAt'] = new Date();
475
+ }
476
+ }
477
+ if (this.createLogs && !bypassLogs) {
478
+ let doc = await this.findOne(filter);
479
+ if (doc) {
480
+ Logs.insertOne({
481
+ _id: objectIdHexString(),
482
+ type: 'document',
483
+ collection: this.collectionName,
484
+ id_document: doc['_id'],
485
+ payload: getBinarySize(JSON.stringify([doc, filter, update, options])) < 200000 ? JSON.stringify([doc, filter, update, options], null, 2) : 'Too Big',
486
+ method: 'findOneAndUpdate',
487
+ id_user: '',
488
+ user: '',
489
+ messageId: '',
490
+ route: ''
491
+ });
492
+ }
493
+ }
494
+ ResolveIOServer.getMainDB().collection(this.collectionName).findOneAndUpdate(filter, update, options).then(res => {
495
+ if (res.ok) {
496
+ resolve(res.value);
497
+ }
498
+ else {
499
+ reject(res.lastErrorObject);
500
+ }
501
+ }, err => reject(err));
502
+ }
503
+ });
504
+ }
505
+ indexes(options) {
506
+ return ResolveIOServer.getMainDB().collection(this.collectionName).indexes(options);
507
+ }
508
+ indexExists(indexes, options) {
509
+ return ResolveIOServer.getMainDB().collection(this.collectionName).indexExists(indexes, options);
510
+ }
511
+ insertMany(docs, options, bypassLogs = false) {
512
+ return new Promise((resolve, reject) => {
513
+ if (!docs.length) {
514
+ reject('No Documents');
627
515
  return;
628
516
  }
629
- if (_this.timestamps) {
630
- docs.forEach(function (doc) {
517
+ let validationResults = [];
518
+ docs.forEach(doc => {
519
+ const validation = this.simplschema.newContext().validate(doc);
520
+ if (!validation.isValid()) {
521
+ console.log(new Date(), this.collectionName, 'Schema Errors', validation.validationErrors());
522
+ ResolveIOServer.getMainServer().getMethodManager().callMethodInternal('insertErrorLog', 'Schema Failed on insertMany - ' + this.collectionName, [validation.validationErrors(), doc]);
523
+ validationResults.push(false);
524
+ }
525
+ else {
526
+ validationResults.push(true);
527
+ }
528
+ });
529
+ let validDocs = docs.filter((a, idx) => validationResults[idx]);
530
+ validDocs.forEach(doc => {
531
+ if (this.timestamps) {
631
532
  doc['createdAt'] = new Date();
632
533
  doc['updatedAt'] = new Date();
633
- });
634
- }
635
- if (_this.createLogs) {
636
- docs.forEach(function (doc) {
637
- log_collection_1.Logs.create({
534
+ }
535
+ if (this.createLogs && !bypassLogs) {
536
+ Logs.insertOne({
638
537
  _id: objectIdHexString(),
639
538
  type: 'document',
640
- collection: _this.collectionName,
539
+ collection: this.collectionName,
641
540
  id_document: doc['_id'],
642
- payload: common_1.getBinarySize(JSON.stringify([doc, options])) < 200000 ? JSON.stringify([doc, options], null, 2) : 'Too Big',
541
+ payload: getBinarySize(JSON.stringify([doc, options])) < 200000 ? JSON.stringify([doc, options], null, 2) : 'Too Big',
643
542
  method: 'insertMany',
644
543
  id_user: '',
645
544
  user: '',
646
545
  messageId: '',
647
546
  route: ''
648
547
  });
649
- });
650
- }
651
- index_1.ResolveIOServer.getMainDB().collection(_this.collectionName).insertMany(docs, options).then(function (res) {
652
- if (res.result.ok) {
653
- resolve(docs);
654
- }
655
- else {
656
- reject(res.result.ok);
657
548
  }
658
- }, function (err) {
659
- reject(err);
660
549
  });
661
- });
662
- };
663
- Collection.prototype.insertOne = function (doc, options) {
664
- var _this = this;
665
- return new Promise(function (resolve, reject) {
666
- if (!doc) {
667
- reject('No document');
668
- return;
669
- }
670
- if (_this.timestamps) {
671
- doc['createdAt'] = new Date();
672
- doc['updatedAt'] = new Date();
673
- }
674
- if (_this.createLogs) {
675
- log_collection_1.Logs.create({
676
- _id: objectIdHexString(),
677
- type: 'document',
678
- collection: _this.collectionName,
679
- id_document: doc['_id'],
680
- payload: common_1.getBinarySize(JSON.stringify([doc, options])) < 200000 ? JSON.stringify([doc, options], null, 2) : 'Too Big',
681
- method: 'insertOne',
682
- id_user: '',
683
- user: '',
684
- messageId: '',
685
- route: ''
550
+ if (validDocs.length) {
551
+ ResolveIOServer.getMainDB().collection(this.collectionName).insertMany(validDocs, options).then(res => {
552
+ if (res.result.ok) {
553
+ resolve(validDocs);
554
+ }
555
+ else {
556
+ reject(res.result.ok);
557
+ }
558
+ }, err => {
559
+ reject(err);
686
560
  });
687
561
  }
688
- index_1.ResolveIOServer.getMainDB().collection(_this.collectionName).insertOne(doc, options).then(function (res) {
689
- if (res.result.ok) {
690
- resolve(doc);
562
+ });
563
+ }
564
+ insertOne(doc, options, bypassLogs = false) {
565
+ return new Promise((resolve, reject) => {
566
+ const validation = this.simplschema.newContext().validate(doc);
567
+ if (!validation.isValid()) {
568
+ console.log(new Date(), this.collectionName, 'Schema Errors', validation.validationErrors());
569
+ ResolveIOServer.getMainServer().getMethodManager().callMethodInternal('insertErrorLog', 'Schema Failed on insertOne - ' + this.collectionName, [validation.validationErrors(), doc]);
570
+ reject(validation.validationErrors());
571
+ }
572
+ else {
573
+ if (this.timestamps) {
574
+ doc['createdAt'] = new Date();
575
+ doc['updatedAt'] = new Date();
691
576
  }
692
- else {
693
- reject(res.result.ok);
577
+ if (this.createLogs && !bypassLogs) {
578
+ Logs.insertOne({
579
+ _id: objectIdHexString(),
580
+ type: 'document',
581
+ collection: this.collectionName,
582
+ id_document: doc['_id'],
583
+ payload: getBinarySize(JSON.stringify([doc, options])) < 200000 ? JSON.stringify([doc, options], null, 2) : 'Too Big',
584
+ method: 'insertOne',
585
+ id_user: '',
586
+ user: '',
587
+ messageId: '',
588
+ route: ''
589
+ });
694
590
  }
695
- }, function (err) {
696
- reject(err);
697
- });
591
+ ResolveIOServer.getMainDB().collection(this.collectionName).insertOne(doc, options).then(res => {
592
+ if (res.result.ok) {
593
+ resolve(doc);
594
+ }
595
+ else {
596
+ reject(res.result.ok);
597
+ }
598
+ }, err => {
599
+ reject(err);
600
+ });
601
+ }
698
602
  });
699
- };
700
- Collection.prototype.listIndexes = function (options) {
701
- return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).listIndexes(options);
702
- };
703
- Collection.prototype.reIndex = function (options) {
704
- return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).reIndex(options);
705
- };
706
- Collection.prototype.rename = function (newName, options) {
707
- return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).rename(newName, options);
708
- };
709
- Collection.prototype.replaceOne = function (filter, replacement, options) {
710
- var _this = this;
711
- return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
712
- var doc;
713
- return __generator(this, function (_a) {
714
- switch (_a.label) {
715
- case 0:
716
- if (!this.createLogs) return [3 /*break*/, 2];
717
- return [4 /*yield*/, this.findOne(filter)];
718
- case 1:
719
- doc = _a.sent();
720
- if (doc) {
721
- log_collection_1.Logs.create({
603
+ }
604
+ listIndexes(options) {
605
+ return ResolveIOServer.getMainDB().collection(this.collectionName).listIndexes(options);
606
+ }
607
+ reIndex(options) {
608
+ return ResolveIOServer.getMainDB().collection(this.collectionName).reIndex(options);
609
+ }
610
+ rename(newName, options) {
611
+ return ResolveIOServer.getMainDB().collection(this.collectionName).rename(newName, options);
612
+ }
613
+ replaceOne(filter, replacement, options, bypassLogs = false, bypassDocument = false) {
614
+ return new Promise(async (resolve, reject) => {
615
+ const validation = this.simplschema.newContext().validate(replacement);
616
+ if (!validation.isValid()) {
617
+ console.log(new Date(), this.collectionName, 'Schema Errors', validation.validationErrors());
618
+ ResolveIOServer.getMainServer().getMethodManager().callMethodInternal('insertErrorLog', 'Schema Failed on replaceOne - ' + this.collectionName, [validation.validationErrors(), replacement]);
619
+ reject(validation.validationErrors());
620
+ }
621
+ else {
622
+ if (this.timestamps) {
623
+ replacement['updatedAt'] = new Date();
624
+ }
625
+ if (!bypassDocument) {
626
+ let doc = await this.findOne(filter);
627
+ if (doc) {
628
+ if (this.createLogs && !bypassLogs) {
629
+ Logs.insertOne({
722
630
  _id: objectIdHexString(),
723
631
  type: 'document',
724
632
  collection: this.collectionName,
725
633
  id_document: doc['_id'],
726
- payload: common_1.getBinarySize(JSON.stringify([doc, filter, replacement, options])) < 200000 ? JSON.stringify([doc, filter, replacement, options], null, 2) : 'Too Big',
634
+ payload: getBinarySize(JSON.stringify([doc, filter, replacement, options])) < 200000 ? JSON.stringify([doc, filter, replacement, options], null, 2) : 'Too Big',
727
635
  method: 'replaceOne',
728
636
  id_user: '',
729
637
  user: '',
@@ -731,381 +639,402 @@ var Collection = /** @class */ (function () {
731
639
  route: ''
732
640
  });
733
641
  }
734
- _a.label = 2;
735
- case 2:
736
- index_1.ResolveIOServer.getMainDB().collection(this.collectionName).replaceOne(filter, replacement, options).then(function (res) {
737
- if (res.result.ok) {
738
- resolve(res.result.nModified);
642
+ if (this.useVersions) {
643
+ let versionDoc = deepCopy(doc);
644
+ versionDoc['_id'] = { _id: doc['_id'], __v: doc['__v'] };
645
+ ResolveIOServer.getMongoManager().collection(this.versionCollection).insertOne(versionDoc);
646
+ if (doc['__v'] >= 4) {
647
+ ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({ $and: [
648
+ { '_id._id': doc['_id'] },
649
+ { '_id.__v': { $lte: doc['__v'] - 4 } }
650
+ ] });
651
+ }
652
+ if (doc['__v'] === replacement['__v']) {
653
+ replacement['__v'] += 1;
654
+ ResolveIOServer.getMainDB().collection(this.collectionName).replaceOne(filter, replacement, options).then(res => {
655
+ if (res.result.ok) {
656
+ resolve(res.result.nModified);
657
+ }
658
+ else {
659
+ reject(res.result.ok);
660
+ }
661
+ }, err => {
662
+ reject(err);
663
+ });
739
664
  }
740
665
  else {
741
- reject(res.result.ok);
666
+ console.log('invalid version - ' + this.collectionName, doc['__v'], replacement['__v']);
667
+ let prevDoc = await ResolveIOServer.getMongoManager().collection(this.versionCollection).findOne({
668
+ $and: [
669
+ { '_id._id': doc['_id'] },
670
+ { '_id.__v': replacement['__v'] }
671
+ ]
672
+ });
673
+ if (prevDoc) {
674
+ let docId = doc['_id'];
675
+ let docVersion = doc['__v'];
676
+ let updatedDoc = getMongoMergeUpdatedDoc(replacement, doc, prevDoc);
677
+ updatedDoc['_id'] = docId;
678
+ updatedDoc['__v'] = docVersion + 1;
679
+ updatedDoc['updatedAt'] = new Date();
680
+ ResolveIOServer.getMainDB().collection(this.collectionName).replaceOne(filter, updatedDoc, options).then(res => {
681
+ if (res.result.ok) {
682
+ resolve(res.result.nModified);
683
+ }
684
+ else {
685
+ reject(res.result.ok);
686
+ }
687
+ }, err => {
688
+ reject(err);
689
+ });
690
+ }
691
+ else {
692
+ reject('Invalid Version And Could Not Find History - DB: ' + doc['__v'] + ', Trying to update with :' + replacement['__v']);
693
+ }
742
694
  }
743
- }, function (err) {
744
- reject(err);
745
- });
746
- return [2 /*return*/];
695
+ }
696
+ else {
697
+ ResolveIOServer.getMainDB().collection(this.collectionName).replaceOne(filter, replacement, options).then(res => {
698
+ if (res.result.ok) {
699
+ resolve(res.result.nModified);
700
+ }
701
+ else {
702
+ reject(res.result.ok);
703
+ }
704
+ }, err => {
705
+ reject(err);
706
+ });
707
+ }
708
+ }
709
+ else {
710
+ reject('No Document');
711
+ }
747
712
  }
748
- });
749
- }); });
750
- };
751
- Collection.prototype.stats = function (options) {
752
- return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).stats(options);
753
- };
754
- Collection.prototype.updateMany = function (filter, update, options) {
755
- var _this = this;
756
- return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
757
- var docs;
758
- var _this = this;
759
- return __generator(this, function (_a) {
760
- switch (_a.label) {
761
- case 0:
762
- if (!update['$set']) {
763
- update['$set'] = { updatedAt: new Date() };
713
+ else {
714
+ ResolveIOServer.getMainDB().collection(this.collectionName).replaceOne(filter, replacement, options).then(res => {
715
+ if (res.result.ok) {
716
+ resolve(res.result.nModified);
764
717
  }
765
718
  else {
766
- update['$set']['updatedAt'] = new Date();
719
+ reject(res.result.ok);
767
720
  }
768
- if (!this.createLogs) return [3 /*break*/, 2];
769
- return [4 /*yield*/, this.find(filter)];
770
- case 1:
771
- docs = _a.sent();
772
- docs.forEach(function (doc) {
773
- log_collection_1.Logs.create({
774
- _id: objectIdHexString(),
775
- type: 'document',
776
- collection: _this.collectionName,
777
- id_document: doc['_id'],
778
- payload: common_1.getBinarySize(JSON.stringify([doc, filter, update, options])) < 200000 ? JSON.stringify([doc, filter, update, options], null, 2) : 'Too Big',
779
- method: 'updateMany',
780
- id_user: '',
781
- user: '',
782
- messageId: '',
783
- route: ''
784
- });
721
+ }, err => {
722
+ reject(err);
723
+ });
724
+ }
725
+ }
726
+ });
727
+ }
728
+ stats(options) {
729
+ return ResolveIOServer.getMainDB().collection(this.collectionName).stats(options);
730
+ }
731
+ updateMany(filter, update, options, bypassLogs = false) {
732
+ return new Promise(async (resolve, reject) => {
733
+ if (this.timestamps) {
734
+ if (!update.$set) {
735
+ update.$set = { updatedAt: new Date() };
736
+ }
737
+ else {
738
+ update.$set['updatedAt'] = new Date();
739
+ }
740
+ }
741
+ const validation = this.simplschema.newContext().validate(update, { modifier: true });
742
+ if (!validation.isValid()) {
743
+ console.log(new Date(), this.collectionName, 'Schema Errors', validation.validationErrors());
744
+ ResolveIOServer.getMainServer().getMethodManager().callMethodInternal('insertErrorLog', 'Schema Failed on updateMany - ' + this.collectionName, [validation.validationErrors(), update]);
745
+ reject(validation.validationErrors());
746
+ }
747
+ else {
748
+ if (this.createLogs && !bypassLogs) {
749
+ let docs = await this.find(filter);
750
+ docs.forEach(doc => {
751
+ Logs.insertOne({
752
+ _id: objectIdHexString(),
753
+ type: 'document',
754
+ collection: this.collectionName,
755
+ id_document: doc['_id'],
756
+ payload: getBinarySize(JSON.stringify([doc, filter, update, options])) < 200000 ? JSON.stringify([doc, filter, update, options], null, 2) : 'Too Big',
757
+ method: 'updateMany',
758
+ id_user: '',
759
+ user: '',
760
+ messageId: '',
761
+ route: ''
762
+ });
763
+ });
764
+ }
765
+ ResolveIOServer.getMainDB().collection(this.collectionName).updateMany(filter, update, options).then(res => {
766
+ if (res.result.ok) {
767
+ resolve(res.result.nModified);
768
+ }
769
+ else {
770
+ reject(res.result.ok);
771
+ }
772
+ }, err => reject(err));
773
+ }
774
+ });
775
+ }
776
+ updateOne(filter, update, options, bypassLogs = false) {
777
+ return new Promise(async (resolve, reject) => {
778
+ if (this.timestamps) {
779
+ if (!update.$set) {
780
+ update.$set = { updatedAt: new Date() };
781
+ }
782
+ else {
783
+ update.$set['updatedAt'] = new Date();
784
+ }
785
+ }
786
+ const validation = this.simplschema.newContext().validate(update, { modifier: true });
787
+ if (!validation.isValid()) {
788
+ console.log(new Date(), this.collectionName, 'Schema Errors', validation.validationErrors());
789
+ ResolveIOServer.getMainServer().getMethodManager().callMethodInternal('insertErrorLog', 'Schema Failed on updateOne - ' + this.collectionName, [validation.validationErrors(), update]);
790
+ reject(validation.validationErrors());
791
+ }
792
+ else {
793
+ let doc = await this.findOne(filter);
794
+ if (doc) {
795
+ if (this.createLogs && !bypassLogs) {
796
+ Logs.insertOne({
797
+ _id: objectIdHexString(),
798
+ type: 'document',
799
+ collection: this.collectionName,
800
+ id_document: doc['_id'],
801
+ payload: getBinarySize(JSON.stringify([doc, filter, update, options])) < 200000 ? JSON.stringify([doc, filter, update, options], null, 2) : 'Too Big',
802
+ method: 'updateOne',
803
+ id_user: '',
804
+ user: '',
805
+ messageId: '',
806
+ route: ''
785
807
  });
786
- _a.label = 2;
787
- case 2:
788
- index_1.ResolveIOServer.getMainDB().collection(this.collectionName).updateMany(filter, update, options).then(function (res) {
808
+ }
809
+ if (this.useVersions) {
810
+ let versionDoc = deepCopy(doc);
811
+ versionDoc['_id'] = { _id: doc['_id'], __v: doc['__v'] };
812
+ ResolveIOServer.getMongoManager().collection(this.versionCollection).insertOne(versionDoc);
813
+ if (doc['__v'] >= 4) {
814
+ ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({ $and: [
815
+ { '_id._id': doc['_id'] },
816
+ { '_id.__v': { $lte: doc['__v'] - 4 } }
817
+ ] });
818
+ }
819
+ if (!update.$inc) {
820
+ update.$inc = { __v: 1 };
821
+ }
822
+ else if (!update.$inc.__v) {
823
+ update.$inc.__v = 1;
824
+ }
825
+ ResolveIOServer.getMainDB().collection(this.collectionName).updateOne(filter, update, options).then(res => {
789
826
  if (res.result.ok) {
790
827
  resolve(res.result.nModified);
791
828
  }
792
829
  else {
793
830
  reject(res.result.ok);
794
831
  }
795
- }, function (err) { return reject(err); });
796
- return [2 /*return*/];
797
- }
798
- });
799
- }); });
800
- return;
801
- };
802
- Collection.prototype.updateOne = function (filter, update, options) {
803
- var _this = this;
804
- return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
805
- var doc;
806
- return __generator(this, function (_a) {
807
- switch (_a.label) {
808
- case 0:
809
- if (!update['$set']) {
810
- update['$set'] = { updatedAt: new Date() };
811
- }
812
- else {
813
- update['$set']['updatedAt'] = new Date();
814
- }
815
- if (!this.createLogs) return [3 /*break*/, 2];
816
- return [4 /*yield*/, this.findOne(filter)];
817
- case 1:
818
- doc = _a.sent();
819
- if (doc) {
820
- log_collection_1.Logs.create({
821
- _id: objectIdHexString(),
822
- type: 'document',
823
- collection: this.collectionName,
824
- id_document: doc['_id'],
825
- payload: common_1.getBinarySize(JSON.stringify([doc, filter, update, options])) < 200000 ? JSON.stringify([doc, filter, update, options], null, 2) : 'Too Big',
826
- method: 'updateOne',
827
- id_user: '',
828
- user: '',
829
- messageId: '',
830
- route: ''
831
- });
832
- }
833
- _a.label = 2;
834
- case 2:
835
- index_1.ResolveIOServer.getMainDB().collection(this.collectionName).updateOne(filter, update, options).then(function (res) {
832
+ }, err => reject(err));
833
+ }
834
+ else {
835
+ ResolveIOServer.getMainDB().collection(this.collectionName).updateOne(filter, update, options).then(res => {
836
836
  if (res.result.ok) {
837
837
  resolve(res.result.nModified);
838
838
  }
839
839
  else {
840
840
  reject(res.result.ok);
841
841
  }
842
- }, function (err) { return reject(err); });
843
- return [2 /*return*/];
842
+ }, err => reject(err));
843
+ }
844
844
  }
845
- });
846
- }); });
847
- };
848
- Collection.prototype.watchCollection = function (pipeline, options) {
849
- if (pipeline === void 0) { pipeline = []; }
850
- return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).watch(pipeline, options);
851
- };
852
- Collection.prototype.watchCollectionWithAggregate = function (pipeline, options) {
853
- if (pipeline === void 0) { pipeline = []; }
854
- return index_1.ResolveIOServer.getMainDB().collection(this.collectionName).watch(pipeline, options);
855
- };
856
- return Collection;
857
- }());
858
- exports.Collection = Collection;
859
- var UserCollection = /** @class */ (function (_super) {
860
- __extends(UserCollection, _super);
861
- function UserCollection() {
862
- return _super !== null && _super.apply(this, arguments) || this;
845
+ else {
846
+ reject('No Document');
847
+ }
848
+ }
849
+ });
863
850
  }
864
- UserCollection.prototype.authenticateStrategy = function () {
865
- var _this = this;
866
- return function (username, password, cb) { return __awaiter(_this, void 0, void 0, function () {
867
- var user;
868
- return __generator(this, function (_a) {
869
- switch (_a.label) {
870
- case 0: return [4 /*yield*/, user_collection_1.Users.findOne({ username: username })];
871
- case 1:
872
- user = _a.sent();
873
- this.authenticate(user, password).then(function (authUser) {
874
- if (authUser.data) {
875
- cb(null, user, null);
876
- }
877
- else {
878
- cb(null, null, authUser.error);
879
- }
880
- }, function (err) {
881
- cb(err, null, null);
882
- });
883
- return [2 /*return*/];
851
+ watchCollection(pipeline = [], options) {
852
+ return ResolveIOServer.getMainDB().collection(this.collectionName).watch(pipeline, options);
853
+ }
854
+ watchCollectionWithAggregate(pipeline = [], options) {
855
+ return ResolveIOServer.getMainDB().collection(this.collectionName).watch(pipeline, options);
856
+ }
857
+ }
858
+ export class UserCollection extends Collection {
859
+ authenticateStrategy() {
860
+ return async (username, password, cb) => {
861
+ let user = await Users.findOne({ username: username });
862
+ this.authenticate(user, password).then(authUser => {
863
+ if (authUser.data) {
864
+ cb(null, user, null);
865
+ }
866
+ else {
867
+ cb(null, null, authUser.error);
884
868
  }
869
+ }, err => {
870
+ cb(err, null, null);
885
871
  });
886
- }); };
887
- };
888
- UserCollection.prototype.authenticate = function (user, password) {
889
- var _this = this;
890
- return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
891
- var attemptsInterval, calculatedInterval;
892
- var _this = this;
893
- return __generator(this, function (_a) {
894
- switch (_a.label) {
895
- case 0:
896
- attemptsInterval = Math.pow(100, Math.log(user.attempts + 1));
897
- calculatedInterval = attemptsInterval < 300000 ? attemptsInterval : 300000;
898
- if (!user.last) return [3 /*break*/, 3];
899
- if (!(Date.now() - user.last.getTime() < calculatedInterval)) return [3 /*break*/, 2];
900
- user.last = new Date();
901
- return [4 /*yield*/, user_collection_1.Users.updateOne({ _id: user._id }, { $set: { last: user.last } })];
902
- case 1:
903
- _a.sent();
872
+ };
873
+ }
874
+ authenticate(user, password) {
875
+ return new Promise(async (resolve, reject) => {
876
+ const attemptsInterval = Math.pow(100, Math.log(user.attempts + 1));
877
+ const calculatedInterval = attemptsInterval < 300000 ? attemptsInterval : 300000;
878
+ if (user.last) {
879
+ if (Date.now() - user.last.getTime() < calculatedInterval) {
880
+ user.last = new Date();
881
+ await Users.updateOne({ _id: user._id }, { $set: { last: user.last } });
882
+ resolve({
883
+ data: null,
884
+ error: 'Attempt Too Soon'
885
+ });
886
+ return;
887
+ }
888
+ }
889
+ else {
890
+ user.last = new Date();
891
+ }
892
+ if (user.attempts >= 5) {
893
+ resolve({
894
+ data: null,
895
+ error: 'Too Many Attempts'
896
+ });
897
+ return;
898
+ }
899
+ if (!user.salt) {
900
+ resolve({
901
+ data: null,
902
+ error: 'No Salt Value Stored'
903
+ });
904
+ return;
905
+ }
906
+ pbkdf2(password, user.salt, {
907
+ iterations: 25000,
908
+ keylen: 512,
909
+ digestAlgorithm: 'sha256'
910
+ }, async (err, hashBuffer) => {
911
+ if (err) {
912
+ return reject(err);
913
+ }
914
+ if (scmp(hashBuffer, Buffer.from(user.hash, 'hex'))) {
915
+ user.last = new Date();
916
+ user.attempts = 0;
917
+ await Users.updateOne({ _id: user._id }, { $set: { last: user.last, attempts: user.attempts } });
918
+ resolve({
919
+ data: user,
920
+ error: ''
921
+ });
922
+ }
923
+ else {
924
+ user.last = new Date();
925
+ user.attempts = user.attempts + 1;
926
+ await Users.updateOne({ _id: user._id }, { $set: { last: user.last, attempts: user.attempts } });
927
+ if (user.attempts >= 5) {
904
928
  resolve({
905
929
  data: null,
906
- error: 'Attempt Too Soon'
930
+ error: 'Too Many Attempts'
907
931
  });
908
- return [2 /*return*/];
909
- case 2: return [3 /*break*/, 4];
910
- case 3:
911
- user.last = new Date();
912
- _a.label = 4;
913
- case 4:
914
- if (user.attempts >= 5) {
915
- resolve({
916
- data: null,
917
- error: 'Too Many Attempts'
918
- });
919
- return [2 /*return*/];
920
- }
921
- if (!user.salt) {
922
- resolve({
923
- data: null,
924
- error: 'No Salt Value Stored'
925
- });
926
- return [2 /*return*/];
927
- }
928
- pbkdf2(password, user.salt, {
929
- iterations: 25000,
930
- keylen: 512,
931
- digestAlgorithm: 'sha256'
932
- }, function (err, hashBuffer) { return __awaiter(_this, void 0, void 0, function () {
933
- return __generator(this, function (_a) {
934
- switch (_a.label) {
935
- case 0:
936
- if (err) {
937
- return [2 /*return*/, reject(err)];
938
- }
939
- if (!scmp(hashBuffer, Buffer.from(user.hash, 'hex'))) return [3 /*break*/, 2];
940
- user.last = new Date();
941
- user.attempts = 0;
942
- return [4 /*yield*/, user_collection_1.Users.updateOne({ _id: user._id }, { $set: { last: user.last, attempts: user.attempts } })];
943
- case 1:
944
- _a.sent();
945
- resolve({
946
- data: user,
947
- error: ''
948
- });
949
- return [3 /*break*/, 4];
950
- case 2:
951
- user.last = new Date();
952
- user.attempts = user.attempts + 1;
953
- return [4 /*yield*/, user_collection_1.Users.updateOne({ _id: user._id }, { $set: { last: user.last, attempts: user.attempts } })];
954
- case 3:
955
- _a.sent();
956
- if (user.attempts >= 5) {
957
- resolve({
958
- data: null,
959
- error: 'Too Many Attempts'
960
- });
961
- }
962
- else {
963
- resolve({
964
- data: null,
965
- error: 'Invalid Username And Password'
966
- });
967
- }
968
- _a.label = 4;
969
- case 4: return [2 /*return*/];
970
- }
971
- });
972
- }); });
973
- return [2 /*return*/];
932
+ }
933
+ else {
934
+ resolve({
935
+ data: null,
936
+ error: 'Invalid Username And Password'
937
+ });
938
+ }
974
939
  }
975
940
  });
976
- }); });
977
- };
978
- UserCollection.prototype.serializeUser = function () {
979
- return function (user, cb) {
941
+ });
942
+ }
943
+ serializeUser() {
944
+ return (user, cb) => {
980
945
  cb(null, user.username);
981
946
  };
982
- };
983
- UserCollection.prototype.deserializeUser = function () {
984
- return function (username, cb) {
985
- user_collection_1.Users.findOne({ username: username }).then(function (res) {
947
+ }
948
+ deserializeUser() {
949
+ return (username, cb) => {
950
+ Users.findOne({ username: username }).then(res => {
986
951
  cb(null, res);
987
- }, function (err) {
952
+ }, err => {
988
953
  cb(err, null);
989
954
  });
990
955
  };
991
- };
992
- UserCollection.prototype.setPassword = function (user, password) {
993
- var _this = this;
994
- return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
995
- var saltBuffer, salt, hashRaw, hash;
996
- return __generator(this, function (_a) {
997
- switch (_a.label) {
998
- case 0:
999
- if (!!user) return [3 /*break*/, 1];
1000
- reject('No User');
1001
- return [3 /*break*/, 6];
1002
- case 1:
1003
- if (!!password) return [3 /*break*/, 2];
1004
- reject('No Password');
1005
- return [3 /*break*/, 6];
1006
- case 2: return [4 /*yield*/, randomBytes(32)];
1007
- case 3:
1008
- saltBuffer = _a.sent();
1009
- salt = saltBuffer.toString('hex');
1010
- return [4 /*yield*/, pbkdf2Promisified(password, salt, {
1011
- iterations: 25000,
1012
- keylen: 512,
1013
- digestAlgorithm: 'sha256'
1014
- })];
1015
- case 4:
1016
- hashRaw = _a.sent();
1017
- hash = Buffer.from(hashRaw, 'binary').toString('hex');
1018
- return [4 /*yield*/, user_collection_1.Users.updateOne({ _id: user._id }, { $set: { hash: hash, salt: salt, services: {} } })];
1019
- case 5:
1020
- _a.sent();
1021
- _a.label = 6;
1022
- case 6: return [2 /*return*/];
1023
- }
1024
- });
1025
- }); });
1026
- };
956
+ }
957
+ setPassword(user, password) {
958
+ return new Promise(async (resolve, reject) => {
959
+ if (!user) {
960
+ reject('No User');
961
+ }
962
+ else if (!password) {
963
+ reject('No Password');
964
+ }
965
+ else {
966
+ let saltBuffer = await randomBytes(32);
967
+ let salt = saltBuffer.toString('hex');
968
+ let hashRaw = await pbkdf2Promisified(password, salt, {
969
+ iterations: 25000,
970
+ keylen: 512,
971
+ digestAlgorithm: 'sha256'
972
+ });
973
+ let hash = Buffer.from(hashRaw, 'binary').toString('hex');
974
+ await Users.updateOne({ _id: user._id }, { $set: { hash: hash, salt: salt, services: {} } });
975
+ }
976
+ });
977
+ }
1027
978
  ;
1028
- UserCollection.prototype.changePassword = function (user, oldPassword, newPassword) {
1029
- var _this = this;
1030
- return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
1031
- var authUser;
1032
- return __generator(this, function (_a) {
1033
- switch (_a.label) {
1034
- case 0:
1035
- if (!!user) return [3 /*break*/, 1];
1036
- reject('Missing User');
1037
- return [3 /*break*/, 4];
1038
- case 1:
1039
- if (!(!oldPassword || !newPassword)) return [3 /*break*/, 2];
1040
- reject('Missing Password');
1041
- return [3 /*break*/, 4];
1042
- case 2: return [4 /*yield*/, this.authenticate(user, oldPassword)];
1043
- case 3:
1044
- authUser = _a.sent();
1045
- if (!authUser['data']) {
1046
- reject(authUser['error']);
1047
- }
1048
- else {
1049
- this.setPassword(user, newPassword);
1050
- }
1051
- _a.label = 4;
1052
- case 4: return [2 /*return*/];
979
+ changePassword(user, oldPassword, newPassword) {
980
+ return new Promise(async (resolve, reject) => {
981
+ if (!user) {
982
+ reject('Missing User');
983
+ }
984
+ else if (!oldPassword || !newPassword) {
985
+ reject('Missing Password');
986
+ }
987
+ else {
988
+ let authUser = await this.authenticate(user, oldPassword);
989
+ if (!authUser['data']) {
990
+ reject(authUser['error']);
1053
991
  }
1054
- });
1055
- }); });
1056
- };
1057
- UserCollection.prototype.register = function (user, password) {
1058
- var _this = this;
1059
- return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
1060
- var dbUser;
1061
- return __generator(this, function (_a) {
1062
- switch (_a.label) {
1063
- case 0:
1064
- if (!!user.username) return [3 /*break*/, 1];
1065
- reject('Missing Username');
1066
- return [3 /*break*/, 5];
1067
- case 1: return [4 /*yield*/, user_collection_1.Users.findOne({ username: user.username })];
1068
- case 2:
1069
- dbUser = _a.sent();
1070
- if (!dbUser) return [3 /*break*/, 3];
1071
- reject('Username Exists');
1072
- return [3 /*break*/, 5];
1073
- case 3: return [4 /*yield*/, user_collection_1.Users.findOne({ email: user.email })];
1074
- case 4:
1075
- dbUser = _a.sent();
1076
- if (dbUser) {
1077
- reject('Email Exists');
1078
- }
1079
- else {
1080
- user.setPassword(password);
1081
- resolve(user);
1082
- }
1083
- _a.label = 5;
1084
- case 5: return [2 /*return*/];
992
+ else {
993
+ this.setPassword(user, newPassword);
1085
994
  }
1086
- });
1087
- }); });
1088
- };
1089
- UserCollection.prototype.resetAttempts = function (user) {
1090
- return user_collection_1.Users.updateOne({ _id: user._id }, { $set: { attempts: 0 } });
1091
- };
1092
- return UserCollection;
1093
- }(Collection));
1094
- exports.UserCollection = UserCollection;
1095
- function objectIdHexString() {
1096
- var objectId = new mongodb_1.ObjectId();
995
+ }
996
+ });
997
+ }
998
+ register(user, password) {
999
+ return new Promise(async (resolve, reject) => {
1000
+ if (!user.username) {
1001
+ reject('Missing Username');
1002
+ }
1003
+ else {
1004
+ let dbUser = await Users.findOne({ username: user.username });
1005
+ if (dbUser) {
1006
+ reject('Username Exists');
1007
+ }
1008
+ else {
1009
+ dbUser = await Users.findOne({ email: user.email });
1010
+ if (dbUser) {
1011
+ reject('Email Exists');
1012
+ }
1013
+ else {
1014
+ user.setPassword(password);
1015
+ resolve(user);
1016
+ }
1017
+ }
1018
+ }
1019
+ });
1020
+ }
1021
+ resetAttempts(user) {
1022
+ return Users.updateOne({ _id: user._id }, { $set: { attempts: 0 } });
1023
+ }
1024
+ }
1025
+ export function objectIdHexString() {
1026
+ let objectId = new ObjectId();
1097
1027
  return objectId.toHexString();
1098
1028
  }
1099
- exports.objectIdHexString = objectIdHexString;
1100
1029
  function pbkdf2(password, salt, options, callback) {
1101
1030
  crypto.pbkdf2(password, salt, options.iterations, options.keylen, options.digestAlgorithm, callback);
1102
1031
  }
1103
1032
  ;
1104
1033
  function pbkdf2Promisified(password, salt, options) {
1105
- return new Promise(function (resolve, reject) { return pbkdf2(password, salt, options, function (err, hashRaw) { return (err ? reject(err) : resolve(hashRaw)); }); });
1034
+ return new Promise((resolve, reject) => pbkdf2(password, salt, options, (err, hashRaw) => (err ? reject(err) : resolve(hashRaw))));
1106
1035
  }
1107
1036
  function randomBytes(saltlen) {
1108
- return new Promise(function (resolve, reject) { return crypto.randomBytes(saltlen, function (err, saltBuffer) { return (err ? reject(err) : resolve(saltBuffer)); }); });
1037
+ return new Promise((resolve, reject) => crypto.randomBytes(saltlen, (err, saltBuffer) => (err ? reject(err) : resolve(saltBuffer))));
1109
1038
  }
1110
1039
 
1111
1040
  //# sourceMappingURL=mongo.manager.js.map