1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//! Lifecycle hooks for floz models.
//!
//! The `FlozHooks` trait provides `before_*` and `after_*` callbacks for
//! create, save, and delete operations. All methods have default no-op
//! implementations — override only the ones you need.
//!
//! # Usage
//!
//! By default, the `schema!` macro generates `impl FlozHooks for Model {}`
//! (using all defaults). To provide custom hooks, annotate the model with
//! `#[hooks]` and implement the trait yourself:
//!
//! ```ignore
//! floz::schema! {
//! #[hooks]
//! model User("users") {
//! id: integer("id").auto_increment().primary(),
//! name: varchar("name", 100),
//! updated_at: datetime("updated_at").tz(),
//! }
//! }
//!
//! impl floz::FlozHooks for User {
//! fn before_save(&mut self) -> Result<(), floz::FlozError> {
//! self.set_updated_at(chrono::Utc::now());
//! Ok(())
//! }
//! }
//! ```
use crateFlozError;
/// Lifecycle hooks for database operations.
///
/// `before_*` hooks return `Result` — returning `Err(...)` aborts the operation.
/// `after_*` hooks are fire-and-forget notifications.