Last active
June 6, 2023 06:44
-
-
Save infysumanta/ebf7b64c6d7b41cc5989952d56962746 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require("mongoose"); | |
const connect = async () => { | |
console.log(process.env.ENV); | |
try { | |
mongoose.connect(process.env.MONGODB_URI, dbConfig, { | |
useNewUrlParser: true, | |
useUnifiedTopology: true, | |
}); | |
} catch (err) { | |
console.log(err); | |
throw err; | |
} | |
}; | |
const dbConfig = | |
process.env.ENV === "local" | |
? {} | |
: { | |
auth: { | |
username: process.env.MONGO_ROOT_USERNAME, | |
password: process.env.MONGO_ROOT_PASSWORD, | |
}, | |
authSource: "admin", | |
}; | |
mongoose.connection.on("disconnected", () => { | |
console.log("Database not Connected"); | |
}); | |
mongoose.connection.on("connected", () => { | |
console.log("Database Connected"); | |
}); | |
module.exports = connect; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment