yamongoose-bird
v0.1.0
Published
blue bird support for mongoose
Downloads
1
Readme
yamongoose-bird
blue bird support for mongoose.
based on mongoose-q, mongoose-bird.
What is it?
mongoose-bird uses the bluebird's Promise.promisifyAll function which will add a Async function for each function on mongoose :
- mongoose.Model
- mongoose.Model.prototype
- mongoose.Query.prototype
So all mongoose-bird does it to call:
Promise.promisifyAll(mongoose.Model);
Promise.promisifyAll(mongoose.Model.prototype);
Promise.promisifyAll(mongoose.Query.prototype);
Promise.promisifyAll(mongoose.Document.prototype);
usage
var mongoose = require('mongoose-bird')(require('mongoose'));
// verbose way: mongooseQ is unused
var mongoose = require('mongoose'),
mongooseBird = require('mongoose-bird')(mongoose)
// shortest way: mongoose will be loaded by mongoose-bird
var mongoose = require('mongoose-bird')();
- use Async
model
statistics functions:
SomeModel.findByIdAsync(....blahblah...)
.then(function (result) { ... })
.catch(function (err) { ... });
- use Async
model
functions:
var someModel = new SomeModel(...);
someModel.populateAsync()
.then(function (result) { ... })
.catch(function (err) { ... });
- use Async
query
methods:
SomeModel.find(...).where(...).skip(...).limit(...).sort(...).populate(...)
.execAsync() // no 'Async' suffix for model statics except for execAsync()
.then(function (result) { ... })
.catch(function (err) { ... });
- to use Async with
spread
:
var mongoose = require('mongoose-bird')(require('mongoose'));
SomeModel.updateAsync(...)
.spread(function (affectedRows, raw) { ... })
.catch(function (err) { ... });
...
var model = new SomeModel();
...
model.saveAsync()
.spread(function (savedDoc, affectedRows) { ... })
.catch(function (err) { ... });
...
- to define custom statics/instance methods using Async
SomeSchema.statics.findByName = function (name) {
return this.findAsync({name: name}); // NOTE: returns Promise object.
};
...
var SomeModel = mongoose.model('Some', SomeSchema);
SomeModel.findByName('foo').then(function(result) {
console.log(result);
});
NOTE: this is not a feature of mongoose-bird
That's all folks!