Prisma - 部分匹配的查询列表

问题描述 投票:0回答:1

我的 prisma 模型有这 2 个字符串列表属性

  names     String[]
  phones    String[]  @unique

我想查询此表以及许多其他搜索参数,我想查找任何名称或任何电话包含给定值的所有记录。

这例如有效,但仅适用于与查询值完全匹配的手机

  if (params.query) {
    whereCondition = {
      phones: {
        has: params.query,
      },
    };
  }

我认为可以编写原始sql,但希望避免这种情况,并执行上述操作以及建立“where 条件”的各种其他标准

postgresql next.js prisma
1个回答
0
投票

无需分解原始 SQL,这是受支持的场景..

以下是匹配部分电子邮件或部分电话号码的示例。我改编自“官方文档”所以一定要检查一下。您没有提供完整的架构,所以我在这里做出一些假设:

const phoneNumber = '123-456';

const users = await prisma.user.findMany({
  where: {
    // Match either email or phone number
    OR: [
      {
        email: {
          // Match emails ending with the value
          endsWith: 'prisma.io',
          // use case-insensitive search
          mode: 'insensitive',
        },
      },
      {
        phones: {
          // Match phone numbers that include the string
          contains: {
            phoneNumber,
          },
        },
      },
    ],
  },
});

© www.soinside.com 2019 - 2024. All rights reserved.