mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
25 lines
No EOL
658 B
TypeScript
25 lines
No EOL
658 B
TypeScript
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);
|
|
}
|
|
}
|
|
|
|
export default new DBConnection(); |