Created
June 21, 2022 18:40
-
-
Save elianbarci/c8d6fcd505e0c0dd4ca625c63dd93319 to your computer and use it in GitHub Desktop.
MongoConnection.js
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 { | |
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