Plugins

Schemas are pluggable, that is, they allow for applying pre-packaged capabilities to extend their functionality. This is a very powerful feature.

Example

Plugins are a tool for reusing logic in multiple schemas. Suppose you have several models in your database and want to add a loadedAt property to each one. Just create a plugin once and apply it to each Schema:

  1. // loadedAt.js
  2. module.exports = function loadedAtPlugin(schema, options) {
  3. schema.virtual('loadedAt').
  4. get(function() { return this._loadedAt; }).
  5. set(function(v) { this._loadedAt = v; });
  6. schema.post(['find', 'findOne'], function(docs) {
  7. if (!Array.isArray(docs)) {
  8. docs = [docs];
  9. }
  10. const now = new Date();
  11. for (const doc of docs) {
  12. doc.loadedAt = now;
  13. }
  14. });
  15. };
  16. // game-schema.js
  17. const loadedAtPlugin = require('./loadedAt');
  18. const gameSchema = new Schema({ ... });
  19. gameSchema.plugin(loadedAtPlugin);
  20. // player-schema.js
  21. const loadedAtPlugin = require('./loadedAt');
  22. const playerSchema = new Schema({ ... });
  23. playerSchema.plugin(loadedAtPlugin);

We just added last-modified behavior to both our Game and Player schemas and declared an index on the lastMod path of our Games to boot. Not bad for a few lines of code.

Global Plugins

Want to register a plugin for all schemas? The mongoose singleton has a .plugin() function that registers a plugin for every schema. For example:

  1. const mongoose = require('mongoose');
  2. mongoose.plugin(require('./loadedAt'));
  3. const gameSchema = new Schema({ ... });
  4. const playerSchema = new Schema({ ... });
  5. // `loadedAtPlugin` gets attached to both schemas
  6. const Game = mongoose.model('Game', gameSchema);
  7. const Player = mongoose.model('Player', playerSchema);

Apply Plugins Before Compiling Models

Because many plugins rely on middleware, you should make sure to apply plugins before you call mongoose.model() or conn.model(). Otherwise, any middleware the plugin registers won’t get applied.

  1. // loadedAt.js
  2. module.exports = function loadedAtPlugin(schema, options) {
  3. schema.virtual('loadedAt').
  4. get(function() { return this._loadedAt; }).
  5. set(function(v) { this._loadedAt = v; });
  6. schema.post(['find', 'findOne'], function(docs) {
  7. if (!Array.isArray(docs)) {
  8. docs = [docs];
  9. }
  10. const now = new Date();
  11. for (const doc of docs) {
  12. doc.loadedAt = now;
  13. }
  14. });
  15. };
  16. // game-schema.js
  17. const loadedAtPlugin = require('./loadedAt');
  18. const gameSchema = new Schema({ ... });
  19. const Game = mongoose.model('Game', gameSchema);
  20. // `find()` and `findOne()` hooks from `loadedAtPlugin()` won't get applied
  21. // because `mongoose.model()` was already called!
  22. gameSchema.plugin(loadedAtPlugin);

Officially Supported Plugins

The Mongoose team maintains several plugins that add cool new features to Mongoose. Here’s a couple:

You can find a full list of officially supported plugins on Mongoose’s plugins search site.

Community!

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 npm and with ‘mongoose’ as an npm keyword will show up on our search results page.