Prisma 사용하기

sxxng_ju·2023년 1월 21일
0

Prisma

프리즈마란 SQL 코드를 직접 작성하지 않고 javascript(ts포함)를 작성하여 데이터베이스를 수정할 수 있도록 연결해주는 서비스입니다.

SQL코드보다 JS코드로 보다 간결하고 단순하게 효율적인 데이터베이스 관리가 가능한 장점이 있습니다.

npm init -y
npm install typescript ts-node @types/node --save-dev
npm install prisma --save-dev
npx prisma init
npx prisma db push
# Profile, Post가 있어도 되고, 없어도 된다.
model User {
    id       Int      @id @default(autoincrement())
    username String
    Profile  Profile?
    Post     Post[]
}
# Profile는 1유저당 1개만 있어야 한다 ( 일대일 대응 )
# Post는 1유저에 여러개를 가질 수 있다 ( 일대다 대응 )


model Profile {
    id     Int     @id @default(autoincrement())
    bio    String?
    userId Int     @unique
    User   User    @relation(fields: [userId], references: [id])
}

# User가 무조건 있어야 한다.
model Post {
    id     Int    @id @default(autoincrement())
    title  String
    photo  String
    userId Int    @unique
    User   User   @relation(fields: [userId], references: [id])

Prisma를 사용해보니 간단하게 Next Edge API와 연결해서 사용할 수 있었습니다. prisma 사용권한을 줄 때 with grant option을 주지 않으면 migrate--dev가 되지않아서 애먹은 기억이 있습니다. prisma를 사용할 때는 root권한 혹은 새로운 유저에 with grant option 권한을 부여해야 작동합니다.

0개의 댓글