From 28cec59c2aaceb25f62ba83fbca423a27c25b935 Mon Sep 17 00:00:00 2001 From: Brendan Allan Date: Fri, 29 Dec 2023 16:48:29 +0800 Subject: [PATCH] use scalar type aliases --- crates/generator/src/lib.rs | 3 +++ crates/generator/src/models/mod.rs | 2 +- crates/generator/src/read_filters.rs | 2 ++ crates/generator/src/write_params.rs | 2 ++ crates/lib/src/lib.rs | 1 + crates/lib/src/scalar_types.rs | 9 +++++++++ crates/sdk/src/extensions.rs | 20 ++------------------ 7 files changed, 20 insertions(+), 19 deletions(-) create mode 100644 crates/lib/src/scalar_types.rs diff --git a/crates/generator/src/lib.rs b/crates/generator/src/lib.rs index 581713d8..b14cba06 100644 --- a/crates/generator/src/lib.rs +++ b/crates/generator/src/lib.rs @@ -52,6 +52,7 @@ impl PrismaGenerator for Generator { #header pub use _prisma::*; + use prisma_client_rust::scalar_types::*; #enums }, @@ -65,6 +66,8 @@ impl PrismaGenerator for Generator { module.add_submodule(Module::new( "_prisma", quote! { + use super::*; + #client #internal_enums #read_filters_module diff --git a/crates/generator/src/models/mod.rs b/crates/generator/src/models/mod.rs index 782b857e..0586fb3f 100644 --- a/crates/generator/src/models/mod.rs +++ b/crates/generator/src/models/mod.rs @@ -111,7 +111,7 @@ pub fn modules(args: &GenerateArgs, module_path: &TokenStream) -> Vec { let mut module = Module::new( model.name(), quote! { - use super::_prisma::*; + use super::{_prisma::*, *}; pub const NAME: &str = #model_name; diff --git a/crates/generator/src/read_filters.rs b/crates/generator/src/read_filters.rs index fcb7e45c..00abda6b 100644 --- a/crates/generator/src/read_filters.rs +++ b/crates/generator/src/read_filters.rs @@ -73,6 +73,8 @@ pub fn generate_module(args: &GenerateArgs) -> TokenStream { quote! { pub mod read_filters { + use super::*; + #(#read_filters)* } } diff --git a/crates/generator/src/write_params.rs b/crates/generator/src/write_params.rs index 8832c62f..03440145 100644 --- a/crates/generator/src/write_params.rs +++ b/crates/generator/src/write_params.rs @@ -84,6 +84,8 @@ pub fn generate_module(args: &GenerateArgs) -> TokenStream { quote! { pub mod write_params { + use super::*; + #(#write_params)* } } diff --git a/crates/lib/src/lib.rs b/crates/lib/src/lib.rs index b68fb8a0..a7afee63 100644 --- a/crates/lib/src/lib.rs +++ b/crates/lib/src/lib.rs @@ -18,6 +18,7 @@ pub mod operator; mod prisma_value; pub mod queries; pub mod raw; +pub mod scalar_types; pub mod serde; mod traits; mod transaction; diff --git a/crates/lib/src/scalar_types.rs b/crates/lib/src/scalar_types.rs new file mode 100644 index 00000000..a69db418 --- /dev/null +++ b/crates/lib/src/scalar_types.rs @@ -0,0 +1,9 @@ +pub type Int = i32; +pub type BigInt = i64; +pub type Decimal = bigdecimal::BigDecimal; +pub type Boolean = bool; +pub type DateTime = chrono::DateTime; +pub type Float = f64; +pub type String = std::string::String; +pub type Json = serde_json::Value; +pub type Bytes = Vec; diff --git a/crates/sdk/src/extensions.rs b/crates/sdk/src/extensions.rs index 3d032a2c..8660f84f 100644 --- a/crates/sdk/src/extensions.rs +++ b/crates/sdk/src/extensions.rs @@ -225,25 +225,9 @@ pub trait ScalarTypeExt { impl ScalarTypeExt for ScalarType { fn to_tokens(&self) -> TokenStream { - let pcr = quote!(::prisma_client_rust); + let ident = format_ident!("{}", self.as_str()); - match self { - ScalarType::Int => quote!(i32), - ScalarType::BigInt => quote!(i64), - ScalarType::Float => quote!(f64), - ScalarType::Decimal => quote!(#pcr::bigdecimal::BigDecimal), - ScalarType::Boolean => quote!(bool), - ScalarType::String => quote!(String), - ScalarType::Json => quote!(#pcr::serde_json::Value), - ScalarType::DateTime => { - quote!( - #pcr::chrono::DateTime< - #pcr::chrono::FixedOffset, - > - ) - } - ScalarType::Bytes => quote!(Vec), - } + quote!(#ident) } fn to_prisma_value(&self, var: &Ident) -> TokenStream {