Last active
January 31, 2019 17:01
-
-
Save THEozmic/555b5647d3291e15bd65ced7fde0babc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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