I'm aware that the Node driver for Mongo can be promisified using external libraries. I was curious to see if ES6 promises could be used with MongoClient.connect
, so I tried this (using Babel 5.8.23 to transpile):
import MongoClient from 'mongodb';
function DbConnection({
host = 'localhost',
port = 27017,
database = 'foo'
}) {
return new Promise((resolve, reject) => {
MongoClient.connect(`mongodb://${host}:${port}/${database}`,
(err, db) => {
err ? reject(err) : resolve(db);
});
});
}
DbConnection({}).then(
db => {
let cursor = db.collection('bar').find();
console.log(cursor.count());
},
err => {
console.log(err);
}
);
The output is {Promise <pending>}
. Anything to do with cursors seems to yield a similar result. Is there a way to get around this or am I barking up the wrong tree entirely?
Edit: node version 4.1.0.