Skip to content

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

chore: export button optimize (#7612)

ecmadao39f50f3f0d26
diff --git a/frontend/src/components/DataExportButton.vue b/frontend/src/components/DataExportButton.vue
new file mode 100644
index 0000000000..f41105dacd
--- /dev/null
+++ b/frontend/src/components/DataExportButton.vue
@@ -0,0 +1,104 @@
+<template>
+ <NDropdown
+ trigger="hover"
+ :options="exportDropdownOptions"
+ @select="doExport"
+ >
+ <NButton
+ :quaternary="size === 'tiny'"
+ :size="size"
+ :loading="state.isRequesting"
+ :disabled="state.isRequesting || disabled"
+ >
+ <template #icon>
+ <heroicons-outline:download class="h-5 w-5" />
+ </template>
+ <span v-if="size !== 'tiny'">
+ {{ t("common.export") }}
+ </span>
+ </NButton>
+ </NDropdown>
+</template>
+
+<script lang="ts" setup>
+import dayjs from "dayjs";
+import { NButton, NDropdown } from "naive-ui";
+import { BinaryLike } from "node:crypto";
+import { computed, reactive } from "vue";
+import { useI18n } from "vue-i18n";
+import { getExportFileType, pushNotification } from "@/store";
+
+export type ExportFormat = "CSV" | "JSON" | "SQL" | "XLSX";
+
+interface LocalState {
+ isRequesting: boolean;
+}
+
+const props = withDefaults(
+ defineProps<{
+ size?: "small" | "tiny";
+ disabled?: boolean;
+ supportFormats: ExportFormat[];
+ }>(),
+ {
+ size: "small",
+ disabled: false,
+ }
+);
+
+const emit = defineEmits<{
+ (
+ event: "export",
+ format: ExportFormat,
+ download: (content: string, format: ExportFormat) => void
+ ): Promise<void>;
+}>();
+
+const { t } = useI18n();
+const state = reactive<LocalState>({
+ isRequesting: false,
+});
+
+const exportDropdownOptions = computed(() => {
+ return props.supportFormats.map((format) => ({
+ label: t("sql-editor.download-as-file", { file: format }),
+ key: format,
+ }));
+});
+
+const doExport = async (format: ExportFormat) => {
+ if (state.isRequesting) {
+ return;
+ }
+
+ state.isRequesting = true;
+
+ try {
+ await emit("export", format, doDownload);
+ } catch (error) {
+ pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title: `Failed to export data`,
+ description: JSON.stringify(error),
+ });
+ } finally {
+ state.isRequesting = false;
+ }
+};
+
+const doDownload = (content: BinaryLike | Blob, format: ExportFormat) => {
+ const blob = new Blob([content], {
+ type: getExportFileType(format),
+ });
+ const url = window.URL.createObjectURL(blob);
+
+ const fileFormat = format.toLowerCase();
+ const formattedDateString = dayjs(new Date()).format("YYYY-MM-DDTHH-mm-ss");
+ const filename = `export-data-${formattedDateString}`;
+ const link = document.createElement("a");
+ link.download = `${filename}.${fileFormat}`;
+ link.href = url;
+ link.click();
+};
+</script>
diff --git a/frontend/src/store/modules/export.ts b/frontend/src/store/modules/export.ts
new file mode 100644
index 0000000000..dd09b6dea1
--- /dev/null
+++ b/frontend/src/store/modules/export.ts
@@ -0,0 +1,36 @@
+import { getExportRequestFormat, useSQLStore } from "@/store";
+import { extractDatabaseResourceName } from "@/utils";
+
+export type ExportDataParams = {
+ format: "CSV" | "JSON" | "SQL" | "XLSX";
+ statement: string;
+ limit: number;
+ database: string; // instances/{instance}/databases/{database}
+ instance: string; // instances/{instance}
+ admin?: boolean;
+};
+
+export const useExportData = () => {
+ const sqlStore = useSQLStore();
+
+ const exportData = async (params: ExportDataParams) => {
+ const connectionDatabase = params.database
+ ? extractDatabaseResourceName(params.database).database
+ : "";
+
+ const { content } = await sqlStore.exportData({
+ name: params.instance,
+ connectionDatabase,
+ statement: params.statement,
+ limit: params.limit,
+ format: getExportRequestFormat(params.format),
+ admin: params.admin ?? false,
+ });
+
+ return content;
+ };
+
+ return {
+ exportData,
+ };
+};
diff --git a/frontend/src/views/ExportCenter/ExportDataButton.vue b/frontend/src/views/ExportCenter/ExportDataButton.vue
index 8fd5f86b40..bfd4d64553 100644
--- a/frontend/src/views/ExportCenter/ExportDataButton.vue
+++ b/frontend/src/views/ExportCenter/ExportDataButton.vue
@@ -1,113 +1,44 @@
<template>
- <NDropdown
- trigger="hover"
- :options="exportDropdownOptions"
- @select="handleExportData"
- >
- <NButton
- quaternary
- size="tiny"
- :loading="state.isRequesting"
- :disabled="state.isRequesting"
- >
- <heroicons-outline:document-download class="h-5 w-5" />
- </NButton>
- </NDropdown>
+ <DataExportButton
+ size="tiny"
+ :support-formats="['CSV', 'JSON', 'SQL', 'XLSX']"
+ @export="handleExportData"
+ />
</template>
<script lang="ts" setup>
-import dayjs from "dayjs";
-import { NButton, NDropdown } from "naive-ui";
-import { computed, reactive } from "vue";
-import { useI18n } from "vue-i18n";
-import {
- getExportFileType,
- pushNotification,
- useProjectIamPolicyStore,
- useSQLStore,
-} from "@/store";
-import { ExportRequest_Format } from "@/types/proto/v1/sql_service";
+import { BinaryLike } from "node:crypto";
+import { ExportFormat } from "@/components/DataExportButton.vue";
+import { useProjectIamPolicyStore } from "@/store";
+import { useExportData } from "@/store/modules/export";
import { ExportRecord } from "./types";
-interface LocalState {
- showConfirmModal: boolean;
- isRequesting: boolean;
-}
-
const props = defineProps<{
exportRecord: ExportRecord;
}>();
-const { t } = useI18n();
-const sqlStore = useSQLStore();
const projectIamPolicyStore = useProjectIamPolicyStore();
-const state = reactive<LocalState>({
- showConfirmModal: false,
- isRequesting: false,
-});
-
-const exportDropdownOptions = computed(() => [
- {
- label: t("sql-editor.download-as-file", { file: "CSV" }),
- key: ExportRequest_Format.CSV,
- },
- {
- label: t("sql-editor.download-as-file", { file: "JSON" }),
- key: ExportRequest_Format.JSON,
- },
- {
- label: t("sql-editor.download-as-file", { file: "SQL" }),
- key: ExportRequest_Format.SQL,
- },
- {
- label: t("sql-editor.download-as-file", { file: "XLSX" }),
- key: ExportRequest_Format.XLSX,
- },
-]);
-
-const handleExportData = async (format: ExportRequest_Format) => {
- if (state.isRequesting) {
- return;
- }
-
- state.isRequesting = true;
+const { exportData } = useExportData();
+const handleExportData = async (
+ format: ExportFormat,
+ callback: (content: BinaryLike | Blob, format: ExportFormat) => void
+) => {
const exportRecord = props.exportRecord;
const database = exportRecord.database;
- try {
- const { content } = await sqlStore.exportData({
- name: database.instance,
- connectionDatabase: database.databaseName,
- statement: exportRecord.statement,
- limit: exportRecord.maxRowCount,
- format: format,
- admin: false,
- });
+ const content = await exportData({
+ database: database.name,
+ instance: database.instance,
+ format,
+ statement: exportRecord.statement,
+ limit: exportRecord.maxRowCount,
+ admin: false,
+ });
- const blob = new Blob([content], {
- type: getExportFileType(exportRecord.exportFormat),
- });
- const url = window.URL.createObjectURL(blob);
+ callback(content, format);
- const fileFormat = exportRecord.exportFormat.toLowerCase();
- const formattedDateString = dayjs(new Date()).format("YYYY-MM-DDTHH-mm-ss");
- const filename = `export-data-${formattedDateString}`;
- const link = document.createElement("a");
- link.download = `${filename}.${fileFormat}`;
- link.href = url;
- link.click();
- // Fetch the latest iam policy.
- await projectIamPolicyStore.fetchProjectIamPolicy(database.project, true);
- state.isRequesting = false;
- state.showConfirmModal = false;
- } catch (error) {
- pushNotification({
- module: "bytebase",
- style: "CRITICAL",
- title: `Failed to export data`,
- description: JSON.stringify(error),
- });
- }
+ // Fetch the latest iam policy.
+ await projectIamPolicyStore.fetchProjectIamPolicy(database.project, true);
};
</script>
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
index b031b97068..3138c42097 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
@@ -45,23 +45,13 @@
>
{{ $t("sql-editor.visualize-explain") }}
</NButton>
- <NDropdown
+ <DataExportButton
v-if="showExportButton"
- trigger="hover"
- :options="exportDropdownOptions"
- @select="handleExportBtnClick"
- >
- <NButton
- size="small"
- :loading="isExportingData"
- :disabled="isExportingData"
- >
- <template #icon>
- <heroicons-outline:download class="h-5 w-5" />
- </template>
- {{ t("common.export") }}
- </NButton>
- </NDropdown>
+ size="small"
+ :disabled="props.result === null || isEmpty(props.result)"
+ :support-formats="['CSV', 'JSON', 'SQL', 'XLSX']"
+ @export="handleExportBtnClick"
+ />
<NButton v-else @click="state.showRequestExportPanel = true">
{{ $t("quick-action.request-export") }}
</NButton>
@@ -128,8 +118,10 @@ import {
import { useDebounceFn } from "@vueuse/core";
import { isEmpty } from "lodash-es";
import { NInput, NPagination } from "naive-ui";
+import { BinaryLike } from "node:crypto";
import { computed, reactive, ref } from "vue";
import { useI18n } from "vue-i18n";
+import { ExportFormat } from "@/components/DataExportButton.vue";
import RequestExportPanel from "@/components/Issue/panel/RequestExportPanel/index.vue";
import {
useInstanceV1Store,
@@ -139,6 +131,7 @@ import {
useDatabaseV1Store,
useCurrentUserV1,
} from "@/store";
+import { useExportData } from "@/store/modules/export";
import {
ExecuteConfig,
ExecuteOption,
@@ -158,7 +151,6 @@ import DataTable from "./DataTable";
import EmptyView from "./EmptyView.vue";
import ErrorView from "./ErrorView.vue";
import { useSQLResultViewContext } from "./context";
-import { useExportData } from "./useExportData";
type LocalState = {
search: string;
@@ -192,7 +184,7 @@ const instanceStore = useInstanceV1Store();
const databaseStore = useDatabaseV1Store();
const currentUserV1 = useCurrentUserV1();
const dataTable = ref<InstanceType<typeof DataTable>>();
-const { isExportingData, exportData } = useExportData();
+const { exportData } = useExportData();
const currentTab = computed(() => tabStore.currentTab);
const viewMode = computed((): ViewMode => {
@@ -297,30 +289,10 @@ const pageSize = computed(() => {
return table.getState().pagination.pageSize;
});
-const exportDropdownOptions = computed(() => [
- {
- label: t("sql-editor.download-as-file", { file: "CSV" }),
- key: "CSV",
- disabled: props.result === null || isEmpty(props.result),
- },
- {
- label: t("sql-editor.download-as-file", { file: "JSON" }),
- key: "JSON",
- disabled: props.result === null || isEmpty(props.result),
- },
- {
- label: t("sql-editor.download-as-file", { file: "SQL" }),
- key: "SQL",
- disabled: props.result === null || isEmpty(props.result),
- },
- {
- label: t("sql-editor.download-as-file", { file: "XLSX" }),
- key: "XLSX",
- disabled: props.result === null || isEmpty(props.result),
- },
-]);
-
-const handleExportBtnClick = (format: "CSV" | "JSON" | "SQL" | "XLSX") => {
+const handleExportBtnClick = async (
+ format: ExportFormat,
+ callback: (content: BinaryLike | Blob, format: ExportFormat) => void
+) => {
const { instanceId, databaseId } = tabStore.currentTab.connection;
const instance = instanceStore.getInstanceByUID(instanceId).name;
const database =
@@ -330,7 +302,8 @@ const handleExportBtnClick = (format: "CSV" | "JSON" | "SQL" | "XLSX") => {
const statement = props.result.statement;
const admin = tabStore.currentTab.mode === TabMode.Admin;
const limit = admin ? 0 : RESULT_ROWS_LIMIT;
- exportData({
+
+ const content = await exportData({
database,
instance,
format,
@@ -338,6 +311,8 @@ const handleExportBtnClick = (format: "CSV" | "JSON" | "SQL" | "XLSX") => {
limit,
admin,
});
+
+ callback(content, format);
};
const showVisualizeButton = computed((): boolean => {
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/useExportData.ts b/frontend/src/views/sql-editor/EditorCommon/ResultView/useExportData.ts
deleted file mode 100644
index b0c1728ce4..0000000000
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/useExportData.ts
+++ /dev/null
@@ -1,71 +0,0 @@
-import dayjs from "dayjs";
-import { ref } from "vue";
-import {
- getExportFileType,
- getExportRequestFormat,
- pushNotification,
- useSQLStore,
-} from "@/store";
-import { extractDatabaseResourceName } from "@/utils";
-
-export type ExportDataParams = {
- format: "CSV" | "JSON" | "SQL" | "XLSX";
- statement: string;
- limit: number;
- database: string; // instances/{instance}/databases/{database}
- instance: string; // instances/{instance}
- admin?: boolean;
-};
-
-export const useExportData = () => {
- const isExportingData = ref(false);
- const sqlStore = useSQLStore();
-
- const exportData = async (params: ExportDataParams) => {
- isExportingData.value = true;
-
- try {
- const connectionDatabase = params.database
- ? extractDatabaseResourceName(params.database).database
- : "";
-
- const { content } = await sqlStore.exportData({
- name: params.instance,
- connectionDatabase,
- statement: params.statement,
- limit: params.limit,
- format: getExportRequestFormat(params.format),
- admin: params.admin ?? false,
- });
-
- const blob = new Blob([content], {
- type: getExportFileType(params.format),
- });
- const url = window.URL.createObjectURL(blob);
-
- const fileExt = params.format.toLowerCase();
- const formattedDateString = dayjs(new Date()).format(
- "YYYY-MM-DDTHH-mm-ss"
- );
- const filename = `export-data-${formattedDateString}`;
- const link = document.createElement("a");
- link.download = `${filename}.${fileExt}`;
- link.href = url;
- link.click();
- } catch (error) {
- pushNotification({
- module: "bytebase",
- style: "CRITICAL",
- title: `Failed to export data`,
- description: JSON.stringify(error),
- });
- } finally {
- isExportingData.value = false;
- }
- };
-
- return {
- isExportingData,
- exportData,
- };
-};