@pi-r/mongodb 0.6.0 → 0.6.2

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/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  ### @pi-r/mongodb
2
2
 
3
+ https://e-mc.readthedocs.io/en/latest/db/mongodb.html
4
+
3
5
  ### LICENSE
4
6
 
5
7
  MIT
package/client/index.js CHANGED
@@ -69,15 +69,9 @@ function getCacheObject(item) {
69
69
  return { uri: item.uri, options: item.options, uuidKey: credential?.uuidKey };
70
70
  }
71
71
  function getPoolKey(credential) {
72
- const options = credential.options;
73
- if (options && credential.uri) {
74
- const auth = options.auth;
75
- let result = credential.uri + (auth ? '_' + (auth.username || '') + '@' + (auth.password || '') : '') + '_' + (options.authMechanism || '') + Db.asString(options.authMechanismProperties, true);
76
- if (options.tls || options.ssl) {
77
- const { tlsCertificateKeyFile = '', tlsCAFile = '', tlsCRLFile = '', tlsCertificateKeyFilePassword = '', tlsAllowInvalidHostnames, tlsAllowInvalidCertificates, tlsInsecure } = options;
78
- result += tlsCertificateKeyFile + tlsCAFile + tlsCRLFile + tlsCertificateKeyFilePassword + (tlsAllowInvalidHostnames ? '1' : '0') + (tlsAllowInvalidCertificates ? '1' : '0') + (tlsInsecure ? '1' : '0');
79
- }
80
- return result;
72
+ const { options, uri } = credential;
73
+ if (uri && options) {
74
+ return uri + JSON.stringify(credential.options);
81
75
  }
82
76
  }
83
77
  function setCredential(item) {
@@ -179,22 +173,19 @@ function setCredential(item) {
179
173
  return;
180
174
  }
181
175
  let username, password, pool;
182
- if (typeof usePool === 'string' && (username = options.auth?.username || item.uri && (0, util_1.parseConnectionString)(item.uri)?.username)) {
183
- [password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
184
- if (pool) {
185
- pool.add(item, password);
176
+ if ((0, types_1.isString)(usePool)) {
177
+ if (username = options.auth?.username || item.uri && (0, util_1.parseConnectionString)(item.uri)?.username) {
178
+ [password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
179
+ if (pool) {
180
+ pool.add(item, password);
181
+ return;
182
+ }
183
+ }
184
+ if (!password) {
185
+ item.usePool = '';
186
186
  return;
187
187
  }
188
188
  }
189
- const poolKey = getPoolKey(item);
190
- if (!poolKey) {
191
- item.usePool = false;
192
- return;
193
- }
194
- if ((pool = POOL_STATE[poolKey]) && !pool.closed) {
195
- pool.add(item);
196
- return;
197
- }
198
189
  const config = this.getPoolConfig("mongodb" /* STRINGS.MODULE_NAME */, password);
199
190
  if (config) {
200
191
  const { min, max, idle, queue_max, queue_idle, timeout, socket_timeout } = config;
@@ -220,6 +211,11 @@ function setCredential(item) {
220
211
  options.socketTimeoutMS ?? (options.socketTimeoutMS = socket_timeout);
221
212
  }
222
213
  }
214
+ const poolKey = getPoolKey(item);
215
+ if ((pool = POOL_STATE[poolKey]) && !pool.closed) {
216
+ pool.add(item);
217
+ return;
218
+ }
223
219
  new MongoDBPool(new mongodb.MongoClient(item.uri, options), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
224
220
  }
225
221
  exports.setCredential = setCredential;
@@ -318,15 +314,14 @@ async function executeBatchQuery(batch, options = '', outResult) {
318
314
  const getCollection = (target) => getInstance(target).collection(table, client?.collection);
319
315
  const runCommand = async (target, document) => getInstance(target).command(document, client?.command);
320
316
  const credential = mongoCredential || onceCredential || getCacheObject(item);
321
- const renewCache = ignoreCache === 0;
322
- const cacheValue = renewCache ? { sessionKey, renewCache } : sessionKey;
317
+ const cacheValue = ignoreCache === undefined ? sessionKey : Array.isArray(ignoreCache) ? { sessionKey, exclusiveOf: ignoreCache } : { sessionKey, renewCache: ignoreCache === 0 };
323
318
  const coercing = this.hasCoerce("mongodb" /* STRINGS.MODULE_NAME */, 'options', credential.uuidKey);
324
319
  const targetObject = typeof checkObject === 'string' ? coercing && (0, types_1.asFunction)(checkObject) : checkObject;
325
320
  if (typeof streamRow === 'string') {
326
321
  streamRow = coercing && (0, types_1.asFunction)(streamRow);
327
322
  }
328
323
  let queryString = '', rows, pipeline;
329
- if ((caching && ignoreCache !== true || ignoreCache === false || ignoreCache === 1 || renewCache) && (!targetObject || typeof cacheObjectKey === 'string') && !streamRow) {
324
+ if (caching && ignoreCache !== true && (!targetObject || typeof cacheObjectKey === 'string') && !streamRow) {
330
325
  queryString = (name || '') + '_' + table + '_' + limit + Db.asString(sort, true) + Db.asString(client, true) + (targetObject ? targetObject.toString() + cacheObjectKey : '');
331
326
  }
332
327
  if (aggregate) {
@@ -383,9 +378,26 @@ async function executeBatchQuery(batch, options = '', outResult) {
383
378
  await Promise.all((!Array.isArray(command) ? [command] : command).map(async (cmd) => runCommand(instance, cmd)))
384
379
  .then(items => items.forEach(output => this.addLog(this.statusType.INFO, output, "mongodb" /* STRINGS.MODULE_NAME */, 'runCommand')));
385
380
  }
381
+ const collection = getCollection(instance);
382
+ if (update && coercing) {
383
+ (0, types_1.coerceObject)(update);
384
+ }
385
+ if (Array.isArray(update)) {
386
+ commandType = this.commandType.INSERT;
387
+ const insertOptions = item.execute?.insert;
388
+ if (insertOptions && coercing) {
389
+ (0, types_1.coerceObject)(insertOptions);
390
+ }
391
+ if (update.length === 1) {
392
+ await collection.insertOne(update[0], insertOptions);
393
+ }
394
+ else if (update.length > 1) {
395
+ await collection.insertMany(update, insertOptions);
396
+ }
397
+ update = undefined;
398
+ }
386
399
  let cursor;
387
400
  if (pipeline || query) {
388
- const collection = getCollection(instance);
389
401
  if (pipeline) {
390
402
  const aggregateOptions = (0, types_1.isPlainObject)(aggregate) ? aggregate.options : undefined;
391
403
  if (coercing) {
@@ -404,31 +416,12 @@ async function executeBatchQuery(batch, options = '', outResult) {
404
416
  }
405
417
  else {
406
418
  const [filter, operation] = getFilterValue(query, coercing);
407
- if (update && coercing) {
408
- (0, types_1.coerceObject)(update);
409
- }
410
- if ((0, types_1.isArray)(update)) {
411
- commandType = this.commandType.INSERT;
412
- const insertOptions = item.execute?.insert;
413
- if (insertOptions && coercing) {
414
- (0, types_1.coerceObject)(insertOptions);
415
- }
416
- if (update.length === 1) {
417
- await collection.insertOne(update[0], insertOptions);
418
- }
419
- else {
420
- await collection.insertMany(update, insertOptions);
421
- }
422
- update = undefined;
423
- }
424
- else if (item.updateType === 1) {
425
- update = undefined;
426
- }
419
+ const updateType = item.updateType;
427
420
  if (limit === 1) {
428
421
  let document;
429
422
  if (update) {
430
423
  commandType = this.commandType.UPDATE;
431
- document = await collection[item.updateType === 2 ? 'findOneAndReplace' : item.updateType === 3 ? 'findOneAndDelete' : 'findOneAndUpdate'](filter, update, operation);
424
+ document = await collection[updateType === 2 ? 'findOneAndReplace' : updateType === 3 ? 'findOneAndDelete' : 'findOneAndUpdate'](filter, update, operation);
432
425
  }
433
426
  else {
434
427
  commandType = this.commandType.SELECT;
@@ -437,7 +430,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
437
430
  rows = document ? [document] : [];
438
431
  }
439
432
  else {
440
- if (update) {
433
+ if (update && updateType === 1) {
441
434
  commandType = this.commandType.UPDATE;
442
435
  const updateOptions = item.execute?.update;
443
436
  if (updateOptions && coercing) {
@@ -450,7 +443,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
450
443
  }
451
444
  }
452
445
  else if (!aggregate) {
453
- cursor = getCollection(instance).find();
446
+ cursor = collection.find();
454
447
  }
455
448
  if (cursor) {
456
449
  commandType = this.commandType.SELECT;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/mongodb",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
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.0",
25
- "@e-mc/request": "^0.8.0",
26
- "@e-mc/types": "^0.8.0",
24
+ "@e-mc/db": "^0.8.2",
25
+ "@e-mc/request": "^0.8.2",
26
+ "@e-mc/types": "^0.8.2",
27
27
  "mongodb": "^6.3.0"
28
28
  }
29
29
  }