@antelopejs/dms-database 0.3.0 → 0.4.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 +19 -7
- package/dist/automation/index.js.map +1 -1
- package/dist/automation/tableWatch.d.ts +3 -2
- package/dist/automation/tableWatch.js +9 -6
- package/dist/automation/tableWatch.js.map +1 -1
- package/dist/db/tables/query_history.table.d.ts +2 -1
- package/dist/db/tables/query_history.table.js.map +1 -1
- package/dist/db/tables/saved_queries.table.d.ts +2 -1
- package/dist/db/tables/saved_queries.table.js.map +1 -1
- package/dist/i18n/messages.js.map +1 -1
- package/dist/module.js +5 -1
- package/dist/module.js.map +1 -1
- package/dist/pages/schemas.js.map +1 -1
- package/dist/routes/data.d.ts +1 -1
- package/dist/routes/data.js +36 -237
- package/dist/routes/data.js.map +1 -1
- package/dist/routes/diagram.js +61 -42
- package/dist/routes/diagram.js.map +1 -1
- package/dist/routes/query.js +44 -46
- package/dist/routes/query.js.map +1 -1
- package/dist/routes/tables.js +13 -7
- package/dist/routes/tables.js.map +1 -1
- package/dist/service/browse.d.ts +37 -0
- package/dist/service/browse.js +135 -0
- package/dist/service/browse.js.map +1 -0
- package/dist/service/fieldDescriptor.d.ts +6 -6
- package/dist/service/fieldDescriptor.js +9 -13
- package/dist/service/fieldDescriptor.js.map +1 -1
- package/dist/service/health.js.map +1 -1
- package/dist/service/introspect.js +13 -12
- package/dist/service/introspect.js.map +1 -1
- package/dist/service/overview.js.map +1 -1
- package/dist/service/queryInspection.d.ts +4 -3
- package/dist/service/queryInspection.js +25 -24
- package/dist/service/queryInspection.js.map +1 -1
- package/dist/service/references.js +2 -6
- package/dist/service/references.js.map +1 -1
- package/dist/service/rowEdit.d.ts +2 -0
- package/dist/service/rowEdit.js +81 -0
- package/dist/service/rowEdit.js.map +1 -0
- package/dist/service/schemaFilters.d.ts +1 -1
- package/dist/service/schemaFilters.js.map +1 -1
- package/dist/service/schemaRegistry.js.map +1 -1
- package/dist/service/stagedSerialization.d.ts +7 -6
- package/dist/service/stagedSerialization.js +29 -21
- package/dist/service/stagedSerialization.js.map +1 -1
- package/dist/types/json.d.ts +8 -0
- package/dist/types/json.js +32 -0
- package/dist/types/json.js.map +1 -0
- package/dist/utils/guards.d.ts +7 -0
- package/dist/utils/guards.js +31 -0
- package/dist/utils/guards.js.map +1 -0
- package/dist/utils/query.d.ts +6 -7
- package/dist/utils/query.js +13 -24
- package/dist/utils/query.js.map +1 -1
- package/dist/utils/requestValidation.d.ts +1 -2
- package/dist/utils/requestValidation.js +4 -9
- package/dist/utils/requestValidation.js.map +1 -1
- package/frontend-vue/app/build/components/DataGridEmpty.vue +89 -0
- package/frontend-vue/app/build/components/DataGridFooter.vue +113 -0
- package/frontend-vue/app/build/components/DataGridReadOnlyBanner.vue +50 -0
- package/frontend-vue/app/build/components/DataGridSaveStatus.vue +73 -0
- package/frontend-vue/app/build/components/DataGridToolbar.vue +72 -0
- package/frontend-vue/app/build/components/DiagramLegend.vue +48 -0
- package/frontend-vue/app/build/components/DiagramSearchBar.vue +82 -0
- package/frontend-vue/app/build/components/DiagramStatusBar.vue +89 -0
- package/frontend-vue/app/build/components/DiagramToolbar.vue +102 -0
- package/frontend-vue/app/build/components/DiagramViewBar.vue +103 -0
- package/frontend-vue/app/build/components/diagramFloat.css +21 -0
- package/frontend-vue/app/build/composables/useDiagramDrag.ts +84 -0
- package/frontend-vue/app/build/composables/useDiagramNotes.ts +146 -0
- package/frontend-vue/app/build/composables/useDiagramSaveStatus.ts +50 -0
- package/frontend-vue/app/build/composables/useDiagramSearch.ts +77 -0
- package/frontend-vue/app/build/composables/useDiagramView.ts +125 -0
- package/frontend-vue/app/build/composables/useElsewhereInstances.ts +94 -0
- package/frontend-vue/app/build/composables/useTableSample.ts +176 -0
- package/frontend-vue/app/build/data/gridData.ts +66 -0
- package/frontend-vue/app/build/data/useStagedSave.ts +13 -20
- package/frontend-vue/app/build/diagram/autosave.ts +3 -3
- package/frontend-vue/app/build/diagram/graph.ts +67 -485
- package/frontend-vue/app/build/diagram/graphModel.ts +368 -0
- package/frontend-vue/app/build/diagram/layout.ts +135 -0
- package/frontend-vue/app/build/query/failures.ts +17 -0
- package/frontend-vue/app/components/DataGrid.vue +64 -358
- package/frontend-vue/app/components/QueryConsole.vue +27 -116
- package/frontend-vue/app/components/SchemaDiagram.vue +118 -678
- package/frontend-vue/app/components/TableInspector.vue +12 -136
- package/frontend-vue/build/backend-boundary.ts +122 -0
- package/frontend-vue/dms.frontend.build.ts +6 -0
- package/frontend-vue/package.json +3 -2
- package/frontend-vue/pnpm-lock.yaml +8 -5
- package/frontend-vue/tests/apiErrorPresentation.test.ts +83 -0
- package/frontend-vue/tests/backend-boundary.test.ts +134 -0
- package/frontend-vue/tests/clientNavigation.test.ts +5 -7
- package/frontend-vue/tests/fixtures/serverRender.ts +13 -1
- package/frontend-vue/tests/locales.test.ts +16 -89
- package/frontend-vue/tests/queryConsole.test.ts +20 -0
- package/frontend-vue/tests/registration.test.ts +0 -18
- package/frontend-vue/vitest.config.ts +5 -2
- package/package.json +23 -12
- package/skills/dms-database/SKILL.md +1 -1
- package/skills/dms-database/cases.json +48 -0
|
@@ -1,25 +1,39 @@
|
|
|
1
|
-
import dagre from '@dagrejs/dagre'
|
|
2
1
|
import { MarkerType } from '@vue-flow/core'
|
|
3
2
|
import type {
|
|
4
3
|
SchemaSummary,
|
|
5
4
|
TableSummary,
|
|
6
5
|
} from '../composables/useDatabaseSchemas'
|
|
7
|
-
import type {
|
|
8
|
-
|
|
9
|
-
DiagramPositions,
|
|
10
|
-
} from '../composables/useDiagramPersistence'
|
|
11
|
-
import { isPrimaryKey } from '../utils/fieldTypes'
|
|
6
|
+
import type { DiagramPosition } from '../composables/useDiagramPersistence'
|
|
7
|
+
import { overlaps, type Rect } from './packing'
|
|
12
8
|
import {
|
|
13
|
-
type
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
9
|
+
type BuiltGraph,
|
|
10
|
+
EDGE_ACTIVE_COLOR,
|
|
11
|
+
EDGE_COLOR,
|
|
12
|
+
EDGE_MANY_COLOR,
|
|
13
|
+
type DiagramEdge,
|
|
14
|
+
type DiagramNode,
|
|
15
|
+
type GraphInput,
|
|
16
|
+
matchingColumns,
|
|
17
|
+
NODE_WIDTH,
|
|
18
|
+
NOTE_NODE_TYPE,
|
|
19
|
+
NOTE_Z_INDEX,
|
|
20
|
+
noteNodeId,
|
|
21
|
+
type NoteNodeData,
|
|
22
|
+
type PendingEdge,
|
|
23
|
+
pickSides,
|
|
24
|
+
RELATION_EDGE_TYPE,
|
|
25
|
+
relationEdges,
|
|
26
|
+
type SchemaLayout,
|
|
27
|
+
shownColumns,
|
|
28
|
+
STUB_NODE_TYPE,
|
|
29
|
+
STUB_NODE_WIDTH,
|
|
30
|
+
type StubNodeData,
|
|
31
|
+
type StubTarget,
|
|
32
|
+
tableNodeId,
|
|
33
|
+
TABLE_NODE_TYPE,
|
|
34
|
+
type TableNodeData,
|
|
35
|
+
} from './graphModel'
|
|
36
|
+
import { layOutSchema } from './layout'
|
|
23
37
|
|
|
24
38
|
// Turns a schema into the nodes and edges of the diagram: one node per table,
|
|
25
39
|
// a dashed stub for a table a relation points to that is not drawn, the
|
|
@@ -29,469 +43,43 @@ import {
|
|
|
29
43
|
// That layout runs dagre on each group of related tables, then sets the
|
|
30
44
|
// groups and the tables on their own in rows shaped like the canvas.
|
|
31
45
|
|
|
32
|
-
export
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
table: TableSummary
|
|
70
|
-
/** Columns of the selected table's relations, drawn highlighted. */
|
|
71
|
-
highlighted: string[]
|
|
72
|
-
dimmed: boolean
|
|
73
|
-
/** Picked on the canvas, by a search or from a link. */
|
|
74
|
-
selected: boolean
|
|
75
|
-
/** The columns drawn, in the table's order: all of them when unfolded. */
|
|
76
|
-
columns: string[]
|
|
77
|
-
/** Columns the folded table leaves out; 0 when it shows them all. */
|
|
78
|
-
hiddenCount: number
|
|
79
|
-
/** Has more columns than a folded table shows. */
|
|
80
|
-
foldable: boolean
|
|
81
|
-
expanded: boolean
|
|
82
|
-
onToggleColumns: (tableName: string) => void
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export interface NoteNodeData {
|
|
86
|
-
noteId: string
|
|
87
|
-
text: string
|
|
88
|
-
width: number
|
|
89
|
-
height: number
|
|
90
|
-
/** Opens the note in edit mode when it mounts: a note just created. */
|
|
91
|
-
autoEdit: boolean
|
|
92
|
-
onTextChange: (noteId: string, text: string) => void
|
|
93
|
-
onDelete: (noteId: string) => void
|
|
94
|
-
/** The note left edit mode, changed or not. */
|
|
95
|
-
onEditEnd: (noteId: string) => void
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export interface DiagramNode {
|
|
99
|
-
id: string
|
|
100
|
-
type: string
|
|
101
|
-
position: { x: number; y: number }
|
|
102
|
-
selectable?: boolean
|
|
103
|
-
draggable?: boolean
|
|
104
|
-
deletable?: boolean
|
|
105
|
-
zIndex?: number
|
|
106
|
-
data: Record<string, unknown>
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export interface EdgeEnds {
|
|
110
|
-
sourceBase: string
|
|
111
|
-
targetBase: string
|
|
112
|
-
isStubTarget: boolean
|
|
113
|
-
active: boolean
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export interface DiagramEdge {
|
|
117
|
-
id: string
|
|
118
|
-
type: typeof RELATION_EDGE_TYPE
|
|
119
|
-
source: string
|
|
120
|
-
target: string
|
|
121
|
-
sourceHandle: string
|
|
122
|
-
targetHandle: string
|
|
123
|
-
label: string
|
|
124
|
-
animated: boolean
|
|
125
|
-
markerEnd: { type: MarkerType; color: string }
|
|
126
|
-
data: EdgeEnds
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
type HandleSide = 'left' | 'right'
|
|
130
|
-
export interface Sides {
|
|
131
|
-
source: HandleSide
|
|
132
|
-
target: HandleSide
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/** Opens a table, in its own schema's diagram when it belongs to another. */
|
|
136
|
-
export type OpenTable = (schemaId: string, tableName: string) => void
|
|
137
|
-
|
|
138
|
-
/** Where dagre puts each table of a schema, and each stub of another one. */
|
|
139
|
-
export interface SchemaLayout {
|
|
140
|
-
tables: DiagramPositions
|
|
141
|
-
/** Keyed by stub node id. */
|
|
142
|
-
stubs: DiagramPositions
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
export interface GraphInput {
|
|
146
|
-
schema: SchemaSummary
|
|
147
|
-
positions: DiagramPositions
|
|
148
|
-
notes: NoteDraft[]
|
|
149
|
-
/** Node id of the selected table: its edges and columns light up. */
|
|
150
|
-
selectedId: string | null
|
|
151
|
-
/** When set, only these tables are drawn (a table and its neighbours). */
|
|
152
|
-
focusIds: Set<string> | null
|
|
153
|
-
noteHandlers: Pick<NoteNodeData, 'onTextChange' | 'onDelete' | 'onEditEnd'>
|
|
154
|
-
/** The note to open in edit mode: the one just created. */
|
|
155
|
-
editNoteId?: string | null
|
|
156
|
-
/** Opens the table a stub stands for. */
|
|
157
|
-
onOpenSchema: OpenTable
|
|
158
|
-
/** Sides each edge used last, so an edge does not flip on every drag. */
|
|
159
|
-
routing: Map<string, Sides>
|
|
160
|
-
/**
|
|
161
|
-
* Where the layout puts every table of the whole schema; computed when
|
|
162
|
-
* absent. The canvas keeps it per schema, so a render does not lay out
|
|
163
|
-
* again.
|
|
164
|
-
*/
|
|
165
|
-
layout?: SchemaLayout
|
|
166
|
-
/** Tables showing all their columns; the others are folded. */
|
|
167
|
-
expanded?: Set<string>
|
|
168
|
-
onToggleColumns?: (tableName: string) => void
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
export interface BuiltGraph {
|
|
172
|
-
nodes: DiagramNode[]
|
|
173
|
-
edges: DiagramEdge[]
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
export function tableNodeId(schemaId: string, tableName: string): string {
|
|
177
|
-
return `${schemaId}::${tableName}`
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
function stubNodeId(schemaId: string, tableName: string): string {
|
|
181
|
-
return `stub::${schemaId}::${tableName}`
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
export function noteNodeId(noteId: string): string {
|
|
185
|
-
return `note::${noteId}`
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Columns of `table` an arrow ends on: its primary key, its relation
|
|
190
|
-
* columns, and the columns relations of `schema` point at.
|
|
191
|
-
*/
|
|
192
|
-
function anchorColumns(
|
|
193
|
-
schema: SchemaSummary,
|
|
194
|
-
table: TableSummary,
|
|
195
|
-
): Set<string> {
|
|
196
|
-
const anchors = new Set(table.relations.map((r) => r.fromField))
|
|
197
|
-
for (const field of Object.keys(table.fields))
|
|
198
|
-
if (isPrimaryKey(field, table.indexes ?? {})) anchors.add(field)
|
|
199
|
-
for (const other of schema.tables) {
|
|
200
|
-
for (const relation of other.relations) {
|
|
201
|
-
if (relation.toSchema === schema.id && relation.toTable === table.name)
|
|
202
|
-
anchors.add(relation.toField)
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
return anchors
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
export interface ShownColumns {
|
|
209
|
-
columns: string[]
|
|
210
|
-
hiddenCount: number
|
|
211
|
-
foldable: boolean
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* The columns a table node draws: every one when expanded; folded, the first
|
|
216
|
-
* FOLDED_COLUMNS and every column an arrow ends on, so no edge points to a
|
|
217
|
-
* row that is not drawn.
|
|
218
|
-
*/
|
|
219
|
-
export function shownColumns(
|
|
220
|
-
schema: SchemaSummary,
|
|
221
|
-
table: TableSummary,
|
|
222
|
-
expanded: boolean,
|
|
223
|
-
): ShownColumns {
|
|
224
|
-
const all = Object.keys(table.fields)
|
|
225
|
-
const foldable = all.length > FOLDED_COLUMNS
|
|
226
|
-
if (!foldable || expanded) return { columns: all, hiddenCount: 0, foldable }
|
|
227
|
-
const anchors = anchorColumns(schema, table)
|
|
228
|
-
const columns = all.filter(
|
|
229
|
-
(name, i) => i < FOLDED_COLUMNS || anchors.has(name),
|
|
230
|
-
)
|
|
231
|
-
return { columns, hiddenCount: all.length - columns.length, foldable }
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* The height of a table node: its header, its rows, and the row that folds
|
|
236
|
-
* or unfolds it when it has one.
|
|
237
|
-
*/
|
|
238
|
-
export function nodeHeight(
|
|
239
|
-
schema: SchemaSummary,
|
|
240
|
-
table: TableSummary,
|
|
241
|
-
expanded = false,
|
|
242
|
-
): number {
|
|
243
|
-
const shown = shownColumns(schema, table, expanded)
|
|
244
|
-
const rows = shown.columns.length + (shown.foldable ? 1 : 0)
|
|
245
|
-
return NODE_HEIGHT_HEADER + rows * NODE_HEIGHT_PER_FIELD
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/** Column names of `table` that hold `needle`, as the search reads it. */
|
|
249
|
-
function matchingColumns(table: TableSummary, needle: string): string[] {
|
|
250
|
-
const text = needle.trim().toLowerCase()
|
|
251
|
-
if (!text) return []
|
|
252
|
-
return Object.keys(table.fields).filter((field) =>
|
|
253
|
-
field.toLowerCase().includes(text),
|
|
254
|
-
)
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* The expanded tables once a search lands on `tableName`: it unfolds when a
|
|
259
|
-
* column the search finds is folded away.
|
|
260
|
-
*/
|
|
261
|
-
export function expandForSearch(
|
|
262
|
-
schema: SchemaSummary,
|
|
263
|
-
tableName: string,
|
|
264
|
-
needle: string,
|
|
265
|
-
expanded: Set<string>,
|
|
266
|
-
): Set<string> {
|
|
267
|
-
const table = schema.tables.find((t) => t.name === tableName)
|
|
268
|
-
if (!table || expanded.has(tableName)) return expanded
|
|
269
|
-
const shown = new Set(shownColumns(schema, table, false).columns)
|
|
270
|
-
const folded = matchingColumns(table, needle).some((c) => !shown.has(c))
|
|
271
|
-
return folded ? new Set([...expanded, tableName]) : expanded
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
/** Ids of a table and of every table it points to or is pointed at from. */
|
|
275
|
-
export function neighbourIds(
|
|
276
|
-
schema: SchemaSummary,
|
|
277
|
-
tableName: string,
|
|
278
|
-
): Set<string> {
|
|
279
|
-
const ids = new Set([tableNodeId(schema.id, tableName)])
|
|
280
|
-
for (const table of schema.tables) {
|
|
281
|
-
for (const relation of table.relations) {
|
|
282
|
-
if (table.name === tableName)
|
|
283
|
-
ids.add(tableNodeId(relation.toSchema, relation.toTable))
|
|
284
|
-
if (relation.toSchema === schema.id && relation.toTable === tableName) {
|
|
285
|
-
ids.add(tableNodeId(schema.id, table.name))
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
return ids
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* Picks the handle sides of an edge from the horizontal distance between its
|
|
294
|
-
* nodes, keeping the previous sides within a small margin.
|
|
295
|
-
*/
|
|
296
|
-
export function pickSides(dx: number, width: number, previous?: Sides): Sides {
|
|
297
|
-
const pure: Sides =
|
|
298
|
-
dx >= width
|
|
299
|
-
? { source: 'right', target: 'left' }
|
|
300
|
-
: dx >= 0
|
|
301
|
-
? { source: 'right', target: 'right' }
|
|
302
|
-
: dx >= -width
|
|
303
|
-
? { source: 'left', target: 'left' }
|
|
304
|
-
: { source: 'left', target: 'right' }
|
|
305
|
-
if (!previous) return pure
|
|
306
|
-
const keepRanges: Record<string, [number, number]> = {
|
|
307
|
-
'right-left': [width - HYSTERESIS_PX, Infinity],
|
|
308
|
-
'right-right': [-HYSTERESIS_PX, width + HYSTERESIS_PX],
|
|
309
|
-
'left-left': [-width - HYSTERESIS_PX, HYSTERESIS_PX],
|
|
310
|
-
'left-right': [-Infinity, -width + HYSTERESIS_PX],
|
|
311
|
-
}
|
|
312
|
-
const [min, max] = keepRanges[`${previous.source}-${previous.target}`] ?? [
|
|
313
|
-
0, 0,
|
|
314
|
-
]
|
|
315
|
-
return dx > min && dx < max ? previous : pure
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
interface PendingEdge {
|
|
319
|
-
id: string
|
|
320
|
-
sourceId: string
|
|
321
|
-
targetId: string
|
|
322
|
-
ends: EdgeEnds
|
|
323
|
-
label: string
|
|
324
|
-
many: boolean
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
/** A node as dagre lays it out: its centre and size. */
|
|
328
|
-
interface LaidNode {
|
|
329
|
-
x: number
|
|
330
|
-
y: number
|
|
331
|
-
width: number
|
|
332
|
-
height: number
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
interface StubTarget {
|
|
336
|
-
schema: string
|
|
337
|
-
table: string
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
interface Relations {
|
|
341
|
-
stubs: Map<string, StubTarget>
|
|
342
|
-
pending: PendingEdge[]
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
function relationEdges(
|
|
346
|
-
schema: SchemaSummary,
|
|
347
|
-
selectedId: string | null,
|
|
348
|
-
visible: Set<string>,
|
|
349
|
-
): Relations {
|
|
350
|
-
const stubs = new Map<string, StubTarget>()
|
|
351
|
-
const pending: PendingEdge[] = []
|
|
352
|
-
for (const table of schema.tables) {
|
|
353
|
-
const sourceId = tableNodeId(schema.id, table.name)
|
|
354
|
-
if (!visible.has(sourceId)) continue
|
|
355
|
-
for (const relation of table.relations) {
|
|
356
|
-
const realTargetId = tableNodeId(relation.toSchema, relation.toTable)
|
|
357
|
-
const isStubTarget = !visible.has(realTargetId)
|
|
358
|
-
const targetId = isStubTarget
|
|
359
|
-
? stubNodeId(relation.toSchema, relation.toTable)
|
|
360
|
-
: realTargetId
|
|
361
|
-
if (isStubTarget && !stubs.has(targetId)) {
|
|
362
|
-
stubs.set(targetId, {
|
|
363
|
-
schema: relation.toSchema,
|
|
364
|
-
table: relation.toTable,
|
|
365
|
-
})
|
|
366
|
-
}
|
|
367
|
-
const target = schema.tables.find((t) => t.name === relation.toTable)
|
|
368
|
-
pending.push({
|
|
369
|
-
id: `${sourceId}::${relation.fromField}->${realTargetId}`,
|
|
370
|
-
sourceId,
|
|
371
|
-
targetId,
|
|
372
|
-
label: relation.fromField,
|
|
373
|
-
many: relation.many,
|
|
374
|
-
ends: {
|
|
375
|
-
sourceBase:
|
|
376
|
-
relation.fromField in table.fields
|
|
377
|
-
? relation.fromField
|
|
378
|
-
: '__node__',
|
|
379
|
-
targetBase: isStubTarget
|
|
380
|
-
? 'stub'
|
|
381
|
-
: target && relation.toField in target.fields
|
|
382
|
-
? relation.toField
|
|
383
|
-
: '__node__',
|
|
384
|
-
isStubTarget,
|
|
385
|
-
active:
|
|
386
|
-
selectedId !== null &&
|
|
387
|
-
(selectedId === sourceId || selectedId === realTargetId),
|
|
388
|
-
},
|
|
389
|
-
})
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
return { stubs, pending }
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
export interface LayoutOptions {
|
|
396
|
-
/** Width / height of the canvas: the shape the rows aim at. */
|
|
397
|
-
aspect?: number
|
|
398
|
-
/** What the tables must not cover: the notes, which never move. */
|
|
399
|
-
obstacles?: Rect[]
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
interface LaidGroup extends Size {
|
|
403
|
-
/** Each node's top-left corner, from the group's own. */
|
|
404
|
-
offsets: Map<string, DiagramPosition>
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
/** Lays out one group of related nodes with dagre. */
|
|
408
|
-
function layOutGroup(
|
|
409
|
-
ids: string[],
|
|
410
|
-
sizes: Map<string, Size>,
|
|
411
|
-
links: [string, string][],
|
|
412
|
-
): LaidGroup {
|
|
413
|
-
const graph = new dagre.graphlib.Graph()
|
|
414
|
-
graph.setDefaultEdgeLabel(() => ({}))
|
|
415
|
-
graph.setGraph({ ...LAYOUT })
|
|
416
|
-
const members = new Set(ids)
|
|
417
|
-
for (const id of ids) graph.setNode(id, { ...sizes.get(id) })
|
|
418
|
-
for (const [from, to] of links)
|
|
419
|
-
if (members.has(from) && members.has(to)) graph.setEdge(from, to)
|
|
420
|
-
dagre.layout(graph)
|
|
421
|
-
const rects = ids.map((id) => {
|
|
422
|
-
const laid = graph.node(id) as LaidNode
|
|
423
|
-
const x = laid.x - laid.width / 2
|
|
424
|
-
const y = laid.y - laid.height / 2
|
|
425
|
-
return { id, x, y, width: laid.width, height: laid.height }
|
|
426
|
-
})
|
|
427
|
-
const bounds = boundsOf(rects)
|
|
428
|
-
const offsets = new Map(
|
|
429
|
-
rects.map((r) => [r.id, { x: r.x - bounds.x, y: r.y - bounds.y }]),
|
|
430
|
-
)
|
|
431
|
-
return { offsets, width: bounds.width, height: bounds.height }
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
/**
|
|
435
|
-
* Lays out the whole schema, every table and a stub for each table of
|
|
436
|
-
* another schema a relation points to: dagre on each group of tables linked
|
|
437
|
-
* by relations, a stub staying with the tables it hangs from, then the
|
|
438
|
-
* groups, largest first, and the tables on their own, by name prefix then
|
|
439
|
-
* name, in rows shaped like the canvas. Tables count at their folded height.
|
|
440
|
-
*/
|
|
441
|
-
export function layOutSchema(
|
|
442
|
-
schema: SchemaSummary,
|
|
443
|
-
options: LayoutOptions = {},
|
|
444
|
-
): SchemaLayout {
|
|
445
|
-
const sizes = new Map<string, Size>()
|
|
446
|
-
const names = new Map<string, string>()
|
|
447
|
-
for (const table of schema.tables) {
|
|
448
|
-
const id = tableNodeId(schema.id, table.name)
|
|
449
|
-
names.set(id, table.name)
|
|
450
|
-
sizes.set(id, { width: NODE_WIDTH, height: nodeHeight(schema, table) })
|
|
451
|
-
}
|
|
452
|
-
const { stubs, pending } = relationEdges(schema, null, new Set(names.keys()))
|
|
453
|
-
for (const id of stubs.keys())
|
|
454
|
-
sizes.set(id, { width: STUB_NODE_WIDTH, height: STUB_NODE_HEIGHT })
|
|
455
|
-
const links = pending.map((e): [string, string] => [e.sourceId, e.targetId])
|
|
456
|
-
|
|
457
|
-
const groups: (Block & LaidGroup & { count: number })[] = []
|
|
458
|
-
const alone: string[] = []
|
|
459
|
-
for (const ids of linkedGroups([...sizes.keys()], links)) {
|
|
460
|
-
const [first = ''] = ids
|
|
461
|
-
if (ids.length === 1 && names.has(first)) {
|
|
462
|
-
alone.push(names.get(first) ?? first)
|
|
463
|
-
continue
|
|
464
|
-
}
|
|
465
|
-
const laid = layOutGroup(ids, sizes, links)
|
|
466
|
-
groups.push({ id: `group::${first}`, count: ids.length, ...laid })
|
|
467
|
-
}
|
|
468
|
-
groups.sort(
|
|
469
|
-
(a, b) => b.count - a.count || b.width * b.height - a.width * a.height,
|
|
470
|
-
)
|
|
471
|
-
const single: Block[] = prefixOrder(alone).map((name) => {
|
|
472
|
-
const id = tableNodeId(schema.id, name)
|
|
473
|
-
return { id, ...(sizes.get(id) as Size) }
|
|
474
|
-
})
|
|
475
|
-
|
|
476
|
-
const packed = packBlocks(
|
|
477
|
-
[...groups, ...single],
|
|
478
|
-
options.aspect ?? DEFAULT_ASPECT,
|
|
479
|
-
options.obstacles,
|
|
480
|
-
)
|
|
481
|
-
const positions = new Map(packed)
|
|
482
|
-
for (const group of groups) {
|
|
483
|
-
const at = packed.get(group.id) ?? { x: 0, y: 0 }
|
|
484
|
-
for (const [id, offset] of group.offsets)
|
|
485
|
-
positions.set(id, { x: at.x + offset.x, y: at.y + offset.y })
|
|
486
|
-
}
|
|
487
|
-
const tables: DiagramPositions = {}
|
|
488
|
-
for (const [id, name] of names)
|
|
489
|
-
tables[name] = positions.get(id) ?? { x: 0, y: 0 }
|
|
490
|
-
const stubPositions: DiagramPositions = {}
|
|
491
|
-
for (const id of stubs.keys())
|
|
492
|
-
stubPositions[id] = positions.get(id) ?? { x: 0, y: 0 }
|
|
493
|
-
return { tables, stubs: stubPositions }
|
|
494
|
-
}
|
|
46
|
+
export {
|
|
47
|
+
EDGE_ACTIVE_COLOR,
|
|
48
|
+
EDGE_COLOR,
|
|
49
|
+
EDGE_MANY_COLOR,
|
|
50
|
+
expandForSearch,
|
|
51
|
+
FOLDED_COLUMNS,
|
|
52
|
+
neighbourIds,
|
|
53
|
+
NODE_WIDTH,
|
|
54
|
+
nodeHeight,
|
|
55
|
+
NOTE_NODE_TYPE,
|
|
56
|
+
NOTE_Z_INDEX,
|
|
57
|
+
noteNodeId,
|
|
58
|
+
pickSides,
|
|
59
|
+
RELATION_EDGE_TYPE,
|
|
60
|
+
shownColumns,
|
|
61
|
+
STUB_NODE_TYPE,
|
|
62
|
+
STUB_NODE_WIDTH,
|
|
63
|
+
TABLE_NODE_TYPE,
|
|
64
|
+
tableNodeId,
|
|
65
|
+
} from './graphModel'
|
|
66
|
+
export type {
|
|
67
|
+
BuiltGraph,
|
|
68
|
+
DiagramEdge,
|
|
69
|
+
DiagramNode,
|
|
70
|
+
EdgeEnds,
|
|
71
|
+
GraphInput,
|
|
72
|
+
NoteDraft,
|
|
73
|
+
NoteNodeData,
|
|
74
|
+
OpenTable,
|
|
75
|
+
SchemaLayout,
|
|
76
|
+
ShownColumns,
|
|
77
|
+
Sides,
|
|
78
|
+
StubNodeData,
|
|
79
|
+
TableNodeData,
|
|
80
|
+
} from './graphModel'
|
|
81
|
+
export { layOutSchema } from './layout'
|
|
82
|
+
export type { LayoutOptions } from './layout'
|
|
495
83
|
|
|
496
84
|
/**
|
|
497
85
|
* The columns of `table` lit up by the selection: its relation columns when it
|
|
@@ -571,12 +159,6 @@ function tableNodes(
|
|
|
571
159
|
})
|
|
572
160
|
}
|
|
573
161
|
|
|
574
|
-
export interface StubNodeData {
|
|
575
|
-
targetSchema: string
|
|
576
|
-
targetTable: string
|
|
577
|
-
onOpenSchema: OpenTable
|
|
578
|
-
}
|
|
579
|
-
|
|
580
162
|
/**
|
|
581
163
|
* A stub for a table of this schema left out by the focus sits where that
|
|
582
164
|
* table is; one for another schema takes the place the layout gives it.
|