Skip to content

Instantly share code, notes, and snippets.

@tvalletta
Last active March 14, 2018 23:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tvalletta/8ba9bce66fc8b1f3b0b8d2ab31508680 to your computer and use it in GitHub Desktop.
Save tvalletta/8ba9bce66fc8b1f3b0b8d2ab31508680 to your computer and use it in GitHub Desktop.
Mongo connection using bluebird promisify and connection disposer
'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