Skip to main content

vantage_table/
references.rs

1//! Table reference system for relationships between tables.
2//!
3//! The `Reference` trait describes a relationship (field names, target factory).
4//! Resolution (building conditions) happens in `Table::get_ref_as`.
5//!
6//! Three concrete types:
7//! - `HasOne` — foreign key on source table (e.g. Client.bakery_id → Bakery)
8//! - `HasMany` — foreign key on target table (e.g. Bakery → Client.bakery_id)
9//! - `HasForeign` — cross-persistence reference with user-provided resolution
10
11use std::any::Any;
12
13use vantage_core::Result;
14
15use crate::any::AnyTable;
16
17pub mod foreign;
18pub mod many;
19pub mod one;
20
21pub use foreign::HasForeign;
22pub use many::HasMany;
23pub use one::HasOne;
24
25/// Describes a relationship between two tables.
26pub trait Reference: Send + Sync {
27    /// Given source and target id field names, return (source_column, target_column).
28    fn columns(&self, source_id: &str, target_id: &str) -> (String, String);
29
30    /// Produce a fresh target table (no conditions applied).
31    fn build_target(&self, data_source: &dyn Any) -> Box<dyn Any>;
32
33    /// Whether this is a cross-persistence reference.
34    fn is_foreign(&self) -> bool {
35        false
36    }
37
38    /// Resolve this reference and return an AnyTable.
39    ///
40    /// For same-backend: builds target, applies condition, wraps in AnyTable.
41    /// For foreign: returns the AnyTable from the user's closure.
42    fn resolve_as_any(&self, source_table: &dyn Any) -> Result<AnyTable>;
43
44    /// Type name of the target table (for error messages).
45    fn target_type_name(&self) -> &'static str;
46}