-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
40 lines (36 loc) · 879 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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const { MongoClient } = require("mongodb");
// Database URL
const url = process.env.DB;
// Preparing the Mongo cilent database
const client = new MongoClient(url);
/** Connect to the Mongodb Client */
module.exports.connect = async function () {
try {
await client.connect();
}
catch (err) {
// error handling
throw new Error(err.message)
}
}
/** Returns the Mongo's database (db) object of the given name
* @param {String} dbName - The name of Database
* @returns db (MongoClient.db)
*/
module.exports.getDB = function (dbName) {
try {
return client.db(dbName)
}
catch (err) {
throw new Error(err.message)
}
}
/** Close mongoDB connection */
module.exports.close = async function () {
try {
await client.close();
}
catch (err) {
throw new Error(err.message)
}
}