From da1a45e9f0fa805cc6a8a18fd279dcb5f0f62ace Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Sun, 7 May 2023 03:37:14 +0300 Subject: [PATCH] update typings --- src/MongoDB.ts | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/src/MongoDB.ts b/src/MongoDB.ts index b96e197..996b05f 100644 --- a/src/MongoDB.ts +++ b/src/MongoDB.ts @@ -187,7 +187,7 @@ class MongoDB { * @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified * @memberof Database */ - async updateMany (db: string, filter: MongoQuery, data: object, upsert = false) { + async updateMany (db: string, filter: MongoQuery, data: Document, upsert = false) { if (!this.#db) throw new Error(`MongoDB not connected`); @@ -195,8 +195,7 @@ class MongoDB { throw new TypeError('Expecting collection name for the first argument'); if (!filter) throw new Error(`Cannot run update many without a filter, if you mean to update every single document, pass an empty object`); - - if (typeof filter._id === 'string') + if (typeof filter._id === 'string') filter._id = new ObjectId(filter._id); this.#logger.debug(`Incoming update query for '${db}' with parameters\n${inspect(filter)}\nand data\n${inspect(data)}`); @@ -214,13 +213,12 @@ class MongoDB { * @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified * @memberof Database */ - async updateOne (db: string, filter: MongoQuery, data: object, upsert = false) { + async updateOne (db: string, filter: MongoQuery, data: Document, upsert = false) { if (!this.#db) throw new Error(`MongoDB not connected`); if (typeof db !== 'string') throw new TypeError('Expecting collection name for the first argument'); - if (typeof filter._id === 'string') filter._id = new ObjectId(filter._id); @@ -239,12 +237,14 @@ class MongoDB { * @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified * @memberof Database */ - async insertOne (db: string, data: object) { + async insertOne (db: string, data: Document) { if (!this.#db) throw new Error(`MongoDB not connected`); if (typeof db !== 'string') throw new TypeError('Expecting collection name for the first argument'); + if (typeof data._id === 'string') + data._id = new ObjectId(data._id); this.#logger.debug(`Incoming insertOne query for ${db} with parameters ${inspect(data)}`); const result = await this.#db.collection(db).insertOne(data); @@ -252,12 +252,14 @@ class MongoDB { } - async deleteOne (db: string, filter: object) { + async deleteOne (db: string, filter: Document) { if (!this.#db) throw new Error(`MongoDB not connected`); if (typeof db !== 'string') throw new TypeError('Expecting collection name for the first argument'); + if (typeof filter._id === 'string') + filter._id = new ObjectId(filter._id); this.#logger.debug(`Incoming deleteOne query for ${db} with parameters ${inspect(filter)}`); const result = await this.#db.collection(db).deleteOne(filter); @@ -275,12 +277,14 @@ class MongoDB { * @returns * @memberof Database */ - async push (db: string, filter: object, data: object, upsert = false) { + async push (db: string, filter: Document, data: object, upsert = false) { if (!this.#db) throw new Error(`MongoDB not connected`); if (typeof db !== 'string') throw new TypeError('Expecting collection name for the first argument'); + if (typeof filter._id === 'string') + filter._id = new ObjectId(filter._id); this.#logger.debug(`Incoming push query for ${db}, with upsert ${upsert} and with parameters ${inspect(filter)} and data ${inspect(data)}`); const result = await this.#db.collection(db).updateOne(filter, { $push: data }, { upsert }); @@ -297,12 +301,14 @@ class MongoDB { * @returns {object} * @memberof Database */ - random (db: string, filter = {}, amount = 1) { + random (db: string, filter: Document = {}, amount = 1) { if (!this.#db) throw new Error(`MongoDB not connected`); if (typeof db !== 'string') throw new TypeError('Expecting collection name for the first argument'); + if (typeof filter._id === 'string') + filter._id = new ObjectId(filter._id); this.#logger.debug(`Incoming random query for ${db} with parameters ${inspect(filter)} and amount ${amount}`); @@ -335,18 +341,6 @@ class MongoDB { await this.#db.collection(collection).createIndex(indices); } - // async getKey (key: string, collection = 'memoryStore') { - // const response = await this.findOne(collection, { key }); - // if (response) - // return response.value; - // return null; - // } - - // async setKey (key: string, value: object, collection = 'memoryStore') { - // await this.updateOne(collection, { key }, { value }, true); - // return value; - // } - } export { MongoDB }; \ No newline at end of file