pub trait ModelHooks {
// Provided methods
fn before_create(&mut self) { ... }
fn after_create<E: Executor + Send + Sync>(
&self,
executor: &E,
) -> impl Future<Output = Result<()>> + Send { ... }
fn before_save(&mut self) { ... }
fn after_save<E: Executor + Send + Sync>(
&self,
executor: &E,
) -> impl Future<Output = Result<()>> + Send { ... }
fn before_delete(&self) { ... }
fn after_delete<E: Executor + Send + Sync>(
&self,
executor: &E,
) -> impl Future<Output = Result<()>> + Send { ... }
}Expand description
Lifecycle hooks fired by the instance write methods.
Every model gets a no-op implementation automatically. To add behavior, mark
the model #[table(hooks)] (which suppresses the generated empty impl) and
write your own:
use tork_orm_core::{Executor, ModelHooks};
impl ModelHooks for User {
fn before_save(&mut self) {
self.updated_at += 1; // e.g. set a timestamp
}
async fn after_create<E: Executor + Send + Sync>(&self, _db: &E) -> tork_orm_core::Result<()> {
// side effect: audit log, emit an event, …
Ok(())
}
}Hooks fire only for the instance methods (create, save, delete, upsert,
upsert_on); bulk operations (bulk_create, QuerySet::update/delete) bypass
them. An after_* hook returning Err aborts the operation — inside a
transaction that rolls the change back; outside one, the row has already been
written.
Provided Methods§
Sourcefn before_create(&mut self)
fn before_create(&mut self)
Runs before an insert, able to mutate the row (set timestamps, a client-side UUID, …).
Sourcefn after_create<E: Executor + Send + Sync>(
&self,
executor: &E,
) -> impl Future<Output = Result<()>> + Send
fn after_create<E: Executor + Send + Sync>( &self, executor: &E, ) -> impl Future<Output = Result<()>> + Send
Runs after a successful insert, on the stored row (with its DB-generated values). Async; an error aborts the operation.
Sourcefn before_save(&mut self)
fn before_save(&mut self)
Runs before a save, able to mutate the row (e.g. bump updated_at).
Sourcefn after_save<E: Executor + Send + Sync>(
&self,
executor: &E,
) -> impl Future<Output = Result<()>> + Send
fn after_save<E: Executor + Send + Sync>( &self, executor: &E, ) -> impl Future<Output = Result<()>> + Send
Runs after a successful save. Async; an error aborts the operation.
Sourcefn before_delete(&self)
fn before_delete(&self)
Runs before a delete.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".