46 lines
1.7 KiB
JavaScript
46 lines
1.7 KiB
JavaScript
const
|
|
debug = require('debug')('agm:db'),
|
|
mongoose = require('mongoose');
|
|
// Our custom middleware
|
|
mongoose.Promise = global.Promise; // Disable Promise depreciated warning
|
|
const opts = {
|
|
user: 'agm', pass: 'Agm2017',
|
|
connectTimeoutMS: 40000,
|
|
// TCP Connection timeout setting. Best = 2-3X time of longest operation (30 minutes)
|
|
socketTimeoutMS: 1.8e+6,
|
|
// The number of milliseconds to wait before initiating keepAlive on the TCP socket.
|
|
keepAlive: true, keepAliveInitialDelay: 30000,
|
|
// for better performace in Production invironment
|
|
autoIndex: false,
|
|
// Disable buffering commands to report error as soon as connections get disconnected
|
|
bufferCommands: false,
|
|
useNewUrlParser: true,
|
|
useFindAndModify: false,
|
|
useCreateIndex: true,
|
|
useUnifiedTopology: true,
|
|
family: 4
|
|
}
|
|
mongoose.connect('mongodb://localhost:27017/agmission?authSource=agmission', opts);
|
|
mongoose.connection.on('disconnected', () => { debug('-> MongoDB lost connection'); });
|
|
mongoose.connection.on('reconnect', () => { debug('-> MongoDB reconnected'); });
|
|
mongoose.connection.on('connected', () => { debug('-> MongoDB connected'); });
|
|
mongoose.connection.on('reconnectFailed', () => { debug('-> MongoDB gave up reconnecting'); });
|
|
mongoose.connection.on('error', console.error.bind(console, 'MongoDB connection errors:'));
|
|
|
|
function postExit() {
|
|
if (mongoose.connection) {
|
|
mongoose.connection.close(() => {
|
|
debug('Mongoose default connection disconnected through app termination');
|
|
process.exit(0);
|
|
});
|
|
} else {
|
|
process.exit(0);
|
|
}
|
|
}
|
|
// If the Node process ends, close the Mongoose connection
|
|
process.on('SIGINT', function () { // Windows
|
|
postExit();
|
|
});
|
|
|
|
module.exports = mongoose.connection;
|