hekireki 0.10.2 → 0.20.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 +179 -1189
- 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 +6 -0
- package/dist/bin/gorm.js +1 -1
- package/dist/bin/hekireki.js +176 -0
- 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-9aHkP_Bs.js → bin-CPb5ZV8W.js} +1436 -1751
- package/dist/file-yN0By3gn.js +48 -0
- package/dist/schema-DwMNeEei.js +823 -0
- package/dist/start-Blp4k61D.js +5764 -0
- package/dist/studio/assets/cells-lAj-v64A.js +2 -0
- package/dist/studio/assets/codicon-Brq4_Ui5.ttf +0 -0
- package/dist/studio/assets/docs-CpeTt3O2.js +34 -0
- package/dist/studio/assets/editor.worker-D9zwrD0f.js +30 -0
- package/dist/studio/assets/enums._name-My5PypWw.js +1 -0
- package/dist/studio/assets/hooks-CiUANSpJ.js +1 -0
- package/dist/studio/assets/index-C4wWEcwe.js +188 -0
- package/dist/studio/assets/index-ZSt1ppSG.css +2 -0
- package/dist/studio/assets/labels-DBXIkmbm.js +1 -0
- package/dist/studio/assets/lazyRouteComponent-DmCXXiGp.js +1 -0
- package/dist/studio/assets/models._name-oUX64rwK.js +1 -0
- package/dist/studio/assets/not-found-DxAsuyR5.js +1 -0
- package/dist/studio/assets/prisma-Bk53qnxm.js +891 -0
- package/dist/studio/assets/prisma-C2rAM-pV.css +1 -0
- package/dist/studio/assets/routes-ZmrohHNP.js +1 -0
- package/dist/studio/assets/schema-gate-DJ_-6NYR.js +1 -0
- package/dist/studio/assets/schema-view-BaoCWAL8.js +8 -0
- package/dist/studio/assets/sql-DpzkSc6F.js +1 -0
- package/dist/studio/assets/useMatch-D73HO_WB.js +1 -0
- package/dist/studio/index.html +16 -0
- package/package.json +53 -25
- package/dist/bin/dbml.js +0 -6
- package/dist/bin/docs.js +0 -6
- package/dist/bin/mermaid-er.d.ts +0 -1
- package/dist/bin/mermaid-er.js +0 -6
- package/dist/cli/index.d.ts +0 -138
- package/dist/cli/index.js +0 -134
- /package/dist/bin/{dbml.d.ts → er.d.ts} +0 -0
- /package/dist/bin/{docs.d.ts → hekireki.d.ts} +0 -0
|
@@ -1,233 +1,49 @@
|
|
|
1
|
-
import "
|
|
1
|
+
import { c as writeFile, i as makeDirectory, n as fileSystemLayer } from "./file-yN0By3gn.js";
|
|
2
|
+
import { _ as parseDocumentWithoutAnnotations, a as inferredERRelations, c as getString, d as isFields, g as makeValidationExtractor, h as makeSnakeCase, i as erRelations, l as getStrings, m as makePascalCase, n as annotatedERRelations, o as extractObjectType, p as makeCommentBlock, r as erKey, s as getBool, t as makeSchema, u as groupByModel, v as schemaFromFields, y as stripAnnotations } from "./schema-DwMNeEei.js";
|
|
3
|
+
import { Data, Effect } from "effect";
|
|
2
4
|
import path from "node:path";
|
|
3
5
|
import pkg from "@prisma/generator-helper";
|
|
4
6
|
import { format } from "oxfmt";
|
|
5
|
-
import
|
|
7
|
+
import { graphlib, layout } from "@dagrejs/dagre";
|
|
8
|
+
import * as v from "valibot";
|
|
6
9
|
import { Resvg } from "@resvg/resvg-js";
|
|
7
|
-
import { run } from "@softwaretechnik/dbml-renderer";
|
|
8
|
-
import { Style, css } from "hono/css";
|
|
9
|
-
import { raw } from "hono/html";
|
|
10
|
-
import { HtmlEscapedCallbackPhase, resolveCallback } from "hono/utils/html";
|
|
11
|
-
import { Fragment, jsx, jsxs } from "hono/jsx/jsx-runtime";
|
|
12
10
|
//#region src/format/index.ts
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
var FormatError = class extends Data.TaggedError("FormatError") {};
|
|
12
|
+
/** Formats TypeScript source with oxfmt using the repository style. */
|
|
13
|
+
function fmt(input) {
|
|
14
|
+
return Effect.gen(function* () {
|
|
15
|
+
const { code, errors } = yield* Effect.promise(() => format("<stdin>.ts", input, {
|
|
16
|
+
printWidth: 100,
|
|
17
|
+
singleQuote: true,
|
|
18
|
+
semi: false
|
|
19
|
+
}));
|
|
20
|
+
if (errors.length > 0) return yield* new FormatError({ message: errors.map((error) => error.message).join("\n") });
|
|
21
|
+
return code;
|
|
18
22
|
});
|
|
19
|
-
if (errors.length > 0) return {
|
|
20
|
-
ok: false,
|
|
21
|
-
error: errors.map((e) => e.message).join("\n")
|
|
22
|
-
};
|
|
23
|
-
return {
|
|
24
|
-
ok: true,
|
|
25
|
-
value: code
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
//#endregion
|
|
29
|
-
//#region src/fsp/index.ts
|
|
30
|
-
async function mkdir(dir) {
|
|
31
|
-
try {
|
|
32
|
-
await fsp.mkdir(dir, { recursive: true });
|
|
33
|
-
return {
|
|
34
|
-
ok: true,
|
|
35
|
-
value: void 0
|
|
36
|
-
};
|
|
37
|
-
} catch (e) {
|
|
38
|
-
return {
|
|
39
|
-
ok: false,
|
|
40
|
-
error: e instanceof Error ? e.message : String(e)
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
async function writeFile(path, data) {
|
|
45
|
-
try {
|
|
46
|
-
await fsp.writeFile(path, data, "utf-8");
|
|
47
|
-
return {
|
|
48
|
-
ok: true,
|
|
49
|
-
value: void 0
|
|
50
|
-
};
|
|
51
|
-
} catch (e) {
|
|
52
|
-
return {
|
|
53
|
-
ok: false,
|
|
54
|
-
error: e instanceof Error ? e.message : String(e)
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
async function writeFileBinary(path, data) {
|
|
59
|
-
try {
|
|
60
|
-
await fsp.writeFile(path, data);
|
|
61
|
-
return {
|
|
62
|
-
ok: true,
|
|
63
|
-
value: void 0
|
|
64
|
-
};
|
|
65
|
-
} catch (e) {
|
|
66
|
-
return {
|
|
67
|
-
ok: false,
|
|
68
|
-
error: e instanceof Error ? e.message : String(e)
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
23
|
}
|
|
72
24
|
//#endregion
|
|
73
25
|
//#region src/emit/index.ts
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
ok: false,
|
|
82
|
-
error: mkdirResult.error
|
|
83
|
-
};
|
|
84
|
-
const writeResult = await writeFile(output, fmtResult.value);
|
|
85
|
-
if (!writeResult.ok) return {
|
|
86
|
-
ok: false,
|
|
87
|
-
error: writeResult.error
|
|
88
|
-
};
|
|
89
|
-
return {
|
|
90
|
-
ok: true,
|
|
91
|
-
value: void 0
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
async function emitRaw(data, dir, output) {
|
|
95
|
-
const mkdirResult = await mkdir(dir);
|
|
96
|
-
if (!mkdirResult.ok) return {
|
|
97
|
-
ok: false,
|
|
98
|
-
error: mkdirResult.error
|
|
99
|
-
};
|
|
100
|
-
const writeResult = typeof data === "string" ? await writeFile(output, data) : await writeFileBinary(output, data);
|
|
101
|
-
if (!writeResult.ok) return {
|
|
102
|
-
ok: false,
|
|
103
|
-
error: writeResult.error
|
|
104
|
-
};
|
|
105
|
-
return {
|
|
106
|
-
ok: true,
|
|
107
|
-
value: void 0
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
async function emitMany(files, dir) {
|
|
111
|
-
const mkdirResult = await mkdir(dir);
|
|
112
|
-
if (!mkdirResult.ok) return {
|
|
113
|
-
ok: false,
|
|
114
|
-
error: mkdirResult.error
|
|
115
|
-
};
|
|
116
|
-
const results = await Promise.all(files.map((f) => writeFile(path.join(dir, f.fileName), f.code)));
|
|
117
|
-
for (const r of results) if (!r.ok) return {
|
|
118
|
-
ok: false,
|
|
119
|
-
error: r.error
|
|
120
|
-
};
|
|
121
|
-
return {
|
|
122
|
-
ok: true,
|
|
123
|
-
value: void 0
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
//#endregion
|
|
127
|
-
//#region src/utils/index.ts
|
|
128
|
-
function parseRelation(line) {
|
|
129
|
-
const match = line.trim().match(/^@relation\s+(\w+)\.(\w+)\s+(\w+)\.(\w+)\s+(\w+-to-\w+)$/u);
|
|
130
|
-
if (!match) return null;
|
|
131
|
-
const [, fromModel, fromField, toModel, toField, type] = match;
|
|
132
|
-
return {
|
|
133
|
-
fromModel,
|
|
134
|
-
fromField,
|
|
135
|
-
toModel,
|
|
136
|
-
toField,
|
|
137
|
-
type
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
function getString(v) {
|
|
141
|
-
return typeof v === "string" ? v : Array.isArray(v) ? v[0] : void 0;
|
|
142
|
-
}
|
|
143
|
-
function getBool(v, fallback = false) {
|
|
144
|
-
return v === true || v === "true" || Array.isArray(v) && v[0] === "true" ? true : fallback;
|
|
145
|
-
}
|
|
146
|
-
function makeSnakeCase(name) {
|
|
147
|
-
return name.replaceAll(/([a-z0-9])([A-Z])/gu, "$1_$2").toLowerCase();
|
|
148
|
-
}
|
|
149
|
-
function makePascalCase(name) {
|
|
150
|
-
return name.split("_").filter((part) => part !== "").map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
|
|
26
|
+
/** Formats TypeScript source and writes it, creating the directory. */
|
|
27
|
+
function emit(code, dir, output) {
|
|
28
|
+
return Effect.gen(function* () {
|
|
29
|
+
const formatted = yield* fmt(code);
|
|
30
|
+
yield* makeDirectory(dir);
|
|
31
|
+
yield* writeFile(output, formatted);
|
|
32
|
+
});
|
|
151
33
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
};
|
|
34
|
+
/** Writes text or bytes as they are, creating the directory. */
|
|
35
|
+
function emitRaw(data, dir, output) {
|
|
36
|
+
return Effect.gen(function* () {
|
|
37
|
+
yield* makeDirectory(dir);
|
|
38
|
+
yield* writeFile(output, data);
|
|
39
|
+
});
|
|
159
40
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
"@e.",
|
|
167
|
-
"@t.",
|
|
168
|
-
"@j.",
|
|
169
|
-
"@p."
|
|
170
|
-
];
|
|
171
|
-
const annotationExact = /* @__PURE__ */ new Set([
|
|
172
|
-
"@z",
|
|
173
|
-
"@v",
|
|
174
|
-
"@a",
|
|
175
|
-
"@e",
|
|
176
|
-
"@t",
|
|
177
|
-
"@j",
|
|
178
|
-
"@p"
|
|
179
|
-
]);
|
|
180
|
-
return documentation.split("\n").map((line) => line.trim()).filter((line) => line.length > 0 && !annotationPrefixes.some((p) => line.startsWith(p)) && !annotationExact.has(line));
|
|
181
|
-
}
|
|
182
|
-
function stripAnnotations(doc) {
|
|
183
|
-
if (!doc) return void 0;
|
|
184
|
-
const annotationPrefixes = [
|
|
185
|
-
"@z.",
|
|
186
|
-
"@v.",
|
|
187
|
-
"@a.",
|
|
188
|
-
"@e.",
|
|
189
|
-
"@t.",
|
|
190
|
-
"@j.",
|
|
191
|
-
"@p."
|
|
192
|
-
];
|
|
193
|
-
const annotationExact = /* @__PURE__ */ new Set([
|
|
194
|
-
"@z",
|
|
195
|
-
"@v",
|
|
196
|
-
"@a",
|
|
197
|
-
"@e",
|
|
198
|
-
"@t",
|
|
199
|
-
"@j",
|
|
200
|
-
"@p"
|
|
201
|
-
]);
|
|
202
|
-
const result = doc.split("\n").filter((line) => {
|
|
203
|
-
const t = line.trim();
|
|
204
|
-
return !annotationPrefixes.some((p) => t.startsWith(p)) && !t.startsWith("@relation") && !annotationExact.has(t);
|
|
205
|
-
}).join("\n").trim();
|
|
206
|
-
return result.length > 0 ? result : void 0;
|
|
207
|
-
}
|
|
208
|
-
function extractObjectType(documentation, prefix) {
|
|
209
|
-
if (!documentation) return void 0;
|
|
210
|
-
const prefixWithoutAt = prefix.slice(1);
|
|
211
|
-
const match = documentation.split("\n").map((l) => l.trim()).find((line) => line.includes(`${prefixWithoutAt}strictObject`) || line.includes(`${prefixWithoutAt}looseObject`));
|
|
212
|
-
if (!match) return void 0;
|
|
213
|
-
if (match.includes("strictObject")) return "strict";
|
|
214
|
-
return "loose";
|
|
215
|
-
}
|
|
216
|
-
function makeCommentBlock(lines, indent) {
|
|
217
|
-
if (lines.length === 0) return "";
|
|
218
|
-
const prefix = " ".repeat(indent);
|
|
219
|
-
return `${prefix}/**\n${lines.map((c) => `${prefix} * ${c}`).join("\n")}\n${prefix} */\n`;
|
|
220
|
-
}
|
|
221
|
-
function groupByModel(validFields) {
|
|
222
|
-
const raw = Object.groupBy(validFields, (f) => f.modelName);
|
|
223
|
-
return Object.fromEntries(Object.entries(raw).filter((entry) => entry[1] !== void 0));
|
|
224
|
-
}
|
|
225
|
-
function isFields(modelFields) {
|
|
226
|
-
return modelFields.flat().filter((field) => field.validation !== null);
|
|
227
|
-
}
|
|
228
|
-
function schemaFromFields(modelFields, comment, schemaBuilder, propertiesGenerator, objectType) {
|
|
229
|
-
const modelName = modelFields[0].modelName;
|
|
230
|
-
return schemaBuilder(modelName, propertiesGenerator(modelFields, comment), objectType);
|
|
41
|
+
/** Writes several files into one directory. */
|
|
42
|
+
function emitMany(files, dir) {
|
|
43
|
+
return Effect.gen(function* () {
|
|
44
|
+
yield* makeDirectory(dir);
|
|
45
|
+
yield* Effect.forEach(files, (f) => writeFile(path.join(dir, f.fileName), f.code), { discard: true });
|
|
46
|
+
});
|
|
231
47
|
}
|
|
232
48
|
//#endregion
|
|
233
49
|
//#region src/helper/activerecord.ts
|
|
@@ -372,15 +188,18 @@ function activeRecordModelFiles(models, enums) {
|
|
|
372
188
|
})).filter((entry) => entry.code.trim().length > 0);
|
|
373
189
|
}
|
|
374
190
|
//#endregion
|
|
191
|
+
//#region src/core/errors.ts
|
|
192
|
+
/** The generator block in schema.prisma is missing or names something the generator cannot use. */
|
|
193
|
+
var GeneratorConfigError = class extends Data.TaggedError("GeneratorConfigError") {};
|
|
194
|
+
//#endregion
|
|
375
195
|
//#region src/core/activerecord.ts
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
return emitMany(activeRecordModelFiles(options.dmmf.datamodel.models, enums), outDir);
|
|
196
|
+
function activerecord(options) {
|
|
197
|
+
return Effect.gen(function* () {
|
|
198
|
+
if (!(options.generator.isCustomOutput && options.generator.output?.value)) return yield* new GeneratorConfigError({ message: "output is required for Hekireki-ActiveRecord. Please specify output in your generator config." });
|
|
199
|
+
const outDir = options.generator.output.value;
|
|
200
|
+
const enums = options.dmmf.datamodel.enums;
|
|
201
|
+
return yield* emitMany(activeRecordModelFiles(options.dmmf.datamodel.models, enums), outDir);
|
|
202
|
+
});
|
|
384
203
|
}
|
|
385
204
|
//#endregion
|
|
386
205
|
//#region src/helper/validation-schema.ts
|
|
@@ -521,20 +340,19 @@ function ajvCode(dmmf, type, comment, relation) {
|
|
|
521
340
|
}
|
|
522
341
|
//#endregion
|
|
523
342
|
//#region src/core/ajv.ts
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
};
|
|
537
|
-
return emit(ajvCode(options.dmmf, getBool(options.generator.config?.type), getBool(options.generator.config?.comment), getBool(options.generator.config?.relation)), resolved.dir, resolved.file);
|
|
343
|
+
function ajv(options) {
|
|
344
|
+
return Effect.gen(function* () {
|
|
345
|
+
if (!(options.generator.isCustomOutput && options.generator.output?.value)) return yield* new GeneratorConfigError({ message: "output is required for Hekireki-AJV. Please specify output in your generator config." });
|
|
346
|
+
const output = options.generator.output.value;
|
|
347
|
+
const resolved = path.extname(output) ? {
|
|
348
|
+
dir: path.dirname(output),
|
|
349
|
+
file: output
|
|
350
|
+
} : {
|
|
351
|
+
dir: output,
|
|
352
|
+
file: path.join(output, "index.ts")
|
|
353
|
+
};
|
|
354
|
+
return yield* emit(ajvCode(options.dmmf, getBool(options.generator.config?.type), getBool(options.generator.config?.comment), getBool(options.generator.config?.relation)), resolved.dir, resolved.file);
|
|
355
|
+
});
|
|
538
356
|
}
|
|
539
357
|
//#endregion
|
|
540
358
|
//#region src/helper/arktype.ts
|
|
@@ -593,20 +411,19 @@ function arktypeCode(dmmf, type, comment, relation) {
|
|
|
593
411
|
}
|
|
594
412
|
//#endregion
|
|
595
413
|
//#region src/core/arktype.ts
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
};
|
|
609
|
-
return emit(arktypeCode(options.dmmf, getBool(options.generator.config?.type), getBool(options.generator.config?.comment), getBool(options.generator.config?.relation)), resolved.dir, resolved.file);
|
|
414
|
+
function arktype(options) {
|
|
415
|
+
return Effect.gen(function* () {
|
|
416
|
+
if (!(options.generator.isCustomOutput && options.generator.output?.value)) return yield* new GeneratorConfigError({ message: "output is required for Hekireki-ArkType. Please specify output in your generator config." });
|
|
417
|
+
const output = options.generator.output.value;
|
|
418
|
+
const resolved = path.extname(output) ? {
|
|
419
|
+
dir: path.dirname(output),
|
|
420
|
+
file: output
|
|
421
|
+
} : {
|
|
422
|
+
dir: output,
|
|
423
|
+
file: path.join(output, "index.ts")
|
|
424
|
+
};
|
|
425
|
+
return yield* emit(arktypeCode(options.dmmf, getBool(options.generator.config?.type), getBool(options.generator.config?.comment), getBool(options.generator.config?.relation)), resolved.dir, resolved.file);
|
|
426
|
+
});
|
|
610
427
|
}
|
|
611
428
|
//#endregion
|
|
612
429
|
//#region src/helper/atlas.ts
|
|
@@ -1149,269 +966,23 @@ function atlasSchema(datamodel, provider, config) {
|
|
|
1149
966
|
}
|
|
1150
967
|
//#endregion
|
|
1151
968
|
//#region src/core/atlas.ts
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
const schemaName = getString(options.generator.config?.schemaName);
|
|
1164
|
-
const comment = getBool(options.generator.config?.comment);
|
|
1165
|
-
const content = atlasSchema(options.dmmf.datamodel, provider, {
|
|
1166
|
-
schemaName,
|
|
1167
|
-
comment
|
|
1168
|
-
});
|
|
1169
|
-
const outPath = path.extname(output) ? output : path.join(output, "schema.hcl");
|
|
1170
|
-
return emitRaw(content, path.dirname(outPath), outPath);
|
|
1171
|
-
}
|
|
1172
|
-
//#endregion
|
|
1173
|
-
//#region src/helper/relation.ts
|
|
1174
|
-
function isCardinality(value) {
|
|
1175
|
-
return value === "zero-one" || value === "one" || value === "zero-many" || value === "many";
|
|
1176
|
-
}
|
|
1177
|
-
function erKey(relation) {
|
|
1178
|
-
return `${relation.from.model}.${relation.from.field}->${relation.to.model}.${relation.to.field}`;
|
|
1179
|
-
}
|
|
1180
|
-
function inferredERRelations(models) {
|
|
1181
|
-
return models.flatMap((model) => model.fields.filter((field) => field.kind === "object" && field.relationFromFields && field.relationFromFields.length > 0).map((field) => {
|
|
1182
|
-
const toModel = model.name;
|
|
1183
|
-
const fromModel = field.type;
|
|
1184
|
-
const toField = field.relationFromFields?.[0] ?? "";
|
|
1185
|
-
const fromField = field.relationToFields?.[0] ?? "id";
|
|
1186
|
-
const toCardinality = (models.find((m) => m.name === fromModel)?.fields.find((f) => f.relationName === field.relationName && f.name !== field.name))?.isList ? field.isRequired ? "many" : "zero-many" : field.isRequired ? "one" : "zero-one";
|
|
1187
|
-
return {
|
|
1188
|
-
from: {
|
|
1189
|
-
model: fromModel,
|
|
1190
|
-
field: fromField,
|
|
1191
|
-
cardinality: "one"
|
|
1192
|
-
},
|
|
1193
|
-
to: {
|
|
1194
|
-
model: toModel,
|
|
1195
|
-
field: toField,
|
|
1196
|
-
cardinality: toCardinality
|
|
1197
|
-
},
|
|
1198
|
-
identifying: true,
|
|
1199
|
-
origin: "inferred"
|
|
1200
|
-
};
|
|
1201
|
-
}));
|
|
1202
|
-
}
|
|
1203
|
-
function annotatedERRelations(models) {
|
|
1204
|
-
return models.flatMap((model) => (model.documentation ?? "").split("\n").flatMap((line) => {
|
|
1205
|
-
const relation = parseRelation(line);
|
|
1206
|
-
if (relation === null) return [];
|
|
1207
|
-
const [fromCard, toCard] = relation.type.split("-to-");
|
|
1208
|
-
if (!isCardinality(fromCard) || !isCardinality(toCard)) return [];
|
|
1209
|
-
return [{
|
|
1210
|
-
from: {
|
|
1211
|
-
model: relation.fromModel,
|
|
1212
|
-
field: relation.fromField,
|
|
1213
|
-
cardinality: fromCard
|
|
1214
|
-
},
|
|
1215
|
-
to: {
|
|
1216
|
-
model: relation.toModel,
|
|
1217
|
-
field: relation.toField,
|
|
1218
|
-
cardinality: toCard
|
|
1219
|
-
},
|
|
1220
|
-
identifying: true,
|
|
1221
|
-
origin: "annotated"
|
|
1222
|
-
}];
|
|
1223
|
-
}));
|
|
1224
|
-
}
|
|
1225
|
-
function mergeERRelations(models) {
|
|
1226
|
-
const inferred = inferredERRelations(models);
|
|
1227
|
-
const annotated = annotatedERRelations(models);
|
|
1228
|
-
const inferredKeys = new Set(inferred.map(erKey));
|
|
1229
|
-
return [...new Map([...inferred.map((r) => [erKey(r), r]), ...annotated.map((r) => [erKey(r), inferredKeys.has(erKey(r)) ? {
|
|
1230
|
-
...r,
|
|
1231
|
-
origin: "inferred"
|
|
1232
|
-
} : r])]).values()];
|
|
1233
|
-
}
|
|
1234
|
-
//#endregion
|
|
1235
|
-
//#region src/helper/dbml.ts
|
|
1236
|
-
function escapeNote(str) {
|
|
1237
|
-
return str.replaceAll("'", "\\'");
|
|
1238
|
-
}
|
|
1239
|
-
function formatConstraints(constraints) {
|
|
1240
|
-
return constraints.length > 0 ? ` [${constraints.join(", ")}]` : "";
|
|
1241
|
-
}
|
|
1242
|
-
function makeEnum(enumDef) {
|
|
1243
|
-
return [
|
|
1244
|
-
`Enum ${enumDef.name} {`,
|
|
1245
|
-
...enumDef.values.map((v) => ` ${v}`),
|
|
1246
|
-
"}"
|
|
1247
|
-
].join("\n");
|
|
1248
|
-
}
|
|
1249
|
-
function makeRefName(ref) {
|
|
1250
|
-
return ref.name ?? `${ref.fromTable}_${ref.fromColumn}_${ref.toTable}_${ref.toColumn}_fk`;
|
|
1251
|
-
}
|
|
1252
|
-
function combineKeys(keys) {
|
|
1253
|
-
return keys.length > 1 ? `(${keys.join(", ")})` : keys[0];
|
|
1254
|
-
}
|
|
1255
|
-
function escapeTriple(str) {
|
|
1256
|
-
return str.replaceAll("\\", "\\\\").replaceAll("'", "\\'");
|
|
1257
|
-
}
|
|
1258
|
-
function noteLiteral(value) {
|
|
1259
|
-
return value.includes("\n") ? `'''${escapeTriple(value)}'''` : `'${escapeNote(value)}'`;
|
|
1260
|
-
}
|
|
1261
|
-
function makeIndex(index) {
|
|
1262
|
-
return ` ${index.columns.length > 1 ? `(${index.columns.join(", ")})` : index.columns[0]}${formatConstraints([
|
|
1263
|
-
index.isPrimaryKey && "pk",
|
|
1264
|
-
index.isUnique && "unique",
|
|
1265
|
-
index.name && `name: '${index.name}'`
|
|
1266
|
-
].filter((c) => Boolean(c)))}`;
|
|
1267
|
-
}
|
|
1268
|
-
function toDbmlAction(action) {
|
|
1269
|
-
if (action === "Cascade") return "cascade";
|
|
1270
|
-
if (action === "SetNull") return "set null";
|
|
1271
|
-
if (action === "SetDefault") return "set default";
|
|
1272
|
-
if (action === "NoAction") return "no action";
|
|
1273
|
-
if (action === "Restrict") return "restrict";
|
|
1274
|
-
return action;
|
|
1275
|
-
}
|
|
1276
|
-
function makeRef(ref) {
|
|
1277
|
-
const name = makeRefName(ref);
|
|
1278
|
-
const operator = ref.type ?? ">";
|
|
1279
|
-
const actions = [ref.onDelete && `delete: ${toDbmlAction(ref.onDelete)}`, ref.onUpdate && `update: ${toDbmlAction(ref.onUpdate)}`].filter((a) => Boolean(a));
|
|
1280
|
-
const actionStr = actions.length > 0 ? ` [${actions.join(", ")}]` : "";
|
|
1281
|
-
return `Ref ${name}: ${ref.fromTable}.${ref.fromColumn} ${operator} ${ref.toTable}.${ref.toColumn}${actionStr}`;
|
|
1282
|
-
}
|
|
1283
|
-
function makePrismaColumn(column) {
|
|
1284
|
-
const constraints = [
|
|
1285
|
-
column.isPrimaryKey && "pk",
|
|
1286
|
-
column.isIncrement && "increment",
|
|
1287
|
-
column.defaultValue !== void 0 && `default: ${column.defaultValue}`,
|
|
1288
|
-
column.isUnique && "unique",
|
|
1289
|
-
column.isNotNull && "not null",
|
|
1290
|
-
column.note && `note: ${noteLiteral(column.note)}`
|
|
1291
|
-
].filter((c) => Boolean(c));
|
|
1292
|
-
return ` ${column.name} ${column.type}${formatConstraints(constraints)}`;
|
|
1293
|
-
}
|
|
1294
|
-
function isFunctionDefault$5(def) {
|
|
1295
|
-
return def !== null && typeof def === "object" && "name" in def;
|
|
1296
|
-
}
|
|
1297
|
-
function toDBMLColumn(field, models, mapToDbSchema) {
|
|
1298
|
-
const defaultName = isFunctionDefault$5(field.default) ? field.default.name : void 0;
|
|
1299
|
-
const baseType = mapToDbSchema ? models.find((m) => m.name === field.type)?.dbName ?? field.type : field.type;
|
|
1300
|
-
const type = field.isList && !field.relationName ? `${baseType}[]` : baseType;
|
|
1301
|
-
const defaultValue = (() => {
|
|
1302
|
-
if (defaultName === "autoincrement") return void 0;
|
|
1303
|
-
if (defaultName === "now") return "`now()`";
|
|
1304
|
-
if (field.hasDefaultValue && typeof field.default !== "object") return field.type === "String" || field.type === "Json" || field.kind === "enum" ? `'${field.default}'` : String(field.default);
|
|
1305
|
-
})();
|
|
1306
|
-
return {
|
|
1307
|
-
name: field.name,
|
|
1308
|
-
type,
|
|
1309
|
-
isPrimaryKey: field.isId,
|
|
1310
|
-
isIncrement: defaultName === "autoincrement",
|
|
1311
|
-
isUnique: field.isUnique,
|
|
1312
|
-
isNotNull: field.isRequired && !field.isId,
|
|
1313
|
-
defaultValue,
|
|
1314
|
-
note: stripAnnotations(field.documentation)
|
|
1315
|
-
};
|
|
1316
|
-
}
|
|
1317
|
-
function makeTables(models, mapToDbSchema = false) {
|
|
1318
|
-
return models.map((model) => {
|
|
1319
|
-
const modelName = mapToDbSchema && model.dbName ? model.dbName : model.name;
|
|
1320
|
-
const columnLines = model.fields.filter((field) => !field.relationName).map((field) => toDBMLColumn(field, models, mapToDbSchema)).map(makePrismaColumn).join("\n");
|
|
1321
|
-
const indexes = [...model.primaryKey?.fields && model.primaryKey.fields.length > 0 ? [{
|
|
1322
|
-
columns: model.primaryKey.fields,
|
|
1323
|
-
isPrimaryKey: true
|
|
1324
|
-
}] : [], ...model.uniqueFields.filter((c) => c.length > 1).map((c) => ({
|
|
1325
|
-
columns: c,
|
|
1326
|
-
isUnique: true
|
|
1327
|
-
}))];
|
|
1328
|
-
const indexBlock = indexes.length > 0 ? `\n\n indexes {\n${indexes.map(makeIndex).join("\n")}\n }` : "";
|
|
1329
|
-
const strippedNote = stripAnnotations(model.documentation);
|
|
1330
|
-
return `Table ${modelName} {\n${columnLines}${indexBlock}${strippedNote ? `\n\n Note: ${noteLiteral(strippedNote)}` : ""}\n}`;
|
|
1331
|
-
});
|
|
1332
|
-
}
|
|
1333
|
-
function makeEnums(enums) {
|
|
1334
|
-
return enums.map((e) => makeEnum({
|
|
1335
|
-
name: e.name,
|
|
1336
|
-
values: e.values.map((v) => v.name)
|
|
1337
|
-
}));
|
|
1338
|
-
}
|
|
1339
|
-
function makeRelations$1(models, mapToDbSchema = false) {
|
|
1340
|
-
return models.flatMap((model) => model.fields.filter((field) => field.relationName && field.relationToFields?.length && field.relationFromFields?.length).map((field) => {
|
|
1341
|
-
const relationFrom = model.name;
|
|
1342
|
-
const relationTo = field.type;
|
|
1343
|
-
const operator = (models.find((m) => m.name === relationTo)?.fields.find((f) => f.type === relationFrom))?.isList ? ">" : "-";
|
|
1344
|
-
const relationFromName = mapToDbSchema && model.dbName ? model.dbName : model.name;
|
|
1345
|
-
const relatedModel = models.find((m) => m.name === relationTo);
|
|
1346
|
-
const relationToName = mapToDbSchema && relatedModel?.dbName ? relatedModel.dbName : relationTo;
|
|
1347
|
-
const fromColumn = combineKeys(field.relationFromFields ?? []);
|
|
1348
|
-
const toColumn = combineKeys(field.relationToFields ?? []);
|
|
1349
|
-
return makeRef({
|
|
1350
|
-
name: `${relationFromName}_${fromColumn}_fk`,
|
|
1351
|
-
fromTable: relationFromName,
|
|
1352
|
-
fromColumn,
|
|
1353
|
-
toTable: relationToName,
|
|
1354
|
-
toColumn,
|
|
1355
|
-
type: operator,
|
|
1356
|
-
onDelete: field.relationOnDelete,
|
|
1357
|
-
onUpdate: field.relationOnUpdate
|
|
969
|
+
function atlas(options) {
|
|
970
|
+
return Effect.gen(function* () {
|
|
971
|
+
if (!(options.generator.isCustomOutput && options.generator.output?.value)) return yield* new GeneratorConfigError({ message: "output is required for Hekireki-Atlas. Please specify output in your generator config." });
|
|
972
|
+
const provider = options.datasources[0]?.activeProvider ?? "postgresql";
|
|
973
|
+
if (!(provider === "postgresql" || provider === "cockroachdb" || provider === "mysql" || provider === "sqlite")) return yield* new GeneratorConfigError({ message: `Unsupported provider for Hekireki-Atlas: ${provider}. Supported providers are postgresql, cockroachdb, mysql, and sqlite.` });
|
|
974
|
+
const output = options.generator.output.value;
|
|
975
|
+
const schemaName = getString(options.generator.config?.schemaName);
|
|
976
|
+
const comment = getBool(options.generator.config?.comment);
|
|
977
|
+
const content = atlasSchema(options.dmmf.datamodel, provider, {
|
|
978
|
+
schemaName,
|
|
979
|
+
comment
|
|
1358
980
|
});
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
function isMany(cardinality) {
|
|
1362
|
-
return cardinality === "many" || cardinality === "zero-many";
|
|
1363
|
-
}
|
|
1364
|
-
function dbmlOperator(leftMany, rightMany) {
|
|
1365
|
-
if (leftMany && !rightMany) return ">";
|
|
1366
|
-
if (!leftMany && rightMany) return "<";
|
|
1367
|
-
if (leftMany && rightMany) return "<>";
|
|
1368
|
-
return "-";
|
|
1369
|
-
}
|
|
1370
|
-
function annotatedDbmlRefs(models) {
|
|
1371
|
-
const inferredKeys = new Set(inferredERRelations(models).map(erKey));
|
|
1372
|
-
return annotatedERRelations(models).filter((relation) => !inferredKeys.has(erKey(relation))).map((relation) => {
|
|
1373
|
-
const left = `${relation.to.model}.${relation.to.field}`;
|
|
1374
|
-
const right = `${relation.from.model}.${relation.from.field}`;
|
|
1375
|
-
const operator = dbmlOperator(isMany(relation.to.cardinality), isMany(relation.from.cardinality));
|
|
1376
|
-
return `Ref ${`${relation.to.model}_${relation.to.field}_${relation.from.model}_${relation.from.field}`}: ${left} ${operator} ${right}`;
|
|
981
|
+
const outPath = path.extname(output) ? output : path.join(output, "schema.hcl");
|
|
982
|
+
return yield* emitRaw(content, path.dirname(outPath), outPath);
|
|
1377
983
|
});
|
|
1378
984
|
}
|
|
1379
985
|
//#endregion
|
|
1380
|
-
//#region src/generator/dbml.ts
|
|
1381
|
-
function dbmlContent(datamodel, mapToDbSchema = false) {
|
|
1382
|
-
const tables = makeTables(datamodel.models, mapToDbSchema);
|
|
1383
|
-
const enums = makeEnums(datamodel.enums);
|
|
1384
|
-
const refs = makeRelations$1(datamodel.models, mapToDbSchema);
|
|
1385
|
-
const logicalRefs = annotatedDbmlRefs(datamodel.models);
|
|
1386
|
-
return [
|
|
1387
|
-
...enums,
|
|
1388
|
-
...tables,
|
|
1389
|
-
...refs,
|
|
1390
|
-
...logicalRefs
|
|
1391
|
-
].join("\n\n");
|
|
1392
|
-
}
|
|
1393
|
-
function dbmlToPng(dbml) {
|
|
1394
|
-
const svg = run(dbml, "svg");
|
|
1395
|
-
return new Resvg(svg, { font: { loadSystemFonts: true } }).render().asPng();
|
|
1396
|
-
}
|
|
1397
|
-
//#endregion
|
|
1398
|
-
//#region src/core/dbml.ts
|
|
1399
|
-
function resolveOutPath(output) {
|
|
1400
|
-
if (path.extname(output)) return output;
|
|
1401
|
-
return path.join(output, "schema.dbml");
|
|
1402
|
-
}
|
|
1403
|
-
async function dbml(options) {
|
|
1404
|
-
if (!(options.generator.isCustomOutput && options.generator.output?.value)) return {
|
|
1405
|
-
ok: false,
|
|
1406
|
-
error: "output is required for Hekireki-DBML. Please specify output in your generator config."
|
|
1407
|
-
};
|
|
1408
|
-
const output = options.generator.output.value;
|
|
1409
|
-
const mapToDbSchema = getString(options.generator.config?.mapToDbSchema) !== "false";
|
|
1410
|
-
const content = dbmlContent(options.dmmf.datamodel, mapToDbSchema);
|
|
1411
|
-
const outPath = resolveOutPath(output);
|
|
1412
|
-
return emitRaw(outPath.endsWith(".png") ? dbmlToPng(content) : content, path.dirname(outPath), outPath);
|
|
1413
|
-
}
|
|
1414
|
-
//#endregion
|
|
1415
986
|
//#region src/helper/django.ts
|
|
1416
987
|
const PRISMA_TO_DJANGO = {
|
|
1417
988
|
String: "TextField",
|
|
@@ -1611,24 +1182,24 @@ function jsonToPythonLiteral$1(value) {
|
|
|
1611
1182
|
if (typeof value === "object") return `{${Object.entries(value).map(([k, v]) => `${toPythonString$1(k)}: ${jsonToPythonLiteral$1(v)}`).join(", ")}}`;
|
|
1612
1183
|
return "None";
|
|
1613
1184
|
}
|
|
1614
|
-
function isFunctionDefault$
|
|
1185
|
+
function isFunctionDefault$5(def) {
|
|
1615
1186
|
return def !== null && typeof def === "object" && "name" in def && !Array.isArray(def);
|
|
1616
1187
|
}
|
|
1617
1188
|
function isAutoincrement$3(field) {
|
|
1618
|
-
return isFunctionDefault$
|
|
1189
|
+
return isFunctionDefault$5(field.default) && field.default.name === "autoincrement";
|
|
1619
1190
|
}
|
|
1620
1191
|
function uuidDefaultVersion$1(field) {
|
|
1621
|
-
if (!(isFunctionDefault$
|
|
1192
|
+
if (!(isFunctionDefault$5(field.default) && field.default.name === "uuid")) return null;
|
|
1622
1193
|
return field.default.args[0] === 7 ? 7 : 4;
|
|
1623
1194
|
}
|
|
1624
1195
|
function isUlidDefault$1(field) {
|
|
1625
|
-
return isFunctionDefault$
|
|
1196
|
+
return isFunctionDefault$5(field.default) && field.default.name === "ulid";
|
|
1626
1197
|
}
|
|
1627
1198
|
function isNowDefault(field) {
|
|
1628
|
-
return field.type === "DateTime" && isFunctionDefault$
|
|
1199
|
+
return field.type === "DateTime" && isFunctionDefault$5(field.default) && field.default.name === "now";
|
|
1629
1200
|
}
|
|
1630
1201
|
function dbGeneratedExpr(field) {
|
|
1631
|
-
return isFunctionDefault$
|
|
1202
|
+
return isFunctionDefault$5(field.default) && field.default.name === "dbgenerated" && typeof field.default.args[0] === "string" ? field.default.args[0] : null;
|
|
1632
1203
|
}
|
|
1633
1204
|
function scalarElementLiteral(type, value) {
|
|
1634
1205
|
if (typeof value === "boolean") return value ? "True" : "False";
|
|
@@ -1749,7 +1320,7 @@ function formatDefaultArgs(model, field, isPk, names) {
|
|
|
1749
1320
|
if (isUlidDefault$1(field)) return ["default=ulid_str"];
|
|
1750
1321
|
const def = field.default;
|
|
1751
1322
|
if (def === void 0 || def === null) return [];
|
|
1752
|
-
if (isFunctionDefault$
|
|
1323
|
+
if (isFunctionDefault$5(def)) return [];
|
|
1753
1324
|
if (Array.isArray(def)) return def.length === 0 ? ["default=list"] : [`default=${helperNameOf(names, model, field)}`];
|
|
1754
1325
|
if (field.kind === "enum" && typeof def === "string") return [`default=${makePascalCase(field.type)}.${enumMemberName(def)}`];
|
|
1755
1326
|
if (isPk) return [];
|
|
@@ -2078,999 +1649,30 @@ function collectGlobalImports$1(models, indexes) {
|
|
|
2078
1649
|
//#region src/generator/django.ts
|
|
2079
1650
|
function djangoCode(models, enums, indexes) {
|
|
2080
1651
|
const idx = indexes ?? [];
|
|
2081
|
-
const emitted = models.filter((model) => hasPrimaryKey(model));
|
|
2082
|
-
const m2mTables = collectManyToManyTables$1(emitted, enums);
|
|
2083
|
-
const names = resolveNames(emitted, m2mTables);
|
|
2084
|
-
return `${[
|
|
2085
|
-
collectGlobalImports$1(emitted, idx).join("\n"),
|
|
2086
|
-
...collectDefaultHelpers(emitted, names, enums),
|
|
2087
|
-
...(enums ?? []).map((e) => generateEnumClass(e)),
|
|
2088
|
-
...emitted.map((model) => generateModelBody$1(model, models, idx, m2mTables, names)).filter((body) => body !== null),
|
|
2089
|
-
...m2mTables.map((t) => generateThroughModel(t))
|
|
2090
|
-
].join("\n\n\n")}\n`;
|
|
2091
|
-
}
|
|
2092
|
-
//#endregion
|
|
2093
|
-
//#region src/core/django.ts
|
|
2094
|
-
async function django(options) {
|
|
2095
|
-
if (!(options.generator.isCustomOutput && options.generator.output?.value)) return {
|
|
2096
|
-
ok: false,
|
|
2097
|
-
error: "output is required for Hekireki-Django. Please specify output in your generator config."
|
|
2098
|
-
};
|
|
2099
|
-
const output = options.generator.output.value;
|
|
2100
|
-
const outPath = path.extname(output) ? output : path.join(output, "models.py");
|
|
2101
|
-
const enums = options.dmmf.datamodel.enums;
|
|
2102
|
-
const indexes = options.dmmf.datamodel.indexes;
|
|
2103
|
-
const conflicts = findNameConflicts(options.dmmf.datamodel.models, enums);
|
|
2104
|
-
if (conflicts.length > 0) return {
|
|
2105
|
-
ok: false,
|
|
2106
|
-
error: `Hekireki-Django cannot represent this schema:\n${conflicts.map((c) => ` - ${c}`).join("\n")}`
|
|
2107
|
-
};
|
|
2108
|
-
return emitRaw(djangoCode(options.dmmf.datamodel.models, enums, indexes), path.dirname(outPath), outPath);
|
|
2109
|
-
}
|
|
2110
|
-
//#endregion
|
|
2111
|
-
//#region src/helper/docs/generator/transform-dmmf.ts
|
|
2112
|
-
const legacyName = (mapping, key) => {
|
|
2113
|
-
const value = Object.entries(mapping).find(([k]) => k === key)?.[1];
|
|
2114
|
-
return typeof value === "string" ? value : void 0;
|
|
2115
|
-
};
|
|
2116
|
-
const getMappings = (mappings, datamodel) => mappings.modelOperations.filter((mapping) => {
|
|
2117
|
-
const model = datamodel.models.find((m) => m.name === mapping.model);
|
|
2118
|
-
if (!model) throw new Error(`Mapping without model ${mapping.model}`);
|
|
2119
|
-
return model.fields.some((f) => f.kind !== "object");
|
|
2120
|
-
}).map((mapping) => ({
|
|
2121
|
-
model: mapping.model,
|
|
2122
|
-
findUnique: legacyName(mapping, "findSingle") ?? legacyName(mapping, "findOne") ?? mapping.findUnique,
|
|
2123
|
-
findFirst: mapping.findFirst,
|
|
2124
|
-
findMany: mapping.findMany,
|
|
2125
|
-
create: legacyName(mapping, "createOne") ?? legacyName(mapping, "createSingle") ?? mapping.create,
|
|
2126
|
-
delete: legacyName(mapping, "deleteOne") ?? legacyName(mapping, "deleteSingle") ?? mapping.delete,
|
|
2127
|
-
update: legacyName(mapping, "updateOne") ?? legacyName(mapping, "updateSingle") ?? mapping.update,
|
|
2128
|
-
deleteMany: mapping.deleteMany,
|
|
2129
|
-
updateMany: mapping.updateMany,
|
|
2130
|
-
upsert: legacyName(mapping, "upsertOne") ?? legacyName(mapping, "upsertSingle") ?? mapping.upsert
|
|
2131
|
-
}));
|
|
2132
|
-
const transformDMMF = (dmmf) => ({
|
|
2133
|
-
...dmmf,
|
|
2134
|
-
datamodel: dmmf.datamodel,
|
|
2135
|
-
mappings: getMappings(dmmf.mappings, dmmf.datamodel)
|
|
2136
|
-
});
|
|
2137
|
-
//#endregion
|
|
2138
|
-
//#region src/helper/docs/styles.ts
|
|
2139
|
-
const globalCss = css`
|
|
2140
|
-
:-hono-global {
|
|
2141
|
-
:root {
|
|
2142
|
-
--bg-primary: #e5e7eb;
|
|
2143
|
-
--bg-secondary: #ffffff;
|
|
2144
|
-
--text-primary: #111827;
|
|
2145
|
-
--text-secondary: #374151;
|
|
2146
|
-
--text-muted: #6b7280;
|
|
2147
|
-
--border-color: #d1d5db;
|
|
2148
|
-
--link-color: #4f46e5;
|
|
2149
|
-
--icon-color: #4f46e5;
|
|
2150
|
-
--code-bg: #f6f8fa;
|
|
2151
|
-
--code-color: inherit;
|
|
2152
|
-
}
|
|
2153
|
-
:root.dark {
|
|
2154
|
-
--bg-primary: #1f2937;
|
|
2155
|
-
--bg-secondary: #1f2937;
|
|
2156
|
-
--text-primary: #f9fafb;
|
|
2157
|
-
--text-secondary: #e5e7eb;
|
|
2158
|
-
--text-muted: #9ca3af;
|
|
2159
|
-
--border-color: #4b5563;
|
|
2160
|
-
--link-color: #818cf8;
|
|
2161
|
-
--icon-color: #818cf8;
|
|
2162
|
-
--code-bg: #374151;
|
|
2163
|
-
--code-color: #e5e7eb;
|
|
2164
|
-
}
|
|
2165
|
-
* {
|
|
2166
|
-
box-sizing: border-box;
|
|
2167
|
-
}
|
|
2168
|
-
body {
|
|
2169
|
-
margin: 0;
|
|
2170
|
-
font-family:
|
|
2171
|
-
-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
|
|
2172
|
-
background-color: var(--bg-primary);
|
|
2173
|
-
color: var(--text-primary);
|
|
2174
|
-
min-height: 100vh;
|
|
2175
|
-
}
|
|
2176
|
-
a {
|
|
2177
|
-
color: var(--link-color);
|
|
2178
|
-
text-decoration: none;
|
|
2179
|
-
}
|
|
2180
|
-
a:hover {
|
|
2181
|
-
text-decoration: underline;
|
|
2182
|
-
}
|
|
2183
|
-
}
|
|
2184
|
-
`;
|
|
2185
|
-
const containerClass = css`
|
|
2186
|
-
display: flex;
|
|
2187
|
-
min-height: 100vh;
|
|
2188
|
-
`;
|
|
2189
|
-
const sidebarClass = css`
|
|
2190
|
-
position: sticky;
|
|
2191
|
-
top: 0;
|
|
2192
|
-
width: 20%;
|
|
2193
|
-
flex-shrink: 0;
|
|
2194
|
-
height: 100vh;
|
|
2195
|
-
padding: 1rem 1.5rem;
|
|
2196
|
-
overflow: auto;
|
|
2197
|
-
background-color: var(--bg-secondary);
|
|
2198
|
-
`;
|
|
2199
|
-
const mainContentClass = css`
|
|
2200
|
-
width: 100%;
|
|
2201
|
-
padding: 1rem;
|
|
2202
|
-
background-color: var(--bg-secondary);
|
|
2203
|
-
overflow-x: hidden;
|
|
2204
|
-
`;
|
|
2205
|
-
const headerClass = css`
|
|
2206
|
-
margin-bottom: 2rem;
|
|
2207
|
-
`;
|
|
2208
|
-
const logoContainerClass = css`
|
|
2209
|
-
display: flex;
|
|
2210
|
-
align-items: center;
|
|
2211
|
-
gap: 0.5rem;
|
|
2212
|
-
`;
|
|
2213
|
-
const logoTextClass = css`
|
|
2214
|
-
font-size: 1.25rem;
|
|
2215
|
-
font-weight: 700;
|
|
2216
|
-
color: var(--text-primary);
|
|
2217
|
-
`;
|
|
2218
|
-
const darkModeToggleContainerClass = css`
|
|
2219
|
-
margin-top: 1.25rem;
|
|
2220
|
-
`;
|
|
2221
|
-
const darkModeToggleLabelClass = css`
|
|
2222
|
-
color: var(--text-primary);
|
|
2223
|
-
margin-left: 0.5rem;
|
|
2224
|
-
`;
|
|
2225
|
-
const iconClass = css`
|
|
2226
|
-
color: var(--icon-color);
|
|
2227
|
-
`;
|
|
2228
|
-
const h1Class = css`
|
|
2229
|
-
font-size: 1.875rem;
|
|
2230
|
-
font-weight: 700;
|
|
2231
|
-
color: var(--text-primary);
|
|
2232
|
-
margin: 0 0 1rem 0;
|
|
2233
|
-
`;
|
|
2234
|
-
const h2Class = css`
|
|
2235
|
-
font-size: 1.5rem;
|
|
2236
|
-
font-weight: 600;
|
|
2237
|
-
color: var(--text-primary);
|
|
2238
|
-
margin: 0 0 0.5rem 0;
|
|
2239
|
-
`;
|
|
2240
|
-
const h3Class = css`
|
|
2241
|
-
font-size: 1.25rem;
|
|
2242
|
-
font-weight: 600;
|
|
2243
|
-
color: var(--text-primary);
|
|
2244
|
-
margin: 0 0 0.5rem 0;
|
|
2245
|
-
`;
|
|
2246
|
-
const h4Class = css`
|
|
2247
|
-
font-size: 1.125rem;
|
|
2248
|
-
font-weight: 700;
|
|
2249
|
-
color: var(--text-primary);
|
|
2250
|
-
margin: 0 0 0.5rem 0;
|
|
2251
|
-
`;
|
|
2252
|
-
const textClass = css`
|
|
2253
|
-
color: var(--text-primary);
|
|
2254
|
-
`;
|
|
2255
|
-
const tableClass = css`
|
|
2256
|
-
border-collapse: collapse;
|
|
2257
|
-
table-layout: auto;
|
|
2258
|
-
& th {
|
|
2259
|
-
padding: 0.5rem 1rem;
|
|
2260
|
-
border: 1px solid var(--border-color);
|
|
2261
|
-
color: var(--text-primary);
|
|
2262
|
-
font-weight: 600;
|
|
2263
|
-
text-align: left;
|
|
2264
|
-
}
|
|
2265
|
-
& td {
|
|
2266
|
-
padding: 0.5rem 1rem;
|
|
2267
|
-
border: 1px solid var(--border-color);
|
|
2268
|
-
color: var(--text-primary);
|
|
2269
|
-
}
|
|
2270
|
-
`;
|
|
2271
|
-
const tocSectionClass = css`
|
|
2272
|
-
margin-bottom: 0.5rem;
|
|
2273
|
-
margin-left: 0.25rem;
|
|
2274
|
-
list-style: none;
|
|
2275
|
-
padding: 0;
|
|
2276
|
-
`;
|
|
2277
|
-
const tocSubHeaderClass = css`
|
|
2278
|
-
font-weight: 600;
|
|
2279
|
-
color: var(--text-secondary);
|
|
2280
|
-
`;
|
|
2281
|
-
const tocSubSectionClass = css`
|
|
2282
|
-
margin-top: 0.25rem;
|
|
2283
|
-
margin-left: 0.5rem;
|
|
2284
|
-
`;
|
|
2285
|
-
const tocSubSectionTitleClass = css`
|
|
2286
|
-
margin-bottom: 0.25rem;
|
|
2287
|
-
font-weight: 500;
|
|
2288
|
-
color: var(--text-muted);
|
|
2289
|
-
`;
|
|
2290
|
-
const tocListClass = css`
|
|
2291
|
-
padding-left: 0.75rem;
|
|
2292
|
-
margin: 0;
|
|
2293
|
-
margin-left: 0.25rem;
|
|
2294
|
-
border-left: 2px solid var(--border-color);
|
|
2295
|
-
list-style: none;
|
|
2296
|
-
`;
|
|
2297
|
-
const tocLinkClass = css`
|
|
2298
|
-
color: var(--text-primary);
|
|
2299
|
-
text-decoration: none;
|
|
2300
|
-
&:hover {
|
|
2301
|
-
text-decoration: underline;
|
|
2302
|
-
}
|
|
2303
|
-
`;
|
|
2304
|
-
const tocHeadingClass = css`
|
|
2305
|
-
margin-bottom: 0.5rem;
|
|
2306
|
-
font-weight: 700;
|
|
2307
|
-
color: var(--text-primary);
|
|
2308
|
-
`;
|
|
2309
|
-
const codeBlockClass = css`
|
|
2310
|
-
background: var(--code-bg);
|
|
2311
|
-
color: var(--code-color);
|
|
2312
|
-
padding: 1em;
|
|
2313
|
-
border-radius: 0.5em;
|
|
2314
|
-
overflow: auto;
|
|
2315
|
-
font-family:
|
|
2316
|
-
'SFMono-Regular', Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace;
|
|
2317
|
-
font-size: 14px;
|
|
2318
|
-
line-height: 1.5;
|
|
2319
|
-
margin: 0;
|
|
2320
|
-
`;
|
|
2321
|
-
const sectionClass = css`
|
|
2322
|
-
margin-bottom: 2rem;
|
|
2323
|
-
`;
|
|
2324
|
-
const modelSectionClass = css`
|
|
2325
|
-
padding-left: 1rem;
|
|
2326
|
-
margin-bottom: 1rem;
|
|
2327
|
-
`;
|
|
2328
|
-
const fieldsSectionClass = css`
|
|
2329
|
-
padding-left: 1rem;
|
|
2330
|
-
margin-top: 1rem;
|
|
2331
|
-
`;
|
|
2332
|
-
const operationsSectionClass = css`
|
|
2333
|
-
padding-left: 1rem;
|
|
2334
|
-
margin-top: 1rem;
|
|
2335
|
-
`;
|
|
2336
|
-
const operationItemClass = css`
|
|
2337
|
-
margin-top: 1rem;
|
|
2338
|
-
`;
|
|
2339
|
-
const hrClass = css`
|
|
2340
|
-
margin: 2rem 0;
|
|
2341
|
-
border: none;
|
|
2342
|
-
border-top: 1px solid var(--border-color);
|
|
2343
|
-
`;
|
|
2344
|
-
const hrSmallClass = css`
|
|
2345
|
-
margin: 1rem 0;
|
|
2346
|
-
border: none;
|
|
2347
|
-
border-top: 1px solid var(--border-color);
|
|
2348
|
-
`;
|
|
2349
|
-
const listItemClass = css`
|
|
2350
|
-
margin-bottom: 1rem;
|
|
2351
|
-
`;
|
|
2352
|
-
const mb2Class = css`
|
|
2353
|
-
margin-bottom: 0.5rem;
|
|
2354
|
-
`;
|
|
2355
|
-
const ml4Class = css`
|
|
2356
|
-
margin-left: 1rem;
|
|
2357
|
-
`;
|
|
2358
|
-
//#endregion
|
|
2359
|
-
//#region src/helper/docs/generator/helpers.ts
|
|
2360
|
-
const capitalize = (str) => str[0].toUpperCase() + str.slice(1);
|
|
2361
|
-
const lowerCase = (name) => name.slice(0, 1).toLowerCase() + name.slice(1);
|
|
2362
|
-
const primitiveTypes = /* @__PURE__ */ new Set([
|
|
2363
|
-
"String",
|
|
2364
|
-
"Boolean",
|
|
2365
|
-
"Int",
|
|
2366
|
-
"Float",
|
|
2367
|
-
"Json",
|
|
2368
|
-
"DateTime",
|
|
2369
|
-
"Null"
|
|
2370
|
-
]);
|
|
2371
|
-
const isScalarType = (type) => primitiveTypes.has(type);
|
|
2372
|
-
//#endregion
|
|
2373
|
-
//#region src/helper/docs/generator/apitypes.tsx
|
|
2374
|
-
const TypeRefLink = ({ typeRef, kind }) => {
|
|
2375
|
-
const typeName = typeRef.type;
|
|
2376
|
-
if (isScalarType(typeName)) return /* @__PURE__ */ jsx(Fragment, { children: typeName });
|
|
2377
|
-
return /* @__PURE__ */ jsxs("a", {
|
|
2378
|
-
href: `#type-${kind}-${typeName}`,
|
|
2379
|
-
children: [typeName, typeRef.isList ? "[]" : ""]
|
|
2380
|
-
});
|
|
2381
|
-
};
|
|
2382
|
-
const TypeFieldRow = ({ field, kind }) => /* @__PURE__ */ jsxs("tr", { children: [
|
|
2383
|
-
/* @__PURE__ */ jsx("td", { children: field.name }),
|
|
2384
|
-
/* @__PURE__ */ jsx("td", { children: field.type.map((t, i) => /* @__PURE__ */ jsxs(Fragment, { children: [i > 0 && " | ", /* @__PURE__ */ jsx(TypeRefLink, {
|
|
2385
|
-
typeRef: t,
|
|
2386
|
-
kind
|
|
2387
|
-
})] })) }),
|
|
2388
|
-
/* @__PURE__ */ jsx("td", { children: field.nullable ? /* @__PURE__ */ jsx("strong", { children: "Yes" }) : "No" })
|
|
2389
|
-
] });
|
|
2390
|
-
const TypeItem = ({ type, kind }) => /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("h3", {
|
|
2391
|
-
class: h3Class,
|
|
2392
|
-
id: `type-${kind}-${type.name}`,
|
|
2393
|
-
children: type.name
|
|
2394
|
-
}), /* @__PURE__ */ jsxs("table", {
|
|
2395
|
-
class: tableClass,
|
|
2396
|
-
children: [/* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsxs("tr", { children: [
|
|
2397
|
-
/* @__PURE__ */ jsx("th", { children: "Name" }),
|
|
2398
|
-
/* @__PURE__ */ jsx("th", { children: "Type" }),
|
|
2399
|
-
/* @__PURE__ */ jsx("th", { children: "Nullable" })
|
|
2400
|
-
] }) }), /* @__PURE__ */ jsx("tbody", { children: type.fields.map((field) => /* @__PURE__ */ jsx(TypeFieldRow, {
|
|
2401
|
-
field,
|
|
2402
|
-
kind
|
|
2403
|
-
})) })]
|
|
2404
|
-
})] });
|
|
2405
|
-
const TypesSection = ({ data }) => /* @__PURE__ */ jsxs("div", {
|
|
2406
|
-
class: sectionClass,
|
|
2407
|
-
children: [
|
|
2408
|
-
/* @__PURE__ */ jsx("h1", {
|
|
2409
|
-
class: h1Class,
|
|
2410
|
-
id: "types",
|
|
2411
|
-
children: "Types"
|
|
2412
|
-
}),
|
|
2413
|
-
/* @__PURE__ */ jsxs("div", {
|
|
2414
|
-
class: ml4Class,
|
|
2415
|
-
children: [/* @__PURE__ */ jsx("h2", {
|
|
2416
|
-
class: h2Class,
|
|
2417
|
-
id: "input-types",
|
|
2418
|
-
children: "Input Types"
|
|
2419
|
-
}), /* @__PURE__ */ jsx("div", {
|
|
2420
|
-
class: ml4Class,
|
|
2421
|
-
children: data.inputTypes.map((inputType, i) => /* @__PURE__ */ jsxs(Fragment, { children: [i > 0 && /* @__PURE__ */ jsx("hr", { class: hrSmallClass }), /* @__PURE__ */ jsx(TypeItem, {
|
|
2422
|
-
type: inputType,
|
|
2423
|
-
kind: "inputType"
|
|
2424
|
-
})] }))
|
|
2425
|
-
})]
|
|
2426
|
-
}),
|
|
2427
|
-
/* @__PURE__ */ jsxs("div", {
|
|
2428
|
-
class: ml4Class,
|
|
2429
|
-
children: [/* @__PURE__ */ jsx("h2", {
|
|
2430
|
-
class: h2Class,
|
|
2431
|
-
id: "output-types",
|
|
2432
|
-
children: "Output Types"
|
|
2433
|
-
}), /* @__PURE__ */ jsx("div", {
|
|
2434
|
-
class: ml4Class,
|
|
2435
|
-
children: data.outputTypes.map((outputType, i) => /* @__PURE__ */ jsxs(Fragment, { children: [i > 0 && /* @__PURE__ */ jsx("hr", { class: hrSmallClass }), /* @__PURE__ */ jsx(TypeItem, {
|
|
2436
|
-
type: outputType,
|
|
2437
|
-
kind: "outputType"
|
|
2438
|
-
})] }))
|
|
2439
|
-
})]
|
|
2440
|
-
})
|
|
2441
|
-
]
|
|
2442
|
-
});
|
|
2443
|
-
const getInputTypes = (dmmfInputType) => dmmfInputType.map((inputType) => ({
|
|
2444
|
-
name: inputType.name,
|
|
2445
|
-
fields: inputType.fields.map((ip) => ({
|
|
2446
|
-
name: ip.name,
|
|
2447
|
-
nullable: ip.isNullable,
|
|
2448
|
-
type: ip.inputTypes
|
|
2449
|
-
}))
|
|
2450
|
-
}));
|
|
2451
|
-
const getOutputTypes = (dmmfOutputTypes) => dmmfOutputTypes.map((outputType) => ({
|
|
2452
|
-
name: outputType.name,
|
|
2453
|
-
fields: outputType.fields.map((op) => ({
|
|
2454
|
-
name: op.name,
|
|
2455
|
-
nullable: !op.isNullable,
|
|
2456
|
-
type: [{
|
|
2457
|
-
isList: op.outputType.isList,
|
|
2458
|
-
type: op.outputType.type
|
|
2459
|
-
}]
|
|
2460
|
-
}))
|
|
2461
|
-
}));
|
|
2462
|
-
const getTypesData = (d) => {
|
|
2463
|
-
const prismaInputTypes = d.schema.inputObjectTypes.prisma ?? [];
|
|
2464
|
-
return {
|
|
2465
|
-
inputTypes: getInputTypes(prismaInputTypes),
|
|
2466
|
-
outputTypes: getOutputTypes([...d.schema.outputObjectTypes.model, ...d.schema.outputObjectTypes.prisma.filter((op) => op.name !== "Query" && op.name !== "Mutation")])
|
|
2467
|
-
};
|
|
2468
|
-
};
|
|
2469
|
-
const createTypes = (d) => {
|
|
2470
|
-
const data = getTypesData(d);
|
|
2471
|
-
return /* @__PURE__ */ jsx(TypesSection, { data });
|
|
2472
|
-
};
|
|
2473
|
-
//#endregion
|
|
2474
|
-
//#region src/helper/docs/generator/model.tsx
|
|
2475
|
-
const ModelAction = {
|
|
2476
|
-
create: "create",
|
|
2477
|
-
deleteMany: "deleteMany",
|
|
2478
|
-
delete: "delete",
|
|
2479
|
-
findMany: "findMany",
|
|
2480
|
-
findUnique: "findUnique",
|
|
2481
|
-
findFirst: "findFirst",
|
|
2482
|
-
update: "update",
|
|
2483
|
-
updateMany: "updateMany",
|
|
2484
|
-
upsert: "upsert"
|
|
2485
|
-
};
|
|
2486
|
-
function isFieldDefault$1(v) {
|
|
2487
|
-
return typeof v === "object" && v !== null && "name" in v && "args" in v;
|
|
2488
|
-
}
|
|
2489
|
-
const fieldDirectiveMap = /* @__PURE__ */ new Map([
|
|
2490
|
-
["isUnique", "@unique"],
|
|
2491
|
-
["isId", "@id"],
|
|
2492
|
-
["hasDefaultValue", "@default"],
|
|
2493
|
-
["isUpdatedAt", "@updatedAt"]
|
|
2494
|
-
]);
|
|
2495
|
-
const escapeHtml = (str) => str.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("\"", """).replaceAll("'", "'");
|
|
2496
|
-
const DirectiveRow = ({ directive }) => /* @__PURE__ */ jsxs("tr", { children: [/* @__PURE__ */ jsx("td", { children: /* @__PURE__ */ jsx("strong", { children: directive.name }) }), /* @__PURE__ */ jsx("td", { children: /* @__PURE__ */ jsx("ul", { children: directive.values.map((val) => /* @__PURE__ */ jsx("li", { children: val })) }) })] });
|
|
2497
|
-
const FieldTypeLink = ({ type, bareTypeName }) => {
|
|
2498
|
-
if (isScalarType(bareTypeName)) return /* @__PURE__ */ jsx(Fragment, { children: type });
|
|
2499
|
-
return /* @__PURE__ */ jsx("a", {
|
|
2500
|
-
href: `#type-outputType-${bareTypeName}`,
|
|
2501
|
-
children: type
|
|
2502
|
-
});
|
|
2503
|
-
};
|
|
2504
|
-
const FieldTableRow = ({ field, modelName }) => /* @__PURE__ */ jsxs("tr", {
|
|
2505
|
-
id: `model-${modelName}-${field.name}`,
|
|
2506
|
-
children: [
|
|
2507
|
-
/* @__PURE__ */ jsx("td", { children: field.name }),
|
|
2508
|
-
/* @__PURE__ */ jsx("td", { children: /* @__PURE__ */ jsx(FieldTypeLink, {
|
|
2509
|
-
type: field.type,
|
|
2510
|
-
bareTypeName: field.bareTypeName
|
|
2511
|
-
}) }),
|
|
2512
|
-
/* @__PURE__ */ jsx("td", { children: /* @__PURE__ */ jsx("ul", { children: field.directives.length > 0 ? field.directives.map((directive) => /* @__PURE__ */ jsx("li", { children: /* @__PURE__ */ jsx("strong", { children: directive }) })) : /* @__PURE__ */ jsx("li", { children: " - " }) }) }),
|
|
2513
|
-
/* @__PURE__ */ jsx("td", { children: field.required ? /* @__PURE__ */ jsx("strong", { children: "Yes" }) : "No" }),
|
|
2514
|
-
/* @__PURE__ */ jsx("td", { children: field.documentation ?? "-" })
|
|
2515
|
-
]
|
|
2516
|
-
});
|
|
2517
|
-
const OperationInputTypeLink = ({ typeRef }) => {
|
|
2518
|
-
const typeName = typeRef.type;
|
|
2519
|
-
if (isScalarType(typeName)) return /* @__PURE__ */ jsx(Fragment, { children: typeName });
|
|
2520
|
-
return /* @__PURE__ */ jsxs("a", {
|
|
2521
|
-
href: `#type-inputType-${typeName}`,
|
|
2522
|
-
children: [typeName, typeRef.isList ? "[]" : ""]
|
|
2523
|
-
});
|
|
2524
|
-
};
|
|
2525
|
-
const OperationInputRow = ({ opKey }) => /* @__PURE__ */ jsxs("tr", { children: [
|
|
2526
|
-
/* @__PURE__ */ jsx("td", { children: opKey.name }),
|
|
2527
|
-
/* @__PURE__ */ jsx("td", { children: opKey.types.map((t, i) => /* @__PURE__ */ jsxs(Fragment, { children: [i > 0 && " | ", /* @__PURE__ */ jsx(OperationInputTypeLink, { typeRef: t })] })) }),
|
|
2528
|
-
/* @__PURE__ */ jsx("td", { children: opKey.required ? /* @__PURE__ */ jsx("strong", { children: "Yes" }) : "No" })
|
|
2529
|
-
] });
|
|
2530
|
-
const OperationMarkup = ({ operation, modelName }) => /* @__PURE__ */ jsxs("div", {
|
|
2531
|
-
class: operationItemClass,
|
|
2532
|
-
children: [
|
|
2533
|
-
/* @__PURE__ */ jsx("h4", {
|
|
2534
|
-
id: `model-${modelName}-${operation.name}`,
|
|
2535
|
-
class: h4Class,
|
|
2536
|
-
children: operation.name
|
|
2537
|
-
}),
|
|
2538
|
-
/* @__PURE__ */ jsx("p", {
|
|
2539
|
-
class: textClass,
|
|
2540
|
-
children: operation.description
|
|
2541
|
-
}),
|
|
2542
|
-
/* @__PURE__ */ jsx("div", {
|
|
2543
|
-
class: mb2Class,
|
|
2544
|
-
children: /* @__PURE__ */ jsx("pre", {
|
|
2545
|
-
class: codeBlockClass,
|
|
2546
|
-
children: /* @__PURE__ */ jsx("code", { children: operation.usage })
|
|
2547
|
-
})
|
|
2548
|
-
}),
|
|
2549
|
-
/* @__PURE__ */ jsx("h4", {
|
|
2550
|
-
class: h4Class,
|
|
2551
|
-
children: "Input"
|
|
2552
|
-
}),
|
|
2553
|
-
/* @__PURE__ */ jsxs("table", {
|
|
2554
|
-
class: tableClass,
|
|
2555
|
-
children: [/* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsxs("tr", { children: [
|
|
2556
|
-
/* @__PURE__ */ jsx("th", { children: "Name" }),
|
|
2557
|
-
/* @__PURE__ */ jsx("th", { children: "Type" }),
|
|
2558
|
-
/* @__PURE__ */ jsx("th", { children: "Required" })
|
|
2559
|
-
] }) }), /* @__PURE__ */ jsx("tbody", { children: operation.opKeys?.map((opK) => /* @__PURE__ */ jsx(OperationInputRow, { opKey: opK })) })]
|
|
2560
|
-
}),
|
|
2561
|
-
/* @__PURE__ */ jsx("h4", {
|
|
2562
|
-
class: h4Class,
|
|
2563
|
-
children: "Output"
|
|
2564
|
-
}),
|
|
2565
|
-
/* @__PURE__ */ jsxs("div", {
|
|
2566
|
-
class: textClass,
|
|
2567
|
-
children: [/* @__PURE__ */ jsx("strong", { children: "Type: " }), /* @__PURE__ */ jsx("a", {
|
|
2568
|
-
href: `#type-outputType-${operation.output.type}`,
|
|
2569
|
-
children: operation.output.type
|
|
2570
|
-
})]
|
|
2571
|
-
}),
|
|
2572
|
-
/* @__PURE__ */ jsxs("div", {
|
|
2573
|
-
class: textClass,
|
|
2574
|
-
children: [/* @__PURE__ */ jsx("strong", { children: "Required: " }), operation.output.required ? "Yes" : "No"]
|
|
2575
|
-
}),
|
|
2576
|
-
/* @__PURE__ */ jsxs("div", {
|
|
2577
|
-
class: textClass,
|
|
2578
|
-
children: [/* @__PURE__ */ jsx("strong", { children: "List: " }), operation.output.list ? "Yes" : "No"]
|
|
2579
|
-
})
|
|
2580
|
-
]
|
|
2581
|
-
});
|
|
2582
|
-
const ModelItem = ({ model, isLast }) => /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs("div", {
|
|
2583
|
-
class: modelSectionClass,
|
|
2584
|
-
children: [
|
|
2585
|
-
/* @__PURE__ */ jsx("h2", {
|
|
2586
|
-
class: h2Class,
|
|
2587
|
-
id: `model-${model.name}`,
|
|
2588
|
-
children: model.name
|
|
2589
|
-
}),
|
|
2590
|
-
model.documentation && /* @__PURE__ */ jsxs("div", {
|
|
2591
|
-
class: textClass,
|
|
2592
|
-
children: ["Description: ", model.documentation]
|
|
2593
|
-
}),
|
|
2594
|
-
model.directives.length > 0 && /* @__PURE__ */ jsxs("table", {
|
|
2595
|
-
class: tableClass,
|
|
2596
|
-
children: [/* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsxs("tr", { children: [/* @__PURE__ */ jsx("th", { children: "Name" }), /* @__PURE__ */ jsx("th", { children: "Value" })] }) }), /* @__PURE__ */ jsx("tbody", { children: model.directives.map((directive) => /* @__PURE__ */ jsx(DirectiveRow, { directive })) })]
|
|
2597
|
-
}),
|
|
2598
|
-
/* @__PURE__ */ jsxs("div", {
|
|
2599
|
-
class: fieldsSectionClass,
|
|
2600
|
-
children: [/* @__PURE__ */ jsx("h3", {
|
|
2601
|
-
class: h3Class,
|
|
2602
|
-
id: `model-${model.name}-fields`,
|
|
2603
|
-
children: "Fields"
|
|
2604
|
-
}), /* @__PURE__ */ jsx("div", {
|
|
2605
|
-
class: ml4Class,
|
|
2606
|
-
children: /* @__PURE__ */ jsxs("table", {
|
|
2607
|
-
class: tableClass,
|
|
2608
|
-
children: [/* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsxs("tr", { children: [
|
|
2609
|
-
/* @__PURE__ */ jsx("th", { children: "Name" }),
|
|
2610
|
-
/* @__PURE__ */ jsx("th", { children: "Type" }),
|
|
2611
|
-
/* @__PURE__ */ jsx("th", { children: "Attributes" }),
|
|
2612
|
-
/* @__PURE__ */ jsx("th", { children: "Required" }),
|
|
2613
|
-
/* @__PURE__ */ jsx("th", { children: "Comment" })
|
|
2614
|
-
] }) }), /* @__PURE__ */ jsx("tbody", { children: model.fields.map((field) => /* @__PURE__ */ jsx(FieldTableRow, {
|
|
2615
|
-
field,
|
|
2616
|
-
modelName: model.name
|
|
2617
|
-
})) })]
|
|
2618
|
-
})
|
|
2619
|
-
})]
|
|
2620
|
-
}),
|
|
2621
|
-
/* @__PURE__ */ jsx("hr", { class: hrClass }),
|
|
2622
|
-
/* @__PURE__ */ jsxs("div", {
|
|
2623
|
-
class: operationsSectionClass,
|
|
2624
|
-
children: [/* @__PURE__ */ jsx("h3", {
|
|
2625
|
-
class: h3Class,
|
|
2626
|
-
id: `model-${model.name}-operations`,
|
|
2627
|
-
children: "Operations"
|
|
2628
|
-
}), /* @__PURE__ */ jsx("div", {
|
|
2629
|
-
class: ml4Class,
|
|
2630
|
-
children: model.operations.map((op, i) => /* @__PURE__ */ jsxs(Fragment, { children: [i > 0 && /* @__PURE__ */ jsx("hr", { class: hrSmallClass }), /* @__PURE__ */ jsx(OperationMarkup, {
|
|
2631
|
-
operation: op,
|
|
2632
|
-
modelName: model.name
|
|
2633
|
-
})] }))
|
|
2634
|
-
})]
|
|
2635
|
-
})
|
|
2636
|
-
]
|
|
2637
|
-
}), !isLast && /* @__PURE__ */ jsx("hr", { class: hrClass })] });
|
|
2638
|
-
const ModelsSection = ({ data }) => /* @__PURE__ */ jsxs("div", {
|
|
2639
|
-
class: sectionClass,
|
|
2640
|
-
children: [/* @__PURE__ */ jsx("h1", {
|
|
2641
|
-
class: h1Class,
|
|
2642
|
-
id: "models",
|
|
2643
|
-
children: "Models"
|
|
2644
|
-
}), data.models.map((model, i) => /* @__PURE__ */ jsx(ModelItem, {
|
|
2645
|
-
model,
|
|
2646
|
-
isLast: i === data.models.length - 1
|
|
2647
|
-
}))]
|
|
2648
|
-
});
|
|
2649
|
-
const getModelDirective = (model) => {
|
|
2650
|
-
const directiveValue = [];
|
|
2651
|
-
if (model.primaryKey) directiveValue.push({
|
|
2652
|
-
name: "@@id",
|
|
2653
|
-
values: [...model.primaryKey.fields]
|
|
2654
|
-
});
|
|
2655
|
-
for (const uniqueField of model.uniqueFields) directiveValue.push({
|
|
2656
|
-
name: "@@unique",
|
|
2657
|
-
values: [...uniqueField]
|
|
2658
|
-
});
|
|
2659
|
-
for (const uniqueIndex of model.uniqueIndexes) directiveValue.push({
|
|
2660
|
-
name: "@@index",
|
|
2661
|
-
values: [...uniqueIndex.fields]
|
|
2662
|
-
});
|
|
2663
|
-
return directiveValue;
|
|
2664
|
-
};
|
|
2665
|
-
const getFieldType = (field) => {
|
|
2666
|
-
const name = field.isRequired || field.isList ? field.type : `${field.type}?`;
|
|
2667
|
-
return field.isList ? `${name.replace("?", "")}[]` : name;
|
|
2668
|
-
};
|
|
2669
|
-
const getFieldDirectives = (field) => {
|
|
2670
|
-
const filteredEntries = Object.entries(field).filter(([, v]) => Boolean(v));
|
|
2671
|
-
const directives = [];
|
|
2672
|
-
for (const [k] of filteredEntries) {
|
|
2673
|
-
const mappedDirectiveValue = fieldDirectiveMap.get(k);
|
|
2674
|
-
if (mappedDirectiveValue) {
|
|
2675
|
-
if (k === "hasDefaultValue" && field.default !== void 0) {
|
|
2676
|
-
if (typeof field.default === "string" || typeof field.default === "number" || typeof field.default === "boolean") directives.push(`${mappedDirectiveValue}(${field.default})`);
|
|
2677
|
-
else if (Array.isArray(field.default)) directives.push(`${mappedDirectiveValue}([${field.default.toString()}])`);
|
|
2678
|
-
else if (isFieldDefault$1(field.default)) directives.push(`${mappedDirectiveValue}(${field.default.name}(${field.default.args.join(",")}))`);
|
|
2679
|
-
} else directives.push(mappedDirectiveValue);
|
|
2680
|
-
}
|
|
2681
|
-
}
|
|
2682
|
-
return directives;
|
|
2683
|
-
};
|
|
2684
|
-
const getModelFields = (model) => model.fields.map((field) => ({
|
|
2685
|
-
name: field.name,
|
|
2686
|
-
type: getFieldType(field),
|
|
2687
|
-
bareTypeName: field.type,
|
|
2688
|
-
documentation: field.documentation,
|
|
2689
|
-
directives: getFieldDirectives(field),
|
|
2690
|
-
required: field.isRequired
|
|
2691
|
-
}));
|
|
2692
|
-
const mapArgs = (args) => args?.map((a) => ({
|
|
2693
|
-
name: a.name,
|
|
2694
|
-
types: a.inputTypes,
|
|
2695
|
-
required: a.isRequired
|
|
2696
|
-
}));
|
|
2697
|
-
const operationDescriptions = {
|
|
2698
|
-
[ModelAction.create]: (singular) => ({
|
|
2699
|
-
desc: `Create one ${singular}`,
|
|
2700
|
-
queryType: "Mutation"
|
|
2701
|
-
}),
|
|
2702
|
-
[ModelAction.deleteMany]: (singular) => ({
|
|
2703
|
-
desc: `Delete zero or more ${singular}`,
|
|
2704
|
-
queryType: "Mutation"
|
|
2705
|
-
}),
|
|
2706
|
-
[ModelAction.delete]: (singular) => ({
|
|
2707
|
-
desc: `Delete one ${singular}`,
|
|
2708
|
-
queryType: "Mutation"
|
|
2709
|
-
}),
|
|
2710
|
-
[ModelAction.findMany]: (_, plural) => ({
|
|
2711
|
-
desc: `Find zero or more ${plural}`,
|
|
2712
|
-
queryType: "Query"
|
|
2713
|
-
}),
|
|
2714
|
-
[ModelAction.findUnique]: (_, plural) => ({
|
|
2715
|
-
desc: `Find zero or one ${plural}`,
|
|
2716
|
-
queryType: "Query"
|
|
2717
|
-
}),
|
|
2718
|
-
[ModelAction.findFirst]: (_, plural) => ({
|
|
2719
|
-
desc: `Find first ${plural}`,
|
|
2720
|
-
queryType: "Query"
|
|
2721
|
-
}),
|
|
2722
|
-
[ModelAction.update]: (singular) => ({
|
|
2723
|
-
desc: `Update one ${singular}`,
|
|
2724
|
-
queryType: "Mutation"
|
|
2725
|
-
}),
|
|
2726
|
-
[ModelAction.updateMany]: (_, plural) => ({
|
|
2727
|
-
desc: `Update zero or one ${plural}`,
|
|
2728
|
-
queryType: "Mutation"
|
|
2729
|
-
}),
|
|
2730
|
-
[ModelAction.upsert]: (_, plural) => ({
|
|
2731
|
-
desc: `Create or update one ${plural}`,
|
|
2732
|
-
queryType: "Mutation"
|
|
2733
|
-
})
|
|
2734
|
-
};
|
|
2735
|
-
const operationUsageTemplates = {
|
|
2736
|
-
[ModelAction.create]: (singular, _, method) => `// Create one ${singular}
|
|
2737
|
-
const ${singular} = await ${method}({
|
|
2738
|
-
data: {
|
|
2739
|
-
// ... data to create a ${singular}
|
|
2740
|
-
}
|
|
2741
|
-
})`,
|
|
2742
|
-
[ModelAction.deleteMany]: (singular, plural, method) => `// Delete a few ${plural}
|
|
2743
|
-
const { count } = await ${method}({
|
|
2744
|
-
where: {
|
|
2745
|
-
// ... provide filter here
|
|
2746
|
-
}
|
|
2747
|
-
})`,
|
|
2748
|
-
[ModelAction.delete]: (singular, _, method) => `// Delete one ${singular}
|
|
2749
|
-
const ${singular} = await ${method}({
|
|
2750
|
-
where: {
|
|
2751
|
-
// ... filter to delete one ${singular}
|
|
2752
|
-
}
|
|
2753
|
-
})`,
|
|
2754
|
-
[ModelAction.findMany]: (_, plural, method) => `// Get all ${plural}
|
|
2755
|
-
const ${plural} = await ${method}()
|
|
2756
|
-
// Get first 10 ${plural}
|
|
2757
|
-
const ${plural} = await ${method}({ take: 10 })`,
|
|
2758
|
-
[ModelAction.findUnique]: (singular, _, method) => `// Get one ${singular}
|
|
2759
|
-
const ${lowerCase(singular)} = await ${method}({
|
|
2760
|
-
where: {
|
|
2761
|
-
// ... provide filter here
|
|
2762
|
-
}
|
|
2763
|
-
})`,
|
|
2764
|
-
[ModelAction.findFirst]: (singular, _, method) => `// Get one ${singular}
|
|
2765
|
-
const ${lowerCase(singular)} = await ${method}({
|
|
2766
|
-
where: {
|
|
2767
|
-
// ... provide filter here
|
|
2768
|
-
}
|
|
2769
|
-
})`,
|
|
2770
|
-
[ModelAction.update]: (singular, _, method) => `// Update one ${singular}
|
|
2771
|
-
const ${lowerCase(singular)} = await ${method}({
|
|
2772
|
-
where: {
|
|
2773
|
-
// ... provide filter here
|
|
2774
|
-
},
|
|
2775
|
-
data: {
|
|
2776
|
-
// ... provide data here
|
|
2777
|
-
}
|
|
2778
|
-
})`,
|
|
2779
|
-
[ModelAction.updateMany]: (_, __, method) => `const { count } = await ${method}({
|
|
2780
|
-
where: {
|
|
2781
|
-
// ... provide filter here
|
|
2782
|
-
},
|
|
2783
|
-
data: {
|
|
2784
|
-
// ... provide data here
|
|
2785
|
-
}
|
|
2786
|
-
})`,
|
|
2787
|
-
[ModelAction.upsert]: (singular, _, method) => `// Update or create a ${singular}
|
|
2788
|
-
const ${lowerCase(singular)} = await ${method}({
|
|
2789
|
-
create: {
|
|
2790
|
-
// ... data to create a ${singular}
|
|
2791
|
-
},
|
|
2792
|
-
update: {
|
|
2793
|
-
// ... in case it already exists, update
|
|
2794
|
-
},
|
|
2795
|
-
where: {
|
|
2796
|
-
// ... the filter for the ${singular} we want to update
|
|
2797
|
-
}
|
|
2798
|
-
})`
|
|
2799
|
-
};
|
|
2800
|
-
const getModelOperations = (model, mappings, schema) => {
|
|
2801
|
-
if (!mappings) throw new Error(`No operation mapping found for model: ${model.name}`);
|
|
2802
|
-
const modelOps = Object.entries(mappings).filter(([map]) => map !== "model");
|
|
2803
|
-
const singular = capitalize(model.name);
|
|
2804
|
-
const plural = capitalize(singular);
|
|
2805
|
-
return modelOps.map(([op, val]) => {
|
|
2806
|
-
const opInfo = operationDescriptions[op];
|
|
2807
|
-
const usageTemplate = operationUsageTemplates[op];
|
|
2808
|
-
if (!opInfo || !usageTemplate) return null;
|
|
2809
|
-
const { desc, queryType } = opInfo(singular, plural);
|
|
2810
|
-
const method = `prisma.${lowerCase(model.name)}.${op}`;
|
|
2811
|
-
const field = schema.outputObjectTypes.prisma.find((t) => t.name === queryType)?.fields.find((f) => f.name === val);
|
|
2812
|
-
return {
|
|
2813
|
-
name: op,
|
|
2814
|
-
description: desc,
|
|
2815
|
-
usage: escapeHtml(usageTemplate(singular, plural, method)),
|
|
2816
|
-
opKeys: mapArgs(field?.args),
|
|
2817
|
-
output: {
|
|
2818
|
-
type: field?.outputType.type,
|
|
2819
|
-
required: !field?.isNullable,
|
|
2820
|
-
list: field?.outputType.isList
|
|
2821
|
-
}
|
|
2822
|
-
};
|
|
2823
|
-
}).filter((op) => op !== null);
|
|
2824
|
-
};
|
|
2825
|
-
const getModels$1 = (dmmf) => dmmf.datamodel.models.map((model) => ({
|
|
2826
|
-
name: model.name,
|
|
2827
|
-
documentation: model.documentation,
|
|
2828
|
-
directives: getModelDirective(model),
|
|
2829
|
-
fields: getModelFields(model),
|
|
2830
|
-
operations: getModelOperations(model, dmmf.mappings.find((map) => map.model === model.name), dmmf.schema)
|
|
2831
|
-
}));
|
|
2832
|
-
const getModelData = (d) => ({ models: getModels$1(d) });
|
|
2833
|
-
const createModels = (d) => {
|
|
2834
|
-
const data = getModelData(d);
|
|
2835
|
-
return /* @__PURE__ */ jsx(ModelsSection, { data });
|
|
2836
|
-
};
|
|
2837
|
-
//#endregion
|
|
2838
|
-
//#region src/helper/docs/generator/toc.tsx
|
|
2839
|
-
const TOCSubHeader = ({ name }) => /* @__PURE__ */ jsx("div", {
|
|
2840
|
-
class: tocSubHeaderClass,
|
|
2841
|
-
children: /* @__PURE__ */ jsx("a", {
|
|
2842
|
-
href: `#model-${name}`,
|
|
2843
|
-
class: tocLinkClass,
|
|
2844
|
-
children: name
|
|
2845
|
-
})
|
|
2846
|
-
});
|
|
2847
|
-
const TOCSubField = ({ identifier, root, field }) => /* @__PURE__ */ jsx("li", { children: /* @__PURE__ */ jsx("a", {
|
|
2848
|
-
href: `#${identifier}-${root}-${field}`,
|
|
2849
|
-
class: tocLinkClass,
|
|
2850
|
-
children: field
|
|
2851
|
-
}) });
|
|
2852
|
-
const TOCModelItem = ({ model }) => /* @__PURE__ */ jsxs("li", {
|
|
2853
|
-
class: listItemClass,
|
|
2854
|
-
children: [
|
|
2855
|
-
/* @__PURE__ */ jsx(TOCSubHeader, { name: model.name }),
|
|
2856
|
-
/* @__PURE__ */ jsxs("div", {
|
|
2857
|
-
class: tocSubSectionClass,
|
|
2858
|
-
children: [/* @__PURE__ */ jsx("div", {
|
|
2859
|
-
class: tocSubSectionTitleClass,
|
|
2860
|
-
children: /* @__PURE__ */ jsx("a", {
|
|
2861
|
-
href: `#model-${model.name}-fields`,
|
|
2862
|
-
class: tocLinkClass,
|
|
2863
|
-
children: "Fields"
|
|
2864
|
-
})
|
|
2865
|
-
}), /* @__PURE__ */ jsx("ul", {
|
|
2866
|
-
class: tocListClass,
|
|
2867
|
-
children: model.fields.map((field) => /* @__PURE__ */ jsx(TOCSubField, {
|
|
2868
|
-
identifier: "model",
|
|
2869
|
-
root: model.name,
|
|
2870
|
-
field
|
|
2871
|
-
}))
|
|
2872
|
-
})]
|
|
2873
|
-
}),
|
|
2874
|
-
/* @__PURE__ */ jsxs("div", {
|
|
2875
|
-
class: tocSubSectionClass,
|
|
2876
|
-
children: [/* @__PURE__ */ jsx("div", {
|
|
2877
|
-
class: tocSubSectionTitleClass,
|
|
2878
|
-
children: /* @__PURE__ */ jsx("a", {
|
|
2879
|
-
href: `#model-${model.name}-operations`,
|
|
2880
|
-
class: tocLinkClass,
|
|
2881
|
-
children: "Operations"
|
|
2882
|
-
})
|
|
2883
|
-
}), /* @__PURE__ */ jsx("ul", {
|
|
2884
|
-
class: tocListClass,
|
|
2885
|
-
children: model.operations.map((op) => /* @__PURE__ */ jsx(TOCSubField, {
|
|
2886
|
-
identifier: "model",
|
|
2887
|
-
root: model.name,
|
|
2888
|
-
field: op
|
|
2889
|
-
}))
|
|
2890
|
-
})]
|
|
2891
|
-
})
|
|
2892
|
-
]
|
|
2893
|
-
});
|
|
2894
|
-
const TOCTypeSection = ({ title, href, types, kind }) => /* @__PURE__ */ jsxs("li", {
|
|
2895
|
-
class: listItemClass,
|
|
2896
|
-
children: [/* @__PURE__ */ jsx("div", {
|
|
2897
|
-
class: tocSubHeaderClass,
|
|
2898
|
-
children: /* @__PURE__ */ jsx("a", {
|
|
2899
|
-
href,
|
|
2900
|
-
class: tocLinkClass,
|
|
2901
|
-
children: title
|
|
2902
|
-
})
|
|
2903
|
-
}), /* @__PURE__ */ jsx("ul", {
|
|
2904
|
-
class: tocListClass,
|
|
2905
|
-
children: types.map((type) => /* @__PURE__ */ jsx(TOCSubField, {
|
|
2906
|
-
identifier: "type",
|
|
2907
|
-
root: kind,
|
|
2908
|
-
field: type
|
|
2909
|
-
}))
|
|
2910
|
-
})]
|
|
2911
|
-
});
|
|
2912
|
-
const TOCComponent = ({ data }) => /* @__PURE__ */ jsxs("div", { children: [
|
|
2913
|
-
/* @__PURE__ */ jsx("h5", {
|
|
2914
|
-
class: tocHeadingClass,
|
|
2915
|
-
children: /* @__PURE__ */ jsx("a", {
|
|
2916
|
-
href: "#models",
|
|
2917
|
-
class: tocLinkClass,
|
|
2918
|
-
children: "Models"
|
|
2919
|
-
})
|
|
2920
|
-
}),
|
|
2921
|
-
/* @__PURE__ */ jsx("ul", {
|
|
2922
|
-
class: tocSectionClass,
|
|
2923
|
-
children: data.models.map((model) => /* @__PURE__ */ jsx(TOCModelItem, { model }))
|
|
2924
|
-
}),
|
|
2925
|
-
/* @__PURE__ */ jsx("h5", {
|
|
2926
|
-
class: tocHeadingClass,
|
|
2927
|
-
children: /* @__PURE__ */ jsx("a", {
|
|
2928
|
-
href: "#types",
|
|
2929
|
-
class: tocLinkClass,
|
|
2930
|
-
children: "Types"
|
|
2931
|
-
})
|
|
2932
|
-
}),
|
|
2933
|
-
/* @__PURE__ */ jsxs("ul", {
|
|
2934
|
-
class: tocSectionClass,
|
|
2935
|
-
children: [/* @__PURE__ */ jsx(TOCTypeSection, {
|
|
2936
|
-
title: "Input Types",
|
|
2937
|
-
href: "#input-types",
|
|
2938
|
-
types: data.types.inputTypes,
|
|
2939
|
-
kind: "inputType"
|
|
2940
|
-
}), /* @__PURE__ */ jsx(TOCTypeSection, {
|
|
2941
|
-
title: "Output Types",
|
|
2942
|
-
href: "#output-types",
|
|
2943
|
-
types: data.types.outputTypes,
|
|
2944
|
-
kind: "outputType"
|
|
2945
|
-
})]
|
|
2946
|
-
})
|
|
2947
|
-
] });
|
|
2948
|
-
const getModels = (dmmfModel, mappings) => dmmfModel.map((model) => ({
|
|
2949
|
-
name: model.name,
|
|
2950
|
-
fields: model.fields.map((field) => field.name),
|
|
2951
|
-
operations: Object.keys(mappings.find((x) => x.model === model.name) ?? {}).filter((op) => op !== "model")
|
|
2952
|
-
}));
|
|
2953
|
-
const getTypes = (dmmfSchema) => {
|
|
2954
|
-
return {
|
|
2955
|
-
inputTypes: (dmmfSchema.inputObjectTypes.prisma ?? []).map((inputType) => inputType.name),
|
|
2956
|
-
outputTypes: [...dmmfSchema.outputObjectTypes.model.map((ot) => ot.name), ...dmmfSchema.outputObjectTypes.prisma.map((outputType) => outputType.name).filter((ot) => ot !== "Query" && ot !== "Mutation")]
|
|
2957
|
-
};
|
|
2958
|
-
};
|
|
2959
|
-
const getTOCData = (d) => ({
|
|
2960
|
-
models: getModels([...d.datamodel.models], d.mappings),
|
|
2961
|
-
types: getTypes(d.schema)
|
|
2962
|
-
});
|
|
2963
|
-
const createTOC = (d) => {
|
|
2964
|
-
const data = getTOCData(d);
|
|
2965
|
-
return /* @__PURE__ */ jsx(TOCComponent, { data });
|
|
2966
|
-
};
|
|
2967
|
-
//#endregion
|
|
2968
|
-
//#region src/helper/docs/printer/index.tsx
|
|
2969
|
-
const HekirekiLogo = () => /* @__PURE__ */ jsxs("svg", {
|
|
2970
|
-
width: "40",
|
|
2971
|
-
height: "40",
|
|
2972
|
-
viewBox: "0 0 40 40",
|
|
2973
|
-
fill: "currentColor",
|
|
2974
|
-
class: iconClass,
|
|
2975
|
-
children: [/* @__PURE__ */ jsx("circle", {
|
|
2976
|
-
cx: "20",
|
|
2977
|
-
cy: "20",
|
|
2978
|
-
r: "18",
|
|
2979
|
-
stroke: "currentColor",
|
|
2980
|
-
"stroke-width": "2",
|
|
2981
|
-
fill: "none"
|
|
2982
|
-
}), /* @__PURE__ */ jsx("path", {
|
|
2983
|
-
d: "M12 20 L20 12 L28 20 L20 28 Z",
|
|
2984
|
-
fill: "currentColor"
|
|
2985
|
-
})]
|
|
2986
|
-
});
|
|
2987
|
-
const DarkModeToggle = () => /* @__PURE__ */ jsxs("div", {
|
|
2988
|
-
class: darkModeToggleContainerClass,
|
|
2989
|
-
children: [
|
|
2990
|
-
/* @__PURE__ */ jsx("input", {
|
|
2991
|
-
type: "checkbox",
|
|
2992
|
-
id: "darkModeToggle"
|
|
2993
|
-
}),
|
|
2994
|
-
/* @__PURE__ */ jsx("label", {
|
|
2995
|
-
for: "darkModeToggle",
|
|
2996
|
-
class: darkModeToggleLabelClass,
|
|
2997
|
-
children: "Dark Mode"
|
|
2998
|
-
}),
|
|
2999
|
-
/* @__PURE__ */ jsx("script", { children: raw(`
|
|
3000
|
-
const darkModeToggle = document.getElementById('darkModeToggle');
|
|
3001
|
-
const isDarkModeStored = localStorage.getItem('isDarkMode') === 'true';
|
|
3002
|
-
darkModeToggle.checked = isDarkModeStored;
|
|
3003
|
-
darkModeToggle.addEventListener('change', function () {
|
|
3004
|
-
const isDark = this.checked;
|
|
3005
|
-
if (isDark) {
|
|
3006
|
-
document.documentElement.classList.add('dark');
|
|
3007
|
-
} else {
|
|
3008
|
-
document.documentElement.classList.remove('dark');
|
|
3009
|
-
}
|
|
3010
|
-
localStorage.setItem('isDarkMode', isDark);
|
|
3011
|
-
});
|
|
3012
|
-
`) })
|
|
3013
|
-
]
|
|
3014
|
-
});
|
|
3015
|
-
const Layout = ({ children }) => /* @__PURE__ */ jsxs("html", {
|
|
3016
|
-
lang: "en",
|
|
3017
|
-
class: globalCss,
|
|
3018
|
-
children: [/* @__PURE__ */ jsxs("head", { children: [
|
|
3019
|
-
/* @__PURE__ */ jsx("meta", { charset: "UTF-8" }),
|
|
3020
|
-
/* @__PURE__ */ jsx("meta", {
|
|
3021
|
-
name: "viewport",
|
|
3022
|
-
content: "width=device-width,initial-scale=1.0"
|
|
3023
|
-
}),
|
|
3024
|
-
/* @__PURE__ */ jsx("title", { children: "Hekireki Generated Docs" }),
|
|
3025
|
-
/* @__PURE__ */ jsx(Style, {}),
|
|
3026
|
-
/* @__PURE__ */ jsx("script", { children: raw(`
|
|
3027
|
-
const isDarkMode = localStorage.getItem('isDarkMode') === 'true';
|
|
3028
|
-
if (isDarkMode) document.documentElement.classList.add('dark');
|
|
3029
|
-
`) })
|
|
3030
|
-
] }), /* @__PURE__ */ jsx("body", { children })]
|
|
3031
|
-
});
|
|
3032
|
-
const Sidebar = ({ toc }) => /* @__PURE__ */ jsxs("div", {
|
|
3033
|
-
class: sidebarClass,
|
|
3034
|
-
children: [/* @__PURE__ */ jsxs("div", {
|
|
3035
|
-
class: headerClass,
|
|
3036
|
-
children: [/* @__PURE__ */ jsxs("div", {
|
|
3037
|
-
class: logoContainerClass,
|
|
3038
|
-
children: [/* @__PURE__ */ jsx(HekirekiLogo, {}), /* @__PURE__ */ jsx("span", {
|
|
3039
|
-
class: logoTextClass,
|
|
3040
|
-
children: "Hekireki Docs"
|
|
3041
|
-
})]
|
|
3042
|
-
}), /* @__PURE__ */ jsx(DarkModeToggle, {})]
|
|
3043
|
-
}), toc]
|
|
3044
|
-
});
|
|
3045
|
-
const MainContent = ({ models, types }) => /* @__PURE__ */ jsxs("div", {
|
|
3046
|
-
class: mainContentClass,
|
|
3047
|
-
children: [models, types]
|
|
3048
|
-
});
|
|
3049
|
-
const generateHTML = async (data) => {
|
|
3050
|
-
const element = /* @__PURE__ */ jsx(Layout, { children: /* @__PURE__ */ jsxs("div", {
|
|
3051
|
-
class: containerClass,
|
|
3052
|
-
children: [/* @__PURE__ */ jsx(Sidebar, { toc: createTOC(data) }), /* @__PURE__ */ jsx(MainContent, {
|
|
3053
|
-
models: createModels(data),
|
|
3054
|
-
types: createTypes(data)
|
|
3055
|
-
})]
|
|
3056
|
-
}) });
|
|
3057
|
-
const rendered = await Promise.resolve(element).then((node) => node.toString());
|
|
3058
|
-
return `<!DOCTYPE html>${await resolveCallback(rendered, HtmlEscapedCallbackPhase.Stringify, false, {})}`;
|
|
3059
|
-
};
|
|
3060
|
-
//#endregion
|
|
3061
|
-
//#region src/generator/docs.ts
|
|
3062
|
-
function docsHTML(dmmf) {
|
|
3063
|
-
return generateHTML(transformDMMF(dmmf));
|
|
1652
|
+
const emitted = models.filter((model) => hasPrimaryKey(model));
|
|
1653
|
+
const m2mTables = collectManyToManyTables$1(emitted, enums);
|
|
1654
|
+
const names = resolveNames(emitted, m2mTables);
|
|
1655
|
+
return `${[
|
|
1656
|
+
collectGlobalImports$1(emitted, idx).join("\n"),
|
|
1657
|
+
...collectDefaultHelpers(emitted, names, enums),
|
|
1658
|
+
...(enums ?? []).map((e) => generateEnumClass(e)),
|
|
1659
|
+
...emitted.map((model) => generateModelBody$1(model, models, idx, m2mTables, names)).filter((body) => body !== null),
|
|
1660
|
+
...m2mTables.map((t) => generateThroughModel(t))
|
|
1661
|
+
].join("\n\n\n")}\n`;
|
|
3064
1662
|
}
|
|
3065
1663
|
//#endregion
|
|
3066
|
-
//#region src/core/
|
|
3067
|
-
|
|
3068
|
-
|
|
3069
|
-
|
|
3070
|
-
|
|
3071
|
-
|
|
3072
|
-
|
|
3073
|
-
|
|
1664
|
+
//#region src/core/django.ts
|
|
1665
|
+
function django(options) {
|
|
1666
|
+
return Effect.gen(function* () {
|
|
1667
|
+
if (!(options.generator.isCustomOutput && options.generator.output?.value)) return yield* new GeneratorConfigError({ message: "output is required for Hekireki-Django. Please specify output in your generator config." });
|
|
1668
|
+
const output = options.generator.output.value;
|
|
1669
|
+
const outPath = path.extname(output) ? output : path.join(output, "models.py");
|
|
1670
|
+
const enums = options.dmmf.datamodel.enums;
|
|
1671
|
+
const indexes = options.dmmf.datamodel.indexes;
|
|
1672
|
+
const conflicts = findNameConflicts(options.dmmf.datamodel.models, enums);
|
|
1673
|
+
if (conflicts.length > 0) return yield* new GeneratorConfigError({ message: `Hekireki-Django cannot represent this schema:\n${conflicts.map((c) => ` - ${c}`).join("\n")}` });
|
|
1674
|
+
return yield* emitRaw(djangoCode(options.dmmf.datamodel.models, enums, indexes), path.dirname(outPath), outPath);
|
|
1675
|
+
});
|
|
3074
1676
|
}
|
|
3075
1677
|
//#endregion
|
|
3076
1678
|
//#region src/helper/drizzle.ts
|
|
@@ -3585,7 +2187,7 @@ function makeRelationField(field, model, models, relFields) {
|
|
|
3585
2187
|
}
|
|
3586
2188
|
return `${field.name}: one(${targetVar})`;
|
|
3587
2189
|
}
|
|
3588
|
-
function makeRelations(models, imports) {
|
|
2190
|
+
function makeRelations$1(models, imports) {
|
|
3589
2191
|
const modelsWithRels = models.filter((model) => model.fields.some((f) => f.kind === "object"));
|
|
3590
2192
|
if (modelsWithRels.length === 0) return [];
|
|
3591
2193
|
imports.orm.add("relations");
|
|
@@ -3605,7 +2207,7 @@ function drizzleSchema(datamodel, provider, indexes) {
|
|
|
3605
2207
|
const imports = createImports();
|
|
3606
2208
|
const enumLines = makeEnumDeclarations$1(datamodel.models, datamodel.enums, db, imports);
|
|
3607
2209
|
const tableLines = [...datamodel.models.map((model) => makeTable(model, datamodel.models, db, imports, datamodel.enums, indexes)), ...makeM2MJoinTables(datamodel.models, db, imports)];
|
|
3608
|
-
const relationsLines = [...makeRelations(datamodel.models, imports), ...makeM2MJoinRelations(datamodel.models, imports)];
|
|
2210
|
+
const relationsLines = [...makeRelations$1(datamodel.models, imports), ...makeM2MJoinRelations(datamodel.models, imports)];
|
|
3609
2211
|
const enumLinesWithGap = enumLines.flatMap((line, i) => i < enumLines.length - 1 ? [line, ""] : [line]);
|
|
3610
2212
|
const tableLinesWithGap = tableLines.flatMap((line, i) => i < tableLines.length - 1 ? [line, ""] : [line]);
|
|
3611
2213
|
const relationsLinesWithGap = relationsLines.flatMap((line, i) => i < relationsLines.length - 1 ? [line, ""] : [line]);
|
|
@@ -3629,22 +2231,22 @@ function parsePrismaProvider(raw) {
|
|
|
3629
2231
|
}
|
|
3630
2232
|
//#endregion
|
|
3631
2233
|
//#region src/core/drizzle.ts
|
|
3632
|
-
|
|
3633
|
-
|
|
3634
|
-
|
|
3635
|
-
|
|
3636
|
-
|
|
3637
|
-
|
|
3638
|
-
|
|
3639
|
-
|
|
3640
|
-
|
|
3641
|
-
|
|
3642
|
-
|
|
3643
|
-
|
|
3644
|
-
|
|
3645
|
-
|
|
3646
|
-
|
|
3647
|
-
|
|
2234
|
+
function drizzle(options) {
|
|
2235
|
+
return Effect.gen(function* () {
|
|
2236
|
+
if (!(options.generator.isCustomOutput && options.generator.output?.value)) return yield* new GeneratorConfigError({ message: "output is required for Hekireki-Drizzle. Please specify output in your generator config." });
|
|
2237
|
+
const provider = options.datasources[0]?.activeProvider ?? "postgresql";
|
|
2238
|
+
const providerResult = parsePrismaProvider(provider);
|
|
2239
|
+
if (!providerResult.ok) return yield* new GeneratorConfigError({ message: `Unsupported provider for Hekireki-Drizzle: ${provider}. Supported providers are postgresql, cockroachdb, mysql, and sqlite.` });
|
|
2240
|
+
const output = options.generator.output.value;
|
|
2241
|
+
const resolved = path.extname(output) ? {
|
|
2242
|
+
dir: path.dirname(output),
|
|
2243
|
+
file: output
|
|
2244
|
+
} : {
|
|
2245
|
+
dir: output,
|
|
2246
|
+
file: path.join(output, "schema.ts")
|
|
2247
|
+
};
|
|
2248
|
+
return yield* emit(drizzleSchema(options.dmmf.datamodel, providerResult.value, options.dmmf.datamodel.indexes), resolved.dir, resolved.file);
|
|
2249
|
+
});
|
|
3648
2250
|
}
|
|
3649
2251
|
//#endregion
|
|
3650
2252
|
//#region src/helper/ecto.ts
|
|
@@ -3982,15 +2584,14 @@ function ectoSchemaFiles(models, app, enums) {
|
|
|
3982
2584
|
}
|
|
3983
2585
|
//#endregion
|
|
3984
2586
|
//#region src/core/ecto.ts
|
|
3985
|
-
|
|
3986
|
-
|
|
3987
|
-
|
|
3988
|
-
|
|
3989
|
-
|
|
3990
|
-
|
|
3991
|
-
|
|
3992
|
-
|
|
3993
|
-
return emitMany(ectoSchemaFiles(options.dmmf.datamodel.models, app, enums), outDir);
|
|
2587
|
+
function ecto(options) {
|
|
2588
|
+
return Effect.gen(function* () {
|
|
2589
|
+
if (!(options.generator.isCustomOutput && options.generator.output?.value)) return yield* new GeneratorConfigError({ message: "output is required for Hekireki-Ecto. Please specify output in your generator config." });
|
|
2590
|
+
const outDir = options.generator.output.value;
|
|
2591
|
+
const app = options.generator.config?.app ?? "MyApp";
|
|
2592
|
+
const enums = options.dmmf.datamodel.enums;
|
|
2593
|
+
return yield* emitMany(ectoSchemaFiles(options.dmmf.datamodel.models, app, enums), outDir);
|
|
2594
|
+
});
|
|
3994
2595
|
}
|
|
3995
2596
|
//#endregion
|
|
3996
2597
|
//#region src/helper/effect.ts
|
|
@@ -4049,20 +2650,19 @@ function effectCode(dmmf, type, comment, relation) {
|
|
|
4049
2650
|
}
|
|
4050
2651
|
//#endregion
|
|
4051
2652
|
//#region src/core/effect.ts
|
|
4052
|
-
|
|
4053
|
-
|
|
4054
|
-
|
|
4055
|
-
|
|
4056
|
-
|
|
4057
|
-
|
|
4058
|
-
|
|
4059
|
-
|
|
4060
|
-
|
|
4061
|
-
|
|
4062
|
-
|
|
4063
|
-
|
|
4064
|
-
};
|
|
4065
|
-
return emit(effectCode(options.dmmf, getBool(options.generator.config?.type), getBool(options.generator.config?.comment), getBool(options.generator.config?.relation)), resolved.dir, resolved.file);
|
|
2653
|
+
function effect(options) {
|
|
2654
|
+
return Effect.gen(function* () {
|
|
2655
|
+
if (!(options.generator.isCustomOutput && options.generator.output?.value)) return yield* new GeneratorConfigError({ message: "output is required for Hekireki-Effect. Please specify output in your generator config." });
|
|
2656
|
+
const output = options.generator.output.value;
|
|
2657
|
+
const resolved = path.extname(output) ? {
|
|
2658
|
+
dir: path.dirname(output),
|
|
2659
|
+
file: output
|
|
2660
|
+
} : {
|
|
2661
|
+
dir: output,
|
|
2662
|
+
file: path.join(output, "index.ts")
|
|
2663
|
+
};
|
|
2664
|
+
return yield* emit(effectCode(options.dmmf, getBool(options.generator.config?.type), getBool(options.generator.config?.comment), getBool(options.generator.config?.relation)), resolved.dir, resolved.file);
|
|
2665
|
+
});
|
|
4066
2666
|
}
|
|
4067
2667
|
//#endregion
|
|
4068
2668
|
//#region src/helper/eloquent.ts
|
|
@@ -4287,31 +2887,1198 @@ function eloquentModels(models, namespace, allModels, enums) {
|
|
|
4287
2887
|
].join("\n");
|
|
4288
2888
|
}).filter(Boolean).join("\n\n");
|
|
4289
2889
|
}
|
|
4290
|
-
//#endregion
|
|
4291
|
-
//#region src/generator/eloquent.ts
|
|
4292
|
-
function eloquentModelFiles(models, namespace, enums) {
|
|
4293
|
-
const resolvedNamespace = (Array.isArray(namespace) ? namespace.join("\\") : namespace).replaceAll(".", "\\");
|
|
4294
|
-
const modelFiles = models.map((model) => ({
|
|
4295
|
-
fileName: `${model.name}.php`,
|
|
4296
|
-
code: eloquentModels([model], resolvedNamespace, models, enums)
|
|
2890
|
+
//#endregion
|
|
2891
|
+
//#region src/generator/eloquent.ts
|
|
2892
|
+
function eloquentModelFiles(models, namespace, enums) {
|
|
2893
|
+
const resolvedNamespace = (Array.isArray(namespace) ? namespace.join("\\") : namespace).replaceAll(".", "\\");
|
|
2894
|
+
const modelFiles = models.map((model) => ({
|
|
2895
|
+
fileName: `${model.name}.php`,
|
|
2896
|
+
code: eloquentModels([model], resolvedNamespace, models, enums)
|
|
2897
|
+
}));
|
|
2898
|
+
const enumFiles = (enums ?? []).map((enumDef) => ({
|
|
2899
|
+
fileName: `${enumDef.name}.php`,
|
|
2900
|
+
code: eloquentEnum(enumDef, resolvedNamespace)
|
|
2901
|
+
}));
|
|
2902
|
+
return [...modelFiles, ...enumFiles].filter((entry) => entry.code.trim().length > 0);
|
|
2903
|
+
}
|
|
2904
|
+
//#endregion
|
|
2905
|
+
//#region src/core/eloquent.ts
|
|
2906
|
+
function eloquent(options) {
|
|
2907
|
+
return Effect.gen(function* () {
|
|
2908
|
+
if (!(options.generator.isCustomOutput && options.generator.output?.value)) return yield* new GeneratorConfigError({ message: "output is required for Hekireki-Eloquent. Please specify output in your generator config." });
|
|
2909
|
+
const outDir = options.generator.output.value;
|
|
2910
|
+
const namespace = options.generator.config?.namespace ?? "App\\Models";
|
|
2911
|
+
const enums = options.dmmf.datamodel.enums;
|
|
2912
|
+
return yield* emitMany(eloquentModelFiles(options.dmmf.datamodel.models, namespace, enums), outDir);
|
|
2913
|
+
});
|
|
2914
|
+
}
|
|
2915
|
+
/** The fields a model node shows: everything but the relation fields. */
|
|
2916
|
+
function diagramFields(model) {
|
|
2917
|
+
return model.fields.filter((f) => f.kind !== "object");
|
|
2918
|
+
}
|
|
2919
|
+
function firstLine(text) {
|
|
2920
|
+
return text?.split("\n")[0]?.trim() ?? "";
|
|
2921
|
+
}
|
|
2922
|
+
const FIELD_NOTE = /^@(?:default\(|updatedAt|db\.)/u;
|
|
2923
|
+
/**
|
|
2924
|
+
* The faint second line of a field row: the attributes the drawing does not show another way,
|
|
2925
|
+
* then the prose of its doc comment. Empty when the field has neither.
|
|
2926
|
+
*/
|
|
2927
|
+
function fieldDetail(field) {
|
|
2928
|
+
const notes = (field.attributes ?? []).filter((attribute) => FIELD_NOTE.test(attribute));
|
|
2929
|
+
const documentation = firstLine(field.documentation);
|
|
2930
|
+
return [notes.join(" "), documentation].filter((part) => part !== "").join(" · ");
|
|
2931
|
+
}
|
|
2932
|
+
function fieldRowHeight(field) {
|
|
2933
|
+
return 22 + (fieldDetail(field) === "" ? 0 : 14);
|
|
2934
|
+
}
|
|
2935
|
+
/** The block attributes a model node lists under its fields, in declaration order. */
|
|
2936
|
+
function diagramConstraints(model) {
|
|
2937
|
+
return model.indexes ?? [];
|
|
2938
|
+
}
|
|
2939
|
+
function nodeHeight(model) {
|
|
2940
|
+
const constraints = diagramConstraints(model);
|
|
2941
|
+
return 44 + diagramFields(model).reduce((sum, field) => sum + fieldRowHeight(field), 0) + 8 + (constraints.length === 0 ? 0 : constraints.length * 20 + 8);
|
|
2942
|
+
}
|
|
2943
|
+
/** An enum card is the same shape as a model card, one row per member. */
|
|
2944
|
+
function enumHeight(value) {
|
|
2945
|
+
return 44 + value.values.length * 22 + 8;
|
|
2946
|
+
}
|
|
2947
|
+
const LayoutNodeSchema = v.pipe(v.object({
|
|
2948
|
+
x: v.pipe(v.number(), v.description("Left edge in canvas pixels")),
|
|
2949
|
+
y: v.pipe(v.number(), v.description("Top edge in canvas pixels")),
|
|
2950
|
+
width: v.pipe(v.number(), v.description("Node width in pixels")),
|
|
2951
|
+
height: v.pipe(v.number(), v.description("Node height in pixels"))
|
|
2952
|
+
}), v.description("A positioned node of the ER diagram"));
|
|
2953
|
+
/** The enums a model's fields hold, as edges from the model to the enum card. */
|
|
2954
|
+
function enumEdges(schema) {
|
|
2955
|
+
const names = new Set((schema.enums ?? []).map((value) => value.name));
|
|
2956
|
+
return schema.models.flatMap((model) => diagramFields(model).flatMap((field) => {
|
|
2957
|
+
const type = field.type ?? "";
|
|
2958
|
+
return field.kind === "enum" && names.has(type) ? [{
|
|
2959
|
+
from: model.name,
|
|
2960
|
+
to: type
|
|
2961
|
+
}] : [];
|
|
2962
|
+
}));
|
|
2963
|
+
}
|
|
2964
|
+
/** Places the blocks left to right along their relations, the way Studio lays a diagram out. */
|
|
2965
|
+
function autoLayout(schema) {
|
|
2966
|
+
const graph = new graphlib.Graph();
|
|
2967
|
+
graph.setGraph({
|
|
2968
|
+
rankdir: "LR",
|
|
2969
|
+
nodesep: 56,
|
|
2970
|
+
ranksep: 180,
|
|
2971
|
+
marginx: 40,
|
|
2972
|
+
marginy: 40
|
|
2973
|
+
});
|
|
2974
|
+
graph.setDefaultEdgeLabel(() => ({}));
|
|
2975
|
+
for (const model of schema.models) graph.setNode(model.name, {
|
|
2976
|
+
width: 340,
|
|
2977
|
+
height: nodeHeight(model)
|
|
2978
|
+
});
|
|
2979
|
+
for (const value of schema.enums ?? []) graph.setNode(value.name, {
|
|
2980
|
+
width: 280,
|
|
2981
|
+
height: enumHeight(value)
|
|
2982
|
+
});
|
|
2983
|
+
const names = /* @__PURE__ */ new Set([...schema.models.map((m) => m.name), ...(schema.enums ?? []).map((e) => e.name)]);
|
|
2984
|
+
const edges = [...schema.relations.map((relation) => ({
|
|
2985
|
+
from: relation.from.model,
|
|
2986
|
+
to: relation.to.model
|
|
2987
|
+
})), ...enumEdges(schema)];
|
|
2988
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2989
|
+
for (const edge of edges) {
|
|
2990
|
+
const key = `${edge.from}->${edge.to}`;
|
|
2991
|
+
if (edge.from === edge.to || !names.has(edge.from) || !names.has(edge.to) || seen.has(key)) continue;
|
|
2992
|
+
seen.add(key);
|
|
2993
|
+
graph.setEdge(edge.from, edge.to);
|
|
2994
|
+
}
|
|
2995
|
+
layout(graph);
|
|
2996
|
+
return Object.fromEntries([...names].map((name) => {
|
|
2997
|
+
const raw = graph.node(name);
|
|
2998
|
+
const node = v.safeParse(LayoutNodeSchema, raw);
|
|
2999
|
+
return [name, node.success ? {
|
|
3000
|
+
x: node.output.x - node.output.width / 2,
|
|
3001
|
+
y: node.output.y - node.output.height / 2
|
|
3002
|
+
} : {
|
|
3003
|
+
x: 0,
|
|
3004
|
+
y: 0
|
|
3005
|
+
}];
|
|
3006
|
+
}));
|
|
3007
|
+
}
|
|
3008
|
+
//#endregion
|
|
3009
|
+
//#region src/diagram/png.ts
|
|
3010
|
+
/** Rasterises an SVG document at the given scale (2 = a crisp image on high-density displays). */
|
|
3011
|
+
function svgToPng(svg, scale = 2) {
|
|
3012
|
+
return new Resvg(svg, {
|
|
3013
|
+
fitTo: {
|
|
3014
|
+
mode: "zoom",
|
|
3015
|
+
value: scale
|
|
3016
|
+
},
|
|
3017
|
+
font: {
|
|
3018
|
+
loadSystemFonts: true,
|
|
3019
|
+
monospaceFamily: "DejaVu Sans Mono",
|
|
3020
|
+
sansSerifFamily: "DejaVu Sans"
|
|
3021
|
+
}
|
|
3022
|
+
}).render().asPng();
|
|
3023
|
+
}
|
|
3024
|
+
const EDGE_BEND_RADIUS = 5;
|
|
3025
|
+
const EDGE_LABEL_FONT_SIZE = 9.5;
|
|
3026
|
+
const CAPTION_GAP = 6;
|
|
3027
|
+
const MONO_ADVANCE$1 = .6;
|
|
3028
|
+
/** Two decimals is as precise as a coordinate in the drawing ever needs to be. */
|
|
3029
|
+
function round(value) {
|
|
3030
|
+
return Math.round(value * 100) / 100;
|
|
3031
|
+
}
|
|
3032
|
+
function bend(a, b, c) {
|
|
3033
|
+
const size = Math.min(Math.hypot(a.x - b.x, a.y - b.y) / 2, Math.hypot(b.x - c.x, b.y - c.y) / 2, EDGE_BEND_RADIUS);
|
|
3034
|
+
const { x, y } = b;
|
|
3035
|
+
if (a.x === x && x === c.x || a.y === y && y === c.y) return `L${round(x)} ${round(y)}`;
|
|
3036
|
+
if (a.y === y) {
|
|
3037
|
+
const xDir = a.x < c.x ? -1 : 1;
|
|
3038
|
+
const yDir = a.y < c.y ? 1 : -1;
|
|
3039
|
+
return `L ${round(x + size * xDir)},${round(y)}Q ${round(x)},${round(y)} ${round(x)},${round(y + size * yDir)}`;
|
|
3040
|
+
}
|
|
3041
|
+
const xDir = a.x < c.x ? 1 : -1;
|
|
3042
|
+
const yDir = a.y < c.y ? -1 : 1;
|
|
3043
|
+
return `L ${round(x)},${round(y + size * yDir)}Q ${round(x)},${round(y)} ${round(x + size * xDir)},${round(y)}`;
|
|
3044
|
+
}
|
|
3045
|
+
/** The polyline through the points, with a rounded corner wherever it turns. */
|
|
3046
|
+
function polylinePath(points) {
|
|
3047
|
+
return points.map((point, index) => {
|
|
3048
|
+
const previous = points[index - 1];
|
|
3049
|
+
const next = points[index + 1];
|
|
3050
|
+
if (previous && next) return bend(previous, point, next);
|
|
3051
|
+
return `${index === 0 ? "M" : "L"}${round(point.x)} ${round(point.y)}`;
|
|
3052
|
+
}).join("");
|
|
3053
|
+
}
|
|
3054
|
+
/** An edge that crosses the gap in a channel down the canvas at `x`. */
|
|
3055
|
+
function throughChannel(source, target, x) {
|
|
3056
|
+
return [
|
|
3057
|
+
source,
|
|
3058
|
+
{
|
|
3059
|
+
x: source.x + 20,
|
|
3060
|
+
y: source.y
|
|
3061
|
+
},
|
|
3062
|
+
{
|
|
3063
|
+
x,
|
|
3064
|
+
y: source.y
|
|
3065
|
+
},
|
|
3066
|
+
{
|
|
3067
|
+
x,
|
|
3068
|
+
y: target.y
|
|
3069
|
+
},
|
|
3070
|
+
{
|
|
3071
|
+
x: target.x - 20,
|
|
3072
|
+
y: target.y
|
|
3073
|
+
},
|
|
3074
|
+
target
|
|
3075
|
+
];
|
|
3076
|
+
}
|
|
3077
|
+
/** An edge that leaves its models first and crosses along a lane across the canvas at `y`. */
|
|
3078
|
+
function alongLane(source, target, y) {
|
|
3079
|
+
return [
|
|
3080
|
+
source,
|
|
3081
|
+
{
|
|
3082
|
+
x: source.x + 20,
|
|
3083
|
+
y: source.y
|
|
3084
|
+
},
|
|
3085
|
+
{
|
|
3086
|
+
x: source.x + 20,
|
|
3087
|
+
y
|
|
3088
|
+
},
|
|
3089
|
+
{
|
|
3090
|
+
x: target.x - 20,
|
|
3091
|
+
y
|
|
3092
|
+
},
|
|
3093
|
+
{
|
|
3094
|
+
x: target.x - 20,
|
|
3095
|
+
y: target.y
|
|
3096
|
+
},
|
|
3097
|
+
target
|
|
3098
|
+
];
|
|
3099
|
+
}
|
|
3100
|
+
/** The corners of an edge from a source on the right of a node to a target on the left of another. */
|
|
3101
|
+
function smoothStepPoints(source, target) {
|
|
3102
|
+
const center = {
|
|
3103
|
+
x: (source.x + target.x) / 2,
|
|
3104
|
+
y: (source.y + target.y) / 2
|
|
3105
|
+
};
|
|
3106
|
+
return source.x + 20 < target.x - 20 ? throughChannel(source, target, center.x) : alongLane(source, target, center.y);
|
|
3107
|
+
}
|
|
3108
|
+
/** How long a stretch of an axis-aligned segment runs inside a box. */
|
|
3109
|
+
function insideBox(a, b, box) {
|
|
3110
|
+
const horizontal = a.y === b.y;
|
|
3111
|
+
const across = horizontal ? a.y : a.x;
|
|
3112
|
+
const [acrossFrom, acrossTo] = horizontal ? [box.y, box.y + box.height] : [box.x, box.x + box.width];
|
|
3113
|
+
if (across <= acrossFrom || across >= acrossTo) return 0;
|
|
3114
|
+
const [from, to] = horizontal ? [Math.min(a.x, b.x), Math.max(a.x, b.x)] : [Math.min(a.y, b.y), Math.max(a.y, b.y)];
|
|
3115
|
+
const [boxFrom, boxTo] = horizontal ? [box.x, box.x + box.width] : [box.y, box.y + box.height];
|
|
3116
|
+
return Math.max(0, Math.min(to, boxTo) - Math.max(from, boxFrom));
|
|
3117
|
+
}
|
|
3118
|
+
/** How much of a route disappears behind the cards, and how long the route is. */
|
|
3119
|
+
function routeCost(points, obstacles) {
|
|
3120
|
+
return points.slice(0, -1).reduce((cost, a, index) => {
|
|
3121
|
+
const b = points[index + 1] ?? a;
|
|
3122
|
+
return {
|
|
3123
|
+
hidden: cost.hidden + obstacles.reduce((sum, box) => sum + insideBox(a, b, box), 0),
|
|
3124
|
+
length: cost.length + Math.hypot(b.x - a.x, b.y - a.y)
|
|
3125
|
+
};
|
|
3126
|
+
}, {
|
|
3127
|
+
hidden: 0,
|
|
3128
|
+
length: 0
|
|
3129
|
+
});
|
|
3130
|
+
}
|
|
3131
|
+
const CLEARANCE = 20;
|
|
3132
|
+
const CARD_INSET = 2;
|
|
3133
|
+
/**
|
|
3134
|
+
* The corners of an edge, routed around the cards it would otherwise vanish behind: the plain
|
|
3135
|
+
* smoothstep is kept whenever it is already in the clear, and only a blocked one looks for
|
|
3136
|
+
* another channel down the canvas or another lane across it.
|
|
3137
|
+
*/
|
|
3138
|
+
function routePoints(source, target, cards) {
|
|
3139
|
+
const obstacles = cards.map((card) => ({
|
|
3140
|
+
x: card.x + CARD_INSET,
|
|
3141
|
+
y: card.y + CARD_INSET,
|
|
3142
|
+
width: card.width - 4,
|
|
3143
|
+
height: card.height - 4
|
|
3144
|
+
}));
|
|
3145
|
+
const direct = smoothStepPoints(source, target);
|
|
3146
|
+
if (routeCost(direct, obstacles).hidden === 0) return direct;
|
|
3147
|
+
const corridor = {
|
|
3148
|
+
x: Math.min(source.x, target.x) - 20,
|
|
3149
|
+
y: Math.min(source.y, target.y) - 20,
|
|
3150
|
+
width: Math.abs(target.x - source.x) + 40,
|
|
3151
|
+
height: Math.abs(target.y - source.y) + 40
|
|
3152
|
+
};
|
|
3153
|
+
const inTheWay = cards.filter((card) => card.x < corridor.x + corridor.width && corridor.x < card.x + card.width && card.y < corridor.y + corridor.height && corridor.y < card.y + card.height);
|
|
3154
|
+
const channels = inTheWay.flatMap((card) => [card.x - CLEARANCE, card.x + card.width + CLEARANCE]).filter((x) => x > source.x + 20 && x < target.x - 20);
|
|
3155
|
+
const lanes = inTheWay.flatMap((card) => [card.y - CLEARANCE, card.y + card.height + CLEARANCE]);
|
|
3156
|
+
return [
|
|
3157
|
+
direct,
|
|
3158
|
+
...channels.map((x) => throughChannel(source, target, x)),
|
|
3159
|
+
...lanes.map((y) => alongLane(source, target, y))
|
|
3160
|
+
].map((points) => ({
|
|
3161
|
+
points,
|
|
3162
|
+
cost: routeCost(points, obstacles)
|
|
3163
|
+
})).reduce((best, candidate) => candidate.cost.hidden < best.cost.hidden || candidate.cost.hidden === best.cost.hidden && candidate.cost.length < best.cost.length ? candidate : best).points;
|
|
3164
|
+
}
|
|
3165
|
+
function selfLoopPoints(source, target) {
|
|
3166
|
+
const flat = Math.abs(target.y - source.y) < 11;
|
|
3167
|
+
const end = {
|
|
3168
|
+
x: target.x,
|
|
3169
|
+
y: flat ? source.y + 22 : target.y
|
|
3170
|
+
};
|
|
3171
|
+
const turn = source.x + 34;
|
|
3172
|
+
return [
|
|
3173
|
+
source,
|
|
3174
|
+
{
|
|
3175
|
+
x: turn,
|
|
3176
|
+
y: source.y
|
|
3177
|
+
},
|
|
3178
|
+
{
|
|
3179
|
+
x: turn,
|
|
3180
|
+
y: end.y
|
|
3181
|
+
},
|
|
3182
|
+
end
|
|
3183
|
+
];
|
|
3184
|
+
}
|
|
3185
|
+
function captionWidth(caption) {
|
|
3186
|
+
return Math.max(...caption.map((line) => line.length * EDGE_LABEL_FONT_SIZE * MONO_ADVANCE$1)) + 10;
|
|
3187
|
+
}
|
|
3188
|
+
function captionBox(caption, center) {
|
|
3189
|
+
const width = captionWidth(caption);
|
|
3190
|
+
const height = caption.length * 12 + 4;
|
|
3191
|
+
return {
|
|
3192
|
+
x: center.x - width / 2,
|
|
3193
|
+
y: center.y - height / 2,
|
|
3194
|
+
width,
|
|
3195
|
+
height
|
|
3196
|
+
};
|
|
3197
|
+
}
|
|
3198
|
+
/** How much of two boxes cover each other, counting the breathing room around them. */
|
|
3199
|
+
function overlapArea(a, b, margin) {
|
|
3200
|
+
const width = Math.min(a.x + a.width, b.x + b.width + margin) - Math.max(a.x, b.x - margin);
|
|
3201
|
+
const height = Math.min(a.y + a.height, b.y + b.height + margin) - Math.max(a.y, b.y - margin);
|
|
3202
|
+
return width > 0 && height > 0 ? width * height : 0;
|
|
3203
|
+
}
|
|
3204
|
+
const ALONG_WIRE = 24;
|
|
3205
|
+
const BESIDE_WIRE = 12;
|
|
3206
|
+
function captionSpots(points, width) {
|
|
3207
|
+
const segments = points.slice(0, -1).map((a, index) => {
|
|
3208
|
+
const b = points[index + 1] ?? a;
|
|
3209
|
+
return {
|
|
3210
|
+
a,
|
|
3211
|
+
b,
|
|
3212
|
+
vertical: a.x === b.x,
|
|
3213
|
+
length: Math.hypot(b.x - a.x, b.y - a.y)
|
|
3214
|
+
};
|
|
3215
|
+
}).filter((segment) => segment.length > (segment.vertical ? BESIDE_WIRE : ALONG_WIRE)).toSorted((a, b) => a.vertical === b.vertical ? b.length - a.length : Number(b.vertical) - Number(a.vertical));
|
|
3216
|
+
const beside = width / 2 + CAPTION_GAP;
|
|
3217
|
+
return segments.flatMap((segment) => [
|
|
3218
|
+
.5,
|
|
3219
|
+
.3,
|
|
3220
|
+
.7,
|
|
3221
|
+
.15,
|
|
3222
|
+
.85
|
|
3223
|
+
].flatMap((t) => {
|
|
3224
|
+
const on = {
|
|
3225
|
+
x: segment.a.x + (segment.b.x - segment.a.x) * t,
|
|
3226
|
+
y: segment.a.y + (segment.b.y - segment.a.y) * t
|
|
3227
|
+
};
|
|
3228
|
+
return segment.vertical ? [
|
|
3229
|
+
{
|
|
3230
|
+
x: on.x + beside,
|
|
3231
|
+
y: on.y
|
|
3232
|
+
},
|
|
3233
|
+
{
|
|
3234
|
+
x: on.x - beside,
|
|
3235
|
+
y: on.y
|
|
3236
|
+
},
|
|
3237
|
+
on
|
|
3238
|
+
] : [on];
|
|
3239
|
+
}));
|
|
3240
|
+
}
|
|
3241
|
+
function midpoint(points) {
|
|
3242
|
+
return {
|
|
3243
|
+
x: ((points[0]?.x ?? 0) + (points.at(-1)?.x ?? 0)) / 2,
|
|
3244
|
+
y: ((points[0]?.y ?? 0) + (points.at(-1)?.y ?? 0)) / 2
|
|
3245
|
+
};
|
|
3246
|
+
}
|
|
3247
|
+
const CAPTION_CLASH = 4;
|
|
3248
|
+
const WIRE_CLASH = 5;
|
|
3249
|
+
/** The segments of every edge as thin boxes, so a caption can be scored against the wires. */
|
|
3250
|
+
function wireBoxes(edges) {
|
|
3251
|
+
return edges.flatMap((edge) => edge.points.slice(0, -1).map((a, index) => {
|
|
3252
|
+
const b = edge.points[index + 1] ?? a;
|
|
3253
|
+
return {
|
|
3254
|
+
x: Math.min(a.x, b.x) - 1,
|
|
3255
|
+
y: Math.min(a.y, b.y) - 1,
|
|
3256
|
+
width: Math.abs(b.x - a.x) + 2,
|
|
3257
|
+
height: Math.abs(b.y - a.y) + 2
|
|
3258
|
+
};
|
|
3259
|
+
}));
|
|
3260
|
+
}
|
|
3261
|
+
/**
|
|
3262
|
+
* Puts every caption on the clearest stretch of its edge: off the models, off the other captions
|
|
3263
|
+
* and off the wires, so a chip never hides the relation it names.
|
|
3264
|
+
*/
|
|
3265
|
+
/** The smallest box holding them all, grown by a margin. */
|
|
3266
|
+
function around(boxes, margin) {
|
|
3267
|
+
const x = Math.min(...boxes.map((box) => box.x)) - margin;
|
|
3268
|
+
const y = Math.min(...boxes.map((box) => box.y)) - margin;
|
|
3269
|
+
return {
|
|
3270
|
+
x,
|
|
3271
|
+
y,
|
|
3272
|
+
width: Math.max(...boxes.map((box) => box.x + box.width)) + margin - x,
|
|
3273
|
+
height: Math.max(...boxes.map((box) => box.y + box.height)) + margin - y
|
|
3274
|
+
};
|
|
3275
|
+
}
|
|
3276
|
+
function meets(a, b) {
|
|
3277
|
+
return a.x < b.x + b.width && b.x < a.x + a.width && a.y < b.y + b.height && b.y < a.y + a.height;
|
|
3278
|
+
}
|
|
3279
|
+
function placeCaptions(edges, cards) {
|
|
3280
|
+
const wires = wireBoxes(edges);
|
|
3281
|
+
return edges.reduce((state, edge) => {
|
|
3282
|
+
const { caption } = edge;
|
|
3283
|
+
if (caption.length === 0) return state;
|
|
3284
|
+
const boxes = captionSpots(edge.points, captionWidth(caption)).map((spot) => captionBox(caption, spot));
|
|
3285
|
+
const reach = around(boxes, 4);
|
|
3286
|
+
const near = cards.filter((card) => meets(reach, card));
|
|
3287
|
+
const crossed = wires.filter((wire) => meets(reach, wire));
|
|
3288
|
+
const taken = state.placed.filter((other) => meets(reach, other.box));
|
|
3289
|
+
const box = boxes.reduce((found, box) => {
|
|
3290
|
+
const cost = near.reduce((sum, card) => sum + overlapArea(box, card, 4), 0) + taken.reduce((sum, other) => sum + overlapArea(box, other.box, 2) * CAPTION_CLASH, 0) + crossed.reduce((sum, wire) => sum + overlapArea(box, wire, 0), 0) * WIRE_CLASH;
|
|
3291
|
+
return found === null || cost < found.cost ? {
|
|
3292
|
+
box,
|
|
3293
|
+
cost
|
|
3294
|
+
} : found;
|
|
3295
|
+
}, null)?.box ?? captionBox(caption, midpoint(edge.points));
|
|
3296
|
+
return { placed: [...state.placed, {
|
|
3297
|
+
edge,
|
|
3298
|
+
caption,
|
|
3299
|
+
box
|
|
3300
|
+
}] };
|
|
3301
|
+
}, { placed: [] }).placed;
|
|
3302
|
+
}
|
|
3303
|
+
//#endregion
|
|
3304
|
+
//#region src/diagram/svg.ts
|
|
3305
|
+
const PALETTES = {
|
|
3306
|
+
light: {
|
|
3307
|
+
canvas: "#f7f8fb",
|
|
3308
|
+
surface: "#ffffff",
|
|
3309
|
+
ink: "#16181f",
|
|
3310
|
+
muted: "#6b7084",
|
|
3311
|
+
faint: "#9a9fb3",
|
|
3312
|
+
lineStrong: "#cfd3e0",
|
|
3313
|
+
accent: "#4f46e5",
|
|
3314
|
+
node: "#1f2233",
|
|
3315
|
+
nodeText: "#f4f5fa",
|
|
3316
|
+
edge: "#9095ab",
|
|
3317
|
+
dots: "#d4d4dc",
|
|
3318
|
+
key: "#b45309",
|
|
3319
|
+
unique: "#0f766e",
|
|
3320
|
+
enumeration: "#7c3aed"
|
|
3321
|
+
},
|
|
3322
|
+
dark: {
|
|
3323
|
+
canvas: "#0f1117",
|
|
3324
|
+
surface: "#171a23",
|
|
3325
|
+
ink: "#e6e8ef",
|
|
3326
|
+
muted: "#9aa0b4",
|
|
3327
|
+
faint: "#6b7189",
|
|
3328
|
+
lineStrong: "#343a4a",
|
|
3329
|
+
accent: "#7c74ff",
|
|
3330
|
+
node: "#2a2f45",
|
|
3331
|
+
nodeText: "#f4f5fa",
|
|
3332
|
+
edge: "#6b7189",
|
|
3333
|
+
dots: "#2a2f3d",
|
|
3334
|
+
key: "#fbbf24",
|
|
3335
|
+
unique: "#5eead4",
|
|
3336
|
+
enumeration: "#c4b5fd"
|
|
3337
|
+
}
|
|
3338
|
+
};
|
|
3339
|
+
const FONT_MONO = "ui-monospace, SF Mono, Menlo, Consolas, Liberation Mono, DejaVu Sans Mono, monospace";
|
|
3340
|
+
const FONT_SANS = "ui-sans-serif, -apple-system, Segoe UI, Roboto, Helvetica Neue, Arial, DejaVu Sans, sans-serif";
|
|
3341
|
+
const MONO_ADVANCE = .6;
|
|
3342
|
+
const MONO_BOLD_ADVANCE = .66;
|
|
3343
|
+
const SANS_ADVANCE = .52;
|
|
3344
|
+
const NODE_RADIUS = 8;
|
|
3345
|
+
const HEADER_PADDING_X = 10;
|
|
3346
|
+
const ICON_SIZE = 11;
|
|
3347
|
+
const ROW_GAP = 6;
|
|
3348
|
+
const BADGE_HEIGHT = 12;
|
|
3349
|
+
const BADGE_FONT_SIZE = 8.5;
|
|
3350
|
+
const CONSTRAINT_FONT_SIZE = 10.5;
|
|
3351
|
+
const DOT_GAP = 20;
|
|
3352
|
+
const DOT_RADIUS = .7;
|
|
3353
|
+
const PADDING = 40;
|
|
3354
|
+
const CROW_FOOT = "M0 -8 L-12 0 L0 8 M-12 0 L0 0";
|
|
3355
|
+
const MAX_ONE_BAR = "M-6 -6 L-6 6";
|
|
3356
|
+
const MIN_ONE_BAR = "M-15 -6 L-15 6";
|
|
3357
|
+
const CARDINALITY_SYMBOLS = {
|
|
3358
|
+
one: {
|
|
3359
|
+
max: MAX_ONE_BAR,
|
|
3360
|
+
optional: false
|
|
3361
|
+
},
|
|
3362
|
+
"zero-one": {
|
|
3363
|
+
max: MAX_ONE_BAR,
|
|
3364
|
+
optional: true
|
|
3365
|
+
},
|
|
3366
|
+
many: {
|
|
3367
|
+
max: CROW_FOOT,
|
|
3368
|
+
optional: false
|
|
3369
|
+
},
|
|
3370
|
+
"zero-many": {
|
|
3371
|
+
max: CROW_FOOT,
|
|
3372
|
+
optional: true
|
|
3373
|
+
}
|
|
3374
|
+
};
|
|
3375
|
+
const KEY_ICON = "<circle cx=\"7.5\" cy=\"15.5\" r=\"4.5\"/><path d=\"m21 2-9.6 9.6\"/><path d=\"m15.5 7.5 3 3L22 7l-3-3\"/>";
|
|
3376
|
+
const LINK_ICON = "<path d=\"M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71\"/><path d=\"M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71\"/>";
|
|
3377
|
+
function escapeXml(text) {
|
|
3378
|
+
return text.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("\"", """);
|
|
3379
|
+
}
|
|
3380
|
+
function monoWidth(text, fontSize) {
|
|
3381
|
+
return text.length * fontSize * MONO_ADVANCE;
|
|
3382
|
+
}
|
|
3383
|
+
function sansWidth(text, fontSize) {
|
|
3384
|
+
return text.length * fontSize * SANS_ADVANCE;
|
|
3385
|
+
}
|
|
3386
|
+
/** Cuts a label to the width it may take, ending it with an ellipsis like `truncate` does. */
|
|
3387
|
+
function truncateLabel(text, maxWidth, advance) {
|
|
3388
|
+
const capacity = Math.floor(maxWidth / advance);
|
|
3389
|
+
if (text.length <= capacity) return text;
|
|
3390
|
+
return capacity <= 1 ? "…" : `${text.slice(0, capacity - 1)}…`;
|
|
3391
|
+
}
|
|
3392
|
+
function fieldTypeLabel(field) {
|
|
3393
|
+
return `${field.type}${field.isList ? "[]" : ""}${field.isRequired || field.isList ? "" : "?"}`;
|
|
3394
|
+
}
|
|
3395
|
+
function placeNodes(models, positions) {
|
|
3396
|
+
return models.map((model) => {
|
|
3397
|
+
const position = positions[model.name] ?? {
|
|
3398
|
+
x: 0,
|
|
3399
|
+
y: 0
|
|
3400
|
+
};
|
|
3401
|
+
return {
|
|
3402
|
+
model,
|
|
3403
|
+
fields: diagramFields(model),
|
|
3404
|
+
constraints: diagramConstraints(model),
|
|
3405
|
+
x: position.x,
|
|
3406
|
+
y: position.y,
|
|
3407
|
+
width: 340,
|
|
3408
|
+
height: nodeHeight(model)
|
|
3409
|
+
};
|
|
3410
|
+
});
|
|
3411
|
+
}
|
|
3412
|
+
function placeEnums(values, positions) {
|
|
3413
|
+
return values.map((value) => {
|
|
3414
|
+
const position = positions[value.name] ?? {
|
|
3415
|
+
x: 0,
|
|
3416
|
+
y: 0
|
|
3417
|
+
};
|
|
3418
|
+
return {
|
|
3419
|
+
value,
|
|
3420
|
+
x: position.x,
|
|
3421
|
+
y: position.y,
|
|
3422
|
+
width: 280,
|
|
3423
|
+
height: enumHeight(value)
|
|
3424
|
+
};
|
|
3425
|
+
});
|
|
3426
|
+
}
|
|
3427
|
+
/** Where the field rows of a card start. */
|
|
3428
|
+
function fieldsTop(card) {
|
|
3429
|
+
return card.y + 36 + 8;
|
|
3430
|
+
}
|
|
3431
|
+
/** The vertical centre of a field name, or of the header when the field is not shown. */
|
|
3432
|
+
function anchorY(node, field) {
|
|
3433
|
+
const index = field === null ? -1 : node.fields.findIndex((f) => f.name === field);
|
|
3434
|
+
if (index === -1) return node.y + 18;
|
|
3435
|
+
const above = node.fields.slice(0, index).reduce((sum, current) => sum + fieldRowHeight(current), 0);
|
|
3436
|
+
return fieldsTop(node) + above + 11;
|
|
3437
|
+
}
|
|
3438
|
+
function humanizeAction(action) {
|
|
3439
|
+
return action.replaceAll(/([A-Z])/gu, " $1").trim().toLowerCase();
|
|
3440
|
+
}
|
|
3441
|
+
/** Prisma's implicit relation name: both model names sorted and joined with `To`. */
|
|
3442
|
+
function defaultRelationName(a, b) {
|
|
3443
|
+
return [a, b].toSorted().join("To");
|
|
3444
|
+
}
|
|
3445
|
+
/** The `@relation("...")` name, when the schema gave the relation one of its own. */
|
|
3446
|
+
function customRelationName(relation) {
|
|
3447
|
+
const name = relation.name ?? null;
|
|
3448
|
+
if (name === null) return null;
|
|
3449
|
+
return name === defaultRelationName(relation.from.model, relation.to.model) ? null : name;
|
|
3450
|
+
}
|
|
3451
|
+
/** What the database does to the child rows, as words: `on delete set null`. */
|
|
3452
|
+
function referentialAction(event, action) {
|
|
3453
|
+
return action === null || action === void 0 ? null : `on ${event} ${humanizeAction(action)}`;
|
|
3454
|
+
}
|
|
3455
|
+
function isMany$1(cardinality) {
|
|
3456
|
+
return cardinality === "many" || cardinality === "zero-many";
|
|
3457
|
+
}
|
|
3458
|
+
/** The relationship the way it is spoken: one to one, one to many, many to many. */
|
|
3459
|
+
function relationshipKind(relation) {
|
|
3460
|
+
const from = isMany$1(relation.from.cardinality);
|
|
3461
|
+
const to = isMany$1(relation.to.cardinality);
|
|
3462
|
+
if (from && to) return "many to many";
|
|
3463
|
+
if (from) return "many to one";
|
|
3464
|
+
return to ? "one to many" : "one to one";
|
|
3465
|
+
}
|
|
3466
|
+
/**
|
|
3467
|
+
* What an edge says about itself, a line at a time: what the relation is — its name, when the
|
|
3468
|
+
* schema gave it one, and the relationship it stands for — and then what the database does to a
|
|
3469
|
+
* child row when the parent changes.
|
|
3470
|
+
*/
|
|
3471
|
+
function edgeCaption(relation) {
|
|
3472
|
+
const what = [customRelationName(relation), relationshipKind(relation)].filter((part) => part !== null).join(" · ");
|
|
3473
|
+
const rules = [referentialAction("delete", relation.onDelete), referentialAction("update", relation.onUpdate)].filter((part) => part !== null).join(" · ");
|
|
3474
|
+
return rules === "" ? [what] : [what, rules];
|
|
3475
|
+
}
|
|
3476
|
+
function fieldIcon(field, primaryKey, palette) {
|
|
3477
|
+
if (field.isId || primaryKey.has(field.name)) return {
|
|
3478
|
+
icon: KEY_ICON,
|
|
3479
|
+
color: palette.key
|
|
3480
|
+
};
|
|
3481
|
+
if (field.isForeignKey) return {
|
|
3482
|
+
icon: LINK_ICON,
|
|
3483
|
+
color: palette.accent
|
|
3484
|
+
};
|
|
3485
|
+
return null;
|
|
3486
|
+
}
|
|
3487
|
+
function renderIcon(icon, color, x, y, size) {
|
|
3488
|
+
return `<g transform="translate(${round(x)} ${round(y)}) scale(${round(size / 24)})" fill="none" stroke="${color}" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">${icon}</g>`;
|
|
3489
|
+
}
|
|
3490
|
+
/** A small rounded chip with a word in it, tinted with the colour of what it marks. */
|
|
3491
|
+
function renderBadge(label, x, centerY, color) {
|
|
3492
|
+
const width = sansWidth(label, BADGE_FONT_SIZE) + 8;
|
|
3493
|
+
return {
|
|
3494
|
+
svg: [`<rect x="${round(x)}" y="${round(centerY - BADGE_HEIGHT / 2)}" width="${round(width)}" height="${BADGE_HEIGHT}" rx="3" fill="${color}" fill-opacity="0.14"/>`, `<text x="${round(x + width / 2)}" y="${round(centerY + BADGE_FONT_SIZE * .36)}" font-family="${FONT_SANS}" font-size="${BADGE_FONT_SIZE}" font-weight="600" text-anchor="middle" fill="${color}">${escapeXml(label)}</text>`].join(""),
|
|
3495
|
+
width
|
|
3496
|
+
};
|
|
3497
|
+
}
|
|
3498
|
+
function badgeWidth(label) {
|
|
3499
|
+
return sansWidth(label, BADGE_FONT_SIZE) + 8;
|
|
3500
|
+
}
|
|
3501
|
+
/** The constraint a field carries on its own: `@unique`, on a field that is not the key already. */
|
|
3502
|
+
function uniqueBadge(field, primaryKey) {
|
|
3503
|
+
return field.isUnique === true && !(field.isId || primaryKey.has(field.name)) ? "UK" : null;
|
|
3504
|
+
}
|
|
3505
|
+
function renderRow(node, field, top, palette) {
|
|
3506
|
+
const primaryKey = new Set(node.model.primaryKey);
|
|
3507
|
+
const left = node.x + HEADER_PADDING_X;
|
|
3508
|
+
const right = node.x + node.width - HEADER_PADDING_X;
|
|
3509
|
+
const centerY = top + 11;
|
|
3510
|
+
const icon = fieldIcon(field, primaryKey, palette);
|
|
3511
|
+
const type = fieldTypeLabel(field);
|
|
3512
|
+
const typeWidth = monoWidth(type, 11);
|
|
3513
|
+
const badge = uniqueBadge(field, primaryKey);
|
|
3514
|
+
const badgeRoom = badge === null ? 0 : badgeWidth(badge) + ROW_GAP;
|
|
3515
|
+
const badgeSvg = badge === null ? "" : renderBadge(badge, right - typeWidth - ROW_GAP - badgeWidth(badge), centerY, palette.unique).svg;
|
|
3516
|
+
const nameLeft = left + ICON_SIZE + ROW_GAP;
|
|
3517
|
+
const name = truncateLabel(field.name, right - nameLeft - ROW_GAP - typeWidth - badgeRoom, 12 * MONO_ADVANCE);
|
|
3518
|
+
const detail = fieldDetail(field);
|
|
3519
|
+
const iconSvg = icon ? renderIcon(icon.icon, icon.color, left, centerY - ICON_SIZE / 2, ICON_SIZE) : "";
|
|
3520
|
+
const detailSvg = detail ? `<text x="${round(left + 17)}" y="${round(top + 26 + 3.78)}" font-family="${FONT_SANS}" font-size="10.5" fill="${palette.faint}">${escapeXml(truncateLabel(detail, right - left - 17, 10.5 * SANS_ADVANCE))}</text>` : "";
|
|
3521
|
+
return [
|
|
3522
|
+
iconSvg,
|
|
3523
|
+
badgeSvg,
|
|
3524
|
+
`<text x="${round(nameLeft)}" y="${round(centerY + 4.32)}" font-family="${FONT_MONO}" font-size="12" fill="${palette.ink}">${escapeXml(name)}</text>`,
|
|
3525
|
+
`<text x="${round(right)}" y="${round(centerY + 3.96)}" font-family="${FONT_MONO}" font-size="11" text-anchor="end" fill="${field.kind === "enum" ? palette.enumeration : palette.muted}">${escapeXml(type)}</text>`,
|
|
3526
|
+
detailSvg
|
|
3527
|
+
].join("");
|
|
3528
|
+
}
|
|
3529
|
+
const CONSTRAINT_STYLES = {
|
|
3530
|
+
id: {
|
|
3531
|
+
label: "KEY",
|
|
3532
|
+
color: (palette) => palette.key
|
|
3533
|
+
},
|
|
3534
|
+
unique: {
|
|
3535
|
+
label: "UNIQUE",
|
|
3536
|
+
color: (palette) => palette.unique
|
|
3537
|
+
},
|
|
3538
|
+
normal: {
|
|
3539
|
+
label: "INDEX",
|
|
3540
|
+
color: (palette) => palette.muted
|
|
3541
|
+
},
|
|
3542
|
+
fulltext: {
|
|
3543
|
+
label: "FULLTEXT",
|
|
3544
|
+
color: (palette) => palette.muted
|
|
3545
|
+
}
|
|
3546
|
+
};
|
|
3547
|
+
/** One `@@id` / `@@unique` / `@@index` of a model: what it is, and the columns it covers. */
|
|
3548
|
+
function renderConstraint(node, constraint, top, palette) {
|
|
3549
|
+
const style = CONSTRAINT_STYLES[constraint.type];
|
|
3550
|
+
const left = node.x + HEADER_PADDING_X;
|
|
3551
|
+
const right = node.x + node.width - HEADER_PADDING_X;
|
|
3552
|
+
const centerY = top + 10;
|
|
3553
|
+
const badge = renderBadge(style.label, left, centerY, style.color(palette));
|
|
3554
|
+
const columnsLeft = left + badge.width + ROW_GAP;
|
|
3555
|
+
const columns = truncateLabel(constraint.fields.join(", "), right - columnsLeft, CONSTRAINT_FONT_SIZE * MONO_ADVANCE);
|
|
3556
|
+
return `${badge.svg}<text x="${round(columnsLeft)}" y="${round(centerY + CONSTRAINT_FONT_SIZE * .36)}" font-family="${FONT_MONO}" font-size="${CONSTRAINT_FONT_SIZE}" fill="${palette.muted}">${escapeXml(columns)}</text>`;
|
|
3557
|
+
}
|
|
3558
|
+
/** The card behind a block: the rounded surface, its dark header band and the header text. */
|
|
3559
|
+
function renderCard(card, id, header, palette) {
|
|
3560
|
+
const nameWidth = header.name.length * 13 * MONO_BOLD_ADVANCE;
|
|
3561
|
+
const pillWidth = header.pill === null ? 0 : sansWidth(header.pill, 11) + 16;
|
|
3562
|
+
const pillX = card.x + card.width - HEADER_PADDING_X - pillWidth;
|
|
3563
|
+
const dbNameLeft = card.x + HEADER_PADDING_X + nameWidth + 8;
|
|
3564
|
+
const dbName = header.dbName ? truncateLabel(header.dbName, pillX - 8 - dbNameLeft, 11 * MONO_ADVANCE) : "";
|
|
3565
|
+
const headerCenter = card.y + 18;
|
|
3566
|
+
return [
|
|
3567
|
+
`<clipPath id="${id}"><rect x="${round(card.x)}" y="${round(card.y)}" width="${round(card.width)}" height="${round(card.height)}" rx="${NODE_RADIUS}"/></clipPath>`,
|
|
3568
|
+
`<rect x="${round(card.x)}" y="${round(card.y)}" width="${round(card.width)}" height="${round(card.height)}" rx="${NODE_RADIUS}" fill="${palette.surface}" stroke="${palette.lineStrong}" filter="url(#node-shadow)"/>`,
|
|
3569
|
+
`<rect x="${round(card.x)}" y="${round(card.y)}" width="${round(card.width)}" height="36" fill="${palette.node}" clip-path="url(#${id})"/>`,
|
|
3570
|
+
`<text x="${round(card.x + HEADER_PADDING_X)}" y="${round(headerCenter + 4.68)}" font-family="${FONT_MONO}" font-size="13" font-weight="700" fill="${palette.nodeText}">${escapeXml(header.name)}${dbName ? `<tspan dx="8" font-size="11" font-weight="400" opacity="0.6">${escapeXml(dbName)}</tspan>` : ""}</text>`,
|
|
3571
|
+
...header.pill === null ? [] : [`<rect x="${round(pillX)}" y="${round(headerCenter - 8)}" width="${round(pillWidth)}" height="16" rx="8" fill="${palette.surface}" opacity="0.15"/>`, `<text x="${round(pillX + pillWidth / 2)}" y="${round(headerCenter + 3.96)}" font-family="${FONT_SANS}" font-size="11" text-anchor="middle" fill="${palette.nodeText}">${escapeXml(header.pill)}</text>`]
|
|
3572
|
+
].join("");
|
|
3573
|
+
}
|
|
3574
|
+
function renderNode(node, index, palette) {
|
|
3575
|
+
const { model, fields } = node;
|
|
3576
|
+
const rows = fields.reduce((state, field) => ({
|
|
3577
|
+
top: state.top + fieldRowHeight(field),
|
|
3578
|
+
svg: [...state.svg, renderRow(node, field, state.top, palette)]
|
|
3579
|
+
}), {
|
|
3580
|
+
top: fieldsTop(node),
|
|
3581
|
+
svg: []
|
|
3582
|
+
});
|
|
3583
|
+
return [
|
|
3584
|
+
`<g class="model-node">`,
|
|
3585
|
+
renderCard(node, `node-clip-${index}`, {
|
|
3586
|
+
name: model.name,
|
|
3587
|
+
dbName: model.dbName,
|
|
3588
|
+
pill: null
|
|
3589
|
+
}, palette),
|
|
3590
|
+
...rows.svg,
|
|
3591
|
+
...node.constraints.length === 0 ? [] : [`<path d="M${round(node.x)} ${round(rows.top + 8)}H${round(node.x + node.width)}" stroke="${palette.lineStrong}" stroke-width="1"/>`, ...node.constraints.map((constraint, position) => renderConstraint(node, constraint, rows.top + 8 + position * 20, palette))],
|
|
3592
|
+
`</g>`
|
|
3593
|
+
].join("");
|
|
3594
|
+
}
|
|
3595
|
+
/** An enum card: its members, with the value the database stores when `@map` renames one. */
|
|
3596
|
+
function renderEnum(card, index, palette) {
|
|
3597
|
+
const { value } = card;
|
|
3598
|
+
const left = card.x + HEADER_PADDING_X;
|
|
3599
|
+
const right = card.x + card.width - HEADER_PADDING_X;
|
|
3600
|
+
const top = fieldsTop(card);
|
|
3601
|
+
return [
|
|
3602
|
+
`<g class="enum-node">`,
|
|
3603
|
+
renderCard(card, `enum-clip-${index}`, {
|
|
3604
|
+
name: value.name,
|
|
3605
|
+
dbName: value.dbName,
|
|
3606
|
+
pill: "enum"
|
|
3607
|
+
}, palette),
|
|
3608
|
+
...value.values.map((member, position) => {
|
|
3609
|
+
const centerY = top + position * 22 + 11;
|
|
3610
|
+
const stored = member.dbName ? `<text x="${round(right)}" y="${round(centerY + 3.96)}" font-family="${FONT_MONO}" font-size="11" text-anchor="end" fill="${palette.faint}">${escapeXml(member.dbName)}</text>` : "";
|
|
3611
|
+
return `<text x="${round(left)}" y="${round(centerY + 4.32)}" font-family="${FONT_MONO}" font-size="12" fill="${palette.enumeration}">${escapeXml(truncateLabel(member.name, right - left, 12 * MONO_ADVANCE))}</text>${stored}`;
|
|
3612
|
+
}),
|
|
3613
|
+
`</g>`
|
|
3614
|
+
].join("");
|
|
3615
|
+
}
|
|
3616
|
+
function hasField(node, field) {
|
|
3617
|
+
return node.fields.some((f) => f.name === field);
|
|
3618
|
+
}
|
|
3619
|
+
/** One end of an edge in IE notation, drawn towards the node the end touches. */
|
|
3620
|
+
function renderCardinality(cardinality, point, towards, palette) {
|
|
3621
|
+
const { max, optional } = CARDINALITY_SYMBOLS[cardinality];
|
|
3622
|
+
const flip = towards === "left" ? " scale(-1 1)" : "";
|
|
3623
|
+
const minimum = optional ? `<circle cx="-16" cy="0" r="3.2" fill="${palette.surface}"/>` : `<path d="${MIN_ONE_BAR}"/>`;
|
|
3624
|
+
return `<g transform="translate(${round(point.x)} ${round(point.y)})${flip}" fill="none" stroke="${palette.edge}" stroke-width="1.4"><path d="${max}"/>${minimum}</g>`;
|
|
3625
|
+
}
|
|
3626
|
+
/** Where an edge leaves and enters its models, and what it says along the way. */
|
|
3627
|
+
function edgeGeometry(relation, nodes, cards) {
|
|
3628
|
+
const from = nodes.get(relation.from.model);
|
|
3629
|
+
const to = nodes.get(relation.to.model);
|
|
3630
|
+
if (!(from && to)) return null;
|
|
3631
|
+
const useHeader = relation.origin === "implicit-many-to-many" || !hasField(from, relation.from.field) || !hasField(to, relation.to.field);
|
|
3632
|
+
const source = {
|
|
3633
|
+
x: from.x + from.width,
|
|
3634
|
+
y: anchorY(from, useHeader ? null : relation.from.field)
|
|
3635
|
+
};
|
|
3636
|
+
const loops = relation.from.model === relation.to.model;
|
|
3637
|
+
const target = {
|
|
3638
|
+
x: loops ? to.x + to.width : to.x,
|
|
3639
|
+
y: anchorY(to, useHeader ? null : relation.to.field)
|
|
3640
|
+
};
|
|
3641
|
+
return {
|
|
3642
|
+
relation,
|
|
3643
|
+
points: loops ? selfLoopPoints(source, target) : routePoints(source, target, cards),
|
|
3644
|
+
caption: edgeCaption(relation),
|
|
3645
|
+
dashed: relation.origin === "annotated" || relation.origin === "implicit-many-to-many",
|
|
3646
|
+
targetTowards: loops ? "left" : "right"
|
|
3647
|
+
};
|
|
3648
|
+
}
|
|
3649
|
+
function renderEdge(edge, palette) {
|
|
3650
|
+
const source = edge.points[0];
|
|
3651
|
+
const target = edge.points.at(-1);
|
|
3652
|
+
if (!(source && target)) return "";
|
|
3653
|
+
return [
|
|
3654
|
+
`<g class="relation-edge">`,
|
|
3655
|
+
`<path d="${polylinePath(edge.points)}" fill="none" stroke="${palette.edge}" stroke-width="1.4"${edge.dashed ? " stroke-dasharray=\"5 4\"" : ""}/>`,
|
|
3656
|
+
renderCardinality(edge.relation.from.cardinality, source, "left", palette),
|
|
3657
|
+
renderCardinality(edge.relation.to.cardinality, target, edge.targetTowards, palette),
|
|
3658
|
+
`</g>`
|
|
3659
|
+
].join("");
|
|
3660
|
+
}
|
|
3661
|
+
/** The dotted links from every enum-typed field to the card that lists its values. */
|
|
3662
|
+
function enumLinks(nodes, enums, cards) {
|
|
3663
|
+
return nodes.flatMap((node) => node.fields.flatMap((field) => {
|
|
3664
|
+
const card = enums.find((candidate) => candidate.value.name === field.type);
|
|
3665
|
+
if (field.kind !== "enum" || card === void 0) return [];
|
|
3666
|
+
return [routePoints({
|
|
3667
|
+
x: node.x + node.width,
|
|
3668
|
+
y: anchorY(node, field.name)
|
|
3669
|
+
}, {
|
|
3670
|
+
x: card.x,
|
|
3671
|
+
y: card.y + 18
|
|
3672
|
+
}, cards)];
|
|
3673
|
+
}));
|
|
3674
|
+
}
|
|
3675
|
+
function renderEnumLink(points, palette) {
|
|
3676
|
+
return `<g class="enum-edge"><path d="${polylinePath(points)}" fill="none" stroke="${palette.enumeration}" stroke-width="1.2" stroke-dasharray="2 4" opacity="0.75"/></g>`;
|
|
3677
|
+
}
|
|
3678
|
+
function renderCaption(caption, box, palette) {
|
|
3679
|
+
const center = box.x + box.width / 2;
|
|
3680
|
+
const lines = caption.map((line, index) => {
|
|
3681
|
+
const baseline = box.y + 2 + (index + .5) * 12 + EDGE_LABEL_FONT_SIZE * .36;
|
|
3682
|
+
return `<text x="${round(center)}" y="${round(baseline)}" font-family="${FONT_MONO}" font-size="${EDGE_LABEL_FONT_SIZE}" text-anchor="middle" fill="${index === 0 ? palette.muted : palette.faint}">${escapeXml(line)}</text>`;
|
|
3683
|
+
});
|
|
3684
|
+
return `<g class="relation-label"><rect x="${round(box.x)}" y="${round(box.y)}" width="${round(box.width)}" height="${round(box.height)}" rx="3" fill="${palette.surface}" stroke="${palette.lineStrong}" stroke-width="0.8"/>${lines.join("")}</g>`;
|
|
3685
|
+
}
|
|
3686
|
+
function union(boxes) {
|
|
3687
|
+
const minX = Math.min(...boxes.map((box) => box.x));
|
|
3688
|
+
const minY = Math.min(...boxes.map((box) => box.y));
|
|
3689
|
+
const maxX = Math.max(...boxes.map((box) => box.x + box.width));
|
|
3690
|
+
const maxY = Math.max(...boxes.map((box) => box.y + box.height));
|
|
3691
|
+
return {
|
|
3692
|
+
x: minX,
|
|
3693
|
+
y: minY,
|
|
3694
|
+
width: maxX - minX,
|
|
3695
|
+
height: maxY - minY
|
|
3696
|
+
};
|
|
3697
|
+
}
|
|
3698
|
+
function pad(box, padding) {
|
|
3699
|
+
return {
|
|
3700
|
+
x: box.x - padding,
|
|
3701
|
+
y: box.y - padding,
|
|
3702
|
+
width: box.width + padding * 2,
|
|
3703
|
+
height: box.height + padding * 2
|
|
3704
|
+
};
|
|
3705
|
+
}
|
|
3706
|
+
/**
|
|
3707
|
+
* Draws the ER diagram as Studio shows it: one node per model with its scalar fields, a
|
|
3708
|
+
* smoothstep edge per relation in crow's-foot notation, and a key to the symbols. The result is a
|
|
3709
|
+
* standalone SVG document with the canvas behind it.
|
|
3710
|
+
*/
|
|
3711
|
+
function renderDiagramSvg(input) {
|
|
3712
|
+
const palette = PALETTES[input.theme ?? "light"];
|
|
3713
|
+
const nodes = placeNodes(input.models, input.positions);
|
|
3714
|
+
const enums = placeEnums(input.enums ?? [], input.positions);
|
|
3715
|
+
const byName = new Map(nodes.map((node) => [node.model.name, node]));
|
|
3716
|
+
const cards = [...nodes, ...enums];
|
|
3717
|
+
const edges = input.relations.map((relation) => edgeGeometry(relation, byName, cards)).filter((edge) => edge !== null);
|
|
3718
|
+
const links = enumLinks(nodes, enums, cards);
|
|
3719
|
+
const captions = placeCaptions([...edges, ...links.map((points) => ({
|
|
3720
|
+
caption: [],
|
|
3721
|
+
points
|
|
3722
|
+
}))], cards);
|
|
3723
|
+
const ends = [...edges.map((edge) => edge.points), ...links].flatMap((points) => points.map((point) => ({
|
|
3724
|
+
x: point.x,
|
|
3725
|
+
y: point.y,
|
|
3726
|
+
width: 0,
|
|
3727
|
+
height: 0
|
|
3728
|
+
})));
|
|
3729
|
+
const bounds = pad(union([
|
|
3730
|
+
...cards.length === 0 ? [{
|
|
3731
|
+
x: 0,
|
|
3732
|
+
y: 0,
|
|
3733
|
+
width: 0,
|
|
3734
|
+
height: 0
|
|
3735
|
+
}] : cards,
|
|
3736
|
+
...ends.map((end) => pad(end, 20)),
|
|
3737
|
+
...captions.map((caption) => caption.box)
|
|
3738
|
+
]), PADDING);
|
|
3739
|
+
return [
|
|
3740
|
+
`<svg xmlns="http://www.w3.org/2000/svg" width="${round(bounds.width)}" height="${round(bounds.height)}" viewBox="${round(bounds.x)} ${round(bounds.y)} ${round(bounds.width)} ${round(bounds.height)}">`,
|
|
3741
|
+
`<defs>`,
|
|
3742
|
+
`<pattern id="dots" x="${round(bounds.x)}" y="${round(bounds.y)}" width="${DOT_GAP}" height="${DOT_GAP}" patternUnits="userSpaceOnUse"><circle cx="${DOT_RADIUS}" cy="${DOT_RADIUS}" r="${DOT_RADIUS}" fill="${palette.dots}"/></pattern>`,
|
|
3743
|
+
`<filter id="node-shadow" x="-5%" y="-5%" width="110%" height="115%"><feDropShadow dx="0" dy="1" stdDeviation="1" flood-color="#000000" flood-opacity="0.08"/></filter>`,
|
|
3744
|
+
`</defs>`,
|
|
3745
|
+
`<rect x="${round(bounds.x)}" y="${round(bounds.y)}" width="${round(bounds.width)}" height="${round(bounds.height)}" fill="${palette.canvas}"/>`,
|
|
3746
|
+
`<rect x="${round(bounds.x)}" y="${round(bounds.y)}" width="${round(bounds.width)}" height="${round(bounds.height)}" fill="url(#dots)"/>`,
|
|
3747
|
+
...links.map((points) => renderEnumLink(points, palette)),
|
|
3748
|
+
...edges.map((edge) => renderEdge(edge, palette)),
|
|
3749
|
+
...nodes.map((node, index) => renderNode(node, index, palette)),
|
|
3750
|
+
...enums.map((card, index) => renderEnum(card, index, palette)),
|
|
3751
|
+
...captions.map((caption) => renderCaption(caption.caption, caption.box, palette)),
|
|
3752
|
+
`</svg>`
|
|
3753
|
+
].filter((line) => line !== "").join("\n");
|
|
3754
|
+
}
|
|
3755
|
+
//#endregion
|
|
3756
|
+
//#region src/helper/dbml.ts
|
|
3757
|
+
function escapeNote(str) {
|
|
3758
|
+
return str.replaceAll("'", "\\'");
|
|
3759
|
+
}
|
|
3760
|
+
function formatConstraints(constraints) {
|
|
3761
|
+
return constraints.length > 0 ? ` [${constraints.join(", ")}]` : "";
|
|
3762
|
+
}
|
|
3763
|
+
function makeEnum(enumDef) {
|
|
3764
|
+
return [
|
|
3765
|
+
`Enum ${enumDef.name} {`,
|
|
3766
|
+
...enumDef.values.map((v) => ` ${v}`),
|
|
3767
|
+
"}"
|
|
3768
|
+
].join("\n");
|
|
3769
|
+
}
|
|
3770
|
+
function makeRefName(ref) {
|
|
3771
|
+
return ref.name ?? `${ref.fromTable}_${ref.fromColumn}_${ref.toTable}_${ref.toColumn}_fk`;
|
|
3772
|
+
}
|
|
3773
|
+
function combineKeys(keys) {
|
|
3774
|
+
return keys.length > 1 ? `(${keys.join(", ")})` : keys[0];
|
|
3775
|
+
}
|
|
3776
|
+
function escapeTriple(str) {
|
|
3777
|
+
return str.replaceAll("\\", "\\\\").replaceAll("'", "\\'");
|
|
3778
|
+
}
|
|
3779
|
+
function noteLiteral(value) {
|
|
3780
|
+
return value.includes("\n") ? `'''${escapeTriple(value)}'''` : `'${escapeNote(value)}'`;
|
|
3781
|
+
}
|
|
3782
|
+
function makeIndex(index) {
|
|
3783
|
+
return ` ${index.columns.length > 1 ? `(${index.columns.join(", ")})` : index.columns[0]}${formatConstraints([
|
|
3784
|
+
index.isPrimaryKey && "pk",
|
|
3785
|
+
index.isUnique && "unique",
|
|
3786
|
+
index.name && `name: '${index.name}'`
|
|
3787
|
+
].filter((c) => Boolean(c)))}`;
|
|
3788
|
+
}
|
|
3789
|
+
function toDbmlAction(action) {
|
|
3790
|
+
if (action === "Cascade") return "cascade";
|
|
3791
|
+
if (action === "SetNull") return "set null";
|
|
3792
|
+
if (action === "SetDefault") return "set default";
|
|
3793
|
+
if (action === "NoAction") return "no action";
|
|
3794
|
+
if (action === "Restrict") return "restrict";
|
|
3795
|
+
return action;
|
|
3796
|
+
}
|
|
3797
|
+
function makeRef(ref) {
|
|
3798
|
+
const name = makeRefName(ref);
|
|
3799
|
+
const operator = ref.type ?? ">";
|
|
3800
|
+
const actions = [ref.onDelete && `delete: ${toDbmlAction(ref.onDelete)}`, ref.onUpdate && `update: ${toDbmlAction(ref.onUpdate)}`].filter((a) => Boolean(a));
|
|
3801
|
+
const actionStr = actions.length > 0 ? ` [${actions.join(", ")}]` : "";
|
|
3802
|
+
return `Ref ${name}: ${ref.fromTable}.${ref.fromColumn} ${operator} ${ref.toTable}.${ref.toColumn}${actionStr}`;
|
|
3803
|
+
}
|
|
3804
|
+
function makePrismaColumn(column) {
|
|
3805
|
+
const constraints = [
|
|
3806
|
+
column.isPrimaryKey && "pk",
|
|
3807
|
+
column.isIncrement && "increment",
|
|
3808
|
+
column.defaultValue !== void 0 && `default: ${column.defaultValue}`,
|
|
3809
|
+
column.isUnique && "unique",
|
|
3810
|
+
column.isNotNull && "not null",
|
|
3811
|
+
column.note && `note: ${noteLiteral(column.note)}`
|
|
3812
|
+
].filter((c) => Boolean(c));
|
|
3813
|
+
return ` ${column.name} ${column.type}${formatConstraints(constraints)}`;
|
|
3814
|
+
}
|
|
3815
|
+
function isFunctionDefault$4(def) {
|
|
3816
|
+
return def !== null && typeof def === "object" && "name" in def;
|
|
3817
|
+
}
|
|
3818
|
+
function toDBMLColumn(field, models, mapToDbSchema) {
|
|
3819
|
+
const defaultName = isFunctionDefault$4(field.default) ? field.default.name : void 0;
|
|
3820
|
+
const baseType = mapToDbSchema ? models.find((m) => m.name === field.type)?.dbName ?? field.type : field.type;
|
|
3821
|
+
const type = field.isList && !field.relationName ? `${baseType}[]` : baseType;
|
|
3822
|
+
const defaultValue = (() => {
|
|
3823
|
+
if (defaultName === "autoincrement") return void 0;
|
|
3824
|
+
if (defaultName === "now") return "`now()`";
|
|
3825
|
+
if (field.hasDefaultValue && typeof field.default !== "object") return field.type === "String" || field.type === "Json" || field.kind === "enum" ? `'${field.default}'` : String(field.default);
|
|
3826
|
+
})();
|
|
3827
|
+
return {
|
|
3828
|
+
name: field.name,
|
|
3829
|
+
type,
|
|
3830
|
+
isPrimaryKey: field.isId,
|
|
3831
|
+
isIncrement: defaultName === "autoincrement",
|
|
3832
|
+
isUnique: field.isUnique,
|
|
3833
|
+
isNotNull: field.isRequired && !field.isId,
|
|
3834
|
+
defaultValue,
|
|
3835
|
+
note: stripAnnotations(field.documentation)
|
|
3836
|
+
};
|
|
3837
|
+
}
|
|
3838
|
+
function makeTables(models, mapToDbSchema = false) {
|
|
3839
|
+
return models.map((model) => {
|
|
3840
|
+
const modelName = mapToDbSchema && model.dbName ? model.dbName : model.name;
|
|
3841
|
+
const columnLines = model.fields.filter((field) => !field.relationName).map((field) => toDBMLColumn(field, models, mapToDbSchema)).map(makePrismaColumn).join("\n");
|
|
3842
|
+
const indexes = [...model.primaryKey?.fields && model.primaryKey.fields.length > 0 ? [{
|
|
3843
|
+
columns: model.primaryKey.fields,
|
|
3844
|
+
isPrimaryKey: true
|
|
3845
|
+
}] : [], ...model.uniqueFields.filter((c) => c.length > 1).map((c) => ({
|
|
3846
|
+
columns: c,
|
|
3847
|
+
isUnique: true
|
|
3848
|
+
}))];
|
|
3849
|
+
const indexBlock = indexes.length > 0 ? `\n\n indexes {\n${indexes.map(makeIndex).join("\n")}\n }` : "";
|
|
3850
|
+
const strippedNote = stripAnnotations(model.documentation);
|
|
3851
|
+
return `Table ${modelName} {\n${columnLines}${indexBlock}${strippedNote ? `\n\n Note: ${noteLiteral(strippedNote)}` : ""}\n}`;
|
|
3852
|
+
});
|
|
3853
|
+
}
|
|
3854
|
+
function makeEnums(enums) {
|
|
3855
|
+
return enums.map((e) => makeEnum({
|
|
3856
|
+
name: e.name,
|
|
3857
|
+
values: e.values.map((v) => v.name)
|
|
4297
3858
|
}));
|
|
4298
|
-
|
|
4299
|
-
|
|
4300
|
-
|
|
3859
|
+
}
|
|
3860
|
+
function makeRelations(models, mapToDbSchema = false) {
|
|
3861
|
+
return models.flatMap((model) => model.fields.filter((field) => field.relationName && field.relationToFields?.length && field.relationFromFields?.length).map((field) => {
|
|
3862
|
+
const relationTo = field.type;
|
|
3863
|
+
const toModel = models.find((m) => m.name === relationTo);
|
|
3864
|
+
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 = mapToDbSchema && model.dbName ? model.dbName : model.name;
|
|
3866
|
+
const relationToName = mapToDbSchema && toModel?.dbName ? toModel.dbName : relationTo;
|
|
3867
|
+
const fromColumn = combineKeys(field.relationFromFields ?? []);
|
|
3868
|
+
const toColumn = combineKeys(field.relationToFields ?? []);
|
|
3869
|
+
return makeRef({
|
|
3870
|
+
name: `${relationFromName}_${fromColumn}_fk`,
|
|
3871
|
+
fromTable: relationFromName,
|
|
3872
|
+
fromColumn,
|
|
3873
|
+
toTable: relationToName,
|
|
3874
|
+
toColumn,
|
|
3875
|
+
type: operator,
|
|
3876
|
+
onDelete: field.relationOnDelete,
|
|
3877
|
+
onUpdate: field.relationOnUpdate
|
|
3878
|
+
});
|
|
4301
3879
|
}));
|
|
4302
|
-
|
|
3880
|
+
}
|
|
3881
|
+
function isMany(cardinality) {
|
|
3882
|
+
return cardinality === "many" || cardinality === "zero-many";
|
|
3883
|
+
}
|
|
3884
|
+
function dbmlOperator(leftMany, rightMany) {
|
|
3885
|
+
if (leftMany && !rightMany) return ">";
|
|
3886
|
+
if (!leftMany && rightMany) return "<";
|
|
3887
|
+
if (leftMany && rightMany) return "<>";
|
|
3888
|
+
return "-";
|
|
3889
|
+
}
|
|
3890
|
+
function annotatedDbmlRefs(models) {
|
|
3891
|
+
const inferredKeys = new Set(inferredERRelations(models).map(erKey));
|
|
3892
|
+
return annotatedERRelations(models).filter((relation) => !inferredKeys.has(erKey(relation))).map((relation) => {
|
|
3893
|
+
const left = `${relation.to.model}.${relation.to.field}`;
|
|
3894
|
+
const right = `${relation.from.model}.${relation.from.field}`;
|
|
3895
|
+
const operator = dbmlOperator(isMany(relation.to.cardinality), isMany(relation.from.cardinality));
|
|
3896
|
+
return `Ref ${`${relation.to.model}_${relation.to.field}_${relation.from.model}_${relation.from.field}`}: ${left} ${operator} ${right}`;
|
|
3897
|
+
});
|
|
4303
3898
|
}
|
|
4304
3899
|
//#endregion
|
|
4305
|
-
//#region src/
|
|
4306
|
-
|
|
4307
|
-
|
|
4308
|
-
|
|
4309
|
-
|
|
4310
|
-
|
|
4311
|
-
|
|
4312
|
-
|
|
4313
|
-
|
|
4314
|
-
|
|
3900
|
+
//#region src/generator/dbml.ts
|
|
3901
|
+
function dbmlContent(datamodel, mapToDbSchema = false) {
|
|
3902
|
+
const tables = makeTables(datamodel.models, mapToDbSchema);
|
|
3903
|
+
const enums = makeEnums(datamodel.enums);
|
|
3904
|
+
const refs = makeRelations(datamodel.models, mapToDbSchema);
|
|
3905
|
+
const logicalRefs = annotatedDbmlRefs(datamodel.models);
|
|
3906
|
+
return [
|
|
3907
|
+
...enums,
|
|
3908
|
+
...tables,
|
|
3909
|
+
...refs,
|
|
3910
|
+
...logicalRefs
|
|
3911
|
+
].join("\n\n");
|
|
3912
|
+
}
|
|
3913
|
+
/** The ER diagram as Studio draws it, laid out automatically, as an SVG document. */
|
|
3914
|
+
function erDiagramSvg(datamodel, theme = "light") {
|
|
3915
|
+
const schema = makeSchema({
|
|
3916
|
+
dmmf: { datamodel },
|
|
3917
|
+
files: [],
|
|
3918
|
+
provider: null,
|
|
3919
|
+
blocks: []
|
|
3920
|
+
});
|
|
3921
|
+
const positions = autoLayout(schema);
|
|
3922
|
+
return renderDiagramSvg({
|
|
3923
|
+
models: schema.models,
|
|
3924
|
+
relations: schema.relations,
|
|
3925
|
+
enums: schema.enums,
|
|
3926
|
+
positions,
|
|
3927
|
+
theme
|
|
3928
|
+
});
|
|
3929
|
+
}
|
|
3930
|
+
/** The ER diagram as Studio draws it, rasterised to PNG at 2x. */
|
|
3931
|
+
function erDiagramPng(datamodel, theme = "light") {
|
|
3932
|
+
return svgToPng(erDiagramSvg(datamodel, theme));
|
|
3933
|
+
}
|
|
3934
|
+
//#endregion
|
|
3935
|
+
//#region src/helper/mermaid-er.ts
|
|
3936
|
+
const LEFT = {
|
|
3937
|
+
"zero-one": "|o",
|
|
3938
|
+
one: "||",
|
|
3939
|
+
"zero-many": "}o",
|
|
3940
|
+
many: "}|"
|
|
3941
|
+
};
|
|
3942
|
+
const RIGHT = {
|
|
3943
|
+
"zero-one": "o|",
|
|
3944
|
+
one: "||",
|
|
3945
|
+
"zero-many": "o{",
|
|
3946
|
+
many: "|{"
|
|
3947
|
+
};
|
|
3948
|
+
function escapeComment(comment) {
|
|
3949
|
+
return comment.replaceAll(/\r?\n/gu, " ").replaceAll("\"", "#quot;");
|
|
3950
|
+
}
|
|
3951
|
+
function erRelationLine(relation, resolveName = (model) => model) {
|
|
3952
|
+
return ` ${resolveName(relation.from.model)} ${LEFT[relation.from.cardinality]}--${RIGHT[relation.to.cardinality]} ${resolveName(relation.to.model)} : "(${relation.from.field}) - (${relation.to.field})"`;
|
|
3953
|
+
}
|
|
3954
|
+
/** The `PK` / `FK` / `UK` markers Mermaid draws beside an attribute, in that order. */
|
|
3955
|
+
function keyMarkers(field, primaryKey, foreignKeys, uniques) {
|
|
3956
|
+
return [
|
|
3957
|
+
field.isId || primaryKey.has(field.name) ? "PK" : null,
|
|
3958
|
+
foreignKeys.has(field.name) ? "FK" : null,
|
|
3959
|
+
field.isUnique || uniques.has(field.name) ? "UK" : null
|
|
3960
|
+
].filter((marker) => marker !== null);
|
|
3961
|
+
}
|
|
3962
|
+
function modelFields(model) {
|
|
3963
|
+
const fkFields = new Set(model.fields.filter((f) => f.relationFromFields && f.relationFromFields.length > 0).flatMap((f) => f.relationFromFields ?? []));
|
|
3964
|
+
const primaryKey = new Set(model.primaryKey?.fields);
|
|
3965
|
+
const uniques = new Set(model.uniqueFields.flat());
|
|
3966
|
+
return model.fields.map((field) => {
|
|
3967
|
+
if (field.relationName) return null;
|
|
3968
|
+
const commentPart = stripAnnotations(field.documentation) ?? "";
|
|
3969
|
+
const markers = keyMarkers(field, primaryKey, fkFields, uniques);
|
|
3970
|
+
const keyPart = markers.length > 0 ? ` ${markers.join(", ")}` : "";
|
|
3971
|
+
return ` ${`${field.type.toLowerCase()}${field.isList ? "[]" : ""}`} ${field.name}${keyPart}${commentPart ? ` "${escapeComment(commentPart)}"` : ""}`;
|
|
3972
|
+
}).filter((field) => field !== null);
|
|
3973
|
+
}
|
|
3974
|
+
function modelInfo(model) {
|
|
3975
|
+
return [
|
|
3976
|
+
` ${model.dbName ?? model.name} {`,
|
|
3977
|
+
...modelFields(model),
|
|
3978
|
+
" }"
|
|
3979
|
+
];
|
|
3980
|
+
}
|
|
3981
|
+
//#endregion
|
|
3982
|
+
//#region src/generator/mermaid-er.ts
|
|
3983
|
+
const ER_HEADER = ["```mermaid", "erDiagram"];
|
|
3984
|
+
const ER_FOOTER = ["```"];
|
|
3985
|
+
function erContent(models) {
|
|
3986
|
+
const resolveName = (model) => models.find((m) => m.name === model)?.dbName ?? model;
|
|
3987
|
+
const relations = erRelations(models).map((relation) => erRelationLine(relation, resolveName));
|
|
3988
|
+
const modelInfos = models.flatMap(modelInfo);
|
|
3989
|
+
return [
|
|
3990
|
+
...ER_HEADER,
|
|
3991
|
+
...relations,
|
|
3992
|
+
...modelInfos,
|
|
3993
|
+
...ER_FOOTER
|
|
3994
|
+
];
|
|
3995
|
+
}
|
|
3996
|
+
//#endregion
|
|
3997
|
+
//#region src/core/er.ts
|
|
3998
|
+
function themeOf(options) {
|
|
3999
|
+
return getString(options.generator.config?.theme) === "dark" ? "dark" : "light";
|
|
4000
|
+
}
|
|
4001
|
+
/**
|
|
4002
|
+
* The four ways the ER model of a schema is written out, each with the options it reads.
|
|
4003
|
+
*
|
|
4004
|
+
* The extension of each file picks one, so the file the caller names is the file they get: an
|
|
4005
|
+
* extension with no format behind it, and an option no chosen format reads, are both errors
|
|
4006
|
+
* rather than something silently ignored.
|
|
4007
|
+
*/
|
|
4008
|
+
const FORMATS = {
|
|
4009
|
+
".md": {
|
|
4010
|
+
options: [],
|
|
4011
|
+
render: (options) => erContent(options.dmmf.datamodel.models).join("\n")
|
|
4012
|
+
},
|
|
4013
|
+
".dbml": {
|
|
4014
|
+
options: ["mapToDbSchema"],
|
|
4015
|
+
render: (options) => dbmlContent(options.dmmf.datamodel, getString(options.generator.config?.mapToDbSchema) !== "false")
|
|
4016
|
+
},
|
|
4017
|
+
".png": {
|
|
4018
|
+
options: ["theme"],
|
|
4019
|
+
render: (options) => erDiagramPng(options.dmmf.datamodel, themeOf(options))
|
|
4020
|
+
},
|
|
4021
|
+
".svg": {
|
|
4022
|
+
options: ["theme"],
|
|
4023
|
+
render: (options) => erDiagramSvg(options.dmmf.datamodel, themeOf(options))
|
|
4024
|
+
}
|
|
4025
|
+
};
|
|
4026
|
+
const EXTENSIONS = Object.keys(FORMATS).join(", ");
|
|
4027
|
+
function isFormat(extension) {
|
|
4028
|
+
return extension in FORMATS;
|
|
4029
|
+
}
|
|
4030
|
+
/** What the chosen format accepts, as the sentence the error ends with. */
|
|
4031
|
+
function accepts(options) {
|
|
4032
|
+
return options.length === 0 ? "takes no options" : `takes ${options.join(", ")}`;
|
|
4033
|
+
}
|
|
4034
|
+
/** How a path ends, as the error says it: an extension in quotes, or nothing at all. */
|
|
4035
|
+
function endsIn(name) {
|
|
4036
|
+
const extension = path.extname(name);
|
|
4037
|
+
return extension === "" ? "nothing" : `"${extension}"`;
|
|
4038
|
+
}
|
|
4039
|
+
/**
|
|
4040
|
+
* The files to write, from `output` alone or from the names `outputs` lists inside it.
|
|
4041
|
+
*
|
|
4042
|
+
* One block writes as many files as it names, so a schema that wants the ER model in several
|
|
4043
|
+
* formats declares `Hekireki-ER` once — `outputs` is the list, `output` the directory it fills.
|
|
4044
|
+
*/
|
|
4045
|
+
function filesOf(output, outputs) {
|
|
4046
|
+
return Effect.gen(function* () {
|
|
4047
|
+
if (outputs === void 0) {
|
|
4048
|
+
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)}.` });
|
|
4050
|
+
return [{
|
|
4051
|
+
file: output,
|
|
4052
|
+
extension
|
|
4053
|
+
}];
|
|
4054
|
+
}
|
|
4055
|
+
if (outputs.length === 0) return yield* new GeneratorConfigError({ message: "outputs for Hekireki-ER has to name at least one file." });
|
|
4056
|
+
if (path.extname(output) !== "") return yield* new GeneratorConfigError({ message: `output for Hekireki-ER names the directory the files in outputs go in, so it cannot name a file; "${output}" ends in ${endsIn(output)}.` });
|
|
4057
|
+
return yield* Effect.forEach(outputs, (name) => {
|
|
4058
|
+
const extension = path.extname(name).toLowerCase();
|
|
4059
|
+
if (!isFormat(extension)) return new GeneratorConfigError({ message: `outputs for Hekireki-ER has to name files ending in ${EXTENSIONS}; "${name}" ends in ${endsIn(name)}.` });
|
|
4060
|
+
return Effect.succeed({
|
|
4061
|
+
file: path.resolve(output, name),
|
|
4062
|
+
extension
|
|
4063
|
+
});
|
|
4064
|
+
});
|
|
4065
|
+
});
|
|
4066
|
+
}
|
|
4067
|
+
function er(options) {
|
|
4068
|
+
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
|
+
const config = options.generator.config ?? {};
|
|
4071
|
+
const outputs = getStrings(config.outputs);
|
|
4072
|
+
const files = yield* filesOf(options.generator.output.value, outputs);
|
|
4073
|
+
const read = new Set(outputs === void 0 ? [] : ["outputs"]);
|
|
4074
|
+
for (const { extension } of files) for (const option of FORMATS[extension].options) read.add(option);
|
|
4075
|
+
const unread = Object.keys(config).filter((name) => !read.has(name));
|
|
4076
|
+
if (unread.length > 0) {
|
|
4077
|
+
const extensions = [...new Set(files.map(({ extension }) => extension))];
|
|
4078
|
+
return yield* new GeneratorConfigError({ message: `Hekireki-ER does not read ${unread.map((name) => `"${name}"`).join(", ")} for ${extensions.length === 1 ? `a ${extensions[0]} output` : `the ${extensions.join(", ")} outputs`}; ${extensions.map((extension) => `${extension} ${accepts(FORMATS[extension].options)}`).join(", ")}.` });
|
|
4079
|
+
}
|
|
4080
|
+
return yield* Effect.forEach(files, ({ file, extension }) => emitRaw(FORMATS[extension].render(options), path.dirname(file), file), { discard: true });
|
|
4081
|
+
});
|
|
4315
4082
|
}
|
|
4316
4083
|
//#endregion
|
|
4317
4084
|
//#region src/helper/gorm.ts
|
|
@@ -4449,7 +4216,6 @@ function buildGormTags(field, isPk, isCompositePk, compositeIndexTags, enums) {
|
|
|
4449
4216
|
const isNowDefault = field.type === "DateTime" && isFunctionDefault$3(field.default) && field.default.name === "now";
|
|
4450
4217
|
const nativeType = resolveNativeType$1(field);
|
|
4451
4218
|
const includeNativeType = nativeType && (!isPk || !isFunctionDefault$3(field.default) || field.default.name !== "uuid");
|
|
4452
|
-
const includeAutoCreate = isNowDefault && (!isPk || isCompositePk || !isCompositePk);
|
|
4453
4219
|
const dbGeneratedExpr = isFunctionDefault$3(field.default) && field.default.name === "dbgenerated" && typeof field.default.args[0] === "string" ? field.default.args[0] : null;
|
|
4454
4220
|
const enumMappedDefault = (() => {
|
|
4455
4221
|
if (!(field.kind === "enum" && typeof field.default === "string")) return null;
|
|
@@ -4467,7 +4233,7 @@ function buildGormTags(field, isPk, isCompositePk, compositeIndexTags, enums) {
|
|
|
4467
4233
|
...compositeIndexTags,
|
|
4468
4234
|
includeNativeType ? `type:${nativeType}` : null,
|
|
4469
4235
|
field.isList && field.kind !== "object" ? "serializer:json" : null,
|
|
4470
|
-
|
|
4236
|
+
isNowDefault ? "autoCreateTime" : null,
|
|
4471
4237
|
defaultVal !== null ? `default:${defaultVal}` : null,
|
|
4472
4238
|
field.isUpdatedAt ? "autoUpdateTime" : null,
|
|
4473
4239
|
field.isRequired && !isPk ? "not null" : null
|
|
@@ -4709,17 +4475,16 @@ function generateGormModels(models, enums, indexes, packageName = "model") {
|
|
|
4709
4475
|
}
|
|
4710
4476
|
//#endregion
|
|
4711
4477
|
//#region src/core/gorm.ts
|
|
4712
|
-
|
|
4713
|
-
|
|
4714
|
-
|
|
4715
|
-
|
|
4716
|
-
|
|
4717
|
-
|
|
4718
|
-
|
|
4719
|
-
|
|
4720
|
-
|
|
4721
|
-
|
|
4722
|
-
return emitRaw(generateGormModels(options.dmmf.datamodel.models, enums, indexes, packageName), path.dirname(outPath), outPath);
|
|
4478
|
+
function gorm(options) {
|
|
4479
|
+
return Effect.gen(function* () {
|
|
4480
|
+
if (!(options.generator.isCustomOutput && options.generator.output?.value)) return yield* new GeneratorConfigError({ message: "output is required for Hekireki-GORM. Please specify output in your generator config." });
|
|
4481
|
+
const output = options.generator.output.value;
|
|
4482
|
+
const outPath = path.extname(output) ? output : path.join(output, "models.go");
|
|
4483
|
+
const packageName = getString(options.generator.config.package) ?? "model";
|
|
4484
|
+
const enums = options.dmmf.datamodel.enums;
|
|
4485
|
+
const indexes = options.dmmf.datamodel.indexes;
|
|
4486
|
+
return yield* emitRaw(generateGormModels(options.dmmf.datamodel.models, enums, indexes, packageName), path.dirname(outPath), outPath);
|
|
4487
|
+
});
|
|
4723
4488
|
}
|
|
4724
4489
|
//#endregion
|
|
4725
4490
|
//#region src/helper/kysely.ts
|
|
@@ -4826,84 +4591,19 @@ function kyselySchema(datamodel) {
|
|
|
4826
4591
|
}
|
|
4827
4592
|
//#endregion
|
|
4828
4593
|
//#region src/core/kysely.ts
|
|
4829
|
-
|
|
4830
|
-
|
|
4831
|
-
|
|
4832
|
-
|
|
4833
|
-
|
|
4834
|
-
|
|
4835
|
-
|
|
4836
|
-
|
|
4837
|
-
|
|
4838
|
-
|
|
4839
|
-
|
|
4840
|
-
|
|
4841
|
-
};
|
|
4842
|
-
return emit(kyselySchema(options.dmmf.datamodel), resolved.dir, resolved.file);
|
|
4843
|
-
}
|
|
4844
|
-
//#endregion
|
|
4845
|
-
//#region src/helper/mermaid-er.ts
|
|
4846
|
-
const RELATIONSHIPS = {
|
|
4847
|
-
"zero-one": "|o",
|
|
4848
|
-
one: "||",
|
|
4849
|
-
"zero-many": "}o",
|
|
4850
|
-
many: "}|"
|
|
4851
|
-
};
|
|
4852
|
-
function escapeComment(comment) {
|
|
4853
|
-
return comment.replaceAll(/\r?\n/gu, " ").replaceAll("\"", "#quot;");
|
|
4854
|
-
}
|
|
4855
|
-
function erRelationLine(relation, resolveName = (model) => model) {
|
|
4856
|
-
return ` ${resolveName(relation.from.model)} ${RELATIONSHIPS[relation.from.cardinality]}--${RELATIONSHIPS[relation.to.cardinality]} ${resolveName(relation.to.model)} : "(${relation.from.field}) - (${relation.to.field})"`;
|
|
4857
|
-
}
|
|
4858
|
-
function modelFields(model) {
|
|
4859
|
-
const fkFields = new Set(model.fields.filter((f) => f.relationFromFields && f.relationFromFields.length > 0).flatMap((f) => f.relationFromFields ?? []));
|
|
4860
|
-
return model.fields.map((field) => {
|
|
4861
|
-
if (field.relationName) return null;
|
|
4862
|
-
const commentPart = stripAnnotations(field.documentation) ?? "";
|
|
4863
|
-
const keyMarker = field.isId ? "PK" : fkFields.has(field.name) ? "FK" : "";
|
|
4864
|
-
const keyPart = keyMarker ? ` ${keyMarker}` : "";
|
|
4865
|
-
return ` ${field.type.toLowerCase()} ${field.name}${keyPart}${commentPart ? ` "${escapeComment(commentPart)}"` : ""}`;
|
|
4866
|
-
}).filter((field) => field !== null);
|
|
4867
|
-
}
|
|
4868
|
-
function modelInfo(model) {
|
|
4869
|
-
return [
|
|
4870
|
-
` ${model.dbName ?? model.name} {`,
|
|
4871
|
-
...modelFields(model),
|
|
4872
|
-
" }"
|
|
4873
|
-
];
|
|
4874
|
-
}
|
|
4875
|
-
//#endregion
|
|
4876
|
-
//#region src/generator/mermaid-er.ts
|
|
4877
|
-
const ER_HEADER = ["```mermaid", "erDiagram"];
|
|
4878
|
-
const ER_FOOTER = ["```"];
|
|
4879
|
-
function erContent(models) {
|
|
4880
|
-
const resolveName = (model) => models.find((m) => m.name === model)?.dbName ?? model;
|
|
4881
|
-
const relations = mergeERRelations(models).map((relation) => erRelationLine(relation, resolveName));
|
|
4882
|
-
const modelInfos = models.flatMap(modelInfo);
|
|
4883
|
-
return [
|
|
4884
|
-
...ER_HEADER,
|
|
4885
|
-
...relations,
|
|
4886
|
-
...modelInfos,
|
|
4887
|
-
...ER_FOOTER
|
|
4888
|
-
];
|
|
4889
|
-
}
|
|
4890
|
-
//#endregion
|
|
4891
|
-
//#region src/core/mermaid-er.ts
|
|
4892
|
-
async function mermaidEr(options) {
|
|
4893
|
-
if (!(options.generator.isCustomOutput && options.generator.output?.value)) return {
|
|
4894
|
-
ok: false,
|
|
4895
|
-
error: "output is required for Hekireki-ER. Please specify output in your generator config."
|
|
4896
|
-
};
|
|
4897
|
-
const output = options.generator.output.value;
|
|
4898
|
-
const content = erContent(options.dmmf.datamodel.models);
|
|
4899
|
-
const resolved = path.extname(output) ? {
|
|
4900
|
-
dir: path.dirname(output),
|
|
4901
|
-
file: output
|
|
4902
|
-
} : {
|
|
4903
|
-
dir: output,
|
|
4904
|
-
file: path.join(output, "ER.md")
|
|
4905
|
-
};
|
|
4906
|
-
return emitRaw(content.join("\n"), resolved.dir, resolved.file);
|
|
4594
|
+
function kysely(options) {
|
|
4595
|
+
return Effect.gen(function* () {
|
|
4596
|
+
if (!(options.generator.isCustomOutput && options.generator.output?.value)) return yield* new GeneratorConfigError({ message: "output is required for Hekireki-Kysely. Please specify output in your generator config." });
|
|
4597
|
+
const output = options.generator.output.value;
|
|
4598
|
+
const resolved = path.extname(output) ? {
|
|
4599
|
+
dir: path.dirname(output),
|
|
4600
|
+
file: output
|
|
4601
|
+
} : {
|
|
4602
|
+
dir: output,
|
|
4603
|
+
file: path.join(output, "types.ts")
|
|
4604
|
+
};
|
|
4605
|
+
return yield* emit(kyselySchema(options.dmmf.datamodel), resolved.dir, resolved.file);
|
|
4606
|
+
});
|
|
4907
4607
|
}
|
|
4908
4608
|
//#endregion
|
|
4909
4609
|
//#region src/helper/pydantic.ts
|
|
@@ -5128,14 +4828,13 @@ function pydanticCode(models, enums, comment, relation = false) {
|
|
|
5128
4828
|
}
|
|
5129
4829
|
//#endregion
|
|
5130
4830
|
//#region src/core/pydantic.ts
|
|
5131
|
-
|
|
5132
|
-
|
|
5133
|
-
|
|
5134
|
-
|
|
5135
|
-
|
|
5136
|
-
|
|
5137
|
-
|
|
5138
|
-
return emitRaw(pydanticCode(options.dmmf.datamodel.models, options.dmmf.datamodel.enums, getBool(options.generator.config?.comment), getBool(options.generator.config?.relation)), path.dirname(outPath), outPath);
|
|
4831
|
+
function pydantic(options) {
|
|
4832
|
+
return Effect.gen(function* () {
|
|
4833
|
+
if (!(options.generator.isCustomOutput && options.generator.output?.value)) return yield* new GeneratorConfigError({ message: "output is required for Hekireki-Pydantic. Please specify output in your generator config." });
|
|
4834
|
+
const output = options.generator.output.value;
|
|
4835
|
+
const outPath = path.extname(output) ? output : path.join(output, "models.py");
|
|
4836
|
+
return yield* emitRaw(pydanticCode(options.dmmf.datamodel.models, options.dmmf.datamodel.enums, getBool(options.generator.config?.comment), getBool(options.generator.config?.relation)), path.dirname(outPath), outPath);
|
|
4837
|
+
});
|
|
5139
4838
|
}
|
|
5140
4839
|
//#endregion
|
|
5141
4840
|
//#region src/helper/sea-orm.ts
|
|
@@ -5376,9 +5075,9 @@ function buildSerdeAttributes(opts) {
|
|
|
5376
5075
|
return [`#[serde(${parts.join(", ")})]`];
|
|
5377
5076
|
}
|
|
5378
5077
|
function generateEnum(e, serde = {}) {
|
|
5379
|
-
const variants = e.values.map((
|
|
5380
|
-
const pascalName =
|
|
5381
|
-
return ` #[sea_orm(string_value = "${
|
|
5078
|
+
const variants = e.values.map((value) => {
|
|
5079
|
+
const pascalName = value.name.split("_").filter((part) => part !== "").map((part) => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase()).join("");
|
|
5080
|
+
return ` #[sea_orm(string_value = "${value.dbName ?? value.name}")]\n ${pascalName},`;
|
|
5382
5081
|
});
|
|
5383
5082
|
return [
|
|
5384
5083
|
"#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize)]",
|
|
@@ -5674,15 +5373,14 @@ function seaOrmFiles(models, enums, serde = {}) {
|
|
|
5674
5373
|
}
|
|
5675
5374
|
//#endregion
|
|
5676
5375
|
//#region src/core/sea-orm.ts
|
|
5677
|
-
|
|
5678
|
-
|
|
5679
|
-
|
|
5680
|
-
|
|
5681
|
-
|
|
5682
|
-
|
|
5683
|
-
|
|
5684
|
-
|
|
5685
|
-
return emitMany(seaOrmFiles(options.dmmf.datamodel.models, enums, serde), outDir);
|
|
5376
|
+
function seaOrm(options) {
|
|
5377
|
+
return Effect.gen(function* () {
|
|
5378
|
+
if (!(options.generator.isCustomOutput && options.generator.output?.value)) return yield* new GeneratorConfigError({ message: "output is required for Hekireki-SeaORM. Please specify output in your generator config." });
|
|
5379
|
+
const outDir = options.generator.output.value;
|
|
5380
|
+
const serde = { renameAll: getString(options.generator.config?.renameAll) };
|
|
5381
|
+
const enums = options.dmmf.datamodel.enums;
|
|
5382
|
+
return yield* emitMany(seaOrmFiles(options.dmmf.datamodel.models, enums, serde), outDir);
|
|
5383
|
+
});
|
|
5686
5384
|
}
|
|
5687
5385
|
//#endregion
|
|
5688
5386
|
//#region src/helper/sqlalchemy.ts
|
|
@@ -6250,16 +5948,15 @@ function generateSingleFile(models, enums, indexes) {
|
|
|
6250
5948
|
}
|
|
6251
5949
|
//#endregion
|
|
6252
5950
|
//#region src/core/sqlalchemy.ts
|
|
6253
|
-
|
|
6254
|
-
|
|
6255
|
-
|
|
6256
|
-
|
|
6257
|
-
|
|
6258
|
-
|
|
6259
|
-
|
|
6260
|
-
|
|
6261
|
-
|
|
6262
|
-
return emitRaw(generateSingleFile(options.dmmf.datamodel.models, enums, indexes), path.dirname(outPath), outPath);
|
|
5951
|
+
function sqlalchemy(options) {
|
|
5952
|
+
return Effect.gen(function* () {
|
|
5953
|
+
if (!(options.generator.isCustomOutput && options.generator.output?.value)) return yield* new GeneratorConfigError({ message: "output is required for Hekireki-SQLAlchemy. Please specify output in your generator config." });
|
|
5954
|
+
const output = options.generator.output.value;
|
|
5955
|
+
const outPath = path.extname(output) ? output : path.join(output, "models.py");
|
|
5956
|
+
const enums = options.dmmf.datamodel.enums;
|
|
5957
|
+
const indexes = options.dmmf.datamodel.indexes;
|
|
5958
|
+
return yield* emitRaw(generateSingleFile(options.dmmf.datamodel.models, enums, indexes), path.dirname(outPath), outPath);
|
|
5959
|
+
});
|
|
6263
5960
|
}
|
|
6264
5961
|
//#endregion
|
|
6265
5962
|
//#region src/helper/typebox.ts
|
|
@@ -6322,20 +6019,19 @@ function typeboxCode(dmmf, type, comment, relation) {
|
|
|
6322
6019
|
}
|
|
6323
6020
|
//#endregion
|
|
6324
6021
|
//#region src/core/typebox.ts
|
|
6325
|
-
|
|
6326
|
-
|
|
6327
|
-
|
|
6328
|
-
|
|
6329
|
-
|
|
6330
|
-
|
|
6331
|
-
|
|
6332
|
-
|
|
6333
|
-
|
|
6334
|
-
|
|
6335
|
-
|
|
6336
|
-
|
|
6337
|
-
};
|
|
6338
|
-
return emit(typeboxCode(options.dmmf, getBool(options.generator.config?.type), getBool(options.generator.config?.comment), getBool(options.generator.config?.relation)), resolved.dir, resolved.file);
|
|
6022
|
+
function typebox(options) {
|
|
6023
|
+
return Effect.gen(function* () {
|
|
6024
|
+
if (!(options.generator.isCustomOutput && options.generator.output?.value)) return yield* new GeneratorConfigError({ message: "output is required for Hekireki-TypeBox. Please specify output in your generator config." });
|
|
6025
|
+
const output = options.generator.output.value;
|
|
6026
|
+
const resolved = path.extname(output) ? {
|
|
6027
|
+
dir: path.dirname(output),
|
|
6028
|
+
file: output
|
|
6029
|
+
} : {
|
|
6030
|
+
dir: output,
|
|
6031
|
+
file: path.join(output, "index.ts")
|
|
6032
|
+
};
|
|
6033
|
+
return yield* emit(typeboxCode(options.dmmf, getBool(options.generator.config?.type), getBool(options.generator.config?.comment), getBool(options.generator.config?.relation)), resolved.dir, resolved.file);
|
|
6034
|
+
});
|
|
6339
6035
|
}
|
|
6340
6036
|
//#endregion
|
|
6341
6037
|
//#region src/helper/valibot.ts
|
|
@@ -6389,20 +6085,19 @@ function valibotCode(dmmf, type, comment, relation) {
|
|
|
6389
6085
|
}
|
|
6390
6086
|
//#endregion
|
|
6391
6087
|
//#region src/core/valibot.ts
|
|
6392
|
-
|
|
6393
|
-
|
|
6394
|
-
|
|
6395
|
-
|
|
6396
|
-
|
|
6397
|
-
|
|
6398
|
-
|
|
6399
|
-
|
|
6400
|
-
|
|
6401
|
-
|
|
6402
|
-
|
|
6403
|
-
|
|
6404
|
-
};
|
|
6405
|
-
return emit(valibotCode(options.dmmf, getBool(options.generator.config?.type), getBool(options.generator.config?.comment), getBool(options.generator.config?.relation)), resolved.dir, resolved.file);
|
|
6088
|
+
function valibot(options) {
|
|
6089
|
+
return Effect.gen(function* () {
|
|
6090
|
+
if (!(options.generator.isCustomOutput && options.generator.output?.value)) return yield* new GeneratorConfigError({ message: "output is required for Hekireki-Valibot. Please specify output in your generator config." });
|
|
6091
|
+
const output = options.generator.output.value;
|
|
6092
|
+
const resolved = path.extname(output) ? {
|
|
6093
|
+
dir: path.dirname(output),
|
|
6094
|
+
file: output
|
|
6095
|
+
} : {
|
|
6096
|
+
dir: output,
|
|
6097
|
+
file: path.join(output, "index.ts")
|
|
6098
|
+
};
|
|
6099
|
+
return yield* emit(valibotCode(options.dmmf, getBool(options.generator.config?.type), getBool(options.generator.config?.comment), getBool(options.generator.config?.relation)), resolved.dir, resolved.file);
|
|
6100
|
+
});
|
|
6406
6101
|
}
|
|
6407
6102
|
//#endregion
|
|
6408
6103
|
//#region src/helper/zod.ts
|
|
@@ -6456,20 +6151,19 @@ function zodCode(dmmf, type, comment, relation, version) {
|
|
|
6456
6151
|
}
|
|
6457
6152
|
//#endregion
|
|
6458
6153
|
//#region src/core/zod.ts
|
|
6459
|
-
|
|
6460
|
-
|
|
6461
|
-
|
|
6462
|
-
|
|
6463
|
-
|
|
6464
|
-
|
|
6465
|
-
|
|
6466
|
-
|
|
6467
|
-
|
|
6468
|
-
|
|
6469
|
-
|
|
6470
|
-
|
|
6471
|
-
};
|
|
6472
|
-
return emit(zodCode(options.dmmf, getBool(options.generator.config?.type), getBool(options.generator.config?.comment), getBool(options.generator.config?.relation), getString(options.generator.config?.zod) ?? "v4"), resolved.dir, resolved.file);
|
|
6154
|
+
function zod(options) {
|
|
6155
|
+
return Effect.gen(function* () {
|
|
6156
|
+
if (!(options.generator.isCustomOutput && options.generator.output?.value)) return yield* new GeneratorConfigError({ message: "output is required for Hekireki-Zod. Please specify output in your generator config." });
|
|
6157
|
+
const output = options.generator.output.value;
|
|
6158
|
+
const resolved = path.extname(output) ? {
|
|
6159
|
+
dir: path.dirname(output),
|
|
6160
|
+
file: output
|
|
6161
|
+
} : {
|
|
6162
|
+
dir: output,
|
|
6163
|
+
file: path.join(output, "index.ts")
|
|
6164
|
+
};
|
|
6165
|
+
return yield* emit(zodCode(options.dmmf, getBool(options.generator.config?.type), getBool(options.generator.config?.comment), getBool(options.generator.config?.relation), getString(options.generator.config?.zod) ?? "v4"), resolved.dir, resolved.file);
|
|
6166
|
+
});
|
|
6473
6167
|
}
|
|
6474
6168
|
//#endregion
|
|
6475
6169
|
//#region src/bin/index.ts
|
|
@@ -6490,18 +6184,14 @@ const GENERATORS = {
|
|
|
6490
6184
|
prettyName: "Hekireki-Atlas",
|
|
6491
6185
|
handler: atlas
|
|
6492
6186
|
},
|
|
6493
|
-
|
|
6494
|
-
prettyName: "Hekireki-
|
|
6495
|
-
handler:
|
|
6187
|
+
er: {
|
|
6188
|
+
prettyName: "Hekireki-ER",
|
|
6189
|
+
handler: er
|
|
6496
6190
|
},
|
|
6497
6191
|
django: {
|
|
6498
6192
|
prettyName: "Hekireki-Django",
|
|
6499
6193
|
handler: django
|
|
6500
6194
|
},
|
|
6501
|
-
docs: {
|
|
6502
|
-
prettyName: "Hekireki-Docs",
|
|
6503
|
-
handler: docs
|
|
6504
|
-
},
|
|
6505
6195
|
drizzle: {
|
|
6506
6196
|
prettyName: "Hekireki-Drizzle",
|
|
6507
6197
|
handler: drizzle
|
|
@@ -6526,10 +6216,6 @@ const GENERATORS = {
|
|
|
6526
6216
|
prettyName: "Hekireki-Kysely",
|
|
6527
6217
|
handler: kysely
|
|
6528
6218
|
},
|
|
6529
|
-
"mermaid-er": {
|
|
6530
|
-
prettyName: "Hekireki-ER",
|
|
6531
|
-
handler: mermaidEr
|
|
6532
|
-
},
|
|
6533
6219
|
pydantic: {
|
|
6534
6220
|
prettyName: "Hekireki-Pydantic",
|
|
6535
6221
|
handler: pydantic
|
|
@@ -6564,9 +6250,8 @@ function registerGenerator(name) {
|
|
|
6564
6250
|
prettyName
|
|
6565
6251
|
};
|
|
6566
6252
|
},
|
|
6567
|
-
|
|
6568
|
-
|
|
6569
|
-
if (!result.ok) throw new Error(result.error);
|
|
6253
|
+
onGenerate(options) {
|
|
6254
|
+
return Effect.runPromise(handler(options).pipe(Effect.mapError((error) => new Error(error.message)), Effect.provide(fileSystemLayer)));
|
|
6570
6255
|
}
|
|
6571
6256
|
});
|
|
6572
6257
|
}
|