DEV Community

Cover image for Hacktoberfest 2022: SQL DDL import and export for Draw.io and Mermaid
lastlink
lastlink Subscriber

Posted on Edited on

Hacktoberfest 2022: SQL DDL import and export for Draw.io and Mermaid

Hacktoberfest: Contribution Chronicles

Writing a tool that generates SQL DDL from a diagram had been on my to-do list for a while, using open source tools if I could.

I like modeling tables and relationships as UML and entity relation diagrams. Lucidchart was the only tool I found that exported SQL DDL well. Other tools have export options for one specific database, but nothing that moved easily between database types.

Mermaid has an erDiagram type, and diagrams.net (Draw.io) has UML and entity relation shapes. I changed both so a database schema can be imported as a diagram and exported again as SQL DDL. I'm happy with how it came out.

Packages

Mermaid

@funktechno/little-mermaid-2-the-sql converts a Mermaid erDiagram to SQL DDL. The samples also show how to generate an erDiagram from an existing database with SchemaCrawler.

A sample diagram:

erDiagram
%% comment 1
Persons {
    int PersonID PK "NOT NULL"
    varchar255 LastName
    varchar255 FirstName
    varchar255 Address
    varchar255 City
}

%% comment 2
Orders {
    int OrderID PK "NOT NULL"
    int PersonID FK "NOT NULL"
}

Persons ||--o{ Orders : "[Persons.PersonId] to [Orders.PersonId]"

And its output:

CREATE TABLE "Persons" (
"City" varchar(255),
"Address" varchar(255),
"FirstName" varchar(255),
"LastName" varchar(255),
"PersonID" int NOT NULL,
PRIMARY KEY("PersonID")
);

CREATE TABLE "Orders" (
"PersonID" int NOT NULL,
"OrderID" int NOT NULL,
PRIMARY KEY("OrderID"),
FOREIGN KEY ("PersonId") REFERENCES "Persons"("PersonId")
);
🏆 ยอดนิยม: lastlink/my hacktober contributions bm - Kapook Update 2025 🏆 ยอดนิยม: lastlink/my hacktober contributions bm - Kapook Update 2025

SQL parser

@funktechno/sqlsimpleparser converts SQL DDL into simple JSON models that are easy to import into Draw.io.

Draw.io pull requests

I opened two pull requests against Draw.io to add SQL import and export. Their README says they don't take outside contributions, so both were closed. The plugins still work when you run them manually. I tested with the desktop repo and an Electron debugger.

I moved the changes into sqltooling-drawio, which has notes on several ways to install the plugins and is easier to contribute to. It has a SQL plugin for importing and exporting DDL, and a NoSQL one for importing and exporting an OpenAPI spec and TypeScript interfaces.

The closed pull requests:

  1. Export SQL DDL plugin

export sql preview

  1. Improved SQL parser for the SQL import plugin

insert sql uml diagram preview

2025 update

See Hacktoberfest 2025.

Top comments (0)