Repository: Brendonovich/prisma-client-rust (language: rust)

Issue:
Combine query mode with operators
I am trying to do a case insensitive search based on two different fields, however it doesn't seem to be working also can't find any docs on this. I tried the following but the search is still case sensitive:

```rust
db
    .candidate()
    .find_many(vec![or!(
            and!(
                candidate::name::mode(crate::prisma::_prisma::QueryMode::Insensitive),
                candidate::name::contains(search.clone()),
            ),
            and!(
                candidate::email::mode(crate::prisma::_prisma::QueryMode::Insensitive),
                candidate::email::contains(search.clone()),
            )
    )])
    .take(query.limit)
    .skip(query.offset)
    .exec();
```

It works fine if I just do it against one field with out the `or`s and `and`s.


Discussion / hints:
Thought I fixed this earlier but didn't apply it to `and`. Try using `rev = "e44cae1e680e5b0a8f73f68fc99fc6c40066fb83"`.
The above code still does case sensitive search on `rev = "e44cae1e680e5b0a8f73f68fc99fc6c40066fb83"`
Did you use that version for both the CLI and library and then regenerate the client? I tried it with Postgres on my machine and it works fine.
I changed my `Cargo.toml` to:
```toml
prisma-client-rust = { git = "https://github.com/Brendonovich/prisma-client-rust", rev = "e44cae1e680e5b0a8f73f68fc99fc6c40066fb83" }
prisma-client-rust-cli = { git = "https://github.com/Brendonovich/prisma-client-rust", rev = "e44cae1e680e5b0a8f73f68fc99fc6c40066fb83" }
```

then ran the generate command. My actual code looks like this in case it makes a difference:

```rust
pub async fn find(
    Extension(db): Extension<Arc<PrismaClient>>,
    Validated(Query(query)): Validated<Query<FindQuery>>,
) -> ApiResult<FindResponse> {
    let mut params = vec![];
    if let Some(search) = &query.search {
        params.push(or!(
            and!(
                candidate::name::mode(crate::prisma::_prisma::QueryMode::Insensitive),
                candidate::name::contains(search.clone()),
            ),
            and!(
                candidate::email::mode(crate::prisma::_prisma::QueryMode::Insensitive),
                candidate::email::contains(search.clone()),
            )
        ));
    }

    let candidates = db
        .candidate()
        .find_many(params.clone())
        .take(query.limit)
        .skip(query.offset)
        .exec();

    let count = db.candidate().count(params).exec();
    let (candidates, count) = try_join!(candidates, count)?;

    Ok(Json(FindResponse {
        records: candidates,
        query,
        total_records: count,
    }))
}
```
note that I clone the params vec.


Fix patch:
diff --git a/cli/src/generator/models/mod.rs b/cli/src/generator/models/mod.rs
index 11a5c885..00bb449d 100644
--- a/cli/src/generator/models/mod.rs
+++ b/cli/src/generator/models/mod.rs
@@ -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()
+                        )
                     )
                 },
             };
diff --git a/src/lib.rs b/src/lib.rs
index 4c3dad4d..08283ef4 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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);
@@ -129,5 +131,5 @@ pub fn merged_object(elements: Vec<(String, PrismaValue)>) -> PrismaValue {
         }
     }
 
-    PrismaValue::Object(merged.into_iter().collect())
+    merged.into_iter().collect()
 }
diff --git a/src/operator.rs b/src/operator.rs
index 6fc27a62..efabe29a 100644
--- a/src/operator.rs
+++ b/src/operator.rs
@@ -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()
 }
-
diff --git a/src/queries/count.rs b/src/queries/count.rs
index 07ab0b67..dfddf0df 100644
--- a/src/queries/count.rs
+++ b/src/queries/count.rs
@@ -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};
 
@@ -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(),
-                ),
+                )),
             );
         }
 
diff --git a/src/queries/create.rs b/src/queries/create.rs
index 4218fbee..2b932da3 100644
--- a/src/queries/create.rs
+++ b/src/queries/create.rs
@@ -6,7 +6,7 @@ use serde::de::DeserializeOwned;
 
 use crate::{
     include::{Include, IncludeType},
-    merged_object,
+    merge_fields,
     select::{Select, SelectType},
     BatchQuery,
 };
@@ -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
diff --git a/src/queries/create_many.rs b/src/queries/create_many.rs
index 4e7314cf..bceeba1f 100644
--- a/src/queries/create_many.rs
+++ b/src/queries/create_many.rs
@@ -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};
 
@@ -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(),
             ),
         );
diff --git a/src/queries/delete_many.rs b/src/queries/delete_many.rs
index 6d23a7fe..75a508ae 100644
--- a/src/queries/delete_many.rs
+++ b/src/queries/delete_many.rs
@@ -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};
 
@@ -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(),
-                ),
+                )),
             );
         }
 
diff --git a/src/queries/find_first.rs b/src/queries/find_first.rs
index 5d6ed9b9..e6d9ee4e 100644
--- a/src/queries/find_first.rs
+++ b/src/queries/find_first.rs
@@ -6,7 +6,7 @@ use serde::de::DeserializeOwned;
 
 use crate::{
     include::{Include, IncludeType},
-    merged_object,
+    merge_fields,
     select::{Select, SelectType},
     BatchQuery,
 };
@@ -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(),
-                ),
+                )),
             );
         }
 
diff --git a/src/queries/find_many.rs b/src/queries/find_many.rs
index 312a9506..c3b8432f 100644
--- a/src/queries/find_many.rs
+++ b/src/queries/find_many.rs
@@ -6,7 +6,7 @@ use serde::de::DeserializeOwned;
 
 use crate::{
     include::{Include, IncludeType},
-    merged_object,
+    merge_fields,
     select::{Select, SelectType},
     BatchQuery,
 };
@@ -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(),
-                ),
+                )),
             );
         }
 
diff --git a/src/queries/update.rs b/src/queries/update.rs
index 5e32a821..dcdd03d4 100644
--- a/src/queries/update.rs
+++ b/src/queries/update.rs
@@ -6,7 +6,7 @@ use serde::de::DeserializeOwned;
 
 use crate::{
     include::{Include, IncludeType},
-    merged_object,
+    merge_fields,
     select::{Select, SelectType},
     BatchQuery,
 };
@@ -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
diff --git a/src/queries/update_many.rs b/src/queries/update_many.rs
index 33dc458e..b0c34ba7 100644
--- a/src/queries/update_many.rs
+++ b/src/queries/update_many.rs
@@ -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};
 
@@ -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(),
-                ),
+                )),
             );
         }
 


Test patch: