Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: support load more in database tree (#15744)
ecmadao4996eeb447f4
diff --git a/frontend/src/components/GrantRequestPanel/DatabaseResourceForm/DatabaseResourceSelector.vue b/frontend/src/components/GrantRequestPanel/DatabaseResourceForm/DatabaseResourceSelector.vueindex b727f9c34a..acd8947f56 100644--- a/frontend/src/components/GrantRequestPanel/DatabaseResourceForm/DatabaseResourceSelector.vue+++ b/frontend/src/components/GrantRequestPanel/DatabaseResourceForm/DatabaseResourceSelector.vue@@ -12,7 +12,7 @@:source-filter-placeholder="$t('common.filter-by-name')"/><div- v-if="loading"+ v-if="initializing"class="z-1 absolute inset-0 bg-white bg-opacity-80 flex flex-row justify-center items-center"><BBSpin size="large" />@@ -24,8 +24,9 @@import { useDebounceFn } from "@vueuse/core";import { orderBy, uniqBy } from "lodash-es";import type { TransferRenderSourceList, TreeOption } from "naive-ui";-import { NTransfer, NTree } from "naive-ui";+import { NTransfer, NTree, NButton } from "naive-ui";import { computed, h, onMounted, ref, watch } from "vue";+import { useI18n } from "vue-i18n";import { BBSpin } from "@/bbkit";import {useDatabaseV1Store,@@ -59,6 +60,7 @@ const emit = defineEmits<{const databaseStore = useDatabaseV1Store();const dbSchemaStore = useDBSchemaV1Store();+const { t } = useI18n();const parseResourceToKey = (resource: DatabaseResource): string => {const data = [@@ -87,9 +89,16 @@ const parseResourceToKey = (resource: DatabaseResource): string => {const selectedValueList = ref<string[]>([]);const expandedKeys = ref<string[]>([]);const indeterminateKeys = ref<string[]>([]);-const loading = ref(true);+const initializing = ref(true);const databaseList = ref<ComposedDatabase[]>([]);-const searchText = ref<string>("");+const fetchDataState = ref<{+ searchText: string;+ nextPageToken?: string;+ loading: boolean;+}>({+ loading: false,+ searchText: "",+});const cascadeLoopTreeNode = (treeNode: DatabaseTreeOption,@@ -102,20 +111,42 @@ const cascadeLoopTreeNode = (};const fetchDatabaseList = useDebounceFn(async () => {- const { databases } = await databaseStore.fetchDatabases({- pageSize: getDefaultPagination(),- parent: props.projectName,- filter: {- query: searchText.value,- },- });- databaseList.value = databases;+ fetchDataState.value.loading = true;+ const pageToken = fetchDataState.value.nextPageToken;++ try {+ const { databases, nextPageToken } = await databaseStore.fetchDatabases({+ pageSize: getDefaultPagination(),+ pageToken,+ parent: props.projectName,+ filter: {+ query: fetchDataState.value.searchText,+ },+ });++ if (pageToken) {+ databaseList.value.push(...databases);+ } else {+ databaseList.value = databases;+ }+ fetchDataState.value.nextPageToken = nextPageToken;+ } finally {+ fetchDataState.value.loading = false;+ }}, 500);-watch(() => searchText.value, fetchDatabaseList);+watch(+ () => fetchDataState.value.searchText,+ async () => {+ fetchDataState.value.nextPageToken = "";+ fetchDatabaseList();+ },+ {+ immediate: true,+ }+);onMounted(async () => {- await fetchDatabaseList();await batchGetOrFetchDatabases(props.databaseResources.map((resource) => resource.databaseFullName));@@ -126,7 +157,7 @@ onMounted(async () => {const input = selectorElement?.querySelector("input");if (input) {input.addEventListener("input", () => {- searchText.value = input.value;+ fetchDataState.value.searchText = input.value;});}@@ -204,7 +235,7 @@ onMounted(async () => {expandedKeys.value = [...newExpandedKeys];indeterminateKeys.value = [...newIndeterminateKeys];- loading.value = false;+ initializing.value = false;});const parseKeyToPathes = (key: string): string[] => {@@ -266,110 +297,136 @@ const onTreeNodeLoad = async (node: TreeOption) => {};const renderSourceList: TransferRenderSourceList = ({ onCheck, pattern }) => {- return h(NTree, {- keyField: "value",- cascade: true,- allowCheckingNotLoaded: true,- checkable: true,- selectable: false,- checkOnClick: true,- disabled: props.disabled,- data: sourceTreeOptions.value,- blockLine: true,- virtualScroll: true,- style: "height: 428px", // since <NTransfer> height is 512- renderLabel: ({ option }: { option: TreeOption }) => {- return h(Label, {- option: option as DatabaseTreeOption,- keyword: pattern,- });- },- pattern,- showIrrelevantNodes: false,- expandedKeys: expandedKeys.value,- checkedKeys: selectedValueList.value,- indeterminateKeys: indeterminateKeys.value,- onLoad: onTreeNodeLoad,- onUpdateExpandedKeys: (keys: string[]) => {- expandedKeys.value = keys;- },- onUpdateCheckedKeys: async (- checkedKeys: string[],- _: Array<TreeOption | null>,- meta: { node: TreeOption | null; action: "check" | "uncheck" }- ) => {- if (!meta.node) {- return;- }-- const oldIndeterminateKeys = new Set(indeterminateKeys.value);- const newCheckedKeys = new Set(checkedKeys);- const oldCheckedKeys = new Set(selectedValueList.value);- const treeNode = meta.node as DatabaseTreeOption;-- const checkNodeAndAllChildren = async () => {- await onTreeNodeLoad(treeNode);- // refresh node in case the schema is updated- const checkedNode = sourceTransferOptions.value.find(- (option) => option.value === treeNode.value- );- if (checkedNode) {- // check and expand all children- cascadeLoopTreeNode(checkedNode, (treeNode) => {- newCheckedKeys.add(treeNode.value);- if (treeNode.children) {- expandedKeys.value.push(treeNode.value);- }- });- }- };-- if (meta.action === "check") {- oldIndeterminateKeys.delete(treeNode.value);- await checkNodeAndAllChildren();-- const parentPathes = parseKeyToPathes(treeNode.value);- parentPathes.pop();- while (parentPathes.length > 0) {- const parentPath = parentPathes.pop() as string;- // If users not manually select the parent,- // then DONOT check the parent,- // move the parent to the indeterminate keys instead.- if (- !oldCheckedKeys.has(parentPath) &&- newCheckedKeys.has(parentPath)- ) {- newCheckedKeys.delete(parentPath);- oldIndeterminateKeys.add(parentPath);- }- }- } else {- if (oldIndeterminateKeys.has(treeNode.value)) {- // uncheck an indeterminate key should be check- oldIndeterminateKeys.delete(treeNode.value);-- await checkNodeAndAllChildren();- } else {- // loop parent pathes to check if we need to update the indeterminate keys- const parentPathes = parseKeyToPathes(treeNode.value);- parentPathes.pop();- while (parentPathes.length > 0) {- const parentPath = parentPathes.pop() as string;- if (!oldIndeterminateKeys.has(parentPath)) {- continue;+ return h(+ "div",+ { class: "flex flex-col space-y-2 pb-4" },+ {+ default: () => [+ h(NTree, {+ keyField: "value",+ cascade: true,+ allowCheckingNotLoaded: true,+ checkable: true,+ selectable: false,+ checkOnClick: true,+ disabled: props.disabled,+ data: sourceTreeOptions.value,+ blockLine: true,+ renderLabel: ({ option }: { option: TreeOption }) => {+ return h(Label, {+ option: option as DatabaseTreeOption,+ keyword: pattern,+ });+ },+ pattern,+ showIrrelevantNodes: false,+ expandedKeys: expandedKeys.value,+ checkedKeys: selectedValueList.value,+ indeterminateKeys: indeterminateKeys.value,+ onLoad: onTreeNodeLoad,+ onUpdateExpandedKeys: (keys: string[]) => {+ expandedKeys.value = keys;+ },+ onUpdateCheckedKeys: async (+ checkedKeys: string[],+ _: Array<TreeOption | null>,+ meta: { node: TreeOption | null; action: "check" | "uncheck" }+ ) => {+ if (!meta.node) {+ return;}- if (!checkedKeys.find((key) => key.startsWith(`${parentPath}/`))) {- oldIndeterminateKeys.delete(parentPath);++ const oldIndeterminateKeys = new Set(indeterminateKeys.value);+ const newCheckedKeys = new Set(checkedKeys);+ const oldCheckedKeys = new Set(selectedValueList.value);+ const treeNode = meta.node as DatabaseTreeOption;++ const checkNodeAndAllChildren = async () => {+ await onTreeNodeLoad(treeNode);+ // refresh node in case the schema is updated+ const checkedNode = sourceTransferOptions.value.find(+ (option) => option.value === treeNode.value+ );+ if (checkedNode) {+ // check and expand all children+ cascadeLoopTreeNode(checkedNode, (treeNode) => {+ newCheckedKeys.add(treeNode.value);+ if (treeNode.children) {+ expandedKeys.value.push(treeNode.value);+ }+ });+ }+ };++ if (meta.action === "check") {+ oldIndeterminateKeys.delete(treeNode.value);+ await checkNodeAndAllChildren();++ const parentPathes = parseKeyToPathes(treeNode.value);+ parentPathes.pop();+ while (parentPathes.length > 0) {+ const parentPath = parentPathes.pop() as string;+ // If users not manually select the parent,+ // then DONOT check the parent,+ // move the parent to the indeterminate keys instead.+ if (+ !oldCheckedKeys.has(parentPath) &&+ newCheckedKeys.has(parentPath)+ ) {+ newCheckedKeys.delete(parentPath);+ oldIndeterminateKeys.add(parentPath);+ }+ }+ } else {+ if (oldIndeterminateKeys.has(treeNode.value)) {+ // uncheck an indeterminate key should be check+ oldIndeterminateKeys.delete(treeNode.value);++ await checkNodeAndAllChildren();+ } else {+ // loop parent pathes to check if we need to update the indeterminate keys+ const parentPathes = parseKeyToPathes(treeNode.value);+ parentPathes.pop();+ while (parentPathes.length > 0) {+ const parentPath = parentPathes.pop() as string;+ if (!oldIndeterminateKeys.has(parentPath)) {+ continue;+ }+ if (+ !checkedKeys.find((key) => key.startsWith(`${parentPath}/`))+ ) {+ oldIndeterminateKeys.delete(parentPath);+ }+ }+ }}- }- }- }- selectedValueList.value = [...newCheckedKeys];- onCheck([...newCheckedKeys]);- indeterminateKeys.value = [...oldIndeterminateKeys];- },- });+ selectedValueList.value = [...newCheckedKeys];+ onCheck([...newCheckedKeys]);+ indeterminateKeys.value = [...oldIndeterminateKeys];+ },+ }),+ fetchDataState.value.nextPageToken+ ? h(+ "div",+ { class: "w-full flex items-center justify-center" },+ h(+ NButton,+ {+ quaternary: true,+ size: "small",+ loading: fetchDataState.value.loading,+ onClick: fetchDatabaseList,+ },+ {+ default: () => t("common.load-more"),+ }+ )+ )+ : undefined,+ ],+ }+ );};const targetTreeOptions = computed(() => {diff --git a/frontend/src/store/modules/sqlEditor/editor.ts b/frontend/src/store/modules/sqlEditor/editor.tsindex 917049e780..bc5422f5f2 100644--- a/frontend/src/store/modules/sqlEditor/editor.ts+++ b/frontend/src/store/modules/sqlEditor/editor.ts@@ -36,26 +36,45 @@ export const useSQLEditorStore = defineStore("sqlEditor", () => {// `databaseList` is query-able databases scoped by `project`const databaseList = ref<ComposedDatabase[]>([]);- const loading = ref<boolean>(false);+ const fetchDataState = ref<{+ nextPageToken?: string;+ loading: boolean;+ }>({+ loading: false,+ });++ const fetchDatabases = useDebounceFn(async (name?: string) => {+ fetchDataState.value.loading = true;+ const pageToken = fetchDataState.value.nextPageToken;- const prepareDatabases = useDebounceFn(async (name?: string) => {- loading.value = true;try {- const { databases } = await databaseStore.fetchDatabases({+ const { databases, nextPageToken } = await databaseStore.fetchDatabases({parent: project.value,+ pageToken,pageSize: getDefaultPagination(),filter: {query: name,},});- databaseList.value = [...databases];++ if (pageToken) {+ databaseList.value.push(...databases);+ } else {+ databaseList.value = [...databases];+ }+ fetchDataState.value.nextPageToken = nextPageToken;} catch {databaseList.value = [];} finally {- loading.value = false;+ fetchDataState.value.loading = false;}}, 500);+ const prepareDatabases = async (name?: string) => {+ fetchDataState.value.nextPageToken = "";+ await fetchDatabases(name);+ };+watchEffect(async () => {if (isValidProjectName(project.value)) {await prepareDatabases();@@ -83,6 +102,7 @@ export const useSQLEditorStore = defineStore("sqlEditor", () => {executingHintDatabase,redisCommandOption,prepareDatabases,- loading,+ fetchDatabases,+ loading: computed(() => fetchDataState.value.loading),};});diff --git a/frontend/src/store/modules/sqlEditor/tree.ts b/frontend/src/store/modules/sqlEditor/tree.tsindex 60f1dbc07b..82609cc4fc 100644--- a/frontend/src/store/modules/sqlEditor/tree.ts+++ b/frontend/src/store/modules/sqlEditor/tree.ts@@ -169,7 +169,6 @@ export const useSQLEditorTreeStore = defineStore("sqlEditorTree", () => {const buildTree = () => {nodeListMapById.clear();- expandedKeys.value = [];tree.value = buildTreeImpl(sortedDatabaseList.value,filteredFactorList.value@@ -195,13 +194,6 @@ export const useSQLEditorTreeStore = defineStore("sqlEditorTree", () => {});- watch(- () => databaseList.value,- () => {- buildTree();- }- );-watch(factorList,(factorList) => {diff --git a/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vue b/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vueindex 31d4245cf7..873340fe05 100644--- a/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vue+++ b/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vue@@ -60,14 +60,10 @@<script lang="ts" setup>import { useElementSize } from "@vueuse/core";import { storeToRefs } from "pinia";-import { computed, ref, watch } from "vue";+import { computed, ref } from "vue";import { ProjectSelect } from "@/components/v2";import { SQL_EDITOR_SETTING_PROJECT_MODULE } from "@/router/sqlEditor";-import {- useSQLEditorTreeStore,- useSQLEditorStore,- useAppFeature,-} from "@/store";+import { useSQLEditorStore, useAppFeature } from "@/store";import { defaultProject, isValidProjectName } from "@/types";import { hasProjectPermissionV2, hasWorkspacePermissionV2 } from "@/utils";import { useSQLEditorContext } from "../context";@@ -77,8 +73,7 @@ import SchemaPane from "./SchemaPane";import WorksheetPane from "./WorksheetPane";const editorStore = useSQLEditorStore();-const treeStore = useSQLEditorTreeStore();-const { events, asidePanelTab } = useSQLEditorContext();+const { asidePanelTab } = useSQLEditorContext();const { project, projectContextReady, strictProject } =storeToRefs(editorStore);const containerRef = ref<HTMLDivElement>();@@ -92,16 +87,6 @@ const allowCreateProject = computed(() => {return hasWorkspacePermissionV2("bb.projects.create");});-watch([project, projectContextReady], ([, ready]) => {- if (!ready) {- treeStore.state = "LOADING";- } else {- treeStore.buildTree();- treeStore.state = "READY";- events.emit("tree-ready");- }-});-const handleSwitchProject = (name: string | undefined) => {if (!name || !isValidProjectName(name)) {project.value = "";diff --git a/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/ConnectionPane.vue b/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/ConnectionPane.vueindex 1a2256fbc1..298b17d53e 100644--- a/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/ConnectionPane.vue+++ b/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/ConnectionPane.vue@@ -2,18 +2,18 @@<div class="sql-editor-tree gap-y-1 h-full flex flex-col relative"><div class="flex flex-row gap-x-0.5 px-1 items-center"><SearchBox- :loading="editrStore.loading"+ :loading="editorStore.loading"v-model:search-pattern="searchPattern"class="flex-1"/>- <GroupingBar :disabled="editrStore.loading" class="shrink-0" />+ <GroupingBar :disabled="editorStore.loading" class="shrink-0" /></div><divv-if="hasMissingQueryDatabases"class="flex items-center space-x-2 px-2 py-2"><NCheckbox- :disabled="editrStore.loading"+ :disabled="editorStore.loading"v-model:checked="showMissingQueryDatabases"><span class="textinfolabel text-sm">@@ -23,25 +23,42 @@</div><divref="treeContainerElRef"- class="relative sql-editor-tree--tree flex-1 px-1 pb-1 text-sm overflow-hidden select-none"+ class="relative sql-editor-tree--tree flex-1 px-1 pb-1 text-sm select-none":data-height="treeContainerHeight">- <MaskSpinner v-if="editrStore.loading" class="!bg-white/75" />- <NTree+ <divv-if="treeStore.state === 'READY'"- ref="treeRef"- v-model:expanded-keys="expandedKeys"- :block-line="true"- :data="treeStore.tree"- :show-irrelevant-nodes="false"- :selected-keys="selectedKeys"- :pattern="mounted ? searchPattern : ''"- :expand-on-click="true"- :node-props="nodeProps"- :virtual-scroll="true"- :theme-overrides="{ nodeHeight: '21px' }"- :render-label="renderLabel"- />+ class="flex flex-col space-y-2 pb-4"+ >+ <NTree+ ref="treeRef"+ v-model:expanded-keys="expandedKeys"+ :block-line="true"+ :data="treeStore.tree"+ :show-irrelevant-nodes="false"+ :selected-keys="selectedKeys"+ :pattern="mounted ? searchPattern : ''"+ :expand-on-click="true"+ :node-props="nodeProps"+ :theme-overrides="{ nodeHeight: '21px' }"+ :render-label="renderLabel"+ />+ <div class="w-full flex items-center justify-center">+ <NButton+ quaternary+ :size="'small'"+ :loading="editorStore.loading"+ @click="+ () =>+ editorStore+ .fetchDatabases(searchPattern)+ .then(() => treeStore.buildTree())+ "+ >+ {{ $t("common.load-more") }}+ </NButton>+ </div>+ </div></div><NDropdown@@ -69,9 +86,15 @@<script lang="ts" setup>import { computedAsync, useElementSize, useMounted } from "@vueuse/core";import { head } from "lodash-es";-import { NTree, NDropdown, NCheckbox, type TreeOption } from "naive-ui";+import {+ NButton,+ NTree,+ NDropdown,+ NCheckbox,+ type TreeOption,+} from "naive-ui";import { storeToRefs } from "pinia";-import { ref, nextTick, watch, h, onMounted } from "vue";+import { ref, nextTick, watch, h } from "vue";import MaskSpinner from "@/components/misc/MaskSpinner.vue";import { useEmitteryEventListener } from "@/composables/useEmitteryEventListener";import {@@ -111,7 +134,7 @@ import { setConnection, useDropdown } from "./actions";const treeStore = useSQLEditorTreeStore();const tabStore = useSQLEditorTabStore();-const editrStore = useSQLEditorStore();+const editorStore = useSQLEditorStore();const databaseStore = useDatabaseV1Store();const isLoggedIn = useIsLoggedIn();@@ -390,12 +413,22 @@ useEmitteryEventListener(editorEvents, "tree-ready", () => {calcDefaultExpandKeys(true);});-onMounted(() => calcDefaultExpandKeys(false));-watch(- () => searchPattern.value,- (search) =>- editrStore.prepareDatabases(search).then(() => calcDefaultExpandKeys(true)),+ [+ () => editorStore.project,+ () => editorStore.projectContextReady,+ () => searchPattern.value,+ ],+ async ([, ready, search]) => {+ if (!ready) {+ treeStore.state = "LOADING";+ } else {+ await editorStore.prepareDatabases(search);+ treeStore.buildTree();+ treeStore.state = "READY";+ editorEvents.emit("tree-ready");+ }+ },{ immediate: true });</script>