24 lines
493 B
JavaScript
24 lines
493 B
JavaScript
const mongodb = require('mongodb');
|
|
|
|
const MongoClient = mongodb.MongoClient;
|
|
|
|
let database;
|
|
|
|
async function connectToDatabase() {
|
|
const url = 'mongodb://127.0.0.1:27017/BDE'; // Ajout du nom de la base de données
|
|
const client = await MongoClient.connect(url);
|
|
database = client.db('BDE');
|
|
}
|
|
|
|
|
|
function getDb() {
|
|
if (!database) {
|
|
throw { message: 'You must connect first!' };
|
|
}
|
|
return database;
|
|
}
|
|
|
|
module.exports = {
|
|
connectToDatabase: connectToDatabase,
|
|
getDb: getDb,
|
|
};
|