From 0feda4935448ababe2a9aaf4120b06772d6a7f83 Mon Sep 17 00:00:00 2001 From: Behzad Rabiei Date: Thu, 11 Jul 2024 14:50:23 +0400 Subject: [PATCH 1/3] feat: add platform and guild db for the connection managment --- package.json | 2 +- src/service/databaseManager.ts | 28 +++++++++++++++++++--------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index aa660da..7d7fd53 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@togethercrew.dev/db", - "version": "3.0.62", + "version": "3.0.63", "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 43f822e..c18ef2c 100644 --- a/src/service/databaseManager.ts +++ b/src/service/databaseManager.ts @@ -28,21 +28,31 @@ export default class DatabaseManager { return DatabaseManager.instance; } - public async getTenantDb(tenantId: Snowflake): Promise { + public async getGuildDb(tenantId: Snowflake): Promise { const dbName = tenantId; const db = mongoose.connection.useDb(dbName, { useCache: true }); - await this.setupModels(db); + await this.setupModels(db, 'guild'); return db; } - private async setupModels(db: Connection): Promise { + public async getPlatformDb(tenantId: Snowflake): Promise { + const dbName = tenantId; + const db = mongoose.connection.useDb(dbName, { useCache: true }); + await this.setupModels(db, 'platform'); + return db; + } + + private async setupModels(db: Connection, dbType: 'guild' | 'platform'): Promise { if (!this.modelCache[db.name]) { - db.model('HeatMap', heatMapSchema); - db.model('RawInfo', rawInfoSchema); - db.model('MemberActivity', MemberActivitySchema); - db.model('GuildMember', guildMemberSchema); - db.model('Channel', channelSchema); - db.model('Role', roleSchema); + if (dbType === 'platform') { + db.model('HeatMap', heatMapSchema); + db.model('MemberActivity', MemberActivitySchema); + } else if (dbType === 'guild') { + db.model('RawInfo', rawInfoSchema); + db.model('GuildMember', guildMemberSchema); + db.model('Channel', channelSchema); + db.model('Role', roleSchema); + } this.modelCache[db.name] = true; } } From 822a290dc092b68aa573245b4950a0678defa14a Mon Sep 17 00:00:00 2001 From: Behzad Rabiei Date: Thu, 11 Jul 2024 15:39:34 +0400 Subject: [PATCH 2/3] refactor: refactor the databaseManger --- src/service/databaseManager.ts | 35 +++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/service/databaseManager.ts b/src/service/databaseManager.ts index c18ef2c..bfcc3b1 100644 --- a/src/service/databaseManager.ts +++ b/src/service/databaseManager.ts @@ -21,6 +21,8 @@ import { type Snowflake } from 'discord.js'; 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(); @@ -28,32 +30,39 @@ export default class DatabaseManager { return DatabaseManager.instance; } - public async getGuildDb(tenantId: Snowflake): Promise { - const dbName = tenantId; + // Method to get Guild Database connection + public async getGuildDb(guildId: Snowflake): Promise { + const dbName = guildId; const db = mongoose.connection.useDb(dbName, { useCache: true }); await this.setupModels(db, 'guild'); return db; } - public async getPlatformDb(tenantId: Snowflake): Promise { - const dbName = tenantId; + // Method to get Platform Database connection + public async getPlatformDb(platformId: Snowflake): Promise { + const dbName = platformId; const db = mongoose.connection.useDb(dbName, { useCache: true }); await this.setupModels(db, 'platform'); return db; } + // Method to setup models based on database type private async setupModels(db: Connection, dbType: 'guild' | 'platform'): Promise { if (!this.modelCache[db.name]) { - if (dbType === 'platform') { - db.model('HeatMap', heatMapSchema); - db.model('MemberActivity', MemberActivitySchema); - } else if (dbType === 'guild') { - db.model('RawInfo', rawInfoSchema); - db.model('GuildMember', guildMemberSchema); - db.model('Channel', channelSchema); - db.model('Role', roleSchema); + try { + if (dbType === 'platform') { + db.model('HeatMap', heatMapSchema); + db.model('MemberActivity', MemberActivitySchema); + } else if (dbType === 'guild') { + db.model('RawInfo', rawInfoSchema); + db.model('GuildMember', guildMemberSchema); + db.model('Channel', channelSchema); + db.model('Role', roleSchema); + } + this.modelCache[db.name] = true; + } catch (error) { + console.error(`Error setting up models for ${db.name}:`, error); } - this.modelCache[db.name] = true; } } } From 8fa8b18c77ab75420776aba6b9d7dfb80a0b10fa Mon Sep 17 00:00:00 2001 From: Behzad Rabiei Date: Thu, 11 Jul 2024 15:44:13 +0400 Subject: [PATCH 3/3] refactor: refactor the databaseManger --- src/service/databaseManager.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/service/databaseManager.ts b/src/service/databaseManager.ts index bfcc3b1..49f355b 100644 --- a/src/service/databaseManager.ts +++ b/src/service/databaseManager.ts @@ -39,7 +39,7 @@ export default class DatabaseManager { } // Method to get Platform Database connection - public async getPlatformDb(platformId: Snowflake): Promise { + public async getPlatformDb(platformId: string): Promise { const dbName = platformId; const db = mongoose.connection.useDb(dbName, { useCache: true }); await this.setupModels(db, 'platform');