Skip to content

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

feat(frontend): better MongoDB support while using MonacoEditor (#4052)

Liu Ji4dd40175407b
diff --git a/frontend/src/components/InstanceEngineIcon.vue b/frontend/src/components/InstanceEngineIcon.vue
index c00f5abe75..3083da068d 100644
--- a/frontend/src/components/InstanceEngineIcon.vue
+++ b/frontend/src/components/InstanceEngineIcon.vue
@@ -2,7 +2,7 @@
<NTooltip>
<template #trigger>
<div class="relative w-4" v-bind="$attrs">
- <img class="h-4 w-auto" :src="SelectedEngineIconPath" />
+ <img class="h-4 w-auto mx-auto" :src="SelectedEngineIconPath" />
<div
v-if="showStatus"
class="bg-green-400 border-surface-high rounded-full absolute border-2"
diff --git a/frontend/src/components/Issue/IssueTaskStatementPanel.vue b/frontend/src/components/Issue/IssueTaskStatementPanel.vue
index c94eba866b..1dedb44a52 100644
--- a/frontend/src/components/Issue/IssueTaskStatementPanel.vue
+++ b/frontend/src/components/Issue/IssueTaskStatementPanel.vue
@@ -7,7 +7,12 @@
class="py-2 text-sm font-medium"
:class="isEmpty(state.editStatement) ? 'text-red-600' : 'text-control'"
>
- {{ $t("common.sql") }}
+ <template v-if="language === 'sql'">
+ {{ $t("common.sql") }}
+ </template>
+ <template v-else>
+ {{ $t("common.statement") }}
+ </template>
<span v-if="create" class="text-red-600">*</span>
<span v-if="sqlHint" class="text-accent">{{ `(${sqlHint})` }}</span>
</div>
@@ -25,7 +30,10 @@
<div class="space-x-2 flex items-center">
<template v-if="create || state.editing">
<!-- mt-0.5 is to prevent jiggling between switching edit/none-edit -->
- <label class="mt-0.5 mr-2 inline-flex items-center gap-1">
+ <label
+ v-if="allowFormatOnSave"
+ class="mt-0.5 mr-2 inline-flex items-center gap-1"
+ >
<input
v-model="formatOnSave"
type="checkbox"
@@ -126,6 +134,7 @@
:value="state.editStatement"
:readonly="!state.editing || !allowEditStatement || isTaskHasSheetId"
:auto-focus="false"
+ :language="language"
:dialect="dialect"
@change="onStatementChange"
@ready="handleMonacoEditorReady"
@@ -185,6 +194,7 @@ import { TableMetadata } from "@/types/proto/store/database";
import MonacoEditor from "../MonacoEditor/MonacoEditor.vue";
import IssueRollbackButton from "./IssueRollbackButton.vue";
import { isUndefined } from "lodash-es";
+import { useInstanceEditorLanguage } from "@/utils";
interface LocalState {
editing: boolean;
@@ -325,6 +335,10 @@ const useTempEditState = (state: LocalState) => {
useTempEditState(state);
+const language = useInstanceEditorLanguage(
+ computed(() => selectedDatabase.value?.instance)
+);
+
const dialect = computed((): SQLDialect => {
const db = selectedDatabase.value;
if (db?.instance.engine === "POSTGRES") {
@@ -339,6 +353,8 @@ const formatOnSave = computed({
set: (value: boolean) => uiStateStore.setIssueFormatStatementOnSave(value),
});
+const allowFormatOnSave = computed(() => language.value === "sql");
+
const allowUploadSheetForTask = computed(() => {
if (!create.value) {
return false;
@@ -391,7 +407,7 @@ const shouldShowStatementEditButton = computed(() => {
return false;
}
- return allowEditStatement;
+ return allowEditStatement.value;
});
onMounted(() => {
@@ -442,7 +458,7 @@ const beginEdit = () => {
};
const saveEdit = () => {
- if (formatOnSave.value) {
+ if (allowFormatOnSave.value && formatOnSave.value) {
editorRef.value?.formatEditorContent();
}
updateStatement(state.editStatement, () => {
diff --git a/frontend/src/components/MonacoEditor/MonacoEditor.vue b/frontend/src/components/MonacoEditor/MonacoEditor.vue
index ec170dad5a..8a5e3caa1e 100644
--- a/frontend/src/components/MonacoEditor/MonacoEditor.vue
+++ b/frontend/src/components/MonacoEditor/MonacoEditor.vue
@@ -16,9 +16,10 @@ import {
shallowRef,
PropType,
onBeforeUnmount,
+ watchEffect,
} from "vue";
import type { editor as Editor } from "monaco-editor";
-import { Database, SQLDialect } from "@/types";
+import { Database, Language, SQLDialect } from "@/types";
import { TableMetadata } from "@/types/proto/store/database";
import { MonacoHelper, useMonaco } from "./useMonaco";
import { useLineDecorations } from "./lineDecorations";
@@ -29,6 +30,10 @@ const props = defineProps({
type: String,
required: true,
},
+ language: {
+ type: String as PropType<Language>,
+ default: "sql",
+ },
dialect: {
type: String as PropType<SQLDialect>,
default: "mysql",
@@ -69,7 +74,7 @@ const initEditorInstance = () => {
const { monaco, formatContent, setPositionAtEndOfLine } =
monacoInstanceRef.value!;
- const model = monaco.editor.createModel(sqlCode.value, "sql");
+ const model = monaco.editor.createModel(sqlCode.value, props.language);
const editorInstance = monaco.editor.create(editorContainerRef.value!, {
model,
theme: "bb",
@@ -100,22 +105,32 @@ const initEditorInstance = () => {
...props.options,
});
- // add `Format SQL` action into context menu
- editorInstance.addAction({
- id: "format-sql",
- label: "Format SQL",
- keybindings: [
- monaco.KeyMod.Alt | monaco.KeyMod.Shift | monaco.KeyCode.KeyF,
- ],
- contextMenuGroupId: "operation",
- contextMenuOrder: 1,
- run: () => {
- if (readOnly.value) {
- return;
- }
- formatContent(editorInstance, dialect.value);
- nextTick(() => setPositionAtEndOfLine(editorInstance));
- },
+ watchEffect((onCleanup) => {
+ if (props.language === "sql") {
+ // add `Format SQL` action into context menu
+ const action = editorInstance.addAction({
+ id: "format-sql",
+ label: "Format SQL",
+ keybindings: [
+ monaco.KeyMod.Alt | monaco.KeyMod.Shift | monaco.KeyCode.KeyF,
+ ],
+ contextMenuGroupId: "operation",
+ contextMenuOrder: 1,
+ run: () => {
+ if (readOnly.value) {
+ return;
+ }
+ formatContent(editorInstance, dialect.value);
+ nextTick(() => setPositionAtEndOfLine(editorInstance));
+ },
+ });
+ onCleanup(() => {
+ action.dispose();
+ });
+ } else {
+ // When the language is "javascript", we can still use Alt+Shift+F to
+ // format the document (the native feature of monaco-editor).
+ }
});
// typed something, change the text
diff --git a/frontend/src/components/MonacoEditor/useMonaco.ts b/frontend/src/components/MonacoEditor/useMonaco.ts
index 5863a7c463..f50225888d 100644
--- a/frontend/src/components/MonacoEditor/useMonaco.ts
+++ b/frontend/src/components/MonacoEditor/useMonaco.ts
@@ -6,10 +6,12 @@ import { getBBTheme } from "./themes/bb";
import { getBBDarkTheme } from "./themes/bb-dark";
export const useMonaco = async () => {
- const [monaco, { default: EditorWorker }] = await Promise.all([
- import("monaco-editor"),
- import("monaco-editor/esm/vs/editor/editor.worker?worker"),
- ]);
+ const [monaco, { default: EditorWorker }, { default: TSWorker }] =
+ await Promise.all([
+ import("monaco-editor"),
+ import("monaco-editor/esm/vs/editor/editor.worker?worker"),
+ import("monaco-editor/esm/vs/language/typescript/ts.worker?worker"),
+ ]);
const bbTheme = getBBTheme();
const bbDarkTheme = getBBDarkTheme();
@@ -19,6 +21,9 @@ export const useMonaco = async () => {
self.MonacoEnvironment = {
getWorker: (workerId, label) => {
console.debug("MonacoEnvironment.getWorker", workerId, label);
+ if (label === "javascript") {
+ return new TSWorker();
+ }
return new EditorWorker();
},
};
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 727a385c22..62c08ac802 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -1419,7 +1419,8 @@
"connect-in-admin-mode": "Connect in admin mode",
"admin-mode": {
"self": "Admin mode"
- }
+ },
+ "read-only-mode-not-allowed": "Instance {instance} is not accessible in read-only mode."
},
"schema-editor": {
"self": "Schema Editor",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 6ef0cc97ff..e30ae5255d 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -1419,7 +1419,8 @@
"connect-in-admin-mode": "以管理员模式连接",
"admin-mode": {
"self": "管理员模式"
- }
+ },
+ "read-only-mode-not-allowed": "实例{instance}无法通过只读模式访问。"
},
"schema-editor": {
"self": "Schema 编辑器",
diff --git a/frontend/src/types/sqlEditor.ts b/frontend/src/types/sqlEditor.ts
index 990dab06c2..0855bb064c 100644
--- a/frontend/src/types/sqlEditor.ts
+++ b/frontend/src/types/sqlEditor.ts
@@ -6,6 +6,7 @@ export type EditorModel = monaco.editor.ITextModel;
export type EditorPosition = monaco.Position;
export type CompletionItems = monaco.languages.CompletionItem[];
+export type Language = "sql" | "javascript";
export type SQLDialect = "mysql" | "postgresql";
export enum SortText {
diff --git a/frontend/src/utils/instance.ts b/frontend/src/utils/instance.ts
index befd58ccc6..a19f678ab1 100644
--- a/frontend/src/utils/instance.ts
+++ b/frontend/src/utils/instance.ts
@@ -1,4 +1,5 @@
-import { Environment, Instance } from "../types";
+import { computed, unref } from "vue";
+import { Environment, Instance, Language, MaybeRef } from "../types";
export function instanceName(instance: Instance) {
let name = instance.name;
@@ -32,3 +33,14 @@ export function sortInstanceList(
return bEnvIndex - aEnvIndex;
});
}
+
+export const useInstanceEditorLanguage = (
+ instance: MaybeRef<Instance | undefined>
+) => {
+ return computed((): Language => {
+ if (unref(instance)?.engine === "MONGODB") {
+ return "javascript";
+ }
+ return "sql";
+ });
+};
diff --git a/frontend/src/views/sql-editor/EditorPanel/SQLEditor.vue b/frontend/src/views/sql-editor/EditorPanel/SQLEditor.vue
index 9e7b85a1fb..6f1615359e 100644
--- a/frontend/src/views/sql-editor/EditorPanel/SQLEditor.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/SQLEditor.vue
@@ -6,6 +6,7 @@
ref="editorRef"
v-model:value="sqlCode"
class="w-full h-full"
+ :language="selectedLanguage"
:dialect="selectedDialect"
:readonly="readonly"
@change="handleChange"
@@ -36,6 +37,7 @@ import type {
SQLDialect,
} from "@/types";
import { TableMetadata } from "@/types/proto/store/database";
+import { useInstanceEditorLanguage } from "@/utils";
const emit = defineEmits<{
(e: "save-sheet", content?: string): void;
@@ -63,6 +65,7 @@ const selectedInstance = useInstanceById(
const selectedInstanceEngine = computed(() => {
return instanceStore.formatEngine(selectedInstance.value);
});
+const selectedLanguage = useInstanceEditorLanguage(selectedInstance);
const selectedDialect = computed((): SQLDialect => {
const engine = selectedInstanceEngine.value;
if (engine === "PostgreSQL") {
diff --git a/frontend/src/views/sql-editor/SQLEditorPage.vue b/frontend/src/views/sql-editor/SQLEditorPage.vue
index 459738a023..7a943b8621 100644
--- a/frontend/src/views/sql-editor/SQLEditorPage.vue
+++ b/frontend/src/views/sql-editor/SQLEditorPage.vue
@@ -6,19 +6,44 @@
<AsidePanel />
</Pane>
<Pane size="80" class="relative">
- <template v-if="allowQuery">
- <Splitpanes
- v-if="tabStore.currentTab.mode === TabMode.ReadOnly"
- horizontal
- class="default-theme"
- >
- <Pane :size="isDisconnected ? 100 : 60">
- <EditorPanel />
- </Pane>
- <Pane :size="isDisconnected ? 0 : 40">
- <TablePanel />
- </Pane>
- </Splitpanes>
+ <template v-if="allowAccess">
+ <template v-if="tabStore.currentTab.mode === TabMode.ReadOnly">
+ <Splitpanes
+ v-if="allowReadOnlyMode"
+ horizontal
+ class="default-theme"
+ >
+ <Pane :size="isDisconnected ? 100 : 60">
+ <EditorPanel />
+ </Pane>
+ <Pane :size="isDisconnected ? 0 : 40">
+ <TablePanel />
+ </Pane>
+ </Splitpanes>
+
+ <div
+ v-else
+ class="w-full h-full flex flex-col items-center justify-center gap-y-2"
+ >
+ <img
+ src="../../assets/illustration/403.webp"
+ class="max-h-[40%]"
+ />
+ <i18n-t
+ class="textinfolabel flex items-center"
+ keypath="sql-editor.read-only-mode-not-allowed"
+ tag="div"
+ >
+ <template #instance>
+ <span class="inline-flex items-center mx-1">
+ <InstanceEngineIcon :instance="instance" />
+ <span>{{ instanceName(instance) }}</span>
+ </span>
+ </template>
+ </i18n-t>
+ <AdminModeButton />
+ </div>
+ </template>
<TerminalPanel v-if="tabStore.currentTab.mode === TabMode.Admin" />
</template>
@@ -49,8 +74,10 @@ import { Splitpanes, Pane } from "splitpanes";
import { TabMode, UNKNOWN_ID } from "@/types";
import {
+ useConnectionTreeStore,
useCurrentUser,
useDatabaseStore,
+ useInstanceById,
useSQLEditorStore,
useTabStore,
} from "@/store";
@@ -60,29 +87,44 @@ import TerminalPanel from "./TerminalPanel/TerminalPanel.vue";
import TabList from "./TabList";
import TablePanel from "./TablePanel/TablePanel.vue";
import { isDatabaseAccessible } from "@/utils";
+import AdminModeButton from "./EditorCommon/AdminModeButton.vue";
const tabStore = useTabStore();
const databaseStore = useDatabaseStore();
+const connectionTreeStore = useConnectionTreeStore();
const sqlEditorStore = useSQLEditorStore();
const currentUser = useCurrentUser();
const isDisconnected = computed(() => tabStore.isDisconnected);
const isFetchingSheet = computed(() => sqlEditorStore.isFetchingSheet);
-const allowQuery = computed(() => {
+const allowAccess = computed(() => {
const { databaseId } = tabStore.currentTab.connection;
const database = databaseStore.getDatabaseById(databaseId);
if (database.id === UNKNOWN_ID) {
// Allowed if connected to an instance
return true;
}
- const { accessControlPolicyList } = sqlEditorStore;
+ const { accessControlPolicyList } = connectionTreeStore;
return isDatabaseAccessible(
database,
accessControlPolicyList,
currentUser.value
);
});
+
+const instance = useInstanceById(
+ computed(() => tabStore.currentTab.connection.instanceId)
+);
+
+const allowReadOnlyMode = computed(() => {
+ if (isDisconnected.value) return true;
+
+ if (instance.value.engine === "MONGODB") {
+ return false;
+ }
+ return true;
+});
</script>
<style>
diff --git a/frontend/src/views/sql-editor/TerminalPanel/CompactSQLEditor.vue b/frontend/src/views/sql-editor/TerminalPanel/CompactSQLEditor.vue
index 50cf8acf2e..083bb78d6d 100644
--- a/frontend/src/views/sql-editor/TerminalPanel/CompactSQLEditor.vue
+++ b/frontend/src/views/sql-editor/TerminalPanel/CompactSQLEditor.vue
@@ -4,24 +4,10 @@
ref="editorRef"
class="w-full h-auto max-h-[360px]"
:value="sql"
+ :language="selectedLanguage"
:dialect="selectedDialect"
:readonly="readonly"
- :options="{
- theme: 'bb-dark',
- minimap: {
- enabled: false,
- },
- scrollbar: {
- vertical: 'hidden',
- horizontal: 'hidden',
- alwaysConsumeMouseWheel: false,
- },
- overviewRulerLanes: 0,
- lineNumbers: getLineNumber,
- lineNumbersMinChars: 5,
- glyphMargin: false,
- cursorStyle: 'block',
- }"
+ :options="EDITOR_OPTIONS"
@change="handleChange"
@change-selection="handleChangeSelection"
@save="handleSaveSheet"
@@ -32,6 +18,7 @@
<script lang="ts" setup>
import { computed, nextTick, ref, watch, watchEffect } from "vue";
+import { editor as Editor } from "monaco-editor";
import {
useInstanceStore,
@@ -44,6 +31,7 @@ import {
import MonacoEditor from "@/components/MonacoEditor/MonacoEditor.vue";
import { Database, ExecuteConfig, ExecuteOption, SQLDialect } from "@/types";
import { TableMetadata } from "@/types/proto/store/database";
+import { useInstanceEditorLanguage } from "@/utils";
const props = defineProps({
sql: {
@@ -83,6 +71,7 @@ const selectedInstance = useInstanceById(
const selectedInstanceEngine = computed(() => {
return instanceStore.formatEngine(selectedInstance.value);
});
+const selectedLanguage = useInstanceEditorLanguage(selectedInstance);
const selectedDialect = computed((): SQLDialect => {
const engine = selectedInstanceEngine.value;
if (engine === "PostgreSQL") {
@@ -110,6 +99,10 @@ watch(
}
);
+const firstLinePrompt = computed(() => {
+ return selectedLanguage.value === "sql" ? "SQL>" : "MONGO>";
+});
+
const getLineNumber = (lineNumber: number) => {
/*
Show a SQL CLI like command prompt.
@@ -117,7 +110,9 @@ const getLineNumber = (lineNumber: number) => {
-> second_line
-> more_lines
*/
- if (lineNumber === 1) return "SQL>";
+ if (lineNumber === 1) {
+ return firstLinePrompt.value;
+ }
return "->";
};
@@ -257,4 +252,25 @@ const updateEditorHeight = () => {
}
editorRef.value?.setEditorContentHeight(actualHeight);
};
+
+const EDITOR_OPTIONS = ref<Editor.IStandaloneEditorConstructionOptions>({
+ theme: "bb-dark",
+ minimap: {
+ enabled: false,
+ },
+ scrollbar: {
+ vertical: "hidden",
+ horizontal: "hidden",
+ alwaysConsumeMouseWheel: false,
+ },
+ overviewRulerLanes: 0,
+ lineNumbers: getLineNumber,
+ lineNumbersMinChars: firstLinePrompt.value.length + 1,
+ glyphMargin: false,
+ cursorStyle: "block",
+});
+watch(
+ firstLinePrompt,
+ (prompt) => (EDITOR_OPTIONS.value.lineNumbersMinChars = prompt.length + 1)
+);
</script>