Compare commits

..

No commits in common. "308c9cb63db1486809f3142eefe25fa20253efdd" and "84ba3423e236cc6af2411f179c3ac88069d0af38" have entirely different histories.

2 changed files with 44 additions and 52 deletions

View File

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

View File

@ -43,13 +43,13 @@ class MongoDB
constructor (server: IServer, config: MongoOptions)
{
if (!server)
if (!server)
throw new Error('Missing reference to server!');
if (!config)
if (!config)
throw new Error('No config options provided!');
const { user, password, host, port, database, URI, authDb } = config.credentials;
if ((!host?.length || !port || !database?.length) && !URI)
if ((!host?.length || !port || !database?.length) && !URI)
throw new Error('Must provide host, port, and database OR URI parameters!');
this.#config = config;
@ -60,7 +60,7 @@ class MongoDB
if (URI)
{
this.#URI = URI;
this.#URI = URI;
}
else
{
@ -73,12 +73,12 @@ class MongoDB
}
else if (!auth)
{
this.#logger.warn('No auth provided, proceeding without');
this.#logger.warn('No auth provided, proceeding without');
}
this.#URI = `mongodb://${auth}${host}:${port}/${AUTH_DB || ''}?readPreference=secondaryPreferred`;
this.#URI = `mongodb://${auth}${host}:${port}/${AUTH_DB || ''}?readPreference=secondaryPreferred`;
}
this.#_client = new MongoClient(this.#URI, this.#config.client);
// TODO figure out reconnecting to DB when connection fails
@ -107,10 +107,10 @@ class MongoDB
async init ()
{
if (!this.#config.load)
if (!this.#config.load)
return this.#logger.info('Not loading MongoDB');
if (this.#db)
if (this.#db)
throw new Error('Database already connected');
this.#logger.status(`Initializing database connection to ${this.#_client.options.hosts}`);
@ -127,9 +127,9 @@ class MongoDB
async close ()
{
if (!this.#db)
if (!this.#db)
return;
this.#logger.status('Closing database connection');
await this.#_client.close();
this.#db = null;
@ -152,13 +152,13 @@ class MongoDB
async find<T extends Document> (db: string, query: MongoQuery, options?: FindOptions<T>): Promise<WithId<T>[]>
{
if (!this.#db)
if (!this.#db)
throw new Error('MongoDB not connected');
if (typeof db !== 'string')
if (typeof db !== 'string')
throw new TypeError('Expecting collection name for the first argument');
if (typeof query._id === 'string')
if (typeof query._id === 'string')
query._id = new ObjectId(query._id);
this.#logger.debug(`Incoming find query for ${db} with parameters ${inspect(query)}`);
@ -179,11 +179,11 @@ class MongoDB
async findOne<T extends Document> (db: string, query: MongoQuery, options: FindOptions<T> = {}): Promise<WithId<T> | null>
{
if (!this.#db)
if (!this.#db)
throw new Error('MongoDB not connected');
if (typeof db !== 'string')
if (typeof db !== 'string')
throw new TypeError('Expecting collection name for the first argument');
if (typeof query._id === 'string' && query._id.length === 12)
query._id = new ObjectId(query._id);
@ -205,11 +205,11 @@ class MongoDB
async updateMany<T extends Document> (db: string, filter: MongoQuery, data: T, upsert = false)
{
if (!this.#db)
if (!this.#db)
throw new Error('MongoDB not connected');
if (typeof db !== 'string')
if (typeof db !== 'string')
throw new TypeError('Expecting collection name for the first argument');
if (!filter)
if (!filter)
throw new Error('Cannot run update many without a filter, if you mean to update every single document, pass an empty object');
if (typeof filter._id === 'string')
filter._id = new ObjectId(filter._id);
@ -232,11 +232,11 @@ class MongoDB
async updateOne (db: string, filter: MongoQuery, data: Document, upsert = false)
{
if (!this.#db)
if (!this.#db)
throw new Error('MongoDB not connected');
if (typeof db !== 'string')
if (typeof db !== 'string')
throw new TypeError('Expecting collection name for the first argument');
if (typeof filter._id === 'string')
if (typeof filter._id === 'string')
filter._id = new ObjectId(filter._id);
this.#logger.debug(`Incoming updateOne query for ${db} with parameters ${inspect(filter)}`);
@ -257,9 +257,9 @@ class MongoDB
async insertOne (db: string, data: Document)
{
if (!this.#db)
if (!this.#db)
throw new Error('MongoDB not connected');
if (typeof db !== 'string')
if (typeof db !== 'string')
throw new TypeError('Expecting collection name for the first argument');
if (typeof data._id === 'string')
data._id = new ObjectId(data._id);
@ -273,9 +273,9 @@ class MongoDB
async deleteOne (db: string, filter: Document)
{
if (!this.#db)
if (!this.#db)
throw new Error('MongoDB not connected');
if (typeof db !== 'string')
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);
@ -299,9 +299,9 @@ class MongoDB
async push (db: string, filter: Document, data: object, upsert = false)
{
if (!this.#db)
if (!this.#db)
throw new Error('MongoDB not connected');
if (typeof db !== 'string')
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);
@ -324,16 +324,16 @@ class MongoDB
random<T extends Document> (db: string, filter: Document = {}, amount = 1)
{
if (!this.#db)
if (!this.#db)
throw new Error('MongoDB not connected');
if (typeof db !== 'string')
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);
this.#logger.debug(`Incoming random query for ${db} with parameters ${inspect(filter)} and amount ${amount}`);
if (amount > 100)
if (amount > 100)
amount = 100;
const cursor = this.#db.collection(db).aggregate<T>([{ $match: filter }, { $sample: { size: amount } }]);
@ -343,7 +343,7 @@ class MongoDB
stats (options = {})
{
if (!this.#db)
if (!this.#db)
throw new Error('MongoDB not connected');
const result = this.#db.stats(options);
return result;
@ -351,33 +351,25 @@ class MongoDB
collection<T extends Document> (coll: string)
{
if (!this.#db)
if (!this.#db)
throw new Error('MongoDB not connected');
return this.#db.collection<T>(coll);
}
count (coll: string, query: Document)
{
if (!this.#db)
if (!this.#db)
throw new Error('MongoDB not connected');
return this.#db.collection(coll).countDocuments(query);
}
async ensureIndex (collection: string, index: IndexSpecification, options?: CreateIndexesOptions): Promise<void>
async ensureIndex (collection: string, indices: IndexSpecification = [], options?: CreateIndexesOptions)
{
if (!this.#db)
return Promise.reject(new Error('MongoDB not connected'));
if (!(index instanceof Array))
index = [ index ];
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);
if (!this.#db)
throw new Error('MongoDB not connected');
if (!(indices instanceof Array))
indices = [ indices ];
await this.#db.collection(collection).createIndex(indices, options);
}
}