floz-orm 0.1.7

A lightweight, typesafe Rust ORM — unifying DAO and DSL from a single schema
Documentation
//! 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 crate::error::FlozError;

/// Lifecycle hooks for database operations.
///
/// `before_*` hooks return `Result` — returning `Err(...)` aborts the operation.
/// `after_*` hooks are fire-and-forget notifications.
pub trait FlozHooks: Sized {
    /// Called before `create()`. Return `Err` to abort the INSERT.
    ///
    /// Takes `&self` (immutable). Modify fields before calling `create()`.
    fn before_create(&self) -> Result<(), FlozError> {
        Ok(())
    }

    /// Called after a successful `create()`.
    fn after_create(&self) {}

    /// Called before `save()`. Can modify the entity (e.g., set `updated_at`).
    /// Return `Err` to abort the UPDATE.
    ///
    /// If this hook sets a field via `set_*()`, the field becomes dirty and
    /// will be included in the UPDATE statement.
    fn before_save(&mut self) -> Result<(), FlozError> {
        Ok(())
    }

    /// Called after a successful `save()`.
    fn after_save(&self) {}

    /// Called before `delete()`. Return `Err` to abort the DELETE.
    fn before_delete(&self) -> Result<(), FlozError> {
        Ok(())
    }

    /// Called after a successful `delete()`.
    fn after_delete(&self) {}
}