lb-promisify
v0.0.4
Published
Add promises to Loopback models
Downloads
5
Maintainers
Readme
lb-promisify
lb-promisify
is the Loopback addon which adds promises to PersistedModel methods by default, but can be extended.
1. Installation
npm install lb-promisify --save
2. Configuration
server/server.js.json
...
var promisify = require('lb-promisify')
...
boot(app, __dirname);
promisify(app);
...
3. Basic use
var User = app.models.User,
Post = app.models.Post;
User
.findById('user-id')
.then(function(user) {
return Post.find({ where: { authorId: user.id }});
})
.then(function(posts) {
// do something with posts
})
.catch(function(err) {
console.log(err);
});
4. Advanced use
You can promisify more methods by add definitions. Before:
promisify(app);
you can add something like this
promisify.add('isValid',
true,
function (deferred) {
return function (valid) {
if(valid) {
deferred.resolve();
}
else {
deferred.reject();
}
}
});