-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.js
29 lines (25 loc) · 810 Bytes
/
mongo.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
import mongoose from 'mongoose'
import * as config from './config.js'
const { mongo: mongoConfig } = config
async function connectToMongo() {
try {
const connection = await mongoose.connect(mongoConfig.uri, {
keepAlive: true,
autoIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true,
})
const {
connection: { host, port },
} = connection
console.log(
`Successfully connected to ${host}:${port} MongoDB cluster in ${process.env.NODE_ENV} mode.`
)
return connection
} catch (err) {
console.warn('Error while attempting to connect to MongoDB:', err)
// Exit app if we fail to connect to Mongo
throw err
}
}
export default connectToMongo