23 lines
429 B
JavaScript
23 lines
429 B
JavaScript
![]() |
const mongodb = require('mongodb');
|
||
|
|
||
|
const MongoClient = mongodb.MongoClient;
|
||
|
|
||
|
let database;
|
||
|
|
||
|
async function connectToDatabase() {
|
||
|
const client = await MongoClient.connect('mongodb://127.0.0.1:27017')
|
||
|
database = client.db('BDS');
|
||
|
}
|
||
|
|
||
|
function getDb() {
|
||
|
if (!database) {
|
||
|
throw { message: 'You must connect first!' };
|
||
|
}
|
||
|
return database;
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
connectToDatabase: connectToDatabase,
|
||
|
getDb: getDb,
|
||
|
};
|