一旦更新了特定模型,如何让 keystoneJS 成为 运行 函数?

How do I get keystoneJS to run a function once a specific model has been updated?

一旦我在特定 KeystoneJS 模型中有新的或更新的项目,我想 运行 一个函数。我怎么做?我会添加一个活动吗……已经有活动了吗?我是将它添加到模型中还是其他地方?

KEystoneJS 使用猫鼬。

当您添加新模型时,CRUD(更改、替换、更新、删除)在管理端自然发生(http:///keystone

但是,对于非管理员端,您需要创建路由,并且在路由视图中,您可以使用 mongoose API 来执行此操作。

keystoneJS 中的任何更改,只需重新启动服务器 (nam start) 即可生效

您可以像在任何非 keystone 项目中一样使用 mongoose 中间件。可以使用 .schema 访问 keystone 列表模式,例如

var keystone = require('keystone');
var Types = keystone.Field.Types;


var User = new keystone.List('User');

User.add({
    name: { type: Types.Name, required: true, index: true },
    email: { type: Types.Email, initial: true, required: true, index: true },
});

//do stuff BEFORE the user document is fully saved to the DB
User.schema.pre('save', function(next){
    console.log('SAVING USER:', this);
    next();
});
//do stuff AFTER the user document has been saved to the DB
User.schema.post('save', function(user){
    console.log('USER WAS SAVED:', user);
});


User.defaultColumns = 'name, email';
User.register();

看看 mongoose middleware,因为有一些限制,例如在进行大量更新时,中间件将 不会 运行,这是设计使然在 mongoose 中,与 keystone 无关。