-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect-db.js
39 lines (33 loc) · 880 Bytes
/
connect-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
import mongoose from 'mongoose';
import debugLib from 'debug';
const debug = debugLib('server:database');
mongoose.set('strictQuery', false);
export default async (connection, dbName) => {
try {
await mongoose.connect(connection, {
dbName,
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 5000,
});
const isConnected = mongoose.connection.readyState;
switch (isConnected) {
case 0:
console.log('MongoDB is disconnected');
break;
case 1:
console.log('MongoDB is connected');
break;
case 2:
console.log('MongoDB is connecting');
break;
case 3:
console.log('MongoDB is disconnecting');
break;
default:
console.log('Unknown MongoDB connection status');
}
} catch (error) {
debug(error);
}
};