Logo

dev-resources.site

for different kinds of informations.

How to draw ER diagram with code using plantuml

Published at
7/2/2019
Categories
plantuml
planttext
beginners
Author
sakko
Categories
3 categories in total
plantuml
open
planttext
open
beginners
open
Author
5 person written this
sakko
open
How to draw ER diagram with code using plantuml

Hi again,

This time I would like to share how I code my ER Diagram using code only.

The tool I'm using is PlantUML. There is an online tool at planttext.com

Why I'm doing this?
I hate using mouse and I love being able to version control my ER Diagram as a code inside git.

Preparation

Online

If you don't want to setup anything, just go ahead and use planttext.com

Local / Atom

Install these packages

plantuml plantuml-viewer or plantuml plantuml-preview

Then create a file for your ER diagram.

touch ~/Desktop/example.pu
Enter fullscreen mode Exit fullscreen mode

I usually go for *.pu for all PlantUML files.

The fun part

Put this in your file then toggle preview using cmd+shift+p then type plantuml.

@startuml
  skinparam linetype ortho
  skinparam packageStyle rectangle
  skinparam shadowing false
  skinparam class {
    BackgroundColor White
    BorderColor Black
    ArrowColor Black
  }
  hide members
  hide circle

  User1 ||-up-|{ Role1
  User2 ||-down-|{ Role2
  User3 ||-left-|{ Role3
  User4 ||-right-|{ Role4

@enduml
Enter fullscreen mode Exit fullscreen mode

This is One and Only One to One or Many

If you don't specify direction, it will default to down.

More options

@startuml
  skinparam linetype ortho
  skinparam packageStyle rectangle
  skinparam shadowing false
  skinparam class {
    BackgroundColor White
    BorderColor Black
    ArrowColor Black
  }
  hide members
  hide circle

  User1 --|{ Role1
  User2 o--|{ Role2
  User3 |o--|{ Role3
  User4 --o{ Role4
  User5 --{ Role5

@enduml
Enter fullscreen mode Exit fullscreen mode

I can't get o--|{ to work properly.

It also work the other way around

@startuml
  skinparam linetype ortho
  skinparam packageStyle rectangle
  skinparam shadowing false
  skinparam class {
    BackgroundColor White
    BorderColor Black
    ArrowColor Black
  }
  hide members
  hide circle

  User1 }|-- Role1
  User2 }o-- Role2
  User3 }--|| Role3
  User4 }--o| Role4
  User5 }--o Role5

@enduml
Enter fullscreen mode Exit fullscreen mode

You can experiment changing it to the other way round.

Showing attributes

By removing hide members (line 10) you can add attributes to your classes. If you add it back, all attributes will be hidden.

@startuml
  skinparam linetype ortho
  skinparam packageStyle rectangle
  skinparam shadowing false
  skinparam class {
    BackgroundColor White
    BorderColor Black
    ArrowColor Black
  }
  hide circle
  class "User" as User1 {
    - int id
    - int age
    - string name
    + ([role]) roles()
    + ([user_role]) user_roles()
  }
  class Role {
    - int id
    - string name
  }
  class "UserRole" as User1Role {
    - int id
    - int user_id
    - int role_id
  }
  User1 ||-right-|{ User1Role
  Role ||-left-|{ User1Role
@enduml
Enter fullscreen mode Exit fullscreen mode

That's it, hope you liked the way I did it.

I also got a gem for rails apps https://github.com/sakKo/prailroady forked from https://github.com/preston/railroady

Let me know if I can improve my post. thankz!

plantuml Article's
28 articles in total
Favicon
Run devcontainers as a non-root user
Favicon
PlantUML to compute diagrams!
Favicon
PlantUMLApp 3.0 - Let's play with AI Multi-Modality
Favicon
Create UML Class Diagrams for Java projects with IntelliJ IDEA and PlantUML
Favicon
Teoria | Documentando arquitetura de software com C4 Model + Plant UML
Favicon
Por que representar a arquitetura de uma aplicação em diagramas?
Favicon
Building a TypeScript-Compatible Webpack Loader: A PlantUML Mind Map Example
Favicon
PlantUML4iPad 2.0
Favicon
Documentation as Code for Cloud - PlantUML
Favicon
PlantUML and Jira: Combining Forces to Simplify Gantt Chart Creation
Favicon
PlantUML meets OpenAI on iPad
Favicon
Draw.io + PlantUML - Como tornar mais fácil o processo de documentação
Favicon
Create Nice-looking Schema Diagrams in PlantUML
Favicon
Keep your diagrams updated with continuous delivery
Favicon
Generate class diagrams with a Kotlin DSL for PlantUML
Favicon
Automatic C4 diagrams for a distributed microservice ecosystem with GitHub Actions
Favicon
PlantUML tips and tricks
Favicon
"Diagram as code"
Favicon
Software architecture documentation - Made easy with arc42 and C4
Favicon
Introduce the new PlantUML IntelliJ Plugin, which has high functionality editor!
Favicon
PlantUML y C4
Favicon
Text based diagramming
Favicon
Software architecture diagrams - which tool should we use?
Favicon
Modelling software architecture with PlantUML
Favicon
How to draw ER diagram with code using plantuml
Favicon
rewrite plantuml with golang or rust
Favicon
Bash/Zsh function to export SVG diagrams using PlantUML
Favicon
Handy Bash/Zsh function to generate PlantUML diagrams

Featured ones: