[TIL]Prisma_Prisma Client

Sohee Yeoยท2024๋…„ 1์›” 3์ผ
0
post-thumbnail

๐Ÿš€์ง€๋‚œ๋ฒˆ Prisma๋ฅผ ์„ค์น˜ํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์„ ์•Œ์•„๋ณด์•˜๋‹ค. ์˜ค๋Š˜์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์–ด๋–ป๊ฒŒ ์ ‘๊ทผํ•˜๊ณ  ์กฐ์ž‘ํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๋ ค ํ•œ๋‹ค.

Prisma Client

Prisma Client๋ž€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์Šคํ‚ค๋งˆ์— ๋งž๊ฒŒ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํด๋ผ์ด์–ธํŠธ์ด๋‹ค. ์‰ฝ๊ฒŒ ๋งํ•ด Prisma๋ฅผ ํ†ตํ•ด ์ƒ์„ฑํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์— ์‰ฝ๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๋„๊ตฌ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

Prisma Client ์„ค์น˜

์•„๋ž˜ ๋ช…๋ น์–ด๋กœ PrismaClient๋ฅผ ์„ค์น˜ํ•œ๋‹ค.

npm install @prisma/client

์„ค์น˜ ํ›„ ์•„๋ž˜ ๋ช…๋ น์–ด๋กœ Prisma Client๋ฅผ ์ƒ์„ฑํ•ด์ค€๋‹ค.

prisma generate

๐Ÿ“ข Prisma Schema์— ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ์žˆ์„ ๋•Œ๋งˆ๋‹ค npx prisma generate๋ฅผ ์‹คํ–‰ํ•ด Prisma Client๋ฅผ ์—…๋ฐ์ดํŠธํ•ด์ค˜์•ผ ํ•œ๋‹ค.

์ด์ œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘๊ทผ์ด ํ•„์š”ํ•œ ๊ณณ์—์„œ PrismaClient๋ฅผ ์ธ์Šคํ„ด์Šคํ™” ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

or

const { PrismaClient } = require('@prisma/client');

const prisma = new PrismaClient();

๐Ÿ’ก Next.js์—์„œ์˜ Prisma Client

Next.js์˜ ๊ฒฝ์šฐ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์—์„œ ํ•ซ ๋ฆฌ๋กœ๋”ฉ(์ฝ”๋“œ ๋ณ€๊ฒฝ์‹œ ๋ณ€๊ฒฝ๋œ ์ฝ”๋“œ ์žฌ์‹คํ–‰. ํŒŒ์ผ์„ ์ €์žฅํ•  ๋•Œ๋งˆ๋‹ค ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ๋“ค์ด ์žฌ์‹คํ–‰๋จ.)์œผ๋กœ ์ธํ•ด ๋งค๋ฒˆ ์ƒˆ๋กœ์šด PrismaClient ์ธ์Šคํ„ด์Šค๊ฐ€ ์ดˆ๊ธฐํ™”๋˜๊ณ  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋Œ€ํ•œ ์—ฐ๊ฒฐ์ด ์ƒ์„ฑ๋œ๋‹ค. ์ด์— ๋”ฐ๋ผ ๋„ˆ๋ฌด ๋งŽ์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ์ด ์ƒ๊ธฐ๋ฉด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ž…์ถœ๋ ฅ์ด ๋งค์šฐ ๋Š๋ ค์ง€๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด PrismaClient๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜์—ฌ gloabalThis ๊ฐœ์ฒด์— ์ €์žฅํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.

// lib/prisma.js

const { PrismaClient } = require("@prisma/client");

// PrismaClient๊ฐ€ global์— ์—†๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ธ์Šคํ„ด์Šคํ™”
const prisma = globalThis.prisma || new PrismaClient();

// ํ”„๋กœ๋•์…˜ ์ƒํƒœ๊ฐ€ ์•„๋‹ ๋•Œ globalThis ๊ฐœ์ฒด์— ์ €์žฅ
if (process.env.NODE_ENV !== "production") globalThis.prisma = prisma;

export default prisma;

Prisma Client ์‚ฌ์šฉ

๋ฐ์ดํ„ฐ ์ ‘๊ทผ์ด ํ•„์š”ํ•œ ๊ณณ์—์„œ ์ธ์Šคํ„ด์Šคํ™”ํ•œ PrismaClient๋ฅผ ๋ถˆ๋Ÿฌ์™€ ์‚ฌ์šฉํ•œ๋‹ค. Prisma Client API๋กœ CRUD ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

Create

create ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ ์ƒ์„ฑ

const member = await prisma.member.create({
  data: {
    name: 'Kim',
    age: 20
  },
})

