Prisma용 DBML 생성기

3636 단어 prismadbml
Prisma 스키마를 기반으로 Prisma DBML Generator 스키마를 자동으로 생성하는 🥳DBML를 소개합니다.

DBML 생성기를 설치하기만 하면 됩니다.

npm install -D prisma-dbml-generator
schema.prisma에 발전기를 추가하십시오.

generator dbml {
  provider = "prisma-dbml-generator"
}

다음 Prisma 스키마에 대해 실행 중npx prisma generate
model User {
  id Int @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  email String @unique
  name String?
  posts Post[]
  profile Profile?
  /// user role
  role Role @default(USER)
}

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

model Post {
  id Int @id @default(autoincrement())
  title String
  content String?
  published Boolean @default(false)
  author User? @relation(fields: [authorId], references: [id])
  authorId Int?
}

/// user role
enum Role {
  ADMIN /// allowed to do everything
  USER
}
schema.dbml에서 prisma/dbml까지 다음을 생성합니다.

Table User {
  id Int [pk, increment]
  createdAt DateTime [default: `now()`, not null]
  updatedAt DateTime [not null]
  email String [unique, not null]
  name String
  posts Post
  profile Profile
  role Role [not null, default: 'USER', note: 'user role']
}

Table Profile {
  id Int [pk, increment]
  bio String
  user User [not null]
  userId Int [unique, not null]

  Note: 'User profile'
}

Table Post {
  id Int [pk, increment]
  title String [not null]
  content String
  published Boolean [not null, default: false]
  author User
  authorId Int
}

Enum Role {
  ADMIN
  USER
}

Ref: Profile.userId - User.id

Ref: Post.authorId > User.id
schema.dbml 콘텐츠와 visualize을 Entity-Relationship Diagram으로 복사합니다.



실행할 때마다 이 출력이 표시되어야 합니다npx prisma generate.

$ npx prisma generate
Environment variables loaded from prisma/.env

✔ Generated Prisma Client to ./node_modules/@prisma/client in 281ms

✔ Generated DBML Schema to ./prisma/dbml in 5ms

You can now start using Prisma Client in your code:

``
import { PrismaClient } from '@prisma/client'
// or const { PrismaClient } = require('@prisma/client')

const prisma = new PrismaClient()
``

Explore the full API: http://pris.ly/d/client

Check out the readme 나만의 Prisma Schema로 시도해 보세요 😎.

좋은 웹페이지 즐겨찾기