From cfbd2fbcd3ad554c71077fea9682f7e351764756 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Wed, 16 Aug 2023 02:13:53 +0300 Subject: [PATCH] new helper method for findoneanddelete --- package.json | 2 +- src/MongoDB.ts | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index bd5e6c1..935fbd6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@navy.gif/wrappers", - "version": "1.5.12", + "version": "1.5.13", "description": "Various wrapper classes I use in my projects", "repository": "https://git.corgi.wtf/Navy.gif/wrappers.git", "author": "Navy.gif", diff --git a/src/MongoDB.ts b/src/MongoDB.ts index 4ab8afa..3c4f8dd 100644 --- a/src/MongoDB.ts +++ b/src/MongoDB.ts @@ -288,7 +288,6 @@ class MongoDB async deleteOne (db: string, filter: Document) { - if (!this.#db) throw new Error('MongoDB not connected'); if (typeof db !== 'string') @@ -299,7 +298,19 @@ class MongoDB this.#logger.debug(`Incoming deleteOne query for ${db} with parameters ${inspect(filter)}`); const result = await this.#db.collection(db).deleteOne(filter); return result; + } + async findOneAndDelete (db: string, filter: Document, meta = 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); + + const result = await this.#db.collection(db).findOneAndDelete(filter as Filter, { includeResultMetadata: meta }); + return result; } /** @@ -389,7 +400,7 @@ class MongoDB const collections = await this.#db.collections(); if (!collections.some((coll) => coll.namespace.split('.')[1] === collection)) await this.#db.createCollection(collection); - + const indexes = await this.#db.collection(collection).indexes(); const existing = indexes.find(idx => idx.name.startsWith(index)); if (existing && this.#indexesEqual(existing, options))