EvalueTonSavoir/server/config/db-connection.ts
2024-11-10 10:19:05 -05:00

25 lines
No EOL
651 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 DBConnection