@e-mc/db 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +11 -0
- package/README.md +5 -0
- package/index.d.ts +5 -0
- package/index.js +288 -0
- package/mongodb/index.d.ts +19 -0
- package/mongodb/index.js +679 -0
- package/mssql/index.d.ts +7 -0
- package/mssql/index.js +505 -0
- package/mysql/index.d.ts +10 -0
- package/mysql/index.js +332 -0
- package/oracle/index.d.ts +7 -0
- package/oracle/index.js +358 -0
- package/package.json +26 -0
- package/pool.d.ts +5 -0
- package/pool.js +144 -0
- package/postgres/index.d.ts +7 -0
- package/postgres/index.js +272 -0
- package/redis/index.d.ts +7 -0
- package/redis/index.js +550 -0
- package/types.d.ts +14 -0
- package/util.d.ts +17 -0
- package/util.js +106 -0
package/mongodb/index.js
ADDED
|
@@ -0,0 +1,679 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DB_SOURCE_TYPE = exports.DB_SOURCE_CLIENT = exports.getSortValue = exports.getFilterValue = exports.checkTimeout = exports.executeBatchQuery = exports.executeQuery = exports.setCredential = void 0;
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const types_1 = require("../../types");
|
|
6
|
+
const util_1 = require("../util");
|
|
7
|
+
const index_1 = require("../index");
|
|
8
|
+
const pool_1 = require("../pool");
|
|
9
|
+
const request_1 = require("../../request");
|
|
10
|
+
const MONGODB_V3 = index_1.default.checkSemVer("mongodb" /* STRINGS.PACKAGE_NAME */, 1, 4);
|
|
11
|
+
const MONGODB_V330 = index_1.default.checkSemVer("mongodb" /* STRINGS.PACKAGE_NAME */, '3.3.0');
|
|
12
|
+
const MONGODB_V333 = index_1.default.checkSemVer("mongodb" /* STRINGS.PACKAGE_NAME */, '3.3.0', '3.3.3');
|
|
13
|
+
const MONGODB_V360 = index_1.default.checkSemVer("mongodb" /* STRINGS.PACKAGE_NAME */, '3.6.0');
|
|
14
|
+
const MONGODB_TOPOLOGY = MONGODB_V3 && MONGODB_V330;
|
|
15
|
+
const MONGODB_NOPOOL = MONGODB_V3 && !MONGODB_V330;
|
|
16
|
+
class MongoDBPool extends pool_1.default {
|
|
17
|
+
getConnection() {
|
|
18
|
+
return this.client.connect();
|
|
19
|
+
}
|
|
20
|
+
detach(force) {
|
|
21
|
+
this.remove();
|
|
22
|
+
return this.closed ? Promise.resolve() : this.close(force);
|
|
23
|
+
}
|
|
24
|
+
close(force = false) {
|
|
25
|
+
return this.client.close(force);
|
|
26
|
+
}
|
|
27
|
+
isEmpty() {
|
|
28
|
+
if (this.closed) {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
const topology = this.client.topology;
|
|
32
|
+
if (topology) {
|
|
33
|
+
if (MONGODB_V360) {
|
|
34
|
+
const name = Object.getOwnPropertySymbols(topology).find(sym => sym.toString() === 'Symbol(waitQueue)');
|
|
35
|
+
if (name) {
|
|
36
|
+
return (0, util_1.checkEmpty)(topology[name]);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return (0, util_1.checkEmpty)(topology.s?.sessions);
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
get closeable() {
|
|
44
|
+
const topology = this.client.topology;
|
|
45
|
+
return topology ? super.closeable || !MONGODB_V333 && topology.s?.state === 'closing' : true;
|
|
46
|
+
}
|
|
47
|
+
get closed() {
|
|
48
|
+
const topology = this.client.topology;
|
|
49
|
+
if (topology && (this.success > 0 || this.failed > 0)) {
|
|
50
|
+
if (MONGODB_V333) {
|
|
51
|
+
return topology.s.servers.size === 0;
|
|
52
|
+
}
|
|
53
|
+
if (typeof topology.isDestroyed === 'function') {
|
|
54
|
+
return topology.isDestroyed();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return this.client.s?.hasBeenClosed === true;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const POOL_STATE = {};
|
|
61
|
+
function removePoolProperties(credential) {
|
|
62
|
+
let options = credential.options;
|
|
63
|
+
if (options && !credential.uuidKey) {
|
|
64
|
+
if (MONGODB_V360) {
|
|
65
|
+
if ('maxPoolSize' in options || 'minPoolSize' in options || 'maxConnecting' in options || 'maxIdleTimeMS' in options || 'waitQueueTimeoutMS' in options || 'serverSelectionTimeoutMS' in options) {
|
|
66
|
+
options = { ...options, maxPoolSize: undefined, minPoolSize: undefined, maxConnecting: undefined, maxIdleTimeMS: undefined, waitQueueTimeoutMS: undefined, serverSelectionTimeoutMS: undefined };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
else if ('poolSize' in options || 'minSize' in options) {
|
|
70
|
+
options = { ...options, poolSize: undefined, minSize: undefined };
|
|
71
|
+
}
|
|
72
|
+
return { uri: credential.uri, options };
|
|
73
|
+
}
|
|
74
|
+
return credential;
|
|
75
|
+
}
|
|
76
|
+
function getCacheObject(item) {
|
|
77
|
+
const credential = item.credential;
|
|
78
|
+
return { uri: item.uri, options: item.options, uuidKey: credential?.uuidKey };
|
|
79
|
+
}
|
|
80
|
+
function getPoolKey(credential) {
|
|
81
|
+
const options = credential.options;
|
|
82
|
+
if (options && credential.uri) {
|
|
83
|
+
const auth = options.auth;
|
|
84
|
+
let result = credential.uri + (auth ? '_' + (auth.username || '') + '@' + (auth.password || '') : '') + '_' + (options.authMechanism || '') + index_1.default.asString(options.authMechanismProperties);
|
|
85
|
+
if (options.tls) {
|
|
86
|
+
const { tlsCertificateKeyFilePassword = '', tlsAllowInvalidHostnames, tlsAllowInvalidCertificates, tlsInsecure } = options;
|
|
87
|
+
let { tlsCertificateFile = '', tlsCertificateKeyFile = '', tlsCAFile = '' } = options;
|
|
88
|
+
if (MONGODB_V3) {
|
|
89
|
+
tlsCertificateFile = '';
|
|
90
|
+
tlsCertificateKeyFile = checkBuffer(tlsCertificateKeyFile);
|
|
91
|
+
tlsCAFile = checkBuffer(tlsCAFile);
|
|
92
|
+
}
|
|
93
|
+
result += tlsCertificateFile + tlsCertificateKeyFile + tlsCAFile + tlsCertificateKeyFilePassword + (tlsAllowInvalidHostnames ? '1' : '0') + (tlsAllowInvalidCertificates ? '1' : '0') + (tlsInsecure ? '1' : '0');
|
|
94
|
+
}
|
|
95
|
+
else if (options.ssl) {
|
|
96
|
+
let { sslCert = '', sslKey = '', sslCA = '' } = options;
|
|
97
|
+
if (MONGODB_V3) {
|
|
98
|
+
sslCert = checkBuffer(sslCert);
|
|
99
|
+
sslKey = checkBuffer(sslKey);
|
|
100
|
+
sslCA = checkBuffer(sslCA);
|
|
101
|
+
}
|
|
102
|
+
result += sslCert + sslKey + sslCA + (options.sslPass || '') + (options.sslValidate !== false ? '1' : '0');
|
|
103
|
+
}
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function checkBuffer(value) {
|
|
108
|
+
if (typeof value === 'string') {
|
|
109
|
+
return value;
|
|
110
|
+
}
|
|
111
|
+
if (Buffer.isBuffer(value)) {
|
|
112
|
+
return value.toString('utf-8');
|
|
113
|
+
}
|
|
114
|
+
if (Array.isArray(value)) {
|
|
115
|
+
const items = value.map(item => checkBuffer(item)).filter(item => item);
|
|
116
|
+
if (items.length) {
|
|
117
|
+
return items.join('');
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return '';
|
|
121
|
+
}
|
|
122
|
+
function setCredential(item) {
|
|
123
|
+
const credential = this.getCredential(item);
|
|
124
|
+
const options = item.options || (item.options = {});
|
|
125
|
+
let uri = item.uri;
|
|
126
|
+
if (credential) {
|
|
127
|
+
let { protocol, server, hostname, port, username, password } = (0, util_1.parseServerAuth)(credential, 27017 /* VALUES.PORT */), { auth, authMechanism = 'DEFAULT', authSource, authMechanismProperties } = credential, authentication = '';
|
|
128
|
+
if (authMechanism === 'GSSAPI') {
|
|
129
|
+
const principal = auth?.username || username || options.auth?.username;
|
|
130
|
+
if ((0, types_1.isString)(principal)) {
|
|
131
|
+
authentication = encodeURIComponent(principal) + '@';
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
else if (auth?.username) {
|
|
135
|
+
if (!MONGODB_V3) {
|
|
136
|
+
options.auth = auth;
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
username = auth.username;
|
|
140
|
+
password = auth.password;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (!uri || !index_1.default.isURL(uri)) {
|
|
144
|
+
if (MONGODB_V3 || !options.auth) {
|
|
145
|
+
authentication = (0, util_1.getBasicAuth)(username, password);
|
|
146
|
+
}
|
|
147
|
+
if (!server && hostname) {
|
|
148
|
+
server = hostname + ':' + (port || 27017 /* VALUES.PORT */);
|
|
149
|
+
}
|
|
150
|
+
if (!server) {
|
|
151
|
+
throw (0, types_1.errorMessage)("mongodb" /* STRINGS.MODULE_NAME */, 'Not defined - server address');
|
|
152
|
+
}
|
|
153
|
+
uri = (protocol || 'mongodb:') + '//' + authentication + server;
|
|
154
|
+
}
|
|
155
|
+
else if (!MONGODB_V3) {
|
|
156
|
+
options.auth || (options.auth = { username, password });
|
|
157
|
+
}
|
|
158
|
+
else if (!(0, util_1.hasBasicAuth)(uri) && (authentication || (authentication = (0, util_1.getBasicAuth)(username, password)))) {
|
|
159
|
+
const url = uri.split('://');
|
|
160
|
+
uri = url[0] + '://' + authentication + url[1];
|
|
161
|
+
}
|
|
162
|
+
if (MONGODB_V3 && 'auth' in options) {
|
|
163
|
+
delete options.auth;
|
|
164
|
+
}
|
|
165
|
+
if (uri.indexOf('?') === -1) {
|
|
166
|
+
if (uri[uri.length - 1] !== '/') {
|
|
167
|
+
uri += '/';
|
|
168
|
+
}
|
|
169
|
+
uri += '?';
|
|
170
|
+
}
|
|
171
|
+
uri += (uri[uri.length - 1] !== '?' ? '&' : '') + 'authMechanism=' + encodeURIComponent(authMechanism);
|
|
172
|
+
switch (authMechanism) {
|
|
173
|
+
case 'MONGODB-X509': {
|
|
174
|
+
const checkFile = (value) => {
|
|
175
|
+
if ((0, types_1.isString)(value)) {
|
|
176
|
+
if (request_1.default.isCert(value = value.trim())) {
|
|
177
|
+
return MONGODB_V3 ? value : '';
|
|
178
|
+
}
|
|
179
|
+
if (index_1.default.isPath(value = path.resolve(value)) && this.canRead(value, { ownPermissionOnly: true })) {
|
|
180
|
+
return MONGODB_V3 ? index_1.default.readText(value, true) : value;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return '';
|
|
184
|
+
};
|
|
185
|
+
let { tlsCertificateFile, tlsCertificateKeyFile, tlsCAFile } = credential, valid;
|
|
186
|
+
if (tlsCertificateFile || tlsCertificateKeyFile) {
|
|
187
|
+
if (!MONGODB_V3 && (tlsCertificateFile = checkFile(tlsCertificateFile))) {
|
|
188
|
+
options.tlsCertificateFile = tlsCertificateFile;
|
|
189
|
+
valid = true;
|
|
190
|
+
}
|
|
191
|
+
if (tlsCertificateKeyFile = checkFile(tlsCertificateKeyFile)) {
|
|
192
|
+
options.tlsCertificateKeyFile = tlsCertificateKeyFile;
|
|
193
|
+
valid = true;
|
|
194
|
+
}
|
|
195
|
+
if (valid) {
|
|
196
|
+
const { tlsCertificateKeyFilePassword, tlsAllowInvalidHostnames, tlsAllowInvalidCertificates, tlsInsecure } = credential;
|
|
197
|
+
if (tlsCAFile = checkFile(tlsCAFile)) {
|
|
198
|
+
options.tlsCAFile = tlsCAFile;
|
|
199
|
+
}
|
|
200
|
+
if (tlsCertificateKeyFilePassword) {
|
|
201
|
+
options.tlsCertificateKeyFilePassword = tlsCertificateKeyFilePassword;
|
|
202
|
+
}
|
|
203
|
+
if (typeof tlsAllowInvalidHostnames === 'boolean') {
|
|
204
|
+
options.tlsAllowInvalidHostnames = tlsAllowInvalidHostnames;
|
|
205
|
+
}
|
|
206
|
+
if (typeof tlsAllowInvalidCertificates === 'boolean') {
|
|
207
|
+
options.tlsAllowInvalidCertificates = tlsAllowInvalidCertificates;
|
|
208
|
+
}
|
|
209
|
+
if (typeof tlsInsecure === 'boolean') {
|
|
210
|
+
options.tlsInsecure = tlsInsecure;
|
|
211
|
+
}
|
|
212
|
+
options.tls = true;
|
|
213
|
+
uri += '&tls=true';
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
let { sslCert, sslKey, sslCA } = credential;
|
|
218
|
+
if (sslCert = checkFile(sslCert)) {
|
|
219
|
+
options.sslCert = sslCert;
|
|
220
|
+
if (sslKey = checkFile(sslKey)) {
|
|
221
|
+
options.sslKey = sslKey;
|
|
222
|
+
}
|
|
223
|
+
if (MONGODB_V3) {
|
|
224
|
+
if (sslCA) {
|
|
225
|
+
const ca = (!Array.isArray(sslCA) ? [sslCA] : sslCA).map(value => this.readFile(value, { ownPermissionOnly: true, encoding: 'utf-8' })).filter(value => value);
|
|
226
|
+
if (ca.length) {
|
|
227
|
+
options.sslCA = ca;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
else if (sslCA = checkFile(sslCA)) {
|
|
232
|
+
options.sslCA = sslCA;
|
|
233
|
+
}
|
|
234
|
+
const { sslPass, sslValidate } = credential;
|
|
235
|
+
if (sslPass) {
|
|
236
|
+
options.sslPass = sslPass;
|
|
237
|
+
}
|
|
238
|
+
if (typeof sslValidate === 'boolean') {
|
|
239
|
+
options.sslValidate = sslValidate;
|
|
240
|
+
}
|
|
241
|
+
options.ssl = true;
|
|
242
|
+
uri += '&ssl=true';
|
|
243
|
+
valid = true;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (!valid) {
|
|
247
|
+
throw (0, types_1.errorMessage)("mongodb" /* STRINGS.MODULE_NAME */, 'Missing TLS/SSL credentials');
|
|
248
|
+
}
|
|
249
|
+
authSource || (authSource = '$external');
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
case 'GSSAPI': {
|
|
253
|
+
authSource || (authSource = '$external');
|
|
254
|
+
let serviceName;
|
|
255
|
+
if ((0, types_1.isObject)(authMechanismProperties)) {
|
|
256
|
+
if (!MONGODB_V3) {
|
|
257
|
+
options.authMechanismProperties = authMechanismProperties;
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
serviceName = authMechanismProperties.SERVICE_NAME;
|
|
261
|
+
}
|
|
262
|
+
else if ((0, types_1.isString)(authMechanismProperties)) {
|
|
263
|
+
serviceName = authMechanismProperties;
|
|
264
|
+
}
|
|
265
|
+
serviceName || (serviceName = "mongodb" /* STRINGS.MODULE_NAME */);
|
|
266
|
+
uri += !MONGODB_V3 ? '&authMechanismProperties=' + encodeURIComponent('SERVICE_NAME:' + serviceName) : '&gssapiServiceName=' + encodeURIComponent(serviceName);
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
case 'PLAIN':
|
|
270
|
+
uri += '&maxPoolSize=1';
|
|
271
|
+
authSource || (authSource = '$external');
|
|
272
|
+
break;
|
|
273
|
+
case 'MONGODB-AWS':
|
|
274
|
+
if ((0, types_1.isObject)(authMechanismProperties)) {
|
|
275
|
+
options.authMechanismProperties = authMechanismProperties;
|
|
276
|
+
}
|
|
277
|
+
authSource || (authSource = '$external');
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
if (authSource) {
|
|
281
|
+
uri += '&authSource=' + encodeURIComponent(authSource);
|
|
282
|
+
}
|
|
283
|
+
item.uri = uri;
|
|
284
|
+
}
|
|
285
|
+
else if (!(0, types_1.isString)(uri)) {
|
|
286
|
+
throw (0, types_1.errorMessage)("mongodb" /* STRINGS.MODULE_NAME */, 'Not defined - credentials');
|
|
287
|
+
}
|
|
288
|
+
if (MONGODB_TOPOLOGY) {
|
|
289
|
+
options.useUnifiedTopology = true;
|
|
290
|
+
}
|
|
291
|
+
const usePool = item.usePool;
|
|
292
|
+
if (usePool) {
|
|
293
|
+
if (MONGODB_NOPOOL) {
|
|
294
|
+
item.usePool = false;
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
let username, password, pool;
|
|
298
|
+
if (typeof usePool === 'string' && (username = !MONGODB_V3 && options.auth?.username || item.uri && (0, util_1.parseConnectionString)(item.uri)?.username)) {
|
|
299
|
+
[password, pool] = pool_1.default.validateKey(POOL_STATE, username, usePool);
|
|
300
|
+
if (pool) {
|
|
301
|
+
pool.add(item, password);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
const poolKey = getPoolKey(item);
|
|
306
|
+
if (poolKey) {
|
|
307
|
+
if (!(pool = POOL_STATE[poolKey]) || pool.closed) {
|
|
308
|
+
const config = this.getPoolConfig("mongodb" /* STRINGS.MODULE_NAME */, password);
|
|
309
|
+
if (config) {
|
|
310
|
+
const { min, max, idle, queue_max, queue_idle } = config;
|
|
311
|
+
if (MONGODB_V360) {
|
|
312
|
+
if (min >= 0) {
|
|
313
|
+
options.minPoolSize ?? (options.minPoolSize = min);
|
|
314
|
+
}
|
|
315
|
+
if (max > 0) {
|
|
316
|
+
options.maxPoolSize ?? (options.maxPoolSize = max);
|
|
317
|
+
}
|
|
318
|
+
if (idle >= 0) {
|
|
319
|
+
options.maxIdleTimeMS ?? (options.maxIdleTimeMS = idle);
|
|
320
|
+
}
|
|
321
|
+
if (queue_max > 0 && !MONGODB_V3) {
|
|
322
|
+
options.maxConnecting ?? (options.maxConnecting = queue_max);
|
|
323
|
+
}
|
|
324
|
+
if (queue_idle >= 0) {
|
|
325
|
+
options.waitQueueTimeoutMS ?? (options.waitQueueTimeoutMS = queue_idle);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
else {
|
|
329
|
+
if (min >= 0) {
|
|
330
|
+
options.minSize ?? (options.minSize = min);
|
|
331
|
+
}
|
|
332
|
+
if (max > 0) {
|
|
333
|
+
options.poolSize ?? (options.poolSize = max);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
const db = require("mongodb" /* STRINGS.PACKAGE_NAME */);
|
|
338
|
+
new MongoDBPool(new db.MongoClient(item.uri, options), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
pool.add(item);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
else {
|
|
345
|
+
item.usePool = false;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
exports.setCredential = setCredential;
|
|
350
|
+
async function executeQuery(item, options) {
|
|
351
|
+
return (await executeBatchQuery.call(this, [item], options))[0] || [];
|
|
352
|
+
}
|
|
353
|
+
exports.executeQuery = executeQuery;
|
|
354
|
+
async function executeBatchQuery(batch, options = '', outResult) {
|
|
355
|
+
const length = batch.length;
|
|
356
|
+
if (length === 0) {
|
|
357
|
+
return [];
|
|
358
|
+
}
|
|
359
|
+
const db = require("mongodb" /* STRINGS.PACKAGE_NAME */);
|
|
360
|
+
let parallel, checkObject, connectOnce, errorQuery, sessionKey, outCacheMiss;
|
|
361
|
+
if ((0, types_1.isPlainObject)(options)) {
|
|
362
|
+
({ parallel, checkObject, connectOnce, errorQuery, sessionKey, outCacheMiss } = options);
|
|
363
|
+
}
|
|
364
|
+
else {
|
|
365
|
+
if (typeof options === 'string') {
|
|
366
|
+
sessionKey = options;
|
|
367
|
+
}
|
|
368
|
+
options = undefined;
|
|
369
|
+
}
|
|
370
|
+
if (length === 1) {
|
|
371
|
+
connectOnce = false;
|
|
372
|
+
parallel = false;
|
|
373
|
+
}
|
|
374
|
+
else if (parallel === undefined) {
|
|
375
|
+
parallel = !batch.some(item => item.parallel === false);
|
|
376
|
+
}
|
|
377
|
+
if (!parallel) {
|
|
378
|
+
outResult || (outResult = new Array(length));
|
|
379
|
+
}
|
|
380
|
+
const caching = this.hasCache("mongodb" /* STRINGS.MODULE_NAME */, sessionKey);
|
|
381
|
+
const tasks = new Array(length);
|
|
382
|
+
const clients = [];
|
|
383
|
+
let mongoClient, mongoCredential, mongoCache = 0, onceCredential = connectOnce ? getCacheObject(batch[0]) : undefined;
|
|
384
|
+
const getConnection = async (item, credential) => {
|
|
385
|
+
item.transactionState = 64 /* TRANSACTION.AUTH */;
|
|
386
|
+
const pool = item.usePool && pool_1.default.findKey(POOL_STATE, item.usePool, getPoolKey(connectOnce ? batch[0] : item), ...connectOnce ? [item, batch[0]] : [item]);
|
|
387
|
+
let client;
|
|
388
|
+
if (pool) {
|
|
389
|
+
try {
|
|
390
|
+
client = await pool.getConnection();
|
|
391
|
+
pool.connected = true;
|
|
392
|
+
}
|
|
393
|
+
catch {
|
|
394
|
+
if (pool.closeable) {
|
|
395
|
+
await pool.detach();
|
|
396
|
+
}
|
|
397
|
+
pool.connected = false;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
if (!client) {
|
|
401
|
+
if (connectOnce && parallel) {
|
|
402
|
+
const size = length - mongoCache;
|
|
403
|
+
if (size > 1) {
|
|
404
|
+
credential.options.maxPoolSize = size;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
clients.push(client = await new db.MongoClient(credential.uri, credential.options).connect());
|
|
408
|
+
}
|
|
409
|
+
if (connectOnce) {
|
|
410
|
+
mongoClient = client;
|
|
411
|
+
mongoCredential = credential;
|
|
412
|
+
}
|
|
413
|
+
item.transactionState &= ~64 /* TRANSACTION.AUTH */;
|
|
414
|
+
return client;
|
|
415
|
+
};
|
|
416
|
+
for (let i = 0; i < length; ++i) {
|
|
417
|
+
const item = batch[i];
|
|
418
|
+
const { source, name, table, id, aggregate, sort, client, cacheObjectKey, ignoreCache } = item;
|
|
419
|
+
let { query, update, streamRow, limit = 0 } = item;
|
|
420
|
+
if (!table) {
|
|
421
|
+
const error = (0, types_1.errorMessage)(source, "Missing database table" /* ERR_DB.TABLE */);
|
|
422
|
+
++mongoCache;
|
|
423
|
+
if (this.handleFail(error, item, { errorQuery })) {
|
|
424
|
+
if (!parallel) {
|
|
425
|
+
tasks.length = 0;
|
|
426
|
+
break;
|
|
427
|
+
}
|
|
428
|
+
tasks[i] = Promise.reject(error);
|
|
429
|
+
}
|
|
430
|
+
else if (parallel) {
|
|
431
|
+
tasks[i] = Promise.resolve([]);
|
|
432
|
+
}
|
|
433
|
+
else {
|
|
434
|
+
outResult[i] = [];
|
|
435
|
+
}
|
|
436
|
+
continue;
|
|
437
|
+
}
|
|
438
|
+
item.transactionState = 1 /* TRANSACTION.ACTIVE */;
|
|
439
|
+
const getCollection = (target) => target.db(name, client?.db).collection(table, client?.collection);
|
|
440
|
+
const credential = mongoCredential || onceCredential || getCacheObject(item);
|
|
441
|
+
const renewCache = ignoreCache === 0;
|
|
442
|
+
const cacheValue = renewCache ? { sessionKey, renewCache } : sessionKey;
|
|
443
|
+
const coercing = this.hasCoerce("mongodb" /* STRINGS.MODULE_NAME */, 'options', credential.uuidKey);
|
|
444
|
+
const targetObject = typeof checkObject === 'string' ? coercing && (0, types_1.asFunction)(checkObject) : checkObject;
|
|
445
|
+
if (typeof streamRow === 'string') {
|
|
446
|
+
streamRow = coercing && (0, types_1.asFunction)(streamRow);
|
|
447
|
+
}
|
|
448
|
+
let queryString = '', rows, pipeline;
|
|
449
|
+
if ((caching && ignoreCache !== true || ignoreCache === false || ignoreCache === 1 || renewCache) && (!targetObject || typeof cacheObjectKey === 'string') && !streamRow) {
|
|
450
|
+
queryString = (name || '') + '_' + table + '_' + limit + index_1.default.asString(sort, true) + index_1.default.asString(client, true) + (targetObject ? targetObject.toString() + cacheObjectKey : '');
|
|
451
|
+
}
|
|
452
|
+
if (aggregate) {
|
|
453
|
+
pipeline = Array.isArray(aggregate) ? aggregate : aggregate.pipeline;
|
|
454
|
+
}
|
|
455
|
+
if (id && (!query || limit === 1) && !pipeline) {
|
|
456
|
+
query = { '_id': new db.ObjectId(id) };
|
|
457
|
+
limit = 1;
|
|
458
|
+
}
|
|
459
|
+
if (queryString) {
|
|
460
|
+
const setResult = () => {
|
|
461
|
+
if (ignoreCache !== 1 && (rows = this.getQueryResult(source, removePoolProperties(credential), queryString, cacheValue))) {
|
|
462
|
+
++mongoCache;
|
|
463
|
+
if (parallel) {
|
|
464
|
+
tasks[i] = Promise.resolve(rows);
|
|
465
|
+
}
|
|
466
|
+
else {
|
|
467
|
+
outResult[i] = rows;
|
|
468
|
+
}
|
|
469
|
+
this.add(item, 4 /* TRANSACTION.COMMIT */ | 128 /* TRANSACTION.CACHE */);
|
|
470
|
+
return true;
|
|
471
|
+
}
|
|
472
|
+
return false;
|
|
473
|
+
};
|
|
474
|
+
if (pipeline || query) {
|
|
475
|
+
queryString += index_1.default.asString(aggregate || query, true);
|
|
476
|
+
if ((aggregate || !update) && setResult()) {
|
|
477
|
+
continue;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
else if (!aggregate && setResult()) {
|
|
481
|
+
continue;
|
|
482
|
+
}
|
|
483
|
+
if (!ignoreCache && outCacheMiss) {
|
|
484
|
+
outCacheMiss.push(source);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
if (onceCredential && parallel) {
|
|
488
|
+
try {
|
|
489
|
+
mongoClient = await getConnection(item, onceCredential);
|
|
490
|
+
}
|
|
491
|
+
catch {
|
|
492
|
+
connectOnce = false;
|
|
493
|
+
parallel = false;
|
|
494
|
+
}
|
|
495
|
+
onceCredential = undefined;
|
|
496
|
+
}
|
|
497
|
+
tasks[i] = new Promise(async (resolve, reject) => {
|
|
498
|
+
let commandType;
|
|
499
|
+
try {
|
|
500
|
+
let cursor;
|
|
501
|
+
if (pipeline || query) {
|
|
502
|
+
const collection = getCollection(mongoClient || await getConnection(item, credential));
|
|
503
|
+
if (pipeline) {
|
|
504
|
+
const aggregateOptions = aggregate?.options;
|
|
505
|
+
if (coercing) {
|
|
506
|
+
pipeline.forEach(doc => (0, types_1.coerceObject)(doc));
|
|
507
|
+
if (aggregateOptions) {
|
|
508
|
+
(0, types_1.coerceObject)(aggregateOptions);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
cursor = collection.aggregate(pipeline, aggregateOptions);
|
|
512
|
+
if (sort) {
|
|
513
|
+
cursor.sort(getSortValue(sort, coercing)[0]);
|
|
514
|
+
}
|
|
515
|
+
if (limit > 0) {
|
|
516
|
+
cursor.limit(limit);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
else {
|
|
520
|
+
const [filter, command] = getFilterValue(query, coercing);
|
|
521
|
+
if (update && coercing) {
|
|
522
|
+
(0, types_1.coerceObject)(update);
|
|
523
|
+
}
|
|
524
|
+
if ((0, types_1.isArray)(update)) {
|
|
525
|
+
commandType = this.commandType.INSERT;
|
|
526
|
+
const insertOptions = item.execute?.insert;
|
|
527
|
+
if (insertOptions) {
|
|
528
|
+
await collection.insertMany(update, coercing ? (0, types_1.coerceObject)(insertOptions) : insertOptions);
|
|
529
|
+
}
|
|
530
|
+
else {
|
|
531
|
+
await collection.insertMany(update);
|
|
532
|
+
}
|
|
533
|
+
update = undefined;
|
|
534
|
+
}
|
|
535
|
+
else if (item.updateType === 1) {
|
|
536
|
+
update = undefined;
|
|
537
|
+
}
|
|
538
|
+
if (limit === 1) {
|
|
539
|
+
let document;
|
|
540
|
+
if (update) {
|
|
541
|
+
commandType = this.commandType.UPDATE;
|
|
542
|
+
document = await collection[item.updateType === 2 ? 'findOneAndReplace' : item.updateType === 3 ? 'findOneAndDelete' : 'findOneAndUpdate'](filter, update, command);
|
|
543
|
+
}
|
|
544
|
+
else {
|
|
545
|
+
commandType = this.commandType.SELECT;
|
|
546
|
+
document = await collection.findOne(filter, command);
|
|
547
|
+
}
|
|
548
|
+
rows = document ? [document] : [];
|
|
549
|
+
}
|
|
550
|
+
else {
|
|
551
|
+
if (update) {
|
|
552
|
+
commandType = this.commandType.UPDATE;
|
|
553
|
+
await collection.updateMany(filter, update);
|
|
554
|
+
}
|
|
555
|
+
cursor = collection.find(filter, command);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
else if (!aggregate) {
|
|
560
|
+
cursor = getCollection(mongoClient || await getConnection(item, credential)).find();
|
|
561
|
+
}
|
|
562
|
+
if (cursor) {
|
|
563
|
+
commandType = this.commandType.SELECT;
|
|
564
|
+
if (!pipeline) {
|
|
565
|
+
if (sort) {
|
|
566
|
+
cursor.sort(...getSortValue(sort, coercing));
|
|
567
|
+
}
|
|
568
|
+
if (limit > 1) {
|
|
569
|
+
cursor.limit(limit);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
if (streamRow) {
|
|
573
|
+
const processRow = typeof streamRow === 'function' && streamRow;
|
|
574
|
+
const stream = cursor.stream();
|
|
575
|
+
let error;
|
|
576
|
+
rows = [];
|
|
577
|
+
stream
|
|
578
|
+
.on('data', row => {
|
|
579
|
+
if (processRow) {
|
|
580
|
+
const err = processRow(row);
|
|
581
|
+
if (err === false) {
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
if (err instanceof Error) {
|
|
585
|
+
error = err;
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
rows.push(row);
|
|
590
|
+
})
|
|
591
|
+
.on('error', err => error = err)
|
|
592
|
+
.on('close', () => {
|
|
593
|
+
if (error && this.handleFail(error, item, { errorQuery, commandType })) {
|
|
594
|
+
reject(error);
|
|
595
|
+
}
|
|
596
|
+
else {
|
|
597
|
+
this.add(item, 4 /* TRANSACTION.COMMIT */);
|
|
598
|
+
resolve(!error ? this.setQueryResult(source, removePoolProperties(mongoCredential || credential), queryString, rows, cacheValue) : rows);
|
|
599
|
+
}
|
|
600
|
+
})
|
|
601
|
+
.on('end', () => stream.destroy());
|
|
602
|
+
}
|
|
603
|
+
else {
|
|
604
|
+
rows = await cursor.toArray();
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
if (rows) {
|
|
608
|
+
this.add(item, 4 /* TRANSACTION.COMMIT */);
|
|
609
|
+
if (targetObject) {
|
|
610
|
+
rows = targetObject(item, rows);
|
|
611
|
+
}
|
|
612
|
+
resolve(this.setQueryResult(source, removePoolProperties(mongoCredential || credential), queryString, rows, cacheValue));
|
|
613
|
+
}
|
|
614
|
+
else {
|
|
615
|
+
throw (0, types_1.errorMessage)(source, "Missing database query" /* ERR_DB.QUERY */);
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
catch (err) {
|
|
619
|
+
if (this.handleFail(err, item, { errorQuery, commandType })) {
|
|
620
|
+
reject(err);
|
|
621
|
+
}
|
|
622
|
+
else {
|
|
623
|
+
resolve([]);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
});
|
|
627
|
+
if (!parallel) {
|
|
628
|
+
try {
|
|
629
|
+
outResult[i] = await tasks[i];
|
|
630
|
+
}
|
|
631
|
+
catch {
|
|
632
|
+
tasks.length = 0;
|
|
633
|
+
break;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
return this.processRows(batch, tasks, {
|
|
638
|
+
disconnect: () => clients.forEach(item => item.close(true)),
|
|
639
|
+
parallel
|
|
640
|
+
}, outResult);
|
|
641
|
+
}
|
|
642
|
+
exports.executeBatchQuery = executeBatchQuery;
|
|
643
|
+
function checkTimeout(value, limit = 0) {
|
|
644
|
+
return pool_1.default.checkTimeout(POOL_STATE, value, limit);
|
|
645
|
+
}
|
|
646
|
+
exports.checkTimeout = checkTimeout;
|
|
647
|
+
function getFilterValue(query, coerce) {
|
|
648
|
+
let value, options;
|
|
649
|
+
if ((0, types_1.isObject)(query) && query.value && query.options) {
|
|
650
|
+
({ value, options } = query);
|
|
651
|
+
}
|
|
652
|
+
else {
|
|
653
|
+
value = query;
|
|
654
|
+
}
|
|
655
|
+
if (coerce) {
|
|
656
|
+
(0, types_1.coerceObject)(value);
|
|
657
|
+
if (options) {
|
|
658
|
+
(0, types_1.coerceObject)(options);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
return [value, options];
|
|
662
|
+
}
|
|
663
|
+
exports.getFilterValue = getFilterValue;
|
|
664
|
+
function getSortValue(sort, coerce) {
|
|
665
|
+
let value, direction;
|
|
666
|
+
if ((0, types_1.isObject)(sort) && sort.value && sort.direction) {
|
|
667
|
+
({ value, direction } = sort);
|
|
668
|
+
}
|
|
669
|
+
else {
|
|
670
|
+
value = sort;
|
|
671
|
+
}
|
|
672
|
+
if (coerce) {
|
|
673
|
+
(0, types_1.coerceObject)(value);
|
|
674
|
+
}
|
|
675
|
+
return [value, direction];
|
|
676
|
+
}
|
|
677
|
+
exports.getSortValue = getSortValue;
|
|
678
|
+
exports.DB_SOURCE_CLIENT = true;
|
|
679
|
+
exports.DB_SOURCE_TYPE = types_1.DB_TYPE.NOSQL | types_1.DB_TYPE.DOCUMENT;
|