Last active
March 14, 2018 23:42
-
-
Save tvalletta/8ba9bce66fc8b1f3b0b8d2ab31508680 to your computer and use it in GitHub Desktop.
Mongo connection using bluebird promisify and connection 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
'use strict'; | |
const Promise = require('bluebird'); | |
const mongoClient = require('mongodb').MongoClient; | |
const url = 'mongodb://localhost:27017/test'; | |
Promise.using(getMongoConnection(url), conn => { | |
return conn.collection('restaurants') | |
.find().limit(10).toArray() | |
.then(out => console.log(out)); | |
}); | |
function getMongoConnection(url) { | |
return mongoClient.connect(url, { promiseLibrary: Promise }) | |
.disposer(conn => conn.close()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment