Compare commits

...

5 Commits

Author SHA1 Message Date
55c9128987
v1.6.1 2023-08-16 03:20:58 +03:00
c7e60eef48
update type overloads 2023-08-16 03:20:45 +03:00
eb08105404
v1.6.0 2023-08-16 02:56:23 +03:00
cfbd2fbcd3
new helper method for findoneanddelete 2023-08-16 02:56:04 +03:00
44be4b491c
bugfix
make sure a collection exists before trying to create an index on it
2023-08-16 02:13:38 +03:00
2 changed files with 20 additions and 3 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@navy.gif/wrappers", "name": "@navy.gif/wrappers",
"version": "1.5.12", "version": "1.6.1",
"description": "Various wrapper classes I use in my projects", "description": "Various wrapper classes I use in my projects",
"repository": "https://git.corgi.wtf/Navy.gif/wrappers.git", "repository": "https://git.corgi.wtf/Navy.gif/wrappers.git",
"author": "Navy.gif", "author": "Navy.gif",

View File

@ -1,5 +1,5 @@
import { inspect } from 'node:util'; import { inspect } from 'node:util';
import { MongoClient, MongoClientOptions, Db, Document, WithId, ObjectId, Filter, IndexSpecification, CreateIndexesOptions, FindOptions } from 'mongodb'; import { MongoClient, MongoClientOptions, Db, Document, WithId, ObjectId, Filter, IndexSpecification, CreateIndexesOptions, FindOptions, ModifyResult } from 'mongodb';
import { IServer, ILogger, LoggerClientOptions } from './interfaces/index.js'; import { IServer, ILogger, LoggerClientOptions } from './interfaces/index.js';
type Credentials = { type Credentials = {
@ -288,7 +288,6 @@ class MongoDB
async deleteOne (db: string, filter: Document) async deleteOne (db: string, filter: Document)
{ {
if (!this.#db) if (!this.#db)
throw new Error('MongoDB not connected'); throw new Error('MongoDB not connected');
if (typeof db !== 'string') if (typeof db !== 'string')
@ -299,7 +298,21 @@ class MongoDB
this.#logger.debug(`Incoming deleteOne query for ${db} with parameters ${inspect(filter)}`); this.#logger.debug(`Incoming deleteOne query for ${db} with parameters ${inspect(filter)}`);
const result = await this.#db.collection(db).deleteOne(filter); const result = await this.#db.collection(db).deleteOne(filter);
return result; return result;
}
async findOneAndDelete<T extends Document> (db: string, filter: Document, meta: false): Promise<WithId<T>>
async findOneAndDelete<T extends Document> (db: string, filter: Document, meta: true): Promise<ModifyResult<T>>
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;
} }
/** /**
@ -386,6 +399,10 @@ class MongoDB
if (!(index instanceof Array)) if (!(index instanceof Array))
index = [ index ]; index = [ index ];
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 indexes = await this.#db.collection(collection).indexes();
const existing = indexes.find(idx => idx.name.startsWith(index)); const existing = indexes.find(idx => idx.name.startsWith(index));
if (existing && this.#indexesEqual(existing, options)) if (existing && this.#indexesEqual(existing, options))