diff options
| author | Jules Laplace <jules@okfoc.us> | 2012-09-24 16:22:07 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2012-09-24 16:22:07 -0400 |
| commit | 686106d544ecc3b6ffd4db2b665d3bc879a58d8c (patch) | |
| tree | a5b5e50237cef70e12f0745371896e96f5f6d578 /node_modules/mongoose/docs/plugins.md | |
ok
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. |
