@pi-r/mongodb 0.12.0 → 0.12.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/client/index.js CHANGED
@@ -249,6 +249,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
249
249
  outResult ||= new Array(length);
250
250
  }
251
251
  const caching = this.hasCache("mongodb", sessionKey);
252
+ const sortKeys = this.settingsOf("mongodb", 'cache', 'sort_keys') === true;
252
253
  const tasks = new Array(length);
253
254
  const clients = [];
254
255
  let mongoClient, mongoCredential, mongoCache = 0, onceCredential = connectOnce ? getCacheObject(batch[0]) : undefined;
@@ -328,11 +329,11 @@ async function executeBatchQuery(batch, options = '', outResult) {
328
329
  if (coercing && typeof streamRow === 'string') {
329
330
  streamRow = asFunction(streamRow);
330
331
  }
331
- let queryString = '', rows, pipeline;
332
- if (caching && ignoreCache !== true && (!targetObject || typeof item.cacheObjectKey === 'string') && !streamRow) {
332
+ let queryString = '', cacheCredential, rows, pipeline;
333
+ if (caching && ignoreCache !== true && !streamRow) {
333
334
  const collection = item.client?.collection;
334
335
  const db = item.client?.db;
335
- queryString = (name || '') + '_' + table + (item.withFields ? '_' + Db.asString(item.withFields, true) : '') + (sort ? '_' + Db.asString(sort, true) : '') + '_' + (limit > 0 ? limit.toString() : '0') + (db ? Db.asString(db, true) : '') + (collection ? Db.asString(collection, true) : '') + (targetObject ? targetObject.toString() + item.cacheObjectKey : '');
336
+ queryString = (name || '') + '_' + table + (item.withFields ? '_' + Db.asString(item.withFields, true) : '') + (sort ? '_' + Db.asString(sort, true) : '') + '_' + (limit > 0 ? limit.toString() : '0') + (db ? Db.asString(db, true) : '') + (collection ? Db.asString(collection, true) : '') + (targetObject && typeof item.cacheObjectKey === 'string' ? targetObject.toString() + item.cacheObjectKey : '');
336
337
  }
337
338
  if (aggregate) {
338
339
  pipeline = Array.isArray(aggregate) ? aggregate : aggregate.pipeline;
@@ -343,7 +344,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
343
344
  }
344
345
  if (queryString) {
345
346
  const setResult = () => {
346
- if (ignoreCache !== 1 && (rows = this.getQueryResult(source, DbPool.sanitize(credential), queryString, cacheValue))) {
347
+ if (ignoreCache !== 1 && (rows = this.getQueryResult(source, cacheCredential, queryString, cacheValue))) {
347
348
  ++mongoCache;
348
349
  if (parallel) {
349
350
  tasks[i] = Promise.resolve(rows);
@@ -358,12 +359,16 @@ async function executeBatchQuery(batch, options = '', outResult) {
358
359
  };
359
360
  if (pipeline || query || distinct) {
360
361
  queryString += Db.asString(aggregate || query || distinct, true);
362
+ cacheCredential = DbPool.sanitize(mongoCredential || credential, sortKeys);
361
363
  if ((aggregate || distinct || !update) && setResult()) {
362
364
  continue;
363
365
  }
364
366
  }
365
- else if (!aggregate && setResult()) {
366
- continue;
367
+ else if (!aggregate) {
368
+ cacheCredential = DbPool.sanitize(mongoCredential || credential, sortKeys);
369
+ if (setResult()) {
370
+ continue;
371
+ }
367
372
  }
368
373
  if (!ignoreCache && outCacheMiss) {
369
374
  outCacheMiss.push(source);
@@ -497,7 +502,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
497
502
  }
498
503
  else {
499
504
  this.add(item, 4);
500
- resolve(!error ? this.setQueryResult(source, DbPool.sanitize(mongoCredential || credential), queryString, rows, cacheValue) : rows);
505
+ resolve(!error ? this.setQueryResult(source, cacheCredential, queryString, rows, cacheValue) : rows);
501
506
  }
502
507
  })
503
508
  .on('end', () => stream.destroy());
@@ -511,7 +516,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
511
516
  if (targetObject) {
512
517
  rows = targetObject(item, rows);
513
518
  }
514
- resolve(this.setQueryResult(source, DbPool.sanitize(mongoCredential || credential), queryString, rows, cacheValue));
519
+ resolve(this.setQueryResult(source, cacheCredential, queryString, rows, cacheValue));
515
520
  }
516
521
  else {
517
522
  throw errorMessage(source, "Missing database query");
package/client/pool.js CHANGED
@@ -12,7 +12,7 @@ class MongoDBPool extends DbPool {
12
12
  }
13
13
  return super.asString(credential);
14
14
  }
15
- static sanitize(credential) {
15
+ static sanitize(credential, sort) {
16
16
  if (!this.canCache(credential)) {
17
17
  return;
18
18
  }
@@ -20,7 +20,7 @@ class MongoDBPool extends DbPool {
20
20
  if (!target || !POOL_ACTIVE.has(credential) || credential.uuidKey) {
21
21
  return credential;
22
22
  }
23
- const options = super.sanitize(target);
23
+ const options = super.sanitize(target, sort);
24
24
  return options !== target ? { uri: credential.uri, options } : credential;
25
25
  }
26
26
  async detach(force) {
package/package.json CHANGED
@@ -1,12 +1,9 @@
1
1
  {
2
2
  "name": "@pi-r/mongodb",
3
- "version": "0.12.0",
3
+ "version": "0.12.2",
4
4
  "description": "MongoDB client driver for E-mc.",
5
5
  "main": "client/index.js",
6
6
  "types": "client/index.d.ts",
7
- "publishConfig": {
8
- "access": "public"
9
- },
10
7
  "repository": {
11
8
  "type": "git",
12
9
  "url": "git+https://github.com/anpham6/pi-r.git",
@@ -20,9 +17,9 @@
20
17
  "license": "MIT",
21
18
  "homepage": "https://github.com/anpham6/pi-r#readme",
22
19
  "dependencies": {
23
- "@e-mc/db": "^0.14.0",
24
- "@e-mc/request": "^0.14.0",
25
- "@e-mc/types": "^0.14.0",
26
- "mongodb": "^7.2.0"
20
+ "@e-mc/db": "^0.14.3",
21
+ "@e-mc/request": "^0.14.3",
22
+ "@e-mc/types": "^0.14.3",
23
+ "mongodb": "^7.3.0"
27
24
  }
28
25
  }