Plugins插件
Schemas are pluggable, that is, they allow for applying pre-packaged capabilities to extend their functionality. 模式是可插入的,也就是说,它们允许应用预打包的功能来扩展其功能。This is a very powerful feature.这是一个非常强大的功能。
- Example
Global Plugins全局插件Apply Plugins Before Compiling Models编译模型前应用插件Officially Supported Plugins官方支持的插件
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. loadedAt
属性。Just create a plugin once and apply it to each 只需创建一次插件并将其应用于每个Schema
:Schema
:
// loadedAt.js
module.exports = function loadedAtPlugin(schema, options) {
schema.virtual('loadedAt').
get(function() { return this._loadedAt; }).
set(function(v) { this._loadedAt = v; });
schema.post(['find', 'findOne'], function(docs) {
if (!Array.isArray(docs)) {
docs = [docs];
}
const now = new Date();
for (const doc of docs) {
doc.loadedAt = now;
}
});
};
// game-schema.js
const loadedAtPlugin = require('./loadedAt');
const gameSchema = new Schema({ /* ... */ });
gameSchema.plugin(loadedAtPlugin);
// player-schema.js
const loadedAtPlugin = require('./loadedAt');
const playerSchema = new Schema({ /* ... */ });
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 mongoose singleton有一个.plugin()
function that registers a plugin for every schema. .plugin()
函数,它为每个模式注册一个插件。For example:例如:
const mongoose = require('mongoose');
mongoose.plugin(require('./loadedAt'));
const gameSchema = new Schema({ /* ... */ });
const playerSchema = new Schema({ /* ... */ });
// `loadedAtPlugin` gets attached to both schemas
const Game = mongoose.model('Game', gameSchema);
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.
// loadedAt.js
module.exports = function loadedAtPlugin(schema, options) {
schema.virtual('loadedAt').
get(function() { return this._loadedAt; }).
set(function(v) { this._loadedAt = v; });
schema.post(['find', 'findOne'], function(docs) {
if (!Array.isArray(docs)) {
docs = [docs];
}
const now = new Date();
for (const doc of docs) {
doc.loadedAt = now;
}
});
};
// game-schema.js
const loadedAtPlugin = require('./loadedAt');
const gameSchema = new Schema({ /* ... */ });
const Game = mongoose.model('Game', gameSchema);
// `find()` and `findOne()` hooks from `loadedAtPlugin()` won't get applied
// because `mongoose.model()` was already called!
gameSchema.plugin(loadedAtPlugin);
Officially Supported Plugins官方支持的插件
The Mongoose team maintains several plugins that add cool new features to Mongoose. Mongoose团队维护了几个插件,为Mongoose添加了很酷的新功能。Here's a couple:这是一对情侣:
- mongoose-autopopulate
: Always populate() certain fields in your Mongoose schemas.:始populate()
Mongoose模式中的某些字段。 - mongoose-lean-virtuals
: Attach virtuals to the results of Mongoose queries when using .lean().:使用.lean()时,将virtuals附加到Mongoose查询的结果。 - mongoose-cast-aggregation
You can find a full list of officially supported plugins on Mongoose's plugins search site.你可以在Mongoose的插件搜索网站上找到官方支持的插件的完整列表。
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. 您不仅可以在自己的项目中重用模式功能,还可以从Mongoose社区中获益。Any plugin published to npm and with 'mongoose' as an npm keyword will show up on our search results page.