Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. // Util function : /utils/connectDb.js
  2. // Use in routes handler like:
  3. // import connectDb from '../../utils/connectDb'
  4. // connectDb()
  5. // ...stuff
  6.  
  7. import mongoose from 'mongoose'
  8. const connection = {}
  9.  
  10. async function connectDb() {
  11. if (connection.isConnected) {
  12. console.log(`Using existing connection.`)
  13. return
  14. }
  15. const db = await mongoose.connect(process.env.MONGO_SRV, {
  16. useCreateIndex: true,
  17. useFindAndModify: false,
  18. useNewUrlParser: true,
  19. useUnifiedTopology: true
  20. })
  21. console.log(`DB created`)
  22. connection.isConnected = db.connections[0].readyState
  23. }
  24.  
  25. export default connectDb
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement