From 686106d544ecc3b6ffd4db2b665d3bc879a58d8c Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 24 Sep 2012 16:22:07 -0400 Subject: ok --- node_modules/mongoose/docs/plugins.md | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 node_modules/mongoose/docs/plugins.md (limited to 'node_modules/mongoose/docs/plugins.md') 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. -- cgit v1.2.3-70-g09d2