sizuku 0.6.4 → 0.6.5

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.
Files changed (2) hide show
  1. package/dist/index.mjs +73 -2
  2. package/package.json +1 -1
package/dist/index.mjs CHANGED
@@ -432,6 +432,66 @@ function extractRelationsFromSchema(code) {
432
432
  });
433
433
  }
434
434
  //#endregion
435
+ //#region src/helper/parse-relation.ts
436
+ const RELATIONSHIPS = {
437
+ "zero-one": "|o",
438
+ one: "||",
439
+ "zero-many": "}o",
440
+ many: "}|"
441
+ };
442
+ function isRelationshipType(type) {
443
+ return type === "zero-one" || type === "one" || type === "zero-many" || type === "many";
444
+ }
445
+ const CARDINALITY = "(?:zero-one|one|zero-many|many)";
446
+ const RELATION_TYPE = `${CARDINALITY}-to-${CARDINALITY}(?:-optional)?`;
447
+ const RELATION_RE = new RegExp(String.raw`^@relation\s+(\w+)\.(\w+)\s+(\w+)\.(\w+)\s+(${RELATION_TYPE})$`);
448
+ function parseRelation(line) {
449
+ const match = line.trim().replace(/^\/\/\/\s*/, "").match(RELATION_RE);
450
+ if (!match) return null;
451
+ const [, fromModel, fromField, toModel, toField, type] = match;
452
+ return {
453
+ fromModel,
454
+ fromField,
455
+ toModel,
456
+ toField,
457
+ type
458
+ };
459
+ }
460
+ function makeRelationLine(input) {
461
+ const parts = input.split("-to-");
462
+ if (parts.length !== 2) return {
463
+ ok: false,
464
+ error: `Invalid input format: ${input}`
465
+ };
466
+ const [to, optionalFlag] = parts[1].includes("-optional") ? [parts[1].replace("-optional", ""), "optional"] : [parts[1], ""];
467
+ const from = parts[0];
468
+ if (!isRelationshipType(from)) return {
469
+ ok: false,
470
+ error: `Invalid relationship: ${from}`
471
+ };
472
+ if (!isRelationshipType(to)) return {
473
+ ok: false,
474
+ error: `Invalid relationship: ${to}`
475
+ };
476
+ return {
477
+ ok: true,
478
+ value: `${RELATIONSHIPS[from]}${optionalFlag === "optional" ? ".." : "--"}${RELATIONSHIPS[to]}`
479
+ };
480
+ }
481
+ function extractRelationsFromAnnotations(code) {
482
+ return code.map(parseRelation).filter((r) => r !== null).flatMap((r) => {
483
+ const result = makeRelationLine(r.type);
484
+ if (!result.ok) return [];
485
+ return [{
486
+ fromModel: r.fromModel,
487
+ fromField: r.fromField,
488
+ toModel: r.toModel,
489
+ toField: r.toField,
490
+ symbol: result.value
491
+ }];
492
+ });
493
+ }
494
+ //#endregion
435
495
  //#region src/generator/arktype.ts
436
496
  function undeclared(objectType) {
437
497
  if (objectType === "strict") return "\"+\":\"reject\",";
@@ -642,8 +702,19 @@ async function emit(code, dir, output) {
642
702
  //#region src/generator/mermaid-er.ts
643
703
  function mermaidER(code) {
644
704
  const tables = parseTableInfo(code);
645
- const relations = extractRelationsFromSchema(code);
646
- const relationLines = [...new Set(relations.map((r) => ` ${r.fromModel} ||--${r.isRequired ? "}|" : "}o"} ${r.toModel} : "(${r.fromField}) - (${r.toField})"`))];
705
+ const inferred = extractRelationsFromSchema(code);
706
+ const annotated = extractRelationsFromAnnotations(code);
707
+ const key = (r) => `${r.fromModel}.${r.fromField}->${r.toModel}.${r.toField}`;
708
+ const merged = /* @__PURE__ */ new Map();
709
+ for (const r of inferred) merged.set(key(r), {
710
+ fromModel: r.fromModel,
711
+ fromField: r.fromField,
712
+ toModel: r.toModel,
713
+ toField: r.toField,
714
+ symbol: r.isRequired ? "||--}|" : "||--}o"
715
+ });
716
+ for (const r of annotated) merged.set(key(r), r);
717
+ const relationLines = [...merged.values()].map((r) => ` ${r.fromModel} ${r.symbol} ${r.toModel} : "(${r.fromField}) - (${r.toField})"`);
647
718
  const tableDefinitions = tables.flatMap((table) => [
648
719
  ` ${table.name} {`,
649
720
  ...table.fields.map((field) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sizuku",
3
- "version": "0.6.4",
3
+ "version": "0.6.5",
4
4
  "description": "Sizuku is a tool that generates validation schemas for Zod and Valibot, as well as ER diagrams and DBML, from Drizzle schemas annotated with comments.",
5
5
  "keywords": [
6
6
  "dbml",