index.js 715 B

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