createMany ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ

const createMembers = await prisma.member.createMany({
  data: [
    { name: 'Kim', age: 22 },
    { name: 'Lee', age: 23 },
    { name: 'Park', age: 23 },
  ],
})

Read

findUnique ํŠน์ • ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ

// name์ด Kim์ธ ๋ฐ์ดํ„ฐ ์กฐํšŒ
const member = await prisma.member.findUnique({
  where: {
    name: 'Kim',
  },
})

findMany ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ

// ๋ชจ๋“  ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
const members = await prisma.member.findMany()
// ์กฐ๊ฑด์— ๋งž๋Š” ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
// age๊ฐ€ 23์ธ ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์กฐํšŒ
const members = await prisma.member.findMany({
  where: {
    age: 23,
  },
})

Update

update where ์•ˆ์˜ ์†์„ฑ์„ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ ์—…๋ฐ์ดํŠธ

// name์ด Park์ธ ๋ฐ์ดํ„ฐ์˜ age๋ฅผ 24๋กœ ์—…๋ฐ์ดํŠธ
const updateMember = await prisma.member.update({
  where: {
    name: 'Park',
  },
  data: {
    age: 24,
  },
})

updateMany where ์•ˆ์˜ ์†์„ฑ์„ ๊ฐ€์ง„ ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์—…๋ฐ์ดํŠธ

// age๊ฐ€ 23 ์ด์ƒ์ธ ๋ชจ๋“  ๋ฐ์ดํ„ฐ์˜ role์„ ADMIN์œผ๋กœ ์—…๋ฐ์ดํŠธ
const updateMembers = await prisma.member.updateMany({
  where: {
    age: {
      gte: 23,
    },
  },
  data: {
    role: 'ADMIN',
  },
})

upsert where ์•ˆ์˜ ์†์„ฑ์„ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๋ฉด ์—…๋ฐ์ดํŠธ, ์—†๋‹ค๋ฉด ๋ฐ์ดํ„ฐ ์ƒ์„ฑ

// name์ด Jo์ธ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๋ฉด age๋ฅผ 20์œผ๋กœ ์—…๋ฐ์ดํŠธ, ์—†๋‹ค๋ฉด name์ด Jo์ด๊ณ  age๊ฐ€ 20๋ฐ์ดํ„ฐ ์ƒ์„ฑ
const upsertMember = await prisma.member.upsert({
  where: {
    name: 'Jo',
  },
  update: {
    age: 20,
  },
  create: {
    name: 'Jo',
    age: 20,
  },
})

Delete

delete where ์•ˆ์˜ ์†์„ฑ์„ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ ์‚ญ์ œ

// age๊ฐ€ 22์ธ ๋ฐ์ดํ„ฐ ์‚ญ์ œํ•˜๊ธฐ
const deleteMember = await prisma.member.delete({
  where: {
    age: 22,
  },
})

deleteMany ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ ์‚ญ์ œ

// ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์‚ญ์ œํ•˜๊ธฐ
const members = await prisma.member.deleteMany()
// ์กฐ๊ฑด์— ๋งž๋Š” ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ ์‚ญ์ œํ•˜๊ธฐ
// age๊ฐ€ 23์ธ ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์‚ญ์ œ
const members = await prisma.member.deleteMany({
  where: {
    age: 23,
  },
})

์ •๋ฆฌ

  • Prisma Client: ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์— ์‰ฝ๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๋„๊ตฌ
  • Prisma Schema์— ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ์žˆ์„ ๋•Œ๋งˆ๋‹ค npx prisma generate๋ฅผ ์‹คํ–‰ํ•˜์—ฌ Prisma Client๋ฅผ ์—…๋ฐ์ดํŠธํ•ด์•ผ ํ•œ๋‹ค.
  • Next.js์—์„œ๋Š” ๋”ฐ๋กœ prisma.js ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด Prisma Client๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.



์ฐธ๊ณ 

https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/generating-prisma-client#why-is-prisma-client-generated-into-node_modulesprismaclient-by-default

https://www.prisma.io/docs/orm/more/help-and-troubleshooting/help-articles/nextjs-prisma-client-dev-practices

https://medium.com/@2018.itsuki/postgresql-with-next-js-and-prisma-44f66a05378a

https://www.prisma.io/docs/orm/prisma-client/queries/crud

profile
์‹คํŒจ์— ๋ฌด๋”˜ ์‚ฌ๋žŒ. ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž๋ฅผ ๊ฟˆ๊ฟ‰๋‹ˆ๋‹ค

0๊ฐœ์˜ ๋Œ“๊ธ€