Skip to content

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

Rework the logic of how to fetching database info for the DataSource

天舟07048e623d3e
diff --git a/frontend/src/components/DataSourceTable.vue b/frontend/src/components/DataSourceTable.vue
index fd010eaa69..3aa0742bb5 100644
--- a/frontend/src/components/DataSourceTable.vue
+++ b/frontend/src/components/DataSourceTable.vue
@@ -23,20 +23,16 @@
class="w-24"
:title="columnList[0].title"
/>
- <BBTableHeaderCell class="w-24" :title="columnList[1].title" />
- <BBTableHeaderCell class="w-4" :title="columnList[2].title" />
+ <BBTableHeaderCell class="w-4" :title="columnList[1].title" />
+ <BBTableHeaderCell class="w-8" :title="columnList[2].title" />
<BBTableHeaderCell class="w-8" :title="columnList[3].title" />
- <BBTableHeaderCell class="w-8" :title="columnList[4].title" />
+ <BBTableHeaderCell class="w-16" :title="columnList[4].title" />
<BBTableHeaderCell class="w-16" :title="columnList[5].title" />
- <BBTableHeaderCell class="w-16" :title="columnList[6].title" />
</template>
<template v-slot:body="{ rowData: dataSource }">
- <BBTableCell :leftPadding="4" class="text-gray-500">
+ <BBTableCell :leftPadding="4">
<span class="">{{ dataSource.name }}</span>
</BBTableCell>
- <BBTableCell>
- {{ dataSource.database ? dataSource.database.name : "*" }}
- </BBTableCell>
<BBTableCell>
{{ dataSource.type }}
</BBTableCell>
@@ -70,9 +66,6 @@ const columnList: BBTableColumn[] = [
{
title: "Name",
},
- {
- title: "Database",
- },
{
title: "Type",
},
@@ -119,18 +112,20 @@ export default {
for (const dataSource of store.getters[
"dataSource/dataSourceListByInstanceId"
](props.instance.id)) {
+ let databaseName = "*";
+ if (dataSource.databaseId) {
+ databaseName = store.getters["database/databaseById"](
+ dataSource.databaseId,
+ props.instance.id
+ ).name;
+ }
if (
!state.searchText ||
dataSource.name
.toLowerCase()
.includes(state.searchText.toLowerCase()) ||
- dataSource.database?.name
- .toLowerCase()
- .includes(state.searchText.toLowerCase())
+ databaseName.toLowerCase().includes(state.searchText.toLowerCase())
) {
- const databaseName = dataSource.database
- ? dataSource.database.name
- : "*";
const list = dataSourceListByDatabase.get(databaseName);
if (list) {
list.push(dataSource);
@@ -157,7 +152,7 @@ export default {
const sectionList = dataSourceListByDatabase.get("*")
? [
{
- title: "All databases (*)",
+ title: "* (All databases)",
list: dataSourceListByDatabase.get("*"),
},
]
diff --git a/frontend/src/store/modules/dataSource.ts b/frontend/src/store/modules/dataSource.ts
index 810b74e57e..3b997a12ac 100644
--- a/frontend/src/store/modules/dataSource.ts
+++ b/frontend/src/store/modules/dataSource.ts
@@ -11,19 +11,20 @@ import {
ResourceIdentifier,
} from "../../types";
-function convert(dataSource: ResourceObject, rootGetters: any): DataSource {
+function convert(dataSource: ResourceObject): DataSource {
const databaseId = dataSource.relationships!.database.data
? (dataSource.relationships!.database.data as ResourceIdentifier).id
: undefined;
const instanceId = (dataSource.relationships!.instance
.data as ResourceIdentifier).id;
-
return {
id: dataSource.id,
- database: databaseId
- ? rootGetters["database/databaseById"](databaseId, instanceId)
- : undefined,
- ...(dataSource.attributes as Omit<DataSource, "id" | "database">),
+ instanceId,
+ databaseId,
+ ...(dataSource.attributes as Omit<
+ DataSource,
+ "id" | "instanceId" | "databaseId"
+ >),
};
}
@@ -87,13 +88,13 @@ const getters = {
const actions = {
async fetchDataSourceListByInstanceId(
- { commit, rootGetters }: any,
+ { commit }: any,
instanceId: InstanceId
) {
const dataSourceList = (
await axios.get(`/api/instance/${instanceId}/datasource`)
).data.data.map((datasource: ResourceObject) => {
- return convert(datasource, rootGetters);
+ return convert(datasource);
});
commit("setDataSourceListByInstanceId", { instanceId, dataSourceList });
@@ -102,7 +103,7 @@ const actions = {
},
async fetchDataSourceById(
- { commit, rootGetters }: any,
+ { commit }: any,
{
instanceId,
dataSourceId,
@@ -113,8 +114,7 @@ const actions = {
await axios.get(
`/api/instance/${instanceId}/datasource/${dataSourceId}`
)
- ).data.data,
- rootGetters
+ ).data.data
);
commit("setDataSourceById", {
@@ -125,7 +125,7 @@ const actions = {
},
async createDataSource(
- { commit, rootGetters }: any,
+ { commit }: any,
{
instanceId,
newDataSource,
@@ -139,8 +139,7 @@ const actions = {
attributes: newDataSource,
},
})
- ).data.data,
- rootGetters
+ ).data.data
);
commit("appendDataSourceByInstanceId", {
@@ -152,7 +151,7 @@ const actions = {
},
async patchDataSource(
- { commit, rootGetters }: any,
+ { commit }: any,
{
instanceId,
dataSource,
@@ -173,8 +172,7 @@ const actions = {
},
}
)
- ).data.data,
- rootGetters
+ ).data.data
);
commit("setDataSourceById", {
diff --git a/frontend/src/types/bytebase.ts b/frontend/src/types/bytebase.ts
index 7780dd1d46..9cd65d5fe6 100644
--- a/frontend/src/types/bytebase.ts
+++ b/frontend/src/types/bytebase.ts
@@ -246,7 +246,14 @@ export type DataSource = {
username?: string;
password?: string;
// If empty, it means it can access all databases from an instance.
- database?: string;
+ // TODO: unlike other objects like environment, here we don't expand
+ // to the database object, this is due to timing issue during rendering.
+ // Unlike environment which is a global state that we can load upon
+ // startup, the database info is per instance, and the author haven't
+ // figured out a elegant way to guarantee it's loaded before the router
+ // fetches the specific data source and requires the database info for
+ // the conversion
+ databaseId?: DatabaseId;
};
export type DataSourceNew = {
diff --git a/frontend/src/views/DataSourceDetail.vue b/frontend/src/views/DataSourceDetail.vue
index 113628c28d..c3620719c5 100644
--- a/frontend/src/views/DataSourceDetail.vue
+++ b/frontend/src/views/DataSourceDetail.vue
@@ -65,7 +65,8 @@
:to="`/instance/${instanceSlug}`"
class="normal-link"
>
- {{ instance.name }}
+ {{ instance.name }} -
+ {{ databaseName }}
</router-link>
</dd>
<dt class="sr-only">Environment</dt>
@@ -177,9 +178,21 @@ export default {
return store.getters["instance/instanceById"](instanceId);
});
+ const databaseName = computed(() => {
+ if (dataSource.value.databaseId) {
+ const database = store.getters["database/databaseById"](
+ dataSource.value.databaseId,
+ instanceId
+ );
+ return database.name;
+ }
+ return "* (All databases)";
+ });
+
return {
dataSource,
instance,
+ databaseName,
};
},
};