@steedos/objectql 3.0.13-beta.3 → 3.0.13-beta.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. package/lib/actions/field_updates.js +84 -153
  2. package/lib/actions/field_updates.js.map +1 -1
  3. package/lib/actions/index.js +1 -1
  4. package/lib/actions/workflow_notifications.js +50 -111
  5. package/lib/actions/workflow_notifications.js.map +1 -1
  6. package/lib/actions/workflow_outbound_messages.js +43 -94
  7. package/lib/actions/workflow_outbound_messages.js.map +1 -1
  8. package/lib/actions/workflow_rule.js +80 -177
  9. package/lib/actions/workflow_rule.js.map +1 -1
  10. package/lib/broker.js +19 -49
  11. package/lib/broker.js.map +1 -1
  12. package/lib/driver/field-encrytion/index.js +1 -1
  13. package/lib/driver/field-encrytion/mongosharedconst.js +17 -17
  14. package/lib/driver/field-encrytion/mongosharedconst.js.map +1 -1
  15. package/lib/driver/format.js +3 -3
  16. package/lib/driver/format.js.map +1 -1
  17. package/lib/driver/index.js +1 -1
  18. package/lib/driver/metadata.js +283 -387
  19. package/lib/driver/metadata.js.map +1 -1
  20. package/lib/driver/metadataOld.js +104 -105
  21. package/lib/driver/metadataOld.js.map +1 -1
  22. package/lib/driver/meteorMongo.js +5 -8
  23. package/lib/driver/meteorMongo.js.map +1 -1
  24. package/lib/driver/mongo.js +321 -506
  25. package/lib/driver/mongo.js.map +1 -1
  26. package/lib/driver/mysql.js +16 -20
  27. package/lib/driver/mysql.js.map +1 -1
  28. package/lib/driver/oracle.js +25 -38
  29. package/lib/driver/oracle.js.map +1 -1
  30. package/lib/driver/postgres.js +16 -20
  31. package/lib/driver/postgres.js.map +1 -1
  32. package/lib/driver/sqlite3.js +15 -19
  33. package/lib/driver/sqlite3.js.map +1 -1
  34. package/lib/driver/sqlserver.js +20 -33
  35. package/lib/driver/sqlserver.js.map +1 -1
  36. package/lib/errors/index.js +10 -11
  37. package/lib/errors/index.js.map +1 -1
  38. package/lib/formula/core.js +307 -594
  39. package/lib/formula/core.js.map +1 -1
  40. package/lib/formula/field_formula.js +80 -126
  41. package/lib/formula/field_formula.js.map +1 -1
  42. package/lib/formula/index.js +56 -97
  43. package/lib/formula/index.js.map +1 -1
  44. package/lib/formula/params.js +11 -11
  45. package/lib/formula/params.js.map +1 -1
  46. package/lib/formula/recompute.js +41 -166
  47. package/lib/formula/recompute.js.map +1 -1
  48. package/lib/formula/simple_params.js +12 -12
  49. package/lib/formula/simple_params.js.map +1 -1
  50. package/lib/formula/util.js +43 -67
  51. package/lib/formula/util.js.map +1 -1
  52. package/lib/functions/function.js +62 -87
  53. package/lib/functions/function.js.map +1 -1
  54. package/lib/index.js +3 -3
  55. package/lib/index.js.map +1 -1
  56. package/lib/services/datasourceServiceFactory.js +77 -111
  57. package/lib/services/datasourceServiceFactory.js.map +1 -1
  58. package/lib/services/index.js +3 -3
  59. package/lib/services/index.js.map +1 -1
  60. package/lib/summary/core.js +162 -355
  61. package/lib/summary/core.js.map +1 -1
  62. package/lib/summary/field_summary.js +44 -71
  63. package/lib/summary/field_summary.js.map +1 -1
  64. package/lib/summary/index.js +65 -80
  65. package/lib/summary/index.js.map +1 -1
  66. package/lib/summary/recompute.js +19 -47
  67. package/lib/summary/recompute.js.map +1 -1
  68. package/lib/triggers/trigger.js +101 -142
  69. package/lib/triggers/trigger.js.map +1 -1
  70. package/lib/ts-types/index.js +1 -1
  71. package/lib/typeorm/driver.js +250 -450
  72. package/lib/typeorm/driver.js.map +1 -1
  73. package/lib/typeorm/index.js +1 -1
  74. package/lib/typeorm/util.js +28 -28
  75. package/lib/typeorm/util.js.map +1 -1
  76. package/lib/types/action.js +24 -33
  77. package/lib/types/action.js.map +1 -1
  78. package/lib/types/app.js +210 -355
  79. package/lib/types/app.js.map +1 -1
  80. package/lib/types/connection.js +42 -78
  81. package/lib/types/connection.js.map +1 -1
  82. package/lib/types/dashboard.js +56 -77
  83. package/lib/types/dashboard.js.map +1 -1
  84. package/lib/types/datasource.js +359 -705
  85. package/lib/types/datasource.js.map +1 -1
  86. package/lib/types/defaultValue.js +53 -65
  87. package/lib/types/defaultValue.js.map +1 -1
  88. package/lib/types/field.js +165 -137
  89. package/lib/types/field.js.map +1 -1
  90. package/lib/types/field_permission.js +35 -66
  91. package/lib/types/field_permission.js.map +1 -1
  92. package/lib/types/index.js +1 -1
  93. package/lib/types/list_view.js +36 -43
  94. package/lib/types/list_view.js.map +1 -1
  95. package/lib/types/metadata.js +6 -6
  96. package/lib/types/metadata.js.map +1 -1
  97. package/lib/types/method_base.js +95 -128
  98. package/lib/types/method_base.js.map +1 -1
  99. package/lib/types/object.js +2005 -3015
  100. package/lib/types/object.js.map +1 -1
  101. package/lib/types/object_events.js +24 -37
  102. package/lib/types/object_events.js.map +1 -1
  103. package/lib/types/object_layouts.js +15 -38
  104. package/lib/types/object_layouts.js.map +1 -1
  105. package/lib/types/object_permission.js +99 -85
  106. package/lib/types/object_permission.js.map +1 -1
  107. package/lib/types/report.js +70 -98
  108. package/lib/types/report.js.map +1 -1
  109. package/lib/types/restrictionRule.js +47 -96
  110. package/lib/types/restrictionRule.js.map +1 -1
  111. package/lib/types/schema.js +115 -177
  112. package/lib/types/schema.js.map +1 -1
  113. package/lib/types/shareRule.js +53 -102
  114. package/lib/types/shareRule.js.map +1 -1
  115. package/lib/types/trigger.js +43 -56
  116. package/lib/types/trigger.js.map +1 -1
  117. package/lib/types/validation_rules.js +18 -56
  118. package/lib/types/validation_rules.js.map +1 -1
  119. package/lib/util/convert.js +24 -24
  120. package/lib/util/convert.js.map +1 -1
  121. package/lib/util/field.js +16 -28
  122. package/lib/util/field.js.map +1 -1
  123. package/lib/util/function_expression.js +7 -8
  124. package/lib/util/function_expression.js.map +1 -1
  125. package/lib/util/index.js +79 -97
  126. package/lib/util/index.js.map +1 -1
  127. package/lib/util/locale.js +4 -4
  128. package/lib/util/locale.js.map +1 -1
  129. package/lib/util/permission_shares.js +7 -7
  130. package/lib/util/permission_shares.js.map +1 -1
  131. package/lib/util/suffix.js +19 -19
  132. package/lib/util/suffix.js.map +1 -1
  133. package/lib/util/transform.js +1 -1
  134. package/lib/validators/index.js +10 -10
  135. package/lib/validators/index.js.map +1 -1
  136. package/package.json +12 -12
@@ -1,23 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SteedosMongoDriver = void 0;
4
- var tslib_1 = require("tslib");
5
- var index_1 = require("./index");
6
- var mongodb_1 = require("mongodb");
7
- var filters_1 = require("@steedos/filters");
8
- var odata_v4_mongodb_1 = require("@steedos/odata-v4-mongodb");
9
- var _ = require("underscore");
10
- var field_encrytion_1 = require("./field-encrytion");
11
- var format_1 = require("./format");
12
- var monitor_enabled = false;
13
- var SLOW_QUERY_THRESHOLD_MS = 0;
4
+ const index_1 = require("./index");
5
+ const mongodb_1 = require("mongodb");
6
+ const filters_1 = require("@steedos/filters");
7
+ const odata_v4_mongodb_1 = require("@steedos/odata-v4-mongodb");
8
+ const _ = require("underscore");
9
+ const field_encrytion_1 = require("./field-encrytion");
10
+ const format_1 = require("./format");
11
+ let monitor_enabled = false;
12
+ let SLOW_QUERY_THRESHOLD_MS = 0;
14
13
  if (process.env.STEEDOS_MONGODB_SLOW_QUERY_THRESHOLD) {
15
14
  monitor_enabled = true;
16
15
  SLOW_QUERY_THRESHOLD_MS = Number(process.env.STEEDOS_MONGODB_SLOW_QUERY_THRESHOLD);
17
16
  }
18
- var runningCommands = new Map();
17
+ const runningCommands = new Map();
19
18
  function connectAndMonitor(client) {
20
- client.on("commandStarted", function (event) {
19
+ client.on("commandStarted", (event) => {
21
20
  runningCommands.set(event.requestId, {
22
21
  startTime: Date.now(),
23
22
  commandName: event.commandName,
@@ -25,53 +24,58 @@ function connectAndMonitor(client) {
25
24
  command: event.command,
26
25
  });
27
26
  });
28
- client.on("commandSucceeded", function (event) {
29
- var commandInfo = runningCommands.get(event.requestId);
27
+ client.on("commandSucceeded", (event) => {
28
+ const commandInfo = runningCommands.get(event.requestId);
30
29
  if (!commandInfo)
31
30
  return;
32
- var duration = Date.now() - commandInfo.startTime;
31
+ const duration = Date.now() - commandInfo.startTime;
33
32
  runningCommands.delete(event.requestId);
34
33
  if (duration >= SLOW_QUERY_THRESHOLD_MS) {
35
- console.warn("\uD83D\uDEA8 \u6162\u67E5\u8BE2\u68C0\u6D4B\u5230 (\u6210\u529F):");
36
- console.warn(" \u547D\u4EE4: ".concat(commandInfo.commandName));
37
- console.warn(" \u8017\u65F6: ".concat(duration, " ms"));
38
- console.warn(" \u6570\u636E\u5E93: ".concat(commandInfo.databaseName));
39
- console.warn(" command: ".concat(JSON.stringify(commandInfo.command)));
34
+ console.warn(`🚨 慢查询检测到 (成功):`);
35
+ console.warn(` 命令: ${commandInfo.commandName}`);
36
+ console.warn(` 耗时: ${duration} ms`);
37
+ console.warn(` 数据库: ${commandInfo.databaseName}`);
38
+ console.warn(` command: ${JSON.stringify(commandInfo.command)}`);
40
39
  }
41
40
  });
42
- client.on("commandFailed", function (event) {
43
- var commandInfo = runningCommands.get(event.requestId);
41
+ client.on("commandFailed", (event) => {
42
+ const commandInfo = runningCommands.get(event.requestId);
44
43
  if (!commandInfo)
45
44
  return;
46
- var duration = Date.now() - commandInfo.startTime;
45
+ const duration = Date.now() - commandInfo.startTime;
47
46
  runningCommands.delete(event.requestId);
48
47
  if (duration >= SLOW_QUERY_THRESHOLD_MS) {
49
- console.error("\u274C \u6162\u64CD\u4F5C\u68C0\u6D4B\u5230 (\u5931\u8D25):");
50
- console.error(" \u547D\u4EE4: ".concat(commandInfo.commandName));
51
- console.error(" \u8017\u65F6: ".concat(duration, " ms"));
52
- console.error(" \u9519\u8BEF: ".concat(event.failure.message));
53
- console.error(" command: ".concat(JSON.stringify(commandInfo.command)));
48
+ console.error(`❌ 慢操作检测到 (失败):`);
49
+ console.error(` 命令: ${commandInfo.commandName}`);
50
+ console.error(` 耗时: ${duration} ms`);
51
+ console.error(` 错误: ${event.failure.message}`);
52
+ console.error(` command: ${JSON.stringify(commandInfo.command)}`);
54
53
  }
55
54
  });
56
55
  }
57
- var SteedosMongoDriver = (function () {
58
- function SteedosMongoDriver(config) {
56
+ class SteedosMongoDriver {
57
+ _url;
58
+ _client;
59
+ _config;
60
+ _collections;
61
+ _encryption;
62
+ constructor(config) {
59
63
  this._collections = {};
60
64
  this._config = config;
61
65
  this._url = this.buildConnectionUrl();
62
66
  }
63
- SteedosMongoDriver.prototype.buildConnectionUrl = function () {
67
+ buildConnectionUrl() {
64
68
  if (this._config.url)
65
69
  return this._config.url;
66
- var credentialsUrlPart = this._config.username && this._config.password
67
- ? "".concat(this._config.username, ":").concat(this._config.password, "@")
70
+ const credentialsUrlPart = this._config.username && this._config.password
71
+ ? `${this._config.username}:${this._config.password}@`
68
72
  : "";
69
73
  if (!this._config.database) {
70
74
  throw new Error("Not find database");
71
75
  }
72
- return "mongodb://".concat(credentialsUrlPart).concat(this._config.host || "127.0.0.1", ":").concat(this._config.port || "27017", "/").concat(this._config.database);
73
- };
74
- SteedosMongoDriver.prototype.getSupportedColumnTypes = function () {
76
+ return `mongodb://${credentialsUrlPart}${this._config.host || "127.0.0.1"}:${this._config.port || "27017"}/${this._config.database}`;
77
+ }
78
+ getSupportedColumnTypes() {
75
79
  return [
76
80
  index_1.SteedosFieldDBType.varchar,
77
81
  index_1.SteedosFieldDBType.text,
@@ -82,118 +86,74 @@ var SteedosMongoDriver = (function () {
82
86
  index_1.SteedosFieldDBType.json,
83
87
  index_1.SteedosFieldDBType.array,
84
88
  ];
85
- };
86
- SteedosMongoDriver.prototype.encryptValue = function (value) {
87
- return tslib_1.__awaiter(this, void 0, void 0, function () {
88
- var altKeyName, encryption, encryptValue;
89
- return tslib_1.__generator(this, function (_a) {
90
- switch (_a.label) {
91
- case 0:
92
- if (!this._encryption) return [3, 2];
93
- altKeyName = (0, field_encrytion_1.getMongoFieldEncryptionConsts)().altKeyName;
94
- encryption = this._encryption;
95
- return [4, encryption.encrypt(value, {
96
- keyAltName: altKeyName,
97
- algorithm: "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic",
98
- })];
99
- case 1:
100
- encryptValue = _a.sent();
101
- return [2, encryptValue];
102
- case 2: return [2, value];
103
- }
104
- });
105
- });
106
- };
107
- SteedosMongoDriver.prototype.decryptValue = function (value) {
108
- return tslib_1.__awaiter(this, void 0, void 0, function () {
109
- var encryption, decryptValue;
110
- return tslib_1.__generator(this, function (_a) {
111
- switch (_a.label) {
112
- case 0:
113
- if (!this._encryption) return [3, 2];
114
- encryption = this._encryption;
115
- return [4, encryption.decrypt(value)];
116
- case 1:
117
- decryptValue = _a.sent();
118
- return [2, decryptValue];
119
- case 2: return [2, value];
120
- }
121
- });
122
- });
123
- };
124
- SteedosMongoDriver.prototype.connect = function () {
125
- return tslib_1.__awaiter(this, void 0, void 0, function () {
126
- var ClientEncryption, _a, keyVaultNamespace, getKMSProviders, kmsProvider, _b, _c;
127
- return tslib_1.__generator(this, function (_d) {
128
- switch (_d.label) {
129
- case 0:
130
- if (!!this._client) return [3, 6];
131
- if (!process.env.STEEDOS_CSFLE_MASTER_KEY) return [3, 3];
132
- ClientEncryption = require("mongodb-client-encryption").ClientEncryption;
133
- _a = (0, field_encrytion_1.getMongoFieldEncryptionConsts)(), keyVaultNamespace = _a.keyVaultNamespace, getKMSProviders = _a.getKMSProviders;
134
- return [4, getKMSProviders()];
135
- case 1:
136
- kmsProvider = _d.sent();
137
- _b = this;
138
- return [4, mongodb_1.MongoClient.connect(this._url, {
139
- useNewUrlParser: true,
140
- useUnifiedTopology: true,
141
- monitorCommands: true,
142
- autoEncryption: {
143
- keyVaultNamespace: keyVaultNamespace,
144
- kmsProviders: kmsProvider,
145
- bypassAutoEncryption: true,
146
- },
147
- })];
148
- case 2:
149
- _b._client = _d.sent();
150
- this._encryption = new ClientEncryption(this._client, {
151
- keyVaultNamespace: keyVaultNamespace,
152
- kmsProviders: kmsProvider,
153
- });
154
- return [3, 5];
155
- case 3:
156
- _c = this;
157
- return [4, mongodb_1.MongoClient.connect(this._url, {
158
- useNewUrlParser: true,
159
- useUnifiedTopology: true,
160
- monitorCommands: monitor_enabled,
161
- })];
162
- case 4:
163
- _c._client = _d.sent();
164
- _d.label = 5;
165
- case 5:
166
- if (monitor_enabled) {
167
- connectAndMonitor(this._client);
168
- }
169
- return [2, true];
170
- case 6: return [2];
171
- }
89
+ }
90
+ async encryptValue(value) {
91
+ if (this._encryption) {
92
+ const { altKeyName } = (0, field_encrytion_1.getMongoFieldEncryptionConsts)();
93
+ const encryption = this._encryption;
94
+ let encryptValue = await encryption.encrypt(value, {
95
+ keyAltName: altKeyName,
96
+ algorithm: "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic",
172
97
  });
173
- });
174
- };
175
- SteedosMongoDriver.prototype.getClient = function () {
98
+ return encryptValue;
99
+ }
100
+ return value;
101
+ }
102
+ async decryptValue(value) {
103
+ if (this._encryption) {
104
+ const encryption = this._encryption;
105
+ let decryptValue = await encryption.decrypt(value);
106
+ return decryptValue;
107
+ }
108
+ return value;
109
+ }
110
+ async connect() {
111
+ if (!this._client) {
112
+ if (process.env.STEEDOS_CSFLE_MASTER_KEY) {
113
+ const { ClientEncryption } = require("mongodb-client-encryption");
114
+ const { keyVaultNamespace, getKMSProviders } = (0, field_encrytion_1.getMongoFieldEncryptionConsts)();
115
+ const kmsProvider = await getKMSProviders();
116
+ this._client = await mongodb_1.MongoClient.connect(this._url, {
117
+ useNewUrlParser: true,
118
+ useUnifiedTopology: true,
119
+ monitorCommands: true,
120
+ autoEncryption: {
121
+ keyVaultNamespace: keyVaultNamespace,
122
+ kmsProviders: kmsProvider,
123
+ bypassAutoEncryption: true,
124
+ },
125
+ });
126
+ this._encryption = new ClientEncryption(this._client, {
127
+ keyVaultNamespace: keyVaultNamespace,
128
+ kmsProviders: kmsProvider,
129
+ });
130
+ }
131
+ else {
132
+ this._client = await mongodb_1.MongoClient.connect(this._url, {
133
+ useNewUrlParser: true,
134
+ useUnifiedTopology: true,
135
+ monitorCommands: monitor_enabled,
136
+ });
137
+ }
138
+ if (monitor_enabled) {
139
+ connectAndMonitor(this._client);
140
+ }
141
+ return true;
142
+ }
143
+ }
144
+ getClient() {
176
145
  return this._client;
177
- };
178
- SteedosMongoDriver.prototype.close = function () {
179
- return tslib_1.__awaiter(this, void 0, void 0, function () {
180
- return tslib_1.__generator(this, function (_a) {
181
- switch (_a.label) {
182
- case 0:
183
- if (!this._client) return [3, 2];
184
- return [4, this._client.close()];
185
- case 1:
186
- _a.sent();
187
- this._client = null;
188
- return [2, true];
189
- case 2: return [2];
190
- }
191
- });
192
- });
193
- };
194
- SteedosMongoDriver.prototype.formatFiltersToMongoQuery = function (filters) {
195
- var emptyFilters = {};
196
- var odataQuery = "";
146
+ }
147
+ async close() {
148
+ if (this._client) {
149
+ await this._client.close();
150
+ this._client = null;
151
+ return true;
152
+ }
153
+ }
154
+ formatFiltersToMongoQuery(filters) {
155
+ let emptyFilters = {};
156
+ let odataQuery = "";
197
157
  if (_.isString(filters)) {
198
158
  odataQuery = filters;
199
159
  }
@@ -203,11 +163,11 @@ var SteedosMongoDriver = (function () {
203
163
  if (!odataQuery) {
204
164
  return emptyFilters;
205
165
  }
206
- var query = (0, odata_v4_mongodb_1.createFilter)(odataQuery);
166
+ let query = (0, odata_v4_mongodb_1.createFilter)(odataQuery);
207
167
  return query;
208
- };
209
- SteedosMongoDriver.prototype.getMongoFilters = function (filters) {
210
- var emptyFilters = {};
168
+ }
169
+ getMongoFilters(filters) {
170
+ let emptyFilters = {};
211
171
  if (_.isUndefined(filters)) {
212
172
  return emptyFilters;
213
173
  }
@@ -217,63 +177,63 @@ var SteedosMongoDriver = (function () {
217
177
  if (_.isArray(filters) && !filters.length) {
218
178
  return emptyFilters;
219
179
  }
220
- var mongoFilters = this.formatFiltersToMongoQuery(filters);
180
+ let mongoFilters = this.formatFiltersToMongoQuery(filters);
221
181
  return mongoFilters;
222
- };
223
- SteedosMongoDriver.prototype.getMongoFieldsOptions = function (fields) {
182
+ }
183
+ getMongoFieldsOptions(fields) {
224
184
  if (typeof fields == "string") {
225
- fields = fields.split(",").map(function (n) {
185
+ fields = fields.split(",").map((n) => {
226
186
  return n.trim();
227
187
  });
228
188
  }
229
189
  if (!(fields && fields.length)) {
230
190
  return {};
231
191
  }
232
- var projection = {};
233
- fields.forEach(function (field) {
192
+ let projection = {};
193
+ fields.forEach((field) => {
234
194
  if (field) {
235
195
  projection[field] = 1;
236
196
  }
237
197
  });
238
198
  return projection;
239
- };
240
- SteedosMongoDriver.prototype.getMongoSortOptions = function (sort) {
241
- var result = undefined;
199
+ }
200
+ getMongoSortOptions(sort) {
201
+ let result = undefined;
242
202
  if (sort && typeof sort === "string") {
243
- var arraySort = sort.split(",").map(function (n) {
203
+ let arraySort = sort.split(",").map((n) => {
244
204
  return n.trim();
245
205
  });
246
- var stringSort_1 = "";
247
- arraySort.forEach(function (n) {
206
+ let stringSort = "";
207
+ arraySort.forEach((n) => {
248
208
  if (n) {
249
- stringSort_1 += "".concat(n, ",");
209
+ stringSort += `${n},`;
250
210
  }
251
211
  });
252
- stringSort_1 = stringSort_1.replace(/,$/g, "");
253
- result = (0, odata_v4_mongodb_1.createQuery)("$orderby=".concat(stringSort_1)).sort;
212
+ stringSort = stringSort.replace(/,$/g, "");
213
+ result = (0, odata_v4_mongodb_1.createQuery)(`$orderby=${stringSort}`).sort;
254
214
  }
255
215
  return result;
256
- };
257
- SteedosMongoDriver.prototype.getMongoOptions = function (options) {
216
+ }
217
+ getMongoOptions(options) {
258
218
  if (_.isUndefined(options)) {
259
219
  return {};
260
220
  }
261
- var result = {};
262
- var projection = this.getMongoFieldsOptions(options.fields);
263
- var sort = this.getMongoSortOptions(options.sort);
221
+ let result = {};
222
+ let projection = this.getMongoFieldsOptions(options.fields);
223
+ let sort = this.getMongoSortOptions(options.sort);
264
224
  result.projection = projection;
265
225
  result.sort = sort;
266
226
  result.limit = options.top;
267
227
  result.skip = options.skip;
268
228
  return result;
269
- };
270
- SteedosMongoDriver.prototype.getAggregateOptions = function (options) {
229
+ }
230
+ getAggregateOptions(options) {
271
231
  if (_.isUndefined(options)) {
272
232
  return [];
273
233
  }
274
- var result = [];
275
- var projection = this.getMongoFieldsOptions(options.fields);
276
- var sort = this.getMongoSortOptions(options.sort);
234
+ let result = [];
235
+ let projection = this.getMongoFieldsOptions(options.fields);
236
+ let sort = this.getMongoSortOptions(options.sort);
277
237
  if (!_.isEmpty(projection)) {
278
238
  result.push({ $project: projection });
279
239
  }
@@ -287,345 +247,200 @@ var SteedosMongoDriver = (function () {
287
247
  result.push({ $limit: options.top });
288
248
  }
289
249
  return result;
290
- };
291
- SteedosMongoDriver.prototype.collection = function (name) {
250
+ }
251
+ collection(name) {
292
252
  if (!this._collections[name]) {
293
- var db = this._client.db();
253
+ const db = this._client.db();
294
254
  this._collections[name] = db.collection(name);
295
255
  }
296
256
  return this._collections[name];
297
- };
298
- SteedosMongoDriver.prototype.find = function (tableName, query) {
299
- return tslib_1.__awaiter(this, void 0, void 0, function () {
300
- var collection, mongoFilters, mongoOptions, result;
301
- return tslib_1.__generator(this, function (_a) {
302
- switch (_a.label) {
303
- case 0: return [4, this.connect()];
304
- case 1:
305
- _a.sent();
306
- collection = this.collection(tableName);
307
- mongoFilters = this.getMongoFilters(query.filters);
308
- mongoOptions = this.getMongoOptions(query);
309
- return [4, collection.find(mongoFilters, mongoOptions).toArray()];
310
- case 2:
311
- result = _a.sent();
312
- return [2, result];
313
- }
314
- });
315
- });
316
- };
317
- SteedosMongoDriver.prototype.aggregate = function (tableName, query, externalPipeline) {
318
- return tslib_1.__awaiter(this, void 0, void 0, function () {
319
- var collection, pipeline, mongoFilters, aggregateOptions, result;
320
- return tslib_1.__generator(this, function (_a) {
321
- switch (_a.label) {
322
- case 0: return [4, this.connect()];
323
- case 1:
324
- _a.sent();
325
- collection = this.collection(tableName);
326
- pipeline = [];
327
- mongoFilters = this.getMongoFilters(query.filters);
328
- aggregateOptions = this.getAggregateOptions(query);
329
- pipeline.push({ $match: mongoFilters });
330
- pipeline = pipeline.concat(aggregateOptions).concat(externalPipeline);
331
- return [4, collection.aggregate(pipeline).toArray()];
332
- case 2:
333
- result = _a.sent();
334
- return [2, result];
335
- }
336
- });
337
- });
338
- };
339
- SteedosMongoDriver.prototype.directAggregate = function (tableName, query, externalPipeline, userId) {
340
- return tslib_1.__awaiter(this, void 0, void 0, function () {
341
- var collection, pipeline, mongoFilters, aggregateOptions, result;
342
- return tslib_1.__generator(this, function (_a) {
343
- switch (_a.label) {
344
- case 0: return [4, this.connect()];
345
- case 1:
346
- _a.sent();
347
- collection = this.collection(tableName);
348
- pipeline = [];
349
- if (query.filters) {
350
- mongoFilters = this.getMongoFilters(query.filters);
351
- aggregateOptions = this.getAggregateOptions(query);
352
- pipeline.push({ $match: mongoFilters });
353
- pipeline = pipeline.concat(aggregateOptions);
354
- }
355
- pipeline = pipeline.concat(externalPipeline);
356
- return [4, collection.aggregate(pipeline).toArray()];
357
- case 2:
358
- result = _a.sent();
359
- return [2, result];
360
- }
361
- });
362
- });
363
- };
364
- SteedosMongoDriver.prototype.directAggregatePrefixalPipeline = function (tableName, query, prefixalPipeline, userId) {
365
- return tslib_1.__awaiter(this, void 0, void 0, function () {
366
- var collection, pipeline, mongoFilters, aggregateOptions, result;
367
- return tslib_1.__generator(this, function (_a) {
368
- switch (_a.label) {
369
- case 0: return [4, this.connect()];
370
- case 1:
371
- _a.sent();
372
- collection = this.collection(tableName);
373
- pipeline = [];
374
- mongoFilters = this.getMongoFilters(query.filters);
375
- aggregateOptions = this.getAggregateOptions(query);
376
- pipeline.push({ $match: mongoFilters });
377
- pipeline = prefixalPipeline.concat(pipeline).concat(aggregateOptions);
378
- return [4, collection.aggregate(pipeline).toArray()];
379
- case 2:
380
- result = _a.sent();
381
- return [2, result];
382
- }
383
- });
384
- });
385
- };
386
- SteedosMongoDriver.prototype.count = function (tableName, query) {
387
- return tslib_1.__awaiter(this, void 0, void 0, function () {
388
- var collection, mongoFilters, mongoOptions, result;
389
- return tslib_1.__generator(this, function (_a) {
390
- switch (_a.label) {
391
- case 0: return [4, this.connect()];
392
- case 1:
393
- _a.sent();
394
- collection = this.collection(tableName);
395
- mongoFilters = this.getMongoFilters(query.filters);
396
- mongoOptions = this.getMongoOptions(query);
397
- return [4, collection.find(mongoFilters, mongoOptions).count()];
398
- case 2:
399
- result = _a.sent();
400
- return [2, result];
401
- }
402
- });
403
- });
404
- };
405
- SteedosMongoDriver.prototype.findOne = function (tableName, id, query) {
406
- return tslib_1.__awaiter(this, void 0, void 0, function () {
407
- var collection, mongoOptions, mongoFilters, selector, result;
408
- return tslib_1.__generator(this, function (_a) {
409
- switch (_a.label) {
410
- case 0: return [4, this.connect()];
411
- case 1:
412
- _a.sent();
413
- collection = this.collection(tableName);
414
- mongoOptions = this.getMongoOptions(query);
415
- mongoFilters = this.getMongoFilters(query.filters);
416
- selector = { _id: id };
417
- if (_.isObject(id)) {
418
- selector = this.getMongoFilters(id["filters"]);
419
- }
420
- if (!_.isEmpty(mongoFilters)) {
421
- selector = Object.assign(mongoFilters, selector);
422
- }
423
- return [4, collection.findOne(selector, mongoOptions)];
424
- case 2:
425
- result = _a.sent();
426
- return [2, result];
427
- }
428
- });
429
- });
430
- };
431
- SteedosMongoDriver.prototype.insert = function (tableName, data) {
432
- return tslib_1.__awaiter(this, void 0, void 0, function () {
433
- var collection, result;
434
- return tslib_1.__generator(this, function (_a) {
435
- switch (_a.label) {
436
- case 0: return [4, this.connect()];
437
- case 1:
438
- _a.sent();
439
- data._id = data._id || new mongodb_1.ObjectId().toHexString();
440
- collection = this.collection(tableName);
441
- return [4, collection.insertOne(data)];
442
- case 2:
443
- result = _a.sent();
444
- return [2, result.ops[0]];
445
- }
446
- });
447
- });
448
- };
449
- SteedosMongoDriver.prototype.update = function (tableName, id, data) {
450
- return tslib_1.__awaiter(this, void 0, void 0, function () {
451
- var collection, selector, options, keys, result;
452
- return tslib_1.__generator(this, function (_a) {
453
- switch (_a.label) {
454
- case 0:
455
- if (_.isEmpty(data)) {
456
- throw new Error("the params 'data' must not be empty");
457
- }
458
- return [4, this.connect()];
459
- case 1:
460
- _a.sent();
461
- collection = this.collection(tableName);
462
- if (_.isObject(id)) {
463
- selector = this.getMongoFilters(id["filters"]);
464
- }
465
- else {
466
- selector = { _id: id };
467
- }
468
- options = { $set: {}, $unset: {} };
469
- keys = _.keys(data);
470
- _.each(keys, function (key) {
471
- if (_.include([
472
- "$inc",
473
- "$min",
474
- "$max",
475
- "$mul",
476
- "$push",
477
- "$addToSet",
478
- "$pop",
479
- "$pull",
480
- "$pullAll",
481
- ], key)) {
482
- options[key] = data[key];
483
- }
484
- else {
485
- if (null === data[key]) {
486
- options.$unset[key] = 1;
487
- }
488
- else {
489
- options.$set[key] = data[key];
490
- }
491
- }
492
- });
493
- if (_.isEmpty(options.$set)) {
494
- delete options.$set;
495
- }
496
- if (_.isEmpty(options.$unset)) {
497
- delete options.$unset;
498
- }
499
- return [4, collection.updateOne(selector, options)];
500
- case 2:
501
- result = _a.sent();
502
- if (!result.result.ok) return [3, 4];
503
- return [4, collection.findOne(selector)];
504
- case 3:
505
- result = _a.sent();
506
- return [2, result];
507
- case 4: return [2];
508
- }
509
- });
510
- });
511
- };
512
- SteedosMongoDriver.prototype.updateOne = function (tableName, id, data) {
513
- return tslib_1.__awaiter(this, void 0, void 0, function () {
514
- var collection, selector, result;
515
- return tslib_1.__generator(this, function (_a) {
516
- switch (_a.label) {
517
- case 0:
518
- if (_.isEmpty(data)) {
519
- throw new Error("the params 'data' must not be empty");
520
- }
521
- return [4, this.connect()];
522
- case 1:
523
- _a.sent();
524
- collection = this.collection(tableName);
525
- if (_.isObject(id)) {
526
- selector = this.getMongoFilters(id["filters"]);
527
- }
528
- else {
529
- selector = { _id: id };
530
- }
531
- return [4, collection.updateOne(selector, { $set: data })];
532
- case 2:
533
- result = _a.sent();
534
- if (!result.result.ok) return [3, 4];
535
- return [4, collection.findOne(selector)];
536
- case 3:
537
- result = _a.sent();
538
- return [2, result];
539
- case 4: return [2];
540
- }
541
- });
542
- });
543
- };
544
- SteedosMongoDriver.prototype.updateMany = function (tableName, queryFilters, data) {
545
- return tslib_1.__awaiter(this, void 0, void 0, function () {
546
- var collection, mongoFilters;
547
- return tslib_1.__generator(this, function (_a) {
548
- switch (_a.label) {
549
- case 0:
550
- if (_.isEmpty(data)) {
551
- throw new Error("the params 'data' must not be empty");
552
- }
553
- return [4, this.connect()];
554
- case 1:
555
- _a.sent();
556
- collection = this.collection(tableName);
557
- mongoFilters = this.getMongoFilters(queryFilters);
558
- return [4, collection.update(mongoFilters, { $set: data }, { multi: true })];
559
- case 2: return [2, _a.sent()];
257
+ }
258
+ async find(tableName, query) {
259
+ await this.connect();
260
+ const collection = this.collection(tableName);
261
+ const mongoFilters = this.getMongoFilters(query.filters);
262
+ const mongoOptions = this.getMongoOptions(query);
263
+ const result = await collection.find(mongoFilters, mongoOptions).toArray();
264
+ return result;
265
+ }
266
+ async aggregate(tableName, query, externalPipeline) {
267
+ await this.connect();
268
+ const collection = this.collection(tableName);
269
+ let pipeline = [];
270
+ const mongoFilters = this.getMongoFilters(query.filters);
271
+ const aggregateOptions = this.getAggregateOptions(query);
272
+ pipeline.push({ $match: mongoFilters });
273
+ pipeline = pipeline.concat(aggregateOptions).concat(externalPipeline);
274
+ const result = await collection.aggregate(pipeline).toArray();
275
+ return result;
276
+ }
277
+ async directAggregate(tableName, query, externalPipeline, userId) {
278
+ await this.connect();
279
+ let collection = this.collection(tableName);
280
+ let pipeline = [];
281
+ if (query.filters) {
282
+ let mongoFilters = this.getMongoFilters(query.filters);
283
+ let aggregateOptions = this.getAggregateOptions(query);
284
+ pipeline.push({ $match: mongoFilters });
285
+ pipeline = pipeline.concat(aggregateOptions);
286
+ }
287
+ pipeline = pipeline.concat(externalPipeline);
288
+ let result = await collection.aggregate(pipeline).toArray();
289
+ return result;
290
+ }
291
+ async directAggregatePrefixalPipeline(tableName, query, prefixalPipeline, userId) {
292
+ await this.connect();
293
+ let collection = this.collection(tableName);
294
+ let pipeline = [];
295
+ let mongoFilters = this.getMongoFilters(query.filters);
296
+ let aggregateOptions = this.getAggregateOptions(query);
297
+ pipeline.push({ $match: mongoFilters });
298
+ pipeline = prefixalPipeline.concat(pipeline).concat(aggregateOptions);
299
+ let result = await collection.aggregate(pipeline).toArray();
300
+ return result;
301
+ }
302
+ async count(tableName, query) {
303
+ await this.connect();
304
+ let collection = this.collection(tableName);
305
+ let mongoFilters = this.getMongoFilters(query.filters);
306
+ let mongoOptions = this.getMongoOptions(query);
307
+ let result = await collection.find(mongoFilters, mongoOptions).count();
308
+ return result;
309
+ }
310
+ async findOne(tableName, id, query) {
311
+ await this.connect();
312
+ let collection = this.collection(tableName);
313
+ let mongoOptions = this.getMongoOptions(query);
314
+ let mongoFilters = this.getMongoFilters(query.filters);
315
+ let selector = { _id: id };
316
+ if (_.isObject(id)) {
317
+ selector = this.getMongoFilters(id["filters"]);
318
+ }
319
+ if (!_.isEmpty(mongoFilters)) {
320
+ selector = Object.assign(mongoFilters, selector);
321
+ }
322
+ let result = await collection.findOne(selector, mongoOptions);
323
+ return result;
324
+ }
325
+ async insert(tableName, data) {
326
+ await this.connect();
327
+ data._id = data._id || new mongodb_1.ObjectId().toHexString();
328
+ let collection = this.collection(tableName);
329
+ let result = await collection.insertOne(data);
330
+ return result.ops[0];
331
+ }
332
+ async update(tableName, id, data) {
333
+ if (_.isEmpty(data)) {
334
+ throw new Error("the params 'data' must not be empty");
335
+ }
336
+ await this.connect();
337
+ let collection = this.collection(tableName);
338
+ let selector;
339
+ if (_.isObject(id)) {
340
+ selector = this.getMongoFilters(id["filters"]);
341
+ }
342
+ else {
343
+ selector = { _id: id };
344
+ }
345
+ const options = { $set: {}, $unset: {} };
346
+ const keys = _.keys(data);
347
+ _.each(keys, function (key) {
348
+ if (_.include([
349
+ "$inc",
350
+ "$min",
351
+ "$max",
352
+ "$mul",
353
+ "$push",
354
+ "$addToSet",
355
+ "$pop",
356
+ "$pull",
357
+ "$pullAll",
358
+ ], key)) {
359
+ options[key] = data[key];
360
+ }
361
+ else {
362
+ if (null === data[key]) {
363
+ options.$unset[key] = 1;
560
364
  }
561
- });
562
- });
563
- };
564
- SteedosMongoDriver.prototype.delete = function (tableName, id) {
565
- return tslib_1.__awaiter(this, void 0, void 0, function () {
566
- var collection, selector, result;
567
- return tslib_1.__generator(this, function (_a) {
568
- switch (_a.label) {
569
- case 0: return [4, this.connect()];
570
- case 1:
571
- _a.sent();
572
- collection = this.collection(tableName);
573
- if (_.isObject(id)) {
574
- selector = this.getMongoFilters(id["filters"]);
575
- }
576
- else {
577
- selector = { _id: id };
578
- }
579
- return [4, collection.deleteOne(selector)];
580
- case 2:
581
- result = _a.sent();
582
- return [2, result === null || result === void 0 ? void 0 : result.deletedCount];
365
+ else {
366
+ options.$set[key] = data[key];
583
367
  }
584
- });
585
- });
586
- };
587
- SteedosMongoDriver.prototype.directFind = function (tableName, query) {
588
- return tslib_1.__awaiter(this, void 0, void 0, function () {
589
- return tslib_1.__generator(this, function (_a) {
590
- return [2, this.find(tableName, query)];
591
- });
592
- });
593
- };
594
- SteedosMongoDriver.prototype.directInsert = function (tableName, data) {
595
- return tslib_1.__awaiter(this, void 0, void 0, function () {
596
- return tslib_1.__generator(this, function (_a) {
597
- return [2, this.insert(tableName, data)];
598
- });
599
- });
600
- };
601
- SteedosMongoDriver.prototype.directUpdate = function (tableName, id, data) {
602
- return tslib_1.__awaiter(this, void 0, void 0, function () {
603
- return tslib_1.__generator(this, function (_a) {
604
- return [2, this.update(tableName, id, data)];
605
- });
606
- });
607
- };
608
- SteedosMongoDriver.prototype.directDelete = function (tableName, id) {
609
- return tslib_1.__awaiter(this, void 0, void 0, function () {
610
- return tslib_1.__generator(this, function (_a) {
611
- return [2, this.delete(tableName, id)];
612
- });
613
- });
614
- };
615
- SteedosMongoDriver.prototype.directUpdateMany = function (tableName, queryFilters, data) {
616
- return tslib_1.__awaiter(this, void 0, void 0, function () {
617
- return tslib_1.__generator(this, function (_a) {
618
- return [2, this.updateMany(tableName, queryFilters, data)];
619
- });
368
+ }
620
369
  });
621
- };
622
- SteedosMongoDriver.prototype._makeNewID = function (tableName) {
370
+ if (_.isEmpty(options.$set)) {
371
+ delete options.$set;
372
+ }
373
+ if (_.isEmpty(options.$unset)) {
374
+ delete options.$unset;
375
+ }
376
+ let result = await collection.updateOne(selector, options);
377
+ if (result.result.ok) {
378
+ result = await collection.findOne(selector);
379
+ return result;
380
+ }
381
+ }
382
+ async updateOne(tableName, id, data) {
383
+ if (_.isEmpty(data)) {
384
+ throw new Error("the params 'data' must not be empty");
385
+ }
386
+ await this.connect();
387
+ let collection = this.collection(tableName);
388
+ let selector;
389
+ if (_.isObject(id)) {
390
+ selector = this.getMongoFilters(id["filters"]);
391
+ }
392
+ else {
393
+ selector = { _id: id };
394
+ }
395
+ let result = await collection.updateOne(selector, { $set: data });
396
+ if (result.result.ok) {
397
+ result = await collection.findOne(selector);
398
+ return result;
399
+ }
400
+ }
401
+ async updateMany(tableName, queryFilters, data) {
402
+ if (_.isEmpty(data)) {
403
+ throw new Error("the params 'data' must not be empty");
404
+ }
405
+ await this.connect();
406
+ let collection = this.collection(tableName);
407
+ let mongoFilters = this.getMongoFilters(queryFilters);
408
+ return await collection.update(mongoFilters, { $set: data }, { multi: true });
409
+ }
410
+ async delete(tableName, id) {
411
+ await this.connect();
412
+ let collection = this.collection(tableName);
413
+ let selector;
414
+ if (_.isObject(id)) {
415
+ selector = this.getMongoFilters(id["filters"]);
416
+ }
417
+ else {
418
+ selector = { _id: id };
419
+ }
420
+ const result = await collection.deleteOne(selector);
421
+ return result?.deletedCount;
422
+ }
423
+ async directFind(tableName, query) {
424
+ return this.find(tableName, query);
425
+ }
426
+ async directInsert(tableName, data) {
427
+ return this.insert(tableName, data);
428
+ }
429
+ async directUpdate(tableName, id, data) {
430
+ return this.update(tableName, id, data);
431
+ }
432
+ async directDelete(tableName, id) {
433
+ return this.delete(tableName, id);
434
+ }
435
+ async directUpdateMany(tableName, queryFilters, data) {
436
+ return this.updateMany(tableName, queryFilters, data);
437
+ }
438
+ _makeNewID(tableName) {
623
439
  return new mongodb_1.ObjectId().toHexString();
624
- };
625
- SteedosMongoDriver.prototype.formatRecord = function (doc, objectConfig) {
440
+ }
441
+ formatRecord(doc, objectConfig) {
626
442
  return (0, format_1.formatRecord)(doc, objectConfig);
627
- };
628
- return SteedosMongoDriver;
629
- }());
443
+ }
444
+ }
630
445
  exports.SteedosMongoDriver = SteedosMongoDriver;
631
446
  //# sourceMappingURL=mongo.js.map