Skip to content

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

Add DataSourceMember model and its related API

天舟182046c4c07d
diff --git a/frontend/src/components/DataSourceMemberTable.vue b/frontend/src/components/DataSourceMemberTable.vue
new file mode 100644
index 0000000000..8896e09270
--- /dev/null
+++ b/frontend/src/components/DataSourceMemberTable.vue
@@ -0,0 +1,98 @@
+<template>
+ <BBTable
+ :columnList="columnList"
+ :dataSource="memberList"
+ :showHeader="true"
+ :rowClickable="false"
+ >
+ <template v-slot:body="{ rowData: member }">
+ <BBTableCell :leftPadding="4" class="w-24">
+ <BBAvatar :username="member.principal.name" />
+ <div class="flex flex-col">
+ <div class="flex flex-row items-center space-x-2">
+ <router-link :to="`/u/${member.principal.id}`" class="normal-link"
+ >{{ member.principal.name }}
+ </router-link>
+ <span
+ v-if="currentUser.id == member.principal.id"
+ class="inline-flex items-center px-2 py-0.5 rounded-lg text-xs font-semibold bg-green-100 text-green-800"
+ >
+ You
+ </span>
+ </div>
+ <span class="textlabel">
+ {{ member.principal.email }}
+ </span>
+ </div>
+ </BBTableCell>
+ <BBTableCell class="w-8">
+ <router-link :to="`/task/${member.taskId}`" class="normal-link"
+ >t/{{ member.taskId }}
+ </router-link>
+ </BBTableCell>
+ <BBTableCell class="w-8">
+ {{ humanizeTs(member.createdTs) }}
+ </BBTableCell>
+ </template>
+ </BBTable>
+</template>
+
+<script lang="ts">
+import { computed, watchEffect } from "vue";
+import { useStore } from "vuex";
+
+const columnList = [
+ {
+ title: "User",
+ },
+ {
+ title: "Requested task",
+ },
+ {
+ title: "Added time",
+ },
+];
+
+export default {
+ name: "DataSourceMemberTable",
+ components: {},
+ props: {
+ instanceId: {
+ required: true,
+ type: String,
+ },
+ dataSourceId: {
+ required: true,
+ type: String,
+ },
+ },
+ setup(props, ctx) {
+ const store = useStore();
+
+ const currentUser = computed(() => store.getters["auth/currentUser"]());
+
+ const prepareMemberList = () => {
+ store
+ .dispatch("dataSource/fetchMemberListById", {
+ instanceId: props.instanceId,
+ dataSourceId: props.dataSourceId,
+ })
+ .catch((error) => {
+ console.log(error);
+ });
+ };
+
+ watchEffect(prepareMemberList);
+
+ const memberList = computed(() =>
+ store.getters["dataSource/memberListById"](props.dataSourceId)
+ );
+
+ return {
+ columnList,
+ currentUser,
+ memberList,
+ };
+ },
+};
+</script>
diff --git a/frontend/src/miragejs/factories/dataSource.js b/frontend/src/miragejs/factories/dataSource.js
index b34d5f7030..118c409a1c 100644
--- a/frontend/src/miragejs/factories/dataSource.js
+++ b/frontend/src/miragejs/factories/dataSource.js
@@ -25,5 +25,18 @@ export default {
password(i) {
return "pwd" + i;
},
+ afterCreate(dataSource, server) {
+ server.create("dataSourceMember", {
+ dataSource,
+ principalId: "1",
+ taskId: "2",
+ });
+
+ server.create("dataSourceMember", {
+ dataSource,
+ principalId: "2",
+ taskId: "3",
+ });
+ },
}),
};
diff --git a/frontend/src/miragejs/factories/dataSourceMember.js b/frontend/src/miragejs/factories/dataSourceMember.js
new file mode 100644
index 0000000000..a434193a74
--- /dev/null
+++ b/frontend/src/miragejs/factories/dataSourceMember.js
@@ -0,0 +1,15 @@
+import { Factory } from "miragejs";
+
+export default {
+ dataSourceMember: Factory.extend({
+ principalId(i) {
+ return "100";
+ },
+ taskId(i) {
+ return (i + 1).toString();
+ },
+ createdTs(i) {
+ return Date.now() - (i + 1) * 1800 * 1000;
+ },
+ }),
+};
diff --git a/frontend/src/miragejs/factories/index.js b/frontend/src/miragejs/factories/index.js
index 206567175e..2c26169253 100644
--- a/frontend/src/miragejs/factories/index.js
+++ b/frontend/src/miragejs/factories/index.js
@@ -3,6 +3,7 @@ import bookmark from "./bookmark";
import environment from "./environment";
import instance from "./instance";
import dataSource from "./dataSource";
+import dataSourceMember from "./dataSourceMember";
import database from "./database";
import task from "./task";
import taskPatch from "./taskPatch";
@@ -27,6 +28,7 @@ export default {
...environment,
...instance,
...dataSource,
+ ...dataSourceMember,
...database,
...task,
...taskPatch,
diff --git a/frontend/src/miragejs/models/index.js b/frontend/src/miragejs/models/index.js
index 58aff84fdb..89012fe0e7 100644
--- a/frontend/src/miragejs/models/index.js
+++ b/frontend/src/miragejs/models/index.js
@@ -55,6 +55,11 @@ export default {
dataSource: Model.extend({
instance: belongsTo(),
database: belongsTo(),
+ dataSourceMember: hasMany(),
+ }),
+
+ dataSourceMember: Model.extend({
+ dataSource: belongsTo(),
}),
database: Model.extend({
diff --git a/frontend/src/miragejs/routes/index.js b/frontend/src/miragejs/routes/index.js
index 2346bc97ea..7698b893bf 100644
--- a/frontend/src/miragejs/routes/index.js
+++ b/frontend/src/miragejs/routes/index.js
@@ -422,8 +422,18 @@ export default function routes() {
function (schema, request) {
const instance = schema.instances.find(request.params.instanceId);
if (instance) {
+ const dataSource = schema.dataSources.find(request.params.id);
+ if (!dataSource) {
+ return new Response(
+ 404,
+ {},
+ {
+ errors: "Data source " + request.params.id + " not found",
+ }
+ );
+ }
const attrs = this.normalizedRequestAttrs("data-source");
- return schema.dataSources.find(request.params.id).update(attrs);
+ return dataSource.update(attrs);
}
return new Response(
404,
@@ -438,7 +448,43 @@ export default function routes() {
function (schema, request) {
const instance = schema.instances.find(request.params.instanceId);
if (instance) {
- return schema.dataSources.find(request.params.id).destroy();
+ const dataSource = schema.dataSources.find(request.params.id);
+ if (!dataSource) {
+ return new Response(
+ 404,
+ {},
+ {
+ errors: "Data source " + request.params.id + " not found",
+ }
+ );
+ }
+ dataSource.destroy();
+ }
+ return new Response(
+ 404,
+ {},
+ { errors: "Instance " + request.params.instanceId + " not found" }
+ );
+ }
+ );
+
+ // Data Source Member
+ this.get(
+ "/instance/:instanceId/datasource/:id/member",
+ function (schema, request) {
+ const instance = schema.instances.find(request.params.instanceId);
+ if (instance) {
+ const dataSource = schema.dataSources.find(request.params.id);
+ if (dataSource) {
+ return schema.dataSourceMembers.where((dataSourceMember) => {
+ return dataSourceMember.dataSourceId == dataSource.id;
+ });
+ }
+ return new Response(
+ 404,
+ {},
+ { errors: "Data source " + request.params.id + " not found" }
+ );
}
return new Response(
404,
diff --git a/frontend/src/store/modules/dataSource.ts b/frontend/src/store/modules/dataSource.ts
index 268c2985f0..810b74e57e 100644
--- a/frontend/src/store/modules/dataSource.ts
+++ b/frontend/src/store/modules/dataSource.ts
@@ -3,6 +3,7 @@ import {
DataSourceId,
DataSource,
DataSourceNew,
+ DataSourceMember,
DataSourceState,
DataSourceType,
InstanceId,
@@ -26,9 +27,28 @@ function convert(dataSource: ResourceObject, rootGetters: any): DataSource {
};
}
+function convertMember(
+ dataSourceMember: ResourceObject,
+ rootGetters: any
+): DataSourceMember {
+ const principal = rootGetters["principal/principalById"](
+ dataSourceMember.attributes.principalId
+ );
+
+ return {
+ id: dataSourceMember.id,
+ principal,
+ ...(dataSourceMember.attributes as Omit<
+ DataSourceMember,
+ "id" | "principal"
+ >),
+ };
+}
+
const state: () => DataSourceState = () => ({
dataSourceListByInstanceId: new Map(),
dataSourceById: new Map(),
+ memberListById: new Map(),
});
const getters = {
@@ -57,6 +77,12 @@ const getters = {
): DataSource | undefined => {
return state.dataSourceById.get(dataSourceId);
},
+
+ memberListById: (state: DataSourceState) => (
+ dataSourceId: DataSourceId
+ ): DataSourceMember[] => {
+ return state.memberListById.get(dataSourceId) || [];
+ },
};
const actions = {
@@ -182,6 +208,29 @@ const actions = {
commit("deleteDataSourceInListById", dataSourceId);
},
+
+ async fetchMemberListById(
+ { commit, rootGetters }: any,
+ {
+ instanceId,
+ dataSourceId,
+ }: { instanceId: InstanceId; dataSourceId: DataSourceId }
+ ) {
+ const dataSourceMemberList = (
+ await axios.get(
+ `/api/instance/${instanceId}/datasource/${dataSourceId}/member`
+ )
+ ).data.data.map((dataSourceMember: ResourceObject) => {
+ return convertMember(dataSourceMember, rootGetters);
+ });
+
+ commit("setDataSourceMemberListById", {
+ dataSourceId,
+ dataSourceMemberList,
+ });
+
+ return dataSourceMemberList;
+ },
};
const mutations = {
@@ -265,6 +314,19 @@ const mutations = {
}
}
},
+
+ setDataSourceMemberListById(
+ state: DataSourceState,
+ {
+ dataSourceId,
+ dataSourceMemberList,
+ }: {
+ dataSourceId: DataSourceId;
+ dataSourceMemberList: DataSourceMember[];
+ }
+ ) {
+ state.memberListById.set(dataSourceId, dataSourceMemberList);
+ },
};
export default {
diff --git a/frontend/src/types/bytebase.ts b/frontend/src/types/bytebase.ts
index d3f9482574..7780dd1d46 100644
--- a/frontend/src/types/bytebase.ts
+++ b/frontend/src/types/bytebase.ts
@@ -27,6 +27,8 @@ export type InstanceId = string;
export type DataSourceId = string;
+export type DataSourceMemberId = string;
+
export type DatabaseId = string;
export type CommandId = string;
@@ -246,6 +248,7 @@ export type DataSource = {
// If empty, it means it can access all databases from an instance.
database?: string;
};
+
export type DataSourceNew = {
name: string;
type: DataSourceType;
@@ -253,6 +256,13 @@ export type DataSourceNew = {
password?: string;
};
+export type DataSourceMember = {
+ id: DataSourceMemberId;
+ principal: Principal;
+ taskId?: TaskId;
+ createdTs: number;
+};
+
// We periodically sync the underlying db schema and stores those info
// in the "database" object.
@@ -364,6 +374,7 @@ export interface InstanceState {
export interface DataSourceState {
dataSourceListByInstanceId: Map<InstanceId, DataSource[]>;
dataSourceById: Map<DataSourceId, DataSource>;
+ memberListById: Map<DataSourceId, DataSourceMember[]>;
}
export interface DatabaseState {
diff --git a/frontend/src/views/DataSourceDetail.vue b/frontend/src/views/DataSourceDetail.vue
index f78da0fae2..887d487625 100644
--- a/frontend/src/views/DataSourceDetail.vue
+++ b/frontend/src/views/DataSourceDetail.vue
@@ -1,8 +1,175 @@
<template>
- <div>DataSource Detail {{ dataSourceSlug }}</div>
+ <!--
+ This example requires Tailwind CSS v2.0+
+
+ This example requires some changes to your config:
+
+ ```
+ // tailwind.config.js
+ const colors = require('tailwindcss/colors')
+
+ module.exports = {
+ // ...
+ theme: {
+ extend: {
+ colors: {
+ cyan: colors.cyan,
+ }
+ }
+ },
+ plugins: [
+ // ...
+ require('@tailwindcss/forms'),
+ ]
+ }
+ ```
+-->
+ <div class="bg-gray-100">
+ <div class="flex-1 overflow-auto focus:outline-none" tabindex="0">
+ <main class="flex-1 relative pb-8 z-0 overflow-y-auto">
+ <!-- Page header -->
+ <div class="bg-white shadow">
+ <div class="px-4 sm:px-6 lg:max-w-6xl lg:mx-auto lg:px-8">
+ <div class="py-6 md:flex md:items-center md:justify-between">
+ <div class="flex-1 min-w-0">
+ <!-- Profile -->
+ <div class="flex items-center">
+ <div>
+ <div class="flex items-center">
+ <h1
+ class="ml-3 text-2xl font-bold leading-7 text-gray-900 sm:leading-9 sm:truncate"
+ >
+ {{ dataSource.name }} - {{ dataSource.type }}
+ </h1>
+ </div>
+ <dl
+ class="mt-6 flex flex-col sm:ml-3 sm:mt-1 sm:flex-row sm:flex-wrap"
+ >
+ <dt class="sr-only">Instance</dt>
+ <dd
+ class="flex items-center text-sm text-gray-500 font-medium capitalize sm:mr-6"
+ >
+ <!-- Heroicon name: solid/database -->
+ <svg
+ class="mr-1.5 w-6 h-6 text-control"
+ fill="currentColor"
+ viewBox="0 0 20 20"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <path
+ d="M3 12v3c0 1.657 3.134 3 7 3s7-1.343 7-3v-3c0 1.657-3.134 3-7 3s-7-1.343-7-3z"
+ ></path>
+ <path
+ d="M3 7v3c0 1.657 3.134 3 7 3s7-1.343 7-3V7c0 1.657-3.134 3-7 3S3 8.657 3 7z"
+ ></path>
+ <path
+ d="M17 5c0 1.657-3.134 3-7 3S3 6.657 3 5s3.134-3 7-3 7 1.343 7 3z"
+ ></path>
+ </svg>
+ <router-link
+ :to="`/instance/${instanceSlug}`"
+ class="normal-link"
+ >
+ {{ instance.name }}
+ </router-link>
+ </dd>
+ <dt class="sr-only">Environment</dt>
+ <dd
+ class="mt-3 flex items-center text-sm text-gray-500 font-medium sm:mr-6 sm:mt-0 capitalize"
+ >
+ <!-- Heroicon name: solid/globle-alt -->
+ <svg
+ class="mr-1.5 w-6 h-6 text-control"
+ fill="currentColor"
+ viewBox="0 0 20 20"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <path
+ fill-rule="evenodd"
+ d="M4.083 9h1.946c.089-1.546.383-2.97.837-4.118A6.004 6.004 0 004.083 9zM10 2a8 8 0 100 16 8 8 0 000-16zm0 2c-.076 0-.232.032-.465.262-.238.234-.497.623-.737 1.182-.389.907-.673 2.142-.766 3.556h3.936c-.093-1.414-.377-2.649-.766-3.556-.24-.56-.5-.948-.737-1.182C10.232 4.032 10.076 4 10 4zm3.971 5c-.089-1.546-.383-2.97-.837-4.118A6.004 6.004 0 0115.917 9h-1.946zm-2.003 2H8.032c.093 1.414.377 2.649.766 3.556.24.56.5.948.737 1.182.233.23.389.262.465.262.076 0 .232-.032.465-.262.238-.234.498-.623.737-1.182.389-.907.673-2.142.766-3.556zm1.166 4.118c.454-1.147.748-2.572.837-4.118h1.946a6.004 6.004 0 01-2.783 4.118zm-6.268 0C6.412 13.97 6.118 12.546 6.03 11H4.083a6.004 6.004 0 002.783 4.118z"
+ clip-rule="evenodd"
+ ></path>
+ </svg>
+ <router-link to="/environment" class="normal-link">
+ {{ instance.environment.name }}
+ </router-link>
+ </dd>
+ </dl>
+ </div>
+ </div>
+ </div>
+ <div class="mt-6 flex space-x-3 md:mt-0 md:ml-4">
+ <button
+ type="button"
+ class="btn-normal"
+ @click.prevent="editUser"
+ >
+ <!-- Heroicon name: solid/pencil -->
+ <svg
+ class="-ml-1 mr-2 h-5 w-5 text-control-light"
+ fill="currentColor"
+ viewBox="0 0 20 20"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <path
+ d="M13.586 3.586a2 2 0 112.828 2.828l-.793.793-2.828-2.828.793-.793zM11.379 5.793L3 14.172V17h2.828l8.38-8.379-2.83-2.828z"
+ ></path>
+ </svg>
+ <span>Edit</span>
+ </button>
+ </div>
+ </div>
+ </div>
+ </div>
+
+ <div class="mt-8">
+ <div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8">
+ <!-- Description list -->
+ <div class="mt-6 max-w-5xl mx-auto px-4 sm:px-6 lg:px-8">
+ <dl class="grid grid-cols-1 gap-x-4 gap-y-8 sm:grid-cols-2">
+ <div class="sm:col-span-1">
+ <dt class="text-sm font-medium text-control-light">
+ Username
+ </dt>
+ <dd class="mt-1 text-sm text-main">
+ {{ dataSource.username }}
+ </dd>
+ </div>
+
+ <div class="sm:col-span-1">
+ <dt class="text-sm font-medium text-control-light">
+ Password
+ </dt>
+ <dd class="mt-1 text-sm text-main">
+ {{ dataSource.password }}
+ </dd>
+ </div>
+ </dl>
+ </div>
+ </div>
+
+ <h2
+ class="max-w-6xl mx-auto mt-8 px-4 text-lg leading-6 font-medium text-gray-900 sm:px-6 lg:px-8"
+ >
+ User list
+ </h2>
+
+ <DataSourceMemberTable
+ :instanceId="instance.id"
+ :dataSourceId="dataSource.id"
+ />
+ </div>
+ </main>
+ </div>
+ </div>
</template>
<script lang="ts">
+import { computed } from "vue";
+import { useStore } from "vuex";
+import DataSourceMemberTable from "../components/DataSourceMemberTable.vue";
+import { idFromSlug } from "../utils";
+
export default {
name: "DataSourceDetail",
props: {
@@ -15,6 +182,27 @@ export default {
type: String,
},
},
- setup(props, ctx) {},
+ components: { DataSourceMemberTable },
+ setup(props, ctx) {
+ const store = useStore();
+ const instanceId = idFromSlug(props.instanceSlug);
+ const dataSourceId = idFromSlug(props.dataSourceSlug);
+
+ const dataSource = computed(() => {
+ return store.getters["dataSource/dataSourceById"](
+ dataSourceId,
+ instanceId
+ );
+ });
+
+ const instance = computed(() => {
+ return store.getters["instance/instanceById"](instanceId);
+ });
+
+ return {
+ dataSource,
+ instance,
+ };
+ },
};
</script>