0.0.2 • Published 10 years ago

mongoose-id-extractor-plugin v0.0.2

Weekly downloads
1
License
MIT License
Repository
github
Last release
10 years ago

mongoose-id-extractor-plugin Build Status

Add {path}_id virtual-paths as mongoose-plugin that is able to extract _id always.

Installation

npm install mongoose-id-extractor-plugin

Examples

var mongoose = require('mongoose');
var idExtractor = require('mongoose-id-extractor-plugin');

var commentSchema = new mongoose.Schema({
  article: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Article'
  }
});

// Apply as mongoose-plugin
commentSchema.plugin(idExtractor, { refs:['article'] });

var Comment = mongoose.model('Comment', commentSchema);

var comment = new Comment({ article:ObjectId() });

// Returns a ObjectId regardless of whether performed population.
comment.article_id;

suffix option:

schema.plugin(idExtractor, { refs:['foo'], suffix:'_object_id' });

doc.foo_object_id;  // Change default "_id" to "_object_id"

map option:

schema.plugin(idExtractor, {
  map: {
    from_path: 'to_path'
  }
});

doc.to_path;  // _id of from_path