Skip to content

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

Use a new DataSource type which is decoupled from the underlying API model

Tianzhou Chena301b71a17f0
diff --git a/frontend/src/store/modules/dataSource.ts b/frontend/src/store/modules/dataSource.ts
index 75ae1c45a8..f9720e1d5f 100644
--- a/frontend/src/store/modules/dataSource.ts
+++ b/frontend/src/store/modules/dataSource.ts
@@ -4,9 +4,21 @@ import {
DataSource,
DataSourceNew,
DataSourceState,
+ DataSourceType,
InstanceId,
+ ResourceObject,
} from "../../types";
+function convert(dataSource: ResourceObject): DataSource {
+ return {
+ id: dataSource.id,
+ name: dataSource.attributes.name as string,
+ type: dataSource.attributes.type as DataSourceType,
+ username: dataSource.attributes.username as string,
+ password: dataSource.attributes.password as string,
+ };
+}
+
const state: () => DataSourceState = () => ({
dataSourceListByInstanceId: new Map(),
dataSourceById: new Map(),
@@ -19,7 +31,7 @@ const getters = {
const list = state.dataSourceListByInstanceId.get(instanceId);
if (list) {
for (const item of list) {
- if (item.attributes.type == "ADMIN") {
+ if (item.type == "ADMIN") {
return item;
}
}
@@ -45,7 +57,9 @@ const actions = {
) {
const dataSourceList = (
await axios.get(`/api/instance/${instanceId}/datasource`)
- ).data.data;
+ ).data.data.map((env: ResourceObject) => {
+ return convert(env);
+ });
commit("setDataSourceListByInstanceId", { instanceId, dataSourceList });
@@ -53,11 +67,14 @@ const actions = {
},
async fetchDataSourceById({ commit }: any, dataSourceId: DataSourceId) {
- const dataSource = (
- await axios.get(
- `/api/instance/${dataSourceId.instanceId}/datasource/${dataSourceId.id}`
- )
- ).data.data;
+ const dataSource = convert(
+ (
+ await axios.get(
+ `/api/instance/${dataSourceId.instanceId}/datasource/${dataSourceId.id}`
+ )
+ ).data.data
+ );
+
commit("setDataSourceById", {
dataSourceId,
dataSource,
@@ -72,11 +89,19 @@ const actions = {
newDataSource,
}: { instanceId: InstanceId; newDataSource: DataSourceNew }
) {
- const createdDataSource = (
- await axios.post(`/api/instance/${instanceId}/datasource`, {
- data: newDataSource,
- })
- ).data.data;
+ const createdDataSource = convert(
+ (
+ await axios.post(`/api/instance/${instanceId}/datasource`, {
+ data: {
+ type: "dataSource",
+ attributes: {
+ type: newDataSource.type,
+ name: newDataSource.name,
+ },
+ },
+ })
+ ).data.data
+ );
commit("appendDataSourceByInstanceId", {
dataSource: createdDataSource,
@@ -96,14 +121,22 @@ const actions = {
dataSource: DataSource;
}
) {
- const updatedDataSource = (
- await axios.patch(
- `/api/instance/${instanceId}/datasource/${dataSource.id}`,
- {
- data: dataSource,
- }
- )
- ).data.data;
+ const { id, ...attrs } = dataSource;
+ const updatedDataSource = convert(
+ (
+ await axios.patch(
+ `/api/instance/${instanceId}/datasource/${dataSource.id}`,
+ {
+ data: {
+ type: "dataSource",
+ attributes: {
+ ...attrs,
+ },
+ },
+ }
+ )
+ ).data.data
+ );
commit("setDataSourceById", {
instanceId: instanceId,
diff --git a/frontend/src/types/bytebase.ts b/frontend/src/types/bytebase.ts
index 396aa2256a..73c755e052 100644
--- a/frontend/src/types/bytebase.ts
+++ b/frontend/src/types/bytebase.ts
@@ -216,14 +216,13 @@ export type Instance = ResourceObject & {
export type InstanceNew = Omit<Instance, "id">;
// Data Source
-export type DataSource = ResourceObject & {
- attributes: {
- name: string;
- type: "ADMIN" | "NORMAL";
- // In mysql, username can be empty which means anonymous user
- username?: string;
- password?: string;
- };
+export type DataSource = {
+ id: string;
+ name: string;
+ type: "ADMIN" | "NORMAL";
+ // In mysql, username can be empty which means anonymous user
+ username?: string;
+ password?: string;
};
export type DataSourceNew = Omit<DataSource, "id">;
diff --git a/frontend/src/views/InstanceDetail.vue b/frontend/src/views/InstanceDetail.vue
index 8b2588b68e..ea1a33eb99 100644
--- a/frontend/src/views/InstanceDetail.vue
+++ b/frontend/src/views/InstanceDetail.vue
@@ -154,7 +154,7 @@ input[type="number"] {
name="username"
type="text"
class="textfield mt-1 w-full"
- :value="state.adminDataSource.attributes.username"
+ :value="state.adminDataSource.username"
@input="updateDataSource('username', $event.target.value)"
/>
</div>
@@ -166,9 +166,10 @@ input[type="number"] {
<input
id="password"
name="password"
+ autocomplete="off"
:type="state.showPassword ? 'text' : 'password'"
class="textfield mt-1 w-full"
- :value="state.adminDataSource.attributes.password"
+ :value="state.adminDataSource.password"
@input="updateDataSource('password', $event.target.value)"
/>
<div class="ml-2 flex items-center">
@@ -308,11 +309,8 @@ import EnvironmentSelect from "../components/EnvironmentSelect.vue";
import { Instance, InstanceNew, DataSource, DataSourceNew } from "../types";
const INIT_DATA_SOURCE: DataSourceNew = {
- type: "dataSource",
- attributes: {
- type: "ADMIN",
- name: "Admin Data Source",
- },
+ type: "ADMIN",
+ name: "Admin Data Source",
};
interface LocalState {
@@ -388,7 +386,7 @@ export default {
};
const updateDataSource = (field: string, value: string) => {
- state.adminDataSource!.attributes[field] = value;
+ (state.adminDataSource as any)[field] = value;
};
const cancel = () => {