https://github.com/yamadapc/mongoose-ref-promises
A mongoose plugin which adds promise-based ref population virtuals to your Models
https://github.com/yamadapc/mongoose-ref-promises
Last synced: 4 months ago
JSON representation
A mongoose plugin which adds promise-based ref population virtuals to your Models
- Host: GitHub
- URL: https://github.com/yamadapc/mongoose-ref-promises
- Owner: yamadapc
- License: mit
- Created: 2013-12-03T18:39:09.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2016-04-05T02:23:14.000Z (over 9 years ago)
- Last Synced: 2025-03-18T15:15:58.713Z (4 months ago)
- Language: JavaScript
- Homepage:
- Size: 15.6 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
mongoose-ref-promises
=====================
[](http://travis-ci.org/yamadapc/mongoose-ref-promises)
[](https://codeclimate.com/github/yamadapc/mongoose-ref-promises)
[](https://coveralls.io/r/yamadapc/mongoose-ref-promises)
[](https://david-dm.org/yamadapc/mongoose-ref-promises)
[](https://david-dm.org/yamadapc/mongoose-ref-promises#info=devDependencies)
[](https://github.com/igrigorik/ga-beacon)
- - -
A mongoose plugin which adds promise-based population virtual properties to your
Models## Installation
Install the module with: `npm install mongoose-ref-promises`Use it by adding it as a plugin to any mongoose.Schema
```javascript
// [...]
var refPromises = require('mongoose-ref-promises');
UserSchema.plugin(refPromises);
// [...]
```## Examples
The plugin adds virtual properties to each 'ref' property in your schema.
Say we have this setup.
```javascript
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;var refPromises = require('mongoose-ref-promises');
var UserSchema = new Schema({
friends: [{ type: ObjectId, ref: 'User' }],
bff: { type: ObjectId, ref: 'User' }
});
UserSchema.plugin(refPromises);var User = mongoose.model('User', UserSchema);
```We can than access the virtual properties ```friendsP``` and ```bffP``` of any
'User' document.*i.e.*
```javascript
// [...]
user.friendsP
.then(function(friends) {
// [...]
});
```or
```javascript
// [...]
user.bffP
.then(function(best_friend_forever) {
// [...]
});
```## License
Copyright (c) 2014 Pedro Yamada. Licensed under the MIT license.## Donations
Would you like to buy me a beer? Send bitcoin to 3JjxJydvoJjTrhLL86LGMc8cNB16pTAF3y