Last active
April 5, 2020 17:46
-
-
Save vladi-strilets/c00c997657df1601554ff83b8434bb50 to your computer and use it in GitHub Desktop.
mongodb client handler node.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
// db.js | |
const MongoClient = require("mongodb").MongoClient; | |
const url = "YOUR_URL"; | |
var client; | |
const getClient = async () => { | |
if (client && client.isConnected()) { | |
console.log("DB CLIENT ALREADY CONNECTED"); | |
} else | |
try { | |
client = await MongoClient.connect(url, { | |
useNewUrlParser: true, | |
useUnifiedTopology: true, | |
}); | |
console.log("DB CLIENT RECONNECTED"); | |
} catch (e) { | |
throw e; | |
} | |
return client; | |
}; | |
module.exports = getClient; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment