2024-11-09 18:36:24 -05:00
|
|
|
import { MongoClient } from 'mongodb';
|
|
|
|
|
import dotenv from 'dotenv'
|
|
|
|
|
|
|
|
|
|
dotenv.config();
|
|
|
|
|
|
|
|
|
|
export class DBConnection {
|
|
|
|
|
|
|
|
|
|
mongoURI:string = process.env.MONGO_URI ?? "localhost:27017"
|
|
|
|
|
databaseName:string = process.env.MONGO_DATABASE ?? "mangodb"
|
|
|
|
|
connection:MongoClient | undefined = undefined;
|
|
|
|
|
|
|
|
|
|
async connect() {
|
|
|
|
|
const client = new MongoClient(this.mongoURI!);
|
|
|
|
|
this.connection = await client.connect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getConnection() {
|
|
|
|
|
if (!this.connection) {
|
|
|
|
|
throw new Error('Connexion MongoDB non établie');
|
|
|
|
|
}
|
|
|
|
|
return this.connection.db(this.databaseName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-10 10:19:05 -05:00
|
|
|
export default DBConnection
|