pub trait Hookable: Model {
Show 17 methods
// Provided methods
fn before_insert(_ctx: &mut HookContext) -> HookResult<()> { ... }
fn after_insert(
_ctx: &HookContext,
_id: &Self::PrimaryKey,
) -> HookResult<()> { ... }
fn before_update(
_ctx: &mut HookContext,
_id: &Self::PrimaryKey,
) -> HookResult<()> { ... }
fn after_update(
_ctx: &HookContext,
_id: &Self::PrimaryKey,
) -> HookResult<()> { ... }
fn before_delete(
_ctx: &mut HookContext,
_id: &Self::PrimaryKey,
) -> HookResult<()> { ... }
fn after_delete(
_ctx: &HookContext,
_id: &Self::PrimaryKey,
) -> HookResult<()> { ... }
fn before_write(_ctx: &mut HookContext) -> HookResult<()> { ... }
fn after_write(_ctx: &HookContext, _id: &Self::PrimaryKey) -> HookResult<()> { ... }
fn before_save(_ctx: &mut HookContext) -> HookResult<()> { ... }
fn after_save(_ctx: &HookContext, _id: &Self::PrimaryKey) -> HookResult<()> { ... }
fn before_restore(
_ctx: &mut HookContext,
_id: &Self::PrimaryKey,
) -> HookResult<()> { ... }
fn after_restore(
_ctx: &HookContext,
_id: &Self::PrimaryKey,
) -> HookResult<()> { ... }
fn before_find(
_ctx: &mut HookContext,
_id: &Self::PrimaryKey,
) -> HookResult<()> { ... }
fn after_find(_ctx: &HookContext, _id: &Self::PrimaryKey) -> HookResult<()> { ... }
fn before_validate(_ctx: &mut HookContext) -> HookResult<()> { ... }
fn validate(_ctx: &mut HookContext) -> HookResult<()> { ... }
fn after_validate(_ctx: &HookContext) -> HookResult<()> { ... }
}Expand description
可钩选 Model trait
实现 Hookable 的 Model 可以在 insert/update/delete 前后执行自定义逻辑。
默认实现为 no-op,Model 按需 override。
§细粒度事件(v0.2.0+)
在原 6 个 insert/update/delete 钩子之外,新增 6 个通用钩子:
before_write/after_write:任何写入(insert 或 update)前后均触发before_save/after_save:保存前后触发(与 write 等价,命名风格不同)before_restore/after_restore:软删除恢复前后触发
调用方应在执行 INSERT 前依次调用 before_write → before_save → before_insert,
INSERT 完成后依次调用 after_insert → after_save → after_write。
Provided Methods§
Sourcefn before_insert(_ctx: &mut HookContext) -> HookResult<()>
fn before_insert(_ctx: &mut HookContext) -> HookResult<()>
插入前钩子(默认 no-op)
Sourcefn after_insert(_ctx: &HookContext, _id: &Self::PrimaryKey) -> HookResult<()>
fn after_insert(_ctx: &HookContext, _id: &Self::PrimaryKey) -> HookResult<()>
插入后钩子(默认 no-op)
Sourcefn before_update(
_ctx: &mut HookContext,
_id: &Self::PrimaryKey,
) -> HookResult<()>
fn before_update( _ctx: &mut HookContext, _id: &Self::PrimaryKey, ) -> HookResult<()>
更新前钩子(默认 no-op)
Sourcefn after_update(_ctx: &HookContext, _id: &Self::PrimaryKey) -> HookResult<()>
fn after_update(_ctx: &HookContext, _id: &Self::PrimaryKey) -> HookResult<()>
更新后钩子(默认 no-op)
Sourcefn before_delete(
_ctx: &mut HookContext,
_id: &Self::PrimaryKey,
) -> HookResult<()>
fn before_delete( _ctx: &mut HookContext, _id: &Self::PrimaryKey, ) -> HookResult<()>
删除前钩子(默认 no-op)
Sourcefn after_delete(_ctx: &HookContext, _id: &Self::PrimaryKey) -> HookResult<()>
fn after_delete(_ctx: &HookContext, _id: &Self::PrimaryKey) -> HookResult<()>
删除后钩子(默认 no-op)
Sourcefn before_write(_ctx: &mut HookContext) -> HookResult<()>
fn before_write(_ctx: &mut HookContext) -> HookResult<()>
通用写入前钩子:insert 或 update 前均触发(默认 no-op)
适合用于审计日志、统一字段填充(如 updated_at = now())等场景。
Sourcefn after_write(_ctx: &HookContext, _id: &Self::PrimaryKey) -> HookResult<()>
fn after_write(_ctx: &HookContext, _id: &Self::PrimaryKey) -> HookResult<()>
通用写入后钩子:insert 或 update 后均触发(默认 no-op)
Sourcefn before_save(_ctx: &mut HookContext) -> HookResult<()>
fn before_save(_ctx: &mut HookContext) -> HookResult<()>
保存前钩子(与 before_write 等价,命名风格不同,默认 no-op)
Sourcefn after_save(_ctx: &HookContext, _id: &Self::PrimaryKey) -> HookResult<()>
fn after_save(_ctx: &HookContext, _id: &Self::PrimaryKey) -> HookResult<()>
保存后钩子(与 after_write 等价,命名风格不同,默认 no-op)
Sourcefn before_restore(
_ctx: &mut HookContext,
_id: &Self::PrimaryKey,
) -> HookResult<()>
fn before_restore( _ctx: &mut HookContext, _id: &Self::PrimaryKey, ) -> HookResult<()>
软删除恢复前钩子(默认 no-op)
当软删除行被恢复(UPDATE deleted_at = NULL)时触发。
Sourcefn after_restore(_ctx: &HookContext, _id: &Self::PrimaryKey) -> HookResult<()>
fn after_restore(_ctx: &HookContext, _id: &Self::PrimaryKey) -> HookResult<()>
软删除恢复后钩子(默认 no-op)
Sourcefn before_find(_ctx: &mut HookContext, _id: &Self::PrimaryKey) -> HookResult<()>
fn before_find(_ctx: &mut HookContext, _id: &Self::PrimaryKey) -> HookResult<()>
单行查询前钩子(默认 no-op)
在执行 SELECT * FROM ... WHERE pk = ? 前触发。
适合用于查询缓存预热、查询审计日志、强制查询条件注入等。
Sourcefn after_find(_ctx: &HookContext, _id: &Self::PrimaryKey) -> HookResult<()>
fn after_find(_ctx: &HookContext, _id: &Self::PrimaryKey) -> HookResult<()>
单行查询后钩子(默认 no-op)
在 SELECT * FROM ... WHERE pk = ? 返回结果后触发。
适合用于查询结果缓存填充、行级权限校验等。
Sourcefn before_validate(_ctx: &mut HookContext) -> HookResult<()>
fn before_validate(_ctx: &mut HookContext) -> HookResult<()>
数据验证前钩子(默认 no-op)
在写入前的业务规则校验之前触发,调用顺序:
before_write → before_save → before_validate → validate → after_validate → before_insert
适合用于字段非空校验、字段格式校验、跨字段一致性校验等。
失败时返回 Err(DbError::Validation(...)),会短路后续 before_insert。
Sourcefn validate(_ctx: &mut HookContext) -> HookResult<()>
fn validate(_ctx: &mut HookContext) -> HookResult<()>
数据验证逻辑(默认 no-op)
在 before_validate 之后、after_validate 之前调用。
Model 可按需 override 此方法以实现实际的业务规则校验。
失败时返回 Err(DbError::Validation(...)),会短路后续 after_validate 与 before_insert。
Sourcefn after_validate(_ctx: &HookContext) -> HookResult<()>
fn after_validate(_ctx: &HookContext) -> HookResult<()>
数据验证后钩子(默认 no-op)
验证成功后触发,可用于清理临时状态、记录验证日志。
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".