Last active
July 25, 2018 09:35
-
-
Save Amberlamps/a257b040b54d505770b3dca2b8b54e48 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 ClientWrapper = require('./mongo-client-wrapper.js'); | |
ClientWrapper("url/to/mongodb").then((db) => { | |
db.insert(); | |
}); |
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').MongoClient; | |
const ClientWrapper = () => { | |
let localDb; | |
return (url) => new Promise((resolve, reject) => { | |
if (localDb) { | |
return resolve(localDb); | |
} | |
MongoClient.connect(url, (err, db) => { | |
if (err) { | |
return reject(err); | |
} | |
localDb = db; | |
process.on('exit', () => localDb.close()); | |
return resolve(localDb); | |
}); | |
}); | |
}; | |
module.exports = ClientWrapper(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment