Skip to content

Instantly share code, notes, and snippets.

@Sanjay007
Last active August 19, 2021 14:19
Show Gist options
  • Save Sanjay007/55e0b35c8909beeaa72b3a3332eae1f1 to your computer and use it in GitHub Desktop.
Save Sanjay007/55e0b35c8909beeaa72b3a3332eae1f1 to your computer and use it in GitHub Desktop.
const mongoose = require("mongoose");
const DB_URL = process.env.MONGO_URI;
module.exports = () => {
const connect = () => {
mongoose.Promise = global.Promise;
mongoose.connect(
DB_URL,
{
keepAlive: true,
useNewUrlParser: true,
useUnifiedTopology: true,
},
(err) => {
let dbStatus = "";
if (err) {
dbStatus = `* Error connecting to DB: ${err}\n****************************\n`;
}
dbStatus = `* DB Connection: OK\n****************************\n`;
if (process.env.NODE_ENV !== "test") {
// Prints initialization
console.log("****************************");
console.log("* Starting Server");
console.log(`* Port: ${process.env.PORT || 3000}`);
console.log(`* NODE_ENV: ${process.env.NODE_ENV}`);
console.log(`* Database: MongoDB`);
console.log(dbStatus);
}
}
);
mongoose.set("useCreateIndex", true);
mongoose.set("useFindAndModify", false);
};
connect();
mongoose.connection.on("error", console.log);
mongoose.connection.on("disconnected", connect);
//loadModels
};
@Sanjay007
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment