Skip to content

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

Database list side panel and fetch database list by user

天舟a0fcee082336
diff --git a/frontend/src/components/DatabaseListSidePanel.vue b/frontend/src/components/DatabaseListSidePanel.vue
new file mode 100644
index 0000000000..7e2e1073b0
--- /dev/null
+++ b/frontend/src/components/DatabaseListSidePanel.vue
@@ -0,0 +1,76 @@
+<template>
+ <BBOutline
+ :id="'database'"
+ :title="'Databases'"
+ :itemList="databaseListByEnvironment"
+ :allowCollapse="false"
+ />
+</template>
+
+<script lang="ts">
+import { computed, watchEffect, ComputedRef } from "vue";
+import { useStore } from "vuex";
+import cloneDeep from "lodash-es/cloneDeep";
+
+import { Database, Environment, EnvironmentId } from "../types";
+import { BBOutlineItem } from "../bbkit/types";
+
+export default {
+ name: "DatabaseListSidePanel",
+ props: {},
+ setup(props, ctx) {
+ const store = useStore();
+
+ const currentUser = computed(() => store.getters["auth/currentUser"]());
+
+ const environmentList = computed(() => {
+ return cloneDeep(
+ store.getters["environment/environmentList"]()
+ ).reverse();
+ });
+
+ const prepareDatabaseList = () => {
+ store
+ .dispatch("database/fetchDatabaseListByUser", currentUser.value.id)
+ .catch((error) => {
+ console.log(error);
+ });
+ };
+
+ watchEffect(prepareDatabaseList);
+
+ const databaseListByEnvironment = computed(() => {
+ const databaseList = store.getters["database/databaseListByUserId"](
+ currentUser.value.id
+ );
+ const envToDbMap: Map<EnvironmentId, BBOutlineItem[]> = new Map();
+ for (const environment of environmentList.value) {
+ envToDbMap.set(environment.id, []);
+ }
+ for (const database of databaseList) {
+ const dbList = envToDbMap.get(database.instance.environment.id)!;
+ dbList.push({
+ id: database.id,
+ name: database.name,
+ link: "/",
+ });
+ }
+ return environmentList.value.map(
+ (environment: Environment): BBOutlineItem => {
+ return {
+ id: "env." + environment.id,
+ name: environment.name,
+ childList: envToDbMap.get(environment.id),
+ childCollapse: true,
+ };
+ }
+ );
+ });
+
+ return {
+ environmentList,
+ databaseListByEnvironment,
+ };
+ },
+};
+</script>
diff --git a/frontend/src/miragejs/routes/index.js b/frontend/src/miragejs/routes/index.js
index 9806408175..69fbfcc866 100644
--- a/frontend/src/miragejs/routes/index.js
+++ b/frontend/src/miragejs/routes/index.js
@@ -32,6 +32,27 @@ export default function routes() {
return schema.users.find(request.params.userId).update(attrs);
});
+ this.get("/user/:userId/database", function (schema, request) {
+ return schema.databases.where((database) => {
+ const dataSourceList = schema.dataSources.where({
+ workspaceId: WORKSPACE_ID,
+ databaseId: database.id,
+ });
+
+ for (const dataSource of dataSourceList.models) {
+ if (
+ schema.dataSourceMembers.findBy({
+ dataSourceId: dataSource.id,
+ principalId: request.params.userId,
+ })
+ ) {
+ return true;
+ }
+ }
+ return false;
+ });
+ });
+
// Auth
this.post("/auth/login", function (schema, request) {
const loginInfo = this.normalizedRequestAttrs("login-info");
diff --git a/frontend/src/store/modules/database.ts b/frontend/src/store/modules/database.ts
index c8f25595f5..400c09bb87 100644
--- a/frontend/src/store/modules/database.ts
+++ b/frontend/src/store/modules/database.ts
@@ -1,5 +1,6 @@
import axios from "axios";
import {
+ UserId,
Database,
DatabaseId,
InstanceId,
@@ -23,6 +24,7 @@ function convert(database: ResourceObject, rootGetters: any): Database {
const state: () => DatabaseState = () => ({
databaseListByInstanceId: new Map(),
+ databaseListByUserId: new Map(),
});
const getters = {
@@ -32,14 +34,24 @@ const getters = {
return state.databaseListByInstanceId.get(instanceId) || [];
},
+ databaseListByUserId: (state: DatabaseState) => (
+ userId: UserId
+ ): Database[] => {
+ return state.databaseListByUserId.get(userId) || [];
+ },
+
databaseById: (state: DatabaseState) => (
databaseId: DatabaseId,
- instanceId?: InstanceId
+ instanceId?: InstanceId,
+ userId?: UserId
): Database => {
let database = undefined;
if (instanceId) {
const list = state.databaseListByInstanceId.get(instanceId) || [];
database = list.find((item) => item.id == databaseId);
+ } else if (userId) {
+ const list = state.databaseListByUserId.get(userId) || [];
+ database = list.find((item) => item.id == databaseId);
} else {
for (let [_, list] of state.databaseListByInstanceId) {
database = list.find((item) => item.id == databaseId);
@@ -91,6 +103,18 @@ const actions = {
return databaseList;
},
+
+ async fetchDatabaseListByUser({ commit, rootGetters }: any, userId: UserId) {
+ const databaseList = (
+ await axios.get(`/api/user/${userId}/database`)
+ ).data.data.map((database: ResourceObject) => {
+ return convert(database, rootGetters);
+ });
+
+ commit("setDatabaseListByUserId", { userId, databaseList });
+
+ return databaseList;
+ },
};
const mutations = {
@@ -106,6 +130,19 @@ const mutations = {
) {
state.databaseListByInstanceId.set(instanceId, databaseList);
},
+
+ setDatabaseListByUserId(
+ state: DatabaseState,
+ {
+ userId,
+ databaseList,
+ }: {
+ userId: UserId;
+ databaseList: Database[];
+ }
+ ) {
+ state.databaseListByUserId.set(userId, databaseList);
+ },
};
export default {
diff --git a/frontend/src/types/bytebase.ts b/frontend/src/types/bytebase.ts
index 605538634a..ca87fdfa40 100644
--- a/frontend/src/types/bytebase.ts
+++ b/frontend/src/types/bytebase.ts
@@ -396,6 +396,11 @@ export interface DataSourceState {
export interface DatabaseState {
databaseListByInstanceId: Map<InstanceId, Database[]>;
+ // It would be quite expensive to find user's data source list
+ // if iterating through dataSourceListByInstanceId. so we create
+ // a separate map for quick access. Needs to be careful to not
+ // introduce inconsistent data source entries between these two maps.
+ databaseListByUserId: Map<UserId, Database[]>;
}
export interface NotificationState {
diff --git a/frontend/src/views/DashboardSidebar.vue b/frontend/src/views/DashboardSidebar.vue
index 8dcd110ed6..3a3afd675c 100644
--- a/frontend/src/views/DashboardSidebar.vue
+++ b/frontend/src/views/DashboardSidebar.vue
@@ -24,6 +24,7 @@
</div>
<div>
<BookmarkListSidePanel />
+ <DatabaseListSidePanel />
</div>
</nav>
</template>
@@ -31,6 +32,7 @@
<script lang="ts">
import { reactive } from "vue";
import BookmarkListSidePanel from "../components/BookmarkListSidePanel.vue";
+import DatabaseListSidePanel from "../components/DatabaseListSidePanel.vue";
interface LocalState {
hasUnreadMessage: boolean;
@@ -41,6 +43,7 @@ export default {
props: {},
components: {
BookmarkListSidePanel,
+ DatabaseListSidePanel,
},
setup(props, ctx) {
const state = reactive<LocalState>({