ferriorm_codegen/
client.rs1use ferriorm_core::schema::Schema;
9use ferriorm_core::utils::to_snake_case;
10use proc_macro2::TokenStream;
11use quote::{format_ident, quote};
12
13pub fn generate_client_module(schema: &Schema) -> TokenStream {
15 let model_accessors: Vec<TokenStream> = schema
16 .models
17 .iter()
18 .map(|m| {
19 let method_name = format_ident!("{}", to_snake_case(&m.name));
20 let actions_type = format_ident!("{}Actions", m.name);
21 let module_name = format_ident!("{}", to_snake_case(&m.name));
22
23 quote! {
24 pub fn #method_name(&self) -> super::#module_name::#actions_type<'_> {
25 super::#module_name::#actions_type::new(&self.inner)
26 }
27 }
28 })
29 .collect();
30
31 quote! {
32 use ferriorm_runtime::prelude::*;
33
34 pub struct FerriormClient {
35 inner: DatabaseClient,
36 }
37
38 impl FerriormClient {
39 pub async fn connect(url: &str) -> Result<Self, FerriormError> {
41 let inner = DatabaseClient::connect(url).await?;
42 Ok(Self { inner })
43 }
44
45 pub async fn connect_with_config(url: &str, config: &PoolConfig) -> Result<Self, FerriormError> {
47 let inner = DatabaseClient::connect_with_config(url, config).await?;
48 Ok(Self { inner })
49 }
50
51 pub fn client(&self) -> &DatabaseClient {
56 &self.inner
57 }
58
59 #(#model_accessors)*
60
61 pub async fn disconnect(self) {
63 self.inner.disconnect().await;
64 }
65 }
66 }
67}