erdraw 0.0.0-stage → 5.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +246 -2
- package/dist/cli.cjs +26814 -0
- package/package.json +60 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Carlos Vinicius
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,247 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src=".github/assets/banner.png" alt="erdraw: the ER diagram of your database, always current" width="100%">
|
|
3
|
+
</p>
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
# erdraw
|
|
6
|
+
|
|
7
|
+
**Draws the ER diagram of your PostgreSQL, MySQL or MariaDB database, in Mermaid, into your docs.**
|
|
8
|
+
Run it after your migrations and the diagram in the README is always the schema you have. A GitHub
|
|
9
|
+
Action, and a command line.
|
|
10
|
+
|
|
11
|
+
- **Reads the real schema**: tables, columns, primary, foreign and unique keys, enum values, the
|
|
12
|
+
columns' comments and, if you want, indexes. Views, partitions and migration tools' tables are
|
|
13
|
+
left out.
|
|
14
|
+
- **Draws real cardinality**: optional or required parent, one-to-one or one-to-many child, and a
|
|
15
|
+
solid line when the foreign key is part of the child's primary key.
|
|
16
|
+
- **Writes where you read**: a `.mmd` file and, between two markers, your README. GitHub renders
|
|
17
|
+
Mermaid in Markdown.
|
|
18
|
+
- **Keeps itself up to date**: commit the diagram from the workflow when the schema changes, or
|
|
19
|
+
fail a pull request whose diagram is stale with `check: true`.
|
|
20
|
+
|
|
21
|
+
```mermaid
|
|
22
|
+
erDiagram
|
|
23
|
+
coupons {
|
|
24
|
+
text code PK
|
|
25
|
+
integer percent
|
|
26
|
+
}
|
|
27
|
+
events {
|
|
28
|
+
bigint id PK
|
|
29
|
+
date at PK
|
|
30
|
+
}
|
|
31
|
+
order_items {
|
|
32
|
+
integer order_id PK, FK
|
|
33
|
+
integer product_id PK, FK
|
|
34
|
+
integer quantity
|
|
35
|
+
}
|
|
36
|
+
orders {
|
|
37
|
+
integer id PK
|
|
38
|
+
integer user_id FK
|
|
39
|
+
text coupon_code FK
|
|
40
|
+
order_status status "pending, paid, shipped, cancelled"
|
|
41
|
+
}
|
|
42
|
+
products {
|
|
43
|
+
integer id PK
|
|
44
|
+
text name UK
|
|
45
|
+
numeric(10_2) price
|
|
46
|
+
}
|
|
47
|
+
profiles {
|
|
48
|
+
integer user_id PK, FK
|
|
49
|
+
text bio
|
|
50
|
+
text[] tags
|
|
51
|
+
}
|
|
52
|
+
shipments {
|
|
53
|
+
bigint id PK
|
|
54
|
+
integer order_id FK
|
|
55
|
+
integer product_id FK
|
|
56
|
+
timestamp shipped_at
|
|
57
|
+
}
|
|
58
|
+
users {
|
|
59
|
+
integer id PK
|
|
60
|
+
varchar(255) email UK "Where the receipts go"
|
|
61
|
+
text name
|
|
62
|
+
user_status status "active, inactive, suspended"
|
|
63
|
+
integer invited_by FK
|
|
64
|
+
timestamptz created_at
|
|
65
|
+
}
|
|
66
|
+
orders ||--o{ order_items : order_id
|
|
67
|
+
products ||--o{ order_items : product_id
|
|
68
|
+
users ||..o{ orders : user_id
|
|
69
|
+
coupons |o..o{ orders : coupon_code
|
|
70
|
+
users ||--o| profiles : user_id
|
|
71
|
+
order_items ||..o| shipments : "order_id, product_id"
|
|
72
|
+
users |o..o{ users : invited_by
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
<sub>The example schema of the tests, drawn by erdraw: [`tests/fixtures/postgres.sql`](tests/fixtures/postgres.sql).</sub>
|
|
76
|
+
|
|
77
|
+
## Quick start
|
|
78
|
+
|
|
79
|
+
Run your migrations against a database service, then draw it. The diagram lands in
|
|
80
|
+
`docs/database.mmd` and in the README, and is committed when it changed:
|
|
81
|
+
|
|
82
|
+
```yaml
|
|
83
|
+
name: database diagram
|
|
84
|
+
|
|
85
|
+
on:
|
|
86
|
+
push:
|
|
87
|
+
branches: [main]
|
|
88
|
+
|
|
89
|
+
permissions:
|
|
90
|
+
contents: write
|
|
91
|
+
|
|
92
|
+
jobs:
|
|
93
|
+
diagram:
|
|
94
|
+
runs-on: ubuntu-latest
|
|
95
|
+
services:
|
|
96
|
+
postgres:
|
|
97
|
+
image: postgres:17
|
|
98
|
+
env:
|
|
99
|
+
POSTGRES_PASSWORD: postgres
|
|
100
|
+
ports: ["5432:5432"]
|
|
101
|
+
options: --health-cmd pg_isready --health-interval 2s --health-retries 30
|
|
102
|
+
steps:
|
|
103
|
+
- uses: actions/checkout@v7
|
|
104
|
+
- run: npm ci && npx prisma migrate deploy # or flyway, knex, rails db:migrate...
|
|
105
|
+
env:
|
|
106
|
+
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres
|
|
107
|
+
- uses: sugomi-dev/erdraw@v5
|
|
108
|
+
with:
|
|
109
|
+
url: postgres://postgres:postgres@localhost:5432/postgres
|
|
110
|
+
readme: README.md
|
|
111
|
+
commit: true
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Against a database you already have, pass its URL as a secret: `url: ${{ secrets.DATABASE_URL }}`.
|
|
115
|
+
To encrypt the connection, add `?sslmode=require` (or `verify-full`, with `ssl-ca` when its CA is not
|
|
116
|
+
a public one) to the URL, or set `ssl`.
|
|
117
|
+
|
|
118
|
+
### Check the diagram in pull requests
|
|
119
|
+
|
|
120
|
+
`check: true` writes nothing and fails when `docs/database.mmd` (and the README) are not the
|
|
121
|
+
diagram of the database, so a pull request that changes the schema also updates its diagram. It
|
|
122
|
+
needs no write permission, and works on pull requests from forks:
|
|
123
|
+
|
|
124
|
+
```yaml
|
|
125
|
+
- uses: sugomi-dev/erdraw@v5
|
|
126
|
+
with:
|
|
127
|
+
url: postgres://postgres:postgres@localhost:5432/postgres
|
|
128
|
+
readme: README.md
|
|
129
|
+
check: true
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Command line
|
|
133
|
+
|
|
134
|
+
The same diagram, from your machine (Node 24):
|
|
135
|
+
|
|
136
|
+
```sh
|
|
137
|
+
DATABASE_URL=postgres://localhost/shop npx erdraw -o - # print it
|
|
138
|
+
npx erdraw --url mysql://root@localhost/shop --readme README.md # write docs/database.mmd and the README
|
|
139
|
+
npx erdraw --check # exit 1 when they are stale
|
|
140
|
+
npx erdraw --help
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
The options are the inputs below; `--ssl-ca` takes a file.
|
|
144
|
+
|
|
145
|
+
## What the diagram shows
|
|
146
|
+
|
|
147
|
+
| In the database | In the diagram |
|
|
148
|
+
|---|---|
|
|
149
|
+
| A column | `type name`, as the database writes the type (`varchar(255)`, `timestamptz`) |
|
|
150
|
+
| Primary, foreign, unique key | `PK`, `FK`, `UK` after the column (a column can be several) |
|
|
151
|
+
| A column's comment (`COMMENT ON COLUMN`, `COMMENT '...'`) | The column's comment |
|
|
152
|
+
| An enum column | Its values in the column's comment |
|
|
153
|
+
| An index (`indexes: true`) | `index name (columns)` in the comment of its first column; a partial one is `partial unique` or `partial index` |
|
|
154
|
+
| A unique index with a `WHERE` (partial) | No `UK`, and no one-to-one: it is unique only among some rows |
|
|
155
|
+
| A required foreign key | `\|\|` on the parent: exactly one |
|
|
156
|
+
| A nullable foreign key | `\|o` on the parent: zero or one |
|
|
157
|
+
| A unique foreign key (or the primary key) | `o\|` on the child: one to one |
|
|
158
|
+
| Any other foreign key | `o{` on the child: one to many |
|
|
159
|
+
| A foreign key inside the primary key | A solid line (`--`): the child is identified by its parent |
|
|
160
|
+
| Any other foreign key | A dashed line (`..`) |
|
|
161
|
+
|
|
162
|
+
Mermaid accepts only letters, digits, `_`, `-`, `()` and `[]` in types and names: anything else
|
|
163
|
+
becomes `_` (`numeric(10,2)` is `numeric(10_2)`). A table name with other characters keeps its name
|
|
164
|
+
as the entity's label. With several schemas (or MySQL databases), tables are named `schema.table`.
|
|
165
|
+
A foreign key to a table outside the diagram is not drawn; the log names it.
|
|
166
|
+
|
|
167
|
+
## Inputs
|
|
168
|
+
|
|
169
|
+
| Input | Default | What it does |
|
|
170
|
+
|---|---|---|
|
|
171
|
+
| `url` | | `postgres://user:password@host:5432/db`, `mysql://...` or `mariadb://...`; replaces the six below |
|
|
172
|
+
| `type` | `postgresql` | `postgresql`, `mysql` or `mariadb` |
|
|
173
|
+
| `host` | `localhost` | |
|
|
174
|
+
| `port` | `5432` or `3306` | |
|
|
175
|
+
| `database` | | Required without `url` |
|
|
176
|
+
| `user` | | Required without `url` |
|
|
177
|
+
| `password` | | |
|
|
178
|
+
| `ssl` | the URL's `sslmode` | `true` or `require` (encrypted, certificate not verified), `verify-ca`, `verify-full`, `false` |
|
|
179
|
+
| `ssl-ca` | | The CA certificate (PEM) to verify the server with; `verify-full` unless `ssl` says otherwise |
|
|
180
|
+
| `schemas` | `public`, or the database | PostgreSQL schemas or MySQL databases to draw, comma-separated |
|
|
181
|
+
| `include` | | Only these tables, comma-separated; `*` matches anything (`order*`, `billing.*`) |
|
|
182
|
+
| `exclude` | | Tables to leave out, comma-separated; `*` matches anything (`audit_*`). The tables of Flyway, Prisma, Drizzle, Knex, Rails, Sequelize, goose, Alembic, Doctrine and EF are always left out |
|
|
183
|
+
| `output` | `docs/database.mmd` | The diagram file |
|
|
184
|
+
| `readme` | | A Markdown file to write the diagram into |
|
|
185
|
+
| `indexes` | `false` | Show indexes |
|
|
186
|
+
| `check` | `false` | Write nothing; fail when the files are not the diagram |
|
|
187
|
+
| `commit` | `false` | Commit and push the files when they changed |
|
|
188
|
+
| `commit-message` | `docs: update the database diagram` | |
|
|
189
|
+
| `commit-author` | the github-actions bot | `Name <email>` |
|
|
190
|
+
|
|
191
|
+
| Output | |
|
|
192
|
+
|---|---|
|
|
193
|
+
| `diagram` | The Mermaid diagram |
|
|
194
|
+
| `path` | The file it was written to |
|
|
195
|
+
| `changed` | `true` when the diagram or the README changed (with `check`: are stale) |
|
|
196
|
+
|
|
197
|
+
### In the README
|
|
198
|
+
|
|
199
|
+
The diagram goes between `<!-- ER_DIAGRAM_START -->` and `<!-- ER_DIAGRAM_END -->`, and only
|
|
200
|
+
what is between them changes. A README without them gets a `## Database diagram` section at the
|
|
201
|
+
end, which you can then move anywhere.
|
|
202
|
+
|
|
203
|
+
### Committing
|
|
204
|
+
|
|
205
|
+
`commit: true` needs `permissions: contents: write` and a branch: in a pull request, check out
|
|
206
|
+
its branch (`actions/checkout` with `ref: ${{ github.head_ref }}`), since the default checkout of
|
|
207
|
+
a pull request is a merge commit nobody can push to. A push with the workflow's `GITHUB_TOKEN`
|
|
208
|
+
starts no other workflow, so the commit cannot loop. When the branch moved during the run, the
|
|
209
|
+
commit is rebased on it and pushed again.
|
|
210
|
+
|
|
211
|
+
## Coming from er-diagram
|
|
212
|
+
|
|
213
|
+
erdraw is the next major version of `ViniciusDev26/er-diagram` (v1 to v4), renamed and moved to
|
|
214
|
+
sugomi-dev. Workflows on `@v1` to `@v4` keep working through GitHub's redirect. To move to
|
|
215
|
+
`sugomi-dev/erdraw@v5`, rename the inputs:
|
|
216
|
+
|
|
217
|
+
| er-diagram | erdraw |
|
|
218
|
+
|---|---|
|
|
219
|
+
| `db-type`, `db-host`, `db-port`, `db-name`, `db-user`, `db-pass` | `type`, `host`, `port`, `database`, `user`, `password` (or one `url`) |
|
|
220
|
+
| `output-dir: docs` (writing `database-er-diagram.mmd`) | `output: docs/database.mmd` |
|
|
221
|
+
| `write-to-readme` and `readme-path` | `readme: README.md` |
|
|
222
|
+
| `excluded-tables` | `exclude` (migration tables need no listing) |
|
|
223
|
+
| `show-indexes` | `indexes` |
|
|
224
|
+
| `auto-commit`, `commit-author-name`, `commit-author-email` | `commit`, `commit-author: Name <email>` |
|
|
225
|
+
|
|
226
|
+
The diagram changes too: columns are written `type name` (they were reversed), enums are the
|
|
227
|
+
values of their columns instead of extra entities, and the cardinality is read from the keys
|
|
228
|
+
instead of from `ON DELETE CASCADE`.
|
|
229
|
+
|
|
230
|
+
## Development
|
|
231
|
+
|
|
232
|
+
Node 24 ([`mise.toml`](mise.toml)) and Docker: the tests start PostgreSQL 13 and 17, MySQL 8.4 and
|
|
233
|
+
MariaDB 11.4 in containers with [Testcontainers](https://testcontainers.com).
|
|
234
|
+
|
|
235
|
+
```sh
|
|
236
|
+
npm ci
|
|
237
|
+
npm test # every test, with 100% coverage
|
|
238
|
+
npm run check # biome and tsc
|
|
239
|
+
npm run build # dist/index.cjs (the Action) and dist/cli.cjs (npx erdraw): commit them
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
`UPDATE_EXAMPLES=1 npm test` writes [`example/database.mmd`](example/database.mmd) again after a
|
|
243
|
+
change to the diagram. Rules for contributors are in [`CLAUDE.md`](CLAUDE.md).
|
|
244
|
+
|
|
245
|
+
## License
|
|
246
|
+
|
|
247
|
+
[MIT](LICENSE). erdraw is a [sugomi](https://github.com/sugomi-dev) tool.
|