Skip to content

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

refactor: migrate table store to pinia (#1093)

Yunwei Xiao97df41a64f6c
diff --git a/frontend/src/components/DatabaseOverviewPanel.vue b/frontend/src/components/DatabaseOverviewPanel.vue
index 9de1c951ba..539b31c162 100644
--- a/frontend/src/components/DatabaseOverviewPanel.vue
+++ b/frontend/src/components/DatabaseOverviewPanel.vue
@@ -208,7 +208,12 @@ import { timezoneString, instanceSlug, isDBAOrOwner } from "../utils";
import { Anomaly, Database, DataSource, DataSourcePatch } from "../types";
import { cloneDeep, isEqual } from "lodash-es";
import { BBTableSectionDataSource } from "../bbkit/types";
-import { featureToRef, useCurrentUser, useDataSourceStore } from "@/store";
+import {
+ featureToRef,
+ useCurrentUser,
+ useDataSourceStore,
+ useTableStore,
+} from "@/store";
interface LocalState {
editingDataSource?: DataSource;
@@ -237,9 +242,10 @@ export default defineComponent({
const state = reactive<LocalState>({});
const currentUser = useCurrentUser();
+ const tableStore = useTableStore();
const prepareTableList = () => {
- store.dispatch("table/fetchTableListByDatabaseId", props.database.id);
+ tableStore.fetchTableListByDatabaseId(props.database.id);
};
watchEffect(prepareTableList);
@@ -266,7 +272,7 @@ export default defineComponent({
const hasDataSourceFeature = featureToRef("bb.feature.data-source");
const tableList = computed(() => {
- return store.getters["table/tableListByDatabaseId"](props.database.id);
+ return tableStore.getTableListByDatabaseId(props.database.id);
});
const viewList = computed(() => {
diff --git a/frontend/src/components/MonacoEditor/useMonaco.ts b/frontend/src/components/MonacoEditor/useMonaco.ts
index 9a51678dae..f0ff22fdd2 100644
--- a/frontend/src/components/MonacoEditor/useMonaco.ts
+++ b/frontend/src/components/MonacoEditor/useMonaco.ts
@@ -13,10 +13,12 @@ import {
SqlDialect,
} from "../../types";
import sqlFormatter from "./sqlFormatter";
-import { useDatabaseStore } from "@/store";
+import { useDatabaseStore, useTableStore } from "@/store";
const useMonaco = async (lang: string) => {
const store = useStore();
+ const dataSourceStore = useDatabaseStore();
+ const tableStore = useTableStore();
const { instanceList } = useNamespacedGetters<InstanceGetters>("instance", [
"instanceList",
@@ -25,12 +27,12 @@ const useMonaco = async (lang: string) => {
const databaseList = computed(() => {
const currentInstanceId =
store.state.sqlEditor.connectionContext.instanceId;
- return useDatabaseStore().getDatabaseListByInstanceId(currentInstanceId);
+ return dataSourceStore.getDatabaseListByInstanceId(currentInstanceId);
});
const tableList = computed(() => {
return databaseList.value
- .map((item) => store.getters["table/tableListByDatabaseId"](item.id))
+ .map((item) => tableStore.getTableListByDatabaseId(item.id))
.flat();
});
diff --git a/frontend/src/components/ProvideSqlEditorContext.vue b/frontend/src/components/ProvideSqlEditorContext.vue
index 5774e600e3..5240990914 100644
--- a/frontend/src/components/ProvideSqlEditorContext.vue
+++ b/frontend/src/components/ProvideSqlEditorContext.vue
@@ -8,6 +8,7 @@ import {
useDatabaseStore,
useInstanceStore,
useProjectStore,
+ useTableStore,
} from "@/store";
import { reactive, onMounted } from "vue";
import { useStore } from "vuex";
@@ -37,6 +38,7 @@ const state = reactive<{
const currentUser = useCurrentUser();
const projectStore = useProjectStore();
+const tableStore = useTableStore();
const prepareAccessibleConnectionByProject = async () => {
// It will also be called when user logout
@@ -102,10 +104,7 @@ const prepareSqlEditorContext = async () => {
);
for (const db of filteredDatabaseList) {
- const tableList = await store.dispatch(
- "table/fetchTableListByDatabaseId",
- db.id
- );
+ const tableList = await tableStore.fetchTableListByDatabaseId(db.id);
const databaseItem = instanceItem.children!.find(
(item: ConnectionAtom) => item.id === db.id
diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts
index 4e91baa905..d0d3ff7b6d 100644
--- a/frontend/src/router/index.ts
+++ b/frontend/src/router/index.ts
@@ -35,6 +35,7 @@ import {
useProjectWebhookStore,
useDataSourceStore,
useProjectStore,
+ useTableStore,
} from "@/store";
const HOME_MODULE = "workspace.home";
@@ -1068,8 +1069,8 @@ router.beforeEach((to, from, next) => {
if (!tableName && !dataSourceSlug && !migrationHistorySlug) {
next();
} else if (tableName) {
- store
- .dispatch("table/fetchTableByDatabaseIdAndTableName", {
+ useTableStore()
+ .fetchTableByDatabaseIdAndTableName({
databaseId: database.id,
tableName,
})
diff --git a/frontend/src/store/modules/sqlEditor.ts b/frontend/src/store/modules/sqlEditor.ts
index 5801f64caf..ab58f3d25f 100644
--- a/frontend/src/store/modules/sqlEditor.ts
+++ b/frontend/src/store/modules/sqlEditor.ts
@@ -21,6 +21,7 @@ import {
useActivityStore,
useDatabaseStore,
useInstanceStore,
+ useTableStore,
} from "../pinia-modules";
export const getDefaultConnectionContext = () => ({
@@ -92,7 +93,7 @@ const getters = {
tableList = instance.children
.map((item: ConnectionAtom) =>
- rootGetters["table/tableListByDatabaseId"](item.id)
+ useTableStore().getTableListByDatabaseId(item.id)
)
.flat();
}
diff --git a/frontend/src/store/modules/table.ts b/frontend/src/store/modules/table.ts
deleted file mode 100644
index 32677fee37..0000000000
--- a/frontend/src/store/modules/table.ts
+++ /dev/null
@@ -1,158 +0,0 @@
-import axios from "axios";
-import {
- Column,
- Database,
- DatabaseId,
- ResourceIdentifier,
- ResourceObject,
- Table,
- TableIndex,
- TableState,
- unknown,
-} from "../../types";
-import {
- getPrincipalFromIncludedList,
- useDatabaseStore,
-} from "../pinia-modules";
-
-function convert(
- table: ResourceObject,
- includedList: ResourceObject[],
- rootGetters: any
-): Table {
- const databaseId = (table.relationships!.database.data as ResourceIdentifier)
- .id;
-
- let database: Database = unknown("DATABASE") as Database;
- const databaseStore = useDatabaseStore();
- for (const item of includedList || []) {
- if (item.type == "database" && item.id == databaseId) {
- database = databaseStore.convert(item, includedList);
- break;
- }
- }
-
- const columnList = (table.attributes.columnList as Column[]) || [];
- const indexList = (table.attributes.indexList as TableIndex[]) || [];
-
- return {
- ...(table.attributes as Omit<
- Table,
- "id" | "database" | "creator" | "updater" | "columnList" | "indexList"
- >),
- id: parseInt(table.id),
- creator: getPrincipalFromIncludedList(
- table.relationships!.creator.data,
- includedList
- ),
- updater: getPrincipalFromIncludedList(
- table.relationships!.updater.data,
- includedList
- ),
- columnList,
- indexList,
- database,
- };
-}
-
-const state: () => TableState = () => ({
- tableListByDatabaseId: new Map(),
-});
-
-const getters = {
- tableListByDatabaseIdAndTableName:
- (state: TableState) =>
- (databaseId: DatabaseId, tableName: string): Table | undefined => {
- const list = state.tableListByDatabaseId.get(databaseId);
- if (list) {
- return list.find((item: Table) => item.name == tableName);
- }
- return undefined;
- },
-
- tableListByDatabaseId:
- (state: TableState) =>
- (databaseId: DatabaseId): Table[] => {
- return state.tableListByDatabaseId.get(databaseId) || [];
- },
-};
-
-const actions = {
- async fetchTableByDatabaseIdAndTableName(
- { commit, rootGetters }: any,
- { databaseId, tableName }: { databaseId: DatabaseId; tableName: string }
- ) {
- const data = (
- await axios.get(`/api/database/${databaseId}/table/${tableName}`)
- ).data;
- const table = convert(data.data, data.included, rootGetters);
-
- commit("setTableByDatabaseIdAndTableName", {
- databaseId,
- tableName,
- table,
- });
- return table;
- },
-
- async fetchTableListByDatabaseId(
- { commit, rootGetters }: any,
- databaseId: DatabaseId
- ) {
- const data = (await axios.get(`/api/database/${databaseId}/table`)).data;
- const tableList = data.data.map((table: ResourceObject) => {
- return convert(table, data.included, rootGetters);
- });
-
- commit("setTableListByDatabaseId", { databaseId, tableList });
- return tableList;
- },
-};
-
-const mutations = {
- setTableByDatabaseIdAndTableName(
- state: TableState,
- {
- databaseId,
- tableName,
- table,
- }: {
- databaseId: DatabaseId;
- tableName: string;
- table: Table;
- }
- ) {
- const list = state.tableListByDatabaseId.get(databaseId);
- if (list) {
- const i = list.findIndex((item: Table) => item.name == tableName);
- if (i != -1) {
- list[i] = table;
- } else {
- list.push(table);
- }
- } else {
- state.tableListByDatabaseId.set(databaseId, [table]);
- }
- },
-
- setTableListByDatabaseId(
- state: TableState,
- {
- databaseId,
- tableList,
- }: {
- databaseId: DatabaseId;
- tableList: Table[];
- }
- ) {
- state.tableListByDatabaseId.set(databaseId, tableList);
- },
-};
-
-export default {
- namespaced: true,
- state,
- getters,
- actions,
- mutations,
-};
diff --git a/frontend/src/store/pinia-modules/index.ts b/frontend/src/store/pinia-modules/index.ts
index 40ef1abd74..a8c93678f1 100644
--- a/frontend/src/store/pinia-modules/index.ts
+++ b/frontend/src/store/pinia-modules/index.ts
@@ -26,5 +26,6 @@ export * from "./router";
export * from "./setting";
export * from "./subscription";
export * from "./tab";
+export * from "./table";
export * from "./uistate";
export * from "./vcs";
diff --git a/frontend/src/store/pinia-modules/table.ts b/frontend/src/store/pinia-modules/table.ts
new file mode 100644
index 0000000000..f63d536f5d
--- /dev/null
+++ b/frontend/src/store/pinia-modules/table.ts
@@ -0,0 +1,135 @@
+import { defineStore } from "pinia";
+import axios from "axios";
+import {
+ Column,
+ Database,
+ DatabaseId,
+ ResourceIdentifier,
+ ResourceObject,
+ Table,
+ TableIndex,
+ TableState,
+ unknown,
+} from "../../types";
+import { getPrincipalFromIncludedList, useDatabaseStore } from ".";
+
+function convert(table: ResourceObject, includedList: ResourceObject[]): Table {
+ const databaseId = (table.relationships!.database.data as ResourceIdentifier)
+ .id;
+
+ let database: Database = unknown("DATABASE") as Database;
+ const databaseStore = useDatabaseStore();
+ for (const item of includedList || []) {
+ if (item.type == "database" && item.id == databaseId) {
+ database = databaseStore.convert(item, includedList);
+ break;
+ }
+ }
+
+ const columnList = (table.attributes.columnList as Column[]) || [];
+ const indexList = (table.attributes.indexList as TableIndex[]) || [];
+
+ return {
+ ...(table.attributes as Omit<
+ Table,
+ "id" | "database" | "creator" | "updater" | "columnList" | "indexList"
+ >),
+ id: parseInt(table.id),
+ creator: getPrincipalFromIncludedList(
+ table.relationships!.creator.data,
+ includedList
+ ),
+ updater: getPrincipalFromIncludedList(
+ table.relationships!.updater.data,
+ includedList
+ ),
+ columnList,
+ indexList,
+ database,
+ };
+}
+
+export const useTableStore = defineStore("table", {
+ state: (): TableState => ({
+ tableListByDatabaseId: new Map(),
+ }),
+
+ actions: {
+ getTableListByDatabaseIdAndTableName(
+ databaseId: DatabaseId,
+ tableName: string
+ ): Table | undefined {
+ const list = this.tableListByDatabaseId.get(databaseId);
+ if (list) {
+ return list.find((item: Table) => item.name == tableName);
+ }
+ return undefined;
+ },
+
+ getTableListByDatabaseId(databaseId: DatabaseId): Table[] {
+ return this.tableListByDatabaseId.get(databaseId) || [];
+ },
+
+ setTableByDatabaseIdAndTableName({
+ databaseId,
+ tableName,
+ table,
+ }: {
+ databaseId: DatabaseId;
+ tableName: string;
+ table: Table;
+ }) {
+ const list = this.tableListByDatabaseId.get(databaseId);
+ if (list) {
+ const i = list.findIndex((item: Table) => item.name == tableName);
+ if (i != -1) {
+ list[i] = table;
+ } else {
+ list.push(table);
+ }
+ } else {
+ this.tableListByDatabaseId.set(databaseId, [table]);
+ }
+ },
+
+ setTableListByDatabaseId({
+ databaseId,
+ tableList,
+ }: {
+ databaseId: DatabaseId;
+ tableList: Table[];
+ }) {
+ this.tableListByDatabaseId.set(databaseId, tableList);
+ },
+
+ async fetchTableByDatabaseIdAndTableName({
+ databaseId,
+ tableName,
+ }: {
+ databaseId: DatabaseId;
+ tableName: string;
+ }) {
+ const data = (
+ await axios.get(`/api/database/${databaseId}/table/${tableName}`)
+ ).data;
+ const table = convert(data.data, data.included);
+
+ this.setTableByDatabaseIdAndTableName({
+ databaseId,
+ tableName,
+ table,
+ });
+ return table;
+ },
+
+ async fetchTableListByDatabaseId(databaseId: DatabaseId) {
+ const data = (await axios.get(`/api/database/${databaseId}/table`)).data;
+ const tableList = data.data.map((table: ResourceObject) => {
+ return convert(table, data.included);
+ });
+
+ this.setTableListByDatabaseId({ databaseId, tableList });
+ return tableList;
+ },
+ },
+});
diff --git a/frontend/src/views/TableDetail.vue b/frontend/src/views/TableDetail.vue
index 00d6bf36e2..13877ef22f 100644
--- a/frontend/src/views/TableDetail.vue
+++ b/frontend/src/views/TableDetail.vue
@@ -204,15 +204,16 @@
</template>
<script lang="ts">
-import { computed } from "vue";
-import { useStore } from "vuex";
+import { computed, defineComponent } from "vue";
import ColumnTable from "../components/ColumnTable.vue";
import IndexTable from "../components/IndexTable.vue";
import InstanceEngineIcon from "../components/InstanceEngineIcon.vue";
import { bytesToString, connectionSlug, idFromSlug } from "../utils";
import { useRouter } from "vue-router";
+import { useTableStore } from "@/store";
+import { Table } from "@/types";
-export default {
+export default defineComponent({
name: "TableDetail",
components: { ColumnTable, IndexTable, InstanceEngineIcon },
props: {
@@ -226,14 +227,14 @@ export default {
},
},
setup(props) {
- const store = useStore();
const router = useRouter();
+ const tableStore = useTableStore();
const table = computed(() => {
- return store.getters["table/tableListByDatabaseIdAndTableName"](
+ return tableStore.getTableListByDatabaseIdAndTableName(
idFromSlug(props.databaseSlug),
props.tableName
- );
+ ) as Table;
});
const database = computed(() => {
@@ -256,5 +257,5 @@ export default {
bytesToString,
};
},
-};
+});
</script>