mongoose-ref-promises

1.0.0 • Public • Published

mongoose-ref-promises

Build Status Code Climate Coverage Status Dependency Status devDependency Status Analytics


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.

Package Sidebar

Install

npm i mongoose-ref-promises

Weekly Downloads

0

Version

1.0.0

License

none

Last publish

Collaborators

  • yamadapc