Compare commits
4 Commits
fc8eb81ffe
...
f1279a8a73
Author | SHA1 | Date | |
---|---|---|---|
f1279a8a73 | |||
005a842004 | |||
8de4dd83e2 | |||
5bacdfdf11 |
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@navy.gif/wrappers",
|
||||
"version": "1.3.0",
|
||||
"version": "1.3.3",
|
||||
"description": "Various wrapper classes I use in my projects",
|
||||
"repository": "https://git.corgi.wtf/Navy.gif/wrappers.git",
|
||||
"author": "Navy.gif",
|
||||
|
@ -79,7 +79,7 @@ class MariaDB {
|
||||
|
||||
async init () {
|
||||
|
||||
if (this.#config.load)
|
||||
if (!this.#config.load)
|
||||
return this.#logger.info('Not loading MariaDB');
|
||||
|
||||
this.#logger.status(`Creating${this.#cluster ? ' cluster' : ''} connection pool`);
|
||||
|
@ -24,6 +24,7 @@ type QueueDef = {
|
||||
|
||||
export type BrokerOptions = {
|
||||
host: string,
|
||||
port: number
|
||||
user: string,
|
||||
pass: string,
|
||||
vhost: string,
|
||||
@ -56,6 +57,7 @@ class MessageBroker {
|
||||
|
||||
// Broker definitions
|
||||
#hosts: string[];
|
||||
#port: number;
|
||||
#username: string;
|
||||
#password: string;
|
||||
#vhost: string;
|
||||
@ -80,6 +82,13 @@ class MessageBroker {
|
||||
this.#username = options.user;
|
||||
this.#password = options.pass;
|
||||
this.#vhost = options.vhost || '';
|
||||
this.#port = options.port;
|
||||
|
||||
if (!this.#hosts.length)
|
||||
throw new Error('Missing hosts configuration');
|
||||
|
||||
if (!options.port)
|
||||
throw new Error('Missing port option');
|
||||
|
||||
this.#exchanges = options.exchanges || {};
|
||||
this.#queues = options.queues || {};
|
||||
@ -104,7 +113,7 @@ class MessageBroker {
|
||||
|
||||
this.#logger.info('Initialising message broker');
|
||||
const credentials = this.#username ? `${this.#username}:${this.#password}@` : '';
|
||||
const connectionStrings = this.#hosts.map(host => `amqp://${credentials}${host}/${this.#vhost}`);
|
||||
const connectionStrings = this.#hosts.map(host => `amqp://${credentials}${host}:${this.#port}/${this.#vhost}`);
|
||||
this.#connection = await amqp.connect(connectionStrings);
|
||||
|
||||
this.#connection.on('disconnect', async ({ err }) => {
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { inspect } from "node:util";
|
||||
import { MongoClient, MongoClientOptions, Db, Document, WithId } from "mongodb";
|
||||
import { MongoClient, MongoClientOptions, Db, Document, WithId, ObjectId } from "mongodb";
|
||||
import { IServer, ILogger, LoggerClientOptions } from "./interfaces/index.js";
|
||||
|
||||
type Credentials = {
|
||||
@ -15,7 +15,7 @@ type Credentials = {
|
||||
export type MongoOptions = {
|
||||
credentials: Credentials,
|
||||
loggerOptions?: LoggerClientOptions,
|
||||
client: MongoClientOptions,
|
||||
client?: MongoClientOptions,
|
||||
load?: boolean
|
||||
}
|
||||
|
||||
@ -91,7 +91,7 @@ class MongoDB {
|
||||
*/
|
||||
async init () {
|
||||
|
||||
if (this.#config.load)
|
||||
if (!this.#config.load)
|
||||
return this.#logger.info('Not loading MongoDB');
|
||||
|
||||
this.#logger.status(`Initializing database connection to ${this.#_client.options.hosts}`);
|
||||
@ -124,13 +124,16 @@ class MongoDB {
|
||||
* @returns {Array} An array containing the corresponding objects for the query
|
||||
* @memberof Database
|
||||
*/
|
||||
async find<T extends Document> (db: string, query: object, options?: object): Promise<WithId<T>[]> {
|
||||
async find<T extends Document> (db: string, query: object & {_id: unknown}, options?: object): Promise<WithId<T>[]> {
|
||||
|
||||
if (!this.#db)
|
||||
throw new Error(`MongoDB not connected`);
|
||||
|
||||
if (typeof db !== 'string')
|
||||
throw new TypeError('Expecting collection name for the first argument');
|
||||
|
||||
if (typeof query._id === 'string')
|
||||
query._id = new ObjectId(query._id);
|
||||
|
||||
this.#logger.debug(`Incoming find query for ${db} with parameters ${inspect(query)}`);
|
||||
|
||||
@ -147,12 +150,15 @@ class MongoDB {
|
||||
* @returns {Object} An object containing the queried data
|
||||
* @memberof Database
|
||||
*/
|
||||
async findOne<T extends Document> (db: string, query: object, options = {}): Promise<WithId<T> | null> {
|
||||
async findOne<T extends Document> (db: string, query: object & {_id: unknown}, options = {}): Promise<WithId<T> | null> {
|
||||
|
||||
if (!this.#db)
|
||||
throw new Error(`MongoDB not connected`);
|
||||
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);
|
||||
|
||||
this.#logger.debug(`Incoming findOne query for ${db} with parameters ${inspect(query)}`);
|
||||
const result = await this.#db.collection<T>(db).findOne(query, options);
|
||||
@ -169,7 +175,8 @@ class MongoDB {
|
||||
* @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified
|
||||
* @memberof Database
|
||||
*/
|
||||
async updateMany (db: string, filter: object, 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)
|
||||
throw new Error(`MongoDB not connected`);
|
||||
@ -177,6 +184,9 @@ class MongoDB {
|
||||
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`);
|
||||
|
||||
if (typeof filter._id === 'string')
|
||||
filter._id = new ObjectId(filter._id);
|
||||
|
||||
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 });
|
||||
@ -193,12 +203,16 @@ class MongoDB {
|
||||
* @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified
|
||||
* @memberof Database
|
||||
*/
|
||||
async updateOne (db: string, filter: object, 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)
|
||||
throw new Error(`MongoDB not connected`);
|
||||
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 updateOne query for ${db} with parameters ${inspect(filter)}`);
|
||||
const result = await this.#db.collection(db).updateOne(filter, { $set: data }, { upsert });
|
||||
|
Loading…
Reference in New Issue
Block a user