Skip to content

Commit

Permalink
UncheckedSetParam (#287)
Browse files Browse the repository at this point in the history
* fixed it

* add create_unchecked action

* UncheckedSetParam

* Unchecked -> SetParam

* update function arguments

* add unchecked test

* more unchecked stuff + scalar where generator macro
  • Loading branch information
Brendonovich committed Mar 23, 2023
1 parent a81c82a commit af7694a
Show file tree
Hide file tree
Showing 19 changed files with 270 additions and 91 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ exclude = ["examples", "integration-tests"]
members = [
"examples/*",
"integration-tests",
# "tests/*",
"sdk",
"sdk/example/*",
"cli",
Expand Down
88 changes: 55 additions & 33 deletions cli/src/generator/models/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,42 @@ use prisma_client_rust_sdk::GenerateArgs;

use super::required_fields;

pub fn create_args_params_pushes(model: &dml::Model) -> Vec<TokenStream> {
let required_fields = required_fields(model);
pub fn create_fn(model: &dml::Model) -> TokenStream {
let (names, (types, push_wrappers)): (Vec<_>, (Vec<_>, Vec<_>)) = required_fields(model)
.into_iter()
.map(|field| (snake_ident(field.name()), (field.typ, field.push_wrapper)))
.unzip();

required_fields
.iter()
.map(|field| {
let field_name_snake = snake_ident(field.name());
let push_wrapper = &field.push_wrapper;
quote! {
pub fn create(self, #(#names: #types,)* mut _params: Vec<SetParam>) -> Create<'a> {
_params.extend([
#(#push_wrappers(#names)),*
]);

quote!(_params.push(#push_wrapper(#field_name_snake)))
})
.collect()
Create::new(
self.client,
_params
)
}
}
}

pub fn create_fn(model: &dml::Model) -> TokenStream {
let required_fields = required_fields(model);

let required_field_names = required_fields
pub fn create_unchecked_fn(model: &dml::Model) -> TokenStream {
let (names, types): (Vec<_>, Vec<_>) = model
.required_scalar_fields()
.iter()
.map(|field| snake_ident(field.name()));
let required_field_types = required_fields.iter().map(|field| &field.typ);

let create_args_params_pushes = create_args_params_pushes(model);
.map(|f| (snake_ident(f.name()), f.type_tokens(quote!())))
.unzip();

quote! {
pub fn create(self, #(#required_field_names: #required_field_types,)* mut _params: Vec<SetParam>) -> Create<'a> {
#(#create_args_params_pushes;)*
pub fn create_unchecked(self, #(#names: #types,)* mut _params: Vec<UncheckedSetParam>) -> Create<'a> {
_params.extend([
#(#names::set(#names)),*
]);

Create::new(
self.client,
_params
_params.into_iter().map(Into::into).collect()
)
}
}
Expand All @@ -54,9 +59,11 @@ pub fn create_many_fn(model: &dml::Model) -> TokenStream {
.collect::<Vec<_>>();

quote! {
pub fn create_many(self, data: Vec<(#(#scalar_field_types,)* Vec<SetParam>)>) -> CreateMany<'a> {
pub fn create_many(self, data: Vec<(#(#scalar_field_types,)* Vec<UncheckedSetParam>)>) -> CreateMany<'a> {
let data = data.into_iter().map(|(#(#scalar_field_names2,)* mut _params)| {
#(_params.push(#scalar_field_names::set(#scalar_field_names));)*
_params.extend([
#(#scalar_field_names::set(#scalar_field_names)),*
]);

_params
}).collect();
Expand All @@ -70,17 +77,21 @@ pub fn create_many_fn(model: &dml::Model) -> TokenStream {
}

pub fn upsert_fn(model: &dml::Model) -> TokenStream {
let required_fields = required_fields(model);

let create_args_names_snake = required_fields
.iter()
.map(|field| snake_ident(field.name()));
let create_args_typs = required_fields.iter().map(|field| &field.typ);
let create_args_params_pushes = create_args_params_pushes(model);
let (names, (types, push_wrappers)): (Vec<_>, (Vec<_>, Vec<_>)) = required_fields(model)
.into_iter()
.map(|field| (snake_ident(field.name()), (field.typ, field.push_wrapper)))
.unzip();

quote! {
pub fn upsert(self, _where: UniqueWhereParam, (#(#create_args_names_snake,)* mut _params): (#(#create_args_typs,)* Vec<SetParam>), _update: Vec<SetParam>) -> Upsert<'a> {
#(#create_args_params_pushes;)*
pub fn upsert(
self,
_where: UniqueWhereParam,
(#(#names,)* mut _params): (#(#types,)* Vec<SetParam>),
_update: Vec<SetParam>
) -> Upsert<'a> {
_params.extend([
#(#push_wrappers(#names)),*
]);

Upsert::new(
self.client,
Expand All @@ -96,6 +107,7 @@ pub fn struct_definition(model: &dml::Model, args: &GenerateArgs) -> TokenStream
let pcr = quote!(::prisma_client_rust);

let create_fn = create_fn(model);
let create_unchecked_fn = create_unchecked_fn(model);
let upsert_fn = upsert_fn(model);

let create_many_fn = (args
Expand Down Expand Up @@ -133,6 +145,7 @@ pub fn struct_definition(model: &dml::Model, args: &GenerateArgs) -> TokenStream
}

#create_fn
#create_unchecked_fn

#create_many_fn

Expand All @@ -145,7 +158,16 @@ pub fn struct_definition(model: &dml::Model, args: &GenerateArgs) -> TokenStream
)
}

pub fn update_many(self, _where: Vec<WhereParam>, _params: Vec<SetParam>) -> UpdateMany<'a> {
pub fn update_unchecked(self, _where: UniqueWhereParam, _params: Vec<UncheckedSetParam>) -> Update<'a> {
Update::new(
self.client,
_where.into(),
_params.into_iter().map(Into::into).collect(),
vec![]
)
}

pub fn update_many(self, _where: Vec<WhereParam>, _params: Vec<UncheckedSetParam>) -> UpdateMany<'a> {
UpdateMany::new(
self.client,
_where,
Expand Down
4 changes: 2 additions & 2 deletions cli/src/generator/models/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ fn create_unchecked(model: &dml::Model) -> TokenStream {
.unzip();

quote! {
pub fn create_unchecked(#(#scalar_field_names: #scalar_field_types,)* _params: Vec<SetParam>)
-> (#(#scalar_field_types,)* Vec<SetParam>) {
pub fn create_unchecked(#(#scalar_field_names: #scalar_field_types,)* _params: Vec<UncheckedSetParam>)
-> (#(#scalar_field_types,)* Vec<UncheckedSetParam>) {
(#(#scalar_field_names,)* _params)
}
}
Expand Down
21 changes: 14 additions & 7 deletions cli/src/generator/models/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,17 @@ pub fn module(

let typ = method.type_tokens(quote!());

Some(quote! {
pub fn #method_name_snake(value: #typ) -> WhereParam {
WhereParam::#field_name_pascal(_prisma::read_filters::#filter_enum::#method_name_pascal(value))
}
})
Some(quote!(fn #method_name_snake(_: #typ) -> #method_name_pascal;))
});

quote! {
#equals

#(#read_methods)*
#pcr::scalar_where_param_fns!(
_prisma::read_filters::#filter_enum,
#field_name_pascal,
{ #(#read_methods)* }
);
}
});

Expand All @@ -246,12 +246,19 @@ pub fn module(

quote! {
pub struct Set(pub #field_type);

impl From<Set> for SetParam {
fn from(value: Set) -> Self {
Self::#set_variant(value.0)
}
}

impl From<Set> for UncheckedSetParam {
fn from(value: Set) -> Self {
Self::#field_name_pascal(value.0)
}
}

pub fn set<T: From<Set>>(value: #field_type) -> T {
Set(value).into()
}
Expand All @@ -275,7 +282,7 @@ pub fn module(
quote! {
pub mod #field_name_snake {
use super::super::*;
use super::{WhereParam, UniqueWhereParam, OrderByParam, WithParam, SetParam};
use super::{WhereParam, UniqueWhereParam, OrderByParam, WithParam, SetParam, UncheckedSetParam};
use super::_prisma::*;

pub const NAME: &str = #field_name;
Expand Down
24 changes: 12 additions & 12 deletions cli/src/generator/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub fn generate(args: &GenerateArgs, module_path: TokenStream) -> Vec<TokenStrea
where_params_entries.extend(OPERATORS.iter().map(|op| {
let variant_name = pascal_ident(&op.name);
let op_action = &op.action;

let value = match op.list {
true => quote! {
#pcr::SerializedWhereValue::List(
Expand Down Expand Up @@ -202,25 +202,25 @@ pub fn generate(args: &GenerateArgs, module_path: TokenStream) -> Vec<TokenStrea
let field = fields[0];

let read_filter = args.read_filter(field.as_scalar_field().unwrap()).unwrap();

where_params_entries.push(Variant::unique(field, read_filter));

None
} else {
let variant_name_string = fields.iter().map(|f| pascal_ident(f.name()).to_string()).collect::<String>();
let variant_name = format_ident!("{}Equals", &variant_name_string);

let variant_data_names = fields.iter().map(|f| snake_ident(f.name())).collect::<Vec<_>>();
let ((field_defs, field_types), (prisma_values, field_names_snake)):

let ((field_defs, field_types), (prisma_values, field_names_snake)):
((Vec<_>, Vec<_>), (Vec<_>, Vec<_>)) = fields.into_iter().map(|field| {
let field_type = match field.arity() {
dml::FieldArity::List | dml::FieldArity::Required => field.type_tokens(quote!()),
dml::FieldArity::Optional => field.field_type().to_tokens(quote!(), &dml::FieldArity::Required)
};

let field_name_snake = snake_ident(field.name());

(
(quote!(#field_name_snake: #field_type), field_type),
(field.field_type().to_prisma_value(&field_name_snake, &dml::FieldArity::Required), field_name_snake)
Expand Down Expand Up @@ -263,7 +263,7 @@ pub fn generate(args: &GenerateArgs, module_path: TokenStream) -> Vec<TokenStrea
.unzip();

where_params_entries.extend(field_where_param_entries.into_iter().flatten());

let where_params_enums = where_params::collate_entries(where_params_entries);
let data_struct = data::struct_definition(&model);
let with_params_enum = with_params::enum_definition(&model);
Expand All @@ -283,13 +283,13 @@ pub fn generate(args: &GenerateArgs, module_path: TokenStream) -> Vec<TokenStrea
use super::_prisma::*;

pub const NAME: &str = #model_name;

#(#field_modules)*

#compound_field_accessors

#create_fn

#select_macro
#select_params_enum

Expand All @@ -312,7 +312,7 @@ pub fn generate(args: &GenerateArgs, module_path: TokenStream) -> Vec<TokenStrea

pub type UniqueArgs = ::prisma_client_rust::UniqueArgs<Types>;
pub type ManyArgs = ::prisma_client_rust::ManyArgs<Types>;

pub type Count<'a> = ::prisma_client_rust::Count<'a, Types>;
pub type Create<'a> = ::prisma_client_rust::Create<'a, Types>;
pub type CreateMany<'a> = ::prisma_client_rust::CreateMany<'a, Types>;
Expand All @@ -324,7 +324,7 @@ pub fn generate(args: &GenerateArgs, module_path: TokenStream) -> Vec<TokenStrea
pub type Upsert<'a> = ::prisma_client_rust::Upsert<'a, Types>;
pub type Delete<'a> = ::prisma_client_rust::Delete<'a, Types>;
pub type DeleteMany<'a> = ::prisma_client_rust::DeleteMany<'a, Types>;

#actions_struct
}
}
Expand Down
59 changes: 46 additions & 13 deletions cli/src/generator/models/set_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ fn field_set_params(field: &dml::Field, args: &GenerateArgs) -> Vec<SetParam> {
if let Some(write_type) = args.write_filter(&scalar_field) {
for method in &write_type.methods {
let typ = method.type_tokens(quote!());

let prisma_value_converter = method.base_type.to_prisma_value(&format_ident!("value"), &method.arity());

let variant_name = format_ident!("{}{}", pascal_ident(&method.name), field_name_pascal);

let action = &method.action;
params.push(SetParam {
variant: quote!(#variant_name(#typ)),
Expand Down Expand Up @@ -176,31 +176,64 @@ fn field_set_params(field: &dml::Field, args: &GenerateArgs) -> Vec<SetParam> {
}

pub fn enum_definition(model: &dml::Model, args: &GenerateArgs) -> TokenStream {
let set_params = model
let (variants, into_pv_arms): (Vec<_>, Vec<_>) = model
.fields()
.map(|f| field_set_params(f, args))
.flatten()
.collect::<Vec<_>>();

let (variants, into_pv_arms): (Vec<_>, Vec<_>) = set_params
.iter()
.map(|p| (&p.variant, &p.into_pv_arm))
.flat_map(|f| field_set_params(f, args))
.map(|p| (p.variant, p.into_pv_arm))
.unzip();

let pcr = quote!(::prisma_client_rust);

let unchecked_enum = {
let (variants, into_pv_arms): (Vec<_>, Vec<_>) = model
.fields()
.filter(|f| f.is_scalar_field())
.map(|field| {
let field_name_pascal = pascal_ident(field.name());

let set_variant = format_ident!("Set{}", field_name_pascal);

let field_type = field.type_tokens(quote!());

(
quote!(#field_name_pascal(#field_type)),
quote! {
UncheckedSetParam::#field_name_pascal(value) => Self::#set_variant(value)
},
)
})
.unzip();

quote! {
#[derive(Clone)]
pub enum UncheckedSetParam {
#(#variants),*
}

impl From<UncheckedSetParam> for SetParam {
fn from(param: UncheckedSetParam) -> Self {
match param {
#(#into_pv_arms),*
}
}
}
}
};

quote! {
#[derive(Clone)]
pub enum SetParam {
#(#variants),*
}

impl Into<(String, #pcr::PrismaValue)> for SetParam {
fn into(self) -> (String, #pcr::PrismaValue) {
match self {
impl From<SetParam> for (String, #pcr::PrismaValue) {
fn from(param: SetParam) -> Self {
match param {
#(#into_pv_arms),*
}
}
}

#unchecked_enum
}
}
1 change: 1 addition & 0 deletions cli/src/generator/models/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub fn struct_definition(model: &dml::Model) -> TokenStream {
impl #pcr::ModelTypes for Types {
type Data = Data;
type Where = WhereParam;
type UncheckedSet = UncheckedSetParam;
type Set = SetParam;
type With = WithParam;
type OrderBy = OrderByParam;
Expand Down

0 comments on commit af7694a

Please sign in to comment.