LoginSignup
19
12

More than 1 year has passed since last update.

Prisma モデルクエリチートシート

Last updated at Posted at 2022-11-30

モデルクエリ一覧

関数 役割 返り値
findUniue 1件のレコードを取得する。見つからない場合はnullを返す 型付きオブジェクト
findUniqueOrThrow 1件のレコードを取得する。見つからない場合は例外を投げる 型付きオブジェクト
findFirst 1件のレコードを取得する。見つからない場合はnullを返す 型付きオブジェクト
findFirstOrThrow 1件のレコードを取得する。見つからない場合は例外を投げる 型付きオブジェクト
findMany 複数件のレコードを取得する。見つからない場合は空配列を返す 型付きオブジェクトの配列
create 1件のレコードを作成する 型付きオブジェクト
update 1件のレコードを更新する 型付きオブジェクト
upsert 1件のレコードを更新する。ない場合はレコードを追加する 型付きオブジェクト
delete 1件のレコードを削除する 型付きオブジェクト
createMany トランザクションで複数件のレコードを作成する 作成されたレコードのカウント
updateMany トランザクションで複数件のレコードを更新する 更新されたレコードのカウント
deleteMany トランザクションで複数件のレコードを削除する 削除されたレコードのカウント
count レコードの件数を取得する レコードの件数
aggregate tableの数値フィールの集約、要約情報。欲しい情報を指定する。 集計値
groupBy 指定条件を付けたtableの数値フィールの集約、要約情報。欲しい情報を指定する。 集計値

参考

モデルクエリオプション

モデルクエリのオプションには以下の種類があります。

  • select
  • include
  • where
  • orderBy
  • distinct

select

selectはモデルの返り値のフィールドを指定することができます。

index.d.tsで型の上書きとセットで使います。

指定はbooleanで行います。

const result = await prisma.user.findUnique({
  where: { id: 1 },
  select: {
    name: true,
    profileViews: true,
  },
})

// {
//   name: "Alice",
//   profileViews: 0
// }

include

includeはモデルの返り値に関係する別テーブルのレコードを含めるように指定することができます。

const users = await prisma.user.findMany({
  include: {
    posts: true, // Returns all fields for all posts
    profile: true, // Returns all Profile fields
  },
})

const user = await prisma.user.create({
  data: {
    email: 'alice@prisma.io',
    posts: {
      create: [
        { title: 'This is my first post' },
        { title: 'Here comes a second post' },
      ],
    },
  },
  include: { posts: true }, // Returns all fields for all posts
})

where

whereは検索条件を指定することができます。

filter条件と演算子

クエリ 役割 参考
equals 等しい name: {equals: 'Eleanor'}、なくてもいい
not 等しくない name: {not: 'Eleanor'}
in いずれか name: {in: ['Alice', 'Bob']}
notIn いずれでもない name: {notIn: ['Alice', 'Bob']}
lt より小さい age: {lt: 18}
lte 以下 age: {lte: 18}
gt より大きい age: {gt: 18}
gte 以上 age: {gte: 18}
contains 部分一致 name: {contains: 'Eleanor'}
search 全文検索 name: {search: 'Eleanor'} stringに対して使用可。実験機能。
mode 全文検索モード name: {search: 'Eleanor', mode: 'insensitive'} searchのオプション
startsWith 前方一致 name: {startsWith: 'Eleanor'}
endsWith 後方一致 name: {endsWith: 'Eleanor'}
AND 複数条件 AND: [{content: {contains: 'Prisma'}},{published: {equals: false}},] なくてもいい
OR 複数条件 OR: [{content: {contains: 'Prisma'}},{published: {equals: false}},] なくてもいい
NOT 複数条件 NOT: [{content: {contains: 'Prisma'}},{published: {equals: false}},] なくてもいい

関係filter

クエリ 役割 参考
some 1つ以上 some: {content: {contains: "Prisma"}} defaultの指定条件。なくていい
every 全て every: {content: {contains: "Prisma"}}
none 一致しないすべて post: {none: {}} postがないすべて
post: {none: {published: true}}published: falseのpostをすべて
is 一致するすべて is: {name: "Bob"}}
isNot 一致しないすべて isNot: {name: "Bob"}}

orderBy

orderByはソート条件を指定することができます。

const users = await prisma.user.findMany({
  orderBy: {
    name: 'asc',
    email: 'desc',
  },
})

distinct

distinctは重複を削除することができます。selectと併用して特定の組み合わせを抽出するのに使われます。

const users = await prisma.user.findMany({
  select: {
    name: true,
  },
  distinct: ['name'],
})

参照

19
12
1

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
19
12