第六章 生命周期函数 Hook
有的时候,我们希望在存储之前干点什么,比如把用户的密码进行 Hash 一下。这个时候就需要用到生命周期函数了。
生命周期函数有哪些?有哪些种类?
支持的生命周期函数如下
(1)
beforeBulkCreate(instances, options)
beforeBulkDestroy(options)
beforeBulkUpdate(options)
(2)
beforeValidate(instance, options)
(-)
validate
(3)
afterValidate(instance, options)
- or -
validationFailed(instance, options, error)
(4)
beforeCreate(instance, options)
beforeDestroy(instance, options)
beforeUpdate(instance, options)
beforeSave(instance, options)
beforeUpsert(values, options)
(-)
create
destroy
update
(5)
afterCreate(instance, options)
afterDestroy(instance, options)
afterUpdate(instance, options)
afterSave(instance, options)
afterUpsert(created, options)
(6)
afterBulkCreate(instances, options)
afterBulkDestroy(options)
afterBulkUpdate(options)这些周期函数分为全局的与模型级别的。
全局级别
模型级别
定义生命周期的三种方式
1.options
2.调用对应的方法
3.调用 addHook
命名生命周期函数
取一个叫 notifyUsers 的名字
移除生命周期函数
只有被命名了的生命周期函数才可移除
Last updated