Skip to content

Yarn Prisma

Directory Structure

.
|-- package.json
|-- prisma
|   |-- dev.db
|   |-- dev.db-journal
|   |-- migrations
|   |   |-- *_init
|   |   |   `-- migration.sql
|   |   `-- migration_lock.toml
|   `-- schema.prisma
|-- src
|   `-- index.ts
`-- tsconfig.json
sh
yarn add prisma --dev
sh
yarn add @prisma/client
sh
DATABASE_URL="file:./dev.db"

schema.prisma作成

sh
yarn prisma init --datasource-provider sqlite

Migrate

sh
yarn prisma migrate dev --name init
ts
import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

const deleteUsers = async () => {
  await prisma.user.deleteMany()
}

deleteUsers()

const createUsers = async () => {
  await prisma.user.createMany({
    data: [
      {
        id: 1,
        name: 'prisma',
        email: 'prisma@email.com',
      },
      {
        id: 2,
        name: 'drizzle',
        email: 'drizzle@email.com',
      },
      {
        id: 3,
        name: 'typeorm',
        email: 'typeorm@email.com',
      },
    ],
  })
}

const main = async () => {
  await createUsers()
  const users = await prisma.user.findMany()
  console.log(users)
}

main()
  .catch((e) => {
    console.error(e)
    process.exit(1)
  })
  .finally(async () => {
    await prisma.$disconnect()
  })

Execution

[
  { id: 1, email: 'prisma@email.com', name: 'prisma' },
  { id: 2, email: 'drizzle@email.com', name: 'drizzle' },
  { id: 3, email: 'typeorm@email.com', name: 'typeorm' }
]

Mermaid

sh
yarn add -D prisma-erd-generator
prisma
generator client {
  provider = "prisma-client-js"
}

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

generator erd {
  provider = "prisma-erd-generator"
  output   = "er.md"
}

model User {
  id        Int        @id @default(autoincrement())
  email     String     @unique
  name      String?
  profile   Profile?
  posts     Post[]
  comments  Comment[]
  roles     UserRole[]
  createdAt DateTime   @default(now())
  updatedAt DateTime   @updatedAt
}

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

model Post {
  id         Int        @id @default(autoincrement())
  title      String
  content    String?
  published  Boolean    @default(false)
  authorId   Int
  author     User       @relation(fields: [authorId], references: [id])
  categories Category[]
  comments   Comment[]
  tags       Tag[]
  createdAt  DateTime   @default(now())
  updatedAt  DateTime   @updatedAt
}

model Comment {
  id        Int      @id @default(autoincrement())
  content   String
  postId    Int
  post      Post     @relation(fields: [postId], references: [id])
  authorId  Int
  author    User     @relation(fields: [authorId], references: [id])
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model Category {
  id    Int    @id @default(autoincrement())
  name  String @unique
  posts Post[]
}

model Tag {
  id    Int    @id @default(autoincrement())
  name  String @unique
  posts Post[]
}

model Role {
  id    Int        @id @default(autoincrement())
  name  String     @unique
  users UserRole[]
}

model UserRole {
  userId Int
  roleId Int
  user   User @relation(fields: [userId], references: [id])
  role   Role @relation(fields: [roleId], references: [id])

  @@id([userId, roleId])
}
mmd
erDiagram

  "User" {
    Int id "🗝️"
    String email 
    String name "❓"
    DateTime createdAt 
    DateTime updatedAt 
    }
  

  "Profile" {
    Int id "🗝️"
    String bio "❓"
    }
  

  "Post" {
    Int id "🗝️"
    String title 
    String content "❓"
    Boolean published 
    DateTime createdAt 
    DateTime updatedAt 
    }
  

  "Comment" {
    Int id "🗝️"
    String content 
    DateTime createdAt 
    DateTime updatedAt 
    }
  

  "Category" {
    Int id "🗝️"
    String name 
    }
  

  "Tag" {
    Int id "🗝️"
    String name 
    }
  

  "Role" {
    Int id "🗝️"
    String name 
    }
  

  "UserRole" {

    }
  
    "User" o{--}o "Profile" : "profile"
    "User" o{--}o "Post" : "posts"
    "User" o{--}o "Comment" : "comments"
    "User" o{--}o "UserRole" : "roles"
    "Profile" o|--|| "User" : "user"
    "Post" o|--|| "User" : "author"
    "Post" o{--}o "Category" : "categories"
    "Post" o{--}o "Comment" : "comments"
    "Post" o{--}o "Tag" : "tags"
    "Comment" o|--|| "Post" : "post"
    "Comment" o|--|| "User" : "author"
    "Category" o{--}o "Post" : "posts"
    "Tag" o{--}o "Post" : "posts"
    "Role" o{--}o "UserRole" : "users"
    "UserRole" o|--|| "User" : "user"
    "UserRole" o|--|| "Role" : "role"

Generate

sh
yarn prisma generate

PlantUML

sh
yarn add -D prisma-generator-plantuml-erd
prisma
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

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

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

generator erd_plantuml {
  provider                   = "prisma-generator-plantuml-erd"
  output                     = "erd.puml"
  showUniqueKeyLabel         = true
  isShowForeignKeyOnRelation = true
}

model User {
  id        Int        @id @default(autoincrement())
  email     String     @unique
  name      String?
  profile   Profile?
  posts     Post[]
  comments  Comment[]
  roles     UserRole[]
  createdAt DateTime   @default(now())
  updatedAt DateTime   @updatedAt
}

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

model Post {
  id         Int        @id @default(autoincrement())
  title      String
  content    String?
  published  Boolean    @default(false)
  authorId   Int
  author     User       @relation(fields: [authorId], references: [id])
  categories Category[]
  comments   Comment[]
  tags       Tag[]
  createdAt  DateTime   @default(now())
  updatedAt  DateTime   @updatedAt
}

model Comment {
  id        Int      @id @default(autoincrement())
  content   String
  postId    Int
  post      Post     @relation(fields: [postId], references: [id])
  authorId  Int
  author    User     @relation(fields: [authorId], references: [id])
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model Category {
  id    Int    @id @default(autoincrement())
  name  String @unique
  posts Post[]
}

model Tag {
  id    Int    @id @default(autoincrement())
  name  String @unique
  posts Post[]
}

model Role {
  id    Int        @id @default(autoincrement())
  name  String     @unique
  users UserRole[]
}

model UserRole {
  userId Int
  roleId Int
  user   User @relation(fields: [userId], references: [id])
  role   Role @relation(fields: [roleId], references: [id])

  @@id([userId, roleId])
}
@startuml erd
skinparam linetype ortho
entity "User" as User {
+ id [PK] : Int 
--
  * email : [UK] String
  name : String
  * createdAt : DateTime
  * updatedAt : DateTime
}

entity "Profile" as Profile {
+ id [PK] : Int 
--
  bio : String
  # userId : [UK] [FK] User
}

entity "Post" as Post {
+ id [PK] : Int 
--
  * title : String
  content : String
  * published : Boolean
  # authorId : [FK] User
  * createdAt : DateTime
  * updatedAt : DateTime
}

entity "Comment" as Comment {
+ id [PK] : Int 
--
  * content : String
  # postId : [FK] Post
  # authorId : [FK] User
  * createdAt : DateTime
  * updatedAt : DateTime
}

entity "Category" as Category {
+ id [PK] : Int 
--
  * name : [UK] String
}

entity "Tag" as Tag {
+ id [PK] : Int 
--
  * name : [UK] String
}

entity "Role" as Role {
+ id [PK] : Int 
--
  * name : [UK] String
}

entity "UserRole" as UserRole {
--
  # userId : [FK] User
  # roleId : [FK] Role
}

' Relations
Profile |o--|| User: userId
Post }o--|| User: authorId
Comment }o--|| Post: postId
Comment }o--|| User: authorId
UserRole }o--|| User: userId
UserRole }o--|| Role: roleId
' ManyToMany Relations
Post }o--o{ Category
Post }o--o{ Tag
' enum relations
@enduml

Generate

sh
yarn prisma generate
erd

Md

prisma
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

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

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

generator erd {
  provider = "prisma-erd-generator"
  output   = "er.md"
}

generator erd_plantuml {
  provider                   = "prisma-generator-plantuml-erd"
  exportPerTables            = true
  showUniqueKeyLabel         = true
  isShowForeignKeyOnRelation = true
  markdownOutput             = "./example-tables.md"
  markdownIncludeERD         = true
}

model User {
  id        Int        @id @default(autoincrement())
  email     String     @unique
  name      String?
  profile   Profile?
  posts     Post[]
  comments  Comment[]
  roles     UserRole[]
  createdAt DateTime   @default(now())
  updatedAt DateTime   @updatedAt
}

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

model Post {
  id         Int        @id @default(autoincrement())
  title      String
  content    String?
  published  Boolean    @default(false)
  authorId   Int
  author     User       @relation(fields: [authorId], references: [id])
  categories Category[]
  comments   Comment[]
  tags       Tag[]
  createdAt  DateTime   @default(now())
  updatedAt  DateTime   @updatedAt
}

model Comment {
  id        Int      @id @default(autoincrement())
  content   String
  postId    Int
  post      Post     @relation(fields: [postId], references: [id])
  authorId  Int
  author    User     @relation(fields: [authorId], references: [id])
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model Category {
  id    Int    @id @default(autoincrement())
  name  String @unique
  posts Post[]
}

model Tag {
  id    Int    @id @default(autoincrement())
  name  String @unique
  posts Post[]
}

model Role {
  id    Int        @id @default(autoincrement())
  name  String     @unique
  users UserRole[]
}

model UserRole {
  userId Int
  roleId Int
  user   User @relation(fields: [userId], references: [id])
  role   Role @relation(fields: [roleId], references: [id])

  @@id([userId, roleId])
}

Generate

sh
yarn prisma generate
Generated Md

Tables

ER diagram

@startuml erd
skinparam linetype ortho
entity "User" as User {
+ id [PK] : Int 
--
  * email : [UK] String
  name : String
  * createdAt : DateTime
  * updatedAt : DateTime
}

entity "Profile" as Profile {
+ id [PK] : Int 
--
  bio : String
  # userId : [UK] [FK] User
}

entity "Post" as Post {
+ id [PK] : Int 
--
  * title : String
  content : String
  * published : Boolean
  # authorId : [FK] User
  * createdAt : DateTime
  * updatedAt : DateTime
}

entity "Comment" as Comment {
+ id [PK] : Int 
--
  * content : String
  # postId : [FK] Post
  # authorId : [FK] User
  * createdAt : DateTime
  * updatedAt : DateTime
}

entity "Category" as Category {
+ id [PK] : Int 
--
  * name : [UK] String
}

entity "Tag" as Tag {
+ id [PK] : Int 
--
  * name : [UK] String
}

entity "Role" as Role {
+ id [PK] : Int 
--
  * name : [UK] String
}

entity "UserRole" as UserRole {
--
  # userId : [FK] User
  # roleId : [FK] Role
}

' Relations
Profile |o--|| User: userId
Post }o--|| User: authorId
Comment }o--|| Post: postId
Comment }o--|| User: authorId
UserRole }o--|| User: userId
UserRole }o--|| Role: roleId
' ManyToMany Relations
Post }o--o{ Category
Post }o--o{ Tag
' enum relations
@enduml

User

Description

Columns

NameTypeDefaultNullableUniqueChildrenParentComment
idIntautoincrementfalsetrueProfile, Post, Comment, UserRole
emailStringfalsetrue
nameStringtruefalse
createdAtDateTimenowfalsefalse
updatedAtDateTimefalsefalse

ER diagram

@startuml User
skinparam linetype ortho
entity "User" as User {
+ id [PK] : Int 
--
  * email : [UK] String
  name : String
  * createdAt : DateTime
  * updatedAt : DateTime
}

entity "Profile" as Profile {
+ id [PK] : Int 
--
  bio : String
  # userId : [UK] [FK] User
}

entity "Post" as Post {
+ id [PK] : Int 
--
  * title : String
  content : String
  * published : Boolean
  # authorId : [FK] User
  * createdAt : DateTime
  * updatedAt : DateTime
}

entity "Comment" as Comment {
+ id [PK] : Int 
--
  * content : String
  # postId : [FK] Post
  # authorId : [FK] User
  * createdAt : DateTime
  * updatedAt : DateTime
}

entity "UserRole" as UserRole {
--
  # userId : [FK] User
  # roleId : [FK] Role
}

' Relations
Profile |o--|| User: userId
Post }o--|| User: authorId
Comment }o--|| Post: postId
Comment }o--|| User: authorId
UserRole }o--|| User: userId
' ManyToMany Relations
' enum relations
@enduml

Profile

Description

Columns

NameTypeDefaultNullableUniqueChildrenParentComment
idIntautoincrementfalsetrue
bioStringtruefalse
userIdIntfalsetrueUser

ER diagram

@startuml Profile
skinparam linetype ortho
entity "User" as User {
+ id [PK] : Int 
--
  * email : [UK] String
  name : String
  * createdAt : DateTime
  * updatedAt : DateTime
}

entity "Profile" as Profile {
+ id [PK] : Int 
--
  bio : String
  # userId : [UK] [FK] User
}

' Relations
Profile |o--|| User: userId
' ManyToMany Relations
' enum relations
@enduml

Post

Description

Columns

NameTypeDefaultNullableUniqueChildrenParentComment
idIntautoincrementfalsetrueComment
titleStringfalsefalse
contentStringtruefalse
publishedBooleanfalsefalse
authorIdIntfalsefalseUser
createdAtDateTimenowfalsefalse
updatedAtDateTimefalsefalse

ER diagram

@startuml Post
skinparam linetype ortho
entity "User" as User {
+ id [PK] : Int 
--
  * email : [UK] String
  name : String
  * createdAt : DateTime
  * updatedAt : DateTime
}

entity "Post" as Post {
+ id [PK] : Int 
--
  * title : String
  content : String
  * published : Boolean
  # authorId : [FK] User
  * createdAt : DateTime
  * updatedAt : DateTime
}

entity "Comment" as Comment {
+ id [PK] : Int 
--
  * content : String
  # postId : [FK] Post
  # authorId : [FK] User
  * createdAt : DateTime
  * updatedAt : DateTime
}

entity "Category" as Category {
+ id [PK] : Int 
--
  * name : [UK] String
}

entity "Tag" as Tag {
+ id [PK] : Int 
--
  * name : [UK] String
}

' Relations
Post }o--|| User: authorId
Comment }o--|| Post: postId
Comment }o--|| User: authorId
' ManyToMany Relations
Post }o--o{ Category
Post }o--o{ Tag
' enum relations
@enduml

Comment

Description

Columns

NameTypeDefaultNullableUniqueChildrenParentComment
idIntautoincrementfalsetrue
contentStringfalsefalse
postIdIntfalsefalsePost
authorIdIntfalsefalseUser
createdAtDateTimenowfalsefalse
updatedAtDateTimefalsefalse

ER diagram

@startuml Comment
skinparam linetype ortho
entity "User" as User {
+ id [PK] : Int 
--
  * email : [UK] String
  name : String
  * createdAt : DateTime
  * updatedAt : DateTime
}

entity "Post" as Post {
+ id [PK] : Int 
--
  * title : String
  content : String
  * published : Boolean
  # authorId : [FK] User
  * createdAt : DateTime
  * updatedAt : DateTime
}

entity "Comment" as Comment {
+ id [PK] : Int 
--
  * content : String
  # postId : [FK] Post
  # authorId : [FK] User
  * createdAt : DateTime
  * updatedAt : DateTime
}

' Relations
Post }o--|| User: authorId
Comment }o--|| Post: postId
Comment }o--|| User: authorId
' ManyToMany Relations
' enum relations
@enduml

Category

Description

Columns

NameTypeDefaultNullableUniqueChildrenParentComment
idIntautoincrementfalsetrue
nameStringfalsetrue

ER diagram

@startuml Category
skinparam linetype ortho
entity "Post" as Post {
+ id [PK] : Int 
--
  * title : String
  content : String
  * published : Boolean
  # authorId : [FK] User
  * createdAt : DateTime
  * updatedAt : DateTime
}

entity "Category" as Category {
+ id [PK] : Int 
--
  * name : [UK] String
}

' Relations
' ManyToMany Relations
Post }o--o{ Category
' enum relations
@enduml

Tag

Description

Columns

NameTypeDefaultNullableUniqueChildrenParentComment
idIntautoincrementfalsetrue
nameStringfalsetrue

ER diagram

@startuml Tag
skinparam linetype ortho
entity "Post" as Post {
+ id [PK] : Int 
--
  * title : String
  content : String
  * published : Boolean
  # authorId : [FK] User
  * createdAt : DateTime
  * updatedAt : DateTime
}

entity "Tag" as Tag {
+ id [PK] : Int 
--
  * name : [UK] String
}

' Relations
' ManyToMany Relations
Post }o--o{ Tag
' enum relations
@enduml

Role

Description

Columns

NameTypeDefaultNullableUniqueChildrenParentComment
idIntautoincrementfalsetrueUserRole
nameStringfalsetrue

ER diagram

@startuml Role
skinparam linetype ortho
entity "Role" as Role {
+ id [PK] : Int 
--
  * name : [UK] String
}

entity "UserRole" as UserRole {
--
  # userId : [FK] User
  # roleId : [FK] Role
}

' Relations
UserRole }o--|| Role: roleId
' ManyToMany Relations
' enum relations
@enduml

UserRole

Description

Columns

NameTypeDefaultNullableUniqueChildrenParentComment
userIdIntfalsefalseUser
roleIdIntfalsefalseRole

ER diagram

@startuml UserRole
skinparam linetype ortho
entity "User" as User {
+ id [PK] : Int 
--
  * email : [UK] String
  name : String
  * createdAt : DateTime
  * updatedAt : DateTime
}

entity "Role" as Role {
+ id [PK] : Int 
--
  * name : [UK] String
}

entity "UserRole" as UserRole {
--
  # userId : [FK] User
  # roleId : [FK] Role
}

' Relations
UserRole }o--|| User: userId
UserRole }o--|| Role: roleId
' ManyToMany Relations
' enum relations
@enduml

Options

exportPerTables

 各テーブルが出力される。デフォルト値はfalse

showUniqueKeyLabel

ER図上で一意のキーとしてもラベル付けされる[UK]。デフォルト値はfalse

lineType

  • ortho
  • polyline
  • unset

デフォルト値はortho

isLeftToRightDirection

trueにすると、PlantUMLleft to right direction指定される。

lineLength

 線の長さを変更できる。デフォルト値は--