@pi-r/mongodb 0.6.3 → 0.6.5

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 CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2023 An Pham
1
+ Copyright 2024 An Pham
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
@@ -1,7 +1,10 @@
1
- ### @pi-r/mongodb
1
+ # @pi-r/mongodb
2
2
 
3
- https://e-mc.readthedocs.io/en/latest/db/mongodb.html
3
+ ## Documentation
4
4
 
5
- ### LICENSE
5
+ - [E-mc](https://e-mc.readthedocs.io/en/latest/db/mongodb.html)
6
+ - [squared](https://squared.readthedocs.io)
7
+
8
+ ## LICENSE
6
9
 
7
10
  MIT
package/client/index.js CHANGED
@@ -47,9 +47,10 @@ class MongoDBPool extends DbPool {
47
47
  }
48
48
  const POOL_STATE = {};
49
49
  const POOL_UNUSED = ['maxPoolSize', 'minPoolSize', 'connectTimeoutMS', 'socketTimeoutMS', 'maxConnecting', 'maxIdleTimeMS', 'waitQueueTimeoutMS', 'serverSelectionTimeoutMS', 'heartbeatFrequencyMS', 'minHeartbeatFrequencyMS', 'compressors', 'zlibCompressionLevel', 'srvMaxHosts'];
50
+ const POOL_ACTIVE = new WeakSet();
50
51
  function removePoolProperties(credential) {
51
52
  const options = credential.options;
52
- if (options && !credential.uuidKey) {
53
+ if (options && !credential.uuidKey && POOL_ACTIVE.has(credential)) {
53
54
  let result;
54
55
  for (let i = 0, length = POOL_UNUSED.length; i < length; ++i) {
55
56
  const attr = POOL_UNUSED[i];
@@ -79,7 +80,7 @@ function setCredential(item) {
79
80
  const options = item.options || (item.options = {});
80
81
  let uri = item.uri;
81
82
  if (credential) {
82
- let { protocol, server, hostname, port, username, password } = (0, util_1.parseServerAuth)(credential, 27017 /* VALUES.PORT */), { auth, authMechanism = 'DEFAULT', authSource, authMechanismProperties } = credential, authentication = '';
83
+ let { protocol, server, hostname, port, username, password } = (0, util_1.parseServerAuth)(credential, 27017), { auth, authMechanism = 'DEFAULT', authSource, authMechanismProperties } = credential, authentication = '';
83
84
  if (authMechanism === 'GSSAPI') {
84
85
  const principal = auth?.username || username || options.auth?.username;
85
86
  if ((0, types_1.isString)(principal)) {
@@ -94,10 +95,10 @@ function setCredential(item) {
94
95
  authentication = (0, util_1.getBasicAuth)(username, password);
95
96
  }
96
97
  if (!server && hostname) {
97
- server = hostname + ':' + (port || 27017 /* VALUES.PORT */);
98
+ server = hostname + ':' + (port || 27017);
98
99
  }
99
100
  if (!server) {
100
- throw (0, types_1.errorMessage)("mongodb" /* STRINGS.MODULE_NAME */, 'Not defined - server address');
101
+ throw (0, types_1.errorMessage)("mongodb", 'Not defined - server address');
101
102
  }
102
103
  uri = (protocol || 'mongodb:') + '//' + authentication + server;
103
104
  }
@@ -142,7 +143,7 @@ function setCredential(item) {
142
143
  uri += '&tls=true';
143
144
  }
144
145
  else {
145
- throw (0, types_1.errorMessage)("mongodb" /* STRINGS.MODULE_NAME */, 'Missing TLS/SSL credentials');
146
+ throw (0, types_1.errorMessage)("mongodb", 'Missing TLS/SSL credentials');
146
147
  }
147
148
  authSource || (authSource = '$external');
148
149
  break;
@@ -166,7 +167,7 @@ function setCredential(item) {
166
167
  item.uri = uri;
167
168
  }
168
169
  else if (!(0, types_1.isString)(uri)) {
169
- throw (0, types_1.errorMessage)("mongodb" /* STRINGS.MODULE_NAME */, 'Not defined - credentials');
170
+ throw (0, types_1.errorMessage)("mongodb", 'Not defined - credentials');
170
171
  }
171
172
  const usePool = item.usePool;
172
173
  if (!usePool) {
@@ -186,7 +187,7 @@ function setCredential(item) {
186
187
  return;
187
188
  }
188
189
  }
189
- const config = this.getPoolConfig("mongodb" /* STRINGS.MODULE_NAME */, password);
190
+ const config = this.getPoolConfig("mongodb", password);
190
191
  if (config) {
191
192
  const { min, max, idle, queue_max, queue_idle, timeout, socket_timeout } = config;
192
193
  if (min >= 0) {
@@ -248,18 +249,19 @@ async function executeBatchQuery(batch, options = '', outResult) {
248
249
  if (!parallel) {
249
250
  outResult || (outResult = new Array(length));
250
251
  }
251
- const caching = this.hasCache("mongodb" /* STRINGS.MODULE_NAME */, sessionKey);
252
+ const caching = this.hasCache("mongodb", sessionKey);
252
253
  const tasks = new Array(length);
253
254
  const clients = [];
254
255
  let mongoClient, mongoCredential, mongoCache = 0, onceCredential = connectOnce ? getCacheObject(batch[0]) : undefined;
255
256
  const getConnection = async (item, credential) => {
256
- item.transactionState = 64 /* DB_TRANSACTION.AUTH */;
257
+ item.transactionState = 64;
257
258
  const pool = item.usePool && DbPool.findKey(POOL_STATE, item.usePool, getPoolKey(connectOnce ? batch[0] : item), ...connectOnce ? [item, batch[0]] : [item]);
258
259
  let client;
259
260
  if (pool) {
260
261
  try {
261
262
  client = await pool.getConnection();
262
263
  pool.connected = true;
264
+ POOL_ACTIVE.add(credential);
263
265
  }
264
266
  catch {
265
267
  if (pool.closeable) {
@@ -281,7 +283,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
281
283
  mongoClient = client;
282
284
  mongoCredential = credential;
283
285
  }
284
- item.transactionState &= ~64 /* DB_TRANSACTION.AUTH */;
286
+ item.transactionState &= ~64;
285
287
  return client;
286
288
  };
287
289
  if (this.host?.username) {
@@ -292,7 +294,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
292
294
  const { source, name, table, id, aggregate, sort, client, cacheObjectKey, ignoreCache } = item;
293
295
  let { query, update, streamRow, limit = 0 } = item;
294
296
  if (!table) {
295
- const error = (0, types_1.errorMessage)(source, "Missing database table" /* ERR_DB.TABLE */);
297
+ const error = (0, types_1.errorMessage)(source, "Missing database table");
296
298
  ++mongoCache;
297
299
  if (this.handleFail(error, item, { errorQuery })) {
298
300
  if (!parallel) {
@@ -309,13 +311,13 @@ async function executeBatchQuery(batch, options = '', outResult) {
309
311
  }
310
312
  continue;
311
313
  }
312
- item.transactionState = 1 /* DB_TRANSACTION.ACTIVE */;
314
+ item.transactionState = 1;
313
315
  const getInstance = (target) => target.db(name, client?.db);
314
316
  const getCollection = (target) => getInstance(target).collection(table, client?.collection);
315
317
  const runCommand = async (target, document) => getInstance(target).command(document, client?.command);
316
318
  const credential = mongoCredential || onceCredential || getCacheObject(item);
317
319
  const cacheValue = ignoreCache === undefined ? sessionKey : Array.isArray(ignoreCache) ? { sessionKey, exclusiveOf: ignoreCache } : { sessionKey, renewCache: ignoreCache === 0 };
318
- const coercing = this.hasCoerce("mongodb" /* STRINGS.MODULE_NAME */, 'options', credential.uuidKey);
320
+ const coercing = this.hasCoerce("mongodb", 'options', credential.uuidKey);
319
321
  const targetObject = typeof checkObject === 'string' ? coercing && (0, types_1.asFunction)(checkObject) : checkObject;
320
322
  if (typeof streamRow === 'string') {
321
323
  streamRow = coercing && (0, types_1.asFunction)(streamRow);
@@ -341,7 +343,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
341
343
  else {
342
344
  outResult[i] = rows;
343
345
  }
344
- this.add(item, 4 /* DB_TRANSACTION.COMMIT */ | 128 /* DB_TRANSACTION.CACHE */);
346
+ this.add(item, 4 | 128);
345
347
  return true;
346
348
  }
347
349
  return false;
@@ -376,7 +378,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
376
378
  const command = item.command;
377
379
  if (command) {
378
380
  await Promise.all((!Array.isArray(command) ? [command] : command).map(async (cmd) => runCommand(instance, cmd)))
379
- .then(items => items.forEach(output => this.addLog(this.statusType.INFO, output, "mongodb" /* STRINGS.MODULE_NAME */, 'runCommand')));
381
+ .then(items => items.forEach(output => this.addLog(this.statusType.INFO, output, "mongodb", 'runCommand')));
380
382
  }
381
383
  const collection = getCollection(instance);
382
384
  if (update && coercing) {
@@ -480,7 +482,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
480
482
  reject(error);
481
483
  }
482
484
  else {
483
- this.add(item, 4 /* DB_TRANSACTION.COMMIT */);
485
+ this.add(item, 4);
484
486
  resolve(!error ? this.setQueryResult(source, removePoolProperties(mongoCredential || credential), queryString, rows, cacheValue) : rows);
485
487
  }
486
488
  })
@@ -491,14 +493,14 @@ async function executeBatchQuery(batch, options = '', outResult) {
491
493
  }
492
494
  }
493
495
  if (rows) {
494
- this.add(item, 4 /* DB_TRANSACTION.COMMIT */);
496
+ this.add(item, 4);
495
497
  if (targetObject) {
496
498
  rows = targetObject(item, rows);
497
499
  }
498
500
  resolve(this.setQueryResult(source, removePoolProperties(mongoCredential || credential), queryString, rows, cacheValue));
499
501
  }
500
502
  else {
501
- throw (0, types_1.errorMessage)(source, "Missing database query" /* ERR_DB.QUERY */);
503
+ throw (0, types_1.errorMessage)(source, "Missing database query");
502
504
  }
503
505
  }
504
506
  catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/mongodb",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
4
4
  "description": "MongoDB client driver for E-mc.",
5
5
  "main": "client/index.js",
6
6
  "types": "client/index.d.ts",
@@ -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.8.3",
25
- "@e-mc/request": "^0.8.3",
26
- "@e-mc/types": "^0.8.3",
27
- "mongodb": "^6.3.0"
24
+ "@e-mc/db": "^0.8.6",
25
+ "@e-mc/request": "^0.8.6",
26
+ "@e-mc/types": "^0.8.6",
27
+ "mongodb": "^6.4.0"
28
28
  }
29
29
  }