UNPKG

mongoose

Version:

Mongoose MongoDB ODM

33 lines (27 loc) 5.17 kB
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Mongoose Plugins v3.0.1</title><link href="http://fonts.googleapis.com/css?family=Anonymous+Pro:400,700|Droid+Sans+Mono|Open+Sans:400,700|Linden+Hill|Quattrocento:400,700|News+Cycle:400,700|Antic+Slab|Cabin+Condensed:400,700" rel="stylesheet" type="text/css"><link href="/docs/css/default.css" rel="stylesheet" type="text/css"><link href="/docs/css/guide.css" rel="stylesheet" type="text/css"></head><body><a id="forkbanner" href="http://github.com/learnboost/mongoose"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png" alt="Fork me on GitHub"></a><div id="links"><div id="header"><h1><a href="../index.html"><div class="mongoose">Mongoose</div></a></h1></div><ul><li class="home"><a href="../index.html">home</a></li><li class="plugins"><a href="http://plugins.mongoosejs.com">plugins</a></li><li class="changelog"><a href="http://github.com/learnboost/mongoose/tree/master/History.md">change log</a></li><li class="support"><a href="../index.html#support">support</a></li><li class="fork"><a href="http://github.com/learnboost/mongoose">fork</a></li><li class="guide"><a href="./guide.html">guide</a><ul><li class="double"><a href="./guide.html">schemas</a><ul><li class="schematypes"><a href="./schematypes.html"><span>schema</span>types</a></li></ul></li><li class="double"><a href="./documents.html">documents</a><ul><li class="subdocs"><a href="./subdocs.html">sub docs</a></li></ul></li><li><a href="./models.html">models</a></li><li><a href="./queries.html">queries</a></li><li><a href="./plugins.html">plugins</a></li><li><a href="./middleware.html">middleware</a></li><li><a href="./validation.html">validation</a></li><li><a href="./populate.html">populate</a></li><li><a href="./migration.html">migrating from 2.x</a></li><li><a href="./contributing.html">contributing</a></li></ul></li><li class="api"><a href="./api.html">api docs</a></li><li class="quickstart"><a href="./index.html">quick start</a></li><li class="contrib"><a href="http://github.com/learnboost/mongoose/contributors">contributors</a></li><li class="prior"><a href="./prior.html">prior releases</a></li></ul></div><div id="content"><div class="module"><h2>Plugins</h2><p>Schemas are pluggable, that is, they allow for applying pre-packaged capabilities to extend their functionality. This is a very powerful feature.</p> <p>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 <code>Schema</code>:</p><pre><code class="javascript"><span class="comment">// lastMod.js</span> module.exports = exports = <span class="function"><span class="keyword">function</span> <span class="title">lastModifiedPlugin</span> <span class="params">(schema, options)</span> {</span> schema.add({ lastMod: Date }) schema.pre(<span class="string">'save'</span>, <span class="function"><span class="keyword">function</span> <span class="params">(next)</span> {</span> <span class="keyword">this</span>.lastMod = <span class="keyword">new</span> Date next() }) <span class="keyword">if</span> (options &amp;&amp; options.index) { schema.path(<span class="string">'lastMod'</span>).index(options.index) } } <span class="comment">// game-schema.js</span> <span class="keyword">var</span> lastMod = require(<span class="string">'./lastMod'</span>); <span class="keyword">var</span> Game = <span class="keyword">new</span> Schema({ ... }); Game.plugin(lastMod, { index: <span class="literal">true</span> }); <span class="comment">// player-schema.js</span> <span class="keyword">var</span> lastMod = require(<span class="string">'./lastMod'</span>); <span class="keyword">var</span> Player = <span class="keyword">new</span> Schema({ ... }); Player.plugin(lastMod);</code></pre><p>We just added last-modified behavior to both our <code>Game</code> and <code>Player</code> schemas and declared an index on the <code>lastMod</code> path of our Games to boot. Not bad for a few lines of code.</p><h3>Community!</h3><p>Not only can you re-use schema functionality in your own projects but you also reap the benefits of the Mongoose community as well. Any plugin published to <a href="https://npmjs.org/">npm</a> and <a href="https://npmjs.org/doc/tag.html">tagged</a> with <code>mongoose</code> will show up on our <a href="http://plugins.mongoosejs.com">search results</a> page.</p></div></div><script>document.body.className = 'load';</script><script>var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-1122274-9']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();</script></body></html>