mongoose-ref-promises
v1.0.0
Published
A mongoose plugin which adds promise-based population virtual properties to your Models
Downloads
5
Maintainers
Readme
mongoose-ref-promises
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
// [...]
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.
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.
// [...]
user.friendsP
.then(function(friends) {
// [...]
});
or
// [...]
user.bffP
.then(function(best_friend_forever) {
// [...]
});
License
Copyright (c) 2014 Pedro Yamada. Licensed under the MIT license.