Skip to content

Commit

Permalink
Allow scalar operations in update many (#326)
Browse files Browse the repository at this point in the history
* restrict set params to corresponding queries

* scalar ops in unchecked

* don't generate CreateUnchecked if unsupported

* unify enum name creation
  • Loading branch information
Brendonovich committed Apr 30, 2023
1 parent 39c03fc commit 4eba61c
Show file tree
Hide file tree
Showing 25 changed files with 448 additions and 151 deletions.
8 changes: 4 additions & 4 deletions crates/cli/src/generator/composite_types/order_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn enum_definition(comp_type: CompositeTypeWalker) -> TokenStream {
quote!(#field_name_pascal(Vec<super::#composite_type_snake::OrderByParam>)),
quote! {
Self::#field_name_pascal(params) => (
#field_name_snake::NAME.to_string(),
#field_name_snake::NAME,
#pcr::PrismaValue::Object(
params
.into_iter()
Expand All @@ -42,7 +42,7 @@ pub fn enum_definition(comp_type: CompositeTypeWalker) -> TokenStream {
quote!(#field_name_pascal(#pcr::Direction)),
quote! {
Self::#field_name_pascal(direction) => (
#field_name_snake::NAME.to_string(),
#field_name_snake::NAME,
#pcr::PrismaValue::String(direction.to_string())
)
},
Expand All @@ -57,8 +57,8 @@ pub fn enum_definition(comp_type: CompositeTypeWalker) -> TokenStream {
#(#variants),*
}

impl Into<(String, #pcr::PrismaValue)> for OrderByParam {
fn into(self) -> (String, #pcr::PrismaValue) {
impl Into<(&'static str, #pcr::PrismaValue)> for OrderByParam {
fn into(self) -> (&'static str, #pcr::PrismaValue) {
match self {
#(#into_pv_arms),*
}
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/generator/composite_types/set_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn enum_definition(comp_type: CompositeTypeWalker, module_path: &TokenStream
quote!(#variant_name(#field_type)),
quote! {
SetParam::#variant_name(value) => (
#field_name_snake::NAME.to_string(),
#field_name_snake::NAME,
#converter
)
},
Expand Down
12 changes: 7 additions & 5 deletions crates/cli/src/generator/models/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub fn create_fn(model: ModelWalker) -> Option<TokenStream> {
}

pub fn create_unchecked_fn(model: ModelWalker) -> Option<TokenStream> {
required_fields(model)?;

let (names, types): (Vec<_>, Vec<_>) = model
.scalar_fields()
.filter_map(|field| {
Expand All @@ -58,12 +60,12 @@ pub fn create_unchecked_fn(model: ModelWalker) -> Option<TokenStream> {
.unzip();

Some(quote! {
pub fn create_unchecked(self, #(#names: #types,)* mut _params: Vec<UncheckedSetParam>) -> CreateQuery<'a> {
pub fn create_unchecked(self, #(#names: #types,)* mut _params: Vec<UncheckedSetParam>) -> CreateUncheckedQuery<'a> {
_params.extend([
#(#names::set(#names)),*
]);

CreateQuery::new(
CreateUncheckedQuery::new(
self.client,
_params.into_iter().map(Into::into).collect()
)
Expand All @@ -78,7 +80,7 @@ pub fn create_many_fn(model: ModelWalker) -> Option<TokenStream> {
.then(|| {
quote! {
pub fn create_many(self, data: Vec<CreateUnchecked>) -> CreateManyQuery<'a> {
let data = data.into_iter().map(CreateUnchecked::to_unchecked_params).collect();
let data = data.into_iter().map(CreateUnchecked::to_params).collect();

CreateManyQuery::new(
self.client,
Expand Down Expand Up @@ -180,8 +182,8 @@ pub fn struct_definition(model: ModelWalker, args: &GenerateArgs) -> TokenStream
)
}

pub fn update_unchecked(self, _where: UniqueWhereParam, _params: Vec<UncheckedSetParam>) -> UpdateQuery<'a> {
UpdateQuery::new(
pub fn update_unchecked(self, _where: UniqueWhereParam, _params: Vec<UncheckedSetParam>) -> UpdateUncheckedQuery<'a> {
UpdateUncheckedQuery::new(
self.client,
_where.into(),
_params.into_iter().map(Into::into).collect(),
Expand Down
14 changes: 4 additions & 10 deletions crates/cli/src/generator/models/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::generator::prelude::*;
use super::required_fields;

fn create_unchecked(model: ModelWalker) -> Option<TokenStream> {
required_fields(model)?;

let model_name_snake = snake_ident(model.name());

let (names, types): (Vec<_>, Vec<_>) = model
Expand Down Expand Up @@ -42,23 +44,15 @@ fn create_unchecked(model: ModelWalker) -> Option<TokenStream> {
}

impl CreateUnchecked {
pub fn to_query<'a>(self, client: &'a PrismaClient) -> CreateQuery<'a> {
pub fn to_query<'a>(self, client: &'a PrismaClient) -> CreateUncheckedQuery<'a> {
client.#model_name_snake()
.create_unchecked(
#(self.#names,)*
self._params
)
}

pub fn to_params(mut self) -> Vec<SetParam> {
self._params.extend([
#(#names::set(self.#names)),*
]);

self._params.into_iter().map(Into::into).collect()
}

pub fn to_unchecked_params(mut self) -> Vec<UncheckedSetParam> {
pub fn to_params(mut self) -> Vec<UncheckedSetParam> {
self._params.extend([
#(#names::set(self.#names)),*
]);
Expand Down
41 changes: 31 additions & 10 deletions crates/cli/src/generator/models/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use prisma_client_rust_sdk::prisma::{
psl::parser_database::ScalarFieldType,
};

use crate::generator::prelude::*;
use crate::generator::{prelude::*, write_params};

use super::{include, order_by, pagination, select, where_params::Variant, with_params};

Expand Down Expand Up @@ -543,7 +543,8 @@ pub fn module(
});

let write_fns = args.write_param(scalar_field).map(|write_param| {
let param_enum = format_ident!("{}Param", &write_param.name);
let param_enum = write_params::enum_name(write_param);
let param_enum_path = quote!(_prisma::write_params::#param_enum);

let other_fns = write_param
.methods
Expand All @@ -557,32 +558,52 @@ pub fn module(
let typ = method.type_tokens(&quote!(), &args.schema.db);

Some(quote! {
pub fn #method_name_snake(value: #typ) -> SetParam {
SetParam::#field_name_pascal(_prisma::write_params::#param_enum::#method_name_pascal(value))
pub fn #method_name_snake<T: From<UpdateOperation>>(value: #typ) -> T {
UpdateOperation(#param_enum_path::#method_name_pascal(value)).into()
}
})
})
.collect::<TokenStream>();

let impl_from_for_set_param = (!scalar_field.is_in_required_relation()).then(|| {
quote! {
impl From<UpdateOperation> for SetParam {
fn from(UpdateOperation(v): UpdateOperation) -> Self {
Self::#field_name_pascal(v)
}
}

impl From<Set> for SetParam {
fn from(Set(v): Set) -> Self {
Self::#field_name_pascal(#param_enum_path::Set(v))
}
}
}
});

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

impl From<Set> for SetParam {
fn from(Set(v): Set) -> Self {
Self::#field_name_pascal(_prisma::write_params::#param_enum::Set(v))
}
}
#impl_from_for_set_param

impl From<Set> for UncheckedSetParam {
fn from(Set(v): Set) -> Self {
Self::#field_name_pascal(v)
Self::#field_name_pascal(#param_enum_path::Set(v))
}
}

pub fn set<T: From<Set>>(value: #field_type) -> T {
Set(value).into()
}

pub struct UpdateOperation(pub #param_enum_path);

impl From<UpdateOperation> for UncheckedSetParam {
fn from(UpdateOperation(v): UpdateOperation) -> Self {
Self::#field_name_pascal(v)
}
}

#other_fns
}
});
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/generator/models/include_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ fn model_macro<'a>(
let specta_rename = cfg!(feature = "specta").then(|| {
quote!(#[specta(rename_from_path = #module_path::#model_name_snake::#field_name_snake::NAME)])
});

quote! {
#specta_rename
pub #field_name_snake: #field_type
Expand Down
4 changes: 3 additions & 1 deletion crates/cli/src/generator/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub fn modules(args: &GenerateArgs, module_path: &TokenStream) -> Vec<TokenStrea
.into_iter()
.map(#pcr::WhereInput::serialize)
.map(Into::into)
.map(|v| vec![v])
.map(|(k, v)| vec![(k.to_string(), v)])
.map(#pcr::PrismaValue::Object)
.collect()
)
Expand Down Expand Up @@ -324,11 +324,13 @@ pub fn modules(args: &GenerateArgs, module_path: &TokenStream) -> Vec<TokenStrea

pub type CountQuery<'a> = #pcr::Count<'a, Types>;
pub type CreateQuery<'a> = #pcr::Create<'a, Types>;
pub type CreateUncheckedQuery<'a> = #pcr::CreateUnchecked<'a, Types>;
pub type CreateManyQuery<'a> = #pcr::CreateMany<'a, Types>;
pub type FindUniqueQuery<'a> = #pcr::FindUnique<'a, Types>;
pub type FindManyQuery<'a> = #pcr::FindMany<'a, Types>;
pub type FindFirstQuery<'a> = #pcr::FindFirst<'a, Types>;
pub type UpdateQuery<'a> = #pcr::Update<'a, Types>;
pub type UpdateUncheckedQuery<'a> = #pcr::UpdateUnchecked<'a, Types>;
pub type UpdateManyQuery<'a> = #pcr::UpdateMany<'a, Types>;
pub type UpsertQuery<'a> = #pcr::Upsert<'a, Types>;
pub type DeleteQuery<'a> = #pcr::Delete<'a, Types>;
Expand Down
8 changes: 4 additions & 4 deletions crates/cli/src/generator/models/order_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn enum_definition(model: ModelWalker) -> TokenStream {
quote!(#field_name_pascal(#pcr::Direction)),
quote! {
Self::#field_name_pascal(direction) => (
#field_name_snake::NAME.to_string(),
#field_name_snake::NAME,
#pcr::PrismaValue::String(direction.to_string())
)
},
Expand All @@ -43,7 +43,7 @@ pub fn enum_definition(model: ModelWalker) -> TokenStream {
quote!(#field_name_pascal(Vec<super::#composite_type_snake::OrderByParam>)),
quote! {
Self::#field_name_pascal(params) => (
#field_name_snake::NAME.to_string(),
#field_name_snake::NAME,
#pcr::PrismaValue::Object(
params
.into_iter()
Expand All @@ -65,8 +65,8 @@ pub fn enum_definition(model: ModelWalker) -> TokenStream {
#(#variants),*
}

impl Into<(String, #pcr::PrismaValue)> for OrderByParam {
fn into(self) -> (String, #pcr::PrismaValue) {
impl Into<(&'static str, #pcr::PrismaValue)> for OrderByParam {
fn into(self) -> (&'static str, #pcr::PrismaValue) {
match self {
#(#into_pv_arms),*
}
Expand Down

1 comment on commit 4eba61c

@vercel
Copy link

@vercel vercel bot commented on 4eba61c Apr 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.