// node.js: 10.15.3
// express: 4.16.4
// mongoose: 5.9.6
const mongoose = require('mongoose');
let isFirstConnected = false;
mongoose.Promise = global.Promise;
function connectDB() {
mongoose.connect(process.env.DATABASE, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
}
mongoose.connection.on('connected', () => {
console.log(
`Mongoose default connection open ${process.env.DATABASE}`,
);
isFirstConnected = true;
});
mongoose.connection.on('error', err => {
console.log('Mongoose default connection error: ' + err);
if (!isFirstConnected) {
setTimeout(connectDB, 1000);
}
});
mongoose.connection.on('disconnected', () => {
console.log('Mongoose default connection disconnected');
});
connectDB();