@antelopejs/dms-database 0.3.1 → 0.4.1

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 (112) hide show
  1. package/README.md +17 -10
  2. package/dist/automation/index.js.map +1 -1
  3. package/dist/automation/tableWatch.d.ts +8 -4
  4. package/dist/automation/tableWatch.js +35 -8
  5. package/dist/automation/tableWatch.js.map +1 -1
  6. package/dist/db/models/diagram_layouts.model.d.ts +7 -5
  7. package/dist/db/models/diagram_notes.model.d.ts +7 -5
  8. package/dist/db/models/query_history.model.d.ts +7 -5
  9. package/dist/db/models/saved_queries.model.d.ts +7 -5
  10. package/dist/db/tables/query_history.table.d.ts +2 -1
  11. package/dist/db/tables/query_history.table.js.map +1 -1
  12. package/dist/db/tables/saved_queries.table.d.ts +2 -1
  13. package/dist/db/tables/saved_queries.table.js.map +1 -1
  14. package/dist/i18n/messages.js +5 -2
  15. package/dist/i18n/messages.js.map +1 -1
  16. package/dist/module.js +5 -1
  17. package/dist/module.js.map +1 -1
  18. package/dist/pages/schemas.js.map +1 -1
  19. package/dist/routes/data.d.ts +4 -3
  20. package/dist/routes/data.js +37 -238
  21. package/dist/routes/data.js.map +1 -1
  22. package/dist/routes/diagram.js +64 -43
  23. package/dist/routes/diagram.js.map +1 -1
  24. package/dist/routes/query.js +51 -53
  25. package/dist/routes/query.js.map +1 -1
  26. package/dist/routes/tables.js +13 -7
  27. package/dist/routes/tables.js.map +1 -1
  28. package/dist/service/browse.d.ts +37 -0
  29. package/dist/service/browse.js +135 -0
  30. package/dist/service/browse.js.map +1 -0
  31. package/dist/service/fieldDescriptor.d.ts +6 -6
  32. package/dist/service/fieldDescriptor.js +56 -50
  33. package/dist/service/fieldDescriptor.js.map +1 -1
  34. package/dist/service/health.js.map +1 -1
  35. package/dist/service/introspect.js +45 -40
  36. package/dist/service/introspect.js.map +1 -1
  37. package/dist/service/overview.js.map +1 -1
  38. package/dist/service/queryInspection.d.ts +5 -4
  39. package/dist/service/queryInspection.js +28 -26
  40. package/dist/service/queryInspection.js.map +1 -1
  41. package/dist/service/references.d.ts +1 -1
  42. package/dist/service/references.js +3 -7
  43. package/dist/service/references.js.map +1 -1
  44. package/dist/service/rowEdit.d.ts +4 -0
  45. package/dist/service/rowEdit.js +80 -0
  46. package/dist/service/rowEdit.js.map +1 -0
  47. package/dist/service/schemaFilters.d.ts +1 -1
  48. package/dist/service/schemaFilters.js.map +1 -1
  49. package/dist/service/schemaLabels.js +1 -1
  50. package/dist/service/schemaLabels.js.map +1 -1
  51. package/dist/service/schemaRegistry.d.ts +1 -1
  52. package/dist/service/schemaRegistry.js +1 -1
  53. package/dist/service/schemaRegistry.js.map +1 -1
  54. package/dist/service/stagedSerialization.d.ts +7 -6
  55. package/dist/service/stagedSerialization.js +40 -32
  56. package/dist/service/stagedSerialization.js.map +1 -1
  57. package/dist/service/types.d.ts +3 -2
  58. package/dist/types/json.d.ts +8 -0
  59. package/dist/types/json.js +53 -0
  60. package/dist/types/json.js.map +1 -0
  61. package/dist/utils/guards.d.ts +8 -0
  62. package/dist/utils/guards.js +35 -0
  63. package/dist/utils/guards.js.map +1 -0
  64. package/dist/utils/query.d.ts +6 -7
  65. package/dist/utils/query.js +22 -30
  66. package/dist/utils/query.js.map +1 -1
  67. package/dist/utils/requestValidation.d.ts +1 -2
  68. package/dist/utils/requestValidation.js +4 -9
  69. package/dist/utils/requestValidation.js.map +1 -1
  70. package/frontend-vue/app/build/components/DataGridEmpty.vue +89 -0
  71. package/frontend-vue/app/build/components/DataGridFooter.vue +113 -0
  72. package/frontend-vue/app/build/components/DataGridReadOnlyBanner.vue +50 -0
  73. package/frontend-vue/app/build/components/DataGridSaveStatus.vue +73 -0
  74. package/frontend-vue/app/build/components/DataGridToolbar.vue +72 -0
  75. package/frontend-vue/app/build/components/DiagramLegend.vue +48 -0
  76. package/frontend-vue/app/build/components/DiagramSearchBar.vue +82 -0
  77. package/frontend-vue/app/build/components/DiagramStatusBar.vue +89 -0
  78. package/frontend-vue/app/build/components/DiagramToolbar.vue +102 -0
  79. package/frontend-vue/app/build/components/DiagramViewBar.vue +103 -0
  80. package/frontend-vue/app/build/components/diagramFloat.css +21 -0
  81. package/frontend-vue/app/build/composables/useDiagramDrag.ts +84 -0
  82. package/frontend-vue/app/build/composables/useDiagramNotes.ts +146 -0
  83. package/frontend-vue/app/build/composables/useDiagramSaveStatus.ts +50 -0
  84. package/frontend-vue/app/build/composables/useDiagramSearch.ts +77 -0
  85. package/frontend-vue/app/build/composables/useDiagramView.ts +125 -0
  86. package/frontend-vue/app/build/composables/useElsewhereInstances.ts +94 -0
  87. package/frontend-vue/app/build/composables/useTableSample.ts +176 -0
  88. package/frontend-vue/app/build/data/gridData.ts +66 -0
  89. package/frontend-vue/app/build/data/useStagedSave.ts +13 -20
  90. package/frontend-vue/app/build/diagram/autosave.ts +3 -3
  91. package/frontend-vue/app/build/diagram/graph.ts +67 -485
  92. package/frontend-vue/app/build/diagram/graphModel.ts +368 -0
  93. package/frontend-vue/app/build/diagram/layout.ts +135 -0
  94. package/frontend-vue/app/build/query/failures.ts +17 -0
  95. package/frontend-vue/app/components/DataGrid.vue +64 -358
  96. package/frontend-vue/app/components/QueryConsole.vue +27 -116
  97. package/frontend-vue/app/components/SchemaDiagram.vue +118 -678
  98. package/frontend-vue/app/components/TableInspector.vue +12 -136
  99. package/frontend-vue/build/backend-boundary.ts +122 -0
  100. package/frontend-vue/dms.frontend.build.ts +6 -0
  101. package/frontend-vue/package.json +46 -45
  102. package/frontend-vue/pnpm-lock.yaml +51 -16
  103. package/frontend-vue/tests/apiErrorPresentation.test.ts +83 -0
  104. package/frontend-vue/tests/backend-boundary.test.ts +134 -0
  105. package/frontend-vue/tests/clientNavigation.test.ts +5 -7
  106. package/frontend-vue/tests/fixtures/serverRender.ts +13 -1
  107. package/frontend-vue/tests/locales.test.ts +16 -89
  108. package/frontend-vue/tests/queryConsole.test.ts +20 -0
  109. package/frontend-vue/tests/registration.test.ts +0 -18
  110. package/frontend-vue/vitest.config.ts +5 -2
  111. package/package.json +28 -17
  112. package/skills/dms-database/cases.json +48 -0
@@ -0,0 +1,368 @@
1
+ import { MarkerType } from '@vue-flow/core'
2
+ import type {
3
+ SchemaSummary,
4
+ TableSummary,
5
+ } from '../composables/useDatabaseSchemas'
6
+ import type { DiagramPositions } from '../composables/useDiagramPersistence'
7
+ import { isPrimaryKey } from '../utils/fieldTypes'
8
+
9
+ export const NODE_WIDTH = 232
10
+ const NODE_HEIGHT_PER_FIELD = 24
11
+ const NODE_HEIGHT_HEADER = 44
12
+ export const STUB_NODE_WIDTH = 180
13
+ export const STUB_NODE_HEIGHT = 28
14
+ export const LAYOUT = { rankdir: 'LR', nodesep: 48, ranksep: 120 } as const
15
+ /** Columns a folded table shows, besides the ones an arrow ends on. */
16
+ export const FOLDED_COLUMNS = 12
17
+ const HYSTERESIS_PX = 15
18
+ // Notes sit above the tables, so a new one is never hidden under a table.
19
+ export const NOTE_Z_INDEX = 10
20
+ // Arrow colours, as theme tokens so they follow the light and dark themes.
21
+ export const EDGE_COLOR = 'var(--ui-border-accented)'
22
+ export const EDGE_MANY_COLOR = 'var(--color-dms-500)'
23
+ export const EDGE_ACTIVE_COLOR = 'var(--ui-primary)'
24
+
25
+ export const TABLE_NODE_TYPE = 'tableNode'
26
+ export const STUB_NODE_TYPE = 'stubNode'
27
+ export const NOTE_NODE_TYPE = 'noteNode'
28
+ export const RELATION_EDGE_TYPE = 'relationEdge'
29
+
30
+ export interface NoteDraft {
31
+ id: string
32
+ schemaName: string
33
+ text: string
34
+ x: number
35
+ y: number
36
+ width: number
37
+ height: number
38
+ color: string | null
39
+ /** Not stored yet: created on the canvas, waiting for its save. */
40
+ isTemp: boolean
41
+ }
42
+
43
+ export interface TableNodeData {
44
+ schemaId: string
45
+ tableName: string
46
+ table: TableSummary
47
+ /** Columns of the selected table's relations, drawn highlighted. */
48
+ highlighted: string[]
49
+ dimmed: boolean
50
+ /** Picked on the canvas, by a search or from a link. */
51
+ selected: boolean
52
+ /** The columns drawn, in the table's order: all of them when unfolded. */
53
+ columns: string[]
54
+ /** Columns the folded table leaves out; 0 when it shows them all. */
55
+ hiddenCount: number
56
+ /** Has more columns than a folded table shows. */
57
+ foldable: boolean
58
+ expanded: boolean
59
+ onToggleColumns: (tableName: string) => void
60
+ }
61
+
62
+ export interface NoteNodeData {
63
+ noteId: string
64
+ text: string
65
+ width: number
66
+ height: number
67
+ /** Opens the note in edit mode when it mounts: a note just created. */
68
+ autoEdit: boolean
69
+ onTextChange: (noteId: string, text: string) => void
70
+ onDelete: (noteId: string) => void
71
+ /** The note left edit mode, changed or not. */
72
+ onEditEnd: (noteId: string) => void
73
+ }
74
+
75
+ export interface DiagramNode {
76
+ id: string
77
+ type: string
78
+ position: { x: number; y: number }
79
+ selectable?: boolean
80
+ draggable?: boolean
81
+ deletable?: boolean
82
+ zIndex?: number
83
+ data: Record<string, unknown>
84
+ }
85
+
86
+ export interface EdgeEnds {
87
+ sourceBase: string
88
+ targetBase: string
89
+ isStubTarget: boolean
90
+ active: boolean
91
+ }
92
+
93
+ export interface DiagramEdge {
94
+ id: string
95
+ type: typeof RELATION_EDGE_TYPE
96
+ source: string
97
+ target: string
98
+ sourceHandle: string
99
+ targetHandle: string
100
+ label: string
101
+ animated: boolean
102
+ markerEnd: { type: MarkerType; color: string }
103
+ data: EdgeEnds
104
+ }
105
+
106
+ export type HandleSide = 'left' | 'right'
107
+ export interface Sides {
108
+ source: HandleSide
109
+ target: HandleSide
110
+ }
111
+
112
+ /** Opens a table, in its own schema's diagram when it belongs to another. */
113
+ export type OpenTable = (schemaId: string, tableName: string) => void
114
+
115
+ /** Where dagre puts each table of a schema, and each stub of another one. */
116
+ export interface SchemaLayout {
117
+ tables: DiagramPositions
118
+ /** Keyed by stub node id. */
119
+ stubs: DiagramPositions
120
+ }
121
+
122
+ export interface GraphInput {
123
+ schema: SchemaSummary
124
+ positions: DiagramPositions
125
+ notes: NoteDraft[]
126
+ /** Node id of the selected table: its edges and columns light up. */
127
+ selectedId: string | null
128
+ /** When set, only these tables are drawn (a table and its neighbours). */
129
+ focusIds: Set<string> | null
130
+ noteHandlers: Pick<NoteNodeData, 'onTextChange' | 'onDelete' | 'onEditEnd'>
131
+ /** The note to open in edit mode: the one just created. */
132
+ editNoteId?: string | null
133
+ /** Opens the table a stub stands for. */
134
+ onOpenSchema: OpenTable
135
+ /** Sides each edge used last, so an edge does not flip on every drag. */
136
+ routing: Map<string, Sides>
137
+ /**
138
+ * Where the layout puts every table of the whole schema; computed when
139
+ * absent. The canvas keeps it per schema, so a render does not lay out
140
+ * again.
141
+ */
142
+ layout?: SchemaLayout
143
+ /** Tables showing all their columns; the others are folded. */
144
+ expanded?: Set<string>
145
+ onToggleColumns?: (tableName: string) => void
146
+ }
147
+
148
+ export interface BuiltGraph {
149
+ nodes: DiagramNode[]
150
+ edges: DiagramEdge[]
151
+ }
152
+
153
+ export function tableNodeId(schemaId: string, tableName: string): string {
154
+ return `${schemaId}::${tableName}`
155
+ }
156
+
157
+ export function stubNodeId(schemaId: string, tableName: string): string {
158
+ return `stub::${schemaId}::${tableName}`
159
+ }
160
+
161
+ export function noteNodeId(noteId: string): string {
162
+ return `note::${noteId}`
163
+ }
164
+
165
+ /**
166
+ * Columns of `table` an arrow ends on: its primary key, its relation
167
+ * columns, and the columns relations of `schema` point at.
168
+ */
169
+ function anchorColumns(
170
+ schema: SchemaSummary,
171
+ table: TableSummary,
172
+ ): Set<string> {
173
+ const anchors = new Set(table.relations.map((r) => r.fromField))
174
+ for (const field of Object.keys(table.fields))
175
+ if (isPrimaryKey(field, table.indexes ?? {})) anchors.add(field)
176
+ for (const other of schema.tables) {
177
+ for (const relation of other.relations) {
178
+ if (relation.toSchema === schema.id && relation.toTable === table.name)
179
+ anchors.add(relation.toField)
180
+ }
181
+ }
182
+ return anchors
183
+ }
184
+
185
+ export interface ShownColumns {
186
+ columns: string[]
187
+ hiddenCount: number
188
+ foldable: boolean
189
+ }
190
+
191
+ /**
192
+ * The columns a table node draws: every one when expanded; folded, the first
193
+ * FOLDED_COLUMNS and every column an arrow ends on, so no edge points to a
194
+ * row that is not drawn.
195
+ */
196
+ export function shownColumns(
197
+ schema: SchemaSummary,
198
+ table: TableSummary,
199
+ expanded: boolean,
200
+ ): ShownColumns {
201
+ const all = Object.keys(table.fields)
202
+ const foldable = all.length > FOLDED_COLUMNS
203
+ if (!foldable || expanded) return { columns: all, hiddenCount: 0, foldable }
204
+ const anchors = anchorColumns(schema, table)
205
+ const columns = all.filter(
206
+ (name, i) => i < FOLDED_COLUMNS || anchors.has(name),
207
+ )
208
+ return { columns, hiddenCount: all.length - columns.length, foldable }
209
+ }
210
+
211
+ /**
212
+ * The height of a table node: its header, its rows, and the row that folds
213
+ * or unfolds it when it has one.
214
+ */
215
+ export function nodeHeight(
216
+ schema: SchemaSummary,
217
+ table: TableSummary,
218
+ expanded = false,
219
+ ): number {
220
+ const shown = shownColumns(schema, table, expanded)
221
+ const rows = shown.columns.length + (shown.foldable ? 1 : 0)
222
+ return NODE_HEIGHT_HEADER + rows * NODE_HEIGHT_PER_FIELD
223
+ }
224
+
225
+ /** Column names of `table` that hold `needle`, as the search reads it. */
226
+ export function matchingColumns(table: TableSummary, needle: string): string[] {
227
+ const text = needle.trim().toLowerCase()
228
+ if (!text) return []
229
+ return Object.keys(table.fields).filter((field) =>
230
+ field.toLowerCase().includes(text),
231
+ )
232
+ }
233
+
234
+ /**
235
+ * The expanded tables once a search lands on `tableName`: it unfolds when a
236
+ * column the search finds is folded away.
237
+ */
238
+ export function expandForSearch(
239
+ schema: SchemaSummary,
240
+ tableName: string,
241
+ needle: string,
242
+ expanded: Set<string>,
243
+ ): Set<string> {
244
+ const table = schema.tables.find((t) => t.name === tableName)
245
+ if (!table || expanded.has(tableName)) return expanded
246
+ const shown = new Set(shownColumns(schema, table, false).columns)
247
+ const folded = matchingColumns(table, needle).some((c) => !shown.has(c))
248
+ return folded ? new Set([...expanded, tableName]) : expanded
249
+ }
250
+
251
+ /** Ids of a table and of every table it points to or is pointed at from. */
252
+ export function neighbourIds(
253
+ schema: SchemaSummary,
254
+ tableName: string,
255
+ ): Set<string> {
256
+ const ids = new Set([tableNodeId(schema.id, tableName)])
257
+ for (const table of schema.tables) {
258
+ for (const relation of table.relations) {
259
+ if (table.name === tableName)
260
+ ids.add(tableNodeId(relation.toSchema, relation.toTable))
261
+ if (relation.toSchema === schema.id && relation.toTable === tableName) {
262
+ ids.add(tableNodeId(schema.id, table.name))
263
+ }
264
+ }
265
+ }
266
+ return ids
267
+ }
268
+
269
+ /**
270
+ * Picks the handle sides of an edge from the horizontal distance between its
271
+ * nodes, keeping the previous sides within a small margin.
272
+ */
273
+ export function pickSides(dx: number, width: number, previous?: Sides): Sides {
274
+ const pure: Sides =
275
+ dx >= width
276
+ ? { source: 'right', target: 'left' }
277
+ : dx >= 0
278
+ ? { source: 'right', target: 'right' }
279
+ : dx >= -width
280
+ ? { source: 'left', target: 'left' }
281
+ : { source: 'left', target: 'right' }
282
+ if (!previous) return pure
283
+ const keepRanges: Record<string, [number, number]> = {
284
+ 'right-left': [width - HYSTERESIS_PX, Infinity],
285
+ 'right-right': [-HYSTERESIS_PX, width + HYSTERESIS_PX],
286
+ 'left-left': [-width - HYSTERESIS_PX, HYSTERESIS_PX],
287
+ 'left-right': [-Infinity, -width + HYSTERESIS_PX],
288
+ }
289
+ const [min, max] = keepRanges[`${previous.source}-${previous.target}`] ?? [
290
+ 0, 0,
291
+ ]
292
+ return dx > min && dx < max ? previous : pure
293
+ }
294
+
295
+ export interface PendingEdge {
296
+ id: string
297
+ sourceId: string
298
+ targetId: string
299
+ ends: EdgeEnds
300
+ label: string
301
+ many: boolean
302
+ }
303
+
304
+ export interface StubTarget {
305
+ schema: string
306
+ table: string
307
+ }
308
+
309
+ export interface Relations {
310
+ stubs: Map<string, StubTarget>
311
+ pending: PendingEdge[]
312
+ }
313
+
314
+ export function relationEdges(
315
+ schema: SchemaSummary,
316
+ selectedId: string | null,
317
+ visible: Set<string>,
318
+ ): Relations {
319
+ const stubs = new Map<string, StubTarget>()
320
+ const pending: PendingEdge[] = []
321
+ for (const table of schema.tables) {
322
+ const sourceId = tableNodeId(schema.id, table.name)
323
+ if (!visible.has(sourceId)) continue
324
+ for (const relation of table.relations) {
325
+ const realTargetId = tableNodeId(relation.toSchema, relation.toTable)
326
+ const isStubTarget = !visible.has(realTargetId)
327
+ const targetId = isStubTarget
328
+ ? stubNodeId(relation.toSchema, relation.toTable)
329
+ : realTargetId
330
+ if (isStubTarget && !stubs.has(targetId)) {
331
+ stubs.set(targetId, {
332
+ schema: relation.toSchema,
333
+ table: relation.toTable,
334
+ })
335
+ }
336
+ const target = schema.tables.find((t) => t.name === relation.toTable)
337
+ pending.push({
338
+ id: `${sourceId}::${relation.fromField}->${realTargetId}`,
339
+ sourceId,
340
+ targetId,
341
+ label: relation.fromField,
342
+ many: relation.many,
343
+ ends: {
344
+ sourceBase:
345
+ relation.fromField in table.fields
346
+ ? relation.fromField
347
+ : '__node__',
348
+ targetBase: isStubTarget
349
+ ? 'stub'
350
+ : target && relation.toField in target.fields
351
+ ? relation.toField
352
+ : '__node__',
353
+ isStubTarget,
354
+ active:
355
+ selectedId !== null &&
356
+ (selectedId === sourceId || selectedId === realTargetId),
357
+ },
358
+ })
359
+ }
360
+ }
361
+ return { stubs, pending }
362
+ }
363
+
364
+ export interface StubNodeData {
365
+ targetSchema: string
366
+ targetTable: string
367
+ onOpenSchema: OpenTable
368
+ }
@@ -0,0 +1,135 @@
1
+ import dagre from '@dagrejs/dagre'
2
+ import type {
3
+ DiagramPosition,
4
+ DiagramPositions,
5
+ } from '../composables/useDiagramPersistence'
6
+ import type { SchemaSummary } from '../composables/useDatabaseSchemas'
7
+ import {
8
+ type Block,
9
+ boundsOf,
10
+ DEFAULT_ASPECT,
11
+ linkedGroups,
12
+ packBlocks,
13
+ prefixOrder,
14
+ type Rect,
15
+ type Size,
16
+ } from './packing'
17
+ import {
18
+ LAYOUT,
19
+ NODE_WIDTH,
20
+ nodeHeight,
21
+ relationEdges,
22
+ type SchemaLayout,
23
+ STUB_NODE_HEIGHT,
24
+ STUB_NODE_WIDTH,
25
+ tableNodeId,
26
+ } from './graphModel'
27
+
28
+ /** A node as dagre lays it out: its centre and size. */
29
+ interface LaidNode {
30
+ x: number
31
+ y: number
32
+ width: number
33
+ height: number
34
+ }
35
+
36
+ export interface LayoutOptions {
37
+ /** Width / height of the canvas: the shape the rows aim at. */
38
+ aspect?: number
39
+ /** What the tables must not cover: the notes, which never move. */
40
+ obstacles?: Rect[]
41
+ }
42
+
43
+ interface LaidGroup extends Size {
44
+ /** Each node's top-left corner, from the group's own. */
45
+ offsets: Map<string, DiagramPosition>
46
+ }
47
+
48
+ /** Lays out one group of related nodes with dagre. */
49
+ function layOutGroup(
50
+ ids: string[],
51
+ sizes: Map<string, Size>,
52
+ links: [string, string][],
53
+ ): LaidGroup {
54
+ const graph = new dagre.graphlib.Graph()
55
+ graph.setDefaultEdgeLabel(() => ({}))
56
+ graph.setGraph({ ...LAYOUT })
57
+ const members = new Set(ids)
58
+ for (const id of ids) graph.setNode(id, { ...sizes.get(id) })
59
+ for (const [from, to] of links)
60
+ if (members.has(from) && members.has(to)) graph.setEdge(from, to)
61
+ dagre.layout(graph)
62
+ const rects = ids.map((id) => {
63
+ const laid = graph.node(id) as LaidNode
64
+ const x = laid.x - laid.width / 2
65
+ const y = laid.y - laid.height / 2
66
+ return { id, x, y, width: laid.width, height: laid.height }
67
+ })
68
+ const bounds = boundsOf(rects)
69
+ const offsets = new Map(
70
+ rects.map((r) => [r.id, { x: r.x - bounds.x, y: r.y - bounds.y }]),
71
+ )
72
+ return { offsets, width: bounds.width, height: bounds.height }
73
+ }
74
+
75
+ /**
76
+ * Lays out the whole schema, every table and a stub for each table of
77
+ * another schema a relation points to: dagre on each group of tables linked
78
+ * by relations, a stub staying with the tables it hangs from, then the
79
+ * groups, largest first, and the tables on their own, by name prefix then
80
+ * name, in rows shaped like the canvas. Tables count at their folded height.
81
+ */
82
+ export function layOutSchema(
83
+ schema: SchemaSummary,
84
+ options: LayoutOptions = {},
85
+ ): SchemaLayout {
86
+ const sizes = new Map<string, Size>()
87
+ const names = new Map<string, string>()
88
+ for (const table of schema.tables) {
89
+ const id = tableNodeId(schema.id, table.name)
90
+ names.set(id, table.name)
91
+ sizes.set(id, { width: NODE_WIDTH, height: nodeHeight(schema, table) })
92
+ }
93
+ const { stubs, pending } = relationEdges(schema, null, new Set(names.keys()))
94
+ for (const id of stubs.keys())
95
+ sizes.set(id, { width: STUB_NODE_WIDTH, height: STUB_NODE_HEIGHT })
96
+ const links = pending.map((e): [string, string] => [e.sourceId, e.targetId])
97
+
98
+ const groups: (Block & LaidGroup & { count: number })[] = []
99
+ const alone: string[] = []
100
+ for (const ids of linkedGroups([...sizes.keys()], links)) {
101
+ const [first = ''] = ids
102
+ if (ids.length === 1 && names.has(first)) {
103
+ alone.push(names.get(first) ?? first)
104
+ continue
105
+ }
106
+ const laid = layOutGroup(ids, sizes, links)
107
+ groups.push({ id: `group::${first}`, count: ids.length, ...laid })
108
+ }
109
+ groups.sort(
110
+ (a, b) => b.count - a.count || b.width * b.height - a.width * a.height,
111
+ )
112
+ const single: Block[] = prefixOrder(alone).map((name) => {
113
+ const id = tableNodeId(schema.id, name)
114
+ return { id, ...(sizes.get(id) as Size) }
115
+ })
116
+
117
+ const packed = packBlocks(
118
+ [...groups, ...single],
119
+ options.aspect ?? DEFAULT_ASPECT,
120
+ options.obstacles,
121
+ )
122
+ const positions = new Map(packed)
123
+ for (const group of groups) {
124
+ const at = packed.get(group.id) ?? { x: 0, y: 0 }
125
+ for (const [id, offset] of group.offsets)
126
+ positions.set(id, { x: at.x + offset.x, y: at.y + offset.y })
127
+ }
128
+ const tables: DiagramPositions = {}
129
+ for (const [id, name] of names)
130
+ tables[name] = positions.get(id) ?? { x: 0, y: 0 }
131
+ const stubPositions: DiagramPositions = {}
132
+ for (const id of stubs.keys())
133
+ stubPositions[id] = positions.get(id) ?? { x: 0, y: 0 }
134
+ return { tables, stubs: stubPositions }
135
+ }
@@ -0,0 +1,17 @@
1
+ /** The query routes' 400 diagnostics stay inline instead of becoming toasts. */
2
+ export function sqlFailure(error: unknown) {
3
+ if (typeof error !== 'object' || error === null) return null
4
+ const status = 'statusCode' in error ? error.statusCode : undefined
5
+ if (status !== 400) return null
6
+ const data = 'data' in error ? error.data : undefined
7
+ if (typeof data === 'string' && data)
8
+ return { message: data, durationMs: null }
9
+ if (typeof data !== 'object' || data === null) return null
10
+ const message = 'message' in data ? data.message : undefined
11
+ if (typeof message !== 'string' || !message) return null
12
+ const duration = 'durationMs' in data ? data.durationMs : undefined
13
+ return {
14
+ message,
15
+ durationMs: typeof duration === 'number' ? duration : null,
16
+ }
17
+ }