prisma client API, ์ฌ์ฉ ์์
index
- data
- where
- select
- connect
- include
data [create, update]
record๋ฅผ createํ๊ฑฐ๋ update ํ ๋ ๋ค์ด๊ฐ key์ value์ ๊ธฐ์ ํ ๋ ์ฌ์ฉ
const result = await client.product.create({
data: {
name : "",
price: 12,
description: "",
imgae: "",
},
});
where [read]
๊ฒ์ํ๊ณ ์ ํ๋ ์ปฌ๋ผ์ ์ง์ ํ๊ณ ์ ํ ๋ ์ฌ์ฉ
// ํน์ ์ปฌ๋ผ์ ๊ตฌ๋ถ์ง์ง ์๊ณ , ๋ชจ๋ product์ ์กฐํํ๊ฒ ๋ค.
// "where์ด ์์ผ๋ฉด ๋ชจ๋ ๋์์์ ์กฐํํ๊ฒ ๋ค" ๋ผ๋ ์๋ฏธ
const result = await client.product.findMany({});
// product์ ์ปฌ๋ผ ์ค์ id๊ฐ 2์ธ ๋ชจ๋ ๊ฒ์ ์กฐํํ๊ฒ ๋ค.
const result = await client.product.findMany({
where: {
id: 2
}
});
// product์ ์ปฌ๋ผ ์ค์ name์ด '๊ฐค๋ญ์'์ด๊ณ , price๊ฐ 123 ์ธ ๊ฒ์ ๋ชจ๋ ์กฐํํ๊ฒ ๋ค.
const result = await client.product.findMany({
where: {
name: '๊ฐค๋ญ์',
price: 123,
}
});
select [read]
where์์ ์ ํํ ๋ฐ์ดํฐ ์ค์์ ํน์ key๊ฐ๋ง ์ถ์ถํ๊ณ ์ ํ ๋ ์ฌ์ฉ
const result = await client.product.findMany({
where: {
id: 2,
},
select: {
name: true,
price: true,
}
});
// { name: "", price: "" }
connect [create, update]
create ๋๋ update๋ฅผ ํ ๋๋ง ์ฌ์ฉ ๊ฐ๋ฅํ๋ฉฐ, ๋ชฉ์ ์ ๋ ์ฝ๋๋ฅผ ์ถ๊ฐํ ๋ @relation ๊ด๊ณ์ ์๋ table์ ์ด๋ ํ ๊ฐ๊ณผ ์ฐ๊ฒฐ์ํฌ ๊ฒ์ธ์ง ์ง์ ํ๊ณ ์ ํ ๋ ์ฌ์ฉํ๋ค. ์๋ ์ฝ๋์์๋ product์ ์ถ๊ฐํ ๋, ํด๋น product์ user, user ์ค์์ id๊ฐ ์ด๋ค ๊ฒ์ธ์ง ์ง์ ํ๋ค. ์ด๊ฒ์ ์ฌ์ฉํ๋ ค๋ฉด ์์ชฝ์ field๊ฐ @relation์ผ๋ก ์ฐ๊ฒฐ๋์ด ์์ด์ผ ํ๋ค.
const result = await client.product.create({
data: {
name : "",
price: 12,
description: "",
imgae: "",
user: {
connect: {
id: 18,
}
}
},
});
์์ฑ๋ ๊ฒ์ ์กฐํํ๋ฉด, connect์์ ์ฐ๊ฒฐํ๋ userId: 18
์ด ์ ๋ณด๋ก ํฌํจ๋์ด ์๋ ๊ฒ์ ํ์ธ ํ ์ ์๋ค. ๋จ์ํ userId 18 ๋ฟ๋ง ์๋๋ผ, user์ ์ ์ฒด ์ ๋ณด๋ฅผ ์กฐํ ํ ์ ์๋ค.
[
{
id: 7,
createdAt: 2022-03-20T14:24:13.100Z,
updatedAt: 2022-03-20T14:24:13.101Z,
userId: 18,
image: 'xxx',
name: '',
price: 12,
description: '12',
user: {
id: 18,
phone: null,
email: 'rem9238@kakao.com',
name: 'Anonymous',
avatar: null,
createdAt: 2022-03-01T07:04:26.393Z,
updatedAt: 2022-03-01T07:04:26.395Z
}
}
]
include [read]
๊ด๊ณ๋ฅผ ์ด๋ฃจ๊ณ ์๋ ํ๋๋ฅผ ํ์ฌ ์กฐํํ ์ฟผ๋ฆฌ์ ํฌํจ์ํฌ ๋ ์ฌ์ฉ
const result = await client.product.findMany({
where: {
id: 2,
},
include: {
user: true,
}
});
์กฐํ ๊ฒฐ๊ณผ : user์ product์ schema์์ @relation ๋์ด ์์
[
{
id: 2,
createdAt: 2022-03-13T10:46:04.141Z,
updatedAt: 2022-03-20T12:48:06.522Z,
userId: 19,
image: 'xxx',
name: '๊ฐค๋ญ์',
price: 123,
description: '์ด๊ฒ์ ์ฒซ๋ฒ์งธ',
user: {
id: 19,
phone: '01099025911',
email: null,
name: 'Anonymous',
avatar: null,
createdAt: 2022-03-02T15:06:12.018Z,
updatedAt: 2022-03-02T15:06:12.019Z
}
}
]
+select์ ํจ๊ป ์ฌ์ฉํ๊ธฐ
const result = await client.product.findMany({
where: {
id: 2,
},
include: {
user: {
select: {
name: true,
id: true,
}
}
}
});
์กฐํ ๊ฒฐ๊ณผ
[
{
id: 2,
createdAt: 2022-03-13T10:46:04.141Z,
updatedAt: 2022-03-20T12:48:06.522Z,
userId: 19,
image: 'xxx',
name: '๊ฐค๋ญ์',
price: 123,
description: '์ด๊ฒ์ ์ฒซ๋ฒ์งธ',
user: { id: 19, name: 'Anonymous' }
}
]
connectOrCreate
๊ณ ์ id ์ ์ํด ๊ธฐ์กด ๋ ์ฝ๋๋ฅผ ์ฐ๊ฒฐํ๊ฑฐ๋ ๋๋ ๋ ์ฝ๋๊ฐ ์กด์ฌํ์ง ์์ผ๋ฉด, ๋ ์ฝ๋๋ฅผ ์๋ก ์์ฑํ ๋ ์ฌ์ฉํ๋ค.
// user : { phone: phone } ๋๋ { email: email } ==> phone๊ณผ email ๋ชจ๋ `@unique`
const token = await client.token.create({
data: {
payload, // token number
// token์ user ์ ๋ณด๋ where ์ ๋ณด๋ฅผ ์กฐํ ํ์ ๋
// ์ด๋ฏธ ์์ผ๋ฉด, connect ํ๊ณ
// ์์ผ๋ฉด, { name: '', ...user } ๋ก user๋ฅผ ์์ฑํ๋ค.
// token ์์ฑ ์ฟผ๋ฆฌ์์ user๋ฅผ ์์ฑํ ์๋ ์๋ค.
user: {
connectOrCreate: {
where: {
...user,
},
create: {
name: "Anonymous",
...user,
},
},
},
},
});
Author And Source
์ด ๋ฌธ์ ์ ๊ดํ์ฌ(prisma client API, ์ฌ์ฉ ์์), ์ฐ๋ฆฌ๋ ์ด๊ณณ์์ ๋ ๋ง์ ์๋ฃ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๋ณด์๋ค https://velog.io/@with-key/prisma-client-API-์ฌ์ฉ-์์์ ์ ๊ท์: ์์์ ์ ๋ณด๊ฐ ์์์ URL์ ํฌํจ๋์ด ์์ผ๋ฉฐ ์ ์๊ถ์ ์์์ ์์ ์ ๋๋ค.
์ฐ์ํ ๊ฐ๋ฐ์ ์ฝํ
์ธ ๋ฐ๊ฒฌ์ ์ ๋
(Collection and Share based on the CC Protocol.)
์ข์ ์นํ์ด์ง ์ฆ๊ฒจ์ฐพ๊ธฐ
๊ฐ๋ฐ์ ์ฐ์ ์ฌ์ดํธ ์์ง
๊ฐ๋ฐ์๊ฐ ์์์ผ ํ ํ์ ์ฌ์ดํธ 100์ ์ถ์ฒ ์ฐ๋ฆฌ๋ ๋น์ ์ ์ํด 100๊ฐ์ ์์ฃผ ์ฌ์ฉํ๋ ๊ฐ๋ฐ์ ํ์ต ์ฌ์ดํธ๋ฅผ ์ ๋ฆฌํ์ต๋๋ค