Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
DataSourceTable
天舟5fbe5691518c
diff --git a/frontend/src/components/DataSourceTable.vue b/frontend/src/components/DataSourceTable.vuenew file mode 100644index 0000000000..ff626e75bc--- /dev/null+++ b/frontend/src/components/DataSourceTable.vue@@ -0,0 +1,198 @@+<template>+ <div class="flex flex-col space-y-2">+ <div class="flex justify-between items-center">+ <h3 class="text-lg leading-6 font-medium text-gray-900">+ All Data Source+ </h3>+ <BBTableSearch+ class="w-56"+ ref="searchField"+ :placeholder="'Search data source name'"+ @change-text="(text) => changeSearchText(text)"+ />+ </div>+ <BBTable+ :columnList="columnList"+ :sectionDataSource="dataSourceSectionList"+ :showHeader="true"+ @click-row="clickDataSource"+ >+ <template v-slot:header>+ <BBTableHeaderCell+ :leftPadding="4"+ class="w-4 table-cell"+ :title="columnList[0].title"+ />+ <BBTableHeaderCell+ class="w-4 table-cell"+ :title="columnList[1].title"+ />+ <BBTableHeaderCell+ class="w-4 table-cell"+ :title="columnList[2].title"+ />+ <BBTableHeaderCell+ class="w-24 table-cell"+ :title="columnList[3].title"+ />+ <BBTableHeaderCell+ class="w-4 table-cell"+ :title="columnList[4].title"+ />+ <BBTableHeaderCell+ class="w-4 table-cell"+ :title="columnList[5].title"+ />+ </template>+ <template v-slot:body="{ rowData: dataSource }">+ <BBTableCell :leftPadding="4" class="w-4 table-cell text-gray-500">+ <span class="">{{ dataSource.name }}</span>+ </BBTableCell>+ <BBTableCell class="w-24 table-cell">+ {{ dataSource.type }}+ </BBTableCell>+ <BBTableCell class="w-24 table-cell">+ {{ dataSource.username }}+ </BBTableCell>+ <BBTableCell class="w-24 table-cell">+ {{ dataSource.password }}+ </BBTableCell>+ <BBTableCell class="w-24 table-cell">+ {{ humanizeTs(dataSource.lastUpdatedTs) }}+ </BBTableCell>+ <BBTableCell class="w-24 table-cell">+ {{ humanizeTs(dataSource.createdTs) }}+ </BBTableCell>+ </template>+ </BBTable>+ </div>+</template>++<script lang="ts">+import { computed, reactive, watchEffect, PropType } from "vue";+import { useStore } from "vuex";+import { useRouter } from "vue-router";+import { BBTableColumn, BBTableSectionDataSource } from "../bbkit/types";++import { idFromSlug, dataSourceSlug, instanceSlug } from "../utils";+import { Instance, DataSource } from "../types";++const columnList: BBTableColumn[] = [+ {+ title: "Name",+ },+ {+ title: "Type",+ },+ {+ title: "Username",+ },+ {+ title: "Password",+ },+ {+ title: "Last updated",+ },+ {+ title: "Created",+ },+];++interface LocalState {+ searchText: string;+}++export default {+ name: "DataSourceTable",+ components: {},+ props: {+ instance: {+ required: true,+ type: Object as PropType<Instance>,+ },+ },+ setup(props, ctx) {+ const store = useStore();+ const router = useRouter();++ const state = reactive<LocalState>({+ searchText: "",+ });++ const prepareDataSourceList = () => {+ store+ .dispatch(+ "dataSource/fetchDataSourceListByInstanceId",+ props.instance.id+ )+ .catch((error) => {+ console.log(error);+ });+ };++ watchEffect(prepareDataSourceList);++ const dataSourceSectionList = computed(() => {+ const adminList = [];+ const readWriteList = [];+ const readOnlyList = [];+ for (const item of store.getters["dataSource/dataSourceListByInstanceId"](+ props.instance.id+ )) {+ if (+ !state.searchText ||+ item.name.toLowerCase().includes(state.searchText.toLowerCase())+ ) {+ if (item.type === "ADMIN") {+ adminList.push(item);+ } else if (item.type === "READWRITE") {+ readWriteList.push(item);+ } else if (item.type === "READONLY") {+ readOnlyList.push(item);+ }+ }+ }+ const dataSource = [];+ dataSource.push({+ title: "Admin",+ list: adminList,+ });+ dataSource.push({+ title: "Read and write",+ list: readWriteList,+ });+ dataSource.push({+ title: "Read only",+ list: readOnlyList,+ });+ return dataSource;+ });++ const clickDataSource = function (section: number, row: number) {+ const ds = dataSourceSectionList.value[section].list[row];+ const environmentName = store.getters["environment/environmentById"](+ props.instance.environmentId+ )?.name;+ router.push(+ `/instance/${instanceSlug(+ environmentName,+ props.instance.name,+ props.instance.id+ )}/ds/${dataSourceSlug(ds.name, ds.id)}`+ );+ };++ const changeSearchText = (searchText: string) => {+ state.searchText = searchText;+ };++ return {+ state,+ columnList,+ dataSourceSectionList,+ clickDataSource,+ changeSearchText,+ };+ },+};+</script>diff --git a/frontend/src/miragejs/factories/dataSource.js b/frontend/src/miragejs/factories/dataSource.jsindex 54f9d00e04..c25d564b5c 100644--- a/frontend/src/miragejs/factories/dataSource.js+++ b/frontend/src/miragejs/factories/dataSource.js@@ -5,8 +5,22 @@ export default {name(i) {return "ds" + i;},+ createdTs(i) {+ return Date.now() - (i + 1) * 1800 * 1000;+ },+ lastUpdatedTs(i) {+ return Date.now() - i * 3600 * 1000;+ },type(i) {- return i == 0 ? "ADMIN" : "NORMAL";+ if (i % 3 == 0) {+ return "ADMIN";+ }+ if (i % 3 == 1) {+ return "READWRITE";+ }+ if (i % 3 == 2) {+ return "READONLY";+ }},username() {return "root";diff --git a/frontend/src/store/modules/dataSource.ts b/frontend/src/store/modules/dataSource.tsindex 1c37d80abe..352de85c15 100644--- a/frontend/src/store/modules/dataSource.ts+++ b/frontend/src/store/modules/dataSource.ts@@ -24,7 +24,7 @@ const state: () => DataSourceState = () => ({const getters = {adminDataSourceByInstanceId: (state: DataSourceState) => (instanceId: InstanceId- ) => {+ ): DataSource | undefined => {const list = state.dataSourceListByInstanceId.get(instanceId);if (list) {for (const item of list) {@@ -33,16 +33,18 @@ const getters = {}}}- return null;+ return undefined;},dataSourceListByInstanceId: (state: DataSourceState) => (instanceId: InstanceId- ) => {- return state.dataSourceListByInstanceId.get(instanceId);+ ): DataSource[] => {+ return state.dataSourceListByInstanceId.get(instanceId) || [];},- dataSourceById: (state: DataSourceState) => (dataSourceId: DataSourceId) => {+ dataSourceById: (state: DataSourceState) => (+ dataSourceId: DataSourceId+ ): DataSource | undefined => {return state.dataSourceById.get(dataSourceId);},};@@ -63,11 +65,17 @@ const actions = {return dataSourceList;},- async fetchDataSourceById({ commit }: any, dataSourceId: DataSourceId) {+ async fetchDataSourceById(+ { commit }: any,+ {+ instanceId,+ dataSourceId,+ }: { instanceId: InstanceId; dataSourceId: DataSourceId }+ ) {const dataSource = convert((await axios.get(- `/api/instance/${dataSourceId.instanceId}/datasource/${dataSourceId.id}`+ `/api/instance/${instanceId}/datasource/${dataSourceId}`)).data.data);@@ -145,10 +153,13 @@ const actions = {async deleteDataSourceById({ state, commit }: { state: DataSourceState; commit: any },- dataSourceId: DataSourceId+ {+ instanceId,+ dataSourceId,+ }: { instanceId: InstanceId; dataSourceId: DataSourceId }) {await axios.delete(- `/api/instance/${dataSourceId.instanceId}/datasource/${dataSourceId.id}`+ `/api/instance/${instanceId}/datasource/${dataSourceId}`);commit("setDataSourceById", {@@ -228,13 +239,14 @@ const mutations = {deleteDataSourceInListById(state: DataSourceState,- dataSourceId: DataSourceId+ {+ instanceId,+ dataSourceId,+ }: { instanceId: InstanceId; dataSourceId: DataSourceId }) {- const list = state.dataSourceListByInstanceId.get(dataSourceId.instanceId);+ const list = state.dataSourceListByInstanceId.get(instanceId);if (list) {- const i = list.findIndex(- (item: DataSource) => item.id == dataSourceId.id- );+ const i = list.findIndex((item: DataSource) => item.id == dataSourceId);if (i != -1) {list.splice(i, 1);}diff --git a/frontend/src/types/bytebase.ts b/frontend/src/types/bytebase.tsindex 2b04e48cfe..3942db5a37 100644--- a/frontend/src/types/bytebase.ts+++ b/frontend/src/types/bytebase.ts@@ -25,10 +25,7 @@ export type EnvironmentId = string;export type InstanceId = string;-export type DataSourceId = {- id: string;- instanceId: InstanceId;-};+export type DataSourceId = string;export type CommandId = string;export type CommandRegisterId = string;@@ -232,17 +229,24 @@ export type Instance = {};export type InstanceNew = Omit<Instance, "id">;-export type DataSourceType = "ADMIN" | "NORMAL";+export type DataSourceType = "ADMIN" | "READWRITE" | "READONLY";// Data Sourceexport type DataSource = {id: string;name: string;+ createdTs: number;+ lastUpdatedTs: number;type: DataSourceType;// In mysql, username can be empty which means anonymous userusername?: string;password?: string;};-export type DataSourceNew = Omit<DataSource, "id">;+export type DataSourceNew = {+ name: string;+ type: DataSourceType;+ username?: string;+ password?: string;+};// Authexport type LoginInfo = {diff --git a/frontend/src/utils/slug.ts b/frontend/src/utils/slug.tsindex c98d481531..7d198ab1f7 100644--- a/frontend/src/utils/slug.ts+++ b/frontend/src/utils/slug.ts@@ -1,4 +1,4 @@-import { InstanceId, TaskId } from "../types";+import { DataSourceId, InstanceId, TaskId } from "../types";import slug from "slug";export function taskSlug(taskName: string, taskId: TaskId) {@@ -9,10 +9,17 @@ export function instanceSlug(environmentName: string,instanceName: string,instanceId: InstanceId-) {+): string {return [slug(environmentName), slug(instanceName), instanceId].join("-");}+export function dataSourceSlug(+ dataSourceName: string,+ dataSourceId: DataSourceId+): string {+ return [slug(dataSourceName), dataSourceId].join("-");+}+export function idFromSlug(slug: string) {const parts = slug.split("-");return parts[parts.length - 1];diff --git a/frontend/src/views/InstanceDetail.vue b/frontend/src/views/InstanceDetail.vueindex 4c5f4946b5..1b915e8105 100644--- a/frontend/src/views/InstanceDetail.vue+++ b/frontend/src/views/InstanceDetail.vue@@ -13,7 +13,7 @@ input[type="number"] {</style><template>- <form class="px-4 space-y-6 divide-y divide-control-border">+ <div class="px-4 space-y-6 divide-y divide-control-border"><!-- Instance Name --><div class="grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-6"><div class="sm:col-span-2">@@ -261,7 +261,11 @@ input[type="number"] {</div></div></div>- </form>++ <div v-if="!state.new" class="pt-6 space-y-4">+ <DataSourceTable :instance="state.instance" />+ </div>+ </div><BBAlertv-if="state.showDeleteModal":style="'CRITICAL'"@@ -300,6 +304,7 @@ import { useRouter } from "vue-router";import cloneDeep from "lodash-es/cloneDeep";import isEqual from "lodash-es/isEqual";import { urlfy, idFromSlug } from "../utils";+import DataSourceTable from "../components/DataSourceTable.vue";import EnvironmentSelect from "../components/EnvironmentSelect.vue";import { Instance, InstanceNew, DataSource, DataSourceNew } from "../types";@@ -324,6 +329,7 @@ export default {name: "InstanceDetail",emits: ["delete"],components: {+ DataSourceTable,EnvironmentSelect,},props: {