-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathdb.js
More file actions
30 lines (26 loc) · 768 Bytes
/
db.js
File metadata and controls
30 lines (26 loc) · 768 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
const mongoose = require("mongoose");
const dotenv = require("dotenv");
dotenv.config();
const connectDB = async () => {
try {
const ConnectDB = process.env.MONGODB_URI;
//Removing the options as they are no longer needed from mongoose6+
await mongoose.connect(ConnectDB);
console.log("MongoDB Connected");
} catch (error) {
console.error("MongoDB Connection Error:", error);
}
};
// connectDB()
const closeDB = () => {
mongoose.connection
.close()
.then(() => {
console.log("MongoDB connection closed");
})
.catch((err) => {
console.error("Error while closing MongoDB connection:", err);
});
};
// Call the connectDB function to establish the MongoDB connection
module.exports = { connectDB, closeDB };