Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge and operator fields #179

Merged
merged 1 commit into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions cli/src/generator/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,13 @@ pub fn generate(args: &GenerateArgs, module_path: TokenStream) -> Vec<TokenStrea
},
false => quote! {
#pcr::SerializedWhereValue::Object(
value
.into_iter()
.map(Into::<#pcr::SerializedWhere>::into)
.map(Into::into)
.collect()
::prisma_client_rust::merge_fields(
value
.into_iter()
.map(Into::<#pcr::SerializedWhere>::into)
.map(Into::into)
.collect()
)
)
},
};
Expand Down
8 changes: 5 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,14 @@ macro_rules! or {
};
}

pub type ObjectFields = Vec<(String, PrismaValue)>;

/// Creates a PrismaValue::Object from a list of key-value pairs.
/// If a key has multiple values that are PrismaValue::Objects, they will be merged.
pub fn merged_object(elements: Vec<(String, PrismaValue)>) -> PrismaValue {
pub fn merge_fields(fields: ObjectFields) -> ObjectFields {
let mut merged = HashMap::new();

for el in elements {
for el in fields {
match (merged.get_mut(&el.0), el.1) {
(Some(PrismaValue::Object(existing)), PrismaValue::Object(incoming)) => {
existing.extend(incoming);
Expand All @@ -129,5 +131,5 @@ pub fn merged_object(elements: Vec<(String, PrismaValue)>) -> PrismaValue {
}
}

PrismaValue::Object(merged.into_iter().collect())
merged.into_iter().collect()
}
1 change: 0 additions & 1 deletion src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ pub fn and<T: From<Operator<T>>>(params: Vec<T>) -> T {
pub fn or<T: From<Operator<T>>>(params: Vec<T>) -> T {
Operator::Or(params).into()
}

6 changes: 3 additions & 3 deletions src/queries/count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use prisma_models::PrismaValue;
use query_core::{Operation, Selection};
use serde::Deserialize;

use crate::{merged_object, BatchQuery, SerializedWhere};
use crate::{merge_fields, BatchQuery, SerializedWhere};

use super::{QueryContext, QueryInfo};

Expand Down Expand Up @@ -62,13 +62,13 @@ where
if self.where_params.len() > 0 {
selection.push_argument(
"where",
merged_object(
PrismaValue::Object(merge_fields(
self.where_params
.into_iter()
.map(Into::<SerializedWhere>::into)
.map(|s| (s.field, s.value.into()))
.collect(),
),
)),
);
}

Expand Down
6 changes: 4 additions & 2 deletions src/queries/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::de::DeserializeOwned;

use crate::{
include::{Include, IncludeType},
merged_object,
merge_fields,
select::{Select, SelectType},
BatchQuery,
};
Expand Down Expand Up @@ -54,7 +54,9 @@ where

selection.push_argument(
"data",
merged_object(set_params.into_iter().map(Into::into).collect()),
PrismaValue::Object(merge_fields(
set_params.into_iter().map(Into::into).collect(),
)),
);

selection
Expand Down
8 changes: 6 additions & 2 deletions src/queries/create_many.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use prisma_models::PrismaValue;
use query_core::{Operation, Selection, SelectionBuilder};

use crate::{merged_object, BatchQuery, BatchResult};
use crate::{merge_fields, BatchQuery, BatchResult};

use super::{QueryContext, QueryInfo};

Expand Down Expand Up @@ -48,7 +48,11 @@ where
PrismaValue::List(
set_params
.into_iter()
.map(|fields| merged_object(fields.into_iter().map(Into::into).collect()))
.map(|fields| {
PrismaValue::Object(merge_fields(
fields.into_iter().map(Into::into).collect(),
))
})
.collect(),
),
);
Expand Down
7 changes: 4 additions & 3 deletions src/queries/delete_many.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use prisma_models::PrismaValue;
use query_core::{Operation, Selection};

use crate::{merged_object, BatchQuery, BatchResult};
use crate::{merge_fields, BatchQuery, BatchResult};

use super::{QueryContext, QueryInfo, SerializedWhere};

Expand Down Expand Up @@ -33,13 +34,13 @@ where
if self.where_params.len() > 0 {
selection.push_argument(
"where",
merged_object(
PrismaValue::Object(merge_fields(
self.where_params
.into_iter()
.map(Into::<SerializedWhere>::into)
.map(|s| (s.field, s.value.into()))
.collect(),
),
)),
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/queries/find_first.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::de::DeserializeOwned;

use crate::{
include::{Include, IncludeType},
merged_object,
merge_fields,
select::{Select, SelectType},
BatchQuery,
};
Expand Down Expand Up @@ -94,13 +94,13 @@ where
if where_params.len() > 0 {
selection.push_argument(
"where",
merged_object(
PrismaValue::Object(merge_fields(
where_params
.into_iter()
.map(Into::<SerializedWhere>::into)
.map(|s| (s.field, s.value.into()))
.collect(),
),
)),
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/queries/find_many.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::de::DeserializeOwned;

use crate::{
include::{Include, IncludeType},
merged_object,
merge_fields,
select::{Select, SelectType},
BatchQuery,
};
Expand Down Expand Up @@ -97,13 +97,13 @@ where
if where_params.len() > 0 {
selection.push_argument(
"where",
merged_object(
PrismaValue::Object(merge_fields(
where_params
.into_iter()
.map(Into::<SerializedWhere>::into)
.map(|s| (s.field, s.value.into()))
.collect(),
),
)),
);
}

Expand Down
6 changes: 4 additions & 2 deletions src/queries/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::de::DeserializeOwned;

use crate::{
include::{Include, IncludeType},
merged_object,
merge_fields,
select::{Select, SelectType},
BatchQuery,
};
Expand Down Expand Up @@ -69,7 +69,9 @@ where

selection.push_argument(
"data",
merged_object(set_params.into_iter().map(Into::into).collect()),
PrismaValue::Object(merge_fields(
set_params.into_iter().map(Into::into).collect(),
)),
);

selection
Expand Down
10 changes: 6 additions & 4 deletions src/queries/update_many.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use prisma_models::PrismaValue;
use query_core::{Operation, Selection};

use crate::{merged_object, BatchQuery, BatchResult};
use crate::{merge_fields, BatchQuery, BatchResult};

use super::{QueryContext, QueryInfo, SerializedWhere};

Expand Down Expand Up @@ -41,19 +41,21 @@ where

selection.push_argument(
"data",
merged_object(self.set_params.into_iter().map(Into::into).collect()),
PrismaValue::Object(merge_fields(
self.set_params.into_iter().map(Into::into).collect(),
)),
);

if self.where_params.len() > 0 {
selection.push_argument(
"where",
merged_object(
PrismaValue::Object(merge_fields(
self.where_params
.into_iter()
.map(Into::<SerializedWhere>::into)
.map(|s| (s.field, s.value.into()))
.collect(),
),
)),
);
}

Expand Down