2020-05-11 19:40:39 +00:00
|
|
|
/**
|
|
|
|
* Created by Syed Afzal
|
|
|
|
*/
|
2020-05-12 13:16:03 +00:00
|
|
|
const mongoose = require("mongoose");
|
2020-05-11 19:40:39 +00:00
|
|
|
|
|
|
|
exports.connect = (app) => {
|
2020-05-12 13:16:03 +00:00
|
|
|
const options = {
|
|
|
|
useNewUrlParser: true,
|
|
|
|
autoIndex: false, // Don't build indexes
|
2021-11-08 10:41:35 +00:00
|
|
|
maxPoolSize: 10, // Maintain up to 10 socket connections
|
2020-05-12 13:16:03 +00:00
|
|
|
};
|
2020-05-11 19:40:39 +00:00
|
|
|
|
2020-05-12 13:16:03 +00:00
|
|
|
const connectWithRetry = () => {
|
|
|
|
mongoose.Promise = global.Promise;
|
|
|
|
console.log("MongoDB connection with retry");
|
|
|
|
mongoose
|
|
|
|
.connect(process.env.MONGODB_URI, options)
|
|
|
|
.then(() => {
|
|
|
|
console.log("MongoDB is connected");
|
|
|
|
app.emit("ready");
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2021-11-08 10:41:35 +00:00
|
|
|
console.log("MongoDB connection unsuccessful, retry after 2 seconds.", err);
|
2020-05-12 13:16:03 +00:00
|
|
|
setTimeout(connectWithRetry, 2000);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
connectWithRetry();
|
2020-05-11 19:40:39 +00:00
|
|
|
};
|