-
Notifications
You must be signed in to change notification settings - Fork 3
/
db.js
23 lines (18 loc) · 776 Bytes
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// import and use mongodb.MongoClient
const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient;
const dbConnectionUrl = 'mongodb+srv://marwan:marwan@cluster0-1mq06.mongodb.net/test?retryWrites=true&w=majority';
function initialize(dbName, dbCollectionName, successCallback, failureCallback) {
MongoClient.connect(dbConnectionUrl, function (err, dbInstance) {
if (err) {
console.log(`[MongoDB connection] ERROR: ${err}`);
failureCallback(err); // this should be "caught" by the calling function
} else {
const dbObject = dbInstance.db(dbName);
const dbCollection = dbObject.collection(dbCollectionName);
console.log("[MongoDB connection] SUCCESS");
successCallback(dbCollection);
}
});
}
module.exports = { initialize };