diff --git a/src/MongoDB.ts b/src/MongoDB.ts index 3bf9f21..566713d 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 } from "mongodb"; +import { MongoClient, MongoClientOptions, Db, Document, WithId, ObjectId, Filter } from "mongodb"; import { IServer, ILogger, LoggerClientOptions } from "./interfaces/index.js"; type Credentials = { @@ -12,6 +12,11 @@ type Credentials = { authDb?: string } +type MongoQuery = { + _id: unknown, + [key: string]: unknown +} + export type MongoOptions = { credentials: Credentials, loggerOptions?: LoggerClientOptions, @@ -124,7 +129,7 @@ class MongoDB { * @returns {Array} An array containing the corresponding objects for the query * @memberof Database */ - async find (db: string, query: object & {_id: unknown}, options?: object): Promise[]> { + async find (db: string, query: MongoQuery, options?: object): Promise[]> { if (!this.#db) throw new Error(`MongoDB not connected`); @@ -137,7 +142,7 @@ class MongoDB { this.#logger.debug(`Incoming find query for ${db} with parameters ${inspect(query)}`); - const cursor = this.#db.collection(db).find(query, options); + const cursor = this.#db.collection(db).find(query as Filter, options); return cursor.toArray(); } @@ -150,7 +155,7 @@ class MongoDB { * @returns {Object} An object containing the queried data * @memberof Database */ - async findOne (db: string, query: object & {_id: unknown}, options = {}): Promise | null> { + async findOne (db: string, query: MongoQuery, options = {}): Promise | null> { if (!this.#db) throw new Error(`MongoDB not connected`); @@ -161,7 +166,7 @@ class MongoDB { query._id = new ObjectId(query._id); this.#logger.debug(`Incoming findOne query for ${db} with parameters ${inspect(query)}`); - const result = await this.#db.collection(db).findOne(query, options); + const result = await this.#db.collection(db).findOne(query as Filter, options); return result; } @@ -175,8 +180,7 @@ class MongoDB { * @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified * @memberof Database */ - // eslint-disable-next-line @typescript-eslint/no-explicit-any - async updateMany (db: string, filter: object & {_id?: any}, data: object, upsert = false) { + async updateMany (db: string, filter: MongoQuery, data: object, upsert = false) { if (!this.#db) throw new Error(`MongoDB not connected`); @@ -189,7 +193,7 @@ class MongoDB { filter._id = new ObjectId(filter._id); this.#logger.debug(`Incoming update query for '${db}' with parameters\n${inspect(filter)}\nand data\n${inspect(data)}`); - const result = await this.#db.collection(db).updateMany(filter, { $set: data }, { upsert }); + const result = await this.#db.collection(db).updateMany(filter as Filter, { $set: data }, { upsert }); return result; } @@ -203,8 +207,7 @@ class MongoDB { * @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified * @memberof Database */ - // eslint-disable-next-line @typescript-eslint/no-explicit-any - async updateOne (db: string, filter: object & {_id?: any}, data: object, upsert = false) { + async updateOne (db: string, filter: MongoQuery, data: object, upsert = false) { if (!this.#db) throw new Error(`MongoDB not connected`); @@ -215,7 +218,7 @@ class MongoDB { filter._id = new ObjectId(filter._id); this.#logger.debug(`Incoming updateOne query for ${db} with parameters ${inspect(filter)}`); - const result = await this.#db.collection(db).updateOne(filter, { $set: data }, { upsert }); + const result = await this.#db.collection(db).updateOne(filter as Filter, { $set: data }, { upsert }); return result; }