我如何编写一个 Prisma findFirst where 子句,将两个 AND 进行 OR 运算——或者 prisma 如何处理未定义的?

Al *_*lin 2 prisma

我正在使用 Prisma

我有一个要搜索的 postgres 表,只查找与 (id 和 accessKey) 或 (id 和 OwnerId) 匹配的记录

如果 accessKey 或 OwnerId 未定义,它会不断查找位置 (id)

我发现如果我使用不可能的字符串,那么搜索就会起作用

我在 prisma 文档中找不到合适的答案,所以我想我会在这里问

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

如果 accessKey 或 OwnerId 未定义,那么它们实际上会从查询中消失,这是有道理的

我使用了这个并且它起作用了——这些值是 guid 的,所以它们永远不会匹配,但是如果我的秘密短语松动了(它在存储库中,而不是在环境变量中),那么这将是类似于 sql 注入的漏洞。 。

  const userId = context?.currentUser?.sub || `can't touch this`;
  const accessKey = vaccessAuth?.accessKey || `can't touch this`;
Run Code Online (Sandbox Code Playgroud)

还有什么更“棱镜方式”呢?

Tas*_*mam 9

据我了解,我们可以这样表述你的问题(如果我错了,请纠正我)。

查找第一个attendee位置:
id字段匹配 AND(accessKeyORownerId字段匹配)

尝试这个:

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

Run Code Online (Sandbox Code Playgroud)