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 build_target(&self, data_source: &dyn Any) -> Box<dyn Any> {
72        let ds = data_source
73            .downcast_ref::<T>()
74            .expect("data source type mismatch in HasMany::build_target");
75        Box::new((self.build_target)(ds.clone()))
76    }
77
78    fn cardinality(&self) -> vantage_vista::ReferenceKind {
79        vantage_vista::ReferenceKind::HasMany
80    }
81
82    fn resolve_from_row(
83        &self,
84        data_source: &dyn Any,
85        source_id_field: &str,
86        source_row: &dyn Any,
87    ) -> Result<Box<dyn Any>> {
88        let ds = data_source
89            .downcast_ref::<T>()
90            .ok_or_else(|| error!("data source type mismatch in HasMany::resolve_from_row"))?;
91        let row = source_row
92            .downcast_ref::<Record<T::Value>>()
93            .ok_or_else(|| error!("source row type mismatch in HasMany::resolve_from_row"))?;
94
95        let mut target = (self.build_target)(ds.clone());
96        let (src_col, tgt_col) = self.columns(source_id_field, "");
97        let join_value = row
98            .get(&src_col)
99            .cloned()
100            .ok_or_else(|| error!("source row missing id field", field = src_col.as_str()))?;
101
102        let condition = ds.eq_value_condition(&tgt_col, join_value)?;
103        target.add_condition(condition);
104
105        Ok(Box::new(target.into_entity::<EmptyEntity>()))
106    }
107
108    fn target_type_name(&self) -> &'static str {
109        std::any::type_name::<Table<T, TargetE>>()
110    }
111}