hekireki 0.20.1 → 0.21.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/README.md +74 -15
- package/dist/bin/activerecord.js +1 -1
- package/dist/bin/ajv.js +1 -1
- package/dist/bin/arktype.js +1 -1
- package/dist/bin/atlas.js +1 -1
- package/dist/bin/django.js +1 -1
- package/dist/bin/drizzle.js +1 -1
- package/dist/bin/ecto.js +1 -1
- package/dist/bin/effect.js +1 -1
- package/dist/bin/eloquent.js +1 -1
- package/dist/bin/er.js +1 -1
- package/dist/bin/gorm.js +1 -1
- package/dist/bin/hekireki.js +2 -2
- package/dist/bin/kysely.js +1 -1
- package/dist/bin/pydantic.js +1 -1
- package/dist/bin/sea-orm.js +1 -1
- package/dist/bin/sqlalchemy.js +1 -1
- package/dist/bin/typebox.js +1 -1
- package/dist/bin/valibot.js +1 -1
- package/dist/bin/zod.js +1 -1
- package/dist/{bin-CPb5ZV8W.js → bin-Ca0TR-Zh.js} +67 -35
- package/dist/{schema-DwMNeEei.js → schema-Ci19PgR9.js} +136 -1
- package/dist/{start-Blp4k61D.js → start-EKGd2Tvk.js} +4610 -197
- package/dist/studio/assets/chip-CLdO-gL5.js +1 -0
- package/dist/studio/assets/copy-CcAufM8p.js +1 -0
- package/dist/studio/assets/debounce-T36H3Pq5.js +3 -0
- package/dist/studio/assets/dist-Ax_1vG1u.js +12 -0
- package/dist/studio/assets/docs-DW8-viIy.js +24 -0
- package/dist/studio/assets/enums._name-C2s5pbQ3.js +1 -0
- package/dist/studio/assets/hooks-Ctp_pGlY.js +1 -0
- package/dist/studio/assets/index-BP1uJf2T.js +16 -0
- package/dist/studio/assets/index-CYncH7vk.css +2 -0
- package/dist/studio/assets/labels-BXtM_faz.js +1 -0
- package/dist/studio/assets/lazyRouteComponent-C6zg30cR.js +1 -0
- package/dist/studio/assets/lu-BKd2Ee9U.js +7 -0
- package/dist/studio/assets/models._name-BgQ9M7k3.js +1 -0
- package/dist/studio/assets/not-found-OmQoujxN.js +1 -0
- package/dist/studio/assets/prisma-BbI26USq.js +891 -0
- package/dist/studio/assets/routes-Bp4Bkwc2.js +1 -0
- package/dist/studio/assets/schema-gate-Dq_XVfAD.js +1 -0
- package/dist/studio/assets/schema-view-BuWOw5GX.js +8 -0
- package/dist/studio/assets/sql--CSU_IR2.js +7 -0
- package/dist/studio/assets/table-Dfy6EciQ.js +1 -0
- package/dist/studio/assets/tabs-Ce14hvOa.js +1 -0
- package/dist/studio/assets/useFormValidation-gdk4CRGT.js +1 -0
- package/dist/studio/assets/useMatch-CdyJXF1Q.js +1 -0
- package/dist/studio/assets/useMultipleSelectionState-a9-PqoPk.js +1 -0
- package/dist/studio/assets/useOverlayTriggerState-DCPjC07T.js +1 -0
- package/dist/studio/index.html +9 -5
- package/package.json +20 -7
- package/dist/studio/assets/cells-lAj-v64A.js +0 -2
- package/dist/studio/assets/docs-CpeTt3O2.js +0 -34
- package/dist/studio/assets/enums._name-My5PypWw.js +0 -1
- package/dist/studio/assets/hooks-CiUANSpJ.js +0 -1
- package/dist/studio/assets/index-C4wWEcwe.js +0 -188
- package/dist/studio/assets/index-ZSt1ppSG.css +0 -2
- package/dist/studio/assets/labels-DBXIkmbm.js +0 -1
- package/dist/studio/assets/lazyRouteComponent-DmCXXiGp.js +0 -1
- package/dist/studio/assets/models._name-oUX64rwK.js +0 -1
- package/dist/studio/assets/not-found-DxAsuyR5.js +0 -1
- package/dist/studio/assets/prisma-Bk53qnxm.js +0 -891
- package/dist/studio/assets/routes-ZmrohHNP.js +0 -1
- package/dist/studio/assets/schema-gate-DJ_-6NYR.js +0 -1
- package/dist/studio/assets/schema-view-BaoCWAL8.js +0 -8
- package/dist/studio/assets/sql-DpzkSc6F.js +0 -1
- package/dist/studio/assets/useMatch-D73HO_WB.js +0 -1
package/README.md
CHANGED
|
@@ -24,7 +24,8 @@ npm install -D hekireki
|
|
|
24
24
|
|
|
25
25
|
Each generator is a `generator` block in `schema.prisma`. `output` is **required**: point it at a
|
|
26
26
|
directory to get the default file name of that generator, or at a path with an extension to name
|
|
27
|
-
the file yourself.
|
|
27
|
+
the file yourself. `hekireki-er` is the exception: it has no default file name, so it only takes
|
|
28
|
+
paths to files, extension included (see [ER diagrams](#er-diagrams)).
|
|
28
29
|
|
|
29
30
|
```prisma
|
|
30
31
|
datasource db {
|
|
@@ -170,7 +171,7 @@ model Post {
|
|
|
170
171
|
| `hekireki-activerecord` | Ruby (Rails ≥ 7.1) | one `.rb` per model | — |
|
|
171
172
|
| `hekireki-eloquent` | PHP (Laravel, PHP ≥ 8.1) | one `.php` per model and enum | `namespace` |
|
|
172
173
|
| `hekireki-atlas` | Atlas HCL | `schema.hcl` | `schemaName`, `comment` |
|
|
173
|
-
| `hekireki-er` | Mermaid / DBML / PNG / SVG | — (
|
|
174
|
+
| `hekireki-er` | Mermaid / DBML / PNG / SVG | — ([extension required](#er-diagrams)) | `outputs`, `theme` |
|
|
174
175
|
|
|
175
176
|
## Configuration
|
|
176
177
|
|
|
@@ -237,25 +238,55 @@ generator Hekireki-Eloquent {
|
|
|
237
238
|
namespace = "App.Models" // PHP namespace, "." becomes "\" (default: App\Models)
|
|
238
239
|
}
|
|
239
240
|
|
|
241
|
+
// ER takes output or outputs (not both). Every path is a file ending in .md, .dbml, .png or .svg,
|
|
242
|
+
// never a directory, and its extension picks the format (see ER diagrams).
|
|
240
243
|
generator Hekireki-ER {
|
|
241
|
-
provider
|
|
242
|
-
output
|
|
243
|
-
outputs
|
|
244
|
-
|
|
245
|
-
theme = "light" // .png / .svg only. "light" (default) or "dark"
|
|
244
|
+
provider = "hekireki-er"
|
|
245
|
+
// output = "docs/er.md" // One file: output alone is enough
|
|
246
|
+
outputs = ["docs/er.md", "docs/schema.dbml", "docs/er.svg"] // Several files: list them here
|
|
247
|
+
theme = "light" // .png / .svg only. "light" (default) or "dark"
|
|
246
248
|
}
|
|
247
249
|
```
|
|
248
250
|
|
|
249
251
|
## ER diagrams
|
|
250
252
|
|
|
251
253
|
`hekireki-er` writes the ER model of the schema in four formats, and the extension of each file
|
|
252
|
-
picks which — so the file you name is the file you get
|
|
253
|
-
|
|
254
|
-
a single file, point `output` straight at it and leave `outputs` out.
|
|
254
|
+
picks which — so the file you name is the file you get. Unlike the other generators it has no
|
|
255
|
+
default file name, so every file has to be named with one of these extensions:
|
|
255
256
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
257
|
+
| Extension | Writes |
|
|
258
|
+
| --------- | ---------------------- |
|
|
259
|
+
| `.md` | A Mermaid `erDiagram` |
|
|
260
|
+
| `.dbml` | DBML |
|
|
261
|
+
| `.png` | The diagram, as a PNG |
|
|
262
|
+
| `.svg` | The diagram, as an SVG |
|
|
263
|
+
|
|
264
|
+
Name the files with `output` or with `outputs` — one of the two, not both. When one file is all
|
|
265
|
+
you need, `output` alone is enough:
|
|
266
|
+
|
|
267
|
+
```prisma
|
|
268
|
+
generator Hekireki-ER {
|
|
269
|
+
provider = "hekireki-er"
|
|
270
|
+
output = "docs/er.md" // Writes docs/er.md
|
|
271
|
+
}
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
For several formats at once, list them in `outputs` instead:
|
|
275
|
+
|
|
276
|
+
```prisma
|
|
277
|
+
generator Hekireki-ER {
|
|
278
|
+
provider = "hekireki-er"
|
|
279
|
+
outputs = ["docs/er.md", "docs/er.svg"] // Writes docs/er.md and docs/er.svg
|
|
280
|
+
}
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
Every path is a whole file, directory and extension included, resolved the way Prisma resolves any
|
|
284
|
+
`output`: against the directory of the schema file that declares the generator. Neither ever names
|
|
285
|
+
a directory, so a path with no extension, or with one outside the four, is an error rather than a
|
|
286
|
+
guess — `output = "docs"` fails.
|
|
287
|
+
|
|
288
|
+
Every option belongs to a format (`theme` to `.png` and `.svg`), and setting one that no chosen
|
|
289
|
+
format reads is an error — so an option never looks like it did something it did not.
|
|
259
290
|
|
|
260
291
|
`.md` writes a Mermaid diagram:
|
|
261
292
|
|
|
@@ -273,7 +304,9 @@ erDiagram
|
|
|
273
304
|
}
|
|
274
305
|
```
|
|
275
306
|
|
|
276
|
-
`.dbml` writes the same model for [dbdiagram.io](https://dbml.dbdiagram.io/)
|
|
307
|
+
`.dbml` writes the same model for [dbdiagram.io](https://dbml.dbdiagram.io/) as the database
|
|
308
|
+
names it: tables, columns, enums and enum values under their `@@map` / `@map` names wherever the
|
|
309
|
+
schema declares one. `.png` and `.svg`
|
|
277
310
|
draw the diagram Hekireki Studio shows, laid out automatically: a card per model and enum, its
|
|
278
311
|
fields with `🔑` / `🔗` / `UK` marks, the attributes and `///` prose under each, the `@@id` /
|
|
279
312
|
`@@unique` / `@@index` block attributes, and an edge per relation in crow's-foot notation captioned
|
|
@@ -307,7 +340,33 @@ hekireki studio -p 3000
|
|
|
307
340
|
| `/models/<name>` | A model's rows, editable, next to its fields |
|
|
308
341
|
| `/enums/<name>` | An enum with its members and the fields that hold it |
|
|
309
342
|
| `/prisma` | The schema in a Prisma editor, with the language server's diagnostics |
|
|
310
|
-
| `/sql` | A SQL console
|
|
343
|
+
| `/sql` | A SQL console: completion from the schema, a data-flow picture, the query plan |
|
|
344
|
+
|
|
345
|
+
**⌘K** / **Ctrl+K** opens a search over the whole schema — every page, model, enum and field, by
|
|
346
|
+
the letters of its name in order, so `usemail` finds `User.email`. Following a field opens its
|
|
347
|
+
model on that row.
|
|
348
|
+
|
|
349
|
+
On a model's page, **/** puts the cursor in the search box and every column is searched as you
|
|
350
|
+
type, with what matched marked in the rows that come back. Clicking a cell picks it and nothing
|
|
351
|
+
more; the two buttons that appear on it copy just that value or open it for editing, and **⌘C** /
|
|
352
|
+
**Ctrl+C** copies the picked cell. Tick rows to act on them together: **Copy** takes them as
|
|
353
|
+
tab-separated text — what a spreadsheet pastes — and **Delete** asks first, naming the rows it is
|
|
354
|
+
about to remove. **Export** downloads or copies the page as CSV or JSON. A SQL result is sorted and narrowed in the browser,
|
|
355
|
+
without asking the database a second question.
|
|
356
|
+
|
|
357
|
+
The SQL page completes table and column names from the schema — `@@map` / `@map` names, as the
|
|
358
|
+
database knows them — and reads the statement as you type: **Flow** draws where its rows come from
|
|
359
|
+
(every table, join, filter, grouping and the projection), **Columns** traces each result column back
|
|
360
|
+
to the base tables and lists every table touched with its alias and the columns read, and **Type**
|
|
361
|
+
shows the row and parameter types as TypeScript, and **Plan** asks the database for its execution
|
|
362
|
+
plan (`EXPLAIN`) as a tree. The models the statement touches light up on the
|
|
363
|
+
schema beside it, with the columns it reads marked; a `?` or `$1` placeholder gets a field typed
|
|
364
|
+
from the column it is compared with, and a name the schema does not know is underlined.
|
|
365
|
+
|
|
366
|
+
**Columns** in the toolbar says which columns the grid draws, remembered per model; what is on
|
|
367
|
+
screen is what **Copy** and **Export** take, so a copy of two columns out of forty is two ticks
|
|
368
|
+
away. A row is still written whole, so the Add row form brings every column back for as long as it
|
|
369
|
+
is open.
|
|
311
370
|
|
|
312
371
|
`--schema` takes either a `schema.prisma` file or a directory — every `.prisma` file directly in the
|
|
313
372
|
directory (subdirectories are not descended into) is read as one schema, which is how Prisma's
|
package/dist/bin/activerecord.js
CHANGED
package/dist/bin/ajv.js
CHANGED
package/dist/bin/arktype.js
CHANGED
package/dist/bin/atlas.js
CHANGED
package/dist/bin/django.js
CHANGED
package/dist/bin/drizzle.js
CHANGED
package/dist/bin/ecto.js
CHANGED
package/dist/bin/effect.js
CHANGED
package/dist/bin/eloquent.js
CHANGED
package/dist/bin/er.js
CHANGED
package/dist/bin/gorm.js
CHANGED
package/dist/bin/hekireki.js
CHANGED
|
@@ -5,7 +5,7 @@ import { Config, Console, Data, Effect, Option, Schema, Stdio } from "effect";
|
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import { CliError, Command, Flag } from "effect/unstable/cli";
|
|
7
7
|
//#region package.json
|
|
8
|
-
var version = "0.
|
|
8
|
+
var version = "0.21.0";
|
|
9
9
|
//#endregion
|
|
10
10
|
//#region src/studio/server/constants/index.ts
|
|
11
11
|
/** Studio only listens on loopback: the API serves the raw schema and runs SQL without authentication. */
|
|
@@ -103,7 +103,7 @@ const studioFlags = {
|
|
|
103
103
|
function runStudio(args) {
|
|
104
104
|
return Effect.gen(function* () {
|
|
105
105
|
const schemaPath = yield* resolveSchemaPath(Option.getOrNull(args.schema), [COMMAND_NAME, "studio"]);
|
|
106
|
-
const { startStudioServer } = yield* Effect.promise(() => import("../start-
|
|
106
|
+
const { startStudioServer } = yield* Effect.promise(() => import("../start-EKGd2Tvk.js"));
|
|
107
107
|
const started = yield* startStudioServer({
|
|
108
108
|
schemaPath,
|
|
109
109
|
port: args.port,
|
package/dist/bin/kysely.js
CHANGED
package/dist/bin/pydantic.js
CHANGED
package/dist/bin/sea-orm.js
CHANGED
package/dist/bin/sqlalchemy.js
CHANGED
package/dist/bin/typebox.js
CHANGED
package/dist/bin/valibot.js
CHANGED
package/dist/bin/zod.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { c as writeFile, i as makeDirectory, n as fileSystemLayer } from "./file-yN0By3gn.js";
|
|
2
|
-
import { _ as
|
|
2
|
+
import { _ as makeValidationExtractor, a as erRelations, b as stripAnnotations, c as getBool, d as groupByModel, f as isFields, g as makeSnakeCase, h as makePascalCase, i as erKey, l as getString, m as makeCommentBlock, n as makeSchema, o as inferredERRelations, r as annotatedERRelations, s as extractObjectType, u as getStrings, v as parseDocumentWithoutAnnotations, y as schemaFromFields } from "./schema-Ci19PgR9.js";
|
|
3
3
|
import { Data, Effect } from "effect";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import pkg from "@prisma/generator-helper";
|
|
@@ -3812,20 +3812,39 @@ function makePrismaColumn(column) {
|
|
|
3812
3812
|
].filter((c) => Boolean(c));
|
|
3813
3813
|
return ` ${column.name} ${column.type}${formatConstraints(constraints)}`;
|
|
3814
3814
|
}
|
|
3815
|
+
/**
|
|
3816
|
+
* The name a model, field, enum or enum value has in the database: its `@map` / `@@map` when it
|
|
3817
|
+
* has one, its Prisma name otherwise. DBML describes the database, so every name it writes is this.
|
|
3818
|
+
*/
|
|
3819
|
+
function dbNameOf(item) {
|
|
3820
|
+
return item.dbName ?? item.name;
|
|
3821
|
+
}
|
|
3822
|
+
/** The column a Prisma field of `model` is stored in. */
|
|
3823
|
+
function columnOf(model, field) {
|
|
3824
|
+
const found = model?.fields.find((f) => f.name === field);
|
|
3825
|
+
return found ? dbNameOf(found) : field;
|
|
3826
|
+
}
|
|
3815
3827
|
function isFunctionDefault$4(def) {
|
|
3816
3828
|
return def !== null && typeof def === "object" && "name" in def;
|
|
3817
3829
|
}
|
|
3818
|
-
function toDBMLColumn(field,
|
|
3830
|
+
function toDBMLColumn(field, enums) {
|
|
3819
3831
|
const defaultName = isFunctionDefault$4(field.default) ? field.default.name : void 0;
|
|
3820
|
-
const
|
|
3832
|
+
const enumDef = field.kind === "enum" ? enums.find((e) => e.name === field.type) : void 0;
|
|
3833
|
+
const baseType = enumDef ? dbNameOf(enumDef) : field.type;
|
|
3821
3834
|
const type = field.isList && !field.relationName ? `${baseType}[]` : baseType;
|
|
3822
3835
|
const defaultValue = (() => {
|
|
3823
3836
|
if (defaultName === "autoincrement") return void 0;
|
|
3824
3837
|
if (defaultName === "now") return "`now()`";
|
|
3825
|
-
if (field.hasDefaultValue && typeof field.default !== "object")
|
|
3838
|
+
if (field.hasDefaultValue && typeof field.default !== "object") {
|
|
3839
|
+
if (field.kind === "enum") {
|
|
3840
|
+
const value = enumDef?.values.find((v) => v.name === field.default);
|
|
3841
|
+
return `'${value ? dbNameOf(value) : String(field.default)}'`;
|
|
3842
|
+
}
|
|
3843
|
+
return field.type === "String" || field.type === "Json" ? `'${field.default}'` : String(field.default);
|
|
3844
|
+
}
|
|
3826
3845
|
})();
|
|
3827
3846
|
return {
|
|
3828
|
-
name: field
|
|
3847
|
+
name: dbNameOf(field),
|
|
3829
3848
|
type,
|
|
3830
3849
|
isPrimaryKey: field.isId,
|
|
3831
3850
|
isIncrement: defaultName === "autoincrement",
|
|
@@ -3835,15 +3854,16 @@ function toDBMLColumn(field, models, mapToDbSchema) {
|
|
|
3835
3854
|
note: stripAnnotations(field.documentation)
|
|
3836
3855
|
};
|
|
3837
3856
|
}
|
|
3838
|
-
function makeTables(models,
|
|
3857
|
+
function makeTables(models, enums = []) {
|
|
3839
3858
|
return models.map((model) => {
|
|
3840
|
-
const modelName =
|
|
3841
|
-
const columnLines = model.fields.filter((field) => !field.relationName).map((field) => toDBMLColumn(field,
|
|
3859
|
+
const modelName = dbNameOf(model);
|
|
3860
|
+
const columnLines = model.fields.filter((field) => !field.relationName).map((field) => toDBMLColumn(field, enums)).map(makePrismaColumn).join("\n");
|
|
3861
|
+
const columnsOf = (fields) => fields.map((f) => columnOf(model, f));
|
|
3842
3862
|
const indexes = [...model.primaryKey?.fields && model.primaryKey.fields.length > 0 ? [{
|
|
3843
|
-
columns: model.primaryKey.fields,
|
|
3863
|
+
columns: columnsOf(model.primaryKey.fields),
|
|
3844
3864
|
isPrimaryKey: true
|
|
3845
3865
|
}] : [], ...model.uniqueFields.filter((c) => c.length > 1).map((c) => ({
|
|
3846
|
-
columns: c,
|
|
3866
|
+
columns: columnsOf(c),
|
|
3847
3867
|
isUnique: true
|
|
3848
3868
|
}))];
|
|
3849
3869
|
const indexBlock = indexes.length > 0 ? `\n\n indexes {\n${indexes.map(makeIndex).join("\n")}\n }` : "";
|
|
@@ -3853,19 +3873,19 @@ function makeTables(models, mapToDbSchema = false) {
|
|
|
3853
3873
|
}
|
|
3854
3874
|
function makeEnums(enums) {
|
|
3855
3875
|
return enums.map((e) => makeEnum({
|
|
3856
|
-
name: e
|
|
3857
|
-
values: e.values.map(
|
|
3876
|
+
name: dbNameOf(e),
|
|
3877
|
+
values: e.values.map(dbNameOf)
|
|
3858
3878
|
}));
|
|
3859
3879
|
}
|
|
3860
|
-
function makeRelations(models
|
|
3880
|
+
function makeRelations(models) {
|
|
3861
3881
|
return models.flatMap((model) => model.fields.filter((field) => field.relationName && field.relationToFields?.length && field.relationFromFields?.length).map((field) => {
|
|
3862
3882
|
const relationTo = field.type;
|
|
3863
3883
|
const toModel = models.find((m) => m.name === relationTo);
|
|
3864
3884
|
const operator = (toModel?.fields.find((f) => f.kind === "object" && f.relationName === field.relationName && !(toModel.name === model.name && f.name === field.name)))?.isList ? ">" : "-";
|
|
3865
|
-
const relationFromName =
|
|
3866
|
-
const relationToName =
|
|
3867
|
-
const fromColumn = combineKeys(field.relationFromFields ?? []);
|
|
3868
|
-
const toColumn = combineKeys(field.relationToFields ?? []);
|
|
3885
|
+
const relationFromName = dbNameOf(model);
|
|
3886
|
+
const relationToName = toModel ? dbNameOf(toModel) : relationTo;
|
|
3887
|
+
const fromColumn = combineKeys((field.relationFromFields ?? []).map((f) => columnOf(model, f)));
|
|
3888
|
+
const toColumn = combineKeys((field.relationToFields ?? []).map((f) => columnOf(toModel, f)));
|
|
3869
3889
|
return makeRef({
|
|
3870
3890
|
name: `${relationFromName}_${fromColumn}_fk`,
|
|
3871
3891
|
fromTable: relationFromName,
|
|
@@ -3889,19 +3909,27 @@ function dbmlOperator(leftMany, rightMany) {
|
|
|
3889
3909
|
}
|
|
3890
3910
|
function annotatedDbmlRefs(models) {
|
|
3891
3911
|
const inferredKeys = new Set(inferredERRelations(models).map(erKey));
|
|
3912
|
+
const end = (side) => {
|
|
3913
|
+
const model = models.find((m) => m.name === side.model);
|
|
3914
|
+
return {
|
|
3915
|
+
table: model ? dbNameOf(model) : side.model,
|
|
3916
|
+
column: columnOf(model, side.field)
|
|
3917
|
+
};
|
|
3918
|
+
};
|
|
3892
3919
|
return annotatedERRelations(models).filter((relation) => !inferredKeys.has(erKey(relation))).map((relation) => {
|
|
3893
|
-
const
|
|
3894
|
-
const
|
|
3920
|
+
const to = end(relation.to);
|
|
3921
|
+
const from = end(relation.from);
|
|
3895
3922
|
const operator = dbmlOperator(isMany(relation.to.cardinality), isMany(relation.from.cardinality));
|
|
3896
|
-
return `Ref ${`${
|
|
3923
|
+
return `Ref ${`${to.table}_${to.column}_${from.table}_${from.column}`}: ${to.table}.${to.column} ${operator} ${from.table}.${from.column}`;
|
|
3897
3924
|
});
|
|
3898
3925
|
}
|
|
3899
3926
|
//#endregion
|
|
3900
3927
|
//#region src/generator/dbml.ts
|
|
3901
|
-
|
|
3902
|
-
|
|
3928
|
+
/** The schema as DBML, named as the database names it: every `@map` / `@@map` applied. */
|
|
3929
|
+
function dbmlContent(datamodel) {
|
|
3930
|
+
const tables = makeTables(datamodel.models, datamodel.enums);
|
|
3903
3931
|
const enums = makeEnums(datamodel.enums);
|
|
3904
|
-
const refs = makeRelations(datamodel.models
|
|
3932
|
+
const refs = makeRelations(datamodel.models);
|
|
3905
3933
|
const logicalRefs = annotatedDbmlRefs(datamodel.models);
|
|
3906
3934
|
return [
|
|
3907
3935
|
...enums,
|
|
@@ -4011,8 +4039,8 @@ const FORMATS = {
|
|
|
4011
4039
|
render: (options) => erContent(options.dmmf.datamodel.models).join("\n")
|
|
4012
4040
|
},
|
|
4013
4041
|
".dbml": {
|
|
4014
|
-
options: [
|
|
4015
|
-
render: (options) => dbmlContent(options.dmmf.datamodel
|
|
4042
|
+
options: [],
|
|
4043
|
+
render: (options) => dbmlContent(options.dmmf.datamodel)
|
|
4016
4044
|
},
|
|
4017
4045
|
".png": {
|
|
4018
4046
|
options: ["theme"],
|
|
@@ -4037,28 +4065,33 @@ function endsIn(name) {
|
|
|
4037
4065
|
return extension === "" ? "nothing" : `"${extension}"`;
|
|
4038
4066
|
}
|
|
4039
4067
|
/**
|
|
4040
|
-
* The files to write
|
|
4068
|
+
* The files to write: the one `output` names, or every one `outputs` lists.
|
|
4041
4069
|
*
|
|
4042
|
-
*
|
|
4043
|
-
*
|
|
4070
|
+
* Neither ever names a directory — each entry is a whole path, extension included — so the file
|
|
4071
|
+
* the caller writes is the file they get. A schema that wants the ER model in several formats
|
|
4072
|
+
* declares `Hekireki-ER` once and lists them in `outputs`, each resolved the way Prisma resolves
|
|
4073
|
+
* `output`: against the directory of the schema file that declares the block.
|
|
4044
4074
|
*/
|
|
4045
|
-
function filesOf(
|
|
4075
|
+
function filesOf(options, outputs) {
|
|
4046
4076
|
return Effect.gen(function* () {
|
|
4047
|
-
|
|
4077
|
+
const output = options.generator.isCustomOutput ? options.generator.output?.value : void 0;
|
|
4078
|
+
if (output && outputs !== void 0) return yield* new GeneratorConfigError({ message: "Hekireki-ER takes output or outputs, not both: output names one file, outputs lists several." });
|
|
4079
|
+
if (output) {
|
|
4048
4080
|
const extension = path.extname(output).toLowerCase();
|
|
4049
|
-
if (!isFormat(extension)) return yield* new GeneratorConfigError({ message: `output for Hekireki-ER has to name a file ending in ${EXTENSIONS}; "${output}" ends in ${endsIn(output)}.` });
|
|
4081
|
+
if (!isFormat(extension)) return yield* new GeneratorConfigError({ message: `output for Hekireki-ER has to name a file ending in ${EXTENSIONS}, not a directory; "${output}" ends in ${endsIn(output)}.` });
|
|
4050
4082
|
return [{
|
|
4051
4083
|
file: output,
|
|
4052
4084
|
extension
|
|
4053
4085
|
}];
|
|
4054
4086
|
}
|
|
4087
|
+
if (outputs === void 0) return yield* new GeneratorConfigError({ message: `output or outputs is required for Hekireki-ER: output names one file, outputs lists several, each ending in ${EXTENSIONS}.` });
|
|
4055
4088
|
if (outputs.length === 0) return yield* new GeneratorConfigError({ message: "outputs for Hekireki-ER has to name at least one file." });
|
|
4056
|
-
|
|
4089
|
+
const base = path.dirname(options.generator.sourceFilePath);
|
|
4057
4090
|
return yield* Effect.forEach(outputs, (name) => {
|
|
4058
4091
|
const extension = path.extname(name).toLowerCase();
|
|
4059
4092
|
if (!isFormat(extension)) return new GeneratorConfigError({ message: `outputs for Hekireki-ER has to name files ending in ${EXTENSIONS}; "${name}" ends in ${endsIn(name)}.` });
|
|
4060
4093
|
return Effect.succeed({
|
|
4061
|
-
file: path.resolve(
|
|
4094
|
+
file: path.resolve(base, name),
|
|
4062
4095
|
extension
|
|
4063
4096
|
});
|
|
4064
4097
|
});
|
|
@@ -4066,10 +4099,9 @@ function filesOf(output, outputs) {
|
|
|
4066
4099
|
}
|
|
4067
4100
|
function er(options) {
|
|
4068
4101
|
return Effect.gen(function* () {
|
|
4069
|
-
if (!(options.generator.isCustomOutput && options.generator.output?.value)) return yield* new GeneratorConfigError({ message: "output is required for Hekireki-ER. Please specify output in your generator config." });
|
|
4070
4102
|
const config = options.generator.config ?? {};
|
|
4071
4103
|
const outputs = getStrings(config.outputs);
|
|
4072
|
-
const files = yield* filesOf(options
|
|
4104
|
+
const files = yield* filesOf(options, outputs);
|
|
4073
4105
|
const read = new Set(outputs === void 0 ? [] : ["outputs"]);
|
|
4074
4106
|
for (const { extension } of files) for (const option of FORMATS[extension].options) read.add(option);
|
|
4075
4107
|
const unread = Object.keys(config).filter((name) => !read.has(name));
|
|
@@ -819,5 +819,140 @@ function makeSchema(input) {
|
|
|
819
819
|
relations: makeRelations({ models })
|
|
820
820
|
};
|
|
821
821
|
}
|
|
822
|
+
const AnalysisDialect = z.enum([
|
|
823
|
+
"postgresql",
|
|
824
|
+
"mysql",
|
|
825
|
+
"sqlite"
|
|
826
|
+
]).meta({
|
|
827
|
+
description: "The SQL dialect the analysis reads types for",
|
|
828
|
+
example: "postgresql"
|
|
829
|
+
});
|
|
830
|
+
z.object({
|
|
831
|
+
dialect: AnalysisDialect.nullable().meta({
|
|
832
|
+
description: "The dialect of the connected database, or null while none is connected.",
|
|
833
|
+
example: "sqlite"
|
|
834
|
+
}),
|
|
835
|
+
provider: z.string().nullable().meta({
|
|
836
|
+
description: "The datasource provider of the schema, the fallback for the dialect.",
|
|
837
|
+
example: "postgresql"
|
|
838
|
+
}),
|
|
839
|
+
models: z.array(z.object({
|
|
840
|
+
name: z.string().meta({
|
|
841
|
+
description: "The model name as declared.",
|
|
842
|
+
example: "User"
|
|
843
|
+
}),
|
|
844
|
+
dbName: z.string().nullable().meta({
|
|
845
|
+
description: "The @@map table name, when set.",
|
|
846
|
+
example: "users"
|
|
847
|
+
}),
|
|
848
|
+
fields: z.array(z.object({
|
|
849
|
+
name: z.string().meta({
|
|
850
|
+
description: "The Prisma field name.",
|
|
851
|
+
example: "createdAt"
|
|
852
|
+
}),
|
|
853
|
+
dbName: z.string().nullable().meta({
|
|
854
|
+
description: "The @map column name, when set.",
|
|
855
|
+
example: "created_at"
|
|
856
|
+
}),
|
|
857
|
+
kind: z.string().meta({
|
|
858
|
+
description: "scalar, object, enum or unsupported.",
|
|
859
|
+
example: "scalar"
|
|
860
|
+
}),
|
|
861
|
+
type: z.string().meta({
|
|
862
|
+
description: "The declared type.",
|
|
863
|
+
example: "DateTime"
|
|
864
|
+
}),
|
|
865
|
+
isList: z.boolean().meta({
|
|
866
|
+
description: "Whether the field is a list.",
|
|
867
|
+
example: false
|
|
868
|
+
}),
|
|
869
|
+
isRequired: z.boolean().meta({
|
|
870
|
+
description: "Whether the field is required.",
|
|
871
|
+
example: true
|
|
872
|
+
}),
|
|
873
|
+
nativeType: z.string().nullable().meta({
|
|
874
|
+
description: "The `@db.*` attribute as written, when declared.",
|
|
875
|
+
example: "@db.VarChar(255)"
|
|
876
|
+
})
|
|
877
|
+
}).readonly()).readonly().meta({ description: "The fields of the model, relations included." })
|
|
878
|
+
}).readonly()).readonly().meta({ description: "The models of the schema." })
|
|
879
|
+
}).readonly().meta({
|
|
880
|
+
description: "The Prisma models the SQL analysis reads its tables from",
|
|
881
|
+
example: {
|
|
882
|
+
dialect: "sqlite",
|
|
883
|
+
provider: "sqlite",
|
|
884
|
+
models: []
|
|
885
|
+
}
|
|
886
|
+
});
|
|
887
|
+
/** The column type Prisma migrates a scalar to when no `@db.*` attribute says otherwise. */
|
|
888
|
+
const PRISMA_COLUMN_TYPES = {
|
|
889
|
+
postgresql: {
|
|
890
|
+
Int: "int4",
|
|
891
|
+
BigInt: "int8",
|
|
892
|
+
Float: "float8",
|
|
893
|
+
Decimal: "numeric",
|
|
894
|
+
String: "text",
|
|
895
|
+
Boolean: "bool",
|
|
896
|
+
DateTime: "timestamp",
|
|
897
|
+
Json: "jsonb",
|
|
898
|
+
Bytes: "bytea"
|
|
899
|
+
},
|
|
900
|
+
mysql: {
|
|
901
|
+
Int: "int",
|
|
902
|
+
BigInt: "bigint",
|
|
903
|
+
Float: "double",
|
|
904
|
+
Decimal: "decimal",
|
|
905
|
+
String: "varchar",
|
|
906
|
+
Boolean: "tinyint",
|
|
907
|
+
DateTime: "datetime",
|
|
908
|
+
Json: "json",
|
|
909
|
+
Bytes: "longblob"
|
|
910
|
+
},
|
|
911
|
+
sqlite: {
|
|
912
|
+
Int: "INTEGER",
|
|
913
|
+
BigInt: "BIGINT",
|
|
914
|
+
Float: "REAL",
|
|
915
|
+
Decimal: "DECIMAL",
|
|
916
|
+
String: "TEXT",
|
|
917
|
+
Boolean: "BOOLEAN",
|
|
918
|
+
DateTime: "DATETIME",
|
|
919
|
+
Json: "JSONB",
|
|
920
|
+
Bytes: "BLOB"
|
|
921
|
+
}
|
|
922
|
+
};
|
|
923
|
+
/** An enum column is stored as text everywhere; MySQL has its own `enum` type for it. */
|
|
924
|
+
const ENUM_COLUMN_TYPES = {
|
|
925
|
+
postgresql: "text",
|
|
926
|
+
mysql: "enum",
|
|
927
|
+
sqlite: "TEXT"
|
|
928
|
+
};
|
|
929
|
+
function analysisDialect(input) {
|
|
930
|
+
if (input.dialect !== null) return input.dialect;
|
|
931
|
+
const provider = AnalysisDialect.safeParse(input.provider);
|
|
932
|
+
return provider.success ? provider.data : "sqlite";
|
|
933
|
+
}
|
|
934
|
+
function columnDataType(dialect, field) {
|
|
935
|
+
const base = (field.nativeType === null ? null : field.nativeType.replace(/^@db\./u, "")) ?? (field.kind === "enum" ? ENUM_COLUMN_TYPES[dialect] : PRISMA_COLUMN_TYPES[dialect][field.type] ?? "unknown");
|
|
936
|
+
return field.isList && dialect === "postgresql" ? `${base}[]` : base;
|
|
937
|
+
}
|
|
938
|
+
/**
|
|
939
|
+
* The tables the SQL analysis runs against, read off the Prisma models: `@@map` / `@map` give
|
|
940
|
+
* the names the database knows, relation fields are left out (they are not columns), and a
|
|
941
|
+
* scalar's column type is its `@db.*` attribute or the type Prisma migrates it to.
|
|
942
|
+
*/
|
|
943
|
+
function makeAnalysisSchema(input) {
|
|
944
|
+
const dialect = analysisDialect(input);
|
|
945
|
+
return {
|
|
946
|
+
dialect,
|
|
947
|
+
tables: input.models.map((model) => ({
|
|
948
|
+
name: model.dbName ?? model.name,
|
|
949
|
+
columns: model.fields.filter((field) => field.kind !== "object").map((field) => ({
|
|
950
|
+
name: field.dbName ?? field.name,
|
|
951
|
+
dataType: columnDataType(dialect, field),
|
|
952
|
+
nullable: !field.isRequired
|
|
953
|
+
}))
|
|
954
|
+
}))
|
|
955
|
+
};
|
|
956
|
+
}
|
|
822
957
|
//#endregion
|
|
823
|
-
export {
|
|
958
|
+
export { makeValidationExtractor as _, erRelations as a, stripAnnotations as b, getBool as c, groupByModel as d, isFields as f, makeSnakeCase as g, makePascalCase as h, erKey as i, getString as l, makeCommentBlock as m, makeSchema as n, inferredERRelations as o, isLoopbackHostname as p, annotatedERRelations as r, extractObjectType as s, makeAnalysisSchema as t, getStrings as u, parseDocumentWithoutAnnotations as v, schemaFromFields as y };
|