Compare commits
4 Commits
15dc7362af
...
f70d15583c
Author | SHA1 | Date | |
---|---|---|---|
f70d15583c | |||
2952660737 | |||
a97eb9adcd | |||
1b832b9c35 |
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@navy.gif/wrappers",
|
"name": "@navy.gif/wrappers",
|
||||||
"version": "1.3.3",
|
"version": "1.3.5",
|
||||||
"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",
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { inspect } from "node:util";
|
import { inspect } from "node:util";
|
||||||
import { MongoClient, MongoClientOptions, Db, Document, WithId, ObjectId } from "mongodb";
|
import { MongoClient, MongoClientOptions, Db, Document, WithId, ObjectId, Filter } from "mongodb";
|
||||||
import { IServer, ILogger, LoggerClientOptions } from "./interfaces/index.js";
|
import { IServer, ILogger, LoggerClientOptions } from "./interfaces/index.js";
|
||||||
|
|
||||||
type Credentials = {
|
type Credentials = {
|
||||||
@ -12,6 +12,11 @@ type Credentials = {
|
|||||||
authDb?: string
|
authDb?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MongoQuery = {
|
||||||
|
_id?: unknown,
|
||||||
|
[key: string]: unknown
|
||||||
|
}
|
||||||
|
|
||||||
export type MongoOptions = {
|
export type MongoOptions = {
|
||||||
credentials: Credentials,
|
credentials: Credentials,
|
||||||
loggerOptions?: LoggerClientOptions,
|
loggerOptions?: LoggerClientOptions,
|
||||||
@ -93,6 +98,9 @@ class MongoDB {
|
|||||||
|
|
||||||
if (!this.#config.load)
|
if (!this.#config.load)
|
||||||
return this.#logger.info('Not loading MongoDB');
|
return this.#logger.info('Not loading MongoDB');
|
||||||
|
|
||||||
|
if (this.#db)
|
||||||
|
throw new Error('Database already connected');
|
||||||
|
|
||||||
this.#logger.status(`Initializing database connection to ${this.#_client.options.hosts}`);
|
this.#logger.status(`Initializing database connection to ${this.#_client.options.hosts}`);
|
||||||
|
|
||||||
@ -107,9 +115,13 @@ class MongoDB {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async close () {
|
async close () {
|
||||||
|
if (!this.#db)
|
||||||
|
return;
|
||||||
|
|
||||||
this.#logger.status('Closing database connection');
|
this.#logger.status('Closing database connection');
|
||||||
await this.#_client.close();
|
await this.#_client.close();
|
||||||
this.#db = null;
|
this.#db = null;
|
||||||
|
this.#logger.status('Database closed');
|
||||||
}
|
}
|
||||||
|
|
||||||
get mongoClient () {
|
get mongoClient () {
|
||||||
@ -124,7 +136,7 @@ class MongoDB {
|
|||||||
* @returns {Array} An array containing the corresponding objects for the query
|
* @returns {Array} An array containing the corresponding objects for the query
|
||||||
* @memberof Database
|
* @memberof Database
|
||||||
*/
|
*/
|
||||||
async find<T extends Document> (db: string, query: object & {_id: unknown}, options?: object): Promise<WithId<T>[]> {
|
async find<T extends Document> (db: string, query: MongoQuery, options?: object): Promise<WithId<T>[]> {
|
||||||
|
|
||||||
if (!this.#db)
|
if (!this.#db)
|
||||||
throw new Error(`MongoDB not connected`);
|
throw new Error(`MongoDB not connected`);
|
||||||
@ -137,7 +149,7 @@ class MongoDB {
|
|||||||
|
|
||||||
this.#logger.debug(`Incoming find query for ${db} with parameters ${inspect(query)}`);
|
this.#logger.debug(`Incoming find query for ${db} with parameters ${inspect(query)}`);
|
||||||
|
|
||||||
const cursor = this.#db.collection<T>(db).find(query, options);
|
const cursor = this.#db.collection<T>(db).find(query as Filter<T>, options);
|
||||||
return cursor.toArray();
|
return cursor.toArray();
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -150,7 +162,7 @@ class MongoDB {
|
|||||||
* @returns {Object} An object containing the queried data
|
* @returns {Object} An object containing the queried data
|
||||||
* @memberof Database
|
* @memberof Database
|
||||||
*/
|
*/
|
||||||
async findOne<T extends Document> (db: string, query: object & {_id: unknown}, options = {}): Promise<WithId<T> | null> {
|
async findOne<T extends Document> (db: string, query: MongoQuery, options = {}): Promise<WithId<T> | null> {
|
||||||
|
|
||||||
if (!this.#db)
|
if (!this.#db)
|
||||||
throw new Error(`MongoDB not connected`);
|
throw new Error(`MongoDB not connected`);
|
||||||
@ -161,7 +173,7 @@ class MongoDB {
|
|||||||
query._id = new ObjectId(query._id);
|
query._id = new ObjectId(query._id);
|
||||||
|
|
||||||
this.#logger.debug(`Incoming findOne query for ${db} with parameters ${inspect(query)}`);
|
this.#logger.debug(`Incoming findOne query for ${db} with parameters ${inspect(query)}`);
|
||||||
const result = await this.#db.collection<T>(db).findOne(query, options);
|
const result = await this.#db.collection<T>(db).findOne(query as Filter<T>, options);
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -175,8 +187,7 @@ class MongoDB {
|
|||||||
* @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified
|
* @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified
|
||||||
* @memberof Database
|
* @memberof Database
|
||||||
*/
|
*/
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
async updateMany (db: string, filter: MongoQuery, data: object, upsert = false) {
|
||||||
async updateMany (db: string, filter: object & {_id?: any}, data: object, upsert = false) {
|
|
||||||
|
|
||||||
if (!this.#db)
|
if (!this.#db)
|
||||||
throw new Error(`MongoDB not connected`);
|
throw new Error(`MongoDB not connected`);
|
||||||
@ -189,7 +200,7 @@ class MongoDB {
|
|||||||
filter._id = new ObjectId(filter._id);
|
filter._id = new ObjectId(filter._id);
|
||||||
|
|
||||||
this.#logger.debug(`Incoming update query for '${db}' with parameters\n${inspect(filter)}\nand data\n${inspect(data)}`);
|
this.#logger.debug(`Incoming update query for '${db}' with parameters\n${inspect(filter)}\nand data\n${inspect(data)}`);
|
||||||
const result = await this.#db.collection(db).updateMany(filter, { $set: data }, { upsert });
|
const result = await this.#db.collection(db).updateMany(filter as Filter<Document>, { $set: data }, { upsert });
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -203,8 +214,7 @@ class MongoDB {
|
|||||||
* @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified
|
* @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified
|
||||||
* @memberof Database
|
* @memberof Database
|
||||||
*/
|
*/
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
async updateOne (db: string, filter: MongoQuery, data: object, upsert = false) {
|
||||||
async updateOne (db: string, filter: object & {_id?: any}, data: object, upsert = false) {
|
|
||||||
|
|
||||||
if (!this.#db)
|
if (!this.#db)
|
||||||
throw new Error(`MongoDB not connected`);
|
throw new Error(`MongoDB not connected`);
|
||||||
@ -215,7 +225,7 @@ class MongoDB {
|
|||||||
filter._id = new ObjectId(filter._id);
|
filter._id = new ObjectId(filter._id);
|
||||||
|
|
||||||
this.#logger.debug(`Incoming updateOne query for ${db} with parameters ${inspect(filter)}`);
|
this.#logger.debug(`Incoming updateOne query for ${db} with parameters ${inspect(filter)}`);
|
||||||
const result = await this.#db.collection(db).updateOne(filter, { $set: data }, { upsert });
|
const result = await this.#db.collection(db).updateOne(filter as Filter<Document>, { $set: data }, { upsert });
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user