2023-04-12 13:23:41 +02:00
|
|
|
import { inspect } from "node:util";
|
2023-04-29 21:59:59 +02:00
|
|
|
import { MongoClient, MongoClientOptions, Db, Document, WithId, ObjectId, Filter } from "mongodb";
|
2023-04-12 13:23:41 +02:00
|
|
|
import { IServer, ILogger, LoggerClientOptions } from "./interfaces/index.js";
|
|
|
|
|
|
|
|
type Credentials = {
|
2023-04-29 21:27:28 +02:00
|
|
|
URI?: string,
|
|
|
|
user?: string,
|
|
|
|
password?: string,
|
|
|
|
host?: string,
|
|
|
|
port?: number,
|
2023-04-12 13:23:41 +02:00
|
|
|
database: string,
|
2023-04-29 21:27:28 +02:00
|
|
|
authDb?: string
|
2023-04-12 13:23:41 +02:00
|
|
|
}
|
|
|
|
|
2023-04-29 21:59:59 +02:00
|
|
|
type MongoQuery = {
|
|
|
|
_id: unknown,
|
|
|
|
[key: string]: unknown
|
|
|
|
}
|
|
|
|
|
2023-04-14 19:39:30 +02:00
|
|
|
export type MongoOptions = {
|
2023-04-12 13:23:41 +02:00
|
|
|
credentials: Credentials,
|
2023-04-16 15:50:01 +02:00
|
|
|
loggerOptions?: LoggerClientOptions,
|
2023-04-16 15:51:19 +02:00
|
|
|
client?: MongoClientOptions,
|
2023-04-16 15:50:01 +02:00
|
|
|
load?: boolean
|
2023-04-12 13:23:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A dedicated class to locally wrap the mongodb API wrapper
|
|
|
|
*
|
|
|
|
* @class MongoDB
|
|
|
|
*/
|
|
|
|
class MongoDB {
|
|
|
|
|
2023-04-13 10:11:39 +02:00
|
|
|
#_database: string;
|
2023-04-12 13:23:41 +02:00
|
|
|
#config: MongoOptions;
|
|
|
|
#logger: ILogger;
|
|
|
|
#URI: string;
|
|
|
|
|
|
|
|
#db: Db | null;
|
2023-04-13 10:11:39 +02:00
|
|
|
#_client: MongoClient;
|
2023-04-12 13:23:41 +02:00
|
|
|
|
|
|
|
constructor (server: IServer, config: MongoOptions) {
|
|
|
|
|
|
|
|
if (!server)
|
|
|
|
throw new Error('Missing reference to server!');
|
|
|
|
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)
|
|
|
|
throw new Error(`Must provide host, port, and database OR URI parameters!`);
|
|
|
|
|
|
|
|
this.#config = config;
|
|
|
|
this.#db = null; // DB connection
|
2023-04-13 10:11:39 +02:00
|
|
|
this.#_database = database; // Which database to connect to
|
2023-04-12 13:23:41 +02:00
|
|
|
|
|
|
|
this.#logger = server.createLogger(this, config.loggerOptions);
|
|
|
|
|
|
|
|
if (URI) {
|
|
|
|
this.#URI = URI;
|
|
|
|
} else {
|
|
|
|
let AUTH_DB = authDb;
|
|
|
|
const auth = user ? `${user}:${password}@` : '';
|
|
|
|
if (!AUTH_DB && auth) {
|
|
|
|
this.#logger.warn(`No explicit auth db provided with MONGO_AUTH_DB, will attempt to use MONGO_DB for auth source`);
|
|
|
|
AUTH_DB = authDb;
|
|
|
|
} else if (!auth) {
|
|
|
|
this.#logger.warn(`No auth provided, proceeding without`);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.#URI = `mongodb://${auth}${host}:${port}/${AUTH_DB || ''}?readPreference=secondaryPreferred`;
|
|
|
|
}
|
|
|
|
|
2023-04-13 10:11:39 +02:00
|
|
|
this.#_client = new MongoClient(this.#URI, this.#config.client);
|
2023-04-12 13:23:41 +02:00
|
|
|
|
|
|
|
// TODO figure out reconnecting to DB when connection fails
|
2023-04-13 10:11:39 +02:00
|
|
|
this.#_client.on('error', (error) => this.#logger.error(`MongoDB error:\n${error.stack}`))
|
2023-04-12 13:23:41 +02:00
|
|
|
.on('timeout', () => this.#logger.warn(`MongoDB timed out`))
|
|
|
|
.on('close', () => this.#logger.info(`MongoDB client disconnected`))
|
|
|
|
.on('open', () => this.#logger.info(`MongoDB client connected`));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-04-13 10:11:39 +02:00
|
|
|
get database () {
|
|
|
|
return this.#_database;
|
|
|
|
}
|
|
|
|
|
|
|
|
get client () {
|
|
|
|
return this.#_client;
|
|
|
|
}
|
|
|
|
|
2023-04-12 13:23:41 +02:00
|
|
|
/**
|
|
|
|
* Initialises the connection to Mongo
|
|
|
|
*
|
|
|
|
* @memberof MongoDB
|
|
|
|
*/
|
|
|
|
async init () {
|
|
|
|
|
2023-04-16 17:17:34 +02:00
|
|
|
if (!this.#config.load)
|
2023-04-16 15:50:01 +02:00
|
|
|
return this.#logger.info('Not loading MongoDB');
|
|
|
|
|
2023-04-13 10:11:39 +02:00
|
|
|
this.#logger.status(`Initializing database connection to ${this.#_client.options.hosts}`);
|
2023-04-12 13:23:41 +02:00
|
|
|
|
2023-04-13 10:11:39 +02:00
|
|
|
await this.#_client.connect();
|
2023-04-12 13:23:41 +02:00
|
|
|
this.#logger.debug(`Connected, selecting DB`);
|
2023-04-13 10:11:39 +02:00
|
|
|
this.#db = await this.#_client.db(this.#_database);
|
2023-04-12 13:23:41 +02:00
|
|
|
|
|
|
|
this.#logger.status('MongoDB ready');
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async close () {
|
|
|
|
this.#logger.status('Closing database connection');
|
2023-04-13 10:11:39 +02:00
|
|
|
await this.#_client.close();
|
2023-04-12 13:23:41 +02:00
|
|
|
this.#db = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
get mongoClient () {
|
2023-04-13 10:11:39 +02:00
|
|
|
return this.#_client;
|
2023-04-12 13:23:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find and return the first match
|
|
|
|
*
|
|
|
|
* @param {String} db The collection in which the data is to be updated
|
|
|
|
* @param {Object} query The filter that is used to find the data
|
|
|
|
* @returns {Array} An array containing the corresponding objects for the query
|
|
|
|
* @memberof Database
|
|
|
|
*/
|
2023-04-29 21:59:59 +02:00
|
|
|
async find<T extends Document> (db: string, query: MongoQuery, options?: object): Promise<WithId<T>[]> {
|
2023-04-12 13:23:41 +02:00
|
|
|
|
|
|
|
if (!this.#db)
|
|
|
|
throw new Error(`MongoDB not connected`);
|
2023-04-13 10:11:39 +02:00
|
|
|
|
2023-04-12 13:23:41 +02:00
|
|
|
if (typeof db !== 'string')
|
|
|
|
throw new TypeError('Expecting collection name for the first argument');
|
2023-04-22 12:29:35 +02:00
|
|
|
|
|
|
|
if (typeof query._id === 'string')
|
|
|
|
query._id = new ObjectId(query._id);
|
2023-04-12 13:23:41 +02:00
|
|
|
|
|
|
|
this.#logger.debug(`Incoming find query for ${db} with parameters ${inspect(query)}`);
|
|
|
|
|
2023-04-29 21:59:59 +02:00
|
|
|
const cursor = this.#db.collection<T>(db).find(query as Filter<T>, options);
|
2023-04-12 13:23:41 +02:00
|
|
|
return cursor.toArray();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find and return the first match
|
|
|
|
*
|
|
|
|
* @param {String} db The collection in which the data is to be updated
|
|
|
|
* @param {Object} query The filter that is used to find the data
|
|
|
|
* @returns {Object} An object containing the queried data
|
|
|
|
* @memberof Database
|
|
|
|
*/
|
2023-04-29 21:59:59 +02:00
|
|
|
async findOne<T extends Document> (db: string, query: MongoQuery, options = {}): Promise<WithId<T> | null> {
|
2023-04-12 13:23:41 +02:00
|
|
|
|
|
|
|
if (!this.#db)
|
|
|
|
throw new Error(`MongoDB not connected`);
|
|
|
|
if (typeof db !== 'string')
|
|
|
|
throw new TypeError('Expecting collection name for the first argument');
|
2023-04-22 12:29:35 +02:00
|
|
|
|
|
|
|
if (typeof query._id === 'string' && query._id.length === 12)
|
|
|
|
query._id = new ObjectId(query._id);
|
2023-04-12 13:23:41 +02:00
|
|
|
|
|
|
|
this.#logger.debug(`Incoming findOne query for ${db} with parameters ${inspect(query)}`);
|
2023-04-29 21:59:59 +02:00
|
|
|
const result = await this.#db.collection<T>(db).findOne(query as Filter<T>, options);
|
2023-04-12 13:23:41 +02:00
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update any and all filter matches.
|
|
|
|
*
|
|
|
|
* @param {String} db The collection in which the data is to be updated
|
|
|
|
* @param {Object} filter The filter that is used to find the data
|
|
|
|
* @param {Object} data The updated data
|
|
|
|
* @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified
|
|
|
|
* @memberof Database
|
|
|
|
*/
|
2023-04-29 21:59:59 +02:00
|
|
|
async updateMany (db: string, filter: MongoQuery, data: object, upsert = false) {
|
2023-04-12 13:23:41 +02:00
|
|
|
|
|
|
|
if (!this.#db)
|
|
|
|
throw new Error(`MongoDB not connected`);
|
|
|
|
if (typeof db !== 'string')
|
|
|
|
throw new TypeError('Expecting collection name for the first argument');
|
|
|
|
if (!filter)
|
|
|
|
throw new Error(`Cannot run update many without a filter, if you mean to update every single document, pass an empty object`);
|
2023-04-22 12:29:35 +02:00
|
|
|
|
|
|
|
if (typeof filter._id === 'string')
|
|
|
|
filter._id = new ObjectId(filter._id);
|
2023-04-12 13:23:41 +02:00
|
|
|
|
|
|
|
this.#logger.debug(`Incoming update query for '${db}' with parameters\n${inspect(filter)}\nand data\n${inspect(data)}`);
|
2023-04-29 21:59:59 +02:00
|
|
|
const result = await this.#db.collection(db).updateMany(filter as Filter<Document>, { $set: data }, { upsert });
|
2023-04-12 13:23:41 +02:00
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the first filter match.
|
|
|
|
*
|
|
|
|
* @param {String} db The collection in which the data is to be updated
|
|
|
|
* @param {Object} filter The filter that is used to find the data
|
|
|
|
* @param {Object} data The updated data
|
|
|
|
* @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified
|
|
|
|
* @memberof Database
|
|
|
|
*/
|
2023-04-29 21:59:59 +02:00
|
|
|
async updateOne (db: string, filter: MongoQuery, data: object, upsert = false) {
|
2023-04-12 13:23:41 +02:00
|
|
|
|
|
|
|
if (!this.#db)
|
|
|
|
throw new Error(`MongoDB not connected`);
|
|
|
|
if (typeof db !== 'string')
|
|
|
|
throw new TypeError('Expecting collection name for the first argument');
|
2023-04-22 12:29:35 +02:00
|
|
|
|
|
|
|
if (typeof filter._id === 'string')
|
|
|
|
filter._id = new ObjectId(filter._id);
|
2023-04-12 13:23:41 +02:00
|
|
|
|
|
|
|
this.#logger.debug(`Incoming updateOne query for ${db} with parameters ${inspect(filter)}`);
|
2023-04-29 21:59:59 +02:00
|
|
|
const result = await this.#db.collection(db).updateOne(filter as Filter<Document>, { $set: data }, { upsert });
|
2023-04-12 13:23:41 +02:00
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert document.
|
|
|
|
*
|
|
|
|
* @param {String} db The collection in which the data is to be updated
|
|
|
|
* @param {Object} filter The filter that is used to find the data
|
|
|
|
* @param {Object} data The updated data
|
|
|
|
* @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified
|
|
|
|
* @memberof Database
|
|
|
|
*/
|
|
|
|
async insertOne (db: string, data: object) {
|
|
|
|
|
|
|
|
if (!this.#db)
|
|
|
|
throw new Error(`MongoDB not connected`);
|
|
|
|
if (typeof db !== 'string')
|
|
|
|
throw new TypeError('Expecting collection name for the first argument');
|
|
|
|
|
|
|
|
this.#logger.debug(`Incoming insertOne query for ${db} with parameters ${inspect(data)}`);
|
|
|
|
const result = await this.#db.collection(db).insertOne(data);
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteOne (db: string, filter: object) {
|
|
|
|
|
|
|
|
if (!this.#db)
|
|
|
|
throw new Error(`MongoDB not connected`);
|
|
|
|
if (typeof db !== 'string')
|
|
|
|
throw new TypeError('Expecting collection name for the first argument');
|
|
|
|
|
|
|
|
this.#logger.debug(`Incoming deleteOne query for ${db} with parameters ${inspect(filter)}`);
|
|
|
|
const result = await this.#db.collection(db).deleteOne(filter);
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Push data to an array
|
|
|
|
*
|
|
|
|
* @param {string} db The collection to query
|
|
|
|
* @param {object} filter The filter to find the document to update
|
|
|
|
* @param {object} data The data to be pushed
|
|
|
|
* @param {boolean} [upsert=false]
|
|
|
|
* @returns
|
|
|
|
* @memberof Database
|
|
|
|
*/
|
|
|
|
async push (db: string, filter: object, data: object, upsert = false) {
|
|
|
|
|
|
|
|
if (!this.#db)
|
|
|
|
throw new Error(`MongoDB not connected`);
|
|
|
|
if (typeof db !== 'string')
|
|
|
|
throw new TypeError('Expecting collection name for the first argument');
|
|
|
|
|
|
|
|
this.#logger.debug(`Incoming push query for ${db}, with upsert ${upsert} and with parameters ${inspect(filter)} and data ${inspect(data)}`);
|
|
|
|
const result = await this.#db.collection(db).updateOne(filter, { $push: data }, { upsert });
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a random element from a database
|
|
|
|
*
|
|
|
|
* @param {string} db The collection to query
|
|
|
|
* @param {object} [filter={}] The filtering object to narrow down the sample pool
|
|
|
|
* @param {number} [amount=1] Amount of items to return
|
|
|
|
* @returns {object}
|
|
|
|
* @memberof Database
|
|
|
|
*/
|
|
|
|
random (db: string, filter = {}, amount = 1) {
|
|
|
|
|
|
|
|
if (!this.#db)
|
|
|
|
throw new Error(`MongoDB not connected`);
|
|
|
|
if (typeof db !== 'string')
|
|
|
|
throw new TypeError('Expecting collection name for the first argument');
|
2023-04-13 10:11:39 +02:00
|
|
|
|
2023-04-12 13:23:41 +02:00
|
|
|
this.#logger.debug(`Incoming random query for ${db} with parameters ${inspect(filter)} and amount ${amount}`);
|
|
|
|
|
|
|
|
if (amount > 100)
|
|
|
|
amount = 100;
|
|
|
|
|
|
|
|
const cursor = this.#db.collection(db).aggregate([{ $match: filter }, { $sample: { size: amount } }]);
|
|
|
|
return cursor.toArray();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
stats (options = {}) {
|
|
|
|
if (!this.#db)
|
|
|
|
throw new Error(`MongoDB not connected`);
|
|
|
|
const result = this.#db.stats(options);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
collection (coll: string) {
|
|
|
|
if (!this.#db)
|
|
|
|
throw new Error(`MongoDB not connected`);
|
|
|
|
return this.#db.collection(coll);
|
|
|
|
}
|
|
|
|
|
2023-04-14 18:54:01 +02:00
|
|
|
async ensureIndex (collection: string, indices: string[] = []) {
|
2023-04-12 13:23:41 +02:00
|
|
|
if (!this.#db)
|
|
|
|
throw new Error(`MongoDB not connected`);
|
|
|
|
if (!(indices instanceof Array))
|
|
|
|
indices = [ indices ];
|
|
|
|
await this.#db.collection(collection).createIndex(indices);
|
|
|
|
}
|
|
|
|
|
2023-04-14 21:21:20 +02:00
|
|
|
// async getKey (key: string, collection = 'memoryStore') {
|
|
|
|
// const response = await this.findOne(collection, { key });
|
|
|
|
// if (response)
|
|
|
|
// return response.value;
|
|
|
|
// return null;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// async setKey (key: string, value: object, collection = 'memoryStore') {
|
|
|
|
// await this.updateOne(collection, { key }, { value }, true);
|
|
|
|
// return value;
|
|
|
|
// }
|
2023-04-12 13:23:41 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export { MongoDB };
|