Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

prisma-erd-generator

keonik138.5kMIT2.0.4TypeScript support: included

[![All Contributors](https://img.shields.io/badge/all_contributors-20-orange.svg?style=flat-square)](#contributors-)

Prisma, TypeScript, Mermaid, Entity Relationship Diagram, ERD

readme

Prisma Entity Relationship Diagram Generator

All Contributors

Prisma generator to create an ER Diagram every time you generate your prisma client.

Like this tool? @Skn0tt started this effort with his web app ER diagram generator

npm i -D prisma-erd-generator @mermaid-js/mermaid-cli
# or
yarn add -D prisma-erd-generator @mermaid-js/mermaid-cli

Add to your schema.prisma

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

Run the generator

npx prisma generate

Example ER Diagram

Versions

  • Prisma >= 4 use 1.x.x
  • Prisma <4 use 0.11.x

Options

Additional configuration

Output

Change output type and location

Usage

generator erd {
  provider = "prisma-erd-generator"
  output = "../ERD.svg"
}

Extensions

  • svg (default: ./prisma/ERD.svg)
  • png
  • pdf
  • md

Theme

Theme selection

Usage

generator erd {
  provider = "prisma-erd-generator"
  theme = "forest"
}

Options

  • default (default)
  • forest
  • dark
  • neutral

This option does not accept environment variables or other dynamic values. If you want to change the theme dynamically, you can use the theme option in the mermaidConfig option. See Mermaid Configuration for more information.

mmdcPath

In order for this generator to succeed you must have mmdc installed. This is the mermaid cli tool that is used to generate the ERD. By default the generator searches for an existing binary file at /node_modules/.bin. If it fails to find that binary it will run find ../.. -name mmdc to search through your folder for a mmdc binary. If you are using a different package manager or have a different location for your binary files, you can specify the path to the binary file.

generator erd {
  provider = "prisma-erd-generator"
  theme = "forest"
  mmdcPath = "node_modules/.bin"
}

Use with yarn 3+

Yarn 3+ doesn't create a node_modules/.bin directory for scripts when using the pnp or pnpm nodeLinkers (see yarn documentation here). It instead makes scripts available directly from the package.json file using yarn <script_name>, which means that there won't be an mmdc file created at all. This issue can be solved by creating your own shell script named mmdc inside your project's files that runs yarn mmdc (note: this shell script does not need to be added to your package.json file's scripts section - prisma-erd-generatr will access this script directly).

An example mmdc script:

#!/bin/bash

# $@ passes the parameters that this mmdc script was run with along to the mermaid cli
yarn mmdc $@

Make this mmdc script executable by using the command chmod +x mmdc, then set the mmdcPath option to point to the directory where the mmdc file you've just created is stored.

Disabled

You won't always need to generate a new ERD. For instance, when you are building your docker containers you often run prisma generate and if this generator is included, odds are you aren't relying on an updated ERD inside your docker container. It also adds additional space to the container because of dependencies such as puppeteer. There are two ways to disable this ERD generator.

  1. Via environment variable
DISABLE_ERD=true
  1. Via configuration
generator erd {
  provider = "prisma-erd-generator"
  disabled = true
}

Another option used is to remove the generator lines from your schema before installing dependencies and running the prisma generate command. I have used sed to remove the lines the generator is located on in my schema.prisma file to do so. Here is an example of the ERD generator being removed on lines 5-9 in a dockerfile.

# remove and replace unnecessary generators (erd generator)
# Deletes lines 5-9 from prisma/schema.prisma
RUN sed -i '5,9d' prisma/schema.prisma

Debugging

If you have issues with generating or outputting an ERD as expected, you may benefit from seeing output of the steps to making your ERD. Enable debugging by either adding the following environment variable

ERD_DEBUG=true

or adding in the debug configuration key set to true

generator erd {
  provider = "prisma-erd-generator"
  erdDebug = true
}

and re-running prisma generate. You should see a directory and files created labeling the steps to create an ER diagram under prisma/debug.

Please use these files as part of opening an issue if you run into problems.

Table only mode

Table mode only draws your models and skips the attributes and columns associated with your table. This feature is helpful for when you have lots of table columns and they are less helpful than seeing the tables and their relationships

generator erd {
  provider = "prisma-erd-generator"
  tableOnly = true
}

Ignore enums

If you enable this option, enum entities will be hidden. This is useful if you want to reduce the number of entities and focus on the tables and their columns and relationships.

generator erd {
  provider = "prisma-erd-generator"
  ignoreEnums = true
}

Include relation from field

By default this module skips relation fields in the result diagram. For example fields userId and productId will not be generated from this prisma schema.

model User {
  id            String         @id
  email         String
  favoriteProducts  FavoriteProducts[]
}


model Product {
  id              String        @id
  title           String
  inFavorites  FavoriteProducts[]
}

model FavoriteProducts {
  userId      String
  user        User    @relation(fields: [userId], references: [id])
  productId   String
  product     Product @relation(fields: [productId], references: [id])

  @@id([userId, productId])
}

It can be useful to show them when working with RDBMS. To show them use includeRelationFromFields = true

generator erd {
  provider = "prisma-erd-generator"
  includeRelationFromFields = true
}

Disable emoji output

The emoji output for primary keys (🗝️) and nullable fields () can be disabled, restoring the older values of PK and nullable, respectively.

generator erd {
  provider     = "prisma-erd-generator"
  disableEmoji = true
}

Mermaid configuration

Overriding the default mermaid configuration may be necessary to represent your schema in the best way possible. There is an example mermaid config here that you can use as a starting point. In the example JavaScript file, types are referenced to view all available options. You can also view them here. The most common use cases for needing to overwrite mermaid configuration is for theming and default sizing of the ERD.

generator erd {
  provider = "prisma-erd-generator"
  mermaidConfig = "mermaidConfig.json"
}

Puppeteer configuration

If you want to change the configuration of Puppeteer, create a Puppeteer config file (JSON) and pass the file path to the generator.

generator erd {
  provider = "prisma-erd-generator"
  puppeteerConfig = "../puppeteerConfig.json"
}

Issues

Because this package relies on mermaid js and puppeteer issues often are opened that relate to those libraries causing issues between different versions of Node.js and your operating system. As a fallback, if you are one of those people not able to generate an ERD using this generator, try running the generator to output a markdown file .md first. Trying to generate a markdown file doesn't run into puppeteer to represent the contents of a mermaid drawing in a browser and often will succeed. This will help get you a functioning ERD while troubleshooting why puppeteer is not working for your machine. Please open an issue if you have any problems or suggestions.

🔴 ARM64 Users 🔴

Puppeteer does not yet come shipped with a version of Chromium for arm64, so you will need to point to a Chromium executable on your system. More details on this issue can be found here.

MacOS Fix:

Install Chromium using Brew:

brew install --cask --no-quarantine chromium

You should now see the path to your installed Chromium.

which chromium

The generator will use this Chromium instead of the one provided by Puppeteer.

Other Operating Systems:

This can be fixed by either:

  • Setting the executablePath property in your puppeteer config file to the file path of the Chromium executable on your system.
  • Setting the following global variables on your system
    PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
    PUPPETEER_EXECUTABLE_PATH=path_to_your_chromium

Contributors ✨

Thanks goes to these wonderful people (emoji key):

John Fay
John Fay

🚧 💻 🤔 🐛
Jonas Strassel
Jonas Strassel

🐛 💻
Steve Gray
Steve Gray

💻 🤔
Jason Abbott
Jason Abbott

🐛 💻
Manuel Maute
Manuel Maute

🐛 💻
James Homer
James Homer

💻
Jan Piotrowski
Jan Piotrowski

🐛 💻 👀
Luke Evers
Luke Evers

💻
rikuyam
rikuyam

💻
Francis Manansala
Francis Manansala

🐛
Vitalii Yarmus
Vitalii Yarmus

💻
Petri Julkunen
Petri Julkunen

🐛
D-PONTARO
D-PONTARO

💻
Stephen Ramthun
Stephen Ramthun

📖
Tristan Chin
Tristan Chin

💻
Brandin Canfield
Brandin Canfield

💻
kota marusue
kota marusue

📖
Lucia
Lucia

🐛
Austin Ziegler
Austin Ziegler

💻
Janeene Beeforth
Janeene Beeforth

📖

This project follows the all-contributors specification. Contributions of any kind welcome!

changelog

Changelog

2.0.4

Patch Changes

  • 464d8b9: Rename .mjs files to .js for binary references to work within prisma generator calls

2.0.2

Patch Changes

  • b438731: CommonJS bin file connection

2.0.1

Patch Changes

  • 94e16d6: Point bin and main files to commonjs

2.0.1

Patch Changes

  • 48c2882: Revert back to commonjs

2.0.0

Major Changes

  • abe34ac: BREAKING CHANGES

    • Removal of Node 16 support

    Features

    • Added support for Prisma v6 #249
    • Local testing migration to vitest
    • Migration to pnpm for dependency management
    • Migration to biome for linting and style guide
    • Update to all minor dependencies to include latest mermaid configuration allowed with mermaid cli
    • Output commonjs and module exports for better compatibility with other tools
    • Migrate to tsup for bundling

    Bug Fixes

    • Remove config option from child_process that was hiding output #250

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.11.1 (2023-08-22)

1.11.0 (2023-08-10)

Features

  • Enable Prisma v5 as a peer dependency. Update all other dependencies. Enabled Logging of config in debug mode. (#231) (85faec5)
  • Option to disable emoji output (#229) (f86ff4d), closes #226

Bug Fixes

  • Remove generator options logging (#230) (d891310)
  • unlock versions of prisma dependencies to include 4 and 5 (b7137d2)
  • Update DISABLE_ERD environment variable to match js truthy expectations (#227) (60a78ba), closes #225

1.9.0 (2023-07-19)

Features

Bug Fixes

  • Default version outdated (233afe0)
  • Dependency upgrade (757f57f)
  • Improve logging for arm64 macos users. Force version 5 prisma to match prisma generator helper (872d6a2)
  • Match @mermaid-cli puppeteer-config.json as default to troubleshoot issue #199 (6a61964)
  • No changes. Just attempting to align versions. (881a0ef)
  • Prerelease config for GitHub (6a2ec99)
  • Remove Git semantic release plugin (fba4a7d)
  • Trim mmdc from mmdcPath. Added test and documentation for how to utilize new config ption. Resolve tests from nullable and primary key changes. (9c56a47)
  • Versioning mismatches (8e31427)

1.4.5 (2023-03-25)

Bug Fixes

  • Add type definitions to package. remove markdown files from package output. (561a98f)

1.4.4 (2023-03-25)

Bug Fixes

  • tarball and deployment setup for releaserc (5f3ee76)

1.4.3 (2023-03-25)

Bug Fixes

  • Improve labeling of nullable and primary keys in table (dbd9117)

1.4.2 (2023-03-25)

1.4.1 (2023-03-25)

Bug Fixes

  • Add back in prepublish script (4ab1dd5)
  • remove duplicate build step (23d86c3)
  • Revert removal of ts files from npm package (a4628e7)

1.4.0 (2023-03-25)

Features

1.8.0 (2023-05-25)

Features

Bug Fixes

  • Composite type relationship lookup resolution. Dependency upgrades: @mermaid-js/mermaid-cli (94ddfd2)
  • Typo in documentation (#193) (e6a3838)

1.5.2 (2023-03-28)

Bug Fixes

  • Resolve many to many issue since adding in double quotes to names (69164bd)

1.5.1 (2023-03-28)

Features

  • Merge in alpha branch. Replace node v14 with node v18 for github actions tests (#191) (1b1d618), closes #175 #188 #190
  • Missing @prisma/generator-helper dependency (ce040d3)

1.4.5 (2023-03-25)

Bug Fixes

  • Add type definitions to package. remove markdown files from package output. (561a98f)

1.4.4 (2023-03-25)

Bug Fixes

  • tarball and deployment setup for releaserc (5f3ee76)

1.4.3 (2023-03-25)

Bug Fixes

  • Improve labeling of nullable and primary keys in table (dbd9117)

1.4.4 (2023-03-25)

1.4.2 (2023-03-25)

1.4.1 (2023-03-25)

Bug Fixes

  • Add back in prepublish script (4ab1dd5)
  • remove duplicate build step (23d86c3)
  • Revert removal of ts files from npm package (a4628e7)

1.4.0 (2023-03-25)

Features

1.3.2 (2023-03-22)

Bug Fixes

  • Bump verison to deploy to main (fec903a)
  • Documentation to assist users with puppeteer issues (bf46791)

1.3.0 (2023-03-22)

Features

  • Upgrade to mermaid version 10. Support for early release branching. Resolve new version of ERD on every generate. (#185) (2a6f2cb), closes #175

1.2.5 (2023-01-12)

Bug Fixes

  • Downgrade @mermaid/cli to version 8 to resolve deterministic id configuration parameter. Fix for test issue 138 (#162) (84a6287)

1.2.4 (2022-11-17)

Bug Fixes

1.2.3 (2022-11-08)

Bug Fixes

  • Remove prefix npx from tests to ensure current version in package.json usage of prisma (1c6e285)

1.2.2 (2022-10-31)

1.2.1 (2022-09-29)

1.2.0 (2022-09-10)

Features

  • Added possibility to generate diagram with relation fields and Fixed generation of ERD with composite keys.(Closes #135) (#136) (f36325b)

1.1.2 (2022-09-08)

Bug Fixes

  • Zero to many relationship linking incorrect (#125) (c283fc8)

1.1.1 (2022-09-08)

1.1.0 (2022-08-19)

Features

1.0.2 (2022-07-26)

Bug Fixes

  • Issue with matching names when overwritting field names to @map names (#115) (6ffc13f)

1.0.1 (2022-07-11)

0.11.5 (2022-07-11)

0.11.4 (2022-05-26)

Features

0.11.3 (2022-05-09)

0.11.2 (2022-05-09)

Features

0.11.1 (2022-04-04)

0.10.2 (2022-04-04)