Skip to content

Instantly share code, notes, and snippets.

@elianbarci
Created June 21, 2022 18:40
Show Gist options
  • Save elianbarci/c8d6fcd505e0c0dd4ca625c63dd93319 to your computer and use it in GitHub Desktop.
Save elianbarci/c8d6fcd505e0c0dd4ca625c63dd93319 to your computer and use it in GitHub Desktop.
MongoConnection.js
const {
MongoClient
} = require("mongodb");
const uri =
`mongodb://${encodeURIComponent(process.env.MONGO_USER)}:${encodeURIComponent(process.env.MONGO_PASSWORD)}@${process.env.MONGO_HOST}?retryWrites=true&writeConcern=majority`;
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
monitorCommands: true
});
function getMongoConnection(dbName, collectionName) {
return client.db(dbName).collection(collectionName)
}
function killAllConnections() {
client.close();
}
module.exports = {
getMongoConnection,
killAllConnections
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment