Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Util function : /utils/connectDb.js
- // Use in routes handler like:
- // import connectDb from '../../utils/connectDb'
- // connectDb()
- // ...stuff
- import mongoose from 'mongoose'
- const connection = {}
- async function connectDb() {
- if (connection.isConnected) {
- console.log(`Using existing connection.`)
- return
- }
- const db = await mongoose.connect(process.env.MONGO_SRV, {
- useCreateIndex: true,
- useFindAndModify: false,
- useNewUrlParser: true,
- useUnifiedTopology: true
- })
- console.log(`DB created`)
- connection.isConnected = db.connections[0].readyState
- }
- export default connectDb
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement