Database diagrams editor that allows you to visualize and design your DB with a single query.
TypeScript databasedbdb-migrationeditorerdmariadbmssqlmysqlpostgresqlreactreact-flowschema-migrationssqlsqlitetypescriptvisualizationxyflow
feat(dbml-import): add error highlighting for dbml imports (#556)
Jonathan Fishner190e4f4ffa83
diff --git a/src/dialogs/import-dbml-dialog/import-dbml-dialog.tsx b/src/dialogs/import-dbml-dialog/import-dbml-dialog.tsxindex 7d0081ab..b2744f22 100644--- a/src/dialogs/import-dbml-dialog/import-dbml-dialog.tsx+++ b/src/dialogs/import-dbml-dialog/import-dbml-dialog.tsx@@ -1,4 +1,11 @@-import React, { useCallback, useEffect, useState, Suspense } from 'react';+import React, {+ useCallback,+ useEffect,+ useState,+ Suspense,+ useRef,+} from 'react';+import * as monaco from 'monaco-editor';import { useDialog } from '@/hooks/use-dialog';import {Dialog,@@ -23,6 +30,46 @@ import { useCanvas } from '@/hooks/use-canvas';import { setupDBMLLanguage } from '@/components/code-snippet/languages/dbml-language';import { useToast } from '@/components/toast/use-toast';import { Spinner } from '@/components/spinner/spinner';+import { debounce } from '@/lib/utils';++interface DBMLError {+ message: string;+ line: number;+ column: number;+}++function parseDBMLError(error: unknown): DBMLError | null {+ try {+ if (typeof error === 'string') {+ const parsed = JSON.parse(error);+ if (parsed.diags?.[0]) {+ const diag = parsed.diags[0];+ return {+ message: diag.message,+ line: diag.location.start.line,+ column: diag.location.start.column,+ };+ }+ } else if (error && typeof error === 'object' && 'diags' in error) {+ const parsed = error as {+ diags: Array<{+ message: string;+ location: { start: { line: number; column: number } };+ }>;+ };+ if (parsed.diags?.[0]) {+ return {+ message: parsed.diags[0].message,+ line: parsed.diags[0].location.start.line,+ column: parsed.diags[0].location.start.column,+ };+ }+ }+ } catch (e) {+ console.error('Error parsing DBML error:', e);+ }+ return null;+}export interface ImportDBMLDialogProps extends BaseDialogProps {}@@ -75,6 +122,16 @@ Ref: comments.user_id > users.id // Each comment is written by one user`;} = useChartDB();const { reorderTables } = useCanvas();const [reorder, setReorder] = useState(false);+ const editorRef = useRef<monaco.editor.IStandaloneCodeEditor>();+ const decorationsCollection =+ useRef<monaco.editor.IEditorDecorationsCollection>();++ const handleEditorDidMount = (+ editor: monaco.editor.IStandaloneCodeEditor+ ) => {+ editorRef.current = editor;+ decorationsCollection.current = editor.createDecorationsCollection();+ };useEffect(() => {if (reorder) {@@ -85,34 +142,97 @@ Ref: comments.user_id > users.id // Each comment is written by one user`;}}, [reorder, reorderTables]);- useEffect(() => {- if (!dialog.open) return;- setErrorMessage(undefined);- setDBMLContent(initialDBML);- }, [dialog.open, initialDBML]);+ const highlightErrorLine = useCallback((error: DBMLError) => {+ if (!editorRef.current) return;- useEffect(() => {- const validateDBML = async () => {- if (!dbmlContent.trim()) {- setErrorMessage(undefined);- return;- }+ const model = editorRef.current.getModel();+ if (!model) return;++ const decorations = [+ {+ range: new monaco.Range(+ error.line,+ 1,+ error.line,+ model.getLineMaxColumn(error.line)+ ),+ options: {+ isWholeLine: true,+ className: 'dbml-error-line',+ glyphMarginClassName: 'dbml-error-glyph',+ hoverMessage: { value: error.message },+ overviewRuler: {+ color: '#ff0000',+ position: monaco.editor.OverviewRulerLane.Right,+ darkColor: '#ff0000',+ },+ },+ },+ ];++ decorationsCollection.current?.set(decorations);+ }, []);++ const clearDecorations = useCallback(() => {+ decorationsCollection.current?.clear();+ }, []);++ const validateDBML = useCallback(+ async (content: string) => {+ // Clear previous errors+ setErrorMessage(undefined);+ clearDecorations();++ if (!content.trim()) return;try {const parser = new Parser();- parser.parse(dbmlContent, 'dbml');- setErrorMessage(undefined);+ parser.parse(content, 'dbml');} catch (e) {- setErrorMessage(- e instanceof Error- ? e.message- : t('import_dbml_dialog.error.description')- );+ const parsedError = parseDBMLError(e);+ if (parsedError) {+ setErrorMessage(+ t('import_dbml_dialog.error.description') ++ ` (1 error found - in line ${parsedError.line})`+ );+ highlightErrorLine(parsedError);+ } else {+ setErrorMessage(+ e instanceof Error ? e.message : JSON.stringify(e)+ );+ }}+ },+ [clearDecorations, highlightErrorLine, t]+ );++ const debouncedValidateRef = useRef<((value: string) => void) | null>(null);++ // Set up debounced validation+ useEffect(() => {+ debouncedValidateRef.current = debounce((value: string) => {+ validateDBML(value);+ }, 500);++ return () => {+ debouncedValidateRef.current = null;};+ }, [validateDBML]);- validateDBML();- }, [dbmlContent, t]);+ // Trigger validation when content changes+ useEffect(() => {+ if (debouncedValidateRef.current) {+ debouncedValidateRef.current(dbmlContent);+ }+ }, [dbmlContent]);++ useEffect(() => {+ if (!dialog.open) {+ setErrorMessage(undefined);+ clearDecorations();+ setDBMLContent(initialDBML);+ }+ }, [dialog.open, initialDBML, clearDecorations]);const handleImport = useCallback(async () => {if (!dbmlContent.trim() || errorMessage) return;@@ -177,7 +297,7 @@ Ref: comments.user_id > users.id // Each comment is written by one user`;description: (<><div>{t('import_dbml_dialog.error.description')}</div>- {e instanceof Error ? <div>{e.message}</div> : null}+ {e instanceof Error ? e.message : JSON.stringify(e)}</>),});@@ -222,6 +342,7 @@ Ref: comments.user_id > users.id // Each comment is written by one user`;value={dbmlContent}onChange={(value) => setDBMLContent(value || '')}language="dbml"+ onMount={handleEditorDidMount}theme={effectiveTheme === 'dark'? 'dbml-dark'@@ -232,6 +353,8 @@ Ref: comments.user_id > users.id // Each comment is written by one user`;minimap: { enabled: false },scrollBeyondLastLine: false,automaticLayout: true,+ glyphMargin: true,+ lineNumbers: 'on',scrollbar: {vertical: 'visible',horizontal: 'visible',diff --git a/src/globals.css b/src/globals.cssindex d815a92c..05842766 100644--- a/src/globals.css+++ b/src/globals.css@@ -109,6 +109,10 @@animation: rainbow-text-simple-animation 0.5s ease-in forwards;}+.dbml-error-line {+ background-color: rgba(255, 0, 0, 0.2) !important;+}+@keyframes rainbow-text-simple-animation-rev {0% {background-size: 650%;diff --git a/src/i18n/locales/ar.ts b/src/i18n/locales/ar.tsindex 87cc5446..84d8b456 100644--- a/src/i18n/locales/ar.ts+++ b/src/i18n/locales/ar.ts@@ -383,7 +383,7 @@ export const ar: LanguageTranslation = {cancel: 'Cancel',error: {title: 'Error',- description: 'Failed to import DBML. Please check the syntax.',+ description: 'Failed to parse DBML. Please check the syntax.',},},relationship_type: {diff --git a/src/i18n/locales/bn.ts b/src/i18n/locales/bn.tsindex 07cb239a..84a68ec2 100644--- a/src/i18n/locales/bn.ts+++ b/src/i18n/locales/bn.ts@@ -387,7 +387,7 @@ export const bn: LanguageTranslation = {cancel: 'Cancel',error: {title: 'Error',- description: 'Failed to import DBML. Please check the syntax.',+ description: 'Failed to parse DBML. Please check the syntax.',},},relationship_type: {diff --git a/src/i18n/locales/de.ts b/src/i18n/locales/de.tsindex 6a4685c5..59ffab13 100644--- a/src/i18n/locales/de.ts+++ b/src/i18n/locales/de.ts@@ -390,7 +390,7 @@ export const de: LanguageTranslation = {cancel: 'Cancel',error: {title: 'Error',- description: 'Failed to import DBML. Please check the syntax.',+ description: 'Failed to parse DBML. Please check the syntax.',},},relationship_type: {diff --git a/src/i18n/locales/en.ts b/src/i18n/locales/en.tsindex a739ff5f..96a8f452 100644--- a/src/i18n/locales/en.ts+++ b/src/i18n/locales/en.ts@@ -382,7 +382,7 @@ export const en = {cancel: 'Cancel',error: {title: 'Error importing DBML',- description: 'Failed to import DBML. Please check the syntax.',+ description: 'Failed to parse DBML. Please check the syntax.',},},relationship_type: {diff --git a/src/i18n/locales/es.ts b/src/i18n/locales/es.tsindex ef356899..dae2a55f 100644--- a/src/i18n/locales/es.ts+++ b/src/i18n/locales/es.ts@@ -389,7 +389,7 @@ export const es: LanguageTranslation = {cancel: 'Cancel',error: {title: 'Error',- description: 'Failed to import DBML. Please check the syntax.',+ description: 'Failed to parse DBML. Please check the syntax.',},},relationship_type: {diff --git a/src/i18n/locales/fr.ts b/src/i18n/locales/fr.tsindex a02a5302..b1f8a0a2 100644--- a/src/i18n/locales/fr.ts+++ b/src/i18n/locales/fr.ts@@ -391,7 +391,7 @@ export const fr: LanguageTranslation = {cancel: 'Cancel',error: {title: 'Error',- description: 'Failed to import DBML. Please check the syntax.',+ description: 'Failed to parse DBML. Please check the syntax.',},},relationship_type: {diff --git a/src/i18n/locales/gu.ts b/src/i18n/locales/gu.tsindex 33c1bbea..82eae238 100644--- a/src/i18n/locales/gu.ts+++ b/src/i18n/locales/gu.ts@@ -387,7 +387,7 @@ export const gu: LanguageTranslation = {cancel: 'Cancel',error: {title: 'Error',- description: 'Failed to import DBML. Please check the syntax.',+ description: 'Failed to parse DBML. Please check the syntax.',},},relationship_type: {diff --git a/src/i18n/locales/hi.ts b/src/i18n/locales/hi.tsindex 7c2e5b95..e582ba20 100644--- a/src/i18n/locales/hi.ts+++ b/src/i18n/locales/hi.ts@@ -391,7 +391,7 @@ export const hi: LanguageTranslation = {cancel: 'Cancel',error: {title: 'Error',- description: 'Failed to import DBML. Please check the syntax.',+ description: 'Failed to parse DBML. Please check the syntax.',},},relationship_type: {diff --git a/src/i18n/locales/id_ID.ts b/src/i18n/locales/id_ID.tsindex a8d90795..89f57618 100644--- a/src/i18n/locales/id_ID.ts+++ b/src/i18n/locales/id_ID.ts@@ -385,7 +385,7 @@ export const id_ID: LanguageTranslation = {cancel: 'Cancel',error: {title: 'Error',- description: 'Failed to import DBML. Please check the syntax.',+ description: 'Failed to parse DBML. Please check the syntax.',},},diff --git a/src/i18n/locales/ja.ts b/src/i18n/locales/ja.tsindex 1864952f..e8f6a053 100644--- a/src/i18n/locales/ja.ts+++ b/src/i18n/locales/ja.ts@@ -394,7 +394,7 @@ export const ja: LanguageTranslation = {cancel: 'Cancel',error: {title: 'Error',- description: 'Failed to import DBML. Please check the syntax.',+ description: 'Failed to parse DBML. Please check the syntax.',},},relationship_type: {diff --git a/src/i18n/locales/ko_KR.ts b/src/i18n/locales/ko_KR.tsindex 93e709cd..8d571bbf 100644--- a/src/i18n/locales/ko_KR.ts+++ b/src/i18n/locales/ko_KR.ts@@ -383,7 +383,7 @@ export const ko_KR: LanguageTranslation = {cancel: 'Cancel',error: {title: 'Error',- description: 'Failed to import DBML. Please check the syntax.',+ description: 'Failed to parse DBML. Please check the syntax.',},},relationship_type: {diff --git a/src/i18n/locales/mr.ts b/src/i18n/locales/mr.tsindex 3e541773..c5d3dbf0 100644--- a/src/i18n/locales/mr.ts+++ b/src/i18n/locales/mr.ts@@ -395,7 +395,7 @@ export const mr: LanguageTranslation = {cancel: 'Cancel',error: {title: 'Error',- description: 'Failed to import DBML. Please check the syntax.',+ description: 'Failed to parse DBML. Please check the syntax.',},},diff --git a/src/i18n/locales/ne.ts b/src/i18n/locales/ne.tsindex dee85ad1..db45f9d2 100644--- a/src/i18n/locales/ne.ts+++ b/src/i18n/locales/ne.ts@@ -388,7 +388,7 @@ export const ne: LanguageTranslation = {cancel: 'Cancel',error: {title: 'Error',- description: 'Failed to import DBML. Please check the syntax.',+ description: 'Failed to parse DBML. Please check the syntax.',},},diff --git a/src/i18n/locales/pt_BR.ts b/src/i18n/locales/pt_BR.tsindex 979448ad..92c79aa6 100644--- a/src/i18n/locales/pt_BR.ts+++ b/src/i18n/locales/pt_BR.ts@@ -388,7 +388,7 @@ export const pt_BR: LanguageTranslation = {cancel: 'Cancel',error: {title: 'Error',- description: 'Failed to import DBML. Please check the syntax.',+ description: 'Failed to parse DBML. Please check the syntax.',},},relationship_type: {diff --git a/src/i18n/locales/ru.ts b/src/i18n/locales/ru.tsindex bba34a8d..f9a310ed 100644--- a/src/i18n/locales/ru.ts+++ b/src/i18n/locales/ru.ts@@ -384,7 +384,7 @@ export const ru: LanguageTranslation = {cancel: 'Cancel',error: {title: 'Error',- description: 'Failed to import DBML. Please check the syntax.',+ description: 'Failed to parse DBML. Please check the syntax.',},},relationship_type: {diff --git a/src/i18n/locales/te.ts b/src/i18n/locales/te.tsindex 6862fede..b66add6c 100644--- a/src/i18n/locales/te.ts+++ b/src/i18n/locales/te.ts@@ -391,7 +391,7 @@ export const te: LanguageTranslation = {cancel: 'Cancel',error: {title: 'Error',- description: 'Failed to import DBML. Please check the syntax.',+ description: 'Failed to parse DBML. Please check the syntax.',},},diff --git a/src/i18n/locales/tr.ts b/src/i18n/locales/tr.tsindex e5e2f0a8..c69771cf 100644--- a/src/i18n/locales/tr.ts+++ b/src/i18n/locales/tr.ts@@ -378,7 +378,7 @@ export const tr: LanguageTranslation = {cancel: 'Cancel',error: {title: 'Error',- description: 'Failed to import DBML. Please check the syntax.',+ description: 'Failed to parse DBML. Please check the syntax.',},},relationship_type: {diff --git a/src/i18n/locales/uk.ts b/src/i18n/locales/uk.tsindex 78cda269..61978a09 100644--- a/src/i18n/locales/uk.ts+++ b/src/i18n/locales/uk.ts@@ -383,7 +383,7 @@ export const uk: LanguageTranslation = {cancel: 'Cancel',error: {title: 'Error',- description: 'Failed to import DBML. Please check the syntax.',+ description: 'Failed to parse DBML. Please check the syntax.',},},relationship_type: {diff --git a/src/i18n/locales/vi.ts b/src/i18n/locales/vi.tsindex d53cc888..1047f0f3 100644--- a/src/i18n/locales/vi.ts+++ b/src/i18n/locales/vi.ts@@ -384,7 +384,7 @@ export const vi: LanguageTranslation = {cancel: 'Cancel',error: {title: 'Error',- description: 'Failed to import DBML. Please check the syntax.',+ description: 'Failed to parse DBML. Please check the syntax.',},},relationship_type: {diff --git a/src/i18n/locales/zh_CN.ts b/src/i18n/locales/zh_CN.tsindex e9d6daef..6876146f 100644--- a/src/i18n/locales/zh_CN.ts+++ b/src/i18n/locales/zh_CN.ts@@ -380,7 +380,7 @@ export const zh_CN: LanguageTranslation = {cancel: 'Cancel',error: {title: 'Error',- description: 'Failed to import DBML. Please check the syntax.',+ description: 'Failed to parse DBML. Please check the syntax.',},},relationship_type: {diff --git a/src/i18n/locales/zh_TW.ts b/src/i18n/locales/zh_TW.tsindex 2ccb4429..e77ebe2d 100644--- a/src/i18n/locales/zh_TW.ts+++ b/src/i18n/locales/zh_TW.ts@@ -379,7 +379,7 @@ export const zh_TW: LanguageTranslation = {cancel: 'Cancel',error: {title: 'Error',- description: 'Failed to import DBML. Please check the syntax.',+ description: 'Failed to parse DBML. Please check the syntax.',},},relationship_type: {