Compare commits
No commits in common. "f1279a8a73fd8bc3c7336d64911e54d1445cbbc5" and "fc8eb81ffe9abc8f72e024f3170d1cdade6d11f7" have entirely different histories.
f1279a8a73
...
fc8eb81ffe
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@navy.gif/wrappers",
|
"name": "@navy.gif/wrappers",
|
||||||
"version": "1.3.3",
|
"version": "1.3.0",
|
||||||
"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",
|
||||||
|
@ -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`);
|
||||||
|
@ -24,7 +24,6 @@ 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,
|
||||||
@ -57,7 +56,6 @@ class MessageBroker {
|
|||||||
|
|
||||||
// Broker definitions
|
// Broker definitions
|
||||||
#hosts: string[];
|
#hosts: string[];
|
||||||
#port: number;
|
|
||||||
#username: string;
|
#username: string;
|
||||||
#password: string;
|
#password: string;
|
||||||
#vhost: string;
|
#vhost: string;
|
||||||
@ -82,13 +80,6 @@ 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 || {};
|
||||||
@ -113,7 +104,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.#port}/${this.#vhost}`);
|
const connectionStrings = this.#hosts.map(host => `amqp://${credentials}${host}/${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 }) => {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { inspect } from "node:util";
|
import { inspect } from "node:util";
|
||||||
import { MongoClient, MongoClientOptions, Db, Document, WithId, ObjectId } from "mongodb";
|
import { MongoClient, MongoClientOptions, Db, Document, WithId } 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,16 +124,13 @@ 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 & {_id: unknown}, options?: object): Promise<WithId<T>[]> {
|
async find<T extends Document> (db: string, query: object, options?: object): Promise<WithId<T>[]> {
|
||||||
|
|
||||||
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 = 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)}`);
|
||||||
|
|
||||||
@ -150,15 +147,12 @@ 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 & {_id: unknown}, options = {}): Promise<WithId<T> | null> {
|
async findOne<T extends Document> (db: string, query: object, 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);
|
||||||
@ -175,8 +169,7 @@ 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
|
||||||
*/
|
*/
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
async updateMany (db: string, filter: object, data: object, upsert = false) {
|
||||||
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`);
|
||||||
@ -184,9 +177,6 @@ class MongoDB {
|
|||||||
throw new TypeError('Expecting collection name for the first argument');
|
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`);
|
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 });
|
||||||
@ -203,16 +193,12 @@ 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
|
||||||
*/
|
*/
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
async updateOne (db: string, filter: object, data: object, upsert = false) {
|
||||||
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 });
|
||||||
|
Loading…
Reference in New Issue
Block a user