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; } } }