-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
36 lines (34 loc) · 838 Bytes
/
db.js
File metadata and controls
36 lines (34 loc) · 838 Bytes
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
const mongoose = require('mongoose');
const db = {};
const init = async (dbConnectionStr) => {
const connection = mongoose.connection;
connection.on('error', (error) => {
console.error(error);
def.reject(error);
});
connection.once('open', () => {
console.log('Database connection established!');
});
const promise = new Promise((resolve, reject) => {
mongoose
.connect(dbConnectionStr, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log('DB Connected!');
db.connection = connection;
resolve(connection);
// db = client.db();
})
.catch((err) => {
reject(err);
console.log(`DB Connection Error: ${err.message}`);
});
});
return promise;
};
module.exports = {
db,
init,
};