Compare commits
No commits in common. "f70d15583c8bf122df5643f34ae2fad786822d80" and "15dc7362af9e35f13d3fd4dc3c175cb2554501da" have entirely different histories.
f70d15583c
...
15dc7362af
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@navy.gif/wrappers",
|
"name": "@navy.gif/wrappers",
|
||||||
"version": "1.3.5",
|
"version": "1.3.3",
|
||||||
"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, Filter } from "mongodb";
|
import { MongoClient, MongoClientOptions, Db, Document, WithId, ObjectId } from "mongodb";
|
||||||
import { IServer, ILogger, LoggerClientOptions } from "./interfaces/index.js";
|
import { IServer, ILogger, LoggerClientOptions } from "./interfaces/index.js";
|
||||||
|
|
||||||
type Credentials = {
|
type Credentials = {
|
||||||
@ -12,11 +12,6 @@ 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,
|
||||||
@ -98,9 +93,6 @@ 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}`);
|
||||||
|
|
||||||
@ -115,13 +107,9 @@ 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 () {
|
||||||
@ -136,7 +124,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: MongoQuery, options?: object): Promise<WithId<T>[]> {
|
async find<T extends Document> (db: string, query: object & {_id: unknown}, options?: object): Promise<WithId<T>[]> {
|
||||||
|
|
||||||
if (!this.#db)
|
if (!this.#db)
|
||||||
throw new Error(`MongoDB not connected`);
|
throw new Error(`MongoDB not connected`);
|
||||||
@ -149,7 +137,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 as Filter<T>, options);
|
const cursor = this.#db.collection<T>(db).find(query, options);
|
||||||
return cursor.toArray();
|
return cursor.toArray();
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -162,7 +150,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: MongoQuery, options = {}): Promise<WithId<T> | null> {
|
async findOne<T extends Document> (db: string, query: object & {_id: unknown}, options = {}): Promise<WithId<T> | null> {
|
||||||
|
|
||||||
if (!this.#db)
|
if (!this.#db)
|
||||||
throw new Error(`MongoDB not connected`);
|
throw new Error(`MongoDB not connected`);
|
||||||
@ -173,7 +161,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 as Filter<T>, options);
|
const result = await this.#db.collection<T>(db).findOne(query, options);
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -187,7 +175,8 @@ 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
|
||||||
*/
|
*/
|
||||||
async updateMany (db: string, filter: MongoQuery, data: object, upsert = false) {
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
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`);
|
||||||
@ -200,7 +189,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 as Filter<Document>, { $set: data }, { upsert });
|
const result = await this.#db.collection(db).updateMany(filter, { $set: data }, { upsert });
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -214,7 +203,8 @@ 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
|
||||||
*/
|
*/
|
||||||
async updateOne (db: string, filter: MongoQuery, data: object, upsert = false) {
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
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`);
|
||||||
@ -225,7 +215,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 as Filter<Document>, { $set: data }, { upsert });
|
const result = await this.#db.collection(db).updateOne(filter, { $set: data }, { upsert });
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user