Can I specify a lifecycle callback that fires for all waterline models in sails.js

柔情痞子 提交于 2019-12-12 19:41:15

问题


I'm trying to add some auditing capability to my sails.js app. The afterUpdate lifecycle callback will give me the proper place to do what I need, but I'm hoping to avoid modifying every model to reference my new code.

Is there a way to specify an afterUpdate callback that will fire on all models?


回答1:


Put your required overrides into config/models.js

module.exports.models = {

  attributes: {},

  afterUpdate: function (valuesToUpdate, cb) {
    // add your logic here...
    console.log('yep, updated');
    cb();
  },
}

The idea is that then all your models obtain this implementation of the method by default. You can also add shared attributes, as given above (currently empty object).



来源:https://stackoverflow.com/questions/34666236/can-i-specify-a-lifecycle-callback-that-fires-for-all-waterline-models-in-sails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!