Database diagrams editor that allows you to visualize and design your DB with a single query.
TypeScript databasedbdb-migrationeditorerdmariadbmssqlmysqlpostgresqlreactreact-flowschema-migrationssqlsqlitetypescriptvisualizationxyflow
fix: improve creating view to table dependency (#874)
Guy Ben-Aharon44be48ff3ad1
diff --git a/src/pages/editor-page/canvas/table-node/table-node-dependency-indicator.tsx b/src/pages/editor-page/canvas/table-node/table-node-dependency-indicator.tsxindex 02815a33..183f4884 100644--- a/src/pages/editor-page/canvas/table-node/table-node-dependency-indicator.tsx+++ b/src/pages/editor-page/canvas/table-node/table-node-dependency-indicator.tsx@@ -7,6 +7,10 @@ import {useUpdateNodeInternals,} from '@xyflow/react';import React, { useEffect, useMemo, useRef } from 'react';+import {+ LEFT_HANDLE_ID_PREFIX,+ RIGHT_HANDLE_ID_PREFIX,+} from './table-node-field';export const TOP_SOURCE_HANDLE_ID_PREFIX = 'top_dep_';export const BOTTOM_SOURCE_HANDLE_ID_PREFIX = 'bottom_dep_';@@ -36,6 +40,22 @@ export const TableNodeDependencyIndicator: React.FC<TableNodeDependencyIndicator[connection, table.id]);+ const isTargetFromTable = useMemo(+ () =>+ connection.inProgress &&+ connection.fromNode.id !== table.id &&+ (connection.fromHandle.id?.startsWith(RIGHT_HANDLE_ID_PREFIX) ||+ connection.fromHandle.id?.startsWith(+ LEFT_HANDLE_ID_PREFIX+ )),+ [+ connection.inProgress,+ connection.fromNode?.id,+ connection.fromHandle?.id,+ table.id,+ ]+ );+const numberOfEdgesToTable = useMemo(() =>dependencies.filter(@@ -58,12 +78,20 @@ export const TableNodeDependencyIndicator: React.FC<TableNodeDependencyIndicatorreturn (<>{table.isView || table.isMaterializedView ? (- <Handle- id={`${TOP_SOURCE_HANDLE_ID_PREFIX}${table.id}`}- className={`!h-4 !w-4 !border-2 !bg-pink-600 ${!focused ? '!invisible' : ''}`}- position={Position.Top}- type="source"- />+ <>+ <Handle+ id={`${TOP_SOURCE_HANDLE_ID_PREFIX}${table.id}`}+ className={`!h-4 !w-4 !border-2 !bg-pink-600 ${!focused || isTargetFromTable ? '!invisible' : ''}`}+ position={Position.Top}+ type="source"+ />+ <Handle+ id={`${BOTTOM_SOURCE_HANDLE_ID_PREFIX}${table.id}`}+ className={`!z-10 !h-4 !w-4 !border-2 !bg-pink-600 ${!focused || isTargetFromTable ? '!invisible' : ''}`}+ position={Position.Bottom}+ type="source"+ />+ </>) : null}{Array.from({ length: numberOfEdgesToTable },@@ -82,7 +110,7 @@ export const TableNodeDependencyIndicator: React.FC<TableNodeDependencyIndicatorid={`${TARGET_DEP_PREFIX}${numberOfEdgesToTable}_${table.id}`}className={isTarget- ? '!absolute !left-0 !top-0 !h-full !w-full !transform-none !rounded-none !border-none !opacity-0'+ ? '!absolute !inset-0 !z-10 !h-full !w-full !transform-none !rounded-none !border-none !opacity-0': `!invisible`}position={Position.Top}diff --git a/src/pages/editor-page/canvas/table-node/table-node-field.tsx b/src/pages/editor-page/canvas/table-node/table-node-field.tsxindex 94e0c86d..4200c9fb 100644--- a/src/pages/editor-page/canvas/table-node/table-node-field.tsx+++ b/src/pages/editor-page/canvas/table-node/table-node-field.tsx@@ -33,6 +33,10 @@ import { useClickAway, useKeyPressEvent } from 'react-use';import { Input } from '@/components/input/input';import { useDiff } from '@/context/diff-context/use-diff';import { useLocalConfig } from '@/hooks/use-local-config';+import {+ BOTTOM_SOURCE_HANDLE_ID_PREFIX,+ TOP_SOURCE_HANDLE_ID_PREFIX,+} from './table-node-dependency-indicator';export const LEFT_HANDLE_ID_PREFIX = 'left_rel_';export const RIGHT_HANDLE_ID_PREFIX = 'right_rel_';@@ -102,6 +106,24 @@ export const TableNodeField: React.FC<TableNodeFieldProps> = React.memo(tableNodeId,]);+ const isTargetFromView = useMemo(+ () =>+ connection.inProgress &&+ connection.fromNode.id !== tableNodeId &&+ (connection.fromHandle.id?.startsWith(+ TOP_SOURCE_HANDLE_ID_PREFIX+ ) ||+ connection.fromHandle.id?.startsWith(+ BOTTOM_SOURCE_HANDLE_ID_PREFIX+ )),+ [+ connection.inProgress,+ connection.fromNode?.id,+ connection.fromHandle?.id,+ tableNodeId,+ ]+ );+const numberOfEdgesToField = useMemo(() => {let count = 0;for (const rel of relationships) {@@ -289,13 +311,13 @@ export const TableNodeField: React.FC<TableNodeFieldProps> = React.memo(<><Handleid={`${RIGHT_HANDLE_ID_PREFIX}${field.id}`}- className={`!h-4 !w-4 !border-2 !bg-pink-600 ${!focused || readonly ? '!invisible' : ''}`}+ className={`!h-4 !w-4 !border-2 !bg-pink-600 ${!focused || readonly || isTargetFromView ? '!invisible' : ''}`}position={Position.Right}type="source"/><Handleid={`${LEFT_HANDLE_ID_PREFIX}${field.id}`}- className={`!h-4 !w-4 !border-2 !bg-pink-600 ${!focused || readonly ? '!invisible' : ''}`}+ className={`!h-4 !w-4 !border-2 !bg-pink-600 ${!focused || readonly || isTargetFromView ? '!invisible' : ''}`}position={Position.Left}type="source"/>diff --git a/src/pages/editor-page/canvas/table-node/table-node.tsx b/src/pages/editor-page/canvas/table-node/table-node.tsxindex 910aa651..de98a076 100644--- a/src/pages/editor-page/canvas/table-node/table-node.tsx+++ b/src/pages/editor-page/canvas/table-node/table-node.tsx@@ -6,7 +6,7 @@ import React, {useEffect,} from 'react';import type { NodeProps, Node } from '@xyflow/react';-import { NodeResizer, useStore } from '@xyflow/react';+import { NodeResizer, useConnection, useStore } from '@xyflow/react';import { Button } from '@/components/button/button';import {ChevronsLeftRight,@@ -80,6 +80,14 @@ export const TableNode: React.FC<NodeProps<TableNodeType>> = React.memo(const inputRef = React.useRef<HTMLInputElement>(null);const [isHovering, setIsHovering] = useState(false);+ const connection = useConnection();++ const isTarget = useMemo(() => {+ if (!isHovering) return false;++ return connection.inProgress && connection.fromNode.id !== table.id;+ }, [connection, table.id, isHovering]);+const {getTableNewName,getTableNewColor,@@ -298,7 +306,7 @@ export const TableNode: React.FC<NodeProps<TableNodeType>> = React.memo(() =>cn('flex w-full flex-col border-2 bg-slate-50 dark:bg-slate-950 rounded-lg shadow-sm transition-transform duration-300',- selected+ selected || isTarget? 'border-pink-600': 'border-slate-500 dark:border-slate-700',isOverlapping@@ -335,6 +343,7 @@ export const TableNode: React.FC<NodeProps<TableNodeType>> = React.memo(isDiffTableChanged,isDiffNewTable,isDiffTableRemoved,+ isTarget,]);