mini_query/
lib.rs

1pub use mini_query_derive::MiniQuery;
2
3#[macro_export]
4macro_rules! has_many {
5  ($child:ident, $fn_name:ident, $pk:ident, $fk:ident) => {
6    pub async fn $fn_name(&self, client: &impl GenericClient) -> anyhow::Result<Vec<$child>> {
7      let recs = client
8        .query(
9          &format!("SELECT * FROM {} WHERE {} = $1", $child::__TABLE_NAME__, stringify!($fk)),
10          &[&self.$pk],
11        )
12        .await?
13        .iter()
14        .map($child::from)
15        .collect();
16      Ok(recs)
17    }
18  };
19}
20
21#[macro_export]
22macro_rules! belongs_to {
23  ($parent:ident, $fn_name:ident, $pk:ident, $fk:ident) => {
24    pub async fn $fn_name(&self, client: &impl GenericClient) -> anyhow::Result<Option<$parent>> {
25      let rec = client
26        .query_opt(
27          &format!("SELECT * FROM {} WHERE {} = $1", $parent::__TABLE_NAME__, stringify!($pk)),
28          &[&self.$fk],
29        )
30        .await?
31        .map($parent::from);
32      Ok(rec)
33    }
34  };
35}