Created
October 17, 2020 09:10
-
-
Save miladr0/d678294715250cf1ccc0918ae68d7a3c 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'; | |
import HitApi from './models/hitApi.model'; | |
mongoose.Promise = Promise; | |
// Exit application on error | |
mongoose.connection.on('error', (err) => { | |
console.error(`MongoDB connection error: ${err}`); | |
process.exit(-1); | |
}); | |
// print mongoose logs in dev env | |
if (process.env.ENV === 'development') { | |
mongoose.set('debug', true); | |
} | |
/** | |
* Connect to mongo db | |
* | |
* @returns {object} Mongoose connection | |
* @public | |
*/ | |
export const connectToMongo = () => { | |
mongoose.connect(process.env.MONGO_URI, { | |
useFindAndModify: false, | |
keepAlive: 1, | |
useNewUrlParser: true, | |
useCreateIndex: true, | |
user: process.env.MONGO_USER, | |
pass: process.env.MONGO_PASS, | |
auth: { authSource: 'admin' }, | |
}); | |
return mongoose.connection; | |
}; | |
export const getMongoConnection = () => mongoose.connection; | |
const models = { HitApi }; | |
export default models; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment