TIL

23/11/29 TIL __ prisma ์‚ฌ์šฉํ•˜๊ธฐ

GABOJOK 2023. 11. 30. 07:19

 

 

๐Ÿš  ์ƒํ™ฉ

์ง€๋‚œ๋ฒˆ orm์œผ๋กœ ์‹œํ€„๋ผ์ด์ฆˆ๋ฅผ ์‚ฌ์šฉํ•ด ๋ดค๋Š”๋ฐ, 

์ด๋ฒˆ์—๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์—์„œ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ํ”„๋ฆฌ์ฆˆ๋งˆ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜๋‹ค.

์–ด๋–ค์ ๋“ค์ด ๋‹ค๋ฅด๊ณ  ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”๊ฑด์ง€ ๊ฐ„๋žตํžˆ ์•Œ์•„๋ณด์ž.

 

 

๐Ÿ“ท  ํ”„๋ฆฌ์ฆˆ๋งˆ

์ผ๋‹จ ํ”„๋ฆฌ์ฆˆ๋งˆ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ, ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ, ๊ทธ๋ฆฌ๊ณ  ๋ฏธ์•ฝํ•˜์ง€๋งŒ mongodb๋„ ์ง€์›ํ•œ๋‹ค.

๋˜ ์‹œํ€„๋ผ์ด์ฆˆ๋Š” ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๋Š” ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ๊ณผ,  ์—ฐ๊ฒฐํ•˜๋Š” ๋ชจ๋ธ ํŒŒ์ผ์ด ์žˆ์–ด ๋‘ ํŒŒ์ผ ๋‹ค ์„ค์ •์„ ํ•ด์ค˜์•ผ๋งŒ ํ•œ๋‹ค.  

์ด์™€ ๋‹ค๋ฅด๊ฒŒ ํ”„๋ฆฌ์ฆˆ๋งˆ๋Š” index.js ํŒŒ์ผ ์—์„œ prisma client๋ฅผ ๊ฐ€์ ธ์™€ ์„ค์ •ํ•˜๊ณ ,

schema.prisma ํŒŒ์ผ์—์„œ ํ…Œ์ด๋ธ”์„ ์„ค์ •, ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

์ผ๋‹จ ์…‹ํŒ… ์ž‘์—…์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

# yarn ํ”„๋กœ์ ํŠธ๋ฅผ ์ดˆ๊ธฐํ™”.
yarn init -y

# express, prisma, @prisma/client ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜.
yarn add express prisma @prisma/client

# nodemon ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ DevDependency๋กœ ์„ค์น˜.
yarn add -D nodemon

# ์„ค์น˜ํ•œ prisma๋ฅผ ์ดˆ๊ธฐํ™” ํ•˜์—ฌ, prisma๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ๋ฅผ ์ƒ์„ฑ.
npx prisma init

#Prisma Client๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ช…๋ น์–ด
#node_modules/@prisma/client ํด๋” ์•ˆ์— ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” prisma client ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑ
npx prisma generate

 

 

 

์ดํ›„ ์•„๋ž˜์™€ ๊ฐ™์ด ์Šคํ‚ค๋งˆ๋ฅผ ์„ค์ •ํ•ด ์ฃผ๋ฉด ๋œ๋‹ค.

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

model Users {
  userId    Int      @id        @default(autoincrement()) @map("userId")
  email     String   @unique          @map("email")
  password  String   @map("password")
  createdAt DateTime @default(now()) @map("createdAt")
  updatedAt DateTime @updatedAt      @map("updatedAt")

  //ํ•˜์œ„ํ…Œ์ด๋ธ”์ด๋ฆ„  ๊ด€๊ณ„
  UserInfos UserInfos?  //1:1 ๊ด€๊ณ„
  Posts     Posts[]   // 1: n ๊ด€๊ณ„
  Comments  Comments[]

  @@map("Users")
}

model UserInfos {
  userInfoId   Int     @id       @default(autoincrement())  @map("userInfoId")
  UserId       Int     @unique   @map("UserId")
  name         String  @map("name")
  age          Int?              @map("age")  //null์„ ํ—ˆ์šฉํ•œ๋‹ค๋Š” ์˜๋ฏธ๋กœ ๋ฐ์ดํ„ฐ ํƒ€์ž… ์˜†์— ? ๋ผ๊ณ  ์ ์Œ
  gender       String  @map("gender")
  profileImage String?           @map("profileImage")
  createdAt DateTime   @default(now()) @map("createdAt")
  updatedAt DateTime   @updatedAt      @map("updatedAt")

  //   fields ( ํ˜„์žฌ ํ…Œ์ด๋ธ”์˜ ์ปฌ๋Ÿผ,  references: ์ฐธ๊ณ ํ•  ํ…Œ์ด๋ธ”์˜ ์ปฌ๋Ÿผ)
  User Users @relation (fields: [UserId], references : [userId], onDelete: Cascade)

  @@map("UserInfos")
}

model Posts {
  postId    Int       @id       @default(autoincrement()) @map("postId")
  UserId    Int       @map("UserID")
  title     String    @map("title")
  content   String    @map("content") @db.Text
  createdAt DateTime  @default(now()) @map("createdAt")
  updatedAt DateTime  @updatedAt      @map("updatedAt")

  User      Users      @relation(fields : [UserId], references : [userId], onDelete : Cascade)
  Comments  Comments[]

  @@map("Posts")
}

 

 

 

์ถ”๊ฐ€์ ์ธ ์‚ฌ์šฉ๋ฒ•์€ ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ฐพ์•„๋ณด์ž/

 

 

์ฐธ๊ณ ํ•œ ์‚ฌ์ดํŠธ

https://medium.com/@olafdev/prisma-schema-%ED%8E%B8-2401c9609ae2

 

Prismaโ€Š—โ€ŠSchema ํŽธ

์ด๋ฒˆ์— ์ด์ง์„ ํ•˜๋ฉด์„œ Prisma๋ฅผ ์ฒ˜์Œ ์ ‘ํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์—…๋ฌด์— ์กฐ๊ธˆ ๋” ๋น ๋ฅด๊ฒŒ ์ ์‘ํ•˜๊ณ ์ž ๊ณต๋ถ€ํ•˜์˜€๊ณ , ๊ณต๋ถ€ํ•˜๋ฉด์„œ ๋Š๋‚€ Prisma์˜ ๋งค๋ ฅ์„ ์ฃผ์š” ๊ตฌ์„ฑ์š”์†Œ๋ณ„๋กœ ๋‚˜๋ˆ„์–ด ์ •๋ฆฌํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

medium.com

https://defineall.tistory.com/1052

 

[Prisma] ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ น์–ด / ์ฃผ์˜์ 

๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ฎ์–ด์“ฐ๊ธฐ npx prisma migrate dev // ์ ์šฉ๋˜์ง€ ์•Š์€ ๊ฐ€์žฅ ์ตœ๊ทผ์˜ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค - ์—†์œผ๋ฉด ์ƒˆ๋กœ ๋งŒ๋“ฌ // migrate๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋‚ด์šฉ์„ ์ดˆ๊ธฐํ™” ํ•ฉ๋‹ˆ๋‹ค

defineall.tistory.com