EvalueTonSavoir/server/config/db.js

63 lines
2 KiB
JavaScript
Raw Permalink Normal View History

2024-03-29 20:08:34 -04:00
const { MongoClient } = require('mongodb');
const dotenv = require('dotenv');
2024-03-29 20:08:34 -04:00
dotenv.config();
class DBConnection {
constructor() {
this.mongoURI = process.env.MONGO_URI;
this.databaseName = process.env.MONGO_DATABASE;
this.client = null;
2024-03-29 20:08:34 -04:00
this.connection = null;
}
// Connect to the database, but don't reconnect if already connected
2024-03-29 20:08:34 -04:00
async connect() {
if (this.connection) {
console.log('Using existing MongoDB connection');
return this.connection;
}
try {
// If client was previously closed but we're trying to reconnect
if (this.client && this.client.topology && this.client.topology.s.state === 'closed') {
// Create a new client if the previous one was closed
this.client = new MongoClient(this.mongoURI);
} else if (!this.client) {
// Create the MongoClient only if it doesn't exist
this.client = new MongoClient(this.mongoURI);
}
await this.client.connect();
this.connection = this.client.db(this.databaseName);
console.log('MongoDB connected');
return this.connection;
} catch (error) {
console.error('MongoDB connection error:', error);
throw new Error('Failed to connect to MongoDB');
}
2024-03-29 20:08:34 -04:00
}
// Return the current database connection
2024-03-29 20:08:34 -04:00
getConnection() {
if (!this.connection) {
throw new Error('MongoDB connection not established');
}
return this.connection;
}
// Close the MongoDB connection gracefully
async closeConnection() {
if (this.client) {
await this.client.close();
this.connection = null;
this.client = null;
console.log('MongoDB connection closed');
2024-03-29 20:08:34 -04:00
}
}
}
// Exporting the singleton instance
2024-03-29 20:08:34 -04:00
const instance = new DBConnection();
module.exports = instance;