diff --git a/package.json b/package.json index 7896eaf..d1fe195 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "@types/mysql": "^2.15.21", "amqp-connection-manager": "^4.1.12", "amqplib": "^0.10.3", - "mongodb": "^6.3.0", + "mongodb": "^6.5.0", "mysql": "^2.18.1" }, "peerDependencies": { diff --git a/src/MessageBroker.ts b/src/MessageBroker.ts index d18f1ec..75ed4e0 100644 --- a/src/MessageBroker.ts +++ b/src/MessageBroker.ts @@ -124,7 +124,6 @@ class MessageBroker async init () { - if (!this.#load) return this.#logger?.info('Not loading RabbitMQ'); diff --git a/src/MongoDB.ts b/src/MongoDB.ts index d8bcb02..188a0c8 100644 --- a/src/MongoDB.ts +++ b/src/MongoDB.ts @@ -1,5 +1,5 @@ import { inspect } from 'node:util'; -import { MongoClient, MongoClientOptions, Db, Document, WithId, ObjectId, Filter, IndexSpecification, CreateIndexesOptions, FindOptions, ModifyResult } from 'mongodb'; +import { MongoClient, MongoClientOptions, Db, Document, WithId, ObjectId, Filter, IndexSpecification, CreateIndexesOptions, FindOptions, ModifyResult, DistinctOptions } from 'mongodb'; import { IServer, ILogger } from './interfaces/index.js'; type Credentials = { @@ -61,7 +61,6 @@ class MongoDB constructor (server: IServer, config: MongoOptions) { - if (!server) throw new Error('Missing reference to server!'); if (!config) @@ -108,7 +107,6 @@ class MongoDB .on('timeout', () => this.#logger?.warn('MongoDB timed out')) .on('close', () => this.#logger?.info('MongoDB client disconnected')) .on('open', () => this.#logger?.info('MongoDB client connected')); - } get database () @@ -350,6 +348,24 @@ class MongoDB return result; } + async distinct (db: string, key: string, filter?: Filter, options: DistinctOptions = {}) + { + if (!this.#db) + throw new Error('MongoDB not connected'); + if (typeof db !== 'string') + throw new Error('Expecting collection name for the first argument'); + if (filter && typeof filter._id === 'string' && filter._id.length === 24) + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + filter._id = new ObjectId(filter._id); + + const endTimer = MongoDB.#queryHistogram?.startTimer({ type: 'distinct' }); + const result = await this.#db.collection(db).distinct(key, filter, options); + if (endTimer) + endTimer(); + return result; + } + /** * Push data to an array * diff --git a/yarn.lock b/yarn.lock index c236bfe..f027efd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -62,10 +62,10 @@ resolved "https://registry.corgi.wtf/@humanwhocodes/object-schema/-/object-schema-1.2.1.tgz#b520529ec21d8e5945a1851dfd1c32e94e39ff45" integrity sha512-ZnQMnLV4e7hDlUvw8H+U8ASL02SS2Gn6+9Ac3wGGLIe7+je2AeAOxPY+izIPJDfFDb7eDjev0Us8MO1iFRN8hA== -"@mongodb-js/saslprep@^1.1.0": - version "1.1.1" - resolved "https://registry.corgi.wtf/@mongodb-js/saslprep/-/saslprep-1.1.1.tgz#9a6c2516bc9188672c4d953ec99760ba49970da7" - integrity sha512-t7c5K033joZZMspnHg/gWPE4kandgc2OxE74aYOtGKfgB9VPuVJPix0H6fhmm2erj5PBJ21mqcx34lpIGtUCsQ== +"@mongodb-js/saslprep@^1.1.5": + version "1.1.5" + resolved "https://registry.corgi.wtf/@mongodb-js/saslprep/-/saslprep-1.1.5.tgz#0c48a96c8d799e81fae311b7251aa5c1dc7c6e95" + integrity sha512-XLNOMH66KhJzUJNwT/qlMnS4WsNDWD5ASdyaSH3EtK+F4r/CFGa3jT4GNi4mfOitGvWXtdLgQJkQjxSVrio+jA== dependencies: sparse-bitfield "^3.0.3" @@ -315,10 +315,10 @@ braces@^3.0.2: dependencies: fill-range "^7.0.1" -bson@^6.2.0: - version "6.2.0" - resolved "https://registry.corgi.wtf/bson/-/bson-6.2.0.tgz#4b6acafc266ba18eeee111373c2699304a9ba0a3" - integrity sha512-ID1cI+7bazPDyL9wYy9GaQ8gEEohWvcUl/Yf0dIdutJxnmInEEyCsb4awy/OiBfall7zBA179Pahi3vCdFze3Q== +bson@^6.4.0: + version "6.5.0" + resolved "https://registry.corgi.wtf/bson/-/bson-6.5.0.tgz#fc4828d065e64e48ea442b1a23099b2e52f7ff0b" + integrity sha512-DXf1BTAS8vKyR90BO4x5v3rKVarmkdkzwOrnYDFdjAY694ILNDkmA3uRh1xXJEl+C1DAh8XCvAQ+Gh3kzubtpg== buffer-more-ints@~1.0.0: version "1.0.0" @@ -788,13 +788,13 @@ mongodb-connection-string-url@^3.0.0: "@types/whatwg-url" "^11.0.2" whatwg-url "^13.0.0" -mongodb@^6.3.0: - version "6.3.0" - resolved "https://registry.corgi.wtf/mongodb/-/mongodb-6.3.0.tgz#ec9993b19f7ed2ea715b903fcac6171c9d1d38ca" - integrity sha512-tt0KuGjGtLUhLoU263+xvQmPHEGTw5LbcNC73EoFRYgSHwZt5tsoJC110hDyO1kjQzpgNrpdcSza9PknWN4LrA== +mongodb@^6.5.0: + version "6.5.0" + resolved "https://registry.corgi.wtf/mongodb/-/mongodb-6.5.0.tgz#3735b4fba085b26ca06f7744e9639bc538e93d87" + integrity sha512-Fozq68InT+JKABGLqctgtb8P56pRrJFkbhW0ux+x1mdHeyinor8oNzJqwLjV/t5X5nJGfTlluxfyMnOXNggIUA== dependencies: - "@mongodb-js/saslprep" "^1.1.0" - bson "^6.2.0" + "@mongodb-js/saslprep" "^1.1.5" + bson "^6.4.0" mongodb-connection-string-url "^3.0.0" ms@2.1.2: