LoginSignup
0
1

PrismaでfindUniqueじゃなくてfindUniqueManyしたい

Last updated at Posted at 2023-09-07

id ではなく unique key でまとめてデータを取得したいこと、あると思います。

unique key で取得するには findUnique というAPIが用意されています。
しかし、まとめて取得する findUniqueMany みたいなAPIはありません。

やりたい場合は、以下のように findManywhere or に配列を渡すことで対応できます。

prisma.my_column.findMany({
  where: {
    OR: myArray.map(item => ({
      unique_key_1: item.unique_key_1,
      unique_key_2: item.unique_key_2,
      unique_key_3: item.unique_key_3,
    }))
  }
})

追記

他にも以下の書き方でも実現できました

prisma.my_column.findMany({
  where: {
    unique_key_1: {
      in: myArray.map(item => item.unique_key_1),
    },
    unique_key_2: {
      in: myArray.map(item => item.unique_key_2),
    },
    unique_key_3: {
      in: myArray.map(item => item.unique_key_3),
    },
  }
})

まれによく使うので、覚えておくと役立ちます。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1