mongoose-id-extractor-plugin

0.0.2 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i mongoose-id-extractor-plugin

Weekly Downloads

0

Version

0.0.2

License

MIT License

Last publish

Collaborators

  • kjirou