@pi-r/mongodb 0.2.2 → 0.3.0

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
@@ -2,15 +2,16 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DB_SOURCE_TYPE = exports.DB_SOURCE_CLIENT = exports.getSortValue = exports.getFilterValue = exports.checkTimeout = exports.executeBatchQuery = exports.executeQuery = exports.setCredential = void 0;
4
4
  const path = require("path");
5
+ const mongodb = require("mongodb");
5
6
  const types_1 = require("@e-mc/types");
6
7
  const util_1 = require("@e-mc/db/util");
7
8
  const Db = require("@e-mc/db");
8
9
  const Request = require("@e-mc/request");
9
10
  const DbPool = require('@e-mc/db/pool');
10
- const MONGODB_V3 = Db.checkSemVer("mongodb" /* STRINGS.PACKAGE_NAME */, 1, 4);
11
- const MONGODB_V330 = Db.checkSemVer("mongodb" /* STRINGS.PACKAGE_NAME */, '3.3.0');
12
- const MONGODB_V333 = Db.checkSemVer("mongodb" /* STRINGS.PACKAGE_NAME */, '3.3.0', '3.3.3');
13
- const MONGODB_V360 = Db.checkSemVer("mongodb" /* STRINGS.PACKAGE_NAME */, '3.6.0');
11
+ const MONGODB_V3 = Db.checkSemVer("mongodb" /* STRINGS.MODULE_NAME */, 1, 4);
12
+ const MONGODB_V330 = Db.checkSemVer("mongodb" /* STRINGS.MODULE_NAME */, '3.3.0');
13
+ const MONGODB_V333 = Db.checkSemVer("mongodb" /* STRINGS.MODULE_NAME */, '3.3.0', '3.3.3');
14
+ const MONGODB_V360 = Db.checkSemVer("mongodb" /* STRINGS.MODULE_NAME */, '3.6.0');
14
15
  const MONGODB_TOPOLOGY = MONGODB_V3 && MONGODB_V330;
15
16
  const MONGODB_NOPOOL = MONGODB_V3 && !MONGODB_V330;
16
17
  class MongoDBPool extends DbPool {
@@ -58,18 +59,26 @@ class MongoDBPool extends DbPool {
58
59
  }
59
60
  }
60
61
  const POOL_STATE = {};
62
+ const POOL_UNUSED = ['maxPoolSize', 'minPoolSize', 'maxConnecting', 'maxIdleTimeMS', 'waitQueueTimeoutMS', 'serverSelectionTimeoutMS', 'heartbeatFrequencyMS', 'minHeartbeatFrequencyMS', 'keepAliveInitialDelay', 'compressors', 'zlibCompressionLevel', 'srvMaxHosts'];
61
63
  function removePoolProperties(credential) {
62
- let options = credential.options;
64
+ const options = credential.options;
63
65
  if (options && !credential.uuidKey) {
66
+ let result;
64
67
  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 };
68
+ for (let i = 0, length = POOL_UNUSED.length; i < length; ++i) {
69
+ const attr = POOL_UNUSED[i];
70
+ if (attr in credential) {
71
+ result || (result = { ...options });
72
+ delete result[attr];
73
+ }
67
74
  }
68
75
  }
69
76
  else if ('poolSize' in options || 'minSize' in options) {
70
- options = { ...options, poolSize: undefined, minSize: undefined };
77
+ result = { ...options, poolSize: undefined, minSize: undefined };
78
+ }
79
+ if (result) {
80
+ return { uri: credential.uri, options: result };
71
81
  }
72
- return { uri: credential.uri, options };
73
82
  }
74
83
  return credential;
75
84
  }
@@ -334,8 +343,7 @@ function setCredential(item) {
334
343
  }
335
344
  }
336
345
  }
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;
346
+ new MongoDBPool(new mongodb.MongoClient(item.uri, options), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
339
347
  }
340
348
  else {
341
349
  pool.add(item);
@@ -356,7 +364,6 @@ async function executeBatchQuery(batch, options = '', outResult) {
356
364
  if (length === 0) {
357
365
  return [];
358
366
  }
359
- const db = require("mongodb" /* STRINGS.PACKAGE_NAME */);
360
367
  let parallel, checkObject, connectOnce, errorQuery, sessionKey, outCacheMiss;
361
368
  if ((0, types_1.isPlainObject)(options)) {
362
369
  ({ parallel, checkObject, connectOnce, errorQuery, sessionKey, outCacheMiss } = options);
@@ -404,7 +411,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
404
411
  credential.options.maxPoolSize = size;
405
412
  }
406
413
  }
407
- clients.push(client = await new db.MongoClient(credential.uri, credential.options).connect());
414
+ clients.push(client = await new mongodb.MongoClient(credential.uri, credential.options).connect());
408
415
  }
409
416
  if (connectOnce) {
410
417
  mongoClient = client;
@@ -453,7 +460,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
453
460
  pipeline = Array.isArray(aggregate) ? aggregate : aggregate.pipeline;
454
461
  }
455
462
  if (id && (!query || limit === 1) && !pipeline) {
456
- query = { '_id': new db.ObjectId(id) };
463
+ query = { '_id': new mongodb.ObjectId(id) };
457
464
  limit = 1;
458
465
  }
459
466
  if (queryString) {
@@ -524,11 +531,14 @@ async function executeBatchQuery(batch, options = '', outResult) {
524
531
  if ((0, types_1.isArray)(update)) {
525
532
  commandType = this.commandType.INSERT;
526
533
  const insertOptions = item.execute?.insert;
527
- if (insertOptions) {
528
- await collection.insertMany(update, coercing ? (0, types_1.coerceObject)(insertOptions) : insertOptions);
534
+ if (insertOptions && coercing) {
535
+ (0, types_1.coerceObject)(insertOptions);
536
+ }
537
+ if (update.length === 1) {
538
+ await collection.insertOne(update[0], insertOptions);
529
539
  }
530
540
  else {
531
- await collection.insertMany(update);
541
+ await collection.insertMany(update, insertOptions);
532
542
  }
533
543
  update = undefined;
534
544
  }
@@ -550,7 +560,11 @@ async function executeBatchQuery(batch, options = '', outResult) {
550
560
  else {
551
561
  if (update) {
552
562
  commandType = this.commandType.UPDATE;
553
- await collection.updateMany(filter, update);
563
+ const updateOptions = item.execute?.update;
564
+ if (updateOptions && coercing) {
565
+ (0, types_1.coerceObject)(updateOptions);
566
+ }
567
+ await collection.updateMany(filter, update, updateOptions);
554
568
  }
555
569
  cursor = collection.find(filter, command);
556
570
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/mongodb",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
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.5.3",
25
- "@e-mc/request": "^0.5.3",
26
- "@e-mc/types": "^0.5.3",
27
- "mongodb": "^5.5.0"
24
+ "@e-mc/db": "^0.6.0",
25
+ "@e-mc/request": "^0.6.0",
26
+ "@e-mc/types": "^0.6.0",
27
+ "mongodb": "^5.7.0"
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, BulkWriteOptions, CollectionOptions, CommandOperationOptions, DbOptions, Document, Filter, MongoClientOptions, OptionalUnlessRequiredId, Sort, SortDirection, UpdateFilter } from 'mongodb';
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?: BulkWriteOptions;
31
+ insert?: CommandOperationOptions;
32
+ update?: CommandOperationOptions;
32
33
  };
33
34
  aggregate?: MongoDBAggregate;
34
35
  sort?: Sort | string | MongoDBSortValue;