Skip to main content

Hookable

Trait Hookable 

Source
pub trait Hookable: Model {
Show 17 methods // Provided methods fn before_insert(_ctx: &mut HookContext) -> Result<(), DbError> { ... } fn after_insert( _ctx: &HookContext, _id: &Self::PrimaryKey, ) -> Result<(), DbError> { ... } fn before_update( _ctx: &mut HookContext, _id: &Self::PrimaryKey, ) -> Result<(), DbError> { ... } fn after_update( _ctx: &HookContext, _id: &Self::PrimaryKey, ) -> Result<(), DbError> { ... } fn before_delete( _ctx: &mut HookContext, _id: &Self::PrimaryKey, ) -> Result<(), DbError> { ... } fn after_delete( _ctx: &HookContext, _id: &Self::PrimaryKey, ) -> Result<(), DbError> { ... } fn before_write(_ctx: &mut HookContext) -> Result<(), DbError> { ... } fn after_write( _ctx: &HookContext, _id: &Self::PrimaryKey, ) -> Result<(), DbError> { ... } fn before_save(_ctx: &mut HookContext) -> Result<(), DbError> { ... } fn after_save( _ctx: &HookContext, _id: &Self::PrimaryKey, ) -> Result<(), DbError> { ... } fn before_restore( _ctx: &mut HookContext, _id: &Self::PrimaryKey, ) -> Result<(), DbError> { ... } fn after_restore( _ctx: &HookContext, _id: &Self::PrimaryKey, ) -> Result<(), DbError> { ... } fn before_find( _ctx: &mut HookContext, _id: &Self::PrimaryKey, ) -> Result<(), DbError> { ... } fn after_find( _ctx: &HookContext, _id: &Self::PrimaryKey, ) -> Result<(), DbError> { ... } fn before_validate(_ctx: &mut HookContext) -> Result<(), DbError> { ... } fn validate(_ctx: &mut HookContext) -> Result<(), DbError> { ... } fn after_validate(_ctx: &HookContext) -> Result<(), DbError> { ... }
}
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_writebefore_savebefore_insert, INSERT 完成后依次调用 after_insertafter_saveafter_write

Provided Methods§

Source

fn before_insert(_ctx: &mut HookContext) -> Result<(), DbError>

插入前钩子(默认 no-op)

Source

fn after_insert( _ctx: &HookContext, _id: &Self::PrimaryKey, ) -> Result<(), DbError>

插入后钩子(默认 no-op)

Source

fn before_update( _ctx: &mut HookContext, _id: &Self::PrimaryKey, ) -> Result<(), DbError>

更新前钩子(默认 no-op)

Source

fn after_update( _ctx: &HookContext, _id: &Self::PrimaryKey, ) -> Result<(), DbError>

更新后钩子(默认 no-op)

Source

fn before_delete( _ctx: &mut HookContext, _id: &Self::PrimaryKey, ) -> Result<(), DbError>

删除前钩子(默认 no-op)

Source

fn after_delete( _ctx: &HookContext, _id: &Self::PrimaryKey, ) -> Result<(), DbError>

删除后钩子(默认 no-op)

Source

fn before_write(_ctx: &mut HookContext) -> Result<(), DbError>

通用写入前钩子:insert 或 update 前均触发(默认 no-op)

适合用于审计日志、统一字段填充(如 updated_at = now())等场景。

Source

fn after_write( _ctx: &HookContext, _id: &Self::PrimaryKey, ) -> Result<(), DbError>

通用写入后钩子:insert 或 update 后均触发(默认 no-op)

Source

fn before_save(_ctx: &mut HookContext) -> Result<(), DbError>

保存前钩子(与 before_write 等价,命名风格不同,默认 no-op)

Source

fn after_save(_ctx: &HookContext, _id: &Self::PrimaryKey) -> Result<(), DbError>

保存后钩子(与 after_write 等价,命名风格不同,默认 no-op)

Source

fn before_restore( _ctx: &mut HookContext, _id: &Self::PrimaryKey, ) -> Result<(), DbError>

软删除恢复前钩子(默认 no-op)

当软删除行被恢复(UPDATE deleted_at = NULL)时触发。

Source

fn after_restore( _ctx: &HookContext, _id: &Self::PrimaryKey, ) -> Result<(), DbError>

软删除恢复后钩子(默认 no-op)

Source

fn before_find( _ctx: &mut HookContext, _id: &Self::PrimaryKey, ) -> Result<(), DbError>

单行查询前钩子(默认 no-op)

在执行 SELECT * FROM ... WHERE pk = ? 前触发。 适合用于查询缓存预热、查询审计日志、强制查询条件注入等。

Source

fn after_find(_ctx: &HookContext, _id: &Self::PrimaryKey) -> Result<(), DbError>

单行查询后钩子(默认 no-op)

SELECT * FROM ... WHERE pk = ? 返回结果后触发。 适合用于查询结果缓存填充、行级权限校验等。

Source

fn before_validate(_ctx: &mut HookContext) -> Result<(), DbError>

数据验证前钩子(默认 no-op)

在写入前的业务规则校验之前触发,调用顺序: before_writebefore_savebefore_validatevalidateafter_validatebefore_insert

适合用于字段非空校验、字段格式校验、跨字段一致性校验等。 失败时返回 Err(DbError::Validation(...)),会短路后续 before_insert。

Source

fn validate(_ctx: &mut HookContext) -> Result<(), DbError>

数据验证逻辑(默认 no-op)

before_validate 之后、after_validate 之前调用。 Model 可按需 override 此方法以实现实际的业务规则校验。 失败时返回 Err(DbError::Validation(...)),会短路后续 after_validatebefore_insert

Source

fn after_validate(_ctx: &HookContext) -> Result<(), DbError>

数据验证后钩子(默认 no-op)

验证成功后触发,可用于清理临时状态、记录验证日志。

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§