Vinit Khandelwal
Vinit Khandelwal

Reputation: 627

Prisma findMany with 2 fields from array

I am using Prisma and I have two field values that I want to search on

const requests = [{ id, cid }, { id, cid }, { id, cid }];

I want to search on both fields together but for many.

for(request of requests) {
    prisma.user.findFirst({
        where: {
            id: request.id,
            cid: request.cid
        }
    });
}

Basically I want to do the above query but for many pairs at once. How to do that?

Upvotes: 2

Views: 5982

Answers (2)

Richard
Richard

Reputation: 26

I recently encounter this problem and here is my solution:

const conditionArray = [{cd1: 'a', cd2: 'b'}, {cd1: 'c', cd2: 'd'}];
await prisma.table.findMany({
where:{
  OR: conditionArray.map(c => ({AND: [{cd1: c.cd1}, {cd2: c.cd2}]})
},
select {...what_you_want_to_take}
})

My response is late but I hope it will help others

Upvotes: 1

Nurul Sundarani
Nurul Sundarani

Reputation: 7598

You are looking for in operator.


const requests = [{ id, cid }, { id, cid }, { id, cid }];

const ids = requests.map((request)=>request.id);
const cids = requests.map((request)=>request.cid);

const getUser = await prisma.user.findMany({
  where: {
    id: { in: ids },
    cid: { in: cids }
  },
})

Here is a link to in operator documentation

Upvotes: 6

Related Questions