Skip to content

Commit

Permalink
Merge pull request #173 from TogetherCrew/adapte-with-analyzer-platfo…
Browse files Browse the repository at this point in the history
…rm-agnostic

feat: add platform and guild db for the connection managment
  • Loading branch information
cyri113 authored Jul 11, 2024
2 parents 24f52b6 + 8fa8b18 commit 5477322
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
41 changes: 30 additions & 11 deletions src/service/databaseManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,48 @@ import { type Snowflake } from 'discord.js';
export default class DatabaseManager {
private static instance: DatabaseManager;
private modelCache: Record<string, boolean> = {};

// Singleton pattern to get the instance of DatabaseManager
public static getInstance(): DatabaseManager {
if (typeof DatabaseManager.instance === 'undefined') {
DatabaseManager.instance = new DatabaseManager();
}
return DatabaseManager.instance;
}

public async getTenantDb(tenantId: Snowflake): Promise<Connection> {
const dbName = tenantId;
// Method to get Guild Database connection
public async getGuildDb(guildId: Snowflake): Promise<Connection> {
const dbName = guildId;
const db = mongoose.connection.useDb(dbName, { useCache: true });
await this.setupModels(db, 'guild');
return db;
}

// Method to get Platform Database connection
public async getPlatformDb(platformId: string): Promise<Connection> {
const dbName = platformId;
const db = mongoose.connection.useDb(dbName, { useCache: true });
await this.setupModels(db);
await this.setupModels(db, 'platform');
return db;
}

private async setupModels(db: Connection): Promise<void> {
// Method to setup models based on database type
private async setupModels(db: Connection, dbType: 'guild' | 'platform'): Promise<void> {
if (!this.modelCache[db.name]) {
db.model<IHeatMap>('HeatMap', heatMapSchema);
db.model<IRawInfo>('RawInfo', rawInfoSchema);
db.model<IMemberActivity>('MemberActivity', MemberActivitySchema);
db.model<IGuildMember>('GuildMember', guildMemberSchema);
db.model<IChannel>('Channel', channelSchema);
db.model<IRole>('Role', roleSchema);
this.modelCache[db.name] = true;
try {
if (dbType === 'platform') {
db.model<IHeatMap>('HeatMap', heatMapSchema);
db.model<IMemberActivity>('MemberActivity', MemberActivitySchema);
} else if (dbType === 'guild') {
db.model<IRawInfo>('RawInfo', rawInfoSchema);
db.model<IGuildMember>('GuildMember', guildMemberSchema);
db.model<IChannel>('Channel', channelSchema);
db.model<IRole>('Role', roleSchema);
}
this.modelCache[db.name] = true;
} catch (error) {
console.error(`Error setting up models for ${db.name}:`, error);
}
}
}
}

0 comments on commit 5477322

Please sign in to comment.