Skip to content

Database diagrams editor that allows you to visualize and design your DB with a single query.

fix(import-json): for broken json imports (#697)

Jonathan Fishner2368e0d26390
diff --git a/src/dialogs/common/import-database/import-database.tsx b/src/dialogs/common/import-database/import-database.tsx
index 797de58f..c2c50df6 100644
--- a/src/dialogs/common/import-database/import-database.tsx
+++ b/src/dialogs/common/import-database/import-database.tsx
@@ -1,4 +1,10 @@
-import React, { Suspense, useCallback, useEffect, useState } from 'react';
+import React, {
+ Suspense,
+ useCallback,
+ useEffect,
+ useState,
+ useRef,
+} from 'react';
import { Button } from '@/components/button/button';
import {
DialogClose,
@@ -67,6 +73,7 @@ export const ImportDatabase: React.FC<ImportDatabaseProps> = ({
}) => {
const { effectiveTheme } = useTheme();
const [errorMessage, setErrorMessage] = useState('');
+ const editorRef = useRef<editor.IStandaloneCodeEditor | null>(null);
const { t } = useTranslation();
const { isSm: isDesktop } = useBreakpoint('sm');
@@ -134,6 +141,16 @@ export const ImportDatabase: React.FC<ImportDatabaseProps> = ({
}
}, [errorMessage.length, onImport, scriptResult]);
+ const formatEditor = useCallback(() => {
+ if (editorRef.current) {
+ setTimeout(() => {
+ editorRef.current
+ ?.getAction('editor.action.formatDocument')
+ ?.run();
+ }, 50);
+ }
+ }, []);
+
const handleInputChange: OnChange = useCallback(
(inputValue) => {
setScriptResult(inputValue ?? '');
@@ -156,17 +173,20 @@ export const ImportDatabase: React.FC<ImportDatabaseProps> = ({
if (isStringMetadataJson(fixedJson)) {
setScriptResult(fixedJson);
setErrorMessage('');
+ formatEditor();
} else {
setScriptResult(fixedJson);
setErrorMessage(errorScriptOutputMessage);
+ formatEditor();
}
setShowCheckJsonButton(false);
setIsCheckingJson(false);
- }, [scriptResult, setScriptResult]);
+ }, [scriptResult, setScriptResult, formatEditor]);
const handleEditorDidMount = useCallback(
(editor: editor.IStandaloneCodeEditor) => {
+ editorRef.current = editor;
editor.onDidPaste(() => {
setTimeout(() => {
editor.getAction('editor.action.formatDocument')?.run();
diff --git a/src/lib/data/import-metadata/utils.ts b/src/lib/data/import-metadata/utils.ts
index a9463988..00cb08d2 100644
--- a/src/lib/data/import-metadata/utils.ts
+++ b/src/lib/data/import-metadata/utils.ts
@@ -24,6 +24,26 @@ export const fixMetadataJson = async (
.replace(/"""([^",}]+)"""/g, '"$1"') // Remove tripple quotes from keys
.replace(/""([^",}]+)""/g, '"$1"') // Remove double quotes from keys
+ .replace(/'"([^"]+)"'/g, '\\"$1\\"') // Replace single-quoted double quotes
+ .replace(/'(".*?")'/g, "'\\$1'") // Handle cases like '"{}"'::json
+
+ // Handle specific case for nextval with quoted identifiers
+ .replace(
+ /nextval\('(".*?")'::regclass\)/g,
+ "nextval('\\$1'::regclass)"
+ )
+
+ // Handle cases like "'CHAT'::"CustomType"" (ensures existing quotes are escaped for JSON)
+ /* eslint-disable-next-line no-useless-escape */
+ .replace(/'([^']+)'::\"([^\"]+)\"/g, '\'$1\'::\\\"$2\\\"')
+
+ // Convert string "null" to actual null for precision field
+ .replace(/"precision": "null"/g, '"precision": null')
+
+ // Convert string "true"/"false" to actual boolean for nullable field
+ .replace(/"nullable": "false"/g, '"nullable": false')
+ .replace(/"nullable": "true"/g, '"nullable": true')
+
/* eslint-disable-next-line no-useless-escape */
.replace(/\"/g, '___ESCAPED_QUOTE___') // Temporarily replace empty strings
.replace(/(?<=:\s*)""(?=\s*[,}])/g, '___EMPTY___') // Temporarily replace empty strings