Skip to content

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

feat(sql-editor): support search databases and add loading spin ui (#293)

Yunwei Xiao5a1a84cf734b
diff --git a/frontend/src/App.vue b/frontend/src/App.vue
index 877b0a7262..ee030dcefe 100644
--- a/frontend/src/App.vue
+++ b/frontend/src/App.vue
@@ -21,7 +21,7 @@
</template>
<script lang="ts">
-import { reactive, watchEffect, onErrorCaptured } from "vue";
+import { reactive, watchEffect, onErrorCaptured, defineComponent } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
@@ -47,7 +47,7 @@ interface LocalState {
prevLoggedIn: boolean;
}
-export default {
+export default defineComponent({
name: "App",
components: {
KBarWrapper,
@@ -141,5 +141,11 @@ export default {
removeNotification,
};
},
-};
+});
</script>
+
+<style>
+body {
+ @apply text-base;
+}
+</style>
diff --git a/frontend/src/components/ProvideSqlEditorContext.vue b/frontend/src/components/ProvideSqlEditorContext.vue
index 9561a60a06..788a980e13 100644
--- a/frontend/src/components/ProvideSqlEditorContext.vue
+++ b/frontend/src/components/ProvideSqlEditorContext.vue
@@ -5,26 +5,36 @@
<script lang="ts" setup>
import { watchEffect } from "vue";
import { useStore } from "vuex";
-import { Instance, Database, Table, ConnectionAtom } from "../types";
-// import { idFromSlug } from "../utils";
+import {
+ Instance,
+ Database,
+ Table,
+ ConnectionAtom,
+ ConnectionAtomType,
+} from "../types";
const store = useStore();
const prepareSqlEdtiorContext = async function () {
+ store.dispatch("sqlEditor/setConnectionContext", {
+ isLoadingTree: true,
+ });
let connectionTree = [];
- const mapConnectionAtom = (item: Instance | Database | Table) => {
- const connectionAtom: ConnectionAtom = {
- id: item.id,
- key: item.id,
- label: item.name,
- };
+ const mapConnectionAtom =
+ (type: ConnectionAtomType) => (item: Instance | Database | Table) => {
+ const connectionAtom: ConnectionAtom = {
+ id: item.id,
+ key: item.id,
+ label: item.name,
+ type,
+ };
- return connectionAtom;
- };
+ return connectionAtom;
+ };
const instanceList = await store.dispatch("instance/fetchInstanceList");
- connectionTree = instanceList.map(mapConnectionAtom);
+ connectionTree = instanceList.map(mapConnectionAtom("instance"));
for (const instance of instanceList) {
const databaseList = await store.dispatch(
@@ -35,7 +45,7 @@ const prepareSqlEdtiorContext = async function () {
const instanceItem = connectionTree.find(
(item: ConnectionAtom) => item.id === instance.id
);
- instanceItem.children = databaseList.map(mapConnectionAtom);
+ instanceItem.children = databaseList.map(mapConnectionAtom("database"));
for (const db of databaseList) {
const tableList = await store.dispatch(
@@ -47,11 +57,14 @@ const prepareSqlEdtiorContext = async function () {
(item: ConnectionAtom) => item.id === db.id
);
- databaseItem.children = tableList.map(mapConnectionAtom);
+ databaseItem.children = tableList.map(mapConnectionAtom("table"));
}
}
store.dispatch("sqlEditor/setConnectionTree", connectionTree);
+ store.dispatch("sqlEditor/setConnectionContext", {
+ isLoadingTree: false,
+ });
};
watchEffect(prepareSqlEdtiorContext);
diff --git a/frontend/src/store/modules/sqlEditor.ts b/frontend/src/store/modules/sqlEditor.ts
index 2bec489fcc..f47fd0b0c4 100644
--- a/frontend/src/store/modules/sqlEditor.ts
+++ b/frontend/src/store/modules/sqlEditor.ts
@@ -1,18 +1,26 @@
import { isEmpty } from "lodash-es";
-import { SqlEditorState, ConnectionAtom, QueryInfo } from "../../types";
+import {
+ SqlEditorState,
+ ConnectionAtom,
+ QueryInfo,
+ ConnectionContext,
+} from "../../types";
import * as types from "../mutation-types";
import { makeActions } from "../actions";
const state: () => SqlEditorState = () => ({
connectionTree: [],
connectionContext: {
+ hasSlug: false,
instanceId: 0,
instanceName: "",
databaseId: 0,
databaseName: "",
tableId: 0,
tableName: "",
+ isLoadingTree: false,
+ selectedTableId: 0,
},
queryStatement: "",
selectedStatement: "",
@@ -85,12 +93,19 @@ const mutations = {
[types.SET_QUERY_RESULT](state: SqlEditorState, payload: Array<any>) {
state.queryResult = payload;
},
+ [types.SET_CONNECTION_CONTEXT](
+ state: SqlEditorState,
+ payload: Partial<ConnectionContext>
+ ) {
+ Object.assign(state.connectionContext, payload);
+ },
};
type SqlEditorActionsMap = {
setSqlEditorState: typeof mutations.SET_SQL_EDITOR_STATE;
setConnectionTree: typeof mutations.SET_CONNECTION_TREE;
setQueryResult: typeof mutations.SET_QUERY_RESULT;
+ setConnectionContext: typeof mutations.SET_CONNECTION_CONTEXT;
};
const actions = {
@@ -98,6 +113,7 @@ const actions = {
setSqlEditorState: types.SET_SQL_EDITOR_STATE,
setConnectionTree: types.SET_CONNECTION_TREE,
setQueryResult: types.SET_QUERY_RESULT,
+ setConnectionContext: types.SET_CONNECTION_CONTEXT,
}),
async executeQuery(
{ commit, dispatch, state }: any,
@@ -134,12 +150,13 @@ const actions = {
);
commit(types.SET_SQL_EDITOR_STATE, {
queryResult: [],
- connectionContext: {
- instanceId,
- instanceName: instanceInfo.name,
- databaseId,
- databaseName: databaseInfo.name,
- },
+ });
+ commit(types.SET_CONNECTION_CONTEXT, {
+ hasSlug: true,
+ instanceId,
+ instanceName: instanceInfo.name,
+ databaseId,
+ databaseName: databaseInfo.name,
});
},
};
diff --git a/frontend/src/store/mutation-types.ts b/frontend/src/store/mutation-types.ts
index ff290efaa8..4f95d7d093 100644
--- a/frontend/src/store/mutation-types.ts
+++ b/frontend/src/store/mutation-types.ts
@@ -2,3 +2,4 @@
export const SET_SQL_EDITOR_STATE = "SET_SQL_EDITOR_STATE";
export const SET_CONNECTION_TREE = "SET_CONNECTION_TREE";
export const SET_QUERY_RESULT = "SET_QUERY_RESULT";
+export const SET_CONNECTION_CONTEXT = "SET_CONNECTION_CONTEXT";
diff --git a/frontend/src/types/sqlEditor.ts b/frontend/src/types/sqlEditor.ts
index 40dbb215b3..419a4d65ba 100644
--- a/frontend/src/types/sqlEditor.ts
+++ b/frontend/src/types/sqlEditor.ts
@@ -6,10 +6,13 @@ export type EditorModel = monaco.editor.ITextModel;
export type EditorPosition = monaco.Position;
export type CompletionItems = monaco.languages.CompletionItem[];
+export type ConnectionAtomType = "instance" | "database" | "table" | "view";
+
export interface ConnectionAtom {
id: InstanceId | DatabaseId | TableId | ViewId;
key: InstanceId | DatabaseId | TableId | ViewId;
label: string;
+ type?: ConnectionAtomType;
children?: ConnectionAtom[];
}
@@ -22,10 +25,13 @@ export enum SortText {
}
export type ConnectionContext = {
+ hasSlug: boolean;
instanceId: InstanceId;
instanceName: string;
databaseId?: DatabaseId;
databaseName?: string;
tableId?: TableId;
tableName?: string;
+ isLoadingTree: boolean;
+ selectedTableId: number;
};
diff --git a/frontend/src/views/SqlEditor/AsidePanel/DatabasesTree.vue b/frontend/src/views/SqlEditor/AsidePanel/DatabasesTree.vue
index 2ce49fafca..eb71a40d4f 100644
--- a/frontend/src/views/SqlEditor/AsidePanel/DatabasesTree.vue
+++ b/frontend/src/views/SqlEditor/AsidePanel/DatabasesTree.vue
@@ -1,17 +1,74 @@
<template>
- <div class="databases-tree p-2">
- <NTree
+ <div
+ class="databases-tree p-2 space-y-2 overflow-x-auto"
+ v-if="!connectionContext.isLoadingTree"
+ >
+ <NInput v-model:value="searchPattern" placeholder="Search Databases" />
+ <NTree
block-line
:data="connectionTree"
+ :pattern="searchPattern"
+ :default-expanded-keys="defaultExpanedKeys"
+ :default-selected-keys="defaultSelectedKeys"
+ :on-update:selected-keys="handleSelectedKeysChange"
/>
</div>
+ <div v-else class="flex justify-center items-center h-full">
+ <BBSpin title="Loading Databases..." />
+ </div>
</template>
<script lang="ts" setup>
-import { computed } from "vue";
-import { useStore } from "vuex";
+import { ref, computed } from "vue";
+import {
+ useNamespacedState,
+ useNamespacedActions,
+} from "vuex-composition-helpers";
+
+import type {
+ SqlEditorState,
+ ConnectionAtom,
+ SqlEditorActions,
+} from "../../../types";
+
+const searchPattern = ref();
+const { connectionTree, connectionContext } =
+ useNamespacedState<SqlEditorState>("sqlEditor", [
+ "connectionTree",
+ "connectionContext",
+ ]);
+const { setConnectionContext } = useNamespacedActions<SqlEditorActions>(
+ "sqlEditor",
+ ["setConnectionContext"]
+);
+
+const defaultExpanedKeys = computed(() => {
+ const ctx = connectionContext.value;
+ if (ctx.hasSlug) {
+ return [ctx.instanceId, ctx.databaseId];
+ } else {
+ [];
+ }
+});
-const store = useStore();
+const defaultSelectedKeys = computed(() => {
+ const ctx = connectionContext.value;
+ if (ctx.hasSlug) {
+ return [ctx.databaseId];
+ } else {
+ [];
+ }
+});
-const connectionTree = computed(() => store.state.sqlEditor.connectionTree);
+const handleSelectedKeysChange = (
+ keys: number[],
+ options: Array<ConnectionAtom>
+) => {
+ const [selectedItem] = options;
+ if (selectedItem.type === "table") {
+ setConnectionContext({
+ selectedTableId: selectedItem.id,
+ });
+ }
+};
</script>
diff --git a/frontend/src/views/SqlEditor/EditorPanel/EditorActions.vue b/frontend/src/views/SqlEditor/EditorPanel/EditorActions.vue
index 427dd7b904..ddbe1566a3 100644
--- a/frontend/src/views/SqlEditor/EditorPanel/EditorActions.vue
+++ b/frontend/src/views/SqlEditor/EditorPanel/EditorActions.vue
@@ -35,6 +35,7 @@
import { ref, watch } from "vue";
import { CascaderOption } from "naive-ui";
import { useStore } from "vuex";
+import { cloneDeep } from "lodash-es";
import {
useNamespacedState,
@@ -57,10 +58,10 @@ const { isEmptyStatement } = useNamespacedGetters<SqlEditorGetters>(
"sqlEditor",
["isEmptyStatement", "connectionInfo"]
);
-const { executeQuery, setSqlEditorState } =
+const { executeQuery, setConnectionContext } =
useNamespacedActions<SqlEditorActions>("sqlEditor", [
"executeQuery",
- "setSqlEditorState",
+ "setConnectionContext",
]);
const selectedConnection = ref();
@@ -88,10 +89,10 @@ watch(
() => connectionTree.value,
(newVal) => {
if (newVal) {
- selectedConnection.value =
- connectionContext.value.tableId ||
- connectionContext.value.databaseId ||
- connectionContext.value.instanceId;
+ const ctx = connectionContext.value;
+ selectedConnection.value = ctx.hasSlug
+ ? ctx.tableId || ctx.databaseId || ctx.instanceId
+ : null;
}
}
);
@@ -104,27 +105,29 @@ const handleConnectionChange = (
isSeletedDatabase.value = pathValues.length >= 2;
if (pathValues.length >= 1) {
- let connectionContext: ConnectionContext = {
- instanceId: 0,
- instanceName: "",
- };
+ let ctx: ConnectionContext = cloneDeep(connectionContext.value);
const [instanceInfo, databaseInfo, tableInfo] = pathValues;
if (pathValues.length >= 1) {
- connectionContext.instanceId = instanceInfo.id as number;
- connectionContext.instanceName = instanceInfo.label as string;
+ ctx.instanceId = instanceInfo.id as number;
+ ctx.instanceName = instanceInfo.label as string;
}
if (pathValues.length >= 2) {
- connectionContext.databaseId = databaseInfo.id as number;
- connectionContext.databaseName = databaseInfo.label as string;
+ ctx.databaseId = databaseInfo.id as number;
+ ctx.databaseName = databaseInfo.label as string;
}
if (pathValues.length >= 3) {
- connectionContext.tableId = tableInfo.id as number;
- connectionContext.tableName = tableInfo.label as string;
+ ctx.tableId = tableInfo.id as number;
+ ctx.tableName = tableInfo.label as string;
}
- setSqlEditorState({
- connectionContext,
+ setConnectionContext({
+ instanceId: ctx.instanceId,
+ instanceName: ctx.instanceName,
+ databaseId: ctx.databaseId,
+ databaseName: ctx.databaseName,
+ tableId: ctx.tableId,
+ tableName: ctx.tableName,
});
}
};