Skip to content

Commit

Permalink
This will work better
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamint08 committed May 29, 2024
1 parent fbc0643 commit 5b44704
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/instances/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,32 @@ import chalk from "chalk";
class Mongo {
private client: MongoClient | null = null;
private isConnected: boolean = false;
private tryCount: number = 0;

async connect(url: string): Promise<void> {
this.client = new MongoClient(url);
const start = Date.now();
console.log(chalk.bold.whiteBright(`Connecting to MongoDB...`));
await this.client.connect().then(() => {
console.log(chalk.bold.whiteBright(`MongoDB connected in `) + chalk.bold.greenBright(`${Date.now() - start}ms`));
this.isConnected = true;
});

this.client.on('close', async () => {
this.isConnected = false;
await this.connect(url);
});
try {
this.tryCount++;
await this.client.connect().then(() => {
this.tryCount = 0;
console.log(chalk.bold.whiteBright(`MongoDB connected in `) + chalk.bold.greenBright(`${Date.now() - start}ms`));
this.isConnected = true;
});

this.client.on('close', async () => {
this.isConnected = false;
await this.connect(url);
});
} catch (e) {
console.log(chalk.bold.whiteBright(`MongoDB connection failed in `) + chalk.bold.redBright(`${Date.now() - start}ms`));
if (this.tryCount < 5) {
await this.connect(url);
} else {
throw new Error('Failed to connect to MongoDB. Are you sure the URL is correct? 🥲');
}
}
}

async getCollection(db: string, col: string): Promise<any> {
Expand Down

0 comments on commit 5b44704

Please sign in to comment.