@resolveio/server-lib 12.5.54 → 12.5.55
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.d.ts +1 -0
- package/client-server-app.js +48 -0
- package/client-server-app.js.map +1 -0
- package/collections/app-status.collection.d.ts +3 -0
- package/collections/app-status.collection.js +51 -0
- package/collections/app-status.collection.js.map +1 -0
- package/collections/counter.collection.d.ts +3 -0
- package/collections/counter.collection.js +54 -0
- package/collections/counter.collection.js.map +1 -0
- package/collections/cron-job-history.collection.d.ts +3 -0
- package/collections/cron-job-history.collection.js +82 -0
- package/collections/cron-job-history.collection.js.map +1 -0
- package/collections/cron-job.collection.d.ts +3 -0
- package/collections/cron-job.collection.js +85 -0
- package/collections/cron-job.collection.js.map +1 -0
- package/collections/email-history.collection.d.ts +3 -0
- package/collections/email-history.collection.js +66 -0
- package/collections/email-history.collection.js.map +1 -0
- package/collections/email-verified.collection.d.ts +3 -0
- package/collections/email-verified.collection.js +61 -0
- package/collections/email-verified.collection.js.map +1 -0
- package/collections/file.collection.d.ts +3 -0
- package/collections/file.collection.js +73 -0
- package/collections/file.collection.js.map +1 -0
- package/collections/flag.collection.d.ts +3 -0
- package/collections/flag.collection.js +56 -0
- package/collections/flag.collection.js.map +1 -0
- package/collections/log-method-latency.collection.d.ts +3 -0
- package/collections/log-method-latency.collection.js +64 -0
- package/collections/log-method-latency.collection.js.map +1 -0
- package/collections/log-subscription.collection.d.ts +3 -0
- package/collections/log-subscription.collection.js +68 -0
- package/collections/log-subscription.collection.js.map +1 -0
- package/collections/log-ts.collection.d.ts +3 -0
- package/collections/log-ts.collection.js +78 -0
- package/collections/log-ts.collection.js.map +1 -0
- package/collections/log.collection.d.ts +3 -0
- package/collections/log.collection.js +77 -0
- package/collections/log.collection.js.map +1 -0
- package/collections/logged-in-users.collection.d.ts +3 -0
- package/collections/logged-in-users.collection.js +65 -0
- package/collections/logged-in-users.collection.js.map +1 -0
- package/collections/method-response.collection.d.ts +3 -0
- package/collections/method-response.collection.js +66 -0
- package/collections/method-response.collection.js.map +1 -0
- package/collections/monitor-cpu.collection.d.ts +3 -0
- package/collections/monitor-cpu.collection.js +63 -0
- package/collections/monitor-cpu.collection.js.map +1 -0
- package/collections/monitor-memory.collection.d.ts +3 -0
- package/collections/monitor-memory.collection.js +75 -0
- package/collections/monitor-memory.collection.js.map +1 -0
- package/collections/monitor-mongo.collection.d.ts +3 -0
- package/collections/monitor-mongo.collection.js +69 -0
- package/collections/monitor-mongo.collection.js.map +1 -0
- package/collections/notification.collection.d.ts +3 -0
- package/collections/notification.collection.js +51 -0
- package/collections/notification.collection.js.map +1 -0
- package/collections/report-builder-dashboard-builder.collection.d.ts +3 -0
- package/collections/report-builder-dashboard-builder.collection.js +109 -0
- package/collections/report-builder-dashboard-builder.collection.js.map +1 -0
- package/collections/report-builder-library.collection.d.ts +3 -0
- package/collections/report-builder-library.collection.js +87 -0
- package/collections/report-builder-library.collection.js.map +1 -0
- package/collections/report-builder-report.collection.d.ts +4 -0
- package/collections/report-builder-report.collection.js +149 -0
- package/collections/report-builder-report.collection.js.map +1 -0
- package/collections/user-group.collection.d.ts +4 -0
- package/collections/user-group.collection.js +90 -0
- package/collections/user-group.collection.js.map +1 -0
- package/collections/user-guide.collection.d.ts +3 -0
- package/collections/user-guide.collection.js +57 -0
- package/collections/user-guide.collection.js.map +1 -0
- package/collections/user.collection.d.ts +4 -0
- package/collections/user.collection.js +259 -0
- package/collections/user.collection.js.map +1 -0
- package/cron/cron.d.ts +5 -0
- package/cron/cron.js +162 -0
- package/cron/cron.js.map +1 -0
- package/fixtures/cron-jobs.d.ts +1 -0
- package/fixtures/cron-jobs.js +73 -0
- package/fixtures/cron-jobs.js.map +1 -0
- package/fixtures/init.d.ts +1 -0
- package/fixtures/init.js +59 -0
- package/fixtures/init.js.map +1 -0
- package/http/auth.d.ts +2 -0
- package/http/auth.js +763 -0
- package/http/auth.js.map +1 -0
- package/http/health.d.ts +1 -0
- package/http/health.js +12 -0
- package/http/health.js.map +1 -0
- package/http/home.d.ts +2 -0
- package/http/home.js +66 -0
- package/http/home.js.map +1 -0
- package/index.d.ts +23 -0
- package/index.js +127 -0
- package/index.js.map +1 -0
- package/managers/cron.manager.d.ts +17 -0
- package/managers/cron.manager.js +242 -0
- package/managers/cron.manager.js.map +1 -0
- package/managers/method.manager.d.ts +38 -0
- package/managers/method.manager.js +595 -0
- package/managers/method.manager.js.map +1 -0
- package/managers/mongo.manager.d.ts +178 -0
- package/managers/mongo.manager.js +2221 -0
- package/managers/mongo.manager.js.map +1 -0
- package/managers/monitor.manager.d.ts +59 -0
- package/managers/monitor.manager.js +351 -0
- package/managers/monitor.manager.js.map +1 -0
- package/managers/subscription.manager.d.ts +50 -0
- package/managers/subscription.manager.js +1091 -0
- package/managers/subscription.manager.js.map +1 -0
- package/managers/subscription.performance.d.ts +1 -0
- package/{src/managers/subscription.performance.ts → managers/subscription.performance.js} +33 -68
- package/managers/subscription.performance.js.map +1 -0
- package/methods/accounts.d.ts +2 -0
- package/methods/accounts.js +274 -0
- package/methods/accounts.js.map +1 -0
- package/methods/aws.d.ts +2 -0
- package/methods/aws.js +617 -0
- package/methods/aws.js.map +1 -0
- package/methods/collections.d.ts +2 -0
- package/methods/collections.js +499 -0
- package/methods/collections.js.map +1 -0
- package/methods/counters.d.ts +2 -0
- package/methods/counters.js +122 -0
- package/methods/counters.js.map +1 -0
- package/methods/cron-jobs.d.ts +2 -0
- package/methods/cron-jobs.js +1315 -0
- package/methods/cron-jobs.js.map +1 -0
- package/methods/flags.d.ts +2 -0
- package/methods/flags.js +9 -0
- package/methods/flags.js.map +1 -0
- package/methods/logs.d.ts +2 -0
- package/methods/logs.js +371 -0
- package/methods/logs.js.map +1 -0
- package/methods/monitor.d.ts +2 -0
- package/methods/monitor.js +565 -0
- package/methods/monitor.js.map +1 -0
- package/methods/pdf.d.ts +2 -0
- package/methods/pdf.js +468 -0
- package/methods/pdf.js.map +1 -0
- package/methods/report-builder.d.ts +2 -0
- package/methods/report-builder.js +858 -0
- package/methods/report-builder.js.map +1 -0
- package/methods/support.d.ts +2 -0
- package/methods/support.js +246 -0
- package/methods/support.js.map +1 -0
- package/{src/models/app-status.model.ts → models/app-status.model.d.ts} +2 -3
- package/models/app-status.model.js +4 -0
- package/models/app-status.model.js.map +1 -0
- package/{src/models/billing-logged-in-users.model.ts → models/billing-logged-in-users.model.d.ts} +4 -5
- package/models/billing-logged-in-users.model.js +4 -0
- package/models/billing-logged-in-users.model.js.map +1 -0
- package/models/collection-document.model.d.ts +19 -0
- package/models/collection-document.model.js +4 -0
- package/models/collection-document.model.js.map +1 -0
- package/{src/models/counter.model.ts → models/counter.model.d.ts} +3 -4
- package/models/counter.model.js +4 -0
- package/models/counter.model.js.map +1 -0
- package/models/cron-job-history.model.d.ts +12 -0
- package/models/cron-job-history.model.js +4 -0
- package/models/cron-job-history.model.js.map +1 -0
- package/models/cron-job.model.d.ts +13 -0
- package/models/cron-job.model.js +4 -0
- package/models/cron-job.model.js.map +1 -0
- package/models/dialog.model.d.ts +23 -0
- package/models/dialog.model.js +4 -0
- package/models/dialog.model.js.map +1 -0
- package/{src/models/email-history.model.ts → models/email-history.model.d.ts} +7 -8
- package/models/email-history.model.js +4 -0
- package/models/email-history.model.js.map +1 -0
- package/{src/models/email-verified.model.ts → models/email-verified.model.d.ts} +5 -6
- package/models/email-verified.model.js +4 -0
- package/models/email-verified.model.js.map +1 -0
- package/{src/models/file.model.ts → models/file.model.d.ts} +7 -8
- package/models/file.model.js +4 -0
- package/models/file.model.js.map +1 -0
- package/{src/models/flag.model.ts → models/flag.model.d.ts} +3 -4
- package/models/flag.model.js +4 -0
- package/models/flag.model.js.map +1 -0
- package/{src/models/log-method-latency.model.ts → models/log-method-latency.model.d.ts} +4 -5
- package/models/log-method-latency.model.js +4 -0
- package/models/log-method-latency.model.js.map +1 -0
- package/{src/models/log-subscription.model.ts → models/log-subscription.model.d.ts} +6 -8
- package/models/log-subscription.model.js +4 -0
- package/models/log-subscription.model.js.map +1 -0
- package/{src/models/log.model.ts → models/log.model.d.ts} +10 -12
- package/models/log.model.js +4 -0
- package/models/log.model.js.map +1 -0
- package/{src/models/logged-in-users.model.ts → models/logged-in-users.model.d.ts} +5 -6
- package/models/logged-in-users.model.js +4 -0
- package/models/logged-in-users.model.js.map +1 -0
- package/{src/models/method-response.model.ts → models/method-response.model.d.ts} +6 -7
- package/models/method-response.model.js +4 -0
- package/models/method-response.model.js.map +1 -0
- package/models/method.model.d.ts +9 -0
- package/models/method.model.js +4 -0
- package/models/method.model.js.map +1 -0
- package/{src/models/monitor-cpu.model.ts → models/monitor-cpu.model.d.ts} +7 -9
- package/models/monitor-cpu.model.js +4 -0
- package/models/monitor-cpu.model.js.map +1 -0
- package/models/monitor-memory.model.d.ts +15 -0
- package/models/monitor-memory.model.js +4 -0
- package/models/monitor-memory.model.js.map +1 -0
- package/models/monitor-mongo.model.d.ts +13 -0
- package/models/monitor-mongo.model.js +4 -0
- package/models/monitor-mongo.model.js.map +1 -0
- package/{src/models/notification.model.ts → models/notification.model.d.ts} +2 -3
- package/models/notification.model.js +4 -0
- package/models/notification.model.js.map +1 -0
- package/models/pagination.model.d.ts +10 -0
- package/models/pagination.model.js +24 -0
- package/models/pagination.model.js.map +1 -0
- package/models/permission.model.d.ts +12 -0
- package/models/permission.model.js +4 -0
- package/models/permission.model.js.map +1 -0
- package/models/report-builder-dashboard-builder.model.d.ts +25 -0
- package/models/report-builder-dashboard-builder.model.js +4 -0
- package/models/report-builder-dashboard-builder.model.js.map +1 -0
- package/models/report-builder-library.model.d.ts +17 -0
- package/models/report-builder-library.model.js +4 -0
- package/models/report-builder-library.model.js.map +1 -0
- package/models/report-builder-report.model.d.ts +98 -0
- package/models/report-builder-report.model.js +4 -0
- package/models/report-builder-report.model.js.map +1 -0
- package/models/report-builder.model.d.ts +61 -0
- package/models/report-builder.model.js +4 -0
- package/models/report-builder.model.js.map +1 -0
- package/models/select-data-label.model.d.ts +9 -0
- package/models/select-data-label.model.js +4 -0
- package/models/select-data-label.model.js.map +1 -0
- package/models/server-response.model.d.ts +5 -0
- package/models/server-response.model.js +4 -0
- package/models/server-response.model.js.map +1 -0
- package/models/subscription.model.d.ts +23 -0
- package/models/subscription.model.js +4 -0
- package/models/subscription.model.js.map +1 -0
- package/models/support-ticket.model.d.ts +72 -0
- package/models/support-ticket.model.js +4 -0
- package/models/support-ticket.model.js.map +1 -0
- package/models/user-group.model.d.ts +19 -0
- package/models/user-group.model.js +4 -0
- package/models/user-group.model.js.map +1 -0
- package/{src/models/user-guide.model.ts → models/user-guide.model.d.ts} +4 -5
- package/models/user-guide.model.js +4 -0
- package/models/user-guide.model.js.map +1 -0
- package/models/user.model.d.ts +70 -0
- package/models/user.model.js +4 -0
- package/models/user.model.js.map +1 -0
- package/package.json +1 -1
- package/{src/public_api.ts → public_api.d.ts} +1 -1
- package/public_api.js +75 -0
- package/public_api.js.map +1 -0
- package/publications/app-status.d.ts +2 -0
- package/publications/app-status.js +17 -0
- package/publications/app-status.js.map +1 -0
- package/publications/cron-jobs.d.ts +2 -0
- package/publications/cron-jobs.js +33 -0
- package/publications/cron-jobs.js.map +1 -0
- package/publications/files.d.ts +2 -0
- package/publications/files.js +37 -0
- package/publications/files.js.map +1 -0
- package/publications/flags.d.ts +2 -0
- package/publications/flags.js +23 -0
- package/publications/flags.js.map +1 -0
- package/publications/logs.d.ts +2 -0
- package/publications/logs.js +165 -0
- package/publications/logs.js.map +1 -0
- package/publications/method-responses.d.ts +2 -0
- package/publications/method-responses.js +17 -0
- package/publications/method-responses.js.map +1 -0
- package/publications/notifications.d.ts +2 -0
- package/publications/notifications.js +17 -0
- package/publications/notifications.js.map +1 -0
- package/publications/report-builder-dashboard-builders.d.ts +2 -0
- package/publications/report-builder-dashboard-builders.js +43 -0
- package/publications/report-builder-dashboard-builders.js.map +1 -0
- package/publications/report-builder-libraries.d.ts +2 -0
- package/publications/report-builder-libraries.js +90 -0
- package/publications/report-builder-libraries.js.map +1 -0
- package/publications/report-builder-reports.d.ts +2 -0
- package/publications/report-builder-reports.js +51 -0
- package/publications/report-builder-reports.js.map +1 -0
- package/publications/super-admin.d.ts +2 -0
- package/publications/super-admin.js +17 -0
- package/publications/super-admin.js.map +1 -0
- package/publications/user-groups.d.ts +1 -0
- package/publications/user-groups.js +17 -0
- package/publications/user-groups.js.map +1 -0
- package/publications/user-guides.d.ts +1 -0
- package/publications/user-guides.js +17 -0
- package/publications/user-guides.js.map +1 -0
- package/server-app.d.ts +52 -0
- package/server-app.js +739 -0
- package/server-app.js.map +1 -0
- package/util/common.d.ts +24 -0
- package/util/common.js +511 -0
- package/util/common.js.map +1 -0
- package/util/schema-report-builder.d.ts +6 -0
- package/util/schema-report-builder.js +453 -0
- package/util/schema-report-builder.js.map +1 -0
- package/.nodemon.json +0 -5
- package/.vscode/settings.json +0 -3
- package/README.md +0 -22
- package/compileDTS.pl +0 -64
- package/gulpfile.js +0 -127
- package/mongodbensurers.js +0 -2
- package/mongostop.js +0 -3
- package/settings.development.json +0 -13
- package/src/client-server-app.ts +0 -7
- package/src/collections/app-status.collection.ts +0 -51
- package/src/collections/counter.collection.ts +0 -54
- package/src/collections/cron-job-history.collection.ts +0 -82
- package/src/collections/cron-job.collection.ts +0 -85
- package/src/collections/email-history.collection.ts +0 -66
- package/src/collections/email-verified.collection.ts +0 -61
- package/src/collections/file.collection.ts +0 -73
- package/src/collections/flag.collection.ts +0 -56
- package/src/collections/log-method-latency.collection.ts +0 -64
- package/src/collections/log-subscription.collection.ts +0 -68
- package/src/collections/log-ts.collection.ts +0 -78
- package/src/collections/log.collection.ts +0 -77
- package/src/collections/logged-in-users.collection.ts +0 -65
- package/src/collections/method-response.collection.ts +0 -66
- package/src/collections/monitor-cpu.collection.ts +0 -63
- package/src/collections/monitor-memory.collection.ts +0 -75
- package/src/collections/monitor-mongo.collection.ts +0 -69
- package/src/collections/notification.collection.ts +0 -51
- package/src/collections/report-builder-dashboard-builder.collection.ts +0 -109
- package/src/collections/report-builder-library.collection.ts +0 -89
- package/src/collections/report-builder-report.collection.ts +0 -149
- package/src/collections/user-group.collection.ts +0 -90
- package/src/collections/user-guide.collection.ts +0 -57
- package/src/collections/user.collection.ts +0 -259
- package/src/cron/cron.ts +0 -188
- package/src/fixtures/cron-jobs.ts +0 -25
- package/src/fixtures/init.ts +0 -10
- package/src/http/auth.ts +0 -732
- package/src/http/health.ts +0 -7
- package/src/http/home.ts +0 -71
- package/src/index.ts +0 -71
- package/src/managers/cron.manager.ts +0 -208
- package/src/managers/method.manager.ts +0 -607
- package/src/managers/mongo.manager.ts +0 -2282
- package/src/managers/monitor.manager.ts +0 -323
- package/src/managers/subscription.manager.ts +0 -1196
- package/src/methods/accounts.ts +0 -202
- package/src/methods/aws.ts +0 -507
- package/src/methods/collections.ts +0 -512
- package/src/methods/counters.ts +0 -75
- package/src/methods/cron-jobs.ts +0 -1454
- package/src/methods/flags.ts +0 -7
- package/src/methods/logs.ts +0 -327
- package/src/methods/monitor.ts +0 -548
- package/src/methods/pdf.ts +0 -615
- package/src/methods/report-builder.ts +0 -876
- package/src/methods/support.ts +0 -146
- package/src/models/collection-document.model.ts +0 -22
- package/src/models/cron-job-history.model.ts +0 -13
- package/src/models/cron-job.model.ts +0 -14
- package/src/models/dialog.model.ts +0 -25
- package/src/models/method.model.ts +0 -11
- package/src/models/monitor-memory.model.ts +0 -17
- package/src/models/monitor-mongo.model.ts +0 -15
- package/src/models/pagination.model.ts +0 -30
- package/src/models/permission.model.ts +0 -14
- package/src/models/report-builder-dashboard-builder.model.ts +0 -29
- package/src/models/report-builder-library.model.ts +0 -20
- package/src/models/report-builder-report.model.ts +0 -110
- package/src/models/report-builder.model.ts +0 -68
- package/src/models/select-data-label.model.ts +0 -9
- package/src/models/server-response.model.ts +0 -5
- package/src/models/subscription.model.ts +0 -64
- package/src/models/support-ticket.model.ts +0 -81
- package/src/models/user-group.model.ts +0 -23
- package/src/models/user.model.ts +0 -79
- package/src/publications/app-status.ts +0 -13
- package/src/publications/cron-jobs.ts +0 -30
- package/src/publications/files.ts +0 -34
- package/src/publications/flags.ts +0 -20
- package/src/publications/logs.ts +0 -164
- package/src/publications/method-responses.ts +0 -13
- package/src/publications/notifications.ts +0 -13
- package/src/publications/report-builder-dashboard-builders.ts +0 -39
- package/src/publications/report-builder-libraries.ts +0 -41
- package/src/publications/report-builder-reports.ts +0 -48
- package/src/publications/super-admin.ts +0 -13
- package/src/publications/user-groups.ts +0 -12
- package/src/publications/user-guides.ts +0 -12
- package/src/server-app.ts +0 -769
- package/src/util/common.ts +0 -519
- package/src/util/schema-report-builder.ts +0 -413
- package/tsconfig.json +0 -30
- package/tslint.json +0 -143
- /package/{src/private → private}/email-templates/enrollment.html +0 -0
- /package/{src/private → private}/email-templates/forgot-password.html +0 -0
- /package/{src/private → private}/email-templates/support-ticket-deleted.html +0 -0
- /package/{src/private → private}/email-templates/support-ticket-modified.html +0 -0
- /package/{src/private → private}/email-templates/support-ticket.html +0 -0
- /package/{src/private → private}/images/ResolveIO.png +0 -0
|
@@ -0,0 +1,2221 @@
|
|
|
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
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
18
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
19
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
20
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
21
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
22
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
23
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
27
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
28
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
29
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
30
|
+
function step(op) {
|
|
31
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
32
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
33
|
+
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;
|
|
34
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
35
|
+
switch (op[0]) {
|
|
36
|
+
case 0: case 1: t = op; break;
|
|
37
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
38
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
39
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
40
|
+
default:
|
|
41
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
42
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
43
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
44
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
45
|
+
if (t[2]) _.ops.pop();
|
|
46
|
+
_.trys.pop(); continue;
|
|
47
|
+
}
|
|
48
|
+
op = body.call(thisArg, _);
|
|
49
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
50
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
54
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
55
|
+
if (ar || !(i in from)) {
|
|
56
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
57
|
+
ar[i] = from[i];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
61
|
+
};
|
|
62
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
63
|
+
exports.objectIdHexString = exports.MongoManagerUserCollection = exports.MongoManagerCollection = exports.MongoManagerModel = exports.MongoManager = void 0;
|
|
64
|
+
var simpl_schema_1 = require("simpl-schema");
|
|
65
|
+
var mongodb_1 = require("mongodb");
|
|
66
|
+
var index_1 = require("../index");
|
|
67
|
+
var user_collection_1 = require("../collections/user.collection");
|
|
68
|
+
var common_1 = require("../util/common");
|
|
69
|
+
var log_collection_1 = require("../collections/log.collection");
|
|
70
|
+
var NodeCache = require("node-cache");
|
|
71
|
+
var crypto = require('crypto');
|
|
72
|
+
var scmp = require('scmp');
|
|
73
|
+
var monitor_manager_1 = require("./monitor.manager");
|
|
74
|
+
var os_1 = require("os");
|
|
75
|
+
var numCPUs = (0, os_1.cpus)().length;
|
|
76
|
+
var v8 = require('v8');
|
|
77
|
+
var MongoManager = /** @class */ (function () {
|
|
78
|
+
function MongoManager() {
|
|
79
|
+
var _this = this;
|
|
80
|
+
this._collections = [];
|
|
81
|
+
this._mongoQueue = [];
|
|
82
|
+
this._mongoQueueId = 0;
|
|
83
|
+
this._mongoQueueRunning = false;
|
|
84
|
+
this._mongoQueueCacheId = 1;
|
|
85
|
+
this._heapStats = v8.getHeapStatistics();
|
|
86
|
+
this._heapLimit = this._heapStats.heap_size_limit / numCPUs / 8;
|
|
87
|
+
this._serverCollections = [];
|
|
88
|
+
this._debugMongoQueueHits = 0;
|
|
89
|
+
this._debugRemoveCacheHits = 0;
|
|
90
|
+
this._debugMongoQueueCollections = [];
|
|
91
|
+
this._nodeCache = new NodeCache({ stdTTL: 0, checkperiod: 0 });
|
|
92
|
+
setInterval(function () {
|
|
93
|
+
if (index_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()) {
|
|
94
|
+
console.log(new Date(), 'Mongo Manager', 'Collections', _this._collections.length);
|
|
95
|
+
console.log(new Date(), 'Mongo Manager', 'Mongo Queue', _this._mongoQueue.length, JSON.stringify(_this._mongoQueue.sort(function (a, b) { return a.name_collection.localeCompare(b.name_collection); }), null, 2));
|
|
96
|
+
console.log(new Date(), 'Mongo Manager', 'Server Collections', _this._serverCollections.length);
|
|
97
|
+
console.log(new Date(), 'Mongo Manager', 'Mongo Queue Hits', _this._debugMongoQueueHits);
|
|
98
|
+
console.log(new Date(), 'Mongo Manager', 'Mongo Queue Collections', _this._debugMongoQueueCollections.sort(function (a, b) { return a.collection.localeCompare(b.collection) || a.function.localeCompare(b.function); }));
|
|
99
|
+
console.log(new Date(), 'Mongo Manager', 'Cache Cleanup Hits', JSON.stringify(_this._debugRemoveCacheHits, null, 2));
|
|
100
|
+
}
|
|
101
|
+
_this._debugMongoQueueHits = 0;
|
|
102
|
+
_this._debugRemoveCacheHits = 0;
|
|
103
|
+
_this._debugMongoQueueCollections = [];
|
|
104
|
+
}, 60000);
|
|
105
|
+
this.initServerCollections();
|
|
106
|
+
this.runQueue();
|
|
107
|
+
}
|
|
108
|
+
MongoManager.prototype.initServerCollections = function () {
|
|
109
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
110
|
+
var collections;
|
|
111
|
+
return __generator(this, function (_a) {
|
|
112
|
+
switch (_a.label) {
|
|
113
|
+
case 0: return [4 /*yield*/, index_1.ResolveIOServer.getMainDB().listCollections().toArray()];
|
|
114
|
+
case 1:
|
|
115
|
+
collections = _a.sent();
|
|
116
|
+
this._serverCollections = collections;
|
|
117
|
+
return [2 /*return*/];
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
MongoManager.prototype.getServerCollections = function () {
|
|
123
|
+
return this._serverCollections;
|
|
124
|
+
};
|
|
125
|
+
MongoManager.prototype.registerCollection = function (collection) {
|
|
126
|
+
var _this = this;
|
|
127
|
+
if (collection.collectionOptions && collection.collectionOptions.timeseries && collection.collectionOptions.timeseries.timeField && this._serverCollections.some(function (a) { return a.name === collection.collectionName && a.type === 'collection'; })) {
|
|
128
|
+
index_1.ResolveIOServer.getMainDB().dropCollection(collection.collectionName).then(function () {
|
|
129
|
+
_this.createCollection(collection);
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
this._collections.push(collection);
|
|
133
|
+
};
|
|
134
|
+
MongoManager.prototype.createCollection = function (collection) {
|
|
135
|
+
index_1.ResolveIOServer.getMainDB().createCollection(collection.collectionName, collection.collectionOptions);
|
|
136
|
+
};
|
|
137
|
+
MongoManager.prototype.collections = function () {
|
|
138
|
+
return this._collections;
|
|
139
|
+
};
|
|
140
|
+
MongoManager.prototype.collection = function (collectionName) {
|
|
141
|
+
return this._collections.filter(function (a) { return a.collectionName === collectionName; })[0];
|
|
142
|
+
};
|
|
143
|
+
MongoManager.prototype.addToQueue = function (mongoQueue) {
|
|
144
|
+
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];
|
|
145
|
+
if (_mongoQueue) {
|
|
146
|
+
if (!_mongoQueue.running) {
|
|
147
|
+
_mongoQueue.cbs.push(mongoQueue.cbs[0]);
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
_mongoQueue.cbs_next.push(mongoQueue.cbs[0]);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
mongoQueue._id = this._mongoQueueId;
|
|
155
|
+
this._mongoQueueId += 1;
|
|
156
|
+
this._mongoQueue.splice(0, 0, mongoQueue);
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
MongoManager.prototype.runQueue = function () {
|
|
160
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
161
|
+
var _this = this;
|
|
162
|
+
return __generator(this, function (_a) {
|
|
163
|
+
setInterval(function () {
|
|
164
|
+
if (!_this._mongoQueueRunning) {
|
|
165
|
+
var queueArr = _this._mongoQueue.filter(function (a) { return !a.running && a.cbs.length; });
|
|
166
|
+
if (queueArr.length) {
|
|
167
|
+
_this._mongoQueueRunning = true;
|
|
168
|
+
var _loop_1 = function (i) {
|
|
169
|
+
var _a, _b, _c;
|
|
170
|
+
_this._debugMongoQueueHits += 1;
|
|
171
|
+
var mongoQueue = queueArr[i];
|
|
172
|
+
if (!_this._debugMongoQueueCollections.some(function (a) { return a.collection === mongoQueue.name_collection && a.function === mongoQueue.name_function; })) {
|
|
173
|
+
_this._debugMongoQueueCollections.push({
|
|
174
|
+
collection: mongoQueue.name_collection,
|
|
175
|
+
function: mongoQueue.name_function,
|
|
176
|
+
hits: 1
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
_this._debugMongoQueueCollections.filter(function (a) { return a.collection === mongoQueue.name_collection && a.function === mongoQueue.name_function; })[0].hits += 1;
|
|
181
|
+
}
|
|
182
|
+
mongoQueue.running = true;
|
|
183
|
+
mongoQueue.invalidateFlag = false;
|
|
184
|
+
if (mongoQueue.cacheId) {
|
|
185
|
+
try {
|
|
186
|
+
var res_1 = JSON.parse(_this._nodeCache.get(mongoQueue.cacheId), common_1.dateReviver);
|
|
187
|
+
mongoQueue.cbs.forEach(function (cb) {
|
|
188
|
+
cb(null, res_1, true);
|
|
189
|
+
});
|
|
190
|
+
mongoQueue.cbs = [];
|
|
191
|
+
mongoQueue.running = false;
|
|
192
|
+
}
|
|
193
|
+
catch (_d) {
|
|
194
|
+
mongoQueue.cacheId = 0;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (!mongoQueue.cacheId) {
|
|
198
|
+
if (!mongoQueue.name_function_addt) {
|
|
199
|
+
var monitor_1 = new monitor_manager_1.MonitorMongo(mongoQueue.name_function, mongoQueue.name_collection, JSON.stringify(mongoQueue.data_function));
|
|
200
|
+
(_a = index_1.ResolveIOServer.getMainDB().collection(mongoQueue.name_collection))[mongoQueue.name_function].apply(_a, mongoQueue.data_function).then(function (res) {
|
|
201
|
+
monitor_1.finish();
|
|
202
|
+
if (!mongoQueue.invalidateFlag) {
|
|
203
|
+
mongoQueue.invalidateCount = 0;
|
|
204
|
+
var stringifyLength = 0;
|
|
205
|
+
var stringifyFail = false;
|
|
206
|
+
try {
|
|
207
|
+
var stringify = JSON.stringify(res).replace(/[\[\]\,\"]/g, '');
|
|
208
|
+
stringifyLength = stringify.length;
|
|
209
|
+
}
|
|
210
|
+
catch (e) {
|
|
211
|
+
stringifyFail = true;
|
|
212
|
+
}
|
|
213
|
+
if (!stringifyFail && res !== null && res !== undefined && (!Array.isArray(res) || res.length) && stringifyLength < 10000000) {
|
|
214
|
+
mongoQueue.cacheId = _this._mongoQueueCacheId;
|
|
215
|
+
_this._mongoQueueCacheId += 1;
|
|
216
|
+
_this.addToCache(mongoQueue.cacheId, res);
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
mongoQueue.cacheId = 0;
|
|
220
|
+
}
|
|
221
|
+
mongoQueue.cbs.forEach(function (cb) {
|
|
222
|
+
cb(null, res, false);
|
|
223
|
+
});
|
|
224
|
+
if (mongoQueue.cbs_next.length) {
|
|
225
|
+
mongoQueue.cbs = mongoQueue.cbs_next;
|
|
226
|
+
mongoQueue.cbs_next = [];
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
mongoQueue.cbs = [];
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
if (mongoQueue.invalidateCount >= 5) {
|
|
234
|
+
mongoQueue.invalidateCount = 0;
|
|
235
|
+
mongoQueue.cbs.forEach(function (cb) {
|
|
236
|
+
cb(null, res, false);
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
mongoQueue.cbs = mongoQueue.cbs.concat(mongoQueue.cbs_next);
|
|
240
|
+
mongoQueue.cbs_next = [];
|
|
241
|
+
}
|
|
242
|
+
mongoQueue.running = false;
|
|
243
|
+
}, function (err) {
|
|
244
|
+
monitor_1.finish();
|
|
245
|
+
console.log.apply(console, __spreadArray(__spreadArray([new Date(), 'Error ' + mongoQueue.name_function, mongoQueue.name_collection], mongoQueue.data_function, false), [err], false));
|
|
246
|
+
mongoQueue.cbs.forEach(function (cb) {
|
|
247
|
+
cb(err, null, false);
|
|
248
|
+
});
|
|
249
|
+
mongoQueue.cbs_next.forEach(function (cb) {
|
|
250
|
+
cb(err, null, false);
|
|
251
|
+
});
|
|
252
|
+
if (_this._mongoQueue.map(function (a) { return a._id; }).indexOf(mongoQueue._id) >= 0) {
|
|
253
|
+
_this._mongoQueue.splice(_this._mongoQueue.map(function (a) { return a._id; }).indexOf(mongoQueue._id), 1);
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
var monitor_2 = new monitor_manager_1.MonitorMongo(mongoQueue.name_function, mongoQueue.name_collection, JSON.stringify(__spreadArray(__spreadArray(__spreadArray([], mongoQueue.data_function, true), [mongoQueue.name_function_addt], false), mongoQueue.data_function_addt, true)));
|
|
259
|
+
(_b = (_c = index_1.ResolveIOServer.getMainDB().collection(mongoQueue.name_collection))[mongoQueue.name_function].apply(_c, mongoQueue.data_function))[mongoQueue.name_function_addt].apply(_b, mongoQueue.data_function_addt).then(function (res) {
|
|
260
|
+
monitor_2.finish();
|
|
261
|
+
if (!mongoQueue.invalidateFlag) {
|
|
262
|
+
mongoQueue.invalidateCount = 0;
|
|
263
|
+
var stringifyLength = 0;
|
|
264
|
+
var stringifyFail = false;
|
|
265
|
+
try {
|
|
266
|
+
var stringify = JSON.stringify(res).replace(/[\[\]\,\"]/g, '');
|
|
267
|
+
stringifyLength = stringify.length;
|
|
268
|
+
}
|
|
269
|
+
catch (e) {
|
|
270
|
+
stringifyFail = true;
|
|
271
|
+
}
|
|
272
|
+
if (!stringifyFail && res !== null && res !== undefined && (!Array.isArray(res) || res.length) && stringifyLength < 10000000) {
|
|
273
|
+
mongoQueue.cacheId = _this._mongoQueueCacheId;
|
|
274
|
+
_this._mongoQueueCacheId += 1;
|
|
275
|
+
_this.addToCache(mongoQueue.cacheId, res);
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
mongoQueue.cacheId = 0;
|
|
279
|
+
}
|
|
280
|
+
mongoQueue.cbs.forEach(function (cb) {
|
|
281
|
+
cb(null, res, false);
|
|
282
|
+
});
|
|
283
|
+
if (mongoQueue.cbs_next.length) {
|
|
284
|
+
mongoQueue.cbs = mongoQueue.cbs_next;
|
|
285
|
+
mongoQueue.cbs_next = [];
|
|
286
|
+
}
|
|
287
|
+
else {
|
|
288
|
+
mongoQueue.cbs = [];
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
if (mongoQueue.invalidateCount >= 5) {
|
|
293
|
+
mongoQueue.invalidateCount = 0;
|
|
294
|
+
mongoQueue.cbs.forEach(function (cb) {
|
|
295
|
+
cb(null, res, false);
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
mongoQueue.cbs = mongoQueue.cbs.concat(mongoQueue.cbs_next);
|
|
299
|
+
mongoQueue.cbs_next = [];
|
|
300
|
+
}
|
|
301
|
+
mongoQueue.running = false;
|
|
302
|
+
}, function (err) {
|
|
303
|
+
monitor_2.finish();
|
|
304
|
+
mongoQueue.cbs.forEach(function (cb) {
|
|
305
|
+
cb(err, null, false);
|
|
306
|
+
});
|
|
307
|
+
mongoQueue.cbs_next.forEach(function (cb) {
|
|
308
|
+
cb(err, null, false);
|
|
309
|
+
});
|
|
310
|
+
if (_this._mongoQueue.map(function (a) { return a._id; }).indexOf(mongoQueue._id) >= 0) {
|
|
311
|
+
_this._mongoQueue.splice(_this._mongoQueue.map(function (a) { return a._id; }).indexOf(mongoQueue._id), 1);
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
for (var i = queueArr.length - 1; i >= 0; i--) {
|
|
318
|
+
_loop_1(i);
|
|
319
|
+
}
|
|
320
|
+
_this._mongoQueueRunning = false;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}, 25);
|
|
324
|
+
return [2 /*return*/];
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
};
|
|
328
|
+
MongoManager.prototype.addToCache = function (id, data) {
|
|
329
|
+
var nodeCacheSize = this._nodeCache.getStats().vsize;
|
|
330
|
+
if (nodeCacheSize > this._heapLimit) {
|
|
331
|
+
var deleteCount = 0;
|
|
332
|
+
var queueArr = this._mongoQueue.filter(function (a) { return a.cacheId && !a.running && !a.cbs.length; });
|
|
333
|
+
for (var zz = queueArr.length - 1; zz >= 0; zz--) {
|
|
334
|
+
this._debugRemoveCacheHits += 1;
|
|
335
|
+
var queue = queueArr[zz];
|
|
336
|
+
this._nodeCache.del(queue.cacheId);
|
|
337
|
+
queue.cacheId = 0;
|
|
338
|
+
if (this._mongoQueue.map(function (a) { return a._id; }).indexOf(queue._id) >= 0) {
|
|
339
|
+
this._mongoQueue.splice(this._mongoQueue.map(function (a) { return a._id; }).indexOf(queue._id), 1);
|
|
340
|
+
}
|
|
341
|
+
deleteCount += 1;
|
|
342
|
+
nodeCacheSize = this._nodeCache.getStats().vsize;
|
|
343
|
+
if (nodeCacheSize < this._heapLimit * 0.75) {
|
|
344
|
+
break;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
console.log('Query Cache: ' + 'Too Big, - Deleted: ' + deleteCount + ' - ' + nodeCacheSize);
|
|
348
|
+
}
|
|
349
|
+
this._nodeCache.set(id, JSON.stringify(data));
|
|
350
|
+
};
|
|
351
|
+
MongoManager.prototype.invalidateQueryCache = function (collection) {
|
|
352
|
+
var queueArr = this._mongoQueue.filter(function (a) { return (a.name_collection === collection || a.lookup_collections.includes(collection)); });
|
|
353
|
+
for (var i = queueArr.length - 1; i >= 0; i--) {
|
|
354
|
+
var queue = queueArr[i];
|
|
355
|
+
if (queue.cacheId) {
|
|
356
|
+
this._nodeCache.del(queue.cacheId);
|
|
357
|
+
queue.cacheId = 0;
|
|
358
|
+
}
|
|
359
|
+
if (queue.running) {
|
|
360
|
+
queue.invalidateFlag = true;
|
|
361
|
+
queue.invalidateCount += 1;
|
|
362
|
+
}
|
|
363
|
+
else if (!queue.cbs.length) {
|
|
364
|
+
if (this._mongoQueue.map(function (a) { return a._id; }).indexOf(queue._id) >= 0) {
|
|
365
|
+
this._mongoQueue.splice(this._mongoQueue.map(function (a) { return a._id; }).indexOf(queue._id), 1);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
return MongoManager;
|
|
371
|
+
}());
|
|
372
|
+
exports.MongoManager = MongoManager;
|
|
373
|
+
var MongoManagerModel = /** @class */ (function () {
|
|
374
|
+
function MongoManagerModel(options) {
|
|
375
|
+
this.collection_main = null;
|
|
376
|
+
this.collection_version = null;
|
|
377
|
+
if (options.collectionName === 'users') {
|
|
378
|
+
this.collection_main = new MongoManagerUserCollection(options);
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
this.collection_main = new MongoManagerCollection(options);
|
|
382
|
+
}
|
|
383
|
+
if (options.useVersionCollection) {
|
|
384
|
+
this.collection_main.useVersions = true;
|
|
385
|
+
var versionSchema = (0, common_1.deepCopy)(options.schema);
|
|
386
|
+
versionSchema._id.type = 'Object';
|
|
387
|
+
versionSchema._id.blackbox = true;
|
|
388
|
+
var versionOptions = {
|
|
389
|
+
collectionName: options.collectionName + '.versions',
|
|
390
|
+
schema: versionSchema,
|
|
391
|
+
useVersionCollection: false,
|
|
392
|
+
useReportBuilder: false,
|
|
393
|
+
reportBuilderLookupTables: [],
|
|
394
|
+
timestamps: true,
|
|
395
|
+
createLogs: false,
|
|
396
|
+
checkSchema: false,
|
|
397
|
+
collectionOptions: null
|
|
398
|
+
};
|
|
399
|
+
if (options.collectionName === 'users') {
|
|
400
|
+
this.collection_version = new MongoManagerUserCollection(versionOptions);
|
|
401
|
+
}
|
|
402
|
+
else {
|
|
403
|
+
this.collection_version = new MongoManagerCollection(versionOptions);
|
|
404
|
+
}
|
|
405
|
+
this.collection_version.createIndex({ '_id._id': 1, '_id.__v': 1 });
|
|
406
|
+
this.collection_main.versionCollection = options.collectionName + '.versions';
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
return MongoManagerModel;
|
|
410
|
+
}());
|
|
411
|
+
exports.MongoManagerModel = MongoManagerModel;
|
|
412
|
+
var MongoManagerCollection = /** @class */ (function () {
|
|
413
|
+
function MongoManagerCollection(options) {
|
|
414
|
+
var _this = this;
|
|
415
|
+
this.collectionName = '';
|
|
416
|
+
this.checkSchema = false;
|
|
417
|
+
this.simplschema = null;
|
|
418
|
+
this.rbSchema = null;
|
|
419
|
+
this.timestamps = false;
|
|
420
|
+
this.useVersions = false;
|
|
421
|
+
this.versionCollection = '';
|
|
422
|
+
this.createLogs = true;
|
|
423
|
+
this.useRB = false;
|
|
424
|
+
this.collectionName = options.collectionName;
|
|
425
|
+
this.simplschema = new simpl_schema_1.default(options.schema);
|
|
426
|
+
this.timestamps = options.timestamps;
|
|
427
|
+
this.createLogs = options.createLogs;
|
|
428
|
+
this.checkSchema = options.checkSchema;
|
|
429
|
+
this.collectionOptions = options.collectionOptions;
|
|
430
|
+
if (options.useReportBuilder) {
|
|
431
|
+
this.useRB = true;
|
|
432
|
+
var schemaCopy = (0, common_1.deepCopy)(options.schema);
|
|
433
|
+
var rbSchema = (0, common_1.buildRbLookups)(options.reportBuilderLookupTables, schemaCopy, []);
|
|
434
|
+
this.rbSchema = (0, common_1.buildRbSchema)(rbSchema);
|
|
435
|
+
}
|
|
436
|
+
var interval = setInterval(function () {
|
|
437
|
+
if (index_1.ResolveIOServer && index_1.ResolveIOServer.getMainServer() && index_1.ResolveIOServer.getMongoManager() && index_1.ResolveIOServer.getMongoManager().getServerCollections().length) {
|
|
438
|
+
index_1.ResolveIOServer.getMongoManager().registerCollection(_this);
|
|
439
|
+
clearInterval(interval);
|
|
440
|
+
}
|
|
441
|
+
}, 1);
|
|
442
|
+
}
|
|
443
|
+
MongoManagerCollection.prototype.aggregate = function (pipeline, options, skipCache) {
|
|
444
|
+
var _this = this;
|
|
445
|
+
if (skipCache === void 0) { skipCache = false; }
|
|
446
|
+
return new Promise(function (resolve, reject) {
|
|
447
|
+
// if (!skipCache) {
|
|
448
|
+
// ResolveIOServer.getMongoManager().addToQueue({
|
|
449
|
+
// _id: 0,
|
|
450
|
+
// name_collection: this.collectionName,
|
|
451
|
+
// name_function: 'aggregate',
|
|
452
|
+
// name_function_addt: 'toArray',
|
|
453
|
+
// data_function: [pipeline, options],
|
|
454
|
+
// data_function_addt: [],
|
|
455
|
+
// cbs: [(err, res, cached) => {
|
|
456
|
+
// if (err) {
|
|
457
|
+
// reject(err);
|
|
458
|
+
// }
|
|
459
|
+
// else {
|
|
460
|
+
// resolve(res);
|
|
461
|
+
// }
|
|
462
|
+
// }],
|
|
463
|
+
// cbs_next: [],
|
|
464
|
+
// running: false,
|
|
465
|
+
// cacheId: 0,
|
|
466
|
+
// lookup_collections: pipeline.filter(a => a['$lookup'] && a['$lookup'].from).map(a => a['$lookup'].from).filter((elem, index, self) => {
|
|
467
|
+
// return index === self.indexOf(elem);
|
|
468
|
+
// }),
|
|
469
|
+
// invalidateFlag: false,
|
|
470
|
+
// invalidateCount: 0
|
|
471
|
+
// });
|
|
472
|
+
// }
|
|
473
|
+
// else {
|
|
474
|
+
// let monitor = new MonitorMongo('aggregate', this.collectionName, JSON.stringify([pipeline, options]));
|
|
475
|
+
// ResolveIOServer.getMainDB().collection<T>(this.collectionName, this.collectionOptions).aggregate(pipeline, options).toArray().then(res => {
|
|
476
|
+
// monitor.finish();
|
|
477
|
+
// resolve(res);
|
|
478
|
+
// }, err => {
|
|
479
|
+
// monitor.finish();
|
|
480
|
+
// console.log(new Date(), 'Error Aggregate', this.collectionName, pipeline, options, err);
|
|
481
|
+
// reject(err);
|
|
482
|
+
// });
|
|
483
|
+
// }
|
|
484
|
+
var monitor = new monitor_manager_1.MonitorMongo('aggregate', _this.collectionName, JSON.stringify([pipeline, options]));
|
|
485
|
+
index_1.ResolveIOServer.getMainDB().collection(_this.collectionName, _this.collectionOptions).aggregate(pipeline, options).toArray().then(function (res) {
|
|
486
|
+
monitor.finish();
|
|
487
|
+
resolve(res);
|
|
488
|
+
}, function (err) {
|
|
489
|
+
monitor.finish();
|
|
490
|
+
console.log(new Date(), 'Error Aggregate', _this.collectionName, pipeline, options, err);
|
|
491
|
+
reject(err);
|
|
492
|
+
});
|
|
493
|
+
});
|
|
494
|
+
};
|
|
495
|
+
MongoManagerCollection.prototype.aggregateCount = function (pipeline, options) {
|
|
496
|
+
var _this = this;
|
|
497
|
+
return new Promise(function (resolve, reject) {
|
|
498
|
+
index_1.ResolveIOServer.getMongoManager().addToQueue({
|
|
499
|
+
_id: 0,
|
|
500
|
+
name_collection: _this.collectionName,
|
|
501
|
+
name_function: 'aggregate',
|
|
502
|
+
name_function_addt: 'count',
|
|
503
|
+
data_function: [pipeline, options],
|
|
504
|
+
data_function_addt: [],
|
|
505
|
+
cbs: [function (err, res, cached) {
|
|
506
|
+
if (err) {
|
|
507
|
+
reject(err);
|
|
508
|
+
}
|
|
509
|
+
else {
|
|
510
|
+
resolve(res);
|
|
511
|
+
}
|
|
512
|
+
}],
|
|
513
|
+
cbs_next: [],
|
|
514
|
+
running: false,
|
|
515
|
+
cacheId: 0,
|
|
516
|
+
lookup_collections: pipeline.filter(function (a) { return a['$lookup'] && a['$lookup'].from; }).map(function (a) { return a['$lookup'].from; }).filter(function (elem, index, self) {
|
|
517
|
+
return index === self.indexOf(elem);
|
|
518
|
+
}),
|
|
519
|
+
invalidateFlag: false,
|
|
520
|
+
invalidateCount: 0
|
|
521
|
+
});
|
|
522
|
+
});
|
|
523
|
+
};
|
|
524
|
+
MongoManagerCollection.prototype.aggregateCursor = function (pipeline, options) {
|
|
525
|
+
var monitor = new monitor_manager_1.MonitorMongo('aggregateCursor', this.collectionName, JSON.stringify([pipeline, options]));
|
|
526
|
+
var cursor = index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).aggregate(pipeline, options);
|
|
527
|
+
cursor.on('close', function (ev) {
|
|
528
|
+
monitor.finish();
|
|
529
|
+
return ev;
|
|
530
|
+
});
|
|
531
|
+
return cursor;
|
|
532
|
+
};
|
|
533
|
+
MongoManagerCollection.prototype.aggregateStream = function (pipeline, options, streamOptions) {
|
|
534
|
+
var monitor = new monitor_manager_1.MonitorMongo('aggregateStream', this.collectionName, JSON.stringify([pipeline, options, streamOptions]));
|
|
535
|
+
var stream = index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).aggregate(pipeline, options).stream(streamOptions);
|
|
536
|
+
stream.on('end', function (ev) {
|
|
537
|
+
monitor.finish();
|
|
538
|
+
return ev;
|
|
539
|
+
});
|
|
540
|
+
return stream;
|
|
541
|
+
};
|
|
542
|
+
// bulkWrite(operations: AnyBulkWriteOperation<T>[], options: BulkWriteOptions): Promise<BulkWriteResult> {
|
|
543
|
+
MongoManagerCollection.prototype.bulkWrite = function (operations, options) {
|
|
544
|
+
var _this = this;
|
|
545
|
+
return new Promise(function (resolve, reject) {
|
|
546
|
+
var monitor = new monitor_manager_1.MonitorMongo('bulkWrite', _this.collectionName, JSON.stringify([operations.slice(-100), options]));
|
|
547
|
+
index_1.ResolveIOServer.getMainDB().collection(_this.collectionName, _this.collectionOptions).bulkWrite(operations, options).then(function (res) {
|
|
548
|
+
monitor.finish();
|
|
549
|
+
resolve(res);
|
|
550
|
+
}, function (err) {
|
|
551
|
+
monitor.finish();
|
|
552
|
+
console.log(new Date(), 'Error Bulk Write', _this.collectionName, operations.slice(-100), options, err);
|
|
553
|
+
reject(err);
|
|
554
|
+
});
|
|
555
|
+
});
|
|
556
|
+
};
|
|
557
|
+
MongoManagerCollection.prototype.countDocuments = function (filter, options) {
|
|
558
|
+
var _this = this;
|
|
559
|
+
if (filter === void 0) { filter = {}; }
|
|
560
|
+
return new Promise(function (resolve, reject) {
|
|
561
|
+
var monitor = new monitor_manager_1.MonitorMongo('countDocuments', _this.collectionName, JSON.stringify([filter, options]));
|
|
562
|
+
index_1.ResolveIOServer.getMainDB().collection(_this.collectionName, _this.collectionOptions).countDocuments(filter, options).then(function (res) {
|
|
563
|
+
monitor.finish();
|
|
564
|
+
resolve(res);
|
|
565
|
+
}, function (err) {
|
|
566
|
+
monitor.finish();
|
|
567
|
+
console.log(new Date(), 'Error Count Documents', _this.collectionName, filter, options, err);
|
|
568
|
+
reject(err);
|
|
569
|
+
});
|
|
570
|
+
});
|
|
571
|
+
};
|
|
572
|
+
//Helper Function (1 or Many) - Not Real Mongo Function
|
|
573
|
+
MongoManagerCollection.prototype.create = function (f_docs, options) {
|
|
574
|
+
if (!Array.isArray(f_docs)) {
|
|
575
|
+
return this.insertOne(f_docs, options);
|
|
576
|
+
}
|
|
577
|
+
else if (f_docs.length === 1) {
|
|
578
|
+
return this.insertOne(f_docs[0], options);
|
|
579
|
+
}
|
|
580
|
+
return this.insertMany(f_docs, options);
|
|
581
|
+
};
|
|
582
|
+
MongoManagerCollection.prototype.createIndex = function (fieldOrSpec, options) {
|
|
583
|
+
var _this = this;
|
|
584
|
+
return new Promise(function (resolve, reject) {
|
|
585
|
+
if (index_1.ResolveIOServer.getMainDB().collection(_this.collectionName, _this.collectionOptions)) {
|
|
586
|
+
var monitor_3 = new monitor_manager_1.MonitorMongo('createIndex', _this.collectionName, JSON.stringify([fieldOrSpec, options]));
|
|
587
|
+
index_1.ResolveIOServer.getMainDB().collection(_this.collectionName, _this.collectionOptions).createIndex(fieldOrSpec, options).then(function (res) {
|
|
588
|
+
monitor_3.finish();
|
|
589
|
+
resolve(res);
|
|
590
|
+
}, function (err) {
|
|
591
|
+
monitor_3.finish();
|
|
592
|
+
console.log(new Date(), 'Error Setting Up Index for Collection', _this.collectionName, fieldOrSpec, options, err);
|
|
593
|
+
reject(err);
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
});
|
|
597
|
+
};
|
|
598
|
+
MongoManagerCollection.prototype.createIndexes = function (indexSpecs, options) {
|
|
599
|
+
var _this = this;
|
|
600
|
+
return new Promise(function (resolve, reject) {
|
|
601
|
+
if (index_1.ResolveIOServer.getMainDB().collection(_this.collectionName, _this.collectionOptions)) {
|
|
602
|
+
var monitor_4 = new monitor_manager_1.MonitorMongo('createIndexes', _this.collectionName, JSON.stringify([indexSpecs, options]));
|
|
603
|
+
index_1.ResolveIOServer.getMainDB().collection(_this.collectionName, _this.collectionOptions).createIndexes(indexSpecs, options).then(function (res) {
|
|
604
|
+
monitor_4.finish();
|
|
605
|
+
resolve(res);
|
|
606
|
+
}, function (err) {
|
|
607
|
+
monitor_4.finish();
|
|
608
|
+
console.log(new Date(), 'Error Setting Up Indexes for Collection', _this.collectionName, indexSpecs, options, err);
|
|
609
|
+
reject(err);
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
});
|
|
613
|
+
};
|
|
614
|
+
MongoManagerCollection.prototype.deleteMany = function (filter, options, bypassLogs) {
|
|
615
|
+
var _this = this;
|
|
616
|
+
if (filter === void 0) { filter = {}; }
|
|
617
|
+
if (bypassLogs === void 0) { bypassLogs = false; }
|
|
618
|
+
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
619
|
+
var docs, i, doc, versionDoc, monitor;
|
|
620
|
+
var _this = this;
|
|
621
|
+
return __generator(this, function (_a) {
|
|
622
|
+
switch (_a.label) {
|
|
623
|
+
case 0:
|
|
624
|
+
if (!(this.createLogs && !bypassLogs)) return [3 /*break*/, 2];
|
|
625
|
+
return [4 /*yield*/, this.find(filter)];
|
|
626
|
+
case 1:
|
|
627
|
+
docs = _a.sent();
|
|
628
|
+
for (i = 0; i < docs.length; i++) {
|
|
629
|
+
doc = docs[i];
|
|
630
|
+
log_collection_1.Logs.insertOne({
|
|
631
|
+
_id: objectIdHexString(),
|
|
632
|
+
type: 'document',
|
|
633
|
+
collection: this.collectionName,
|
|
634
|
+
id_document: doc._id,
|
|
635
|
+
payload: (0, common_1.getBinarySize)(JSON.stringify([doc, filter, options])) < 200000 ? JSON.stringify([doc, filter, options], null, 2) : 'Too Big',
|
|
636
|
+
method: 'deleteMany',
|
|
637
|
+
id_user: '',
|
|
638
|
+
user: '',
|
|
639
|
+
messageId: 0,
|
|
640
|
+
route: ''
|
|
641
|
+
});
|
|
642
|
+
if (this.useVersions) {
|
|
643
|
+
versionDoc = (0, common_1.deepCopy)(doc);
|
|
644
|
+
versionDoc._id = { _id: doc._id, __v: doc.__v };
|
|
645
|
+
index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({ _id: { _id: doc._id, __v: doc.__v } }, versionDoc, { upsert: true });
|
|
646
|
+
index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({ $and: [{ '_id._id': doc._id }, { '_id.__v': { $lt: doc.__v - 1 } }] });
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
_a.label = 2;
|
|
650
|
+
case 2:
|
|
651
|
+
monitor = new monitor_manager_1.MonitorMongo('deleteMany', this.collectionName, JSON.stringify([filter, options]));
|
|
652
|
+
index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).deleteMany(filter, options).then(function (res) {
|
|
653
|
+
monitor.finish();
|
|
654
|
+
if (res.acknowledged) {
|
|
655
|
+
index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_this.collectionName);
|
|
656
|
+
resolve(res.deletedCount);
|
|
657
|
+
}
|
|
658
|
+
else {
|
|
659
|
+
reject(res.acknowledged);
|
|
660
|
+
}
|
|
661
|
+
}, function (err) {
|
|
662
|
+
monitor.finish();
|
|
663
|
+
console.log(new Date(), 'Error Delete Many', _this.collectionName, filter, options, err);
|
|
664
|
+
reject(err);
|
|
665
|
+
});
|
|
666
|
+
return [2 /*return*/];
|
|
667
|
+
}
|
|
668
|
+
});
|
|
669
|
+
}); });
|
|
670
|
+
};
|
|
671
|
+
MongoManagerCollection.prototype.deleteOne = function (filter, options, bypassLogs) {
|
|
672
|
+
var _this = this;
|
|
673
|
+
if (filter === void 0) { filter = {}; }
|
|
674
|
+
if (bypassLogs === void 0) { bypassLogs = false; }
|
|
675
|
+
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
676
|
+
var monitor_5, monitor_6;
|
|
677
|
+
var _this = this;
|
|
678
|
+
return __generator(this, function (_a) {
|
|
679
|
+
if (this.createLogs && !bypassLogs) {
|
|
680
|
+
monitor_5 = new monitor_manager_1.MonitorMongo('findOneAndDelete', this.collectionName, JSON.stringify([filter, options]));
|
|
681
|
+
index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).findOneAndDelete(filter, options).then(function (returnVal) { return __awaiter(_this, void 0, void 0, function () {
|
|
682
|
+
var doc, versionDoc;
|
|
683
|
+
return __generator(this, function (_a) {
|
|
684
|
+
monitor_5.finish();
|
|
685
|
+
doc = returnVal.value;
|
|
686
|
+
if (doc) {
|
|
687
|
+
if (this.createLogs && !bypassLogs) {
|
|
688
|
+
log_collection_1.Logs.insertOne({
|
|
689
|
+
_id: objectIdHexString(),
|
|
690
|
+
type: 'document',
|
|
691
|
+
collection: this.collectionName,
|
|
692
|
+
id_document: doc._id,
|
|
693
|
+
payload: (0, common_1.getBinarySize)(JSON.stringify([doc, filter, options])) < 200000 ? JSON.stringify([doc, filter, options], null, 2) : 'Too Big',
|
|
694
|
+
method: 'deleteOne',
|
|
695
|
+
id_user: '',
|
|
696
|
+
user: '',
|
|
697
|
+
messageId: 0,
|
|
698
|
+
route: ''
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
if (this.useVersions) {
|
|
702
|
+
versionDoc = (0, common_1.deepCopy)(doc);
|
|
703
|
+
versionDoc._id = { _id: doc._id, __v: doc.__v };
|
|
704
|
+
index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({ _id: { _id: doc._id, __v: doc.__v } }, versionDoc, { upsert: true });
|
|
705
|
+
index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({ $and: [{ '_id._id': doc._id }, { '_id.__v': { $lt: doc.__v - 1 } }] });
|
|
706
|
+
}
|
|
707
|
+
index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName);
|
|
708
|
+
resolve(1);
|
|
709
|
+
}
|
|
710
|
+
else {
|
|
711
|
+
resolve(0);
|
|
712
|
+
}
|
|
713
|
+
return [2 /*return*/];
|
|
714
|
+
});
|
|
715
|
+
}); }, function (err) {
|
|
716
|
+
monitor_5.finish();
|
|
717
|
+
console.log(new Date(), 'Error Find One And Delete', _this.collectionName, filter, options, err);
|
|
718
|
+
reject(err);
|
|
719
|
+
});
|
|
720
|
+
}
|
|
721
|
+
else {
|
|
722
|
+
monitor_6 = new monitor_manager_1.MonitorMongo('deleteOne', this.collectionName, JSON.stringify([filter, options]));
|
|
723
|
+
index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).deleteOne(filter, options).then(function (res) {
|
|
724
|
+
monitor_6.finish();
|
|
725
|
+
if (res.acknowledged) {
|
|
726
|
+
index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_this.collectionName);
|
|
727
|
+
resolve(res.deletedCount);
|
|
728
|
+
}
|
|
729
|
+
else {
|
|
730
|
+
reject(res.acknowledged);
|
|
731
|
+
}
|
|
732
|
+
}, function (err) {
|
|
733
|
+
monitor_6.finish();
|
|
734
|
+
console.log(new Date(), 'Error Delete One', _this.collectionName, filter, options, err);
|
|
735
|
+
reject(err);
|
|
736
|
+
});
|
|
737
|
+
}
|
|
738
|
+
return [2 /*return*/];
|
|
739
|
+
});
|
|
740
|
+
}); });
|
|
741
|
+
};
|
|
742
|
+
MongoManagerCollection.prototype.distinct = function (key, filter, options) {
|
|
743
|
+
var _this = this;
|
|
744
|
+
return new Promise(function (resolve, reject) {
|
|
745
|
+
var monitor = new monitor_manager_1.MonitorMongo('distinct', _this.collectionName, JSON.stringify([key, filter, options]));
|
|
746
|
+
index_1.ResolveIOServer.getMainDB().collection(_this.collectionName, _this.collectionOptions).distinct(key, filter, options).then(function (res) {
|
|
747
|
+
monitor.finish();
|
|
748
|
+
resolve(res);
|
|
749
|
+
}, function (err) {
|
|
750
|
+
monitor.finish();
|
|
751
|
+
console.log(new Date(), 'Error Distinct', _this.collectionName, filter, options, err);
|
|
752
|
+
reject(err);
|
|
753
|
+
});
|
|
754
|
+
});
|
|
755
|
+
};
|
|
756
|
+
MongoManagerCollection.prototype.drop = function (options) {
|
|
757
|
+
var _this = this;
|
|
758
|
+
return new Promise(function (resolve, reject) {
|
|
759
|
+
var monitor = new monitor_manager_1.MonitorMongo('drop', _this.collectionName, JSON.stringify([options]));
|
|
760
|
+
index_1.ResolveIOServer.getMainDB().collection(_this.collectionName, _this.collectionOptions).drop(options).then(function (res) {
|
|
761
|
+
monitor.finish();
|
|
762
|
+
resolve(res);
|
|
763
|
+
}, function (err) {
|
|
764
|
+
monitor.finish();
|
|
765
|
+
console.log(new Date(), 'Error Count Documents', _this.collectionName, options, err);
|
|
766
|
+
reject(err);
|
|
767
|
+
});
|
|
768
|
+
});
|
|
769
|
+
};
|
|
770
|
+
MongoManagerCollection.prototype.dropIndex = function (indexName, options) {
|
|
771
|
+
var _this = this;
|
|
772
|
+
return new Promise(function (resolve, reject) {
|
|
773
|
+
var monitor = new monitor_manager_1.MonitorMongo('dropIndex', _this.collectionName, JSON.stringify([indexName, options]));
|
|
774
|
+
index_1.ResolveIOServer.getMainDB().collection(_this.collectionName, _this.collectionOptions).dropIndex(indexName, options).then(function (res) {
|
|
775
|
+
monitor.finish();
|
|
776
|
+
resolve(res);
|
|
777
|
+
}, function (err) {
|
|
778
|
+
monitor.finish();
|
|
779
|
+
console.log(new Date(), 'Error Drop Index', _this.collectionName, indexName, options, err);
|
|
780
|
+
reject(err);
|
|
781
|
+
});
|
|
782
|
+
});
|
|
783
|
+
};
|
|
784
|
+
MongoManagerCollection.prototype.dropIndexes = function (options) {
|
|
785
|
+
var _this = this;
|
|
786
|
+
return new Promise(function (resolve, reject) {
|
|
787
|
+
var monitor = new monitor_manager_1.MonitorMongo('dropIndexes', _this.collectionName, JSON.stringify([options]));
|
|
788
|
+
index_1.ResolveIOServer.getMainDB().collection(_this.collectionName, _this.collectionOptions).dropIndexes(options).then(function (res) {
|
|
789
|
+
monitor.finish();
|
|
790
|
+
resolve(res);
|
|
791
|
+
}, function (err) {
|
|
792
|
+
monitor.finish();
|
|
793
|
+
console.log(new Date(), 'Error Drop Indexes', _this.collectionName, options, err);
|
|
794
|
+
reject(err);
|
|
795
|
+
});
|
|
796
|
+
});
|
|
797
|
+
};
|
|
798
|
+
MongoManagerCollection.prototype.find = function (filter, options, skipCache) {
|
|
799
|
+
var _this = this;
|
|
800
|
+
if (filter === void 0) { filter = {}; }
|
|
801
|
+
if (skipCache === void 0) { skipCache = false; }
|
|
802
|
+
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
803
|
+
var monitor_7;
|
|
804
|
+
var _this = this;
|
|
805
|
+
return __generator(this, function (_a) {
|
|
806
|
+
if (!skipCache) {
|
|
807
|
+
index_1.ResolveIOServer.getMongoManager().addToQueue({
|
|
808
|
+
_id: 0,
|
|
809
|
+
name_collection: this.collectionName,
|
|
810
|
+
name_function: 'find',
|
|
811
|
+
name_function_addt: 'toArray',
|
|
812
|
+
data_function: [filter, options],
|
|
813
|
+
data_function_addt: [],
|
|
814
|
+
cbs: [function (err, res, cached) {
|
|
815
|
+
if (err) {
|
|
816
|
+
reject(err);
|
|
817
|
+
}
|
|
818
|
+
else {
|
|
819
|
+
resolve(res);
|
|
820
|
+
}
|
|
821
|
+
}],
|
|
822
|
+
cbs_next: [],
|
|
823
|
+
running: false,
|
|
824
|
+
cacheId: 0,
|
|
825
|
+
lookup_collections: [],
|
|
826
|
+
invalidateFlag: false,
|
|
827
|
+
invalidateCount: 0
|
|
828
|
+
});
|
|
829
|
+
}
|
|
830
|
+
else {
|
|
831
|
+
monitor_7 = new monitor_manager_1.MonitorMongo('find', this.collectionName, JSON.stringify([filter, options]));
|
|
832
|
+
index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).find(filter, options).toArray().then(function (res) {
|
|
833
|
+
monitor_7.finish();
|
|
834
|
+
resolve(res);
|
|
835
|
+
}, function (err) {
|
|
836
|
+
monitor_7.finish();
|
|
837
|
+
console.log(new Date(), 'Error Find', _this.collectionName, filter, options, err);
|
|
838
|
+
reject(err);
|
|
839
|
+
});
|
|
840
|
+
}
|
|
841
|
+
return [2 /*return*/];
|
|
842
|
+
});
|
|
843
|
+
}); });
|
|
844
|
+
};
|
|
845
|
+
MongoManagerCollection.prototype.findById = function (id, options) {
|
|
846
|
+
var _this = this;
|
|
847
|
+
return new Promise(function (resolve, reject) {
|
|
848
|
+
var monitor = new monitor_manager_1.MonitorMongo('findById', _this.collectionName, JSON.stringify([{ _id: id }, options]));
|
|
849
|
+
index_1.ResolveIOServer.getMainDB().collection(_this.collectionName, _this.collectionOptions).findOne({ _id: id }, options).then(function (res) {
|
|
850
|
+
monitor.finish();
|
|
851
|
+
resolve(res);
|
|
852
|
+
}, function (err) {
|
|
853
|
+
monitor.finish();
|
|
854
|
+
console.log(new Date(), 'Error Find By Id', _this.collectionName, { _id: id }, options, err);
|
|
855
|
+
reject(err);
|
|
856
|
+
});
|
|
857
|
+
});
|
|
858
|
+
};
|
|
859
|
+
MongoManagerCollection.prototype.findCount = function (filter, options) {
|
|
860
|
+
var _this = this;
|
|
861
|
+
if (filter === void 0) { filter = {}; }
|
|
862
|
+
return new Promise(function (resolve, reject) {
|
|
863
|
+
var monitor = new monitor_manager_1.MonitorMongo('findCount', _this.collectionName, JSON.stringify([filter, options]));
|
|
864
|
+
index_1.ResolveIOServer.getMainDB().collection(_this.collectionName, _this.collectionOptions).find(filter, options).count().then(function (res) {
|
|
865
|
+
monitor.finish();
|
|
866
|
+
resolve(res);
|
|
867
|
+
}, function (err) {
|
|
868
|
+
monitor.finish();
|
|
869
|
+
console.log(new Date(), 'Error Find Count', _this.collectionName, filter, options, err);
|
|
870
|
+
reject(err);
|
|
871
|
+
});
|
|
872
|
+
});
|
|
873
|
+
};
|
|
874
|
+
MongoManagerCollection.prototype.findCursor = function (filter, options) {
|
|
875
|
+
if (filter === void 0) { filter = {}; }
|
|
876
|
+
var monitor = new monitor_manager_1.MonitorMongo('findCursor', this.collectionName, JSON.stringify([filter, options]));
|
|
877
|
+
var cursor = index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).find(filter, options);
|
|
878
|
+
cursor.on('close', function (ev) {
|
|
879
|
+
monitor.finish();
|
|
880
|
+
return ev;
|
|
881
|
+
});
|
|
882
|
+
return cursor;
|
|
883
|
+
};
|
|
884
|
+
MongoManagerCollection.prototype.findStream = function (filter, options, streamOptions) {
|
|
885
|
+
if (filter === void 0) { filter = {}; }
|
|
886
|
+
var monitor = new monitor_manager_1.MonitorMongo('findStream', this.collectionName, JSON.stringify([filter, options, streamOptions]));
|
|
887
|
+
var stream = index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).find(filter, options).stream(streamOptions);
|
|
888
|
+
stream.on('end', function (ev) {
|
|
889
|
+
monitor.finish();
|
|
890
|
+
return ev;
|
|
891
|
+
});
|
|
892
|
+
return stream;
|
|
893
|
+
};
|
|
894
|
+
MongoManagerCollection.prototype.findOne = function (filter, options, skipCache) {
|
|
895
|
+
var _this = this;
|
|
896
|
+
if (filter === void 0) { filter = {}; }
|
|
897
|
+
if (skipCache === void 0) { skipCache = false; }
|
|
898
|
+
return new Promise(function (resolve, reject) {
|
|
899
|
+
if (!skipCache) {
|
|
900
|
+
index_1.ResolveIOServer.getMongoManager().addToQueue({
|
|
901
|
+
_id: 0,
|
|
902
|
+
name_collection: _this.collectionName,
|
|
903
|
+
name_function: 'findOne',
|
|
904
|
+
name_function_addt: '',
|
|
905
|
+
data_function: [filter, options],
|
|
906
|
+
data_function_addt: [],
|
|
907
|
+
cbs: [function (err, res, cached) {
|
|
908
|
+
if (err) {
|
|
909
|
+
reject(err);
|
|
910
|
+
}
|
|
911
|
+
else {
|
|
912
|
+
resolve(res);
|
|
913
|
+
}
|
|
914
|
+
}],
|
|
915
|
+
cbs_next: [],
|
|
916
|
+
running: false,
|
|
917
|
+
cacheId: 0,
|
|
918
|
+
lookup_collections: [],
|
|
919
|
+
invalidateFlag: false,
|
|
920
|
+
invalidateCount: 0
|
|
921
|
+
});
|
|
922
|
+
}
|
|
923
|
+
else {
|
|
924
|
+
var monitor_8 = new monitor_manager_1.MonitorMongo('findOne', _this.collectionName, JSON.stringify([filter, options]));
|
|
925
|
+
index_1.ResolveIOServer.getMainDB().collection(_this.collectionName, _this.collectionOptions).findOne(filter, options).then(function (res) {
|
|
926
|
+
monitor_8.finish();
|
|
927
|
+
resolve(res);
|
|
928
|
+
}, function (err) {
|
|
929
|
+
monitor_8.finish();
|
|
930
|
+
console.log(new Date(), 'Error Find One', _this.collectionName, filter, options, err);
|
|
931
|
+
reject(err);
|
|
932
|
+
});
|
|
933
|
+
}
|
|
934
|
+
});
|
|
935
|
+
};
|
|
936
|
+
MongoManagerCollection.prototype.findOneAndDelete = function (filter, options, bypassLogs) {
|
|
937
|
+
var _this = this;
|
|
938
|
+
if (filter === void 0) { filter = {}; }
|
|
939
|
+
if (bypassLogs === void 0) { bypassLogs = false; }
|
|
940
|
+
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
941
|
+
var monitor;
|
|
942
|
+
var _this = this;
|
|
943
|
+
return __generator(this, function (_a) {
|
|
944
|
+
monitor = new monitor_manager_1.MonitorMongo('findOneAndDelete', this.collectionName, JSON.stringify([filter, options]));
|
|
945
|
+
index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).findOneAndDelete(filter, options).then(function (res) {
|
|
946
|
+
monitor.finish();
|
|
947
|
+
var doc = res.value;
|
|
948
|
+
if (doc) {
|
|
949
|
+
if (_this.createLogs && !bypassLogs) {
|
|
950
|
+
log_collection_1.Logs.insertOne({
|
|
951
|
+
_id: objectIdHexString(),
|
|
952
|
+
type: 'document',
|
|
953
|
+
collection: _this.collectionName,
|
|
954
|
+
id_document: doc._id,
|
|
955
|
+
payload: (0, common_1.getBinarySize)(JSON.stringify([doc, filter, options])) < 200000 ? JSON.stringify([doc, filter, options], null, 2) : 'Too Big',
|
|
956
|
+
method: 'findOneAndDelete',
|
|
957
|
+
id_user: '',
|
|
958
|
+
user: '',
|
|
959
|
+
messageId: 0,
|
|
960
|
+
route: ''
|
|
961
|
+
});
|
|
962
|
+
}
|
|
963
|
+
if (_this.useVersions) {
|
|
964
|
+
var versionDoc = (0, common_1.deepCopy)(doc);
|
|
965
|
+
versionDoc._id = { _id: doc._id, __v: doc.__v };
|
|
966
|
+
index_1.ResolveIOServer.getMongoManager().collection(_this.versionCollection).replaceOne({ _id: { _id: doc._id, __v: doc.__v } }, versionDoc, { upsert: true });
|
|
967
|
+
}
|
|
968
|
+
index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_this.collectionName);
|
|
969
|
+
resolve(doc);
|
|
970
|
+
}
|
|
971
|
+
else {
|
|
972
|
+
resolve(null);
|
|
973
|
+
}
|
|
974
|
+
}, function (err) {
|
|
975
|
+
monitor.finish();
|
|
976
|
+
console.log(new Date(), 'Error Find One And Delete', _this.collectionName, filter, options, err);
|
|
977
|
+
reject(err);
|
|
978
|
+
});
|
|
979
|
+
return [2 /*return*/];
|
|
980
|
+
});
|
|
981
|
+
}); });
|
|
982
|
+
};
|
|
983
|
+
MongoManagerCollection.prototype.findOneAndReplace = function (filter, replacement, options, bypassLogs, bypassCheckSchema) {
|
|
984
|
+
var _this = this;
|
|
985
|
+
if (filter === void 0) { filter = {}; }
|
|
986
|
+
if (bypassLogs === void 0) { bypassLogs = false; }
|
|
987
|
+
if (bypassCheckSchema === void 0) { bypassCheckSchema = false; }
|
|
988
|
+
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
989
|
+
var validation, isValid, date, monitor;
|
|
990
|
+
var _this = this;
|
|
991
|
+
return __generator(this, function (_a) {
|
|
992
|
+
if (this.checkSchema && !bypassCheckSchema) {
|
|
993
|
+
validation = this.simplschema.newContext();
|
|
994
|
+
isValid = validation.validate(replacement);
|
|
995
|
+
if (!isValid) {
|
|
996
|
+
console.log(new Date(), this.collectionName, 'Schema Errors - findOneAndReplace', validation.validationErrors());
|
|
997
|
+
index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(), 'insertErrorLog', 'Schema Failed on findOneAndReplace - ' + this.collectionName, [validation.validationErrors(), replacement]);
|
|
998
|
+
reject(validation.validationErrors());
|
|
999
|
+
return [2 /*return*/];
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
date = new Date();
|
|
1003
|
+
if (this.timestamps) {
|
|
1004
|
+
replacement.updatedAt = date;
|
|
1005
|
+
}
|
|
1006
|
+
if (options && options.upsert) {
|
|
1007
|
+
if (!replacement._id) {
|
|
1008
|
+
replacement._id = objectIdHexString();
|
|
1009
|
+
}
|
|
1010
|
+
if (this.useVersions) {
|
|
1011
|
+
replacement.__v = 0;
|
|
1012
|
+
}
|
|
1013
|
+
if (this.timestamps && !replacement.createdAt) {
|
|
1014
|
+
replacement.createdAt = date;
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
monitor = new monitor_manager_1.MonitorMongo('findOneAndReplace', this.collectionName, JSON.stringify([filter, replacement, options]));
|
|
1018
|
+
index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).findOneAndReplace(filter, replacement, options).then(function (res) {
|
|
1019
|
+
monitor.finish();
|
|
1020
|
+
var doc = res.value;
|
|
1021
|
+
if (doc) {
|
|
1022
|
+
if (_this.createLogs && !bypassLogs) {
|
|
1023
|
+
log_collection_1.Logs.insertOne({
|
|
1024
|
+
_id: objectIdHexString(),
|
|
1025
|
+
type: 'document',
|
|
1026
|
+
collection: _this.collectionName,
|
|
1027
|
+
id_document: doc._id,
|
|
1028
|
+
payload: (0, common_1.getBinarySize)(JSON.stringify([doc, filter, replacement, options])) < 200000 ? JSON.stringify([doc, filter, replacement, options], null, 2) : 'Too Big',
|
|
1029
|
+
method: 'findOneAndReplace',
|
|
1030
|
+
id_user: '',
|
|
1031
|
+
user: '',
|
|
1032
|
+
messageId: 0,
|
|
1033
|
+
route: ''
|
|
1034
|
+
});
|
|
1035
|
+
}
|
|
1036
|
+
if (_this.useVersions) {
|
|
1037
|
+
var versionDoc = (0, common_1.deepCopy)(doc);
|
|
1038
|
+
versionDoc._id = { _id: doc._id, __v: doc.__v };
|
|
1039
|
+
index_1.ResolveIOServer.getMongoManager().collection(_this.versionCollection).replaceOne({ _id: { _id: doc._id, __v: doc.__v } }, versionDoc, { upsert: true });
|
|
1040
|
+
}
|
|
1041
|
+
index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_this.collectionName);
|
|
1042
|
+
resolve(doc);
|
|
1043
|
+
}
|
|
1044
|
+
else {
|
|
1045
|
+
resolve(null);
|
|
1046
|
+
}
|
|
1047
|
+
}, function (err) {
|
|
1048
|
+
monitor.finish();
|
|
1049
|
+
console.log(new Date(), 'Error Find One And Replace', _this.collectionName, filter, options, err);
|
|
1050
|
+
reject(err);
|
|
1051
|
+
});
|
|
1052
|
+
return [2 /*return*/];
|
|
1053
|
+
});
|
|
1054
|
+
}); });
|
|
1055
|
+
};
|
|
1056
|
+
MongoManagerCollection.prototype.findOneAndUpdate = function (filter, update, options, bypassLogs, bypassCheckSchema) {
|
|
1057
|
+
var _this = this;
|
|
1058
|
+
if (filter === void 0) { filter = {}; }
|
|
1059
|
+
if (bypassLogs === void 0) { bypassLogs = false; }
|
|
1060
|
+
if (bypassCheckSchema === void 0) { bypassCheckSchema = false; }
|
|
1061
|
+
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
1062
|
+
var validation, isValid, date, monitor;
|
|
1063
|
+
var _this = this;
|
|
1064
|
+
return __generator(this, function (_a) {
|
|
1065
|
+
if (this.checkSchema && !bypassCheckSchema) {
|
|
1066
|
+
validation = this.simplschema.newContext();
|
|
1067
|
+
isValid = validation.validate(update, { modifier: true });
|
|
1068
|
+
if (!isValid) {
|
|
1069
|
+
console.log(new Date(), this.collectionName, 'Schema Errors - findOneAndUpdate', validation.validationErrors());
|
|
1070
|
+
index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(), 'insertErrorLog', 'Schema Failed on findOneAndUpdate - ' + this.collectionName, [validation.validationErrors(), update]);
|
|
1071
|
+
reject(validation.validationErrors());
|
|
1072
|
+
return [2 /*return*/];
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
if (this.timestamps) {
|
|
1076
|
+
date = new Date();
|
|
1077
|
+
if (!update.$set) {
|
|
1078
|
+
update.$set = { updatedAt: date };
|
|
1079
|
+
}
|
|
1080
|
+
else {
|
|
1081
|
+
update.$set.updatedAt = date;
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
if (options && options.upsert) {
|
|
1085
|
+
if (!update.$setOnInsert) {
|
|
1086
|
+
if (this.timestamps) {
|
|
1087
|
+
update.$setOnInsert = {
|
|
1088
|
+
_id: objectIdHexString(),
|
|
1089
|
+
createdAt: new Date()
|
|
1090
|
+
};
|
|
1091
|
+
}
|
|
1092
|
+
else {
|
|
1093
|
+
update.$setOnInsert = {
|
|
1094
|
+
_id: objectIdHexString()
|
|
1095
|
+
};
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
else {
|
|
1099
|
+
if (!update.$setOnInsert._id) {
|
|
1100
|
+
update.$setOnInsert._id = objectIdHexString();
|
|
1101
|
+
}
|
|
1102
|
+
if (this.timestamps) {
|
|
1103
|
+
update.$setOnInsert.createdAt = new Date();
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
monitor = new monitor_manager_1.MonitorMongo('findOneAndUpdate', this.collectionName, JSON.stringify([filter, update, options]));
|
|
1108
|
+
index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).findOneAndUpdate(filter, update, options).then(function (res) {
|
|
1109
|
+
monitor.finish();
|
|
1110
|
+
var doc = res.value;
|
|
1111
|
+
if (doc) {
|
|
1112
|
+
if (_this.createLogs && !bypassLogs) {
|
|
1113
|
+
log_collection_1.Logs.insertOne({
|
|
1114
|
+
_id: objectIdHexString(),
|
|
1115
|
+
type: 'document',
|
|
1116
|
+
collection: _this.collectionName,
|
|
1117
|
+
id_document: doc._id,
|
|
1118
|
+
payload: (0, common_1.getBinarySize)(JSON.stringify([doc, filter, update, options])) < 200000 ? JSON.stringify([doc, filter, update, options], null, 2) : 'Too Big',
|
|
1119
|
+
method: 'findOneAndUpdate',
|
|
1120
|
+
id_user: '',
|
|
1121
|
+
user: '',
|
|
1122
|
+
messageId: 0,
|
|
1123
|
+
route: ''
|
|
1124
|
+
});
|
|
1125
|
+
}
|
|
1126
|
+
if (_this.useVersions) {
|
|
1127
|
+
var versionDoc = (0, common_1.deepCopy)(doc);
|
|
1128
|
+
versionDoc._id = { _id: doc._id, __v: doc.__v };
|
|
1129
|
+
index_1.ResolveIOServer.getMongoManager().collection(_this.versionCollection).replaceOne({ _id: { _id: doc._id, __v: doc.__v } }, versionDoc, { upsert: true });
|
|
1130
|
+
}
|
|
1131
|
+
index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_this.collectionName);
|
|
1132
|
+
resolve(doc);
|
|
1133
|
+
}
|
|
1134
|
+
else {
|
|
1135
|
+
resolve(null);
|
|
1136
|
+
}
|
|
1137
|
+
}, function (err) {
|
|
1138
|
+
monitor.finish();
|
|
1139
|
+
console.log(new Date(), 'Error Find One And Update', _this.collectionName, filter, options, err);
|
|
1140
|
+
reject(err);
|
|
1141
|
+
});
|
|
1142
|
+
return [2 /*return*/];
|
|
1143
|
+
});
|
|
1144
|
+
}); });
|
|
1145
|
+
};
|
|
1146
|
+
MongoManagerCollection.prototype.indexes = function (options) {
|
|
1147
|
+
var _this = this;
|
|
1148
|
+
return new Promise(function (resolve, reject) {
|
|
1149
|
+
var monitor = new monitor_manager_1.MonitorMongo('indexes', _this.collectionName, JSON.stringify([options]));
|
|
1150
|
+
index_1.ResolveIOServer.getMainDB().collection(_this.collectionName, _this.collectionOptions).indexes(options).then(function (res) {
|
|
1151
|
+
monitor.finish();
|
|
1152
|
+
resolve(res);
|
|
1153
|
+
}, function (err) {
|
|
1154
|
+
monitor.finish();
|
|
1155
|
+
console.log(new Date(), 'Error Indexes', _this.collectionName, options, err);
|
|
1156
|
+
reject(err);
|
|
1157
|
+
});
|
|
1158
|
+
});
|
|
1159
|
+
};
|
|
1160
|
+
MongoManagerCollection.prototype.indexExists = function (indexes, options) {
|
|
1161
|
+
var _this = this;
|
|
1162
|
+
return new Promise(function (resolve, reject) {
|
|
1163
|
+
var monitor = new monitor_manager_1.MonitorMongo('indexExists', _this.collectionName, JSON.stringify([indexes, options]));
|
|
1164
|
+
index_1.ResolveIOServer.getMainDB().collection(_this.collectionName, _this.collectionOptions).indexExists(indexes, options).then(function (res) {
|
|
1165
|
+
monitor.finish();
|
|
1166
|
+
resolve(res);
|
|
1167
|
+
}, function (err) {
|
|
1168
|
+
monitor.finish();
|
|
1169
|
+
console.log(new Date(), 'Error Index Exists', _this.collectionName, indexes, options, err);
|
|
1170
|
+
reject(err);
|
|
1171
|
+
});
|
|
1172
|
+
});
|
|
1173
|
+
};
|
|
1174
|
+
MongoManagerCollection.prototype.insertMany = function (docs, options, bypassLogs, bypassCheckSchema, bypassMonitor) {
|
|
1175
|
+
var _this = this;
|
|
1176
|
+
if (bypassLogs === void 0) { bypassLogs = false; }
|
|
1177
|
+
if (bypassCheckSchema === void 0) { bypassCheckSchema = false; }
|
|
1178
|
+
if (bypassMonitor === void 0) { bypassMonitor = false; }
|
|
1179
|
+
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
1180
|
+
var validationResults, validDocs, i, doc, date, monitor_9;
|
|
1181
|
+
var _this = this;
|
|
1182
|
+
return __generator(this, function (_a) {
|
|
1183
|
+
if (!docs.length) {
|
|
1184
|
+
resolve([]);
|
|
1185
|
+
return [2 /*return*/];
|
|
1186
|
+
}
|
|
1187
|
+
validationResults = [];
|
|
1188
|
+
if (this.checkSchema && !bypassCheckSchema) {
|
|
1189
|
+
docs.forEach(function (doc) {
|
|
1190
|
+
var validation = _this.simplschema.newContext();
|
|
1191
|
+
var isValid = validation.validate(doc);
|
|
1192
|
+
if (!isValid) {
|
|
1193
|
+
console.log(new Date(), _this.collectionName, 'Schema Errors - insertMany', validation.validationErrors());
|
|
1194
|
+
index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(), 'insertErrorLog', 'Schema Failed on insertMany - ' + _this.collectionName, [validation.validationErrors(), doc]);
|
|
1195
|
+
validationResults.push(false);
|
|
1196
|
+
}
|
|
1197
|
+
else {
|
|
1198
|
+
validationResults.push(true);
|
|
1199
|
+
}
|
|
1200
|
+
});
|
|
1201
|
+
}
|
|
1202
|
+
validDocs = this.checkSchema ? docs.filter(function (a, idx) { return validationResults[idx]; }) : docs;
|
|
1203
|
+
for (i = 0; i < validDocs.length; i++) {
|
|
1204
|
+
doc = validDocs[i];
|
|
1205
|
+
if (!doc._id) {
|
|
1206
|
+
doc._id = objectIdHexString();
|
|
1207
|
+
}
|
|
1208
|
+
if (this.timestamps) {
|
|
1209
|
+
date = new Date();
|
|
1210
|
+
doc.createdAt = date;
|
|
1211
|
+
doc.updatedAt = date;
|
|
1212
|
+
}
|
|
1213
|
+
if (this.createLogs && !bypassLogs) {
|
|
1214
|
+
log_collection_1.Logs.insertOne({
|
|
1215
|
+
_id: objectIdHexString(),
|
|
1216
|
+
type: 'document',
|
|
1217
|
+
collection: this.collectionName,
|
|
1218
|
+
id_document: doc._id,
|
|
1219
|
+
payload: (0, common_1.getBinarySize)(JSON.stringify([doc, options])) < 200000 ? JSON.stringify([doc, options], null, 2) : 'Too Big',
|
|
1220
|
+
method: 'insertMany',
|
|
1221
|
+
id_user: '',
|
|
1222
|
+
user: '',
|
|
1223
|
+
messageId: 0,
|
|
1224
|
+
route: ''
|
|
1225
|
+
});
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
if (validDocs.length) {
|
|
1229
|
+
monitor_9 = null;
|
|
1230
|
+
if (!bypassMonitor) {
|
|
1231
|
+
monitor_9 = new monitor_manager_1.MonitorMongo('insertMany', this.collectionName, JSON.stringify([validDocs, options]));
|
|
1232
|
+
}
|
|
1233
|
+
index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).insertMany(validDocs, options).then(function (res) {
|
|
1234
|
+
if (monitor_9) {
|
|
1235
|
+
monitor_9.finish();
|
|
1236
|
+
}
|
|
1237
|
+
if (res.acknowledged) {
|
|
1238
|
+
index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_this.collectionName);
|
|
1239
|
+
resolve(validDocs);
|
|
1240
|
+
}
|
|
1241
|
+
else {
|
|
1242
|
+
reject(res.acknowledged);
|
|
1243
|
+
}
|
|
1244
|
+
}, function (err) {
|
|
1245
|
+
if (monitor_9) {
|
|
1246
|
+
monitor_9.finish();
|
|
1247
|
+
}
|
|
1248
|
+
console.log(new Date(), 'Error Insert Many', _this.collectionName, validDocs, options, err);
|
|
1249
|
+
reject(err);
|
|
1250
|
+
});
|
|
1251
|
+
}
|
|
1252
|
+
return [2 /*return*/];
|
|
1253
|
+
});
|
|
1254
|
+
}); });
|
|
1255
|
+
};
|
|
1256
|
+
MongoManagerCollection.prototype.insertOne = function (doc, options, bypassLogs, bypassCheckSchema) {
|
|
1257
|
+
var _this = this;
|
|
1258
|
+
if (bypassLogs === void 0) { bypassLogs = false; }
|
|
1259
|
+
if (bypassCheckSchema === void 0) { bypassCheckSchema = false; }
|
|
1260
|
+
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
1261
|
+
var validation, isValid, date, monitor;
|
|
1262
|
+
var _this = this;
|
|
1263
|
+
return __generator(this, function (_a) {
|
|
1264
|
+
if (!doc._id) {
|
|
1265
|
+
doc._id = objectIdHexString();
|
|
1266
|
+
}
|
|
1267
|
+
if (this.checkSchema && !bypassCheckSchema) {
|
|
1268
|
+
validation = this.simplschema.newContext();
|
|
1269
|
+
isValid = validation.validate(doc);
|
|
1270
|
+
if (!isValid) {
|
|
1271
|
+
console.log(new Date(), this.collectionName, 'Schema Errors - insertOne', validation.validationErrors());
|
|
1272
|
+
index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(), 'insertErrorLog', 'Schema Failed on insertOne - ' + this.collectionName, [validation.validationErrors(), doc]);
|
|
1273
|
+
reject(validation.validationErrors());
|
|
1274
|
+
return [2 /*return*/];
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
if (this.timestamps) {
|
|
1278
|
+
date = new Date();
|
|
1279
|
+
doc.createdAt = date;
|
|
1280
|
+
doc.updatedAt = date;
|
|
1281
|
+
}
|
|
1282
|
+
if (this.createLogs && !bypassLogs) {
|
|
1283
|
+
log_collection_1.Logs.insertOne({
|
|
1284
|
+
_id: objectIdHexString(),
|
|
1285
|
+
type: 'document',
|
|
1286
|
+
collection: this.collectionName,
|
|
1287
|
+
id_document: doc._id,
|
|
1288
|
+
payload: (0, common_1.getBinarySize)(JSON.stringify([doc, options])) < 200000 ? JSON.stringify([doc, options], null, 2) : 'Too Big',
|
|
1289
|
+
method: 'insertOne',
|
|
1290
|
+
id_user: '',
|
|
1291
|
+
user: '',
|
|
1292
|
+
messageId: 0,
|
|
1293
|
+
route: ''
|
|
1294
|
+
});
|
|
1295
|
+
}
|
|
1296
|
+
if (this.useVersions && !doc.hasOwnProperty('__v')) {
|
|
1297
|
+
doc.__v = 0;
|
|
1298
|
+
}
|
|
1299
|
+
monitor = new monitor_manager_1.MonitorMongo('insertOne', this.collectionName, JSON.stringify([doc, options]));
|
|
1300
|
+
index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).insertOne(doc, options).then(function (res) {
|
|
1301
|
+
monitor.finish();
|
|
1302
|
+
if (res.acknowledged) {
|
|
1303
|
+
index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_this.collectionName);
|
|
1304
|
+
resolve(doc);
|
|
1305
|
+
}
|
|
1306
|
+
else {
|
|
1307
|
+
reject(res.acknowledged);
|
|
1308
|
+
}
|
|
1309
|
+
}, function (err) {
|
|
1310
|
+
monitor.finish();
|
|
1311
|
+
console.log(new Date(), 'Error Insert One', _this.collectionName, doc, options, err);
|
|
1312
|
+
reject(err);
|
|
1313
|
+
});
|
|
1314
|
+
return [2 /*return*/];
|
|
1315
|
+
});
|
|
1316
|
+
}); });
|
|
1317
|
+
};
|
|
1318
|
+
MongoManagerCollection.prototype.listIndexes = function (options) {
|
|
1319
|
+
var monitor = new monitor_manager_1.MonitorMongo('listIndexes', this.collectionName, JSON.stringify([options]));
|
|
1320
|
+
var cursor = index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).listIndexes(options);
|
|
1321
|
+
cursor.on('close', function (ev) {
|
|
1322
|
+
monitor.finish();
|
|
1323
|
+
return ev;
|
|
1324
|
+
});
|
|
1325
|
+
return cursor;
|
|
1326
|
+
};
|
|
1327
|
+
MongoManagerCollection.prototype.rename = function (newName, options) {
|
|
1328
|
+
var _this = this;
|
|
1329
|
+
return new Promise(function (resolve, reject) {
|
|
1330
|
+
var monitor = new monitor_manager_1.MonitorMongo('rename', _this.collectionName, JSON.stringify([newName, options]));
|
|
1331
|
+
index_1.ResolveIOServer.getMainDB().collection(_this.collectionName, _this.collectionOptions).rename(newName, options).then(function (res) {
|
|
1332
|
+
monitor.finish();
|
|
1333
|
+
resolve(res);
|
|
1334
|
+
}, function (err) {
|
|
1335
|
+
monitor.finish();
|
|
1336
|
+
console.log(new Date(), 'Error Rename', _this.collectionName, newName, options, err);
|
|
1337
|
+
reject(err);
|
|
1338
|
+
});
|
|
1339
|
+
});
|
|
1340
|
+
};
|
|
1341
|
+
MongoManagerCollection.prototype.replaceOne = function (filter, replacement, options, bypassLogs, bypassCheckSchema, doc) {
|
|
1342
|
+
var _this = this;
|
|
1343
|
+
if (bypassLogs === void 0) { bypassLogs = false; }
|
|
1344
|
+
if (bypassCheckSchema === void 0) { bypassCheckSchema = false; }
|
|
1345
|
+
if (doc === void 0) { doc = null; }
|
|
1346
|
+
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
1347
|
+
var validation, isValid, date, versionDoc, monitor_10, prevDoc, docId, docVersion, updatedDoc_1, monitor_11, monitor_12, monitor_13, monitor_14;
|
|
1348
|
+
var _this = this;
|
|
1349
|
+
return __generator(this, function (_a) {
|
|
1350
|
+
switch (_a.label) {
|
|
1351
|
+
case 0:
|
|
1352
|
+
if (this.checkSchema && !bypassCheckSchema) {
|
|
1353
|
+
validation = this.simplschema.newContext();
|
|
1354
|
+
isValid = validation.validate(replacement);
|
|
1355
|
+
if (!isValid) {
|
|
1356
|
+
console.log(new Date(), this.collectionName, 'Schema Errors - replaceOne', validation.validationErrors());
|
|
1357
|
+
index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(), 'insertErrorLog', 'Schema Failed on replaceOne - ' + this.collectionName, [validation.validationErrors(), replacement]);
|
|
1358
|
+
reject(validation.validationErrors());
|
|
1359
|
+
return [2 /*return*/];
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
date = new Date();
|
|
1363
|
+
if (this.timestamps) {
|
|
1364
|
+
replacement.updatedAt = date;
|
|
1365
|
+
}
|
|
1366
|
+
if (!this.useVersions) return [3 /*break*/, 8];
|
|
1367
|
+
if (!!doc) return [3 /*break*/, 2];
|
|
1368
|
+
return [4 /*yield*/, this.findOne(filter, null, true)];
|
|
1369
|
+
case 1:
|
|
1370
|
+
doc = _a.sent();
|
|
1371
|
+
_a.label = 2;
|
|
1372
|
+
case 2:
|
|
1373
|
+
if (!doc) return [3 /*break*/, 6];
|
|
1374
|
+
if (this.timestamps && !replacement.createdAt && doc.createdAt) {
|
|
1375
|
+
replacement.createdAt = doc.createdAt;
|
|
1376
|
+
}
|
|
1377
|
+
if (!(doc.__v === replacement.__v)) return [3 /*break*/, 3];
|
|
1378
|
+
replacement.__v += 1;
|
|
1379
|
+
versionDoc = (0, common_1.deepCopy)(doc);
|
|
1380
|
+
versionDoc._id = { _id: doc._id, __v: doc.__v };
|
|
1381
|
+
index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({ _id: { _id: doc._id, __v: doc.__v } }, versionDoc, { upsert: true });
|
|
1382
|
+
if (doc.__v >= 4) {
|
|
1383
|
+
index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({ $and: [{ '_id._id': doc._id }, { '_id.__v': { $lt: doc.__v - 4 } }] });
|
|
1384
|
+
}
|
|
1385
|
+
if (this.createLogs && !bypassLogs) {
|
|
1386
|
+
log_collection_1.Logs.insertOne({
|
|
1387
|
+
_id: objectIdHexString(),
|
|
1388
|
+
type: 'document',
|
|
1389
|
+
collection: this.collectionName,
|
|
1390
|
+
id_document: doc._id,
|
|
1391
|
+
payload: (0, common_1.getBinarySize)(JSON.stringify([doc, filter, replacement, options])) < 200000 ? JSON.stringify([doc, filter, replacement, options], null, 2) : 'Too Big',
|
|
1392
|
+
method: 'replaceOne',
|
|
1393
|
+
id_user: '',
|
|
1394
|
+
user: '',
|
|
1395
|
+
messageId: 0,
|
|
1396
|
+
route: ''
|
|
1397
|
+
});
|
|
1398
|
+
}
|
|
1399
|
+
monitor_10 = new monitor_manager_1.MonitorMongo('replaceOne', this.collectionName, JSON.stringify([filter, replacement, options]));
|
|
1400
|
+
index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).replaceOne(filter, replacement, options).then(function (res) {
|
|
1401
|
+
monitor_10.finish();
|
|
1402
|
+
if (res.acknowledged) {
|
|
1403
|
+
index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_this.collectionName);
|
|
1404
|
+
resolve(res.modifiedCount);
|
|
1405
|
+
}
|
|
1406
|
+
else {
|
|
1407
|
+
reject(res.acknowledged);
|
|
1408
|
+
}
|
|
1409
|
+
}, function (err) {
|
|
1410
|
+
monitor_10.finish();
|
|
1411
|
+
console.log(new Date(), 'Error Replace One', _this.collectionName, filter, replacement, options, err);
|
|
1412
|
+
reject(err);
|
|
1413
|
+
});
|
|
1414
|
+
return [3 /*break*/, 5];
|
|
1415
|
+
case 3:
|
|
1416
|
+
console.log('invalid version - ' + this.collectionName, doc.__v, replacement.__v);
|
|
1417
|
+
return [4 /*yield*/, index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).findOne({
|
|
1418
|
+
_id: {
|
|
1419
|
+
_id: doc._id,
|
|
1420
|
+
__v: replacement.__v
|
|
1421
|
+
}
|
|
1422
|
+
}, null, true)];
|
|
1423
|
+
case 4:
|
|
1424
|
+
prevDoc = _a.sent();
|
|
1425
|
+
if (prevDoc) {
|
|
1426
|
+
docId = doc._id;
|
|
1427
|
+
docVersion = doc.__v;
|
|
1428
|
+
updatedDoc_1 = (0, common_1.getMongoMergeUpdatedDoc)(replacement, doc, prevDoc);
|
|
1429
|
+
updatedDoc_1._id = docId;
|
|
1430
|
+
updatedDoc_1.__v = docVersion + 1;
|
|
1431
|
+
if (this.createLogs && !bypassLogs) {
|
|
1432
|
+
log_collection_1.Logs.insertOne({
|
|
1433
|
+
_id: objectIdHexString(),
|
|
1434
|
+
type: 'document',
|
|
1435
|
+
collection: this.collectionName,
|
|
1436
|
+
id_document: docId,
|
|
1437
|
+
payload: (0, common_1.getBinarySize)(JSON.stringify(['invalidVersion - merge', doc, filter, updatedDoc_1, options])) < 200000 ? JSON.stringify(['invalidVersion - merge', doc, filter, updatedDoc_1, options], null, 2) : 'Too Big',
|
|
1438
|
+
method: 'replaceOne',
|
|
1439
|
+
id_user: '',
|
|
1440
|
+
user: '',
|
|
1441
|
+
messageId: 0,
|
|
1442
|
+
route: ''
|
|
1443
|
+
});
|
|
1444
|
+
}
|
|
1445
|
+
monitor_11 = new monitor_manager_1.MonitorMongo('replaceOne', this.collectionName, JSON.stringify([filter, updatedDoc_1, options]));
|
|
1446
|
+
index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).replaceOne(filter, updatedDoc_1, options).then(function (res) {
|
|
1447
|
+
monitor_11.finish();
|
|
1448
|
+
if (res.acknowledged) {
|
|
1449
|
+
index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_this.collectionName);
|
|
1450
|
+
resolve(res.modifiedCount);
|
|
1451
|
+
}
|
|
1452
|
+
else {
|
|
1453
|
+
reject(res.acknowledged);
|
|
1454
|
+
}
|
|
1455
|
+
}, function (err) {
|
|
1456
|
+
monitor_11.finish();
|
|
1457
|
+
console.log(new Date(), 'Error Replace One', _this.collectionName, filter, updatedDoc_1, options, err);
|
|
1458
|
+
reject(err);
|
|
1459
|
+
});
|
|
1460
|
+
}
|
|
1461
|
+
else {
|
|
1462
|
+
reject('Invalid Version And Could Not Find History - DB: ' + doc.__v + ', Trying to update with :' + replacement.__v);
|
|
1463
|
+
}
|
|
1464
|
+
_a.label = 5;
|
|
1465
|
+
case 5: return [3 /*break*/, 7];
|
|
1466
|
+
case 6:
|
|
1467
|
+
if (options && options.upsert) {
|
|
1468
|
+
if (this.timestamps) {
|
|
1469
|
+
replacement.createdAt = date;
|
|
1470
|
+
}
|
|
1471
|
+
if (!replacement._id) {
|
|
1472
|
+
replacement._id = objectIdHexString();
|
|
1473
|
+
}
|
|
1474
|
+
replacement.__v = 0;
|
|
1475
|
+
if (this.createLogs && !bypassLogs) {
|
|
1476
|
+
log_collection_1.Logs.insertOne({
|
|
1477
|
+
_id: objectIdHexString(),
|
|
1478
|
+
type: 'document',
|
|
1479
|
+
collection: this.collectionName,
|
|
1480
|
+
id_document: replacement._id,
|
|
1481
|
+
payload: (0, common_1.getBinarySize)(JSON.stringify(['upsert', filter, replacement, options])) < 200000 ? JSON.stringify(['upsert', filter, replacement, options], null, 2) : 'Too Big',
|
|
1482
|
+
method: 'replaceOne',
|
|
1483
|
+
id_user: '',
|
|
1484
|
+
user: '',
|
|
1485
|
+
messageId: 0,
|
|
1486
|
+
route: ''
|
|
1487
|
+
});
|
|
1488
|
+
}
|
|
1489
|
+
monitor_12 = new monitor_manager_1.MonitorMongo('replaceOne', this.collectionName, JSON.stringify([filter, replacement, options]));
|
|
1490
|
+
index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).replaceOne(filter, replacement, options).then(function (res) {
|
|
1491
|
+
monitor_12.finish();
|
|
1492
|
+
if (res.acknowledged) {
|
|
1493
|
+
index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_this.collectionName);
|
|
1494
|
+
resolve(res.modifiedCount);
|
|
1495
|
+
}
|
|
1496
|
+
else {
|
|
1497
|
+
reject(res.acknowledged);
|
|
1498
|
+
}
|
|
1499
|
+
}, function (err) {
|
|
1500
|
+
monitor_12.finish();
|
|
1501
|
+
console.log(new Date(), 'Error Replace One', _this.collectionName, filter, replacement, options, err);
|
|
1502
|
+
reject(err);
|
|
1503
|
+
});
|
|
1504
|
+
}
|
|
1505
|
+
else {
|
|
1506
|
+
reject('No Document');
|
|
1507
|
+
}
|
|
1508
|
+
_a.label = 7;
|
|
1509
|
+
case 7: return [3 /*break*/, 9];
|
|
1510
|
+
case 8:
|
|
1511
|
+
if (options && options.upsert) {
|
|
1512
|
+
if (this.timestamps && !replacement.createdAt) {
|
|
1513
|
+
replacement.createdAt = date;
|
|
1514
|
+
}
|
|
1515
|
+
if (!replacement._id) {
|
|
1516
|
+
replacement._id = objectIdHexString();
|
|
1517
|
+
}
|
|
1518
|
+
replacement.__v = 0;
|
|
1519
|
+
}
|
|
1520
|
+
if (this.createLogs && !bypassLogs) {
|
|
1521
|
+
if (!options) {
|
|
1522
|
+
options = { returnDocument: 'before' };
|
|
1523
|
+
}
|
|
1524
|
+
else {
|
|
1525
|
+
options.returnDocument = 'before';
|
|
1526
|
+
}
|
|
1527
|
+
monitor_13 = new monitor_manager_1.MonitorMongo('findOneAndReplace', this.collectionName, JSON.stringify([filter, replacement, options]));
|
|
1528
|
+
index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).findOneAndReplace(filter, replacement, options).then(function (res) {
|
|
1529
|
+
monitor_13.finish();
|
|
1530
|
+
var doc = res.value;
|
|
1531
|
+
if (doc) {
|
|
1532
|
+
log_collection_1.Logs.insertOne({
|
|
1533
|
+
_id: objectIdHexString(),
|
|
1534
|
+
type: 'document',
|
|
1535
|
+
collection: _this.collectionName,
|
|
1536
|
+
id_document: doc._id,
|
|
1537
|
+
payload: (0, common_1.getBinarySize)(JSON.stringify([doc, filter, replacement, options])) < 200000 ? JSON.stringify([doc, filter, replacement, options], null, 2) : 'Too Big',
|
|
1538
|
+
method: 'replaceOne',
|
|
1539
|
+
id_user: '',
|
|
1540
|
+
user: '',
|
|
1541
|
+
messageId: 0,
|
|
1542
|
+
route: ''
|
|
1543
|
+
});
|
|
1544
|
+
}
|
|
1545
|
+
else {
|
|
1546
|
+
log_collection_1.Logs.insertOne({
|
|
1547
|
+
_id: objectIdHexString(),
|
|
1548
|
+
type: 'document',
|
|
1549
|
+
collection: _this.collectionName,
|
|
1550
|
+
id_document: replacement._id,
|
|
1551
|
+
payload: (0, common_1.getBinarySize)(JSON.stringify(['upsert', filter, replacement, options])) < 200000 ? JSON.stringify(['upsert', filter, replacement, options], null, 2) : 'Too Big',
|
|
1552
|
+
method: 'replaceOne',
|
|
1553
|
+
id_user: '',
|
|
1554
|
+
user: '',
|
|
1555
|
+
messageId: 0,
|
|
1556
|
+
route: ''
|
|
1557
|
+
});
|
|
1558
|
+
}
|
|
1559
|
+
index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_this.collectionName);
|
|
1560
|
+
resolve(res.ok);
|
|
1561
|
+
}, function (err) {
|
|
1562
|
+
monitor_13.finish();
|
|
1563
|
+
console.log(new Date(), 'Error Find One And Replace', _this.collectionName, filter, replacement, options, err);
|
|
1564
|
+
reject(err);
|
|
1565
|
+
});
|
|
1566
|
+
}
|
|
1567
|
+
else {
|
|
1568
|
+
monitor_14 = new monitor_manager_1.MonitorMongo('replaceOne', this.collectionName, JSON.stringify([filter, replacement, options]));
|
|
1569
|
+
index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).replaceOne(filter, replacement, options).then(function (res) {
|
|
1570
|
+
monitor_14.finish();
|
|
1571
|
+
if (res.acknowledged) {
|
|
1572
|
+
index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_this.collectionName);
|
|
1573
|
+
resolve(res.modifiedCount);
|
|
1574
|
+
}
|
|
1575
|
+
else {
|
|
1576
|
+
reject(res.acknowledged);
|
|
1577
|
+
}
|
|
1578
|
+
}, function (err) {
|
|
1579
|
+
monitor_14.finish();
|
|
1580
|
+
console.log(new Date(), 'Error Replace One', _this.collectionName, filter, replacement, options, err);
|
|
1581
|
+
reject(err);
|
|
1582
|
+
});
|
|
1583
|
+
}
|
|
1584
|
+
_a.label = 9;
|
|
1585
|
+
case 9: return [2 /*return*/];
|
|
1586
|
+
}
|
|
1587
|
+
});
|
|
1588
|
+
}); });
|
|
1589
|
+
};
|
|
1590
|
+
MongoManagerCollection.prototype.stats = function (options) {
|
|
1591
|
+
var _this = this;
|
|
1592
|
+
return new Promise(function (resolve, reject) {
|
|
1593
|
+
var monitor = new monitor_manager_1.MonitorMongo('stats', _this.collectionName, JSON.stringify([options]));
|
|
1594
|
+
index_1.ResolveIOServer.getMainDB().collection(_this.collectionName, _this.collectionOptions).stats(options).then(function (res) {
|
|
1595
|
+
monitor.finish();
|
|
1596
|
+
resolve(res);
|
|
1597
|
+
}, function (err) {
|
|
1598
|
+
monitor.finish();
|
|
1599
|
+
console.log(new Date(), 'Error Stats', _this.collectionName, options, err);
|
|
1600
|
+
reject(err);
|
|
1601
|
+
});
|
|
1602
|
+
});
|
|
1603
|
+
};
|
|
1604
|
+
MongoManagerCollection.prototype.updateMany = function (filter, update, options, bypassLogs, bypassCheckSchema, bypassVersions) {
|
|
1605
|
+
var _this = this;
|
|
1606
|
+
if (bypassLogs === void 0) { bypassLogs = false; }
|
|
1607
|
+
if (bypassCheckSchema === void 0) { bypassCheckSchema = false; }
|
|
1608
|
+
if (bypassVersions === void 0) { bypassVersions = false; }
|
|
1609
|
+
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
1610
|
+
var date, validation, isValid, docs, i, doc, versionDoc, monitor;
|
|
1611
|
+
var _this = this;
|
|
1612
|
+
return __generator(this, function (_a) {
|
|
1613
|
+
switch (_a.label) {
|
|
1614
|
+
case 0:
|
|
1615
|
+
if ((!update.$inc || JSON.stringify(update.$inc) === '{}') && (!update.$set || JSON.stringify(update.$set) === '{}') && (!update.$setOnInsert || JSON.stringify(update.$setOnInsert) === '{}') && (!update.$unset || JSON.stringify(update.$unset) === '{}')) {
|
|
1616
|
+
resolve(1);
|
|
1617
|
+
return [2 /*return*/];
|
|
1618
|
+
}
|
|
1619
|
+
if (this.timestamps) {
|
|
1620
|
+
date = new Date();
|
|
1621
|
+
if (!update.$set) {
|
|
1622
|
+
update.$set = { updatedAt: date };
|
|
1623
|
+
}
|
|
1624
|
+
else {
|
|
1625
|
+
update.$set.updatedAt = date;
|
|
1626
|
+
}
|
|
1627
|
+
}
|
|
1628
|
+
if (this.checkSchema && !bypassCheckSchema) {
|
|
1629
|
+
validation = this.simplschema.newContext();
|
|
1630
|
+
isValid = validation.validate(update, { modifier: true });
|
|
1631
|
+
if (!isValid) {
|
|
1632
|
+
console.log(new Date(), this.collectionName, 'Schema Errors - updateMany', validation.validationErrors());
|
|
1633
|
+
index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(), 'insertErrorLog', 'Schema Failed on updateMany - ' + this.collectionName, [validation.validationErrors(), update]);
|
|
1634
|
+
reject(validation.validationErrors());
|
|
1635
|
+
return [2 /*return*/];
|
|
1636
|
+
}
|
|
1637
|
+
}
|
|
1638
|
+
if (!((this.useVersions && !bypassVersions) || (this.createLogs && !bypassLogs))) return [3 /*break*/, 2];
|
|
1639
|
+
return [4 /*yield*/, this.find(filter)];
|
|
1640
|
+
case 1:
|
|
1641
|
+
docs = _a.sent();
|
|
1642
|
+
for (i = 0; i < docs.length; i++) {
|
|
1643
|
+
doc = docs[i];
|
|
1644
|
+
if (this.createLogs && !bypassLogs) {
|
|
1645
|
+
log_collection_1.Logs.insertOne({
|
|
1646
|
+
_id: objectIdHexString(),
|
|
1647
|
+
type: 'document',
|
|
1648
|
+
collection: this.collectionName,
|
|
1649
|
+
id_document: doc._id,
|
|
1650
|
+
payload: (0, common_1.getBinarySize)(JSON.stringify([doc, filter, update, options])) < 200000 ? JSON.stringify([doc, filter, update, options], null, 2) : 'Too Big',
|
|
1651
|
+
method: 'updateMany',
|
|
1652
|
+
id_user: '',
|
|
1653
|
+
user: '',
|
|
1654
|
+
messageId: 0,
|
|
1655
|
+
route: ''
|
|
1656
|
+
});
|
|
1657
|
+
}
|
|
1658
|
+
if (this.useVersions) {
|
|
1659
|
+
versionDoc = (0, common_1.deepCopy)(doc);
|
|
1660
|
+
versionDoc._id = { _id: doc._id, __v: doc.__v };
|
|
1661
|
+
index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({ _id: { _id: doc._id, __v: doc.__v } }, versionDoc, { upsert: true });
|
|
1662
|
+
if (doc.__v >= 4) {
|
|
1663
|
+
index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({ $and: [{ '_id._id': doc._id }, { '_id.__v': { $lt: doc.__v - 4 } }] });
|
|
1664
|
+
}
|
|
1665
|
+
if (!objectContainsPropertyDeep(update, '__v')) {
|
|
1666
|
+
if (!update.$inc) {
|
|
1667
|
+
update.$inc = { __v: 1 };
|
|
1668
|
+
}
|
|
1669
|
+
else if (!update.$inc.__v) {
|
|
1670
|
+
update.$inc.__v = 1;
|
|
1671
|
+
}
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1674
|
+
}
|
|
1675
|
+
_a.label = 2;
|
|
1676
|
+
case 2:
|
|
1677
|
+
monitor = new monitor_manager_1.MonitorMongo('updateMany', this.collectionName, JSON.stringify([filter, update, options]));
|
|
1678
|
+
index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).updateMany(filter, update, options).then(function (res) {
|
|
1679
|
+
monitor.finish();
|
|
1680
|
+
if (res.acknowledged) {
|
|
1681
|
+
index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_this.collectionName);
|
|
1682
|
+
resolve(res.modifiedCount);
|
|
1683
|
+
}
|
|
1684
|
+
else {
|
|
1685
|
+
reject(res.acknowledged);
|
|
1686
|
+
}
|
|
1687
|
+
}, function (err) {
|
|
1688
|
+
monitor.finish();
|
|
1689
|
+
console.log(new Date(), 'Error Update Many', _this.collectionName, filter, update, options, err);
|
|
1690
|
+
reject(err);
|
|
1691
|
+
});
|
|
1692
|
+
return [2 /*return*/];
|
|
1693
|
+
}
|
|
1694
|
+
});
|
|
1695
|
+
}); });
|
|
1696
|
+
};
|
|
1697
|
+
MongoManagerCollection.prototype.updateOne = function (filter, update, options, bypassLogs, bypassCheckSchema) {
|
|
1698
|
+
var _this = this;
|
|
1699
|
+
if (bypassLogs === void 0) { bypassLogs = false; }
|
|
1700
|
+
if (bypassCheckSchema === void 0) { bypassCheckSchema = false; }
|
|
1701
|
+
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
1702
|
+
var validation, isValid, date, doc, versionDoc, monitor_15, monitor_16, monitor_17, monitor_18;
|
|
1703
|
+
var _this = this;
|
|
1704
|
+
return __generator(this, function (_a) {
|
|
1705
|
+
switch (_a.label) {
|
|
1706
|
+
case 0:
|
|
1707
|
+
if (this.checkSchema && !bypassCheckSchema) {
|
|
1708
|
+
validation = this.simplschema.newContext();
|
|
1709
|
+
isValid = validation.validate(update, { modifier: true });
|
|
1710
|
+
if (!isValid) {
|
|
1711
|
+
console.log(new Date(), this.collectionName, 'Schema Errors - updateOne', validation.validationErrors());
|
|
1712
|
+
index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(), 'insertErrorLog', 'Schema Failed on updateOne - ' + this.collectionName, [validation.validationErrors(), update]);
|
|
1713
|
+
reject(validation.validationErrors());
|
|
1714
|
+
return [2 /*return*/];
|
|
1715
|
+
}
|
|
1716
|
+
}
|
|
1717
|
+
if ((!update.$inc || JSON.stringify(update.$inc) === '{}') && (!update.$set || JSON.stringify(update.$set) === '{}') && (!update.$setOnInsert || JSON.stringify(update.$setOnInsert) === '{}') && (!update.$unset || JSON.stringify(update.$unset) === '{}')) {
|
|
1718
|
+
resolve(1);
|
|
1719
|
+
return [2 /*return*/];
|
|
1720
|
+
}
|
|
1721
|
+
date = new Date();
|
|
1722
|
+
if (this.timestamps) {
|
|
1723
|
+
if (!update.$set) {
|
|
1724
|
+
update.$set = { updatedAt: date };
|
|
1725
|
+
}
|
|
1726
|
+
else {
|
|
1727
|
+
update.$set.updatedAt = date;
|
|
1728
|
+
}
|
|
1729
|
+
}
|
|
1730
|
+
if (!this.useVersions) return [3 /*break*/, 2];
|
|
1731
|
+
return [4 /*yield*/, this.findOne(filter, null, true)];
|
|
1732
|
+
case 1:
|
|
1733
|
+
doc = _a.sent();
|
|
1734
|
+
if (doc) {
|
|
1735
|
+
if (this.createLogs && !bypassLogs) {
|
|
1736
|
+
log_collection_1.Logs.insertOne({
|
|
1737
|
+
_id: objectIdHexString(),
|
|
1738
|
+
type: 'document',
|
|
1739
|
+
collection: this.collectionName,
|
|
1740
|
+
id_document: doc._id,
|
|
1741
|
+
payload: (0, common_1.getBinarySize)(JSON.stringify([doc, filter, update, options])) < 200000 ? JSON.stringify([doc, filter, update, options], null, 2) : 'Too Big',
|
|
1742
|
+
method: 'updateOne',
|
|
1743
|
+
id_user: '',
|
|
1744
|
+
user: '',
|
|
1745
|
+
messageId: 0,
|
|
1746
|
+
route: ''
|
|
1747
|
+
});
|
|
1748
|
+
}
|
|
1749
|
+
versionDoc = (0, common_1.deepCopy)(doc);
|
|
1750
|
+
versionDoc._id = { _id: doc._id, __v: doc.__v };
|
|
1751
|
+
index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({ _id: { _id: doc._id, __v: doc.__v } }, versionDoc, { upsert: true });
|
|
1752
|
+
if (doc.__v >= 4) {
|
|
1753
|
+
index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({ $and: [{ '_id._id': doc._id }, { '_id.__v': { $lt: doc.__v - 4 } }] });
|
|
1754
|
+
}
|
|
1755
|
+
if (!objectContainsPropertyDeep(update, '__v')) {
|
|
1756
|
+
if (!update.$inc) {
|
|
1757
|
+
update.$inc = { __v: 1 };
|
|
1758
|
+
}
|
|
1759
|
+
else if (!update.$inc.__v) {
|
|
1760
|
+
update.$inc.__v = 1;
|
|
1761
|
+
}
|
|
1762
|
+
}
|
|
1763
|
+
monitor_15 = new monitor_manager_1.MonitorMongo('updateOne', this.collectionName, JSON.stringify([filter, update, options]));
|
|
1764
|
+
index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).updateOne(filter, update, options).then(function (res) {
|
|
1765
|
+
monitor_15.finish();
|
|
1766
|
+
if (res.acknowledged) {
|
|
1767
|
+
index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_this.collectionName);
|
|
1768
|
+
resolve(res.modifiedCount);
|
|
1769
|
+
}
|
|
1770
|
+
else {
|
|
1771
|
+
reject(res.acknowledged);
|
|
1772
|
+
}
|
|
1773
|
+
}, function (err) {
|
|
1774
|
+
monitor_15.finish();
|
|
1775
|
+
console.log(new Date(), 'Error Update One', _this.collectionName, filter, update, options, err);
|
|
1776
|
+
reject(err);
|
|
1777
|
+
});
|
|
1778
|
+
}
|
|
1779
|
+
else if (options && options.upsert) {
|
|
1780
|
+
if (!update.$setOnInsert) {
|
|
1781
|
+
if (this.timestamps) {
|
|
1782
|
+
update.$setOnInsert = {
|
|
1783
|
+
_id: objectIdHexString(),
|
|
1784
|
+
__v: 0,
|
|
1785
|
+
createdAt: new Date()
|
|
1786
|
+
};
|
|
1787
|
+
}
|
|
1788
|
+
else {
|
|
1789
|
+
update.$setOnInsert = {
|
|
1790
|
+
_id: objectIdHexString(),
|
|
1791
|
+
__v: 0
|
|
1792
|
+
};
|
|
1793
|
+
}
|
|
1794
|
+
}
|
|
1795
|
+
else {
|
|
1796
|
+
if (!update.$setOnInsert._id) {
|
|
1797
|
+
update.$setOnInsert._id = objectIdHexString();
|
|
1798
|
+
}
|
|
1799
|
+
if (this.timestamps) {
|
|
1800
|
+
update.$setOnInsert.createdAt = new Date();
|
|
1801
|
+
}
|
|
1802
|
+
}
|
|
1803
|
+
if (this.createLogs && !bypassLogs) {
|
|
1804
|
+
log_collection_1.Logs.insertOne({
|
|
1805
|
+
_id: objectIdHexString(),
|
|
1806
|
+
type: 'document',
|
|
1807
|
+
collection: this.collectionName,
|
|
1808
|
+
id_document: update.$setOnInsert._id,
|
|
1809
|
+
payload: (0, common_1.getBinarySize)(JSON.stringify(['upsert', filter, update, options])) < 200000 ? JSON.stringify(['upsert', filter, update, options], null, 2) : 'Too Big',
|
|
1810
|
+
method: 'updateOne',
|
|
1811
|
+
id_user: '',
|
|
1812
|
+
user: '',
|
|
1813
|
+
messageId: 0,
|
|
1814
|
+
route: ''
|
|
1815
|
+
});
|
|
1816
|
+
}
|
|
1817
|
+
monitor_16 = new monitor_manager_1.MonitorMongo('updateOne', this.collectionName, JSON.stringify([filter, update, options]));
|
|
1818
|
+
index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).updateOne(filter, update, options).then(function (res) {
|
|
1819
|
+
monitor_16.finish();
|
|
1820
|
+
if (res.acknowledged) {
|
|
1821
|
+
index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_this.collectionName);
|
|
1822
|
+
resolve(res.modifiedCount);
|
|
1823
|
+
}
|
|
1824
|
+
else {
|
|
1825
|
+
reject(res.acknowledged);
|
|
1826
|
+
}
|
|
1827
|
+
}, function (err) {
|
|
1828
|
+
monitor_16.finish();
|
|
1829
|
+
console.log(new Date(), 'Error Update One', _this.collectionName, filter, update, options, err);
|
|
1830
|
+
reject(err);
|
|
1831
|
+
});
|
|
1832
|
+
}
|
|
1833
|
+
else {
|
|
1834
|
+
reject('No Document');
|
|
1835
|
+
}
|
|
1836
|
+
return [3 /*break*/, 3];
|
|
1837
|
+
case 2:
|
|
1838
|
+
if (options && options.upsert) {
|
|
1839
|
+
if (!update.$setOnInsert) {
|
|
1840
|
+
if (this.timestamps) {
|
|
1841
|
+
update.$setOnInsert = {
|
|
1842
|
+
_id: objectIdHexString(),
|
|
1843
|
+
createdAt: new Date()
|
|
1844
|
+
};
|
|
1845
|
+
}
|
|
1846
|
+
else {
|
|
1847
|
+
update.$setOnInsert = {
|
|
1848
|
+
_id: objectIdHexString()
|
|
1849
|
+
};
|
|
1850
|
+
}
|
|
1851
|
+
}
|
|
1852
|
+
else {
|
|
1853
|
+
if (!update.$setOnInsert._id) {
|
|
1854
|
+
update.$setOnInsert._id = objectIdHexString();
|
|
1855
|
+
}
|
|
1856
|
+
if (this.timestamps) {
|
|
1857
|
+
update.$setOnInsert.createdAt = new Date();
|
|
1858
|
+
}
|
|
1859
|
+
}
|
|
1860
|
+
}
|
|
1861
|
+
if (this.createLogs && !bypassLogs) {
|
|
1862
|
+
if (!options) {
|
|
1863
|
+
options = { returnDocument: 'before' };
|
|
1864
|
+
}
|
|
1865
|
+
else {
|
|
1866
|
+
options.returnDocument = 'before';
|
|
1867
|
+
}
|
|
1868
|
+
monitor_17 = new monitor_manager_1.MonitorMongo('findOneAndUpdate', this.collectionName, JSON.stringify([filter, update, options]));
|
|
1869
|
+
index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).findOneAndUpdate(filter, update, options).then(function (res) {
|
|
1870
|
+
monitor_17.finish();
|
|
1871
|
+
var doc = res.value;
|
|
1872
|
+
if (doc) {
|
|
1873
|
+
log_collection_1.Logs.insertOne({
|
|
1874
|
+
_id: objectIdHexString(),
|
|
1875
|
+
type: 'document',
|
|
1876
|
+
collection: _this.collectionName,
|
|
1877
|
+
id_document: doc._id,
|
|
1878
|
+
payload: (0, common_1.getBinarySize)(JSON.stringify([doc, filter, update, options])) < 200000 ? JSON.stringify([doc, filter, update, options], null, 2) : 'Too Big',
|
|
1879
|
+
method: 'updateOne',
|
|
1880
|
+
id_user: '',
|
|
1881
|
+
user: '',
|
|
1882
|
+
messageId: 0,
|
|
1883
|
+
route: ''
|
|
1884
|
+
});
|
|
1885
|
+
resolve(res.ok);
|
|
1886
|
+
}
|
|
1887
|
+
else if (update.$setOnInsert) {
|
|
1888
|
+
log_collection_1.Logs.insertOne({
|
|
1889
|
+
_id: objectIdHexString(),
|
|
1890
|
+
type: 'document',
|
|
1891
|
+
collection: _this.collectionName,
|
|
1892
|
+
id_document: update.$setOnInsert._id,
|
|
1893
|
+
payload: (0, common_1.getBinarySize)(JSON.stringify(['upsert', filter, update, options])) < 200000 ? JSON.stringify(['upsert', filter, update, options], null, 2) : 'Too Big',
|
|
1894
|
+
method: 'updateOne',
|
|
1895
|
+
id_user: '',
|
|
1896
|
+
user: '',
|
|
1897
|
+
messageId: 0,
|
|
1898
|
+
route: ''
|
|
1899
|
+
});
|
|
1900
|
+
index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_this.collectionName);
|
|
1901
|
+
resolve(res.ok);
|
|
1902
|
+
}
|
|
1903
|
+
else {
|
|
1904
|
+
resolve(0);
|
|
1905
|
+
}
|
|
1906
|
+
}, function (err) {
|
|
1907
|
+
monitor_17.finish();
|
|
1908
|
+
console.log(new Date(), 'Error Find One And Update', _this.collectionName, filter, update, options, err);
|
|
1909
|
+
reject(err);
|
|
1910
|
+
});
|
|
1911
|
+
}
|
|
1912
|
+
else {
|
|
1913
|
+
monitor_18 = new monitor_manager_1.MonitorMongo('updateOne', this.collectionName, JSON.stringify([filter, update, options]));
|
|
1914
|
+
index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).updateOne(filter, update, options).then(function (res) {
|
|
1915
|
+
monitor_18.finish();
|
|
1916
|
+
if (res.acknowledged) {
|
|
1917
|
+
index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_this.collectionName);
|
|
1918
|
+
resolve(res.modifiedCount);
|
|
1919
|
+
}
|
|
1920
|
+
else {
|
|
1921
|
+
reject(res.acknowledged);
|
|
1922
|
+
}
|
|
1923
|
+
}, function (err) {
|
|
1924
|
+
monitor_18.finish();
|
|
1925
|
+
console.log(new Date(), 'Error Update One', _this.collectionName, filter, update, options, err);
|
|
1926
|
+
reject(err);
|
|
1927
|
+
});
|
|
1928
|
+
}
|
|
1929
|
+
_a.label = 3;
|
|
1930
|
+
case 3: return [2 /*return*/];
|
|
1931
|
+
}
|
|
1932
|
+
});
|
|
1933
|
+
}); });
|
|
1934
|
+
};
|
|
1935
|
+
MongoManagerCollection.prototype.watchCollection = function (pipeline, options) {
|
|
1936
|
+
var _this = this;
|
|
1937
|
+
if (pipeline === void 0) { pipeline = []; }
|
|
1938
|
+
var stream = index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).watch(pipeline, options);
|
|
1939
|
+
stream.on('change', function (evData) {
|
|
1940
|
+
index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_this.collectionName);
|
|
1941
|
+
return evData;
|
|
1942
|
+
});
|
|
1943
|
+
return stream;
|
|
1944
|
+
};
|
|
1945
|
+
MongoManagerCollection.prototype.watchCollectionWithAggregate = function (pipeline, options) {
|
|
1946
|
+
var _this = this;
|
|
1947
|
+
if (pipeline === void 0) { pipeline = []; }
|
|
1948
|
+
var stream = index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).watch(pipeline, options);
|
|
1949
|
+
stream.on('change', function (evData) {
|
|
1950
|
+
index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_this.collectionName);
|
|
1951
|
+
return evData;
|
|
1952
|
+
});
|
|
1953
|
+
return stream;
|
|
1954
|
+
};
|
|
1955
|
+
return MongoManagerCollection;
|
|
1956
|
+
}());
|
|
1957
|
+
exports.MongoManagerCollection = MongoManagerCollection;
|
|
1958
|
+
var MongoManagerUserCollection = /** @class */ (function (_super) {
|
|
1959
|
+
__extends(MongoManagerUserCollection, _super);
|
|
1960
|
+
function MongoManagerUserCollection() {
|
|
1961
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
|
1962
|
+
}
|
|
1963
|
+
MongoManagerUserCollection.prototype.authenticateStrategy = function () {
|
|
1964
|
+
var _this = this;
|
|
1965
|
+
return function (username, password, cb) { return __awaiter(_this, void 0, void 0, function () {
|
|
1966
|
+
var user;
|
|
1967
|
+
return __generator(this, function (_a) {
|
|
1968
|
+
switch (_a.label) {
|
|
1969
|
+
case 0: return [4 /*yield*/, user_collection_1.Users.findOne({ username: username })];
|
|
1970
|
+
case 1:
|
|
1971
|
+
user = _a.sent();
|
|
1972
|
+
this.authenticate(user, password).then(function (authUser) {
|
|
1973
|
+
if (authUser.data) {
|
|
1974
|
+
cb(null, user, null);
|
|
1975
|
+
}
|
|
1976
|
+
else {
|
|
1977
|
+
cb(null, null, authUser.error);
|
|
1978
|
+
}
|
|
1979
|
+
}, function (err) {
|
|
1980
|
+
cb(err, null, null);
|
|
1981
|
+
});
|
|
1982
|
+
return [2 /*return*/];
|
|
1983
|
+
}
|
|
1984
|
+
});
|
|
1985
|
+
}); };
|
|
1986
|
+
};
|
|
1987
|
+
MongoManagerUserCollection.prototype.authenticate = function (user, password) {
|
|
1988
|
+
var _this = this;
|
|
1989
|
+
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
1990
|
+
var attemptsInterval, calculatedInterval;
|
|
1991
|
+
var _this = this;
|
|
1992
|
+
return __generator(this, function (_a) {
|
|
1993
|
+
switch (_a.label) {
|
|
1994
|
+
case 0:
|
|
1995
|
+
attemptsInterval = Math.pow(100, Math.log(user.attempts + 1));
|
|
1996
|
+
calculatedInterval = attemptsInterval < 300000 ? attemptsInterval : 300000;
|
|
1997
|
+
if (!user.last) return [3 /*break*/, 3];
|
|
1998
|
+
if (!(Date.now() - user.last.getTime() < calculatedInterval)) return [3 /*break*/, 2];
|
|
1999
|
+
user.last = new Date();
|
|
2000
|
+
return [4 /*yield*/, user_collection_1.Users.updateOne({ _id: user._id }, { $set: { last: user.last } })];
|
|
2001
|
+
case 1:
|
|
2002
|
+
_a.sent();
|
|
2003
|
+
resolve({
|
|
2004
|
+
data: null,
|
|
2005
|
+
error: 'Attempt Too Soon'
|
|
2006
|
+
});
|
|
2007
|
+
return [2 /*return*/];
|
|
2008
|
+
case 2: return [3 /*break*/, 4];
|
|
2009
|
+
case 3:
|
|
2010
|
+
user.last = new Date();
|
|
2011
|
+
_a.label = 4;
|
|
2012
|
+
case 4:
|
|
2013
|
+
if (user.attempts >= 5) {
|
|
2014
|
+
resolve({
|
|
2015
|
+
data: null,
|
|
2016
|
+
error: 'Too Many Attempts'
|
|
2017
|
+
});
|
|
2018
|
+
return [2 /*return*/];
|
|
2019
|
+
}
|
|
2020
|
+
if (!user.salt) {
|
|
2021
|
+
resolve({
|
|
2022
|
+
data: null,
|
|
2023
|
+
error: 'No Salt Value Stored'
|
|
2024
|
+
});
|
|
2025
|
+
return [2 /*return*/];
|
|
2026
|
+
}
|
|
2027
|
+
pbkdf2(password, user.salt, {
|
|
2028
|
+
iterations: 25000,
|
|
2029
|
+
keylen: 512,
|
|
2030
|
+
digestAlgorithm: 'sha256'
|
|
2031
|
+
}, function (err, hashBuffer) { return __awaiter(_this, void 0, void 0, function () {
|
|
2032
|
+
return __generator(this, function (_a) {
|
|
2033
|
+
switch (_a.label) {
|
|
2034
|
+
case 0:
|
|
2035
|
+
if (err) {
|
|
2036
|
+
return [2 /*return*/, reject(err)];
|
|
2037
|
+
}
|
|
2038
|
+
if (!scmp(hashBuffer, Buffer.from(user.hash, 'hex'))) return [3 /*break*/, 2];
|
|
2039
|
+
user.last = new Date();
|
|
2040
|
+
user.attempts = 0;
|
|
2041
|
+
return [4 /*yield*/, user_collection_1.Users.updateOne({ _id: user._id }, { $set: { last: user.last, attempts: user.attempts } })];
|
|
2042
|
+
case 1:
|
|
2043
|
+
_a.sent();
|
|
2044
|
+
resolve({
|
|
2045
|
+
data: user,
|
|
2046
|
+
error: ''
|
|
2047
|
+
});
|
|
2048
|
+
return [3 /*break*/, 4];
|
|
2049
|
+
case 2:
|
|
2050
|
+
user.last = new Date();
|
|
2051
|
+
user.attempts = user.attempts + 1;
|
|
2052
|
+
return [4 /*yield*/, user_collection_1.Users.updateOne({ _id: user._id }, { $set: { last: user.last, attempts: user.attempts } })];
|
|
2053
|
+
case 3:
|
|
2054
|
+
_a.sent();
|
|
2055
|
+
if (user.attempts >= 5) {
|
|
2056
|
+
resolve({
|
|
2057
|
+
data: null,
|
|
2058
|
+
error: 'Too Many Attempts'
|
|
2059
|
+
});
|
|
2060
|
+
}
|
|
2061
|
+
else {
|
|
2062
|
+
resolve({
|
|
2063
|
+
data: null,
|
|
2064
|
+
error: 'Invalid Username And Password'
|
|
2065
|
+
});
|
|
2066
|
+
}
|
|
2067
|
+
_a.label = 4;
|
|
2068
|
+
case 4: return [2 /*return*/];
|
|
2069
|
+
}
|
|
2070
|
+
});
|
|
2071
|
+
}); });
|
|
2072
|
+
return [2 /*return*/];
|
|
2073
|
+
}
|
|
2074
|
+
});
|
|
2075
|
+
}); });
|
|
2076
|
+
};
|
|
2077
|
+
MongoManagerUserCollection.prototype.serializeUser = function () {
|
|
2078
|
+
return function (user, cb) {
|
|
2079
|
+
cb(null, user.username);
|
|
2080
|
+
};
|
|
2081
|
+
};
|
|
2082
|
+
MongoManagerUserCollection.prototype.deserializeUser = function () {
|
|
2083
|
+
return function (username, cb) {
|
|
2084
|
+
user_collection_1.Users.findOne({ username: username }).then(function (res) {
|
|
2085
|
+
cb(null, res);
|
|
2086
|
+
}, function (err) {
|
|
2087
|
+
cb(err, null);
|
|
2088
|
+
});
|
|
2089
|
+
};
|
|
2090
|
+
};
|
|
2091
|
+
MongoManagerUserCollection.prototype.setPassword = function (user, password) {
|
|
2092
|
+
var _this = this;
|
|
2093
|
+
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
2094
|
+
var saltBuffer, salt, hashRaw, hash;
|
|
2095
|
+
return __generator(this, function (_a) {
|
|
2096
|
+
switch (_a.label) {
|
|
2097
|
+
case 0:
|
|
2098
|
+
if (!!user) return [3 /*break*/, 1];
|
|
2099
|
+
reject('No User');
|
|
2100
|
+
return [3 /*break*/, 5];
|
|
2101
|
+
case 1:
|
|
2102
|
+
if (!!password) return [3 /*break*/, 2];
|
|
2103
|
+
reject('No Password');
|
|
2104
|
+
return [3 /*break*/, 5];
|
|
2105
|
+
case 2: return [4 /*yield*/, randomBytes(32)];
|
|
2106
|
+
case 3:
|
|
2107
|
+
saltBuffer = _a.sent();
|
|
2108
|
+
salt = saltBuffer.toString('hex');
|
|
2109
|
+
return [4 /*yield*/, pbkdf2Promisified(password, salt, {
|
|
2110
|
+
iterations: 25000,
|
|
2111
|
+
keylen: 512,
|
|
2112
|
+
digestAlgorithm: 'sha256'
|
|
2113
|
+
})];
|
|
2114
|
+
case 4:
|
|
2115
|
+
hashRaw = _a.sent();
|
|
2116
|
+
hash = Buffer.from(hashRaw, 'binary').toString('hex');
|
|
2117
|
+
user_collection_1.Users.updateOne({ _id: user._id }, { $set: { hash: hash, salt: salt, services: {}, attempts: 0 } }).then(function (res) { return resolve(res); }, function (rej) { return reject(rej); });
|
|
2118
|
+
_a.label = 5;
|
|
2119
|
+
case 5: return [2 /*return*/];
|
|
2120
|
+
}
|
|
2121
|
+
});
|
|
2122
|
+
}); });
|
|
2123
|
+
};
|
|
2124
|
+
;
|
|
2125
|
+
MongoManagerUserCollection.prototype.changePassword = function (user, oldPassword, newPassword) {
|
|
2126
|
+
var _this = this;
|
|
2127
|
+
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
2128
|
+
var authUser;
|
|
2129
|
+
return __generator(this, function (_a) {
|
|
2130
|
+
switch (_a.label) {
|
|
2131
|
+
case 0:
|
|
2132
|
+
if (!!user) return [3 /*break*/, 1];
|
|
2133
|
+
reject('Missing User');
|
|
2134
|
+
return [3 /*break*/, 4];
|
|
2135
|
+
case 1:
|
|
2136
|
+
if (!(!oldPassword || !newPassword)) return [3 /*break*/, 2];
|
|
2137
|
+
reject('Missing Password');
|
|
2138
|
+
return [3 /*break*/, 4];
|
|
2139
|
+
case 2: return [4 /*yield*/, this.authenticate(user, oldPassword)];
|
|
2140
|
+
case 3:
|
|
2141
|
+
authUser = _a.sent();
|
|
2142
|
+
if (!authUser['data']) {
|
|
2143
|
+
reject(authUser['error']);
|
|
2144
|
+
}
|
|
2145
|
+
else {
|
|
2146
|
+
this.setPassword(user, newPassword).then(function (res) { return resolve(res); }, function (rej) { return reject(rej); });
|
|
2147
|
+
}
|
|
2148
|
+
_a.label = 4;
|
|
2149
|
+
case 4: return [2 /*return*/];
|
|
2150
|
+
}
|
|
2151
|
+
});
|
|
2152
|
+
}); });
|
|
2153
|
+
};
|
|
2154
|
+
MongoManagerUserCollection.prototype.register = function (user, password) {
|
|
2155
|
+
var _this = this;
|
|
2156
|
+
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
2157
|
+
var dbUser;
|
|
2158
|
+
return __generator(this, function (_a) {
|
|
2159
|
+
switch (_a.label) {
|
|
2160
|
+
case 0:
|
|
2161
|
+
if (!!user.username) return [3 /*break*/, 1];
|
|
2162
|
+
reject('Missing Username');
|
|
2163
|
+
return [3 /*break*/, 5];
|
|
2164
|
+
case 1: return [4 /*yield*/, user_collection_1.Users.findOne({ username: user.username })];
|
|
2165
|
+
case 2:
|
|
2166
|
+
dbUser = _a.sent();
|
|
2167
|
+
if (!dbUser) return [3 /*break*/, 3];
|
|
2168
|
+
reject('Username Exists');
|
|
2169
|
+
return [3 /*break*/, 5];
|
|
2170
|
+
case 3: return [4 /*yield*/, user_collection_1.Users.findOne({ email: user.email })];
|
|
2171
|
+
case 4:
|
|
2172
|
+
dbUser = _a.sent();
|
|
2173
|
+
if (dbUser) {
|
|
2174
|
+
reject('Email Exists');
|
|
2175
|
+
}
|
|
2176
|
+
else {
|
|
2177
|
+
user.setPassword(password);
|
|
2178
|
+
resolve(user);
|
|
2179
|
+
}
|
|
2180
|
+
_a.label = 5;
|
|
2181
|
+
case 5: return [2 /*return*/];
|
|
2182
|
+
}
|
|
2183
|
+
});
|
|
2184
|
+
}); });
|
|
2185
|
+
};
|
|
2186
|
+
MongoManagerUserCollection.prototype.resetAttempts = function (user) {
|
|
2187
|
+
return user_collection_1.Users.updateOne({ _id: user._id }, { $set: { attempts: 0 } });
|
|
2188
|
+
};
|
|
2189
|
+
return MongoManagerUserCollection;
|
|
2190
|
+
}(MongoManagerCollection));
|
|
2191
|
+
exports.MongoManagerUserCollection = MongoManagerUserCollection;
|
|
2192
|
+
function objectIdHexString() {
|
|
2193
|
+
var objectId = new mongodb_1.ObjectId();
|
|
2194
|
+
return objectId.toHexString();
|
|
2195
|
+
}
|
|
2196
|
+
exports.objectIdHexString = objectIdHexString;
|
|
2197
|
+
function pbkdf2(password, salt, options, callback) {
|
|
2198
|
+
crypto.pbkdf2(password, salt, options.iterations, options.keylen, options.digestAlgorithm, callback);
|
|
2199
|
+
}
|
|
2200
|
+
;
|
|
2201
|
+
function pbkdf2Promisified(password, salt, options) {
|
|
2202
|
+
return new Promise(function (resolve, reject) { return pbkdf2(password, salt, options, function (err, hashRaw) { return (err ? reject(err) : resolve(hashRaw)); }); });
|
|
2203
|
+
}
|
|
2204
|
+
function randomBytes(saltlen) {
|
|
2205
|
+
return new Promise(function (resolve, reject) { return crypto.randomBytes(saltlen, function (err, saltBuffer) { return (err ? reject(err) : resolve(saltBuffer)); }); });
|
|
2206
|
+
}
|
|
2207
|
+
function objectContainsPropertyDeep(obj, prop) {
|
|
2208
|
+
var keys = Object.keys(obj);
|
|
2209
|
+
if (keys.some(function (a) { return a === prop; })) {
|
|
2210
|
+
return true;
|
|
2211
|
+
}
|
|
2212
|
+
for (var i = 0; i < keys.length; i++) {
|
|
2213
|
+
var key = keys[i];
|
|
2214
|
+
if (obj[key] && typeof obj[key] === 'object' && objectContainsPropertyDeep(obj[key], prop)) {
|
|
2215
|
+
return true;
|
|
2216
|
+
}
|
|
2217
|
+
}
|
|
2218
|
+
return false;
|
|
2219
|
+
}
|
|
2220
|
+
|
|
2221
|
+
//# sourceMappingURL=mongo.manager.js.map
|