@pi-r/mongodb 0.3.5 → 0.3.6

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,7 +1,7 @@
1
- Copyright 2024 An Pham
2
-
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
-
5
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
-
1
+ Copyright 2023 An Pham
2
+
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
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
7
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,7 +1,5 @@
1
- # @pi-r/mongodb
2
-
3
- PEP 402 - Forever
4
-
5
- ## LICENSE
6
-
1
+ # @pi-r/mongodb
2
+
3
+ ## LICENSE
4
+
7
5
  MIT
package/client/index.d.ts CHANGED
@@ -1,12 +1,12 @@
1
- import type { IDbSourceClient } from '@e-mc/db/types';
2
-
3
- import type { MongoDBDataSource, MongoDBFilterValue, MongoDBSortValue } from '../types';
4
-
5
- import type { CommandOperationOptions, Document, Filter, Sort, SortDirection } from 'mongodb';
6
-
7
- declare const MongoDB: IDbSourceClient<MongoDBDataSource> & {
8
- getFilterValue(query: Filter<Document> | MongoDBFilterValue, coerce?: boolean): [Filter<Document>, CommandOperationOptions?];
9
- getSortValue(sort: Sort | string | MongoDBSortValue, coerce?: boolean): [Sort | string, SortDirection?];
10
- };
11
-
1
+ import type { IDbSourceClient } from '@e-mc/db/types';
2
+
3
+ import type { MongoDBDataSource, MongoDBFilterValue, MongoDBSortValue } from '../types';
4
+
5
+ import type { CommandOperationOptions, Document, Filter, Sort, SortDirection } from 'mongodb';
6
+
7
+ declare const MongoDB: IDbSourceClient<MongoDBDataSource> & {
8
+ getFilterValue(query: Filter<Document> | MongoDBFilterValue, coerce?: boolean): [Filter<Document>, CommandOperationOptions?];
9
+ getSortValue(sort: Sort | string | MongoDBSortValue, coerce?: boolean): [Sort | string, SortDirection?];
10
+ };
11
+
12
12
  export = MongoDB;
package/client/index.js CHANGED
@@ -2,7 +2,6 @@
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");
6
5
  const types_1 = require("@e-mc/types");
7
6
  const util_1 = require("@e-mc/db/util");
8
7
  const Db = require("@e-mc/db");
@@ -26,7 +25,7 @@ class MongoDBPool extends DbPool {
26
25
  return this.client.close(force);
27
26
  }
28
27
  isEmpty() {
29
- var _a, _b;
28
+ var _a;
30
29
  if (this.closed) {
31
30
  return true;
32
31
  }
@@ -38,7 +37,7 @@ class MongoDBPool extends DbPool {
38
37
  return (0, util_1.checkEmpty)(topology[name]);
39
38
  }
40
39
  }
41
- return (0, util_1.checkEmpty)((_a = topology.s) === null || _a === void 0 ? void 0 : _a.sessions) && (0, util_1.checkEmpty)((_b = topology.s) === null || _b === void 0 ? void 0 : _b.servers);
40
+ return (0, util_1.checkEmpty)((_a = topology.s) === null || _a === void 0 ? void 0 : _a.sessions);
42
41
  }
43
42
  return false;
44
43
  }
@@ -347,7 +346,8 @@ function setCredential(item) {
347
346
  }
348
347
  }
349
348
  }
350
- new MongoDBPool(new mongodb.MongoClient(item.uri, options), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
349
+ const db = require("mongodb");
350
+ new MongoDBPool(new db.MongoClient(item.uri, options), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
351
351
  }
352
352
  else {
353
353
  pool.add(item);
@@ -368,6 +368,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
368
368
  if (length === 0) {
369
369
  return [];
370
370
  }
371
+ const db = require("mongodb");
371
372
  let parallel, checkObject, connectOnce, errorQuery, sessionKey, outCacheMiss;
372
373
  if ((0, types_1.isPlainObject)(options)) {
373
374
  ({ parallel, checkObject, connectOnce, errorQuery, sessionKey, outCacheMiss } = options);
@@ -415,7 +416,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
415
416
  credential.options.maxPoolSize = size;
416
417
  }
417
418
  }
418
- clients.push(client = await new mongodb.MongoClient(credential.uri, credential.options).connect());
419
+ clients.push(client = await new db.MongoClient(credential.uri, credential.options).connect());
419
420
  }
420
421
  if (connectOnce) {
421
422
  mongoClient = client;
@@ -464,7 +465,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
464
465
  pipeline = Array.isArray(aggregate) ? aggregate : aggregate.pipeline;
465
466
  }
466
467
  if (id && (!query || limit === 1) && !pipeline) {
467
- query = { '_id': new mongodb.ObjectId(id) };
468
+ query = { '_id': new db.ObjectId(id) };
468
469
  limit = 1;
469
470
  }
470
471
  if (queryString) {
@@ -569,7 +570,6 @@ async function executeBatchQuery(batch, options = '', outResult) {
569
570
  if (updateOptions && coercing) {
570
571
  (0, types_1.coerceObject)(updateOptions);
571
572
  }
572
- await collection.updateMany(filter, update, updateOptions);
573
573
  }
574
574
  cursor = collection.find(filter, command);
575
575
  }
package/client/pool.js ADDED
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ const util_1 = require("@e-mc/db/util");
3
+ const DbPool = require('@e-mc/db/pool');
4
+ const POOL_ACTIVE = new WeakSet();
5
+ class MongoDBPool extends DbPool {
6
+ static CACHE_UNUSED = ['maxPoolSize', 'minPoolSize', 'connectTimeoutMS', 'socketTimeoutMS', 'maxConnecting', 'maxIdleTimeMS', 'waitQueueTimeoutMS', 'maxStalenessSeconds', 'localThresholdMS', 'serverSelectionTimeoutMS', 'heartbeatFrequencyMS', 'minHeartbeatFrequencyMS', 'timeoutMS', 'compressors', 'zlibCompressionLevel', 'srvMaxHosts', 'mongodbLogPath', 'mongodbLogComponentSeverities', 'mongodbLogMaxDocumentLength'];
7
+ static CACHE_IGNORE = ['pkFactory'];
8
+ static asString(credential) {
9
+ const { options, uri } = credential;
10
+ if (uri && options) {
11
+ return uri + JSON.stringify(options);
12
+ }
13
+ return super.asString(credential);
14
+ }
15
+ static sanitize(credential) {
16
+ if (!this.canCache(credential)) {
17
+ return;
18
+ }
19
+ const target = credential.options;
20
+ if (!target || !POOL_ACTIVE.has(credential) || credential.uuidKey) {
21
+ return credential;
22
+ }
23
+ const options = super.sanitize(target);
24
+ return options !== target ? { uri: credential.uri, options } : credential;
25
+ }
26
+ async detach(force) {
27
+ this.remove();
28
+ if (this.closed) {
29
+ return;
30
+ }
31
+ return this.close(force);
32
+ }
33
+ get closeable() {
34
+ const topology = this.client.topology;
35
+ return topology ? super.closeable || topology.s.state === 'closing' : true;
36
+ }
37
+ async getConnection(credential) {
38
+ if (credential) {
39
+ POOL_ACTIVE.add(credential);
40
+ }
41
+ return this.client.connect();
42
+ }
43
+ async close(force = false) {
44
+ return this.client.close(force);
45
+ }
46
+ isEmpty() {
47
+ if (this.closed) {
48
+ return true;
49
+ }
50
+ const topology = this.client.topology;
51
+ if (topology) {
52
+ const name = Object.getOwnPropertySymbols(topology).find(sym => sym.toString() === 'Symbol(waitQueue)');
53
+ return (0, util_1.checkEmpty)(name ? topology[name] : topology.s.servers);
54
+ }
55
+ return false;
56
+ }
57
+ get closed() {
58
+ const topology = this.client.topology;
59
+ if (topology && (this.success > 0 || this.failed > 0)) {
60
+ return topology.isDestroyed();
61
+ }
62
+ return this.client.s?.hasBeenClosed === true;
63
+ }
64
+ }
65
+ module.exports = MongoDBPool;
package/package.json CHANGED
@@ -1,29 +1,29 @@
1
- {
2
- "name": "@pi-r/mongodb",
3
- "version": "0.3.5",
4
- "description": "MongoDB client driver for E-mc.",
5
- "main": "client/index.js",
6
- "types": "client/index.d.ts",
7
- "publishConfig": {
8
- "access": "public"
9
- },
10
- "repository": {
11
- "type": "git",
12
- "url": "git+https://github.com/anpham6/pi-r.git",
13
- "directory": "src/db/mongodb"
14
- },
15
- "keywords": [
16
- "squared",
17
- "e-mc",
18
- "squared-functions"
19
- ],
20
- "author": "An Pham <anpham6@gmail.com>",
21
- "license": "MIT",
22
- "homepage": "https://github.com/anpham6/pi-r#readme",
23
- "dependencies": {
24
- "@e-mc/db": "^0.6.7",
25
- "@e-mc/request": "^0.6.7",
26
- "@e-mc/types": "^0.6.7",
27
- "mongodb": "^5.9.2"
28
- }
29
- }
1
+ {
2
+ "name": "@pi-r/mongodb",
3
+ "version": "0.3.6",
4
+ "description": "MongoDB client driver for E-mc.",
5
+ "main": "client/index.js",
6
+ "types": "client/index.d.ts",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/anpham6/pi-r.git",
13
+ "directory": "src/db/mongodb"
14
+ },
15
+ "keywords": [
16
+ "squared",
17
+ "e-mc",
18
+ "squared-functions"
19
+ ],
20
+ "author": "An Pham <anpham6@gmail.com>",
21
+ "license": "MIT",
22
+ "homepage": "https://github.com/anpham6/pi-r#readme",
23
+ "dependencies": {
24
+ "@e-mc/db": "^0.6.16",
25
+ "@e-mc/request": "^0.6.16",
26
+ "@e-mc/types": "^0.6.16",
27
+ "mongodb": "^5.9.2"
28
+ }
29
+ }
package/types/index.d.ts CHANGED
@@ -1,46 +1,46 @@
1
- import type { DbDataSource } from '@e-mc/types/lib/squared';
2
-
3
- import type { CascadeAction, ServerAuth } from '@e-mc/types/lib/db';
4
-
5
- import type { AggregateOptions, CollectionOptions, CommandOperationOptions, DbOptions, Document, Filter, MongoClientOptions, OptionalUnlessRequiredId, Sort, SortDirection, UpdateFilter } from 'mongodb';
6
-
7
- export interface MongoDBDataSource extends DbDataSource<Filter<Document> | MongoDBQueryWithOptions, MongoClientOptions, UpdateFilter<Document> | OptionalUnlessRequiredId<unknown>[], string | MongoDBCredential, string>, MongoDBClientDBOptions, CascadeAction {
8
- source: "mongodb";
9
- id?: string;
10
- updateType?: 0 | 1 | 2 | 3;
11
- }
12
-
13
- export interface MongoDBCredential extends ServerAuth, Pick<MongoClientOptions, "auth" | "authMechanism" | "authSource" | "tlsCAFile" | "tlsCertificateFile" | "tlsCertificateKeyFile" | "tlsCertificateKeyFilePassword" | "sslCA" | "sslKey" | "sslCert"> {}
14
-
15
- export interface MongoDBSortValue {
16
- value?: Sort;
17
- direction?: SortDirection;
18
- }
19
-
20
- export interface MongoDBAggregate {
21
- pipeline?: Document[];
22
- options?: AggregateOptions;
23
- }
24
-
25
- export interface MongoDBClientDBOptions {
26
- client?: {
27
- db?: DbOptions;
28
- collection?: CollectionOptions;
29
- };
30
- execute?: {
31
- insert?: CommandOperationOptions;
32
- update?: CommandOperationOptions;
33
- };
34
- aggregate?: MongoDBAggregate;
35
- sort?: Sort | string | MongoDBSortValue;
36
- }
37
-
38
- export interface MongoDBQueryWithOptions {
39
- value: Filter<Document>;
40
- options: CommandOperationOptions;
41
- }
42
-
43
- export interface MongoDBFilterValue {
44
- value?: Filter<Document>;
45
- options?: CommandOperationOptions;
1
+ import type { DbDataSource } from '@e-mc/types/lib/squared';
2
+
3
+ import type { CascadeAction, ServerAuth } from '@e-mc/types/lib/db';
4
+
5
+ import type { AggregateOptions, CollectionOptions, CommandOperationOptions, DbOptions, Document, Filter, MongoClientOptions, OptionalUnlessRequiredId, Sort, SortDirection, UpdateFilter } from 'mongodb';
6
+
7
+ export interface MongoDBDataSource extends DbDataSource<Filter<Document> | MongoDBQueryWithOptions, MongoClientOptions, UpdateFilter<Document> | OptionalUnlessRequiredId<unknown>[], string | MongoDBCredential, string>, MongoDBClientDBOptions, CascadeAction {
8
+ source: "mongodb";
9
+ id?: string;
10
+ updateType?: 0 | 1 | 2 | 3;
11
+ }
12
+
13
+ export interface MongoDBCredential extends ServerAuth, Pick<MongoClientOptions, "auth" | "authMechanism" | "authSource" | "tlsCAFile" | "tlsCertificateFile" | "tlsCertificateKeyFile" | "tlsCertificateKeyFilePassword" | "sslCA" | "sslKey" | "sslCert"> {}
14
+
15
+ export interface MongoDBSortValue {
16
+ value?: Sort;
17
+ direction?: SortDirection;
18
+ }
19
+
20
+ export interface MongoDBAggregate {
21
+ pipeline?: Document[];
22
+ options?: AggregateOptions;
23
+ }
24
+
25
+ export interface MongoDBClientDBOptions {
26
+ client?: {
27
+ db?: DbOptions;
28
+ collection?: CollectionOptions;
29
+ };
30
+ execute?: {
31
+ insert?: CommandOperationOptions;
32
+ update?: CommandOperationOptions;
33
+ };
34
+ aggregate?: MongoDBAggregate;
35
+ sort?: Sort | string | MongoDBSortValue;
36
+ }
37
+
38
+ export interface MongoDBQueryWithOptions {
39
+ value: Filter<Document>;
40
+ options: CommandOperationOptions;
41
+ }
42
+
43
+ export interface MongoDBFilterValue {
44
+ value?: Filter<Document>;
45
+ options?: CommandOperationOptions;
46
46
  }