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§
Sourcefn before_save(&mut self) -> BoxFuture<'_>
fn before_save(&mut self) -> BoxFuture<'_>
Before INSERT or UPDATE.
Sourcefn after_save(&self) -> BoxFuture<'_>
fn after_save(&self) -> BoxFuture<'_>
After INSERT or UPDATE.
Sourcefn before_create(&mut self) -> BoxFuture<'_>
fn before_create(&mut self) -> BoxFuture<'_>
Before INSERT only.
Sourcefn after_create(&self) -> BoxFuture<'_>
fn after_create(&self) -> BoxFuture<'_>
After INSERT only.
Sourcefn before_update(&mut self) -> BoxFuture<'_>
fn before_update(&mut self) -> BoxFuture<'_>
Before UPDATE only.
Sourcefn after_update(&self) -> BoxFuture<'_>
fn after_update(&self) -> BoxFuture<'_>
After UPDATE only.
Sourcefn before_delete(&self) -> BoxFuture<'_>
fn before_delete(&self) -> BoxFuture<'_>
Before DELETE.
Sourcefn after_delete(&self) -> BoxFuture<'_>
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".