Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(schema-diagram): Schema Diagram improvements (#4107)

Liu Jib0d08f25a50c
diff --git a/frontend/package.json b/frontend/package.json
index b5785b2860..490cd72116 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -25,10 +25,10 @@
"bootstrap": "5.2.2",
"canvas-confetti": "1.6.0",
"d3-shape": "^3.2.0",
- "dagre": "^0.8.5",
"dayjs": "1.11.6",
"dompurify": "2.4.0",
"downloadjs": "1.4.7",
+ "elkjs": "^0.8.2",
"emittery": "^1.0.1",
"escape-string-regexp": "5.0.0",
"highlight.js": "11.6.0",
@@ -73,7 +73,6 @@
"@tailwindcss/typography": "0.5.8",
"@types/canvas-confetti": "1.4.3",
"@types/d3-shape": "^3.1.0",
- "@types/dagre": "^0.7.48",
"@types/dompurify": "2.3.4",
"@types/downloadjs": "1.4.3",
"@types/js-yaml": "4.0.5",
diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yaml
index fd19f96bd1..5ad00ba5de 100644
--- a/frontend/pnpm-lock.yaml
+++ b/frontend/pnpm-lock.yaml
@@ -17,7 +17,6 @@ specifiers:
'@tanstack/vue-table': 8.5.22
'@types/canvas-confetti': 1.4.3
'@types/d3-shape': ^3.1.0
- '@types/dagre': ^0.7.48
'@types/dompurify': 2.3.4
'@types/downloadjs': 1.4.3
'@types/js-yaml': 4.0.5
@@ -44,10 +43,10 @@ specifiers:
bootstrap: 5.2.2
canvas-confetti: 1.6.0
d3-shape: ^3.2.0
- dagre: ^0.8.5
dayjs: 1.11.6
dompurify: 2.4.0
downloadjs: 1.4.7
+ elkjs: ^0.8.2
emittery: ^1.0.1
escape-string-regexp: 5.0.0
eslint: 8.26.0
@@ -115,10 +114,10 @@ dependencies:
bootstrap: 5.2.2_@[email protected]
canvas-confetti: 1.6.0
d3-shape: 3.2.0
- dagre: 0.8.5
dayjs: 1.11.6
dompurify: 2.4.0
downloadjs: 1.4.7
+ elkjs: 0.8.2
emittery: 1.0.1
escape-string-regexp: 5.0.0
highlight.js: 11.6.0
@@ -163,7 +162,6 @@ devDependencies:
'@tailwindcss/typography': [email protected]
'@types/canvas-confetti': 1.4.3
'@types/d3-shape': 3.1.0
- '@types/dagre': 0.7.48
'@types/dompurify': 2.3.4
'@types/downloadjs': 1.4.3
'@types/js-yaml': 4.0.5
@@ -436,8 +434,8 @@ packages:
vue-i18n:
optional: true
dependencies:
- '@intlify/message-compiler': 9.3.0-beta.11
- '@intlify/shared': 9.3.0-beta.11
+ '@intlify/message-compiler': 9.3.0-beta.12
+ '@intlify/shared': 9.3.0-beta.12
jsonc-eslint-parser: 1.4.1
source-map: 0.6.1
@@ -466,8 +464,8 @@ packages:
'@intlify/shared': 9.2.2
source-map: 0.6.1
- /@intlify/message-compiler/9.3.0-beta.11:
- resolution: {integrity: sha512-gGGfBGzM7JBXp1Q9gbDAy5jELz9ho3ILqnpxp2yp64+gkqohrqc2YXIvCdwZoc6AtKIh/Zmv4sWVqxkvMsBWtQ==}
+ /@intlify/message-compiler/9.3.0-beta.12:
+ resolution: {integrity: sha512-A8/s7pb3v8nf6HG77qFPJntxgQKI9GXxGnkn7aO+b03/X/GkF/4WceDSAIk3i+yLeIgszeBn9GZ23tSg4sTEHA==}
engines: {node: '>= 14'}
dependencies:
'@intlify/shared': 9.3.0-beta.11
@@ -483,6 +481,11 @@ packages:
engines: {node: '>= 14'}
dev: true
+ /@intlify/shared/9.3.0-beta.12:
+ resolution: {integrity: sha512-WsmaS54sA8xuwezPKpa/OMoaX1v2VF2fCgAmYS6prDr2ir0CkUFWPm9A8ilmxzv4nkS61/v8+vf4lGGkn5uBdA==}
+ engines: {node: '>= 14'}
+ dev: true
+
/@intlify/vite-plugin-vue-i18n/[email protected][email protected]:
resolution: {integrity: sha512-6SgNzPAOCR90wvt368lKzi7f/5ZEWJn22UCGvhFsP3XvKqlF3cVzojahgQ6o+LTdCkExeM6wPgd+haFf28E9VQ==}
engines: {node: '>= 14.6'}
@@ -499,7 +502,7 @@ packages:
optional: true
dependencies:
'@intlify/bundle-utils': [email protected]
- '@intlify/shared': 9.3.0-beta.11
+ '@intlify/shared': 9.3.0-beta.12
'@rollup/pluginutils': 4.2.1
debug: 4.3.4
fast-glob: 3.2.12
@@ -887,10 +890,6 @@ packages:
'@types/d3-zoom': 3.0.1
dev: false
- /@types/dagre/0.7.48:
- resolution: {integrity: sha512-rF3yXSwHIrDxEkN6edCE4TXknb5YSEpiXfLaspw1I08grC49ZFuAVGOQCmZGIuLUGoFgcqGlUFBL/XrpgYpQgw==}
- dev: true
-
/@types/dompurify/2.3.4:
resolution: {integrity: sha512-EXzDatIb5EspL2eb/xPGmaC8pePcTHrkDCONjeisusLFrVfl38Pjea/R0YJGu3k9ZQadSvMqW0WXPI2hEo2Ajg==}
dependencies:
@@ -1952,13 +1951,6 @@ packages:
d3-zoom: 3.0.0
dev: false
- /dagre/0.8.5:
- resolution: {integrity: sha512-/aTqmnRta7x7MCCpExk7HQL2O4owCT2h8NT//9I1OQ9vt29Pa0BzSAkR5lwFUcQ7491yVi/3CXU9jQ5o0Mn2Sw==}
- dependencies:
- graphlib: 2.1.8
- lodash: 4.17.21
- dev: false
-
/dataloader/1.4.0:
resolution: {integrity: sha512-68s5jYdlvasItOJnCuI2Q9s4q98g0pCyL3HrcKJu8KNugUl8ahgmZYg38ysLTgQjjXX3H8CJLkAvWrclWfcalw==}
dev: true
@@ -2101,6 +2093,10 @@ packages:
resolution: {integrity: sha512-M8WEXFuKXMYMVr45fo8mq0wUrrJHheiKZf6BArTKk9ZBYCKJEOU5H8cdWgDT+qCVZf7Na4lVUaZsA+h6uA9+PA==}
dev: true
+ /elkjs/0.8.2:
+ resolution: {integrity: sha512-L6uRgvZTH+4OF5NE/MBbzQx/WYpru1xCBE9respNj6qznEewGUIfhzmm7horWWxbNO2M0WckQypGctR8lH79xQ==}
+ dev: false
+
/emittery/1.0.1:
resolution: {integrity: sha512-2ID6FdrMD9KDLldGesP6317G78K7km/kMcwItRtVFva7I/cSEOIaLpewaUb+YLXVwdAp3Ctfxh/V5zIl1sj7dQ==}
engines: {node: '>=14.16'}
@@ -2713,12 +2709,6 @@ packages:
resolution: {integrity: sha512-bzh50DW9kTPM00T8y4o8vQg89Di9oLJVLW/KaOGIXJWP/iqCN6WKYkbNOF04vFLJhwcpYUh9ydh/+5vpOqV4YQ==}
dev: true
- /graphlib/2.1.8:
- resolution: {integrity: sha512-jcLLfkpoVGmH7/InMC/1hIvOPSUh38oJtGhvrOFGzioE1DZ+0YW16RgmOJhHiuWTvGiJQ9Z1Ik43JvkRPRvE+A==}
- dependencies:
- lodash: 4.17.21
- dev: false
-
/has-flag/4.0.0:
resolution: {integrity: sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==}
engines: {node: '>=8'}
diff --git a/frontend/src/components/SchemaDiagram/ER/libs/SVGLine.vue b/frontend/src/components/SchemaDiagram/ER/libs/SVGLine.vue
index 43b8806155..5351764bd5 100644
--- a/frontend/src/components/SchemaDiagram/ER/libs/SVGLine.vue
+++ b/frontend/src/components/SchemaDiagram/ER/libs/SVGLine.vue
@@ -2,59 +2,69 @@
<svg
version="1.1"
xmlns="http://www.w3.org/2000/svg"
- class="absolute"
+ class="absolute cursor-pointer"
+ pointer-events="none"
+ :viewBox="[viewBox.x, viewBox.y, viewBox.width, viewBox.height].join(' ')"
:style="{
- left: `${bbox.x - AA_OFFSET[0]}px`,
- top: `${bbox.y - AA_OFFSET[1]}px`,
- width: `${bbox.width + AA_OFFSET[0] * 2}px`,
- height: `${bbox.height + AA_OFFSET[1] * 2}px`,
+ left: `${bbox.x + viewBox.x}px`,
+ top: `${bbox.y + viewBox.y}px`,
+ width: `${viewBox.width}px`,
+ height: `${viewBox.height}px`,
}"
>
<path
+ ref="track"
version="1.1"
xmlns="http://www.w3.org/2000/svg"
:d="svgLine"
- v-bind="aaProps"
pointer-events="visibleStroke"
fill="none"
- stroke="11.5"
- stroke-width="11.5"
- class="opacity-40"
- ></path>
+ :stroke="hover ? COLORS.GLOW.HOVER : COLORS.GLOW.NORMAL"
+ :stroke-width="GLOW_WIDTH"
+ />
<path
v-for="(decorator, i) in svgDecorators"
:key="i"
version="1.1"
xmlns="http://www.w3.org/2000/svg"
:d="decorator"
- v-bind="aaProps"
- stroke="rgb(40, 40, 40)"
fill="none"
+ :stroke="hover ? COLORS.LINE.HOVER : COLORS.LINE.NORMAL"
stroke-width="1"
pointer-events="visibleStroke"
/>
<path
+ ref="line"
version="1.1"
xmlns="http://www.w3.org/2000/svg"
:d="svgLine"
- v-bind="aaProps"
pointer-events="visibleStroke"
fill="none"
- stroke="rgb(40, 40, 40)"
- stroke-width="1.5"
- ></path>
+ :stroke="hover ? COLORS.LINE.HOVER : COLORS.LINE.NORMAL"
+ :stroke-width="hover ? 2 : 1.5"
+ />
</svg>
</template>
<script lang="ts" setup>
-import { computed } from "vue";
-import { Path } from "../../types";
-import { line as d3Line } from "d3-shape";
+import { computed, ref } from "vue";
+import { Path, Rect } from "../../types";
+import { curveMonotoneX, line as d3Line } from "d3-shape";
import { calcBBox } from "../../common";
+import { useElementHover } from "@vueuse/core";
-// For SVG anti-aliasing, especially a line aligned to the x-axis
-// Just a simple workaround, not perfect.
-const AA_OFFSET = [2.25, 2.25];
+const GLOW_WIDTH = 12;
+const PADDING = GLOW_WIDTH / 2;
+const COLORS = {
+ GLOW: {
+ NORMAL: "transparent",
+ HOVER: "rgba(55,48,163,0.1)",
+ },
+ LINE: {
+ NORMAL: "#1f2937",
+ HOVER: "#4f46e5",
+ },
+};
const props = withDefaults(
defineProps<{
@@ -66,15 +76,27 @@ const props = withDefaults(
}
);
+const track = ref<SVGPathElement>();
+const line = ref<SVGPathElement>();
+
+const trackHover = useElementHover(track);
+const lineHover = useElementHover(line);
+const hover = computed(() => trackHover.value || lineHover.value);
+
const bbox = computed(() => {
const points = [...props.path];
props.decorators.forEach((decorator) => points.push(...decorator));
return calcBBox(points);
});
-const aaProps = computed(() => ({
- transform: `translate(${AA_OFFSET.join(",")})`,
-}));
+const viewBox = computed((): Rect => {
+ return {
+ x: -PADDING,
+ y: -PADDING,
+ width: Math.max(bbox.value.width, 0) + PADDING * 2,
+ height: Math.max(bbox.value.height, 0) + PADDING * 2,
+ };
+});
const normalize = (x: number, y: number): [number, number] => {
const dx = bbox.value.x;
@@ -83,7 +105,11 @@ const normalize = (x: number, y: number): [number, number] => {
};
const svgLine = computed(() => {
- return d3Line()(props.path.map((p) => normalize(p.x, p.y))) ?? "";
+ return (
+ d3Line().curve(curveMonotoneX)(
+ props.path.map((p) => normalize(p.x, p.y))
+ ) ?? ""
+ );
});
const svgDecorators = computed(() => {
diff --git a/frontend/src/components/SchemaDiagram/ER/libs/autoLayout.ts b/frontend/src/components/SchemaDiagram/ER/libs/autoLayout.ts
deleted file mode 100644
index a436b04001..0000000000
--- a/frontend/src/components/SchemaDiagram/ER/libs/autoLayout.ts
+++ /dev/null
@@ -1,60 +0,0 @@
-import { Path, Rect, Size } from "../../types";
-import dagre from "dagre";
-
-export type GraphNodeItem = {
- id: string;
- size: Size;
-};
-
-export type GraphEdgeItem = {
- id: string;
- from: string;
- to: string;
-};
-
-export type Layout = {
- rects: Map<string, Rect>;
- paths: Map<string, Path>;
-};
-
-export const layoutDagre = async (
- nodeList: GraphNodeItem[],
- edgeList: GraphEdgeItem[]
-): Promise<Layout> => {
- const g = new dagre.graphlib.Graph({ multigraph: true });
- g.setGraph({
- rankdir: "LR",
- nodesep: 160, // gap between nodes in a layer
- ranksep: 160, // gap between layers
- });
- g.setDefaultEdgeLabel(() => ({}));
- nodeList.forEach(({ id, size }) => {
- g.setNode(id, {
- ...size,
- });
- });
- edgeList.forEach(({ id, from, to }) => {
- g.setEdge({ name: id, v: from, w: to });
- });
- dagre.layout(g);
- const rects = new Map<string, Rect>();
- g.nodes().forEach((id) => {
- const node = g.node(id);
- rects.set(id, node);
- });
- const paths = new Map<string, Path>();
- g.edges().forEach((e) => {
- const edge = g.edge(e);
- const { points } = edge;
- paths.set(e.name!, points);
- });
-
- return { rects, paths };
-};
-
-export const autoLayout = async (
- nodeList: GraphNodeItem[],
- edgeList: GraphEdgeItem[]
-): Promise<Layout> => {
- return layoutDagre(nodeList, edgeList);
-};
diff --git a/frontend/src/components/SchemaDiagram/ER/libs/autoLayout/engines/elk.ts b/frontend/src/components/SchemaDiagram/ER/libs/autoLayout/engines/elk.ts
new file mode 100644
index 0000000000..f5fcb44ae2
--- /dev/null
+++ b/frontend/src/components/SchemaDiagram/ER/libs/autoLayout/engines/elk.ts
@@ -0,0 +1,58 @@
+import ELK, { type ElkNode } from "elkjs/lib/elk.bundled";
+
+import type { Path, Rect } from "@/components/SchemaDiagram/types";
+import type { GraphEdgeItem, GraphNodeItem, Layout } from "../types";
+
+export const layoutELK = async (
+ nodeList: GraphNodeItem[],
+ edgeList: GraphEdgeItem[]
+): Promise<Layout> => {
+ const elk = new ELK({});
+ const graph: ElkNode = {
+ id: "root",
+ layoutOptions: {
+ "elk.algorithm": "layered",
+ "elk.direction": "RIGHT",
+ "elk.spacing.nodeNode": "100",
+ "elk.layered.spacing.nodeNodeBetweenLayers": "200",
+ "elk.layered.nodePlacement.favorStraightEdges": "true",
+ "elk.edgeRouting": "POLYLINE",
+ },
+ children: nodeList.map(({ id, size }) => ({
+ id,
+ ...size,
+ })),
+ edges: edgeList.map(({ id, from, to }) => ({
+ id,
+ sources: [from],
+ targets: [to],
+ })),
+ };
+
+ const result = await elk.layout(graph, {
+ logging: true,
+ });
+ const rects = new Map<string, Rect>();
+ result.children?.forEach((node) => {
+ rects.set(node.id, {
+ x: node.x!,
+ y: node.y!,
+ width: node.width!,
+ height: node.height!,
+ });
+ });
+ const paths = new Map<string, Path>();
+ result.edges?.forEach((edge) => {
+ const { sections = [] } = edge;
+ const path: Path = sections.flatMap((section) => {
+ return [
+ section.startPoint,
+ ...(section.bendPoints ?? []),
+ section.endPoint,
+ ];
+ });
+ paths.set(edge.id, path);
+ });
+
+ return { rects, paths };
+};
diff --git a/frontend/src/components/SchemaDiagram/ER/libs/autoLayout/index.ts b/frontend/src/components/SchemaDiagram/ER/libs/autoLayout/index.ts
new file mode 100644
index 0000000000..c1b881bc18
--- /dev/null
+++ b/frontend/src/components/SchemaDiagram/ER/libs/autoLayout/index.ts
@@ -0,0 +1,11 @@
+import { GraphEdgeItem, GraphNodeItem, Layout } from "./types";
+
+export * from "./types";
+
+export const autoLayout = async (
+ nodeList: GraphNodeItem[],
+ edgeList: GraphEdgeItem[]
+): Promise<Layout> => {
+ const { layoutELK } = await import("./engines/elk");
+ return layoutELK(nodeList, edgeList);
+};
diff --git a/frontend/src/components/SchemaDiagram/ER/libs/autoLayout/types.ts b/frontend/src/components/SchemaDiagram/ER/libs/autoLayout/types.ts
new file mode 100644
index 0000000000..2a4b1fea7d
--- /dev/null
+++ b/frontend/src/components/SchemaDiagram/ER/libs/autoLayout/types.ts
@@ -0,0 +1,25 @@
+import type {
+ Path,
+ Position,
+ Rect,
+ Size,
+} from "@/components/SchemaDiagram/types";
+
+export type GraphChildNodeItem = { id: string; size: Size; pos: Position };
+
+export type GraphNodeItem = {
+ id: string;
+ size: Size;
+ children: GraphChildNodeItem[]; // not used yet
+};
+
+export type GraphEdgeItem = {
+ id: string;
+ from: string;
+ to: string;
+};
+
+export type Layout = {
+ rects: Map<string, Rect>;
+ paths: Map<string, Path>;
+};
diff --git a/frontend/src/components/SchemaDiagram/SchemaDiagram.vue b/frontend/src/components/SchemaDiagram/SchemaDiagram.vue
index e460015ee9..be215910e4 100644
--- a/frontend/src/components/SchemaDiagram/SchemaDiagram.vue
+++ b/frontend/src/components/SchemaDiagram/SchemaDiagram.vue
@@ -1,10 +1,24 @@
<template>
<Canvas>
<template #desktop>
- <TableNode v-for="(table, i) in tableList" :key="i" :table="table" />
+ <TableNode
+ v-for="(table, i) in tableList"
+ :key="i"
+ :table="table"
+ :class="initialized ? '' : 'invisible'"
+ />
- <ForeignKeyLine v-for="(fk, i) in foreignKeys" :key="i" :fk="fk" />
+ <template v-if="initialized">
+ <ForeignKeyLine v-for="(fk, i) in foreignKeys" :key="i" :fk="fk" />
+ </template>
</template>
+
+ <div
+ v-if="!initialized"
+ class="absolute inset-0 bg-white/40 flex items-center justify-center"
+ >
+ <BBSpin />
+ </div>
</Canvas>
</template>
@@ -23,7 +37,7 @@ import {
ForeignKey,
} from "./types";
import Canvas from "./Canvas";
-import { TableNode, autoLayout, GraphNodeItem } from "./ER";
+import { TableNode, autoLayout, GraphNodeItem, GraphEdgeItem } from "./ER";
import { provideSchemaDiagramContext } from "./common";
const props = withDefaults(
@@ -34,6 +48,7 @@ const props = withDefaults(
{}
);
+const initialized = ref(false);
const zoom = ref(1);
const position = ref<Position>({ x: 0, y: 0 });
@@ -101,16 +116,18 @@ const layout = () => {
height: item.elem.clientHeight,
};
return {
- ...item,
+ id: item.id,
size,
- ports: [],
+ children: [],
};
});
- const edgeList = foreignKeys.value.map((fk) => {
+
+ const edgeList = foreignKeys.value.map<GraphEdgeItem>((fk) => {
+ const { from, to } = fk;
return {
- id: `${fk.from.table.name}.${fk.from.column}->${fk.to.table.name}.${fk.to.column}`,
- from: idOfTable(fk.from.table),
- to: idOfTable(fk.to.table),
+ id: `${from.table.name}.${from.column}->${to.table.name}.${to.column}`,
+ from: idOfTable(from.table),
+ to: idOfTable(to.table),
};
});
const { rects } = await autoLayout(nodeList, edgeList);
@@ -138,6 +155,7 @@ provideSchemaDiagramContext({
// autoLayout and fit view at the first time the diagram is mounted.
onMounted(async () => {
await layout();
+ initialized.value = true;
nextTick(() => {
events.emit("fit-view");
});