Compare commits

..

4 Commits

Author SHA1 Message Date
f1279a8a73
v1.3.3 2023-04-22 13:29:35 +03:00
005a842004
v1.3.2 2023-04-16 18:17:48 +03:00
8de4dd83e2
Inverted logic.. 2023-04-16 18:17:34 +03:00
5bacdfdf11
v1.3.1 2023-04-16 16:51:19 +03:00
4 changed files with 33 additions and 10 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@navy.gif/wrappers", "name": "@navy.gif/wrappers",
"version": "1.3.0", "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",

View File

@ -79,7 +79,7 @@ class MariaDB {
async init () { async init () {
if (this.#config.load) if (!this.#config.load)
return this.#logger.info('Not loading MariaDB'); return this.#logger.info('Not loading MariaDB');
this.#logger.status(`Creating${this.#cluster ? ' cluster' : ''} connection pool`); this.#logger.status(`Creating${this.#cluster ? ' cluster' : ''} connection pool`);

View File

@ -24,6 +24,7 @@ type QueueDef = {
export type BrokerOptions = { export type BrokerOptions = {
host: string, host: string,
port: number
user: string, user: string,
pass: string, pass: string,
vhost: string, vhost: string,
@ -56,6 +57,7 @@ class MessageBroker {
// Broker definitions // Broker definitions
#hosts: string[]; #hosts: string[];
#port: number;
#username: string; #username: string;
#password: string; #password: string;
#vhost: string; #vhost: string;
@ -80,6 +82,13 @@ class MessageBroker {
this.#username = options.user; this.#username = options.user;
this.#password = options.pass; this.#password = options.pass;
this.#vhost = options.vhost || ''; 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.#exchanges = options.exchanges || {};
this.#queues = options.queues || {}; this.#queues = options.queues || {};
@ -104,7 +113,7 @@ class MessageBroker {
this.#logger.info('Initialising message broker'); this.#logger.info('Initialising message broker');
const credentials = this.#username ? `${this.#username}:${this.#password}@` : ''; 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 = await amqp.connect(connectionStrings);
this.#connection.on('disconnect', async ({ err }) => { this.#connection.on('disconnect', async ({ err }) => {

View File

@ -1,5 +1,5 @@
import { inspect } from "node:util"; 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"; import { IServer, ILogger, LoggerClientOptions } from "./interfaces/index.js";
type Credentials = { type Credentials = {
@ -15,7 +15,7 @@ type Credentials = {
export type MongoOptions = { export type MongoOptions = {
credentials: Credentials, credentials: Credentials,
loggerOptions?: LoggerClientOptions, loggerOptions?: LoggerClientOptions,
client: MongoClientOptions, client?: MongoClientOptions,
load?: boolean load?: boolean
} }
@ -91,7 +91,7 @@ class MongoDB {
*/ */
async init () { async init () {
if (this.#config.load) if (!this.#config.load)
return this.#logger.info('Not loading MongoDB'); return this.#logger.info('Not loading MongoDB');
this.#logger.status(`Initializing database connection to ${this.#_client.options.hosts}`); this.#logger.status(`Initializing database connection to ${this.#_client.options.hosts}`);
@ -124,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: 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) if (!this.#db)
throw new Error(`MongoDB not connected`); throw new Error(`MongoDB not connected`);
@ -132,6 +132,9 @@ class MongoDB {
if (typeof db !== 'string') if (typeof db !== 'string')
throw new TypeError('Expecting collection name for the first argument'); 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)}`); this.#logger.debug(`Incoming find query for ${db} with parameters ${inspect(query)}`);
const cursor = this.#db.collection<T>(db).find(query, options); const cursor = this.#db.collection<T>(db).find(query, options);
@ -147,13 +150,16 @@ 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: 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) if (!this.#db)
throw new Error(`MongoDB not connected`); throw new Error(`MongoDB not connected`);
if (typeof db !== 'string') if (typeof db !== 'string')
throw new TypeError('Expecting collection name for the first argument'); 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)}`); this.#logger.debug(`Incoming findOne query for ${db} with parameters ${inspect(query)}`);
const result = await this.#db.collection<T>(db).findOne(query, options); const result = await this.#db.collection<T>(db).findOne(query, options);
return result; return result;
@ -169,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: 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) if (!this.#db)
throw new Error(`MongoDB not connected`); throw new Error(`MongoDB not connected`);
@ -178,6 +185,9 @@ class MongoDB {
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`); 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)}`); 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 }); const result = await this.#db.collection(db).updateMany(filter, { $set: data }, { upsert });
return result; return result;
@ -193,13 +203,17 @@ 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: 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) if (!this.#db)
throw new Error(`MongoDB not connected`); throw new Error(`MongoDB not connected`);
if (typeof db !== 'string') if (typeof db !== 'string')
throw new TypeError('Expecting collection name for the first argument'); 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)}`); this.#logger.debug(`Incoming updateOne query for ${db} with parameters ${inspect(filter)}`);
const result = await this.#db.collection(db).updateOne(filter, { $set: data }, { upsert }); const result = await this.#db.collection(db).updateOne(filter, { $set: data }, { upsert });
return result; return result;