-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added mongo db config and handled uncaught error
- Loading branch information
Prasun Jaiswal
committed
Mar 1, 2020
1 parent
150d435
commit b1b02bf
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const mongoose = require('mongoose'); | ||
const chalk = require('chalk'); | ||
const { | ||
log, | ||
error, | ||
info | ||
} = require('../utils/logging'); | ||
|
||
function connectDb() { | ||
mongoose.connect(process.env.MONGO_URI, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true | ||
}); | ||
|
||
mongoose.connection.on('connected', function () { | ||
info(chalk.blue(' [ ✓ ] ') + 'Application - Connected to MongoDb'); | ||
}); | ||
|
||
mongoose.connection.on('error', function (err) { | ||
error('Mongoose default connection has occured ' + err + ' error') | ||
}); | ||
|
||
mongoose.connection.on('disconnected', function () { | ||
info('Mongoose default connection is disconnected') | ||
}); | ||
|
||
process.on('SIGINT', function () { | ||
mongoose.connection.close(function () { | ||
info('Mongoose default connection is disconnected due to application termination') | ||
}); | ||
}); | ||
} | ||
|
||
module.exports = connectDb() |