index.js 742 B

12345678910111213141516171819202122232425262728
  1. /**
  2. * Created by Syed Afzal
  3. */
  4. const mongoose = require("mongoose");
  5. exports.connect = (app) => {
  6. const options = {
  7. useNewUrlParser: true,
  8. autoIndex: false, // Don't build indexes
  9. maxPoolSize: 10, // Maintain up to 10 socket connections
  10. };
  11. const connectWithRetry = () => {
  12. mongoose.Promise = global.Promise;
  13. console.log("MongoDB connection with retry");
  14. mongoose
  15. .connect(process.env.MONGODB_URI, options)
  16. .then(() => {
  17. console.log("MongoDB is connected");
  18. app.emit("ready");
  19. })
  20. .catch((err) => {
  21. console.log("MongoDB connection unsuccessful, retry after 2 seconds.", err);
  22. setTimeout(connectWithRetry, 2000);
  23. });
  24. };
  25. connectWithRetry();
  26. };