updated typings

This commit is contained in:
Erik 2023-04-29 22:59:59 +03:00
parent 15dc7362af
commit 1b832b9c35
Signed by: Navy.gif
GPG Key ID: 2532FBBB61C65A68

View File

@ -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<T extends Document> (db: string, query: object & {_id: unknown}, options?: object): Promise<WithId<T>[]> {
async find<T extends Document> (db: string, query: MongoQuery, options?: object): Promise<WithId<T>[]> {
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<T>(db).find(query, options);
const cursor = this.#db.collection<T>(db).find(query as Filter<T>, options);
return cursor.toArray();
}
@ -150,7 +155,7 @@ class MongoDB {
* @returns {Object} An object containing the queried data
* @memberof Database
*/
async findOne<T extends Document> (db: string, query: object & {_id: unknown}, options = {}): Promise<WithId<T> | null> {
async findOne<T extends Document> (db: string, query: MongoQuery, options = {}): Promise<WithId<T> | 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<T>(db).findOne(query, options);
const result = await this.#db.collection<T>(db).findOne(query as Filter<T>, 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<Document>, { $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<Document>, { $set: data }, { upsert });
return result;
}