new helper method for findoneanddelete

This commit is contained in:
Erik 2023-08-16 02:13:53 +03:00
parent 44be4b491c
commit cfbd2fbcd3
Signed by: Navy.gif
GPG Key ID: 2532FBBB61C65A68
2 changed files with 14 additions and 3 deletions

View File

@ -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",

View File

@ -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<T extends Document> (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<T>(db).findOneAndDelete(filter as Filter<T>, { 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))