Skip to content

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

Add dataSourceList to the database model.

天舟3f9c0963b61e
diff --git a/frontend/src/components/DatabaseListSidePanel.vue b/frontend/src/components/DatabaseListSidePanel.vue
index 839c526a0f..905c2f5c2d 100644
--- a/frontend/src/components/DatabaseListSidePanel.vue
+++ b/frontend/src/components/DatabaseListSidePanel.vue
@@ -8,17 +8,12 @@
</template>
<script lang="ts">
-import { computed, watchEffect, ComputedRef } from "vue";
+import { computed, watchEffect } from "vue";
import { useStore } from "vuex";
import cloneDeep from "lodash-es/cloneDeep";
-import {
- ALL_DATABASE_NAME,
- Database,
- Environment,
- EnvironmentId,
-} from "../types";
-import { instanceSlug, databaseSlug } from "../utils";
+import { ALL_DATABASE_NAME, Environment, EnvironmentId } from "../types";
+import { databaseSlug, allowDatabaseAccess } from "../utils";
import { BBOutlineItem } from "../bbkit/types";
export default {
@@ -58,7 +53,9 @@ export default {
const dbList = envToDbMap.get(database.instance.environment.id)!;
dbList.push({
id: database.id,
- name: database.name,
+ name: allowDatabaseAccess(database, currentUser.value, "RW")
+ ? database.name
+ : database.name + " (read)",
link: `/db/${databaseSlug(database)}`,
});
}
diff --git a/frontend/src/store/modules/dataSource.ts b/frontend/src/store/modules/dataSource.ts
index 9b04a34a53..0db473e638 100644
--- a/frontend/src/store/modules/dataSource.ts
+++ b/frontend/src/store/modules/dataSource.ts
@@ -71,6 +71,18 @@ const state: () => DataSourceState = () => ({
});
const getters = {
+ convert: (
+ state: DataSourceState,
+ getters: any,
+ rootState: any,
+ rootGetters: any
+ ) => (dataSource: ResourceObject): DataSource => {
+ // Pass includedList with [] here, otherwise, it may cause cyclic dependency
+ // e.g. Database calls this to convert its dataSourceList, while data source here
+ // also tries to convert its database.
+ return convert(dataSource, [], rootGetters);
+ },
+
dataSourceListByInstanceId: (state: DataSourceState) => (
instanceId: InstanceId
): DataSource[] => {
diff --git a/frontend/src/store/modules/database.ts b/frontend/src/store/modules/database.ts
index cb3486f8eb..3dbaecf2ca 100644
--- a/frontend/src/store/modules/database.ts
+++ b/frontend/src/store/modules/database.ts
@@ -12,6 +12,7 @@ import {
EnvironmentId,
PrincipalId,
unknown,
+ DataSource,
} from "../../types";
type IDParams = {
@@ -29,17 +30,28 @@ function convert(
.data as ResourceIdentifier).id;
let instance: Instance = unknown("INSTANCE") as Instance;
+ const dataSourceList: DataSource[] = [];
for (const item of includedList) {
if (item.type == "instance" && item.id == instanceId) {
instance = rootGetters["instance/convert"](item);
- break;
+ }
+ if (
+ item.type == "data-source" &&
+ (item.relationships!.database.data as ResourceIdentifier).id ==
+ database.id
+ ) {
+ dataSourceList.push(rootGetters["dataSource/convert"](item));
}
}
return {
id: database.id,
instance,
- ...(database.attributes as Omit<Database, "id" | "instance">),
+ dataSourceList,
+ ...(database.attributes as Omit<
+ Database,
+ "id" | "instance" | "dataSourceList"
+ >),
};
}
@@ -154,6 +166,7 @@ const getters = {
createdTs: ts,
lastUpdatedTs: ts,
},
+ dataSourceList: [],
};
},
};
@@ -164,7 +177,9 @@ const actions = {
instanceId: InstanceId
) {
const data = (
- await axios.get(`/api/instance/${instanceId}/database?include=instance`)
+ await axios.get(
+ `/api/instance/${instanceId}/database?include=instance,dataSource`
+ )
).data;
const databaseList = data.data.map((database: ResourceObject) => {
return convert(database, data.included, rootGetters);
@@ -177,7 +192,9 @@ const actions = {
async fetchDatabaseListByUser({ commit, rootGetters }: any, userId: UserId) {
const data = (
- await axios.get(`/api/user/${userId}/database?include=instance`)
+ await axios.get(
+ `/api/user/${userId}/database?include=instance,dataSource`
+ )
).data;
const databaseList = data.data.map((database: ResourceObject) => {
return convert(database, data.included, rootGetters);
@@ -194,7 +211,7 @@ const actions = {
) {
const data = (
await axios.get(
- `/api/database?environment=${environmentId}&include=instance`
+ `/api/database?environment=${environmentId}&include=instance,dataSource`
)
).data;
const databaseList = data.data.map((database: ResourceObject) => {
@@ -214,8 +231,8 @@ const actions = {
}: { databaseId: DatabaseId; instanceId?: InstanceId }
) {
const url = instanceId
- ? `/api/instance/${instanceId}/database/${databaseId}?include=instance`
- : `/api/database/${databaseId}?include=instance`;
+ ? `/api/instance/${instanceId}/database/${databaseId}?include=instance,dataSource`
+ : `/api/database/${databaseId}?include=instance,dataSource`;
const data = (await axios.get(url)).data;
const database = convert(data.data, data.included, rootGetters);
@@ -229,7 +246,7 @@ const actions = {
async createDatabase({ commit, rootGetters }: any, newDatabase: DatabaseNew) {
const data = (
- await axios.post(`/api/database?include=instance`, {
+ await axios.post(`/api/database?include=instance,dataSource`, {
data: {
type: "database",
attributes: newDatabase,
@@ -263,14 +280,17 @@ const actions = {
}
) {
const data = (
- await axios.patch(`/api/database/${databaseId}?include=instance`, {
- data: {
- type: "databasepatch",
- attributes: {
- ownerId,
+ await axios.patch(
+ `/api/database/${databaseId}?include=instance,dataSource`,
+ {
+ data: {
+ type: "databasepatch",
+ attributes: {
+ ownerId,
+ },
},
- },
- })
+ }
+ )
).data;
const updatedDatabase = convert(data.data, data.included, rootGetters);
diff --git a/frontend/src/types/bytebase.ts b/frontend/src/types/bytebase.ts
index 10b185d99f..c3aba88600 100644
--- a/frontend/src/types/bytebase.ts
+++ b/frontend/src/types/bytebase.ts
@@ -67,6 +67,7 @@ export const unknown = (
const UNKNOWN_DATABASE: Database = {
id: UNKNOWN_ID,
instance: UNKNOWN_INSTANCE,
+ dataSourceList: [],
createdTs: 0,
lastUpdatedTs: 0,
name: "<<Unknown database>>",
@@ -543,6 +544,7 @@ export type DatabaseSyncStatus = "OK" | "MISMATCH" | "NOT_FOUND";
export type Database = {
id: DatabaseId;
instance: Instance;
+ dataSourceList: DataSource[];
createdTs: number;
lastUpdatedTs: number;
name: string;
diff --git a/frontend/src/utils/database.ts b/frontend/src/utils/database.ts
new file mode 100644
index 0000000000..f0f9eadf8d
--- /dev/null
+++ b/frontend/src/utils/database.ts
@@ -0,0 +1,30 @@
+import { Store } from "vuex";
+import {
+ Database,
+ DataSourceMember,
+ DataSourceType,
+ Principal,
+} from "../types";
+
+export function allowDatabaseAccess(
+ database: Database,
+ principal: Principal,
+ type: DataSourceType
+): boolean {
+ if (principal.role == "DBA" || principal.role == "OWNER") {
+ return true;
+ }
+
+ for (const dataSource of database.dataSourceList) {
+ if (
+ // Returns true if the current user has equal or higher access
+ (dataSource.type == type || (dataSource.type == "RW" && type == "RO")) &&
+ dataSource.memberList.find((item: DataSourceMember) => {
+ return item.principal.id == principal.id;
+ })
+ ) {
+ return true;
+ }
+ }
+ return false;
+}
diff --git a/frontend/src/utils/index.ts b/frontend/src/utils/index.ts
index 7bfe16f844..f7be9ac9c8 100644
--- a/frontend/src/utils/index.ts
+++ b/frontend/src/utils/index.ts
@@ -1,4 +1,5 @@
export * from "./env";
+export * from "./database";
export * from "./link";
export * from "./slug";
export * from "./plan";