mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
Merge pull request #287 from ets-cfuhrman-pfe/fuhrmanator/singleton-mongo-connection
Some checks failed
CI/CD Pipeline for Backend / build_and_push_backend (push) Failing after 19s
CI/CD Pipeline for Nginx Router / build_and_push_nginx (push) Failing after 18s
CI/CD Pipeline for Frontend / build_and_push_frontend (push) Failing after 18s
Tests / lint-and-tests (client) (push) Failing after 1m10s
Tests / lint-and-tests (server) (push) Failing after 56s
Some checks failed
CI/CD Pipeline for Backend / build_and_push_backend (push) Failing after 19s
CI/CD Pipeline for Nginx Router / build_and_push_nginx (push) Failing after 18s
CI/CD Pipeline for Frontend / build_and_push_frontend (push) Failing after 18s
Tests / lint-and-tests (client) (push) Failing after 1m10s
Tests / lint-and-tests (server) (push) Failing after 56s
Fixes #286 - Faire un vrai singleton pour la connexion à la BD
This commit is contained in:
commit
c57a4d69ab
2 changed files with 39 additions and 7 deletions
|
|
@ -127,4 +127,11 @@ async function start() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Graceful shutdown on SIGINT (Ctrl+C)
|
||||||
|
process.on('SIGINT', async () => {
|
||||||
|
console.log('Shutting down...');
|
||||||
|
await db.closeConnection();
|
||||||
|
process.exit(0);
|
||||||
|
});
|
||||||
|
|
||||||
start();
|
start();
|
||||||
|
|
|
||||||
|
|
@ -1,28 +1,53 @@
|
||||||
const { MongoClient } = require('mongodb');
|
const { MongoClient } = require('mongodb');
|
||||||
const dotenv = require('dotenv')
|
const dotenv = require('dotenv');
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
class DBConnection {
|
class DBConnection {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.mongoURI = process.env.MONGO_URI;
|
this.mongoURI = process.env.MONGO_URI;
|
||||||
this.databaseName = process.env.MONGO_DATABASE;
|
this.databaseName = process.env.MONGO_DATABASE;
|
||||||
|
this.client = null;
|
||||||
this.connection = null;
|
this.connection = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Connect to the database, but don't reconnect if already connected
|
||||||
async connect() {
|
async connect() {
|
||||||
const client = new MongoClient(this.mongoURI);
|
if (this.connection) {
|
||||||
this.connection = await client.connect();
|
console.log('Using existing MongoDB connection');
|
||||||
|
return this.connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Create the MongoClient only if the connection does not 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');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return the current database connection
|
||||||
getConnection() {
|
getConnection() {
|
||||||
if (!this.connection) {
|
if (!this.connection) {
|
||||||
throw new Error('Connexion MongoDB non établie');
|
throw new Error('MongoDB connection not established');
|
||||||
|
}
|
||||||
|
return this.connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the MongoDB connection gracefully
|
||||||
|
async closeConnection() {
|
||||||
|
if (this.client) {
|
||||||
|
await this.client.close();
|
||||||
|
console.log('MongoDB connection closed');
|
||||||
}
|
}
|
||||||
return this.connection.db(this.databaseName);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Exporting the singleton instance
|
||||||
const instance = new DBConnection();
|
const instance = new DBConnection();
|
||||||
module.exports = instance;
|
module.exports = instance;
|
||||||
Loading…
Reference in a new issue