prisma-client-rust icon indicating copy to clipboard operation
prisma-client-rust copied to clipboard

Combine query mode with operators

Open affanshahid opened this issue 3 years ago • 1 comments

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:

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 ors and ands.

affanshahid avatar Oct 14 '22 13:10 affanshahid

Thought I fixed this earlier but didn't apply it to and. Try using rev = "e44cae1e680e5b0a8f73f68fc99fc6c40066fb83".

Brendonovich avatar Oct 15 '22 11:10 Brendonovich

The above code still does case sensitive search on rev = "e44cae1e680e5b0a8f73f68fc99fc6c40066fb83"

affanshahid avatar Oct 15 '22 22:10 affanshahid

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.

Brendonovich avatar Oct 16 '22 02:10 Brendonovich

I changed my Cargo.toml to:

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:

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.

affanshahid avatar Oct 16 '22 08:10 affanshahid

My bad, I pointed you to the wrong commit. Try rev = "be9440002417fcbbe1a7ab2967d626577468f4c4".

Brendonovich avatar Oct 17 '22 03:10 Brendonovich

Works, thanks!

affanshahid avatar Oct 18 '22 23:10 affanshahid