-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
40 lines (27 loc) · 917 Bytes
/
db.js
File metadata and controls
40 lines (27 loc) · 917 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
37
38
39
/**
* @fileoverview This file is the database connection file.
**/
const mongoose = require("mongoose");
/**
* Returns the Mongoose instance (having established the default connection)
* @param {string} ip : where the database is hosted
* @param {string|number} port : the port of the database
* @param {string} dbName : the name of the database to access
* @returns {Promise<*|undefined>}
*/
function dbDefaultConnect(ip, port, dbName) {
const dbUri = `mongodb://${ip}:${port}/${dbName}`;
const init = async () => {
try {
return await mongoose.connect(dbUri, {
autoIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true
});
} catch (error) {
console.error(`ERROR connection to ${dbUri} : ${error}`);
}
};
return init();
}
module.exports = dbDefaultConnect;