'How do I write a Prisma findFirst where clause that OR's two AND's -- or how does prisma treat undefined's?

I am using Prisma

I have a postgres table I want to search, finding only the record that matches where (id and accessKey) or (id and ownerId)

It keeps finding where (id) if the accessKey or ownerId are undefined

I've found that if I use impossible strings then the search works

I can't find the appropriate answer in the prisma docs so I thought I'd ask here

  return await db.attendee.findFirst({
    where: {
      OR: [
        { AND: { id, ownerId } },
        { AND: { id, accessKey } },
      ]
    }
  })

it makes sense that if accessKey or ownerId are undefined that they then effectively disappear from the query

I used this and it worked -- the values are guid's so they will never match, but if my secret phrase got loose (it's in the repo, not in an env var) then that would be an exploit akin to sql-injection...

  const userId = context?.currentUser?.sub || `can't touch this`;
  const accessKey = vaccessAuth?.accessKey || `can't touch this`;

What might be more the "prisma-way" ?



Solution 1:[1]

From what I understand, we can phrase your problem like this (please correct me if I'm wrong).

Find the first attendee where:
id field matches AND ( accessKey OR ownerId field matches)

Try this:

return await prisma.attendee.findFirst({
  where: {
    OR: [
      { ownerId: ownerIdValue },
      { accessKey: accessKeyValue }
    ],
    AND: { id: idValue }
  }
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jonschlinkert