Skip to content

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

chore: update fetch database filter (#9868)

boojack753edc631930
diff --git a/frontend/src/components/DatabaseSelect.vue b/frontend/src/components/DatabaseSelect.vue
deleted file mode 100644
index 07334ce222..0000000000
--- a/frontend/src/components/DatabaseSelect.vue
+++ /dev/null
@@ -1,247 +0,0 @@
-<template>
- <BBSelect
- :selected-item="selectedDatabase"
- :item-list="databaseList"
- :disabled="disabled"
- :placeholder="placeholder || $t('database.select')"
- :show-prefix-item="true"
- @select-item="(database: ComposedDatabase) => $emit('select-database-id', database.uid)"
- >
- <template #menuItem="{ item: database }: { item: ComposedDatabase }">
- <slot
- v-if="customizeItem"
- name="customizeItem"
- :database="database"
- ></slot>
- <div v-else class="flex items-center">
- <span>{{ database.databaseName }}</span>
- </div>
- </template>
- </BBSelect>
-</template>
-
-<script lang="ts">
-import {
- computed,
- reactive,
- watch,
- watchEffect,
- PropType,
- defineComponent,
-} from "vue";
-import {
- useCurrentUserV1,
- useDatabaseV1Store,
- useEnvironmentV1Store,
- useInstanceV1Store,
-} from "@/store";
-import { Engine, State } from "@/types/proto/v1/common";
-import { isNullOrUndefined } from "@/utils";
-import {
- UNKNOWN_ID,
- ComposedDatabase,
- DEFAULT_PROJECT_V1_NAME,
-} from "../types";
-
-interface LocalState {
- selectedId?: string;
-}
-
-export default defineComponent({
- name: "DatabaseSelect",
- props: {
- selectedId: {
- required: true,
- type: String,
- },
- mode: {
- required: true,
- type: String as PropType<"ALL" | "INSTANCE" | "ENVIRONMENT" | "USER">,
- },
- placeholder: {
- type: String,
- default: "",
- },
- environmentId: {
- type: String,
- default: String(UNKNOWN_ID),
- },
- instanceId: {
- type: String,
- default: String(UNKNOWN_ID),
- },
- projectId: {
- type: String,
- default: String(UNKNOWN_ID),
- },
- engineTypeList: {
- type: Array as PropType<Engine[]>,
- default: undefined,
- },
- engineType: {
- type: Number as PropType<Engine>,
- default: undefined,
- },
- syncState: {
- type: Number as PropType<State>,
- default: undefined,
- },
- disabled: {
- type: Boolean,
- default: false,
- },
- customizeItem: {
- type: Boolean,
- default: false,
- },
- },
- emits: ["select-database-id"],
- setup(props, { emit }) {
- const databaseStore = useDatabaseV1Store();
- const state = reactive<LocalState>({
- selectedId: props.selectedId,
- });
-
- const currentUserV1 = useCurrentUserV1();
-
- const prepareDatabaseList = async () => {
- // TODO(tianzhou): Instead of fetching each time, we maybe able to let the outside context
- // to provide the database list and we just do a get here.
- if (props.mode === "ALL") {
- databaseStore.fetchDatabaseList({
- parent: "instances/-",
- });
- } else if (
- props.mode == "ENVIRONMENT" &&
- props.environmentId !== String(UNKNOWN_ID)
- ) {
- // TODO: we do not support filtering database list by environment in v1
- // API so we need to fetch them all
- databaseStore.fetchDatabaseList({
- parent: "instances/-",
- });
- } else if (
- props.mode == "INSTANCE" &&
- props.instanceId !== String(UNKNOWN_ID)
- ) {
- const instance = await useInstanceV1Store().getOrFetchInstanceByUID(
- props.instanceId
- );
- databaseStore.fetchDatabaseList({
- parent: instance.name,
- });
- } else if (props.mode == "USER") {
- // We assume the database list for the current user should have already been fetched, so we won't do a fetch here.
- }
- };
-
- watchEffect(prepareDatabaseList);
-
- const databaseList = computed(() => {
- let list: ComposedDatabase[] = [];
- if (props.mode === "ALL") {
- list = [...databaseStore.databaseList];
- } else if (
- props.mode === "ENVIRONMENT" &&
- props.environmentId !== String(UNKNOWN_ID)
- ) {
- const environment = useEnvironmentV1Store().getEnvironmentByUID(
- props.environmentId
- );
- list = databaseStore.databaseListByEnvironment(environment.name);
- } else if (
- props.mode === "INSTANCE" &&
- props.instanceId !== String(UNKNOWN_ID)
- ) {
- const instance = useInstanceV1Store().getInstanceByUID(
- props.instanceId
- );
- list = databaseStore.databaseListByInstance(instance.name);
- } else if (props.mode == "USER") {
- list = databaseStore.databaseListByUser(currentUserV1.value);
- }
-
- if (!isNullOrUndefined(props.engineTypeList)) {
- list = list.filter((database) => {
- return props.engineTypeList?.includes(database.instanceEntity.engine);
- });
- }
-
- if (!isNullOrUndefined(props.syncState)) {
- list = list.filter((database) => {
- return database.syncState === props.syncState;
- });
- }
-
- if (props.environmentId !== String(UNKNOWN_ID)) {
- const environment = useEnvironmentV1Store().getEnvironmentByUID(
- props.environmentId
- );
- list = list.filter(
- (db) => db.effectiveEnvironment === environment.name
- );
- }
- if (props.instanceId !== String(UNKNOWN_ID)) {
- list = list.filter((db) => db.instanceEntity.uid === props.instanceId);
- }
- if (props.projectId !== String(UNKNOWN_ID)) {
- list = list.filter((db) => db.projectEntity.uid === props.projectId);
- }
-
- list = list.filter((database) => {
- return database.project !== DEFAULT_PROJECT_V1_NAME;
- });
-
- return list;
- });
-
- const selectedDatabase = computed(() => {
- return databaseList.value.find(
- (database) => database.uid == state.selectedId
- );
- });
-
- const invalidateSelectionIfNeeded = () => {
- if (
- state.selectedId !== String(UNKNOWN_ID) &&
- !databaseList.value.find((database) => database.uid == state.selectedId)
- ) {
- state.selectedId = String(UNKNOWN_ID);
- emit("select-database-id", state.selectedId);
- }
- };
-
- const onSelectChange = (e: Event) => {
- const id = (e.target as HTMLSelectElement).value;
- state.selectedId = id;
- emit("select-database-id", id);
- };
-
- // The database list might change if environmentId changes, and the previous selected id
- // might not exist in the new list. In such case, we need to invalidate the selection
- // and emit the event.
- watch(
- () => databaseList.value,
- () => {
- invalidateSelectionIfNeeded();
- }
- );
-
- watch(
- () => props.selectedId,
- (selectedId) => {
- invalidateSelectionIfNeeded();
- state.selectedId = selectedId;
- }
- );
-
- return {
- UNKNOWN_ID,
- state,
- databaseList,
- selectedDatabase,
- onSelectChange,
- };
- },
-});
-</script>
diff --git a/frontend/src/components/ProvideDashboardContext.vue b/frontend/src/components/ProvideDashboardContext.vue
index 3fabbb9e63..5378ba30e2 100644
--- a/frontend/src/components/ProvideDashboardContext.vue
+++ b/frontend/src/components/ProvideDashboardContext.vue
@@ -1,9 +1,10 @@
<template>
- <slot />
+ <slot v-if="!isLoading" />
</template>
-<script lang="ts">
-import { defineComponent } from "vue";
+<script lang="ts" setup>
+import { ref, onMounted } from "vue";
+import { useRoute } from "vue-router";
import {
useEnvironmentV1Store,
useInstanceV1Store,
@@ -16,26 +17,43 @@ import {
useDatabaseV1Store,
useDBGroupStore,
} from "@/store";
+import { projectNamePrefix } from "@/store/modules/v1/common";
-export default defineComponent({
- name: "ProvideDashboardContext",
- async setup() {
- await Promise.all([
- useUserStore().fetchUserList(),
- useSettingV1Store().fetchSettingList(),
- useRoleStore().fetchRoleList(),
- useEnvironmentV1Store().fetchEnvironments(),
- useInstanceV1Store().fetchInstanceList(),
- useProjectV1Store().fetchProjectList(true),
- usePolicyV1Store().getOrFetchPolicyByName("policies/WORKSPACE_IAM"),
- ]);
- await Promise.all([
- useDatabaseV1Store().fetchDatabaseList({
- parent: "instances/-",
- }),
- useDBGroupStore().fetchAllDatabaseGroupList(),
- useUIStateStore().restoreState(),
- ]);
- },
+const route = useRoute();
+const isLoading = ref<boolean>(true);
+
+const databaseStore = useDatabaseV1Store();
+
+const prepareDatabases = async () => {
+ let filter = "";
+ // If `projectId` is provided in the route, filter the database list by the project.
+ if (route.params.projectId) {
+ filter = `project == "${projectNamePrefix}${route.params.projectId}"`;
+ }
+
+ await databaseStore.fetchDatabaseList({
+ parent: "instances/-",
+ filter: filter,
+ });
+};
+
+onMounted(async () => {
+ await Promise.all([
+ useUserStore().fetchUserList(),
+ useSettingV1Store().fetchSettingList(),
+ useRoleStore().fetchRoleList(),
+ useEnvironmentV1Store().fetchEnvironments(),
+ useInstanceV1Store().fetchInstanceList(),
+ useProjectV1Store().fetchProjectList(true),
+ usePolicyV1Store().getOrFetchPolicyByName("policies/WORKSPACE_IAM"),
+ ]);
+
+ await Promise.all([
+ prepareDatabases(),
+ useDBGroupStore().fetchAllDatabaseGroupList(),
+ useUIStateStore().restoreState(),
+ ]);
+
+ isLoading.value = false;
});
</script>
diff --git a/frontend/src/components/ProvideSQLEditorContext.vue b/frontend/src/components/ProvideSQLEditorContext.vue
index 4be4d3e254..68f18818af 100644
--- a/frontend/src/components/ProvideSQLEditorContext.vue
+++ b/frontend/src/components/ProvideSQLEditorContext.vue
@@ -53,9 +53,9 @@ import {
isSimilarTab,
} from "@/utils";
+const { t } = useI18n();
const route = useRoute();
const router = useRouter();
-const { t } = useI18n();
const isLoading = ref<boolean>(true);
const currentUserV1 = useCurrentUserV1();