Skip to main content

vantage_table/references/
many.rs

1//! HasMany — one-to-many relationship where the foreign key is on the target table.
2//!
3//! Example: Bakery has many Clients (via client.bakery_id)
4
5use std::fmt::Display;
6use std::{any::Any, marker::PhantomData, sync::Arc};
7
8use vantage_core::{Result, error};
9use vantage_types::{EmptyEntity, Entity, Record};
10
11use crate::{references::Reference, table::Table, traits::table_source::TableSource};
12
13pub struct HasMany<T: TableSource, SourceE: Entity<T::Value>, TargetE: Entity<T::Value>> {
14    /// Foreign key column on the target table (e.g. "bakery_id" on client)
15    target_foreign_key: String,
16    /// Factory: given a data source, produce the target table
17    build_target: Arc<dyn Fn(T) -> Table<T, TargetE> + Send + Sync>,
18    _phantom: PhantomData<SourceE>,
19}
20
21impl<T: TableSource, SourceE: Entity<T::Value>, TargetE: Entity<T::Value>>
22    HasMany<T, SourceE, TargetE>
23{
24    pub fn new(
25        target_foreign_key: impl Into<String>,
26        build_target: impl Fn(T) -> Table<T, TargetE> + Send + Sync + 'static,
27    ) -> Self {
28        Self {
29            target_foreign_key: target_foreign_key.into(),
30            build_target: Arc::new(build_target),
31            _phantom: PhantomData,
32        }
33    }
34}
35
36impl<T: TableSource, SourceE: Entity<T::Value>, TargetE: Entity<T::Value>> Clone
37    for HasMany<T, SourceE, TargetE>
38{
39    fn clone(&self) -> Self {
40        Self {
41            target_foreign_key: self.target_foreign_key.clone(),
42            build_target: self.build_target.clone(),
43            _phantom: PhantomData,
44        }
45    }
46}
47
48impl<T: TableSource, SourceE: Entity<T::Value>, TargetE: Entity<T::Value>> std::fmt::Debug
49    for HasMany<T, SourceE, TargetE>
50{
51    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52        f.debug_struct("HasMany")
53            .field("target_foreign_key", &self.target_foreign_key)
54            .finish()
55    }
56}
57
58impl<
59    T: TableSource + 'static,
60    SourceE: Entity<T::Value> + 'static,
61    TargetE: Entity<T::Value> + 'static,
62> Reference for HasMany<T, SourceE, TargetE>
63where
64    T::Value: Into<ciborium::Value> + From<ciborium::Value>,
65    T::Id: Display + From<String>,
66{
67    fn columns(&self, source_id: &str, _target_id: &str) -> (String, String) {
68        (source_id.to_string(), self.target_foreign_key.clone())
69    }
70
71    fn foreign_key(&self) -> &str {
72        &self.target_foreign_key
73    }
74
75    fn build_target(&self, data_source: &dyn Any) -> Box<dyn Any> {
76        let ds = data_source
77            .downcast_ref::<T>()
78            .expect("data source type mismatch in HasMany::build_target");
79        Box::new((self.build_target)(ds.clone()))
80    }
81
82    fn cardinality(&self) -> vantage_vista::ReferenceKind {
83        vantage_vista::ReferenceKind::HasMany
84    }
85
86    fn resolve_from_row(
87        &self,
88        data_source: &dyn Any,
89        source_id_field: &str,
90        source_row: &dyn Any,
91    ) -> Result<Box<dyn Any>> {
92        let ds = data_source
93            .downcast_ref::<T>()
94            .ok_or_else(|| error!("data source type mismatch in HasMany::resolve_from_row"))?;
95        let row = source_row
96            .downcast_ref::<Record<T::Value>>()
97            .ok_or_else(|| error!("source row type mismatch in HasMany::resolve_from_row"))?;
98
99        let mut target = (self.build_target)(ds.clone());
100        let (src_col, tgt_col) = self.columns(source_id_field, "");
101        let join_value = row
102            .get(&src_col)
103            .cloned()
104            .ok_or_else(|| error!("source row missing id field", field = src_col.as_str()))?;
105
106        let condition = ds.eq_value_condition(&tgt_col, join_value)?;
107        target.add_condition(condition);
108
109        Ok(Box::new(target.into_entity::<EmptyEntity>()))
110    }
111
112    fn target_type_name(&self) -> &'static str {
113        std::any::type_name::<Table<T, TargetE>>()
114    }
115}