Skip to content

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

Add datasource store

Tianzhou Chenfb90bef75382
diff --git a/frontend/src/store/index.ts b/frontend/src/store/index.ts
index d2d11a2804..381db9fd22 100644
--- a/frontend/src/store/index.ts
+++ b/frontend/src/store/index.ts
@@ -4,6 +4,7 @@ import bookmark from "./modules/bookmark";
import project from "./modules/project";
import environment from "./modules/environment";
import instance from "./modules/instance";
+import datasource from "./modules/datasource";
import repository from "./modules/repository";
import auth from "./modules/auth";
import group from "./modules/group";
@@ -20,6 +21,7 @@ export const store: Store<any> = createStore({
project,
environment,
instance,
+ datasource,
repository,
auth,
group,
diff --git a/frontend/src/store/modules/datasource.ts b/frontend/src/store/modules/datasource.ts
new file mode 100644
index 0000000000..a73e5e2094
--- /dev/null
+++ b/frontend/src/store/modules/datasource.ts
@@ -0,0 +1,230 @@
+import axios from "axios";
+import {
+ DataSourceId,
+ DataSource,
+ NewDataSource,
+ DataSourceState,
+ InstanceId,
+} from "../../types";
+
+const state: () => DataSourceState = () => ({
+ dataSourceListByInstanceId: new Map(),
+ dataSourceById: new Map(),
+});
+
+const getters = {
+ adminDataSourceByInstanceId: (state: DataSourceState) => (
+ instanceId: InstanceId
+ ) => {
+ const list = state.dataSourceListByInstanceId.get(instanceId);
+ if (list) {
+ for (const item of list) {
+ if (item.attributes.type == "ADMIN") {
+ return item;
+ }
+ }
+ }
+ return null;
+ },
+ dataSourceListByInstanceId: (state: DataSourceState) => (
+ instanceId: InstanceId
+ ) => {
+ return state.dataSourceListByInstanceId.get(instanceId);
+ },
+ dataSourceById: (state: DataSourceState) => (dataSourceId: DataSourceId) => {
+ return state.dataSourceById.get(dataSourceId);
+ },
+};
+
+const actions = {
+ async fetchDataSourceListByInstanceId(
+ { commit }: any,
+ instanceId: InstanceId
+ ) {
+ const dataSourceList = (
+ await axios.get(`/api/instance/${instanceId}/datasource`)
+ ).data.data;
+
+ commit("setDataSourceListByInstanceId", { instanceId, dataSourceList });
+
+ return dataSourceList;
+ },
+
+ async fetchDataSourceById({ commit }: any, dataSourceId: DataSourceId) {
+ const dataSource = (
+ await axios.get(
+ `/api/instance/${dataSourceId.instanceId}/datasource/${dataSourceId.id}`
+ )
+ ).data.data;
+ commit("setDataSourceById", {
+ dataSourceId,
+ dataSource,
+ });
+ return dataSource;
+ },
+
+ async createDataSource(
+ { commit }: any,
+ {
+ instanceId,
+ newDataSource,
+ }: { instanceId: InstanceId; newDataSource: NewDataSource }
+ ) {
+ const createdDataSource = (
+ await axios.post(`/api/instance/${instanceId}/datasource`, {
+ data: newDataSource,
+ })
+ ).data.data;
+
+ commit("appendDataSourceByInstanceId", {
+ dataSource: createdDataSource,
+ instanceId,
+ });
+
+ return createdDataSource;
+ },
+
+ async patchDataSourceById(
+ { commit }: any,
+ {
+ dataSourceId,
+ dataSource,
+ }: {
+ dataSourceId: DataSourceId;
+ dataSource: DataSource;
+ }
+ ) {
+ const updatedDataSource = (
+ await axios.patch(
+ `/api/instance/${dataSourceId.instanceId}/datasource/${dataSourceId.id}`,
+ {
+ data: dataSource,
+ }
+ )
+ ).data.data;
+
+ commit("setDataSourceById", {
+ instanceId: dataSourceId.instanceId,
+ updatedDataSource,
+ });
+
+ commit("replaceDataSourceInListByInstanceId", {
+ instanceId: dataSourceId.instanceId,
+ updatedDataSource,
+ });
+
+ return updatedDataSource;
+ },
+
+ async deleteDataSourceById(
+ { state, commit }: { state: DataSourceState; commit: any },
+ {
+ dataSourceId,
+ }: {
+ dataSourceId: DataSourceId;
+ }
+ ) {
+ await axios.delete(
+ `/api/instance/${dataSourceId.instanceId}/datasource/${dataSourceId.id}`
+ );
+
+ commit("setDataSourceById", {
+ dataSourceId: dataSourceId,
+ dataSource: null,
+ });
+
+ commit("deleteDataSourceInListById", {
+ dataSourceId,
+ });
+ },
+};
+
+const mutations = {
+ setDataSourceListByInstanceId(
+ state: DataSourceState,
+ {
+ instanceId,
+ dataSourceList,
+ }: {
+ instanceId: InstanceId;
+ dataSourceList: DataSource[];
+ }
+ ) {
+ state.dataSourceListByInstanceId.set(instanceId, dataSourceList);
+ },
+ setDataSourceById(
+ state: DataSourceState,
+ {
+ dataSourceId,
+ dataSource,
+ }: {
+ dataSourceId: DataSourceId;
+ dataSource: DataSource;
+ }
+ ) {
+ state.dataSourceById.set(dataSourceId, dataSource);
+ },
+ appendDataSourceByInstanceId(
+ state: DataSourceState,
+ {
+ instanceId,
+ dataSource,
+ }: {
+ instanceId: InstanceId;
+ dataSource: DataSource;
+ }
+ ) {
+ const list = state.dataSourceListByInstanceId.get(instanceId);
+ if (list) {
+ list.push(dataSource);
+ } else {
+ state.dataSourceListByInstanceId.set(instanceId, [dataSource]);
+ }
+ },
+ replaceDataSourceInListByInstanceId(
+ state: DataSourceState,
+ {
+ instanceId,
+ updatedDataSource,
+ }: {
+ instanceId: InstanceId;
+ updatedDataSource: DataSource;
+ }
+ ) {
+ const list = state.dataSourceListByInstanceId.get(instanceId);
+ if (list) {
+ const i = list.findIndex(
+ (item: DataSource) => item.id == updatedDataSource.id
+ );
+ if (i != -1) {
+ list[i] = updatedDataSource;
+ }
+ }
+ },
+ deleteDataSourceInListById(
+ state: DataSourceState,
+ {
+ dataSourceId,
+ }: {
+ dataSourceId: DataSourceId;
+ }
+ ) {
+ const list = state.dataSourceListByInstanceId.get(dataSourceId.instanceId);
+ if (list) {
+ const i = list.findIndex(
+ (item: DataSource) => item.id == dataSourceId.id
+ );
+ if (i != -1) {
+ list.splice(i, 1);
+ }
+ }
+ },
+};
+
+export default {
+ namespaced: true,
+ state,
+ getters,
+ actions,
+ mutations,
+};
diff --git a/frontend/src/types/bytebase.ts b/frontend/src/types/bytebase.ts
index cb126c4e37..1ee14b6bea 100644
--- a/frontend/src/types/bytebase.ts
+++ b/frontend/src/types/bytebase.ts
@@ -14,6 +14,11 @@ export type EnvironmentId = string;
export type InstanceId = string;
+export type DataSourceId = {
+ id: string;
+ instanceId: InstanceId;
+};
+
// Models
export type User = ResourceObject & {};
export type NewUser = Omit<ResourceObject, "id">;
@@ -84,6 +89,18 @@ export type Instance = ResourceObject & {
};
export type NewInstance = Omit<ResourceObject, "id">;
+export type DataSource = ResourceObject & {
+ attributes: {
+ name: string;
+ type: "ADMIN" | "NORMAL";
+ // In mysql, username can be empty which means annonymous user
+ username?: string;
+ password?: string;
+ database?: string;
+ };
+};
+export type NewDataSource = Omit<ResourceObject, "id">;
+
export type Group = ResourceObject & {};
export type NewGroup = Omit<ResourceObject, "id">;
@@ -149,6 +166,11 @@ export interface InstanceState {
instanceById: Map<InstanceId, Instance>;
}
+export interface DataSourceState {
+ dataSourceListByInstanceId: Map<InstanceId, DataSource[]>;
+ dataSourceById: Map<DataSourceId, DataSource>;
+}
+
export interface RepositoryState {
repositoryByProject: Map<ProjectId, Repository>;
}