-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
30 lines (22 loc) · 798 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
const mongoose = require('mongoose');
require('dotenv').config();
// Define the MongoDB connection URL
const mongoURL = process.env.MONGODB_URL_LOCAL // Replace 'mydatabase' with your database name
// const mongoURL = process.env.MONGODB_URL;
// Set up MongoDB connection
mongoose.connect(mongoURL)
// Get the default connection
// Mongoose maintains a default connection object representing the MongoDB connection.
const db = mongoose.connection;
// Define event listeners for database connection
db.on('connected', () => {
console.log('Connected to MongoDB server');
});
db.on('error', (err) => {
console.error('MongoDB connection error:', err);
});
db.on('disconnected', () => {
console.log('MongoDB disconnected');
});
// Export the database connection
module.exports = db;