Compare commits

..

2 Commits

Author SHA1 Message Date
308c9cb63d
v1.5.6 2023-07-19 23:24:53 +03:00
2cd8ba3ba0
method for ensuring multiple indices 2023-07-19 23:24:00 +03:00
2 changed files with 52 additions and 44 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@navy.gif/wrappers", "name": "@navy.gif/wrappers",
"version": "1.5.5", "version": "1.5.6",
"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

@ -363,13 +363,21 @@ class MongoDB
return this.#db.collection(coll).countDocuments(query); return this.#db.collection(coll).countDocuments(query);
} }
async ensureIndex (collection: string, indices: IndexSpecification = [], options?: CreateIndexesOptions) async ensureIndex (collection: string, index: IndexSpecification, options?: CreateIndexesOptions): Promise<void>
{ {
if (!this.#db) if (!this.#db)
throw new Error('MongoDB not connected'); return Promise.reject(new Error('MongoDB not connected'));
if (!(indices instanceof Array)) if (!(index instanceof Array))
indices = [ indices ]; index = [ index ];
await this.#db.collection(collection).createIndex(indices, options); await this.#db.collection(collection).createIndex(index, options);
}
async ensureIndices (collection: string, indices: IndexSpecification[], options?: CreateIndexesOptions): Promise<void>
{
if (!this.#db)
return Promise.reject(new Error('MongoDB not connected'));
for (const index of indices)
await this.ensureIndex(collection, index, options);
} }
} }