@pi-r/mongodb 0.2.2 → 0.2.8
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 +1 -1
- package/README.md +4 -2
- package/client/index.js +46 -32
- package/package.json +6 -6
- package/types/index.d.ts +3 -2
package/LICENSE
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Copyright 2023
|
|
1
|
+
Copyright 2023 Mile Square Park
|
|
2
2
|
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
4
|
|
package/README.md
CHANGED
package/client/index.js
CHANGED
|
@@ -7,10 +7,10 @@ const util_1 = require("@e-mc/db/util");
|
|
|
7
7
|
const Db = require("@e-mc/db");
|
|
8
8
|
const Request = require("@e-mc/request");
|
|
9
9
|
const DbPool = require('@e-mc/db/pool');
|
|
10
|
-
const MONGODB_V3 = Db.checkSemVer("mongodb"
|
|
11
|
-
const MONGODB_V330 = Db.checkSemVer("mongodb"
|
|
12
|
-
const MONGODB_V333 = Db.checkSemVer("mongodb"
|
|
13
|
-
const MONGODB_V360 = Db.checkSemVer("mongodb"
|
|
10
|
+
const MONGODB_V3 = Db.checkSemVer("mongodb", 1, 4);
|
|
11
|
+
const MONGODB_V330 = Db.checkSemVer("mongodb", '3.3.0');
|
|
12
|
+
const MONGODB_V333 = Db.checkSemVer("mongodb", '3.3.0', '3.3.3');
|
|
13
|
+
const MONGODB_V360 = Db.checkSemVer("mongodb", '3.6.0');
|
|
14
14
|
const MONGODB_TOPOLOGY = MONGODB_V3 && MONGODB_V330;
|
|
15
15
|
const MONGODB_NOPOOL = MONGODB_V3 && !MONGODB_V330;
|
|
16
16
|
class MongoDBPool extends DbPool {
|
|
@@ -58,18 +58,26 @@ class MongoDBPool extends DbPool {
|
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
const POOL_STATE = {};
|
|
61
|
+
const POOL_UNUSED = ['maxPoolSize', 'minPoolSize', 'maxConnecting', 'maxIdleTimeMS', 'waitQueueTimeoutMS', 'serverSelectionTimeoutMS', 'heartbeatFrequencyMS', 'minHeartbeatFrequencyMS', 'keepAliveInitialDelay', 'compressors', 'zlibCompressionLevel', 'srvMaxHosts'];
|
|
61
62
|
function removePoolProperties(credential) {
|
|
62
|
-
|
|
63
|
+
const options = credential.options;
|
|
63
64
|
if (options && !credential.uuidKey) {
|
|
65
|
+
let result;
|
|
64
66
|
if (MONGODB_V360) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
+
for (let i = 0, length = POOL_UNUSED.length; i < length; ++i) {
|
|
68
|
+
const attr = POOL_UNUSED[i];
|
|
69
|
+
if (attr in credential) {
|
|
70
|
+
result || (result = { ...options });
|
|
71
|
+
delete result[attr];
|
|
72
|
+
}
|
|
67
73
|
}
|
|
68
74
|
}
|
|
69
75
|
else if ('poolSize' in options || 'minSize' in options) {
|
|
70
|
-
|
|
76
|
+
result = { ...options, poolSize: undefined, minSize: undefined };
|
|
77
|
+
}
|
|
78
|
+
if (result) {
|
|
79
|
+
return { uri: credential.uri, options: result };
|
|
71
80
|
}
|
|
72
|
-
return { uri: credential.uri, options };
|
|
73
81
|
}
|
|
74
82
|
return credential;
|
|
75
83
|
}
|
|
@@ -124,7 +132,7 @@ function setCredential(item) {
|
|
|
124
132
|
const options = item.options || (item.options = {});
|
|
125
133
|
let uri = item.uri;
|
|
126
134
|
if (credential) {
|
|
127
|
-
let { protocol, server, hostname, port, username, password } = (0, util_1.parseServerAuth)(credential, 27017
|
|
135
|
+
let { protocol, server, hostname, port, username, password } = (0, util_1.parseServerAuth)(credential, 27017), { auth, authMechanism = 'DEFAULT', authSource, authMechanismProperties } = credential, authentication = '';
|
|
128
136
|
if (authMechanism === 'GSSAPI') {
|
|
129
137
|
const principal = auth?.username || username || options.auth?.username;
|
|
130
138
|
if ((0, types_1.isString)(principal)) {
|
|
@@ -145,10 +153,10 @@ function setCredential(item) {
|
|
|
145
153
|
authentication = (0, util_1.getBasicAuth)(username, password);
|
|
146
154
|
}
|
|
147
155
|
if (!server && hostname) {
|
|
148
|
-
server = hostname + ':' + (port || 27017
|
|
156
|
+
server = hostname + ':' + (port || 27017);
|
|
149
157
|
}
|
|
150
158
|
if (!server) {
|
|
151
|
-
throw (0, types_1.errorMessage)("mongodb"
|
|
159
|
+
throw (0, types_1.errorMessage)("mongodb", 'Not defined - server address');
|
|
152
160
|
}
|
|
153
161
|
uri = (protocol || 'mongodb:') + '//' + authentication + server;
|
|
154
162
|
}
|
|
@@ -244,7 +252,7 @@ function setCredential(item) {
|
|
|
244
252
|
}
|
|
245
253
|
}
|
|
246
254
|
if (!valid) {
|
|
247
|
-
throw (0, types_1.errorMessage)("mongodb"
|
|
255
|
+
throw (0, types_1.errorMessage)("mongodb", 'Missing TLS/SSL credentials');
|
|
248
256
|
}
|
|
249
257
|
authSource || (authSource = '$external');
|
|
250
258
|
break;
|
|
@@ -262,7 +270,7 @@ function setCredential(item) {
|
|
|
262
270
|
else if ((0, types_1.isString)(authMechanismProperties)) {
|
|
263
271
|
serviceName = authMechanismProperties;
|
|
264
272
|
}
|
|
265
|
-
serviceName || (serviceName = "mongodb"
|
|
273
|
+
serviceName || (serviceName = "mongodb");
|
|
266
274
|
uri += !MONGODB_V3 ? '&authMechanismProperties=' + encodeURIComponent('SERVICE_NAME:' + serviceName) : '&gssapiServiceName=' + encodeURIComponent(serviceName);
|
|
267
275
|
break;
|
|
268
276
|
}
|
|
@@ -283,7 +291,7 @@ function setCredential(item) {
|
|
|
283
291
|
item.uri = uri;
|
|
284
292
|
}
|
|
285
293
|
else if (!(0, types_1.isString)(uri)) {
|
|
286
|
-
throw (0, types_1.errorMessage)("mongodb"
|
|
294
|
+
throw (0, types_1.errorMessage)("mongodb", 'Not defined - credentials');
|
|
287
295
|
}
|
|
288
296
|
if (MONGODB_TOPOLOGY) {
|
|
289
297
|
options.useUnifiedTopology = true;
|
|
@@ -305,7 +313,7 @@ function setCredential(item) {
|
|
|
305
313
|
const poolKey = getPoolKey(item);
|
|
306
314
|
if (poolKey) {
|
|
307
315
|
if (!(pool = POOL_STATE[poolKey]) || pool.closed) {
|
|
308
|
-
const config = this.getPoolConfig("mongodb"
|
|
316
|
+
const config = this.getPoolConfig("mongodb", password);
|
|
309
317
|
if (config) {
|
|
310
318
|
const { min, max, idle, queue_max, queue_idle } = config;
|
|
311
319
|
if (MONGODB_V360) {
|
|
@@ -334,7 +342,7 @@ function setCredential(item) {
|
|
|
334
342
|
}
|
|
335
343
|
}
|
|
336
344
|
}
|
|
337
|
-
const db = require("mongodb"
|
|
345
|
+
const db = require("mongodb");
|
|
338
346
|
new MongoDBPool(new db.MongoClient(item.uri, options), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
|
|
339
347
|
}
|
|
340
348
|
else {
|
|
@@ -356,7 +364,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
356
364
|
if (length === 0) {
|
|
357
365
|
return [];
|
|
358
366
|
}
|
|
359
|
-
const db = require("mongodb"
|
|
367
|
+
const db = require("mongodb");
|
|
360
368
|
let parallel, checkObject, connectOnce, errorQuery, sessionKey, outCacheMiss;
|
|
361
369
|
if ((0, types_1.isPlainObject)(options)) {
|
|
362
370
|
({ parallel, checkObject, connectOnce, errorQuery, sessionKey, outCacheMiss } = options);
|
|
@@ -377,12 +385,12 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
377
385
|
if (!parallel) {
|
|
378
386
|
outResult || (outResult = new Array(length));
|
|
379
387
|
}
|
|
380
|
-
const caching = this.hasCache("mongodb"
|
|
388
|
+
const caching = this.hasCache("mongodb", sessionKey);
|
|
381
389
|
const tasks = new Array(length);
|
|
382
390
|
const clients = [];
|
|
383
391
|
let mongoClient, mongoCredential, mongoCache = 0, onceCredential = connectOnce ? getCacheObject(batch[0]) : undefined;
|
|
384
392
|
const getConnection = async (item, credential) => {
|
|
385
|
-
item.transactionState = 64
|
|
393
|
+
item.transactionState = 64;
|
|
386
394
|
const pool = item.usePool && DbPool.findKey(POOL_STATE, item.usePool, getPoolKey(connectOnce ? batch[0] : item), ...connectOnce ? [item, batch[0]] : [item]);
|
|
387
395
|
let client;
|
|
388
396
|
if (pool) {
|
|
@@ -410,7 +418,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
410
418
|
mongoClient = client;
|
|
411
419
|
mongoCredential = credential;
|
|
412
420
|
}
|
|
413
|
-
item.transactionState &= ~64
|
|
421
|
+
item.transactionState &= ~64;
|
|
414
422
|
return client;
|
|
415
423
|
};
|
|
416
424
|
for (let i = 0; i < length; ++i) {
|
|
@@ -418,7 +426,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
418
426
|
const { source, name, table, id, aggregate, sort, client, cacheObjectKey, ignoreCache } = item;
|
|
419
427
|
let { query, update, streamRow, limit = 0 } = item;
|
|
420
428
|
if (!table) {
|
|
421
|
-
const error = (0, types_1.errorMessage)(source, "Missing database table"
|
|
429
|
+
const error = (0, types_1.errorMessage)(source, "Missing database table");
|
|
422
430
|
++mongoCache;
|
|
423
431
|
if (this.handleFail(error, item, { errorQuery })) {
|
|
424
432
|
if (!parallel) {
|
|
@@ -435,12 +443,12 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
435
443
|
}
|
|
436
444
|
continue;
|
|
437
445
|
}
|
|
438
|
-
item.transactionState = 1
|
|
446
|
+
item.transactionState = 1;
|
|
439
447
|
const getCollection = (target) => target.db(name, client?.db).collection(table, client?.collection);
|
|
440
448
|
const credential = mongoCredential || onceCredential || getCacheObject(item);
|
|
441
449
|
const renewCache = ignoreCache === 0;
|
|
442
450
|
const cacheValue = renewCache ? { sessionKey, renewCache } : sessionKey;
|
|
443
|
-
const coercing = this.hasCoerce("mongodb"
|
|
451
|
+
const coercing = this.hasCoerce("mongodb", 'options', credential.uuidKey);
|
|
444
452
|
const targetObject = typeof checkObject === 'string' ? coercing && (0, types_1.asFunction)(checkObject) : checkObject;
|
|
445
453
|
if (typeof streamRow === 'string') {
|
|
446
454
|
streamRow = coercing && (0, types_1.asFunction)(streamRow);
|
|
@@ -466,7 +474,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
466
474
|
else {
|
|
467
475
|
outResult[i] = rows;
|
|
468
476
|
}
|
|
469
|
-
this.add(item, 4
|
|
477
|
+
this.add(item, 4 | 128);
|
|
470
478
|
return true;
|
|
471
479
|
}
|
|
472
480
|
return false;
|
|
@@ -524,11 +532,14 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
524
532
|
if ((0, types_1.isArray)(update)) {
|
|
525
533
|
commandType = this.commandType.INSERT;
|
|
526
534
|
const insertOptions = item.execute?.insert;
|
|
527
|
-
if (insertOptions) {
|
|
528
|
-
|
|
535
|
+
if (insertOptions && coercing) {
|
|
536
|
+
(0, types_1.coerceObject)(insertOptions);
|
|
537
|
+
}
|
|
538
|
+
if (update.length === 1) {
|
|
539
|
+
await collection.insertOne(update[0], insertOptions);
|
|
529
540
|
}
|
|
530
541
|
else {
|
|
531
|
-
await collection.insertMany(update);
|
|
542
|
+
await collection.insertMany(update, insertOptions);
|
|
532
543
|
}
|
|
533
544
|
update = undefined;
|
|
534
545
|
}
|
|
@@ -550,7 +561,10 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
550
561
|
else {
|
|
551
562
|
if (update) {
|
|
552
563
|
commandType = this.commandType.UPDATE;
|
|
553
|
-
|
|
564
|
+
const updateOptions = item.execute?.update;
|
|
565
|
+
if (updateOptions && coercing) {
|
|
566
|
+
(0, types_1.coerceObject)(updateOptions);
|
|
567
|
+
}
|
|
554
568
|
}
|
|
555
569
|
cursor = collection.find(filter, command);
|
|
556
570
|
}
|
|
@@ -594,7 +608,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
594
608
|
reject(error);
|
|
595
609
|
}
|
|
596
610
|
else {
|
|
597
|
-
this.add(item, 4
|
|
611
|
+
this.add(item, 4);
|
|
598
612
|
resolve(!error ? this.setQueryResult(source, removePoolProperties(mongoCredential || credential), queryString, rows, cacheValue) : rows);
|
|
599
613
|
}
|
|
600
614
|
})
|
|
@@ -605,14 +619,14 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
605
619
|
}
|
|
606
620
|
}
|
|
607
621
|
if (rows) {
|
|
608
|
-
this.add(item, 4
|
|
622
|
+
this.add(item, 4);
|
|
609
623
|
if (targetObject) {
|
|
610
624
|
rows = targetObject(item, rows);
|
|
611
625
|
}
|
|
612
626
|
resolve(this.setQueryResult(source, removePoolProperties(mongoCredential || credential), queryString, rows, cacheValue));
|
|
613
627
|
}
|
|
614
628
|
else {
|
|
615
|
-
throw (0, types_1.errorMessage)(source, "Missing database query"
|
|
629
|
+
throw (0, types_1.errorMessage)(source, "Missing database query");
|
|
616
630
|
}
|
|
617
631
|
}
|
|
618
632
|
catch (err) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/mongodb",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.8",
|
|
4
4
|
"description": "MongoDB client driver for E-mc.",
|
|
5
5
|
"main": "client/index.js",
|
|
6
6
|
"types": "client/index.d.ts",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
},
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
|
-
"url": "https://github.com/anpham6/pi-r.git",
|
|
12
|
+
"url": "git+https://github.com/anpham6/pi-r.git",
|
|
13
13
|
"directory": "src/db/mongodb"
|
|
14
14
|
},
|
|
15
15
|
"keywords": [
|
|
@@ -21,9 +21,9 @@
|
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@e-mc/db": "^0.5.
|
|
25
|
-
"@e-mc/request": "^0.5.
|
|
26
|
-
"@e-mc/types": "^0.5.
|
|
27
|
-
"mongodb": "^5.
|
|
24
|
+
"@e-mc/db": "^0.5.4",
|
|
25
|
+
"@e-mc/request": "^0.5.4",
|
|
26
|
+
"@e-mc/types": "^0.5.4",
|
|
27
|
+
"mongodb": "^5.9.2"
|
|
28
28
|
}
|
|
29
29
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { DbDataSource } from '@e-mc/types/lib/squared';
|
|
|
2
2
|
|
|
3
3
|
import type { CascadeAction, ServerAuth } from '@e-mc/types/lib/db';
|
|
4
4
|
|
|
5
|
-
import type { AggregateOptions,
|
|
5
|
+
import type { AggregateOptions, CollectionOptions, CommandOperationOptions, DbOptions, Document, Filter, MongoClientOptions, OptionalUnlessRequiredId, Sort, SortDirection, UpdateFilter } from 'mongodb';
|
|
6
6
|
|
|
7
7
|
export interface MongoDBDataSource extends DbDataSource<Filter<Document> | MongoDBQueryWithOptions, MongoClientOptions, UpdateFilter<Document> | OptionalUnlessRequiredId<unknown>[], string | MongoDBCredential, string>, MongoDBClientDBOptions, CascadeAction {
|
|
8
8
|
source: "mongodb";
|
|
@@ -28,7 +28,8 @@ export interface MongoDBClientDBOptions {
|
|
|
28
28
|
collection?: CollectionOptions;
|
|
29
29
|
};
|
|
30
30
|
execute?: {
|
|
31
|
-
insert?:
|
|
31
|
+
insert?: CommandOperationOptions;
|
|
32
|
+
update?: CommandOperationOptions;
|
|
32
33
|
};
|
|
33
34
|
aggregate?: MongoDBAggregate;
|
|
34
35
|
sort?: Sort | string | MongoDBSortValue;
|