larust_orm/model.rs
1//! `Model` - a thin, compile-time-only marker `#[derive(Model)]` (in
2//! `larust-macros`) implements for every annotated struct, automatically.
3//! Never written by hand.
4//!
5//! Exists so *generic* code (model factories first; later milestones add
6//! more) can call `T::create(...)` without knowing the concrete struct -
7//! the same "reach the real generated methods through a trait, nothing
8//! resolved at runtime" shape `larust_repository::Repository` already
9//! established, but for genericity over a single `#[derive(Model)]`
10//! struct's own generated methods rather than storage-backend
11//! abstraction. `Self::create` forwards to the struct's already-generated
12//! *inherent* `create` - inherent methods take priority over trait
13//! methods of the same name, so this doesn't recurse.
14
15use larust_core::AppError;
16use std::future::Future;
17
18/// Implemented automatically by `#[derive(Model)]`.
19///
20/// `-> impl Future<...> + Send` rather than a plain `async fn`, matching
21/// `larust_repository::Repository`'s own established reasoning: trait
22/// `async fn` doesn't propagate `Send` by default, and this needs to be
23/// callable from axum handlers (and from generic code reached by them)
24/// that require `Send` futures. The generated impl can still be written
25/// as an ordinary `async fn` - its desugared return type already
26/// satisfies `-> impl Future<...>`.
27pub trait Model:
28 for<'r> sqlx::FromRow<'r, sqlx::any::AnyRow> + Send + Unpin + Sized + 'static
29{
30 /// The struct's own generated `New{Model}` type (every field except
31 /// the primary key).
32 type New: Send;
33
34 /// Mirrors the inherent `Self::TABLE` constant `#[derive(Model)]`
35 /// already generates.
36 const TABLE: &'static str;
37
38 /// `Some(column)` only for a struct marked `#[soft_deletes]` - the
39 /// nullable timestamp column `delete()` sets instead of actually
40 /// removing a row. Defaults to `None` so every *other* model needs no
41 /// changes at all; only the soft-delete-aware code paths
42 /// (`QueryBuilder`'s own filtering, `belongs_to_many`'s generated
43 /// `JOIN`) ever read this, always at runtime through the real,
44 /// monomorphized const for whatever concrete type is in play - never
45 /// something a macro invocation elsewhere could see at expansion
46 /// time, since it has no visibility into a *different* struct's own
47 /// `#[derive(Model)]` attributes.
48 const SOFT_DELETE_COLUMN: Option<&'static str> = None;
49
50 fn create(data: Self::New) -> impl Future<Output = Result<Self, AppError>> + Send;
51}