Skip to main content

ferriorm_codegen/
client.rs

1//! Generates the `client.rs` module containing the `FerriormClient` struct.
2//!
3//! `FerriormClient` is the user-facing entry point. It wraps a
4//! `ferriorm_runtime::client::DatabaseClient` and exposes a method per model
5//! (e.g., `.user()`, `.post()`) that returns the model's `Actions` struct
6//! for performing CRUD operations.
7
8use ferriorm_core::schema::Schema;
9use ferriorm_core::utils::to_snake_case;
10use proc_macro2::TokenStream;
11use quote::{format_ident, quote};
12
13/// Generate the `client.rs` module with the FerriormClient struct.
14pub 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        #![allow(unused_imports, dead_code, unused_variables, clippy::all, clippy::pedantic, clippy::nursery)]
33
34        use ferriorm_runtime::prelude::*;
35
36        pub struct FerriormClient {
37            inner: DatabaseClient,
38        }
39
40        impl FerriormClient {
41            /// Connect to the database using the provided URL.
42            pub async fn connect(url: &str) -> Result<Self, FerriormError> {
43                let inner = DatabaseClient::connect(url).await?;
44                Ok(Self { inner })
45            }
46
47            /// Connect to the database using the provided URL and pool configuration.
48            pub async fn connect_with_config(url: &str, config: &PoolConfig) -> Result<Self, FerriormError> {
49                let inner = DatabaseClient::connect_with_config(url, config).await?;
50                Ok(Self { inner })
51            }
52
53            /// Get a reference to the underlying [`DatabaseClient`].
54            ///
55            /// Use this for raw SQL queries via `client().pg_pool()` or
56            /// `client().sqlite_pool()`.
57            pub fn client(&self) -> &DatabaseClient {
58                &self.inner
59            }
60
61            #(#model_accessors)*
62
63            /// Close the database connection.
64            pub async fn disconnect(self) {
65                self.inner.disconnect().await;
66            }
67        }
68    }
69}