I'd like to use the MongoDB native JS driver with bluebird promises. How can I use Promise.promisifyAll()
on this library?
The 2.0 branch documentation contains a better promisification guide https://github.com/petkaantonov/bluebird/blob/master/API.md#promisification
It actually has mongodb example which is much simpler:
var Promise = require("bluebird");
var MongoDB = require("mongodb");
Promise.promisifyAll(MongoDB);
Cursor.prototype
(which the above code does) not Cursor
–
Incorruption Cursor.prototype
. –
Unbidden var MongoDB = Promise.promisifyAll(require("mongodb"))
now? –
Amperehour require("mongodb")
. All cursors then have toArrayAsync method, for example. –
Wolgast 1.4.*
to the newer 2.*
versions and currently this approach which used to work no longer seems to work, the MongoClient
object seems to be missing the connectAsync
method etc. –
Sevastopol When using Promise.promisifyAll()
, it helps to identify a target prototype if your target object must be instantiated. In case of the MongoDB JS driver, the standard pattern is:
- Get a
Db
object, using eitherMongoClient
static method or theDb
constructor - Call
Db#collection()
to get aCollection
object.
So, borrowing from https://mcmap.net/q/538287/-mongodb-promise-gets-returned-too-early-closed, you can:
var Promise = require('bluebird');
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var Collection = mongodb.Collection;
Promise.promisifyAll(Collection.prototype);
Promise.promisifyAll(MongoClient);
Now you can:
var client = MongoClient.connectAsync('mongodb://localhost:27017/test')
.then(function(db) {
return db.collection("myCollection").findOneAsync({ id: 'someId' })
})
.then(function(item) {
// Use `item`
})
.catch(function(err) {
// An error occurred
});
This gets you pretty far, except it'll also help to make sure the Cursor
objects returned by Collection#find()
are also promisified. In the MongoDB JS driver, the cursor returned by Collection#find()
is not built from a prototype. So, you can wrap the method and promisify the cursor each time. This isn't necessary if you don't use cursors, or don't want to incur the overhead. Here's one approach:
Collection.prototype._find = Collection.prototype.find;
Collection.prototype.find = function() {
var cursor = this._find.apply(this, arguments);
cursor.toArrayAsync = Promise.promisify(cursor.toArray, cursor);
cursor.countAsync = Promise.promisify(cursor.count, cursor);
return cursor;
}
Cursor.prototype
? –
Amperehour Async
prefix in the "Now you can" part? –
Amperehour Async
after I closed the tab out. Just fixed that. Got the impression from the referenced answer that the cursor isn't built from a prototype. Testing that out now. –
Unbidden MongoClient
. –
Unbidden Cursor
construction because of the aforementioned referenced answer. –
Unbidden Cursor
has a prototype, that prototype isn't used in constructing the Cursor
that's ultimately passed by Collection#find()
. Have a look at github.com/mongodb/node-mongodb-native/blob/1.4/lib/mongodb/…, which is called via github.com/mongodb/node-mongodb-native/blob/1.4/lib/mongodb/…. All of that said, I don't think Promisification is all that expensive, relative to, you know, disk and network IO. –
Unbidden I know this has been answered several times, but I wanted to add in a little more information regarding this topic. Per Bluebird's own documentation, you should use the 'using' for cleaning up connections and prevent memory leaks. Resource Management in Bluebird
I looked all over the place for how to do this correctly and information was scarce so I thought I'd share what I found after much trial and error. The data I used below (restaurants) came from the MongoDB sample data. You can get that here: MongoDB Import Data
// Using dotenv for environment / connection information
require('dotenv').load();
var Promise = require('bluebird'),
mongodb = Promise.promisifyAll(require('mongodb'))
using = Promise.using;
function getConnectionAsync(){
// process.env.MongoDbUrl stored in my .env file using the require above
return mongodb.MongoClient.connectAsync(process.env.MongoDbUrl)
// .disposer is what handles cleaning up the connection
.disposer(function(connection){
connection.close();
});
}
// The two methods below retrieve the same data and output the same data
// but the difference is the first one does as much as it can asynchronously
// while the 2nd one uses the blocking versions of each
// NOTE: using limitAsync seems to go away to never-never land and never come back!
// Everything is done asynchronously here with promises
using(
getConnectionAsync(),
function(connection) {
// Because we used promisifyAll(), most (if not all) of the
// methods in what was promisified now have an Async sibling
// collection : collectionAsync
// find : findAsync
// etc.
return connection.collectionAsync('restaurants')
.then(function(collection){
return collection.findAsync()
})
.then(function(data){
return data.limit(10).toArrayAsync();
});
}
// Before this ".then" is called, the using statement will now call the
// .dispose() that was set up in the getConnectionAsync method
).then(
function(data){
console.log("end data", data);
}
);
// Here, only the connection is asynchronous - the rest are blocking processes
using(
getConnectionAsync(),
function(connection) {
// Here because I'm not using any of the Async functions, these should
// all be blocking requests unlike the promisified versions above
return connection.collection('restaurants').find().limit(10).toArray();
}
).then(
function(data){
console.log("end data", data);
}
);
I hope this helps someone else out who wanted to do things by the Bluebird book.
Version 1.4.9 of mongodb
should now be easily promisifiable as such:
Promise.promisifyAll(mongo.Cursor.prototype);
See https://github.com/mongodb/node-mongodb-native/pull/1201 for more details.
We have been using the following driver in production for a while now. Its essentially a promise wrapper over the native node.js driver. It also adds some additional helper functions.
poseidon-mongo
- https://github.com/playlyfe/poseidon-mongo
© 2022 - 2024 — McMap. All rights reserved.