@pi-r/mongodb 0.8.3 → 0.9.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/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
-
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.
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
+
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
@@ -4,4 +4,4 @@ https://e-mc.readthedocs.io/en/latest/db/mongodb.html
4
4
 
5
5
  ### LICENSE
6
6
 
7
- MIT
7
+ MIT
package/client/index.d.ts CHANGED
@@ -1,12 +1,15 @@
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 { IModule } from '@e-mc/types/lib';
2
+
3
+ import type { IDbSourceClient } from '@e-mc/db/types';
4
+
5
+ import type { MongoDBDataSource, MongoDBFilterValue, MongoDBSortValue } from '../types';
6
+
7
+ import type { Collection, CommandOperationOptions, Document, Filter, MongoClient, Sort, SortDirection } from 'mongodb';
8
+
9
+ declare const MongoDB: IDbSourceClient<MongoDBDataSource> & {
10
+ getFilterValue(query: Filter<Document> | MongoDBFilterValue, coerce?: boolean): [Filter<Document>, CommandOperationOptions?];
11
+ getSortValue(sort: Sort | string | MongoDBSortValue, coerce?: boolean): [Sort | string, SortDirection?];
12
+ initCollection(this: IModule, target: MongoClient, item: Pick<MongoDBDataSource, "bulkWrite" | "client" | "command" | "name" | "table">): Promise<Collection>;
13
+ };
14
+
12
15
  export = MongoDB;
package/client/index.js CHANGED
@@ -4,9 +4,10 @@ exports.setCredential = setCredential;
4
4
  exports.executeQuery = executeQuery;
5
5
  exports.executeBatchQuery = executeBatchQuery;
6
6
  exports.checkTimeout = checkTimeout;
7
+ exports.initCollection = initCollection;
7
8
  exports.getFilterValue = getFilterValue;
8
9
  exports.getSortValue = getSortValue;
9
- const path = require("path");
10
+ const path = require("node:path");
10
11
  const mongodb = require("mongodb");
11
12
  const Db = require("@e-mc/db");
12
13
  const Request = require("@e-mc/request");
@@ -291,15 +292,12 @@ async function executeBatchQuery(batch, options = '', outResult) {
291
292
  continue;
292
293
  }
293
294
  item.transactionState = 1;
294
- const getInstance = (target) => target.db(name, client?.db);
295
- const getCollection = (target) => getInstance(target).collection(table, client?.collection);
296
- const runCommand = async (target, document) => getInstance(target).command(document, client?.command);
297
295
  const credential = mongoCredential || onceCredential || getCacheObject(item);
298
296
  const cacheValue = ignoreCache === undefined ? sessionKey : Array.isArray(ignoreCache) ? { sessionKey, exclusiveOf: ignoreCache } : { sessionKey, renewCache: ignoreCache === 0 };
299
297
  const coercing = this.hasCoerce("mongodb", 'options', credential.uuidKey);
300
298
  const targetObject = typeof checkObject === 'string' ? coercing && (0, types_1.asFunction)(checkObject) : checkObject;
301
- if (typeof streamRow === 'string') {
302
- streamRow = coercing && (0, types_1.asFunction)(streamRow);
299
+ if (coercing && typeof streamRow === 'string') {
300
+ streamRow = (0, types_1.asFunction)(streamRow);
303
301
  }
304
302
  let queryString = '', rows, pipeline;
305
303
  if (caching && ignoreCache !== true && (!targetObject || typeof cacheObjectKey === 'string') && !streamRow) {
@@ -354,15 +352,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
354
352
  let commandType;
355
353
  try {
356
354
  const instance = mongoClient || await getConnection(item, credential);
357
- if (item.command) {
358
- await Promise.all((!Array.isArray(item.command) ? [item.command] : item.command).map(async (cmd) => runCommand(instance, cmd)))
359
- .then(items => {
360
- for (const output of items) {
361
- this.addLog(this.statusType.INFO, output, "mongodb", 'runCommand');
362
- }
363
- });
364
- }
365
- const collection = getCollection(instance);
355
+ const collection = await initCollection.call(this, instance, item);
366
356
  if (update && coercing) {
367
357
  (0, types_1.coerceObject)(update);
368
358
  }
@@ -518,6 +508,29 @@ async function executeBatchQuery(batch, options = '', outResult) {
518
508
  async function checkTimeout(value, limit = 0) {
519
509
  return DbPool.checkTimeout(POOL_STATE, value, limit);
520
510
  }
511
+ async function initCollection(target, item) {
512
+ const db = target.db(item.name, item.client?.db);
513
+ if (item.command) {
514
+ await Promise.all((!Array.isArray(item.command) ? [item.command] : item.command).map(async (cmd) => db.command(document, cmd)))
515
+ .then(items => {
516
+ for (const output of items) {
517
+ this.addLog(this.statusType.INFO, output, this.moduleName, 'runCommand');
518
+ }
519
+ });
520
+ }
521
+ if (item.bulkWrite) {
522
+ await target.bulkWrite(item.bulkWrite, item.client?.bulkWrite).then(result => {
523
+ if (result.acknowledged) {
524
+ const { insertedCount, upsertedCount, matchedCount, modifiedCount, deletedCount } = result;
525
+ this.addLog(this.statusType.INFO, `inserted: ${insertedCount}, upserted: ${upsertedCount}, matched: ${matchedCount}, modified: ${modifiedCount}, deleted: ${deletedCount}`, "mongodb", 'bulkWrite');
526
+ }
527
+ else {
528
+ this.addLog(this.statusType.WARN, 'Operation was not acknowledged', this.moduleName, 'bulkWrite');
529
+ }
530
+ });
531
+ }
532
+ return db.collection(item.table, item.client?.collection);
533
+ }
521
534
  function getFilterValue(query, coerce) {
522
535
  let value, options;
523
536
  if ((0, types_1.isObject)(query) && query.value && query.options) {
package/client/pool.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import type { DbPoolConstructor } from '@e-mc/types/lib/db';
2
-
3
- import type { DbPoolCredential, MongoDBDataSource } from '../types';
4
-
5
- import type { MongoClient } from 'mongodb';
6
-
7
- declare const MongoDBPool: DbPoolConstructor<MongoDBDataSource, MongoClient, MongoClient, DbPoolCredential>;
8
-
1
+ import type { DbPoolConstructor } from '@e-mc/types/lib/db';
2
+
3
+ import type { DbPoolCredential, MongoDBDataSource } from '../types';
4
+
5
+ import type { MongoClient } from 'mongodb';
6
+
7
+ declare const MongoDBPool: DbPoolConstructor<MongoDBDataSource, MongoClient, MongoClient, DbPoolCredential>;
8
+
9
9
  export = MongoDBPool;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/mongodb",
3
- "version": "0.8.3",
3
+ "version": "0.9.0",
4
4
  "description": "MongoDB client driver for E-mc.",
5
5
  "main": "client/index.js",
6
6
  "types": "client/index.d.ts",
@@ -14,16 +14,15 @@
14
14
  },
15
15
  "keywords": [
16
16
  "squared",
17
- "e-mc",
18
- "squared-functions"
17
+ "e-mc"
19
18
  ],
20
19
  "author": "An Pham <anpham6@gmail.com>",
21
20
  "license": "MIT",
22
21
  "homepage": "https://github.com/anpham6/pi-r#readme",
23
22
  "dependencies": {
24
- "@e-mc/db": "^0.10.3",
25
- "@e-mc/request": "^0.10.3",
26
- "@e-mc/types": "^0.10.3",
27
- "mongodb": "^6.9.0"
23
+ "@e-mc/db": "^0.11.0",
24
+ "@e-mc/request": "^0.11.0",
25
+ "@e-mc/types": "^0.11.0",
26
+ "mongodb": "^6.11.0"
28
27
  }
29
28
  }
package/types/index.d.ts CHANGED
@@ -3,7 +3,7 @@ import type { DbDataSource } from '@e-mc/types/lib/squared';
3
3
  import type { IdentifierAction } from '@e-mc/types/lib/core';
4
4
  import type { CascadeAction, ServerAuth } from '@e-mc/types/lib/db';
5
5
 
6
- import type { AggregateOptions, CollectionOptions, CommandOperationOptions, DbOptions, Document, Filter, MongoClientOptions, OptionalUnlessRequiredId, RunCommandOptions, Sort, SortDirection, UpdateFilter } from 'mongodb';
6
+ import type { AggregateOptions, ClientBulkWriteModel, ClientBulkWriteOptions, CollectionOptions, CommandOperationOptions, DbOptions, Document, Filter, MongoClientOptions, OptionalUnlessRequiredId, RunCommandOptions, Sort, SortDirection, UpdateFilter } from 'mongodb';
7
7
 
8
8
  export interface MongoDBDataSource extends DbDataSource<Filter<Document> | MongoDBFilterValue, MongoClientOptions, UpdateFilter<Document> | OptionalUnlessRequiredId<unknown>[], MongoDBCredential, string>, MongoDBClientDBOptions, CascadeAction {
9
9
  source: "mongodb";
@@ -28,12 +28,14 @@ export interface MongoDBClientDBOptions {
28
28
  db?: DbOptions;
29
29
  collection?: CollectionOptions;
30
30
  command?: RunCommandOptions;
31
+ bulkWrite?: ClientBulkWriteOptions;
31
32
  };
32
33
  execute?: {
33
34
  insert?: CommandOperationOptions;
34
35
  update?: CommandOperationOptions;
35
36
  };
36
- command?: ArrayOf<Document>;
37
+ command?: Document | Document[];
38
+ bulkWrite?: ClientBulkWriteModel[];
37
39
  aggregate?: MongoDBAggregate | Document[];
38
40
  sort?: Sort | string | MongoDBSortValue;
39
41
  }