use ferriorm_core::schema::Schema;
use ferriorm_core::utils::to_snake_case;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
#[must_use]
pub fn generate_client_module(schema: &Schema) -> TokenStream {
let model_accessors: Vec<TokenStream> = schema
.models
.iter()
.map(|m| {
let method_name = format_ident!("{}", to_snake_case(&m.name));
let actions_type = format_ident!("{}Actions", m.name);
let module_name = format_ident!("{}", to_snake_case(&m.name));
quote! {
pub fn #method_name(&self) -> super::#module_name::#actions_type<'_> {
super::#module_name::#actions_type::new(&self.inner)
}
}
})
.collect();
quote! {
#![allow(unused_imports, dead_code, unused_variables, clippy::all, clippy::pedantic, clippy::nursery)]
use ferriorm_runtime::prelude::*;
pub struct FerriormClient {
inner: DatabaseClient,
}
impl FerriormClient {
pub async fn connect(url: &str) -> Result<Self, FerriormError> {
let inner = DatabaseClient::connect(url).await?;
Ok(Self { inner })
}
pub async fn connect_with_config(url: &str, config: &PoolConfig) -> Result<Self, FerriormError> {
let inner = DatabaseClient::connect_with_config(url, config).await?;
Ok(Self { inner })
}
pub fn client(&self) -> &DatabaseClient {
&self.inner
}
#(#model_accessors)*
pub async fn disconnect(self) {
self.inner.disconnect().await;
}
}
}
}