Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yamadapc/mongoose-bluebird-utils
A utility library for using mongoose with bluebird promises
https://github.com/yamadapc/mongoose-bluebird-utils
Last synced: 3 months ago
JSON representation
A utility library for using mongoose with bluebird promises
- Host: GitHub
- URL: https://github.com/yamadapc/mongoose-bluebird-utils
- Owner: yamadapc
- License: mit
- Created: 2013-11-27T20:30:47.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2016-03-23T15:01:58.000Z (almost 9 years ago)
- Last Synced: 2024-10-11T22:53:46.931Z (4 months ago)
- Language: JavaScript
- Homepage:
- Size: 17.6 KB
- Stars: 19
- Watchers: 4
- Forks: 1
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
mongoose-bluebird-utils [![Build Status](https://travis-ci.org/yamadapc/mongoose-bluebird-utils.png)](https://travis-ci.org/yamadapc/mongoose-bluebird-utils)
==================================================**DEPRECATED**
This module is deprecated, since it's a better and faster approach now to just
promisify your models. This can be achieved with:
```javascript
var Promise = require('bluebird');
var User = mongoose.model('User');
Promise.promisifyAll(User);
Promise.promisifyAll(User.prototype);// Example:
var user = new User();
user.saveAsync()
// We need to use `spread` because `user.save` yields an array
.spread(function(user) {
});
```The `promisifyAll` bluebird function will add a `Async` posfixed promisified
method for all the methods in an object. This will be faster and more consistent
than using this module.
- - -This is basically just a bunch of simple functions that serve as tiny sugars
very often.But, I find it kind of useful and don't want to flood a helper module into
*every* project just to keep things tidy, so it's here.Instalation
--------------------------------------------------```npm install mongoose-bluebird-utils```
```javascript
// [...]var mpUtils = require('mongoose-bluebird-utils');
// [...]
```Examples
--------------------------------------------------**Before:**
*Don't ever do this. This is terrible*```javascript
var Promise = require('bluebird'),
mongoose = require('mongoose'),
User = mongoose.model('User');function setNameToJohn (old_name) {
// note a mpromise promise is being returned, which may be harmful
return User.findOne({ name: old_name })
.then(function(user) {
if(!user){
var err = new Error('User not found');
err.status = 404; // I do this to bind HTTP response codes into errors
throw err;
}
user.name = 'John';
return Promise.promisify(user.save, user)();
});
}
```**After:**
```javascript
var mongoose = require('mongoose'),
User = mongoose.model('User'),
mpUtils = require('mongoose-bluebird-utils');function setNameToJohn (old_name) {
return mpUtils.findOneP(User, { name: old_name }, 'User not found')
.then(function(user) {
user.name = 'John';
return mpUtils.saveP(user);
});
}
```## Donations
Would you like to buy me a beer? Send bitcoin to 3JjxJydvoJjTrhLL86LGMc8cNB16pTAF3y