DEV Community

Sualeh Fatehi
Sualeh Fatehi

Posted on • Updated on

Generate Mermaid Diagrams for Your Database

mermaid describes itself as a "Markdownish syntax" for generating entity-relationship diagrams. You can learn syntax in minutes, but it may take you longer to represent your database. SchemaCrawler generates mermaid syntax from your existing database. Then you can see what it looks like in the mermaid live editor, as well as make modifications.

Try it out. Make sure that you have Docker installed on your system, or download SchemaCrawler. Download a sample SQLite database called "chinook-database-2.0.1.sqlite" into your current directory.

Then run a command like:

docker run \
--mount type=bind,source="$(pwd)",target=/home/schcrwlr/share \
--rm -it \
schemacrawler/schemacrawler \
/opt/schemacrawler/bin/schemacrawler.sh \
--server=sqlite \
--database=share/chinook-database-2.0.1.sqlite \
--info-level=standard \
--command script \
--script-language python \
--script mermaid.py
Enter fullscreen mode Exit fullscreen mode

(If you are using PowerShell on Windows, replace the trailing backslash on each line with a back-tick "`".)

Copy the output into the mermaid live editor and see what it looks like. Once you have got this working, you can connect to any database - whether Oracle, MySQL, PostgreSQL, Microsoft SQL Server, IBM DB2 or any other database that SchemaCrawler supports.

If you want to customize the script, grab the source from of "mermaid.py" from SchemaCrawler's GitHub repository, and follow instructions on the scripting gist.

To learn about how to use SchemaCrawler, download the SchemaCrawler distribution zip file and go through the examples.

Top comments (6)

Collapse
 
lastlink profile image
lastlink

Got a better example of this working see github.com/Software-Developers-IRL....

To run on windows cmd.exe:

          docker run ^
            -v "%cd%":/home/schcrwlr/share ^
            --name schemacrawler ^
            --rm -i -t ^
            --entrypoint="/bin/bash" ^
            schemacrawler/schemacrawler
Enter fullscreen mode Exit fullscreen mode

run mermaid script, need to have the mermaid.py and sqlite file in the same directory where you launched

      schemacrawler \
        --server=sqlite --database=./share/chinook-database-2.0.1.sqlite \
        --info-level=maximum \
        --command script \
        --script-language python \
        --script ./share/mermaid.py > ./share/output.md
Enter fullscreen mode Exit fullscreen mode
Collapse
 
lastlink profile image
lastlink

Package now released. You can generate sql DDLs from markdown mermaid erDiagram.
See npmjs.com/package/@funktechno/litt....

Collapse
 
adriens profile image
adriens

Go to killercoda.com/schemacrawler for up-to-date Schemacrawler course

Collapse
 
kgilpin profile image
Kevin Gilpin

Can you show an example of the generated diagram?

Collapse
 
sualeh profile image
Sualeh Fatehi
Collapse
 
adriens profile image
adriens

Hi @sualeh : could you please provide us the killercoda version ?