prismaprisma-graphql

how to use OR array inside AND prisma


ièm trying to use OR inside AND by passing an array of object to OR

city=[{city:berlin},{city:newyork}]
brand=[{brand:bmw},{brand:nissan}]
  filter: async(_,{city ,brand,model}) =>{
  const ads =await Client.Ads.findMany({where:{
      AND:[
        OR:city
      ,
      OR:brand
      ]
        
      
  }})



Solution

  • Considering this schema file:

    generator client {
      provider        = "prisma-client-js"
    }
    
    datasource db {
      provider = "postgresql"
      url      = env("DATABASE_URL")
    }
    
    model test {
      id        Int      @id @default(autoincrement())
      name      String
      updatedAt DateTime @updatedAt
    }
    

    You can nest AND and OR operators like this:

      await prisma.test.findMany({
        where: {
          AND: [
            {
              OR: [
                {
                  name: 'test1',
                },
                {
                  name: 'test2',
                },
              ],
            },
            {},
          ],
        },
      });
    

    By default all the fields in the where clause are combined with AND operator so you might only need to use the OR operator.

    For reference: AND and OR Prisma Documentation.