Skip to content

Instantly share code, notes, and snippets.

@THEozmic
Last active January 31, 2019 17:01
Show Gist options
  • Save THEozmic/555b5647d3291e15bd65ced7fde0babc to your computer and use it in GitHub Desktop.
Save THEozmic/555b5647d3291e15bd65ced7fde0babc to your computer and use it in GitHub Desktop.
import mongoose from 'mongoose'
const dotenv = require('dotenv')
dotenv.config()
const env = { ...process.env }
const isProduction = env.NODE_ENV === 'production'
// Initialize connection to database
const dbUrl = isProduction ? process.env.DATABASE_PROD : process.env.DATABASE_DEV,
dbOptions = {
useNewUrlParser: true,
useFindAndModify: false
}
// Set DB from mongoose connection
mongoose.connect(dbUrl, dbOptions)
const db = mongoose.connection
db.on('error', console.error.bind(console, 'MongoDB connection error:'))
export default db
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment