modo_db/hooks.rs
1/// Lifecycle hooks for database entities.
2///
3/// Provides no-op default implementations for `before_save`, `after_save`, and
4/// `before_delete` via a blanket impl on all types.
5///
6/// When the `#[entity]` macro generates `insert`/`update`/`delete` methods it
7/// calls `self.before_save()` etc. using this trait. If the user defines an
8/// inherent method with the same name and signature on their struct, Rust's
9/// inherent-method priority means the user's method takes precedence. If no
10/// inherent method is defined the blanket default (no-op) fires automatically.
11///
12/// # Caution
13///
14/// The inherent-method shadowing relies on exact signature matching.
15/// If you define a hook with a different return type (e.g.,
16/// `Result<(), String>` instead of `Result<(), modo::Error>`), it will
17/// NOT shadow the trait method — both will coexist and the no-op default
18/// will fire. Always match the exact signatures shown above.
19pub trait DefaultHooks {
20 /// Called before the entity is inserted or updated.
21 fn before_save(&mut self) -> Result<(), modo::Error> {
22 Ok(())
23 }
24
25 /// Called after the entity has been successfully inserted or updated.
26 fn after_save(&self) -> Result<(), modo::Error> {
27 Ok(())
28 }
29
30 /// Called before the entity is deleted.
31 fn before_delete(&self) -> Result<(), modo::Error> {
32 Ok(())
33 }
34}
35
36impl<T> DefaultHooks for T {}