|
|
@ -1,29 +1,29 @@ |
|
|
|
<script setup lang="ts"> |
|
|
|
<script setup lang="ts"> |
|
|
|
import { useI18n } from 'vue-i18n' |
|
|
|
import type { Edge } from '@braks/vue-flow' |
|
|
|
import type { Edge, Node } from '@braks/vue-flow' |
|
|
|
import { Background, Controls, VueFlow, useVueFlow } from '@braks/vue-flow' |
|
|
|
import { Background, MarkerType, VueFlow, isNode, useVueFlow } from '@braks/vue-flow' |
|
|
|
|
|
|
|
import { ref } from 'vue' |
|
|
|
import { ref } from 'vue' |
|
|
|
import { ColumnType, UITypes } from 'nocodb-sdk' |
|
|
|
import { UITypes } from 'nocodb-sdk' |
|
|
|
import dagre from 'dagre' |
|
|
|
import dagre from 'dagre' |
|
|
|
import TableNode from './erd/TableNode.vue' |
|
|
|
import TableNode from './erd/TableNode.vue' |
|
|
|
import RelationEdge from './erd/RelationEdge.vue' |
|
|
|
import RelationEdge from './erd/RelationEdge.vue' |
|
|
|
import { useNuxtApp, useProject } from '#imports' |
|
|
|
import { useProject } from '#imports' |
|
|
|
|
|
|
|
|
|
|
|
const dagreGraph = new dagre.graphlib.Graph() |
|
|
|
const dagreGraph = new dagre.graphlib.Graph() |
|
|
|
dagreGraph.setDefaultEdgeLabel(() => ({})) |
|
|
|
dagreGraph.setDefaultEdgeLabel(() => ({})) |
|
|
|
|
|
|
|
|
|
|
|
const { updateNodeInternals } = useVueFlow() |
|
|
|
const { updateNodeInternals, removeNodes, removeEdges, $reset } = useVueFlow() |
|
|
|
|
|
|
|
|
|
|
|
const { $api } = useNuxtApp() |
|
|
|
|
|
|
|
const { project, tables } = useProject() |
|
|
|
|
|
|
|
const { t } = useI18n() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { tables } = useProject() |
|
|
|
const { metas, getMeta, metasWithId } = useMetas() |
|
|
|
const { metas, getMeta, metasWithId } = useMetas() |
|
|
|
|
|
|
|
|
|
|
|
const nodes = ref<Node[]>([]) |
|
|
|
const nodes = ref<any[]>([]) |
|
|
|
const edges = ref<Edge[]>([]) |
|
|
|
const edges = ref<Edge[]>([]) |
|
|
|
|
|
|
|
|
|
|
|
let isLoading = $ref(true) |
|
|
|
let isLoading = $ref(true) |
|
|
|
|
|
|
|
const isLayouting = ref(false) |
|
|
|
|
|
|
|
const config = ref({ |
|
|
|
|
|
|
|
showPkAndFk: true, |
|
|
|
|
|
|
|
showViews: false, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
const loadMetasOfTablesNotInMetas = async () => { |
|
|
|
const loadMetasOfTablesNotInMetas = async () => { |
|
|
|
await Promise.all( |
|
|
|
await Promise.all( |
|
|
@ -36,24 +36,26 @@ const loadMetasOfTablesNotInMetas = async () => { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const populateTables = () => { |
|
|
|
const populateTables = () => { |
|
|
|
Object.keys(metasWithId.value).forEach((tableId) => { |
|
|
|
tables.value.forEach((table) => { |
|
|
|
|
|
|
|
if (!table?.id) return |
|
|
|
|
|
|
|
|
|
|
|
nodes.value.push({ |
|
|
|
nodes.value.push({ |
|
|
|
id: tableId, |
|
|
|
id: table.id, |
|
|
|
data: metasWithId.value[tableId], |
|
|
|
data: { ...metasWithId.value[table.id], showPkAndFk: config.value.showPkAndFk }, |
|
|
|
type: 'custom', |
|
|
|
type: 'custom', |
|
|
|
}) |
|
|
|
}) |
|
|
|
dagreGraph.setNode(tableId, { width: 250, height: 30 * metasWithId.value[tableId].columns.length }) |
|
|
|
dagreGraph.setNode(table.id, { width: 250, height: 30 * metasWithId.value[table.id].columns.length }) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
dagreGraph.setGraph({ rankdir: 'LR' }) |
|
|
|
dagreGraph.setGraph({ rankdir: 'LR' }) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const populateRelations = () => { |
|
|
|
const populateRelations = () => { |
|
|
|
const ltarColumns = Object.keys(metasWithId.value).reduce((acc, tableId) => { |
|
|
|
const ltarColumns = Object.keys(metasWithId.value).reduce((acc: any[], tableId) => { |
|
|
|
const table = metasWithId.value[tableId] |
|
|
|
const table = metasWithId.value[tableId] |
|
|
|
const ltarColumns = table.columns.filter((column) => column.uidt === UITypes.LinkToAnotherRecord) |
|
|
|
const ltarColumns = table.columns.filter((column: any) => column.uidt === UITypes.LinkToAnotherRecord) |
|
|
|
|
|
|
|
|
|
|
|
ltarColumns.forEach((column) => { |
|
|
|
ltarColumns.forEach((column: any) => { |
|
|
|
if (column.colOptions.type === 'hm') { |
|
|
|
if (column.colOptions.type === 'hm') { |
|
|
|
acc.push(column) |
|
|
|
acc.push(column) |
|
|
|
} |
|
|
|
} |
|
|
@ -73,6 +75,7 @@ const populateRelations = () => { |
|
|
|
edges.value = ltarColumns.map((column: any) => { |
|
|
|
edges.value = ltarColumns.map((column: any) => { |
|
|
|
const source = column.fk_model_id |
|
|
|
const source = column.fk_model_id |
|
|
|
const target = column.colOptions.fk_related_model_id |
|
|
|
const target = column.colOptions.fk_related_model_id |
|
|
|
|
|
|
|
const targetColumnId = column.colOptions.fk_related_column_id |
|
|
|
|
|
|
|
|
|
|
|
dagreGraph.setEdge(source, target) |
|
|
|
dagreGraph.setEdge(source, target) |
|
|
|
|
|
|
|
|
|
|
@ -81,10 +84,9 @@ const populateRelations = () => { |
|
|
|
source: `${source}`, |
|
|
|
source: `${source}`, |
|
|
|
target: `${target}`, |
|
|
|
target: `${target}`, |
|
|
|
sourceHandle: `s-${column.id}-${source}`, |
|
|
|
sourceHandle: `s-${column.id}-${source}`, |
|
|
|
targetHandle: `d-${column.id}-${target}`, |
|
|
|
targetHandle: `d-${targetColumnId}-${target}`, |
|
|
|
type: 'custom', |
|
|
|
type: 'custom', |
|
|
|
data: { column, table: metasWithId.value[source], relatedTable: metasWithId.value[target] }, |
|
|
|
data: { column, table: metasWithId.value[source], relatedTable: metasWithId.value[target] }, |
|
|
|
markerEnd: MarkerType.ArrowClosed, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
@ -107,27 +109,54 @@ const populateElements = () => { |
|
|
|
populateTables() |
|
|
|
populateTables() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
onMounted(async () => { |
|
|
|
const populateErd = () => { |
|
|
|
if (isLoading) { |
|
|
|
isLayouting.value = true |
|
|
|
await loadMetasOfTablesNotInMetas() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
populateElements() |
|
|
|
populateElements() |
|
|
|
populateRelations() |
|
|
|
populateRelations() |
|
|
|
layoutNodes() |
|
|
|
layoutNodes() |
|
|
|
|
|
|
|
|
|
|
|
console.log('nodes', nodes.value) |
|
|
|
updateNodeInternals(nodes.value as any) |
|
|
|
console.log('edges', edges.value) |
|
|
|
isLayouting.value = false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const resetElements = () => { |
|
|
|
|
|
|
|
$reset() |
|
|
|
updateNodeInternals(nodes.value as any) |
|
|
|
updateNodeInternals(nodes.value as any) |
|
|
|
isLoading = false |
|
|
|
removeNodes(nodes.value) |
|
|
|
|
|
|
|
removeEdges(edges.value) |
|
|
|
|
|
|
|
nodes.value = [] |
|
|
|
|
|
|
|
edges.value = [] |
|
|
|
|
|
|
|
dagreGraph.nodes().forEach((node: any) => dagreGraph.removeNode(node)) |
|
|
|
|
|
|
|
dagreGraph.edges().forEach((edge: any) => dagreGraph.removeEdge(edge)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
onMounted(async () => { |
|
|
|
|
|
|
|
isLoading = true |
|
|
|
|
|
|
|
resetElements() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await loadMetasOfTablesNotInMetas() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isLoading = false |
|
|
|
|
|
|
|
populateErd() |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
watch(config.value, () => { |
|
|
|
|
|
|
|
populateErd() |
|
|
|
}) |
|
|
|
}) |
|
|
|
</script> |
|
|
|
</script> |
|
|
|
|
|
|
|
|
|
|
|
<template> |
|
|
|
<template> |
|
|
|
<div style="height: 650px"> |
|
|
|
<div v-if="isLoading" style="height: 650px"></div> |
|
|
|
<div v-if="isLoading"></div> |
|
|
|
<div v-else class="relative" style="height: 650px"> |
|
|
|
<VueFlow v-else :nodes="nodes" :edges="edges" :fit-view-on-init="true" :default-edge-options="{ type: 'step' }"> |
|
|
|
<VueFlow |
|
|
|
|
|
|
|
:key="config.toString()" |
|
|
|
|
|
|
|
:nodes="nodes" |
|
|
|
|
|
|
|
:edges="edges" |
|
|
|
|
|
|
|
:fit-view-on-init="true" |
|
|
|
|
|
|
|
:default-edge-options="{ type: 'step' }" |
|
|
|
|
|
|
|
:elevate-edges-on-select="true" |
|
|
|
|
|
|
|
> |
|
|
|
|
|
|
|
<Controls :show-fit-view="false" :show-interactive="false" /> |
|
|
|
<template #node-custom="props"> |
|
|
|
<template #node-custom="props"> |
|
|
|
<TableNode :data="props.data" /> |
|
|
|
<TableNode :data="props.data" /> |
|
|
|
</template> |
|
|
|
</template> |
|
|
@ -136,5 +165,15 @@ onMounted(async () => { |
|
|
|
</template> |
|
|
|
</template> |
|
|
|
<Background /> |
|
|
|
<Background /> |
|
|
|
</VueFlow> |
|
|
|
</VueFlow> |
|
|
|
|
|
|
|
<div class="absolute top-4 right-4 flex-col bg-white py-2 px-4 border-1 border-gray-100 rounded-md z-50 space-y-1"> |
|
|
|
|
|
|
|
<div class="flex flex-row items-center"> |
|
|
|
|
|
|
|
<a-checkbox v-model:checked="config.showPkAndFk" /> |
|
|
|
|
|
|
|
<span class="ml-2" style="font-size: 0.65rem">Show PK and FK</span> |
|
|
|
|
|
|
|
</div> |
|
|
|
|
|
|
|
<div class="flex flex-row items-center"> |
|
|
|
|
|
|
|
<a-checkbox v-model:checked="config.showViews" /> |
|
|
|
|
|
|
|
<span class="ml-2" style="font-size: 0.65rem">Show views</span> |
|
|
|
|
|
|
|
</div> |
|
|
|
|
|
|
|
</div> |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
</template> |
|
|
|
</template> |
|
|
|