From ff6d120860c01a12cc75358a7316d18ec978577e Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Tue, 19 Nov 2024 11:31:07 +0200 Subject: [PATCH 1/2] feat: add mongoDb connet function --- package.json | 2 +- src/service/databaseManager.ts | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 252311b..61eda86 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@togethercrew.dev/db", - "version": "3.0.72", + "version": "3.0.73", "description": "All interactions with DB", "main": "./dist/index.js", "types": "./dist/index.d.ts", diff --git a/src/service/databaseManager.ts b/src/service/databaseManager.ts index 99415e8..e096f5f 100644 --- a/src/service/databaseManager.ts +++ b/src/service/databaseManager.ts @@ -22,7 +22,6 @@ export default class DatabaseManager { private static instance: DatabaseManager; private modelCache: Record = {}; - // Singleton pattern to get the instance of DatabaseManager public static getInstance(): DatabaseManager { if (typeof DatabaseManager.instance === 'undefined') { DatabaseManager.instance = new DatabaseManager(); @@ -30,7 +29,15 @@ export default class DatabaseManager { return DatabaseManager.instance; } - // Method to get Guild Database connection + public async connectToMongoDB(url: string): Promise { + try { + await mongoose.connect(url); + console.log('Connected to MongoDB!'); + } catch (error) { + console.log({ error }, 'Failed to connect to MongoDB!'); + } + } + public async getGuildDb(guildId: Snowflake): Promise { const dbName = guildId; const db = mongoose.connection.useDb(dbName, { useCache: true }); @@ -38,7 +45,6 @@ export default class DatabaseManager { return db; } - // Method to get Platform Database connection public async getPlatformDb(platformId: string): Promise { const dbName = platformId; const db = mongoose.connection.useDb(dbName, { useCache: true }); @@ -46,7 +52,6 @@ export default class DatabaseManager { return db; } - // Method to setup models based on database type private async setupModels(db: Connection, dbType: 'guild' | 'platform'): Promise { if (!this.modelCache[db.name]) { try { @@ -66,7 +71,6 @@ export default class DatabaseManager { } } - // Method to delete a database using the connection public async deleteDatabase(db: Connection): Promise { const dbName = db.name; try { From 164800da1ca1f9aa27e1e7ec4ac623d774ebaf19 Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Tue, 19 Nov 2024 11:41:49 +0200 Subject: [PATCH 2/2] feat: add disconnect method and track connection --- src/service/databaseManager.ts | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/service/databaseManager.ts b/src/service/databaseManager.ts index e096f5f..edab662 100644 --- a/src/service/databaseManager.ts +++ b/src/service/databaseManager.ts @@ -21,23 +21,41 @@ import { type Snowflake } from 'discord.js'; export default class DatabaseManager { private static instance: DatabaseManager; private modelCache: Record = {}; - - public static getInstance(): DatabaseManager { - if (typeof DatabaseManager.instance === 'undefined') { - DatabaseManager.instance = new DatabaseManager(); - } - return DatabaseManager.instance; - } + private mongoConnection: mongoose.Connection | null = null; public async connectToMongoDB(url: string): Promise { try { + if (this.mongoConnection !== null) { + throw new Error('MongoDB connection already exists'); + } await mongoose.connect(url); + this.mongoConnection = mongoose.connection; console.log('Connected to MongoDB!'); } catch (error) { console.log({ error }, 'Failed to connect to MongoDB!'); + throw new Error(`Failed to connect to MongoDB: ${error instanceof Error ? error.message : 'Unknown error'}`); } } + public async disconnectFromMongoDB(): Promise { + try { + if (this.mongoConnection !== null) { + await this.mongoConnection.close(); + this.mongoConnection = null; + console.log('Disconnected from MongoDB'); + } + } catch (error) { + throw new Error(`Failed to disconnect from MongoDB: ${error instanceof Error ? error.message : 'Unknown error'}`); + } + } + + public static getInstance(): DatabaseManager { + if (typeof DatabaseManager.instance === 'undefined') { + DatabaseManager.instance = new DatabaseManager(); + } + return DatabaseManager.instance; + } + public async getGuildDb(guildId: Snowflake): Promise { const dbName = guildId; const db = mongoose.connection.useDb(dbName, { useCache: true });