Skip to main content

ModelHooks

Trait ModelHooks 

Source
pub trait ModelHooks: Sized {
    // Provided methods
    fn before_save(&mut self) -> BoxFuture<'_> { ... }
    fn after_save(&self) -> BoxFuture<'_> { ... }
    fn before_create(&mut self) -> BoxFuture<'_> { ... }
    fn after_create(&self) -> BoxFuture<'_> { ... }
    fn before_update(&mut self) -> BoxFuture<'_> { ... }
    fn after_update(&self) -> BoxFuture<'_> { ... }
    fn before_delete(&self) -> BoxFuture<'_> { ... }
    fn after_delete(&self) -> BoxFuture<'_> { ... }
}
Expand description

Optional lifecycle hooks for a Model. All methods have a no-op default — implement only what you need. Returning Err from a before_* hook cancels the operation (no SQL runs); the error reaches the caller as Error::Hook. An after_* error is returned too, but the statement has already run: it is rolled back only if the caller is inside atomic() / a transaction.

Fired by insert, update, delete, get_or_create and update_or_create; never by bulk operations, query-builder updates/deletes or raw SQL. #[derive(Model)] emits an empty impl unless the struct has #[model(hooks)], in which case you write impl ModelHooks.

Provided Methods§

Source

fn before_save(&mut self) -> BoxFuture<'_>

Before INSERT or UPDATE.

Source

fn after_save(&self) -> BoxFuture<'_>

After INSERT or UPDATE.

Source

fn before_create(&mut self) -> BoxFuture<'_>

Before INSERT only.

Source

fn after_create(&self) -> BoxFuture<'_>

After INSERT only.

Source

fn before_update(&mut self) -> BoxFuture<'_>

Before UPDATE only.

Source

fn after_update(&self) -> BoxFuture<'_>

After UPDATE only.

Source

fn before_delete(&self) -> BoxFuture<'_>

Before DELETE.

Source

fn after_delete(&self) -> BoxFuture<'_>

After DELETE.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§