2023-04-12 13:23:41 +02:00
|
|
|
import { inspect } from "node:util";
|
|
|
|
import { MongoClient, MongoClientOptions, Db } from "mongodb";
|
|
|
|
import { IServer, ILogger, LoggerClientOptions } from "./interfaces/index.js";
|
|
|
|
|
|
|
|
type Credentials = {
|
|
|
|
URI: string,
|
|
|
|
user: string,
|
|
|
|
password: string,
|
|
|
|
host: string,
|
|
|
|
port: number,
|
|
|
|
database: string,
|
|
|
|
authDb: string
|
|
|
|
}
|
|
|
|
|
|
|
|
type MongoOptions = {
|
|
|
|
credentials: Credentials,
|
|
|
|
loggerOptions: LoggerClientOptions,
|
|
|
|
client: MongoClientOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-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
|
|
|
|
*/
|
|
|
|
async find (db: string, query: object, options: object) {
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
this.#logger.debug(`Incoming find query for ${db} with parameters ${inspect(query)}`);
|
|
|
|
|
|
|
|
const cursor = this.#db.collection(db).find(query, options);
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
async findOne (db: string, query: object, options = {}) {
|
|
|
|
|
|
|
|
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 findOne query for ${db} with parameters ${inspect(query)}`);
|
|
|
|
const result = await this.#db.collection(db).findOne(query, options);
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
async updateMany (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');
|
|
|
|
if (!filter)
|
|
|
|
throw new Error(`Cannot run update many without a filter, if you mean to update every single document, pass an empty object`);
|
|
|
|
|
|
|
|
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 });
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
async updateOne (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 updateOne query for ${db} with parameters ${inspect(filter)}`);
|
|
|
|
const result = await this.#db.collection(db).updateOne(filter, { $set: data }, { upsert });
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
async ensureIndex (collection: string, indices = []) {
|
|
|
|
if (!this.#db)
|
|
|
|
throw new Error(`MongoDB not connected`);
|
|
|
|
if (!(indices instanceof Array))
|
|
|
|
indices = [ indices ];
|
|
|
|
await this.#db.collection(collection).createIndex(indices);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export { MongoDB };
|