Prisma에서는 데이터베이스의 여러 테이블에서 관련된 정보를 한번에 가져오는 기능을 지원함
include 옵션은 쿼리 응답에 join되는 테이블의 일부 필드를 포함해줄 수 있게 함
select를 중첩해서 relation field의 특정 값을 가져올 수 있게 함
가령 include문을 사용해 특정한 field를 join할 수 있음
const user = await prisma.user.findFirst({
include: {
posts: true,
},
})
/*
{
id: 19,
name: null,
email: 'emma@prisma.io',
profileViews: 0,
role: 'USER',
coinflips: [],
posts: [
{
id: 20,
title: 'My first post',
published: true,
authorId: 19,
comments: null,
views: 0,
likes: 0
},
{
id: 21,
title: 'How to make cookies',
published: true,
authorId: 19,
comments: null,
views: 0,
likes: 0
}
]
}
*/
const user = await prisma.user.findFirst({
include: {
posts: {
include: {
categories: true,
},
},
},
})
/*
{
"id": 40,
"name": "Yvette",
"email": "yvette@prisma.io",
"profileViews": 0,
"role": "USER",
"coinflips": [],
"testing": [],
"city": null,
"country": "Sweden",
"posts": [
{
"id": 66,
"title": "How to make an omelette",
"published": true,
"authorId": 40,
"comments": null,
"views": 0,
"likes": 0,
"categories": [
{
"id": 3,
"name": "Easy cooking"
}
]
},
{
"id": 67,
"title": "How to eat an omelette",
"published": true,
"authorId": 40,
"comments": null,
"views": 0,
"likes": 0,
"categories": []
}
]
}
*/
const user = await prisma.user.findFirst({
select: {
name: true,
posts: {
select: {
title: true,
},
},
},
})
/*
{
name: "Elsa",
posts: [ { title: 'My first post' }, { title: 'How to make cookies' } ]
}
*/
const user = await prisma.user.findFirst({
include: {
posts: {
select: {
title: true,
},
},
},
})
/*
"id": 1,
"name": null,
"email": "martina@prisma.io",
"profileViews": 0,
"role": "USER",
"coinflips": [],
"posts": [
{ "title": "How to grow salad" },
{ "title": "How to ride a horse" }
]
}
*/
// Invalid `prisma.user.findUnique()` invocation:
const user = await prisma.user.findFirst({
select: { // 같은 level에서 select, include 중복
email: true
}
include: { // 같은 level에서 select, include 중복
posts: {
select: {
title: true
}
}
},
})
// 아래와 같이 쿼리를 수정해야함
const user = await prisma.user.findFirst({
select: {
// This will work!
email: true,
posts: {
select: {
title: true,
},
},
},
})
const relationCount = await prisma.user.findMany({
include: {
_count: {
select: { posts: true },
},
},
})
/*
{ id: 1, _count: { posts: 3 } },
{ id: 2, _count: { posts: 2 } },
{ id: 3, _count: { posts: 2 } },
{ id: 4, _count: { posts: 0 } },
{ id: 5, _count: { posts: 0 } }
*/
const result = await prisma.user.findFirst({
select: {
posts: {
where: {
published: false,
},
orderBy: {
title: 'asc',
},
select: {
title: true,
},
},
},
})
const result = await prisma.user.create({
data: {
email: 'elsa@prisma.io',
name: 'Elsa Prisma',
posts: {
create: [
{ title: 'How to make an omelette' },
{ title: 'How to eat an omelette' },
],
},
},
include: {
posts: true, // 결과를 반환할 때 posts 값도 포함
},
})
// 다음과 같은 쿼리는 불가능함
const createMany = await prisma.user.createMany({
data: [
{
name: 'Yewande',
email: 'yewande@prisma.io',
posts: {
// 게시물 생성 불가
},
},
{
name: 'Noor',
email: 'noor@prisma.io',
posts: {
// 게시물 생성 불가
},
},
],
})
const result = await prisma.user.create({
data: {
email: 'vlad@prisma.io',
posts: {
connect: [{ id: 8 }, { id: 9 }, { id: 10 }],
},
},
include: {
posts: true, // Include all posts in the returned object
},
})
const result = await prisma.post.create({
data: {
title: 'How to make croissants',
author: {
connectOrCreate: {
where: {
email: 'viola@prisma.io',
},
create: {
email: 'viola@prisma.io',
name: 'Viola',
},
},
},
},
include: {
author: true,
},
})
const result = await prisma.user.update({
where: {
id: 16,
},
data: {
posts: {
disconnect: [{ id: 12 }, { id: 19 }],
},
},
include: {
posts: true,
},
})
const result = await prisma.post.update({
where: {
id: 23,
},
data: {
author: {
disconnect: true,
},
},
include: {
author: true,
},
})
const result = await prisma.user.update({
where: {
id: 16,
},
data: {
posts: {
set: [],
},
},
include: {
posts: true,
},
})
const result = await prisma.user.update({
where: {
id: 11,
},
data: {
posts: {
deleteMany: {},
},
},
include: {
posts: true,
},
})
const result = await prisma.user.update({
where: {
id: 11,
},
data: {
posts: {
deleteMany: {
published: false,
},
},
},
include: {
posts: true,
},
})
const result = await prisma.user.update({
where: {
id: 6,
},
data: {
posts: {
deleteMany: [{ id: 7 }],
},
},
include: {
posts: true,
},
})
const result = await prisma.user.update({
where: {
id: 6,
},
data: {
posts: {
updateMany: {
where: {
published: true,
},
data: {
published: false,
},
},
},
},
include: {
posts: true,
},
})
const result = await prisma.user.update({
where: {
id: 6,
},
data: {
posts: {
update: {
where: {
id: 9,
},
data: {
title: 'My updated title',
},
},
},
},
include: {
posts: true,
},
})
const result = await prisma.post.update({
where: {
id: 6,
},
data: {
author: {
upsert: {
create: {
email: 'bob@prisma.io',
name: 'Bob the New User',
},
update: {
email: 'bob@prisma.io',
name: 'Bob the existing user',
},
},
},
},
include: {
author: true,
},
})
// 조회수가 100회 이상인 게시물이 없고, 모든 게시물의 좋아요는 50개 이하
const users = await prisma.user.findMany({
where: {
posts: {
none: {
views: {
gt: 100,
},
},
every: {
likes: {
lte: 50,
},
},
},
},
include: {
posts: true,
},
})
// 이름이 Bob이 아니며, 40세 이상인 모든 유저를 출력하는 쿼리
const users = await prisma.post.findMany({
where: {
author: {
isNot: {
name: 'Bob',
},
is: {
age: {
gt: 40,
},
},
},
},
include: {
author: true,
},
})
const postsWithNoAuthor = await prisma.post.findMany({
where: {
author: null, // or author: { }
},
include: {
author: true,
},
})
출처:
https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries