diff options
Diffstat (limited to 'node_modules/mongoose/docs/plugins.md')
| -rw-r--r-- | node_modules/mongoose/docs/plugins.md | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/node_modules/mongoose/docs/plugins.md b/node_modules/mongoose/docs/plugins.md new file mode 100644 index 0000000..2d22a55 --- /dev/null +++ b/node_modules/mongoose/docs/plugins.md @@ -0,0 +1,38 @@ +Plugins +==================== + +`Schema`s are pluggable, that is, they allow for applying pre-packaged +capabilities to extend their functionality. + +Suppose that we have several collections in our database and want +to add last-modified functionality to each one. With plugins this +is easy. Just create a plugin once and apply it to each `Schema`: + + // lastMod.js + module.exports = exports = function lastModifiedPlugin (schema, options) { + schema.add({ lastMod: Date }) + + schema.pre('save', function (next) { + this.lastMod = new Date + next() + }) + + if (options && options.index) { + schema.path('lastMod').index(options.index) + } + } + + // in your schema files + var lastMod = require('./lastMod'); + + var Game = new Schema({ ... }); + + Game.plugin(lastMod); + + var Player = new Schema({ ... }); + + Player.plugin(lastMod, { index: true }); + +In the example above we added last-modified functionality to both the Game +and Player schemas. We also took advantage of options passing supported by +the `plugin()` method to dynamically define an index on the Player. |
