Last active
April 16, 2017 19:16
-
-
Save midnightcodr/f8df708bc701ab4f0fbb2ab72a776d1a to your computer and use it in GitHub Desktop.
Bluebird mongodb disposer
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
// tested with mongodb 2.2.25 | |
const MongoClient=require('mongodb').MongoClient | |
const Promise = require('bluebird') | |
module.exports = (connOpts) => { | |
return MongoClient | |
.connect(connOpts, {promiseLibrary: Promise}) | |
// .then(db => db) /* not necessary */ | |
.disposer(db => { | |
console.log('to disconnect') | |
db.close() | |
}) | |
} | |
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 Promise = require('bluebird') | |
const conn = require('./connect') | |
Promise.using(conn('mongodb://localhost:27017/test'), db => { | |
const col = db.collection('colThing') | |
col.insert({name: 'something', value: 1234}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment