@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.
- package/client-server-app.js +7 -48
- package/client-server-app.js.map +1 -1
- package/collections/app-status.collection.js +8 -11
- package/collections/app-status.collection.js.map +1 -1
- package/collections/app-version.collection.js +8 -11
- package/collections/app-version.collection.js.map +1 -1
- package/collections/counter.collection.js +8 -11
- package/collections/counter.collection.js.map +1 -1
- package/collections/cron-job-history.collection.js +9 -12
- package/collections/cron-job-history.collection.js.map +1 -1
- package/collections/cron-job.collection.js +8 -11
- package/collections/cron-job.collection.js.map +1 -1
- package/collections/email-history.collection.js +8 -11
- package/collections/email-history.collection.js.map +1 -1
- package/collections/email-verified.collection.js +9 -12
- package/collections/email-verified.collection.js.map +1 -1
- package/collections/file.collection.js +8 -11
- package/collections/file.collection.js.map +1 -1
- package/collections/flag.collection.js +8 -11
- package/collections/flag.collection.js.map +1 -1
- package/collections/log-method-latency.collection.js +11 -14
- package/collections/log-method-latency.collection.js.map +1 -1
- package/collections/log-subscription.collection.js +13 -16
- package/collections/log-subscription.collection.js.map +1 -1
- package/collections/log.collection.js +16 -19
- package/collections/log.collection.js.map +1 -1
- package/collections/logged-in-users.collection.js +9 -12
- package/collections/logged-in-users.collection.js.map +1 -1
- package/collections/method-call.collection.js +10 -13
- package/collections/method-call.collection.js.map +1 -1
- package/collections/method-response.collection.js +10 -13
- package/collections/method-response.collection.js.map +1 -1
- package/collections/monitor-cpu.collection.js +13 -16
- package/collections/monitor-cpu.collection.js.map +1 -1
- package/collections/monitor-https-inbound.collection.js +13 -16
- package/collections/monitor-https-inbound.collection.js.map +1 -1
- package/collections/monitor-https-outbound.collection.js +15 -18
- package/collections/monitor-https-outbound.collection.js.map +1 -1
- package/collections/monitor-memory.collection.js +12 -15
- package/collections/monitor-memory.collection.js.map +1 -1
- package/collections/monitor-mongo.collection.js +12 -15
- package/collections/monitor-mongo.collection.js.map +1 -1
- package/collections/notification.collection.js +8 -11
- package/collections/notification.collection.js.map +1 -1
- package/collections/queue-flag.collection.js +9 -12
- package/collections/queue-flag.collection.js.map +1 -1
- package/collections/queue-method.collection.js +9 -12
- package/collections/queue-method.collection.js.map +1 -1
- package/collections/queue-response.collection.js +9 -12
- package/collections/queue-response.collection.js.map +1 -1
- package/collections/report-builder-dashboard-builder.collection.js +10 -13
- package/collections/report-builder-dashboard-builder.collection.js.map +1 -1
- package/collections/report-builder-library.collection.js +11 -14
- package/collections/report-builder-library.collection.js.map +1 -1
- package/collections/report-builder-report.collection.js +11 -14
- package/collections/report-builder-report.collection.js.map +1 -1
- package/collections/user-group.collection.js +11 -14
- package/collections/user-group.collection.js.map +1 -1
- package/collections/user-guide.collection.js +8 -11
- package/collections/user-guide.collection.js.map +1 -1
- package/collections/user.collection.js +12 -15
- package/collections/user.collection.js.map +1 -1
- package/cron/cron.js +25 -25
- package/cron/cron.js.map +1 -1
- package/fixtures/cron-jobs.js +30 -80
- package/fixtures/cron-jobs.js.map +1 -1
- package/fixtures/init.js +9 -59
- package/fixtures/init.js.map +1 -1
- package/http/auth.js +344 -460
- package/http/auth.js.map +1 -1
- package/http/health.js +1 -5
- package/http/health.js.map +1 -1
- package/http/home.js +7 -11
- package/http/home.js.map +1 -1
- package/index.js +36 -98
- package/index.js.map +1 -1
- package/managers/cron.manager.js +78 -157
- package/managers/cron.manager.js.map +1 -1
- package/managers/method.manager.js +323 -303
- package/managers/method.manager.js.map +1 -1
- package/managers/mongo.manager.d.ts +14 -14
- package/managers/mongo.manager.js +794 -865
- package/managers/mongo.manager.js.map +1 -1
- package/managers/monitor.manager.js +171 -254
- package/managers/monitor.manager.js.map +1 -1
- package/managers/queue-method.manager.js +69 -166
- package/managers/queue-method.manager.js.map +1 -1
- package/managers/subscription.manager.js +515 -708
- package/managers/subscription.manager.js.map +1 -1
- package/methods/accounts.js +111 -183
- package/methods/accounts.js.map +1 -1
- package/methods/aws.js +156 -262
- package/methods/aws.js.map +1 -1
- package/methods/cloudconvert.js +100 -180
- package/methods/cloudconvert.js.map +1 -1
- package/methods/collections.js +114 -530
- package/methods/collections.js.map +1 -1
- package/methods/counters.js +29 -79
- package/methods/counters.js.map +1 -1
- package/methods/cron-jobs.js +819 -884
- package/methods/cron-jobs.js.map +1 -1
- package/methods/flags.js +1 -5
- package/methods/flags.js.map +1 -1
- package/methods/logs.js +258 -313
- package/methods/logs.js.map +1 -1
- package/methods/monitor.js +289 -348
- package/methods/monitor.js.map +1 -1
- package/methods/pdf.js +341 -340
- package/methods/pdf.js.map +1 -1
- package/methods/report-builder.js +565 -657
- package/methods/report-builder.js.map +1 -1
- package/methods/support.js +67 -185
- package/methods/support.js.map +1 -1
- package/models/app-status.model.js +1 -2
- package/models/app-version.model.js +1 -2
- package/models/billing-logged-in-users.model.js +1 -2
- package/models/collection-document.model.js +1 -2
- package/models/counter.model.js +1 -2
- package/models/cron-job-history.model.js +1 -2
- package/models/cron-job.model.js +1 -2
- package/models/dialog.model.js +1 -2
- package/models/email-history.model.js +1 -2
- package/models/email-verified.model.js +1 -2
- package/models/file.model.js +1 -2
- package/models/flag.model.js +1 -2
- package/models/log-method-latency.model.js +1 -2
- package/models/log-subscription.model.js +1 -2
- package/models/log.model.js +1 -2
- package/models/logged-in-users.model.js +1 -2
- package/models/method-call.model.js +1 -2
- package/models/method-response.model.js +1 -2
- package/models/method.model.js +1 -2
- package/models/monitor-cpu.model.js +1 -2
- package/models/monitor-https-inbound.model.js +1 -2
- package/models/monitor-https-outbound.model.js +1 -2
- package/models/monitor-memory.model.js +1 -2
- package/models/monitor-mongo.model.js +1 -2
- package/models/notification.model.js +1 -2
- package/models/pagination.model.js +2 -5
- package/models/pagination.model.js.map +1 -1
- package/models/permission.model.js +1 -2
- package/models/queue-method.model.js +1 -2
- package/models/queue-response.model.js +1 -2
- package/models/report-builder-dashboard-builder.model.js +1 -2
- package/models/report-builder-library.model.js +1 -2
- package/models/report-builder-report.model.js +1 -2
- package/models/report-builder.model.js +1 -2
- package/models/select-data-label.model.js +1 -2
- package/models/server-response.model.js +1 -2
- package/models/subscription.model.js +1 -2
- package/models/support-ticket.model.js +1 -2
- package/models/user-group.model.js +1 -2
- package/models/user-guide.model.js +1 -2
- package/models/user.model.js +1 -2
- package/package.json +1 -1
- package/public_api.js +70 -82
- package/public_api.js.map +1 -1
- package/publications/app-status.js +4 -8
- package/publications/app-status.js.map +1 -1
- package/publications/app-version.js +4 -8
- package/publications/app-version.js.map +1 -1
- package/publications/cron-jobs.js +9 -13
- package/publications/cron-jobs.js.map +1 -1
- package/publications/files.js +11 -15
- package/publications/files.js.map +1 -1
- package/publications/flags.js +7 -11
- package/publications/flags.js.map +1 -1
- package/publications/logs.js +31 -35
- package/publications/logs.js.map +1 -1
- package/publications/method-calls.js +4 -8
- package/publications/method-calls.js.map +1 -1
- package/publications/method-responses.js +4 -8
- package/publications/method-responses.js.map +1 -1
- package/publications/notifications.js +4 -8
- package/publications/notifications.js.map +1 -1
- package/publications/report-builder-dashboard-builders.js +8 -12
- package/publications/report-builder-dashboard-builders.js.map +1 -1
- package/publications/report-builder-libraries.js +16 -66
- package/publications/report-builder-libraries.js.map +1 -1
- package/publications/report-builder-reports.js +17 -21
- package/publications/report-builder-reports.js.map +1 -1
- package/publications/super-admin.js +3 -7
- package/publications/super-admin.js.map +1 -1
- package/publications/user-groups.js +4 -8
- package/publications/user-groups.js.map +1 -1
- package/publications/user-guides.js +4 -8
- package/publications/user-guides.js.map +1 -1
- package/server-app.js +393 -534
- package/server-app.js.map +1 -1
- package/util/common.js +55 -92
- package/util/common.js.map +1 -1
- package/util/schema-report-builder.js +76 -134
- package/util/schema-report-builder.js.map +1 -1
|
@@ -1,81 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
16
|
+
registerCollection(collection) {
|
|
69
17
|
this._collections.push(collection);
|
|
70
|
-
}
|
|
71
|
-
|
|
18
|
+
}
|
|
19
|
+
collections() {
|
|
72
20
|
return this._collections;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return this._collections.filter(
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
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
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
|
|
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
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
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
|
-
|
|
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
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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.
|
|
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
|
|
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
|
-
|
|
207
|
-
|
|
208
|
-
this.rbSchema =
|
|
138
|
+
let schemaCopy = deepCopy(schema);
|
|
139
|
+
let rbSchema = buildRbLookups(reportBuilderLookupTables, schemaCopy, []);
|
|
140
|
+
this.rbSchema = buildRbSchema(rbSchema);
|
|
209
141
|
}
|
|
210
|
-
|
|
211
|
-
if (
|
|
212
|
-
|
|
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
|
-
|
|
218
|
-
|
|
219
|
-
|
|
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:
|
|
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: [
|
|
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
|
-
|
|
241
|
-
|
|
242
|
-
|
|
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:
|
|
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: [
|
|
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
|
-
|
|
264
|
-
return
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
return
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
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
|
-
|
|
281
|
-
return
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
return
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
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
|
-
|
|
370
|
-
return
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
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:
|
|
299
|
+
collection: this.collectionName,
|
|
390
300
|
id_document: '',
|
|
391
|
-
payload:
|
|
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
|
-
|
|
309
|
+
ResolveIOServer.getMongoManager().addToQueue({
|
|
400
310
|
_id: 0,
|
|
401
|
-
name_collection:
|
|
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: [
|
|
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
|
-
|
|
420
|
-
return
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
if (
|
|
424
|
-
|
|
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:
|
|
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
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
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:
|
|
361
|
+
collection: this.collectionName,
|
|
457
362
|
id_document: '',
|
|
458
|
-
payload:
|
|
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
|
-
|
|
371
|
+
ResolveIOServer.getMongoManager().addToQueue({
|
|
467
372
|
_id: 0,
|
|
468
|
-
name_collection:
|
|
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: [
|
|
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
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
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
|
-
|
|
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
|
-
|
|
617
|
-
return
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
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
|
-
|
|
630
|
-
|
|
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
|
-
|
|
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:
|
|
539
|
+
collection: this.collectionName,
|
|
641
540
|
id_document: doc['_id'],
|
|
642
|
-
payload:
|
|
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
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
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
|
-
|
|
689
|
-
|
|
690
|
-
|
|
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
|
-
|
|
693
|
-
|
|
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
|
-
|
|
696
|
-
|
|
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
|
-
|
|
701
|
-
return
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
return
|
|
705
|
-
}
|
|
706
|
-
|
|
707
|
-
return
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
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:
|
|
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
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
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
|
-
|
|
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
|
-
}
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
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
|
-
|
|
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
|
-
|
|
719
|
+
reject(res.result.ok);
|
|
767
720
|
}
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
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
|
-
|
|
787
|
-
|
|
788
|
-
|
|
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
|
-
},
|
|
796
|
-
|
|
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
|
-
},
|
|
843
|
-
|
|
842
|
+
}, err => reject(err));
|
|
843
|
+
}
|
|
844
844
|
}
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
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
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
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
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
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: '
|
|
930
|
+
error: 'Too Many Attempts'
|
|
907
931
|
});
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
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
|
-
|
|
979
|
-
return
|
|
941
|
+
});
|
|
942
|
+
}
|
|
943
|
+
serializeUser() {
|
|
944
|
+
return (user, cb) => {
|
|
980
945
|
cb(null, user.username);
|
|
981
946
|
};
|
|
982
|
-
}
|
|
983
|
-
|
|
984
|
-
return
|
|
985
|
-
|
|
947
|
+
}
|
|
948
|
+
deserializeUser() {
|
|
949
|
+
return (username, cb) => {
|
|
950
|
+
Users.findOne({ username: username }).then(res => {
|
|
986
951
|
cb(null, res);
|
|
987
|
-
},
|
|
952
|
+
}, err => {
|
|
988
953
|
cb(err, null);
|
|
989
954
|
});
|
|
990
955
|
};
|
|
991
|
-
}
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
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
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
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
|
-
|
|
1090
|
-
return
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
}
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
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(
|
|
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(
|
|
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
|