@pi-r/mongodb 0.6.7 → 0.7.1

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,12 +1,9 @@
1
- # @pi-r/mongodb
1
+ ### @pi-r/mongodb
2
2
 
3
- PEP 402 - Forever Dodgers
4
-
5
- ## Documentation
3
+ https://e-mc.readthedocs.io/en/latest/db/mongodb.html
6
4
 
7
- - [E-mc](https://e-mc.readthedocs.io/en/latest/db/mongodb.html)
8
- - [squared](https://squared.readthedocs.io)
5
+ PEP 402 - Forever Dodgers
9
6
 
10
- ## LICENSE
7
+ ### LICENSE
11
8
 
12
9
  MIT
package/client/index.js CHANGED
@@ -37,12 +37,11 @@ class MongoDBPool extends DbPool {
37
37
  return topology ? super.closeable || topology.s.state === 'closing' : true;
38
38
  }
39
39
  get closed() {
40
- var _a;
41
40
  const topology = this.client.topology;
42
41
  if (topology && (this.success > 0 || this.failed > 0)) {
43
42
  return topology.isDestroyed();
44
43
  }
45
- return ((_a = this.client.s) === null || _a === void 0 ? void 0 : _a.hasBeenClosed) === true;
44
+ return this.client.s?.hasBeenClosed === true;
46
45
  }
47
46
  }
48
47
  const POOL_STATE = {};
@@ -55,7 +54,7 @@ function removePoolProperties(credential) {
55
54
  for (let i = 0, length = POOL_UNUSED.length; i < length; ++i) {
56
55
  const attr = POOL_UNUSED[i];
57
56
  if (attr in options) {
58
- result || (result = { ...options });
57
+ result ||= { ...options };
59
58
  delete result[attr];
60
59
  }
61
60
  }
@@ -67,7 +66,7 @@ function removePoolProperties(credential) {
67
66
  }
68
67
  function getCacheObject(item) {
69
68
  const credential = item.credential;
70
- return { uri: item.uri, options: item.options, uuidKey: credential === null || credential === void 0 ? void 0 : credential.uuidKey };
69
+ return { uri: item.uri, options: item.options, uuidKey: credential?.uuidKey };
71
70
  }
72
71
  function getPoolKey(credential) {
73
72
  const { options, uri } = credential;
@@ -76,19 +75,18 @@ function getPoolKey(credential) {
76
75
  }
77
76
  }
78
77
  function setCredential(item) {
79
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
80
78
  const credential = this.getCredential(item);
81
- const options = item.options || (item.options = {});
79
+ const options = item.options ||= {};
82
80
  let uri = item.uri;
83
81
  if (credential) {
84
82
  let { protocol, server, hostname, port, username, password } = (0, util_1.parseServerAuth)(credential, 27017), { auth, authMechanism = 'DEFAULT', authSource, authMechanismProperties } = credential, authentication = '';
85
83
  if (authMechanism === 'GSSAPI') {
86
- const principal = (auth === null || auth === void 0 ? void 0 : auth.username) || username || ((_a = options.auth) === null || _a === void 0 ? void 0 : _a.username);
84
+ const principal = auth?.username || username || options.auth?.username;
87
85
  if ((0, types_1.isString)(principal)) {
88
86
  authentication = encodeURIComponent(principal) + '@';
89
87
  }
90
88
  }
91
- else if (auth === null || auth === void 0 ? void 0 : auth.username) {
89
+ else if (auth?.username) {
92
90
  options.auth = auth;
93
91
  }
94
92
  if (!uri || !Db.isURL(uri)) {
@@ -104,7 +102,7 @@ function setCredential(item) {
104
102
  uri = (protocol || 'mongodb:') + '//' + authentication + server;
105
103
  }
106
104
  else {
107
- options.auth || (options.auth = { username, password });
105
+ options.auth ||= { username, password };
108
106
  }
109
107
  if (!uri.includes('?')) {
110
108
  uri += (uri.endsWith('/') ? '' : '/') + '?';
@@ -146,7 +144,7 @@ function setCredential(item) {
146
144
  else {
147
145
  throw (0, types_1.errorMessage)("mongodb", 'Missing TLS/SSL credentials');
148
146
  }
149
- authSource || (authSource = '$external');
147
+ authSource ||= '$external';
150
148
  break;
151
149
  }
152
150
  case 'GSSAPI':
@@ -154,12 +152,12 @@ function setCredential(item) {
154
152
  if ((0, types_1.isObject)(authMechanismProperties)) {
155
153
  options.authMechanismProperties = authMechanismProperties;
156
154
  }
157
- authSource || (authSource = '$external');
155
+ authSource ||= '$external';
158
156
  break;
159
157
  }
160
158
  case 'PLAIN':
161
159
  uri += '&maxPoolSize=1';
162
- authSource || (authSource = '$external');
160
+ authSource ||= '$external';
163
161
  break;
164
162
  }
165
163
  if (authSource) {
@@ -176,7 +174,7 @@ function setCredential(item) {
176
174
  }
177
175
  let username, password, pool;
178
176
  if ((0, types_1.isString)(usePool)) {
179
- if (username = ((_b = options.auth) === null || _b === void 0 ? void 0 : _b.username) || item.uri && ((_c = (0, util_1.parseConnectionString)(item.uri)) === null || _c === void 0 ? void 0 : _c.username)) {
177
+ if (username = options.auth?.username || item.uri && (0, util_1.parseConnectionString)(item.uri)?.username) {
180
178
  [password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
181
179
  if (pool) {
182
180
  pool.add(item, password);
@@ -192,25 +190,25 @@ function setCredential(item) {
192
190
  if (config) {
193
191
  const { min, max, idle, queue_max, queue_idle, timeout, socket_timeout } = config;
194
192
  if (min >= 0) {
195
- (_d = options.minPoolSize) !== null && _d !== void 0 ? _d : (options.minPoolSize = min);
193
+ options.minPoolSize ??= min;
196
194
  }
197
195
  if (max > 0) {
198
- (_e = options.maxPoolSize) !== null && _e !== void 0 ? _e : (options.maxPoolSize = max);
196
+ options.maxPoolSize ??= max;
199
197
  }
200
198
  if (idle >= 0) {
201
- (_f = options.maxIdleTimeMS) !== null && _f !== void 0 ? _f : (options.maxIdleTimeMS = idle);
199
+ options.maxIdleTimeMS ??= idle;
202
200
  }
203
201
  if (queue_max > 0) {
204
- (_g = options.maxConnecting) !== null && _g !== void 0 ? _g : (options.maxConnecting = queue_max);
202
+ options.maxConnecting ??= queue_max;
205
203
  }
206
204
  if (queue_idle >= 0) {
207
- (_h = options.waitQueueTimeoutMS) !== null && _h !== void 0 ? _h : (options.waitQueueTimeoutMS = queue_idle);
205
+ options.waitQueueTimeoutMS ??= queue_idle;
208
206
  }
209
207
  if (timeout > 0) {
210
- (_j = options.connectTimeoutMS) !== null && _j !== void 0 ? _j : (options.connectTimeoutMS = timeout);
208
+ options.connectTimeoutMS ??= timeout;
211
209
  }
212
210
  if (socket_timeout > 0) {
213
- (_k = options.socketTimeoutMS) !== null && _k !== void 0 ? _k : (options.socketTimeoutMS = socket_timeout);
211
+ options.socketTimeoutMS ??= socket_timeout;
214
212
  }
215
213
  }
216
214
  const poolKey = getPoolKey(item);
@@ -226,7 +224,6 @@ async function executeQuery(item, options) {
226
224
  }
227
225
  exports.executeQuery = executeQuery;
228
226
  async function executeBatchQuery(batch, options = '', outResult) {
229
- var _a;
230
227
  const length = batch.length;
231
228
  if (length === 0) {
232
229
  return [];
@@ -249,7 +246,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
249
246
  parallel = !batch.some(item => item.parallel === false);
250
247
  }
251
248
  if (!parallel) {
252
- outResult || (outResult = new Array(length));
249
+ outResult ||= new Array(length);
253
250
  }
254
251
  const caching = this.hasCache("mongodb", sessionKey);
255
252
  const tasks = new Array(length);
@@ -288,7 +285,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
288
285
  item.transactionState &= ~64;
289
286
  return client;
290
287
  };
291
- if ((_a = this.host) === null || _a === void 0 ? void 0 : _a.username) {
288
+ if (this.host?.username) {
292
289
  this.applyCommand(...batch);
293
290
  }
294
291
  for (let i = 0; i < length; ++i) {
@@ -314,9 +311,9 @@ async function executeBatchQuery(batch, options = '', outResult) {
314
311
  continue;
315
312
  }
316
313
  item.transactionState = 1;
317
- const getInstance = (target) => target.db(name, client === null || client === void 0 ? void 0 : client.db);
318
- const getCollection = (target) => getInstance(target).collection(table, client === null || client === void 0 ? void 0 : client.collection);
319
- const runCommand = async (target, document) => getInstance(target).command(document, client === null || client === void 0 ? void 0 : client.command);
314
+ const getInstance = (target) => target.db(name, client?.db);
315
+ const getCollection = (target) => getInstance(target).collection(table, client?.collection);
316
+ const runCommand = async (target, document) => getInstance(target).command(document, client?.command);
320
317
  const credential = mongoCredential || onceCredential || getCacheObject(item);
321
318
  const cacheValue = ignoreCache === undefined ? sessionKey : Array.isArray(ignoreCache) ? { sessionKey, exclusiveOf: ignoreCache } : { sessionKey, renewCache: ignoreCache === 0 };
322
319
  const coercing = this.hasCoerce("mongodb", 'options', credential.uuidKey);
@@ -374,7 +371,6 @@ async function executeBatchQuery(batch, options = '', outResult) {
374
371
  onceCredential = undefined;
375
372
  }
376
373
  tasks[i] = new Promise(async (resolve, reject) => {
377
- var _a, _b;
378
374
  let commandType;
379
375
  try {
380
376
  const instance = mongoClient || await getConnection(item, credential);
@@ -389,7 +385,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
389
385
  }
390
386
  if (Array.isArray(update)) {
391
387
  commandType = this.commandType.INSERT;
392
- const insertOptions = (_a = item.execute) === null || _a === void 0 ? void 0 : _a.insert;
388
+ const insertOptions = item.execute?.insert;
393
389
  if (insertOptions && coercing) {
394
390
  (0, types_1.coerceObject)(insertOptions);
395
391
  }
@@ -437,7 +433,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
437
433
  else {
438
434
  if (update && updateType === 1) {
439
435
  commandType = this.commandType.UPDATE;
440
- const updateOptions = (_b = item.execute) === null || _b === void 0 ? void 0 : _b.update;
436
+ const updateOptions = item.execute?.update;
441
437
  if (updateOptions && coercing) {
442
438
  (0, types_1.coerceObject)(updateOptions);
443
439
  }
@@ -526,7 +522,9 @@ async function executeBatchQuery(batch, options = '', outResult) {
526
522
  }
527
523
  }
528
524
  return this.processRows(batch, tasks, {
529
- disconnect: () => clients.forEach(async (item) => item.close(true)),
525
+ disconnect: () => clients.forEach(item => {
526
+ item.close(true);
527
+ }),
530
528
  parallel
531
529
  }, outResult);
532
530
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/mongodb",
3
- "version": "0.6.7",
3
+ "version": "0.7.1",
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.8",
25
- "@e-mc/request": "^0.8.8",
26
- "@e-mc/types": "^0.8.8",
27
- "mongodb": "^6.5.0"
24
+ "@e-mc/db": "^0.9.2",
25
+ "@e-mc/request": "^0.9.2",
26
+ "@e-mc/types": "^0.9.2",
27
+ "mongodb": "^6.6.2"
28
28
  }
29
29
  }
package/types/index.d.ts CHANGED
@@ -4,7 +4,7 @@ import type { CascadeAction, ServerAuth } from '@e-mc/types/lib/db';
4
4
 
5
5
  import type { AggregateOptions, CollectionOptions, CommandOperationOptions, DbOptions, Document, Filter, MongoClientOptions, OptionalUnlessRequiredId, RunCommandOptions, Sort, SortDirection, UpdateFilter } from 'mongodb';
6
6
 
7
- export interface MongoDBDataSource extends DbDataSource<Filter<Document> | MongoDBQueryWithOptions, MongoClientOptions, UpdateFilter<Document> | OptionalUnlessRequiredId<unknown>[], string | MongoDBCredential, string>, MongoDBClientDBOptions, CascadeAction {
7
+ export interface MongoDBDataSource extends DbDataSource<Filter<Document> | MongoDBFilterValue, MongoClientOptions, UpdateFilter<Document> | OptionalUnlessRequiredId<unknown>[], MongoDBCredential, string>, MongoDBClientDBOptions, CascadeAction {
8
8
  source: "mongodb";
9
9
  id?: string;
10
10
  updateType?: 0 | 1 | 2 | 3;
@@ -37,11 +37,6 @@ export interface MongoDBClientDBOptions {
37
37
  sort?: Sort | string | MongoDBSortValue;
38
38
  }
39
39
 
40
- export interface MongoDBQueryWithOptions {
41
- value: Filter<Document>;
42
- options: CommandOperationOptions;
43
- }
44
-
45
40
  export interface MongoDBFilterValue {
46
41
  value?: Filter<Document>;
47
42
  options?: CommandOperationOptions;