@pi-r/mongodb 0.10.1 → 0.10.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.d.ts CHANGED
@@ -7,7 +7,7 @@ import type { MongoDBDataSource, MongoDBFilterValue, MongoDBSortValue } from '..
7
7
  import type { Collection, CommandOperationOptions, Document, Filter, MongoClient, Sort, SortDirection } from 'mongodb';
8
8
 
9
9
  declare const MongoDB: IDbSourceClient<MongoDBDataSource> & {
10
- getFilterValue(query: Filter<Document> | MongoDBFilterValue, coerce?: boolean): [Filter<Document>, CommandOperationOptions?];
10
+ getFilterValue(query: Filter<Document> | MongoDBFilterValue, coerce?: boolean): [Filter<Document>, (CommandOperationOptions & { sort?: Sort })?];
11
11
  getSortValue(sort: Sort | string | MongoDBSortValue, coerce?: boolean): [Sort | string, SortDirection?];
12
12
  initCollection(this: IModule, target: MongoClient, item: Pick<MongoDBDataSource, "bulkWrite" | "client" | "command" | "name" | "table">): Promise<Collection>;
13
13
  };
package/client/index.js CHANGED
@@ -66,11 +66,9 @@ function setCredential(item) {
66
66
  }
67
67
  else if (Request.isCert(value)) {
68
68
  if (attr === 'tlsCertificateKeyFile') {
69
- const pattern = /-+([^-]+)-+\s+[^-]+-+[^-]+-+/g;
70
69
  const key = [];
71
70
  const cert = [];
72
- let match;
73
- while (match = pattern.exec(value)) {
71
+ for (const match of value.matchAll(/-+([^-]+)-+\s+[^-]+-+[^-]+-+/g)) {
74
72
  if (match[1].includes('BEGIN')) {
75
73
  if (match[1].includes('KEY')) {
76
74
  key.push(match[0]);
@@ -205,6 +203,9 @@ function setCredential(item) {
205
203
  return;
206
204
  }
207
205
  const client = new mongodb.MongoClient(item.uri, options);
206
+ if (item.driverInfo) {
207
+ client.appendMetadata(item.driverInfo);
208
+ }
208
209
  new DbPool(client, poolKey, username && password ? { username, password } : undefined)
209
210
  .add(item)
210
211
  .parent = POOL_STATE;
@@ -264,7 +265,11 @@ async function executeBatchQuery(batch, options = '', outResult) {
264
265
  credential.options.maxPoolSize = size;
265
266
  }
266
267
  }
267
- clients.push(client = await new mongodb.MongoClient(credential.uri, credential.options).connect());
268
+ client = new mongodb.MongoClient(credential.uri, credential.options);
269
+ if (item.driverInfo) {
270
+ client.appendMetadata(item.driverInfo);
271
+ }
272
+ clients.push(client = await client.connect());
268
273
  }
269
274
  if (connectOnce) {
270
275
  mongoClient = client;
@@ -278,7 +283,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
278
283
  }
279
284
  for (let i = 0; i < length; ++i) {
280
285
  const item = batch[i];
281
- const { source, name, table, id, aggregate, sort, client, cacheObjectKey, ignoreCache } = item;
286
+ const { source, name, table, id, aggregate, distinct, sort, client, cacheObjectKey, ignoreCache } = item;
282
287
  let { query, update, streamRow, limit = 0 } = item;
283
288
  if (!table) {
284
289
  const error = (0, types_1.errorMessage)(source, "Missing database table");
@@ -332,9 +337,9 @@ async function executeBatchQuery(batch, options = '', outResult) {
332
337
  }
333
338
  return false;
334
339
  };
335
- if (pipeline || query) {
336
- queryString += Db.asString(aggregate || query, true);
337
- if ((aggregate || !update) && setResult()) {
340
+ if (pipeline || query || distinct) {
341
+ queryString += Db.asString(aggregate || query || distinct, true);
342
+ if ((aggregate || distinct || !update) && setResult()) {
338
343
  continue;
339
344
  }
340
345
  }
@@ -398,13 +403,15 @@ async function executeBatchQuery(batch, options = '', outResult) {
398
403
  }
399
404
  }
400
405
  else {
401
- const [filter, operation] = getFilterValue(query, coercing);
402
- const updateType = item.updateType;
406
+ const [filter, operation = {}] = getFilterValue(query, coercing);
407
+ if (sort) {
408
+ operation.sort = getSortValue(sort, coercing)[0];
409
+ }
403
410
  if (limit === 1) {
404
411
  let document;
405
412
  if (update) {
406
413
  commandType = this.commandType.UPDATE;
407
- document = await collection[updateType === 2 ? 'findOneAndReplace' : updateType === 3 ? 'findOneAndDelete' : 'findOneAndUpdate'](filter, update, operation);
414
+ document = await collection[item.updateType === 2 ? 'findOneAndReplace' : item.updateType === 3 ? 'findOneAndDelete' : 'findOneAndUpdate'](filter, update, operation);
408
415
  }
409
416
  else {
410
417
  commandType = this.commandType.SELECT;
@@ -413,7 +420,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
413
420
  rows = document ? [document] : [];
414
421
  }
415
422
  else {
416
- if (update && updateType === 1) {
423
+ if (update && item.updateType === 1) {
417
424
  commandType = this.commandType.UPDATE;
418
425
  const updateOptions = item.execute?.update;
419
426
  if (updateOptions && coercing) {
@@ -425,6 +432,10 @@ async function executeBatchQuery(batch, options = '', outResult) {
425
432
  }
426
433
  }
427
434
  }
435
+ else if (distinct) {
436
+ const [a, b, c] = Array.isArray(distinct) ? distinct : [distinct];
437
+ rows = await (b ? c ? collection.distinct(a, b, c) : collection.distinct(a, b) : collection.distinct(a));
438
+ }
428
439
  else if (!aggregate) {
429
440
  cursor = collection.find();
430
441
  }
package/client/pool.js CHANGED
@@ -50,7 +50,7 @@ class MongoDBPool extends DbPool {
50
50
  const topology = this.client.topology;
51
51
  if (topology) {
52
52
  const name = Object.getOwnPropertySymbols(topology).find(sym => sym.toString() === 'Symbol(waitQueue)');
53
- return (0, util_1.checkEmpty)(name ? topology[name] : topology.s.servers);
53
+ return (0, util_1.checkEmpty)(name ? topology[name] : topology.waitQueue || topology.s.servers);
54
54
  }
55
55
  return false;
56
56
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/mongodb",
3
- "version": "0.10.1",
3
+ "version": "0.10.2",
4
4
  "description": "MongoDB client driver for E-mc.",
5
5
  "main": "client/index.js",
6
6
  "types": "client/index.d.ts",
@@ -20,9 +20,9 @@
20
20
  "license": "MIT",
21
21
  "homepage": "https://github.com/anpham6/pi-r#readme",
22
22
  "dependencies": {
23
- "@e-mc/db": "^0.12.3",
24
- "@e-mc/request": "^0.12.3",
25
- "@e-mc/types": "^0.12.3",
26
- "mongodb": "^6.16.0"
23
+ "@e-mc/db": "^0.12.5",
24
+ "@e-mc/request": "^0.12.5",
25
+ "@e-mc/types": "^0.12.5",
26
+ "mongodb": "^6.18.0"
27
27
  }
28
28
  }
package/types/index.d.ts CHANGED
@@ -3,12 +3,13 @@ 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, ClientBulkWriteModel, ClientBulkWriteOptions, CollectionOptions, CommandOperationOptions, DbOptions, Document, Filter, MongoClientOptions, OptionalUnlessRequiredId, RunCommandOptions, Sort, SortDirection, UpdateFilter } from 'mongodb';
6
+ import type { AggregateOptions, ClientBulkWriteModel, ClientBulkWriteOptions, CollectionOptions, CommandOperationOptions, DbOptions, DistinctOptions, Document, DriverInfo, Filter, MongoClientOptions, OptionalUnlessRequiredId, RunCommandOptions, Sort, SortDirection, UpdateFilter, WithId } 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";
10
10
  id?: string;
11
11
  updateType?: 0 | 1 | 2 | 3;
12
+ driverInfo?: DriverInfo;
12
13
  }
13
14
 
14
15
  export interface MongoDBCredential extends ServerAuth, Pick<MongoClientOptions, "auth" | "authMechanism" | "authSource" | "tlsCertificateKeyFile" | "tlsCertificateKeyFilePassword" | "tlsCAFile" | "tlsCRLFile" | "tlsAllowInvalidHostnames" | "tlsAllowInvalidCertificates" | "tlsInsecure"> {}
@@ -35,6 +36,7 @@ export interface MongoDBClientDBOptions {
35
36
  update?: CommandOperationOptions;
36
37
  };
37
38
  command?: Document | Document[];
39
+ distinct?: keyof WithId<Document> | [keyof WithId<Document>, Filter<Document>?, DistinctOptions?];
38
40
  bulkWrite?: ClientBulkWriteModel[];
39
41
  aggregate?: MongoDBAggregate | Document[];
40
42
  sort?: Sort | string | MongoDBSortValue;