Skip to content

Instantly share code, notes, and snippets.

@kunxin-chor
Created December 7, 2020 05:23
Show Gist options
  • Save kunxin-chor/fcd96a51fccd5ca4fe93b7b2676865e8 to your computer and use it in GitHub Desktop.
Save kunxin-chor/fcd96a51fccd5ca4fe93b7b2676865e8 to your computer and use it in GitHub Desktop.
const MongoClient = require("mongodb").MongoClient;
// global variable
let _db;
async function connect(url, dbname)
{
let client = await MongoClient.connect(url, {
useUnifiedTopology:true
});
_db = client.db(dbname)
console.log("Database connected");
}
function getDB() {
return _db;
}
module.exports = {
connect, getDB
}
/*
In JavaScript ES6, the export will be done as:
export { connect, getDB }
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment