Skip to content

Commit

Permalink
Merge pull request #179 from TogetherCrew/add-connect-to-DB
Browse files Browse the repository at this point in the history
feat: add mongoDb connet function
  • Loading branch information
Behzad-rabiei authored Nov 19, 2024
2 parents 572f9ff + 164800d commit 22e9660
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 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.72",
"version": "3.0.73",
"description": "All interactions with DB",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
32 changes: 27 additions & 5 deletions src/service/databaseManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,55 @@ import { type Snowflake } from 'discord.js';
export default class DatabaseManager {
private static instance: DatabaseManager;
private modelCache: Record<string, boolean> = {};
private mongoConnection: mongoose.Connection | null = null;

public async connectToMongoDB(url: string): Promise<void> {
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<void> {
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'}`);
}
}

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

// 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, 'platform');
return db;
}

// Method to setup models based on database type
private async setupModels(db: Connection, dbType: 'guild' | 'platform'): Promise<void> {
if (!this.modelCache[db.name]) {
try {
Expand All @@ -66,7 +89,6 @@ export default class DatabaseManager {
}
}

// Method to delete a database using the connection
public async deleteDatabase(db: Connection): Promise<void> {
const dbName = db.name;
try {
Expand Down

0 comments on commit 22e9660

Please sign in to comment.