@tandemcreativedev/strabo 0.1.1 → 0.2.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 CHANGED
@@ -5,7 +5,7 @@ Named after the Greek geographer who wrote down the known world. Strabo writes d
5
5
  Generates a readable record of a Postgres schema from `pg_dump`: a tidy SQL reference plus DBML, PlantUML and SVG diagrams. Commit the output next to your migrations so people and AI agents can read the schema without a running database.
6
6
 
7
7
  - `schema.sql`: every enum, table, view, function and trigger, grouped and sorted. Each table carries its constraints, indexes, comments, row level security and policies. It is for reading only, never for running.
8
- - `.dbml`: diagrams for [dbdiagram.io](https://dbdiagram.io), split by domain or by schema. References to tables outside a diagram stay as comments.
8
+ - `.dbml`: diagrams for [dbdiagram.io](https://dbdiagram.io), split by domain or by schema. Foreign keys sit on their column, and references to tables outside a diagram stay as comments there. Only composite foreign keys get a standalone `Ref:`, and only unique indexes not already shown on a column are listed.
9
9
  - `.puml`: the same diagrams as PlantUML entity-relationship diagrams.
10
10
  - `.svg`: the DBML diagrams rendered, with the optional `@softwaretechnik/dbml-renderer`.
11
11
 
@@ -79,6 +79,7 @@ A typical `package.json` script, run after applying migrations locally:
79
79
  - `diagrams`: one entry per diagram. `name` is the output path without an extension, relative to `outDir`.
80
80
  - `tables` lists the tables to draw, in drawing order. Bare names belong to the first schema, and `*` is a wildcard. When any diagram uses `tables`, the run warns about tables that no diagram lists, so new tables don't go undocumented.
81
81
  - Without `tables`, a diagram draws every table in `schemas`, or in every dumped schema if `schemas` is also absent.
82
+ - `enums: false` leaves enum definitions out, such as when another diagram already lists them.
82
83
  - `formats` takes any of `dbml` (the default), `puml` and `svg`.
83
84
  - `hiddenRefs`: foreign keys to leave out of diagrams, each with a note to show on the column instead. Useful for denormalised columns that would tangle a diagram.
84
85
  - `pgDump`: the command that runs `pg_dump`. Without local client tools, use the Supabase container: `"docker exec supabase_db_<project> pg_dump"`, with `databaseUrl` set to `postgresql://postgres:postgres@localhost:5432/postgres`, which is the address inside the container.
package/dist/config.d.ts CHANGED
@@ -9,6 +9,8 @@ export interface DiagramConfig {
9
9
  tables?: string[];
10
10
  /** Schemas to draw in full when `tables` is not given. Defaults to every dumped schema. */
11
11
  schemas?: string[];
12
+ /** Set false to leave enum definitions out of this diagram, such as when another diagram already lists them. */
13
+ enums?: boolean;
12
14
  /** Defaults to `["dbml"]`. `svg` renders the DBML and needs `@softwaretechnik/dbml-renderer` installed. */
13
15
  formats?: DiagramFormat[];
14
16
  }
package/dist/config.js CHANGED
@@ -29,6 +29,8 @@ function validateDiagram(value, index) {
29
29
  for (const key of ["notes", "tables", "schemas"])
30
30
  if (diagram[key] !== undefined && !isStringArray(diagram[key]))
31
31
  fail(`${at}.${key} must be an array of strings`);
32
+ if (diagram.enums !== undefined && typeof diagram.enums !== "boolean")
33
+ fail(`${at}.enums must be a boolean`);
32
34
  if (diagram.formats !== undefined) {
33
35
  if (!isStringArray(diagram.formats))
34
36
  fail(`${at}.formats must be an array of strings`);
package/dist/diagram.js CHANGED
@@ -59,7 +59,7 @@ export function buildDiagram(schema, config, primarySchema, hiddenRefs, warn) {
59
59
  notes: config.notes ?? [],
60
60
  primarySchema,
61
61
  tables,
62
- enums: enumsUsedBy(tables, schema.enums),
62
+ enums: config.enums === false ? [] : enumsUsedBy(tables, schema.enums),
63
63
  relations,
64
64
  externalRelations,
65
65
  hiddenRefs: hidden,
@@ -25,6 +25,19 @@ function defaultValue(value) {
25
25
  return `default: ${text(string[1].replace(/''/g, "'"))}`;
26
26
  return `default: \`${value.replace(/`/g, "'")}\``;
27
27
  }
28
+ function endpoint(qualified, columns, diagram) {
29
+ const names = columns.map(identifier);
30
+ return `${tableName(qualified, diagram)}.${names.length === 1 ? names[0] : `(${names.join(", ")})`}`;
31
+ }
32
+ function target(relation, diagram) {
33
+ return `${relation.cardinality === "one-to-one" ? "-" : ">"} ${endpoint(relation.refTable, relation.refColumns, diagram)}`;
34
+ }
35
+ function ref(relation, diagram) {
36
+ return `Ref: ${endpoint(relation.table, relation.columns, diagram)} ${target(relation, diagram)}`;
37
+ }
38
+ function singleColumn(relations, table, column) {
39
+ return relations.find((relation) => relation.table === table.name && relation.columns.length === 1 && relation.columns[0] === column.name);
40
+ }
28
41
  function columnSettings(table, column, diagram) {
29
42
  const settings = [];
30
43
  const singlePk = table.primaryKey.length === 1 && table.primaryKey[0] === column.name;
@@ -38,6 +51,9 @@ function columnSettings(table, column, diagram) {
38
51
  table.indexes.some((index) => index.unique && !index.partial && index.columns?.length === 1 && index.columns[0] === column.name);
39
52
  if (unique && !singlePk)
40
53
  settings.push("unique");
54
+ const relation = singleColumn(diagram.relations, table, column);
55
+ if (relation)
56
+ settings.push(`ref: ${target(relation, diagram)}`);
41
57
  const fallback = column.default === null ? null : defaultValue(column.default);
42
58
  if (fallback)
43
59
  settings.push(fallback);
@@ -55,9 +71,9 @@ function indexes(table) {
55
71
  const lines = [
56
72
  ...(table.primaryKey.length > 1 ? [`${columns(table.primaryKey)} [pk]`] : []),
57
73
  ...table.uniques.filter((unique) => unique.length > 1).map((unique) => `${columns(unique)} [unique]`),
58
- ...table.indexes.map((index) => {
74
+ ...table.indexes.filter((index) => index.unique && (index.partial || index.columns?.length !== 1)).map((index) => {
59
75
  const target = index.columns ? columns(index.columns) : `\`${index.expression}\``;
60
- const settings = [...(index.unique ? ["unique"] : []), `name: ${text(index.name)}`, ...(index.partial ? ["note: 'partial'"] : [])];
76
+ const settings = ["unique", `name: ${text(index.name)}`, ...(index.partial ? ["note: 'partial'"] : [])];
61
77
  return `${target} [${settings.join(", ")}]`;
62
78
  }),
63
79
  ];
@@ -68,7 +84,8 @@ function table(table, diagram) {
68
84
  `Table ${tableName(table.name, diagram)} {`,
69
85
  ...orderColumns(table).map((column) => {
70
86
  const settings = columnSettings(table, column, diagram);
71
- return ` ${identifier(column.name)} ${type(column, diagram)}${settings.length ? ` [${settings.join(", ")}]` : ""}`;
87
+ const external = singleColumn(diagram.externalRelations, table, column);
88
+ return ` ${identifier(column.name)} ${type(column, diagram)}${settings.length ? ` [${settings.join(", ")}]` : ""}${external ? ` // ref: ${target(external, diagram)}` : ""}`;
72
89
  }),
73
90
  ...indexes(table),
74
91
  ...(table.comment === null ? [] : ["", ` Note: ${text(table.comment)}`]),
@@ -76,13 +93,9 @@ function table(table, diagram) {
76
93
  "",
77
94
  ];
78
95
  }
79
- function endpoint(qualified, columns, diagram) {
80
- const names = columns.map(identifier);
81
- return `${tableName(qualified, diagram)}.${names.length === 1 ? names[0] : `(${names.join(", ")})`}`;
82
- }
83
- function ref(relation, diagram) {
84
- const operator = relation.cardinality === "one-to-one" ? "-" : ">";
85
- return `Ref: ${endpoint(relation.table, relation.columns, diagram)} ${operator} ${endpoint(relation.refTable, relation.refColumns, diagram)}`;
96
+ /** DBML can only reference several columns from a standalone Ref, so these stay out of the column lines. */
97
+ function composite(relations) {
98
+ return relations.filter((relation) => relation.columns.length > 1);
86
99
  }
87
100
  export function writeDbml(diagram) {
88
101
  const enums = diagram.enums.flatMap(({ name, values }) => [
@@ -98,9 +111,8 @@ export function writeDbml(diagram) {
98
111
  "",
99
112
  ...enums,
100
113
  ...diagram.tables.flatMap((entry) => table(entry, diagram)),
101
- ...diagram.relations.map((relation) => ref(relation, diagram)),
102
- ...(diagram.externalRelations.length ? ["", "// References to tables outside this diagram"] : []),
103
- ...diagram.externalRelations.map((relation) => `// ${ref(relation, diagram)}`),
114
+ ...composite(diagram.relations).map((relation) => ref(relation, diagram)),
115
+ ...composite(diagram.externalRelations).map((relation) => `// ${ref(relation, diagram)}`),
104
116
  ]
105
117
  .join("\n")
106
118
  .trimEnd()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tandemcreativedev/strabo",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Turn a Postgres schema into a readable record for people and AI: a tidy SQL reference plus DBML, PlantUML and SVG diagrams, generated from pg_dump.",
5
5
  "keywords": [
6
6
  "postgres",