Skip to content

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

Introduce InstanceLayout and use ProvideInstanceContext to fetch all relevant info for the instance related routes

天舟4b7388fb017e
diff --git a/frontend/src/components/DataSourceTable.vue b/frontend/src/components/DataSourceTable.vue
index 1a4d967ca7..e54645d063 100644
--- a/frontend/src/components/DataSourceTable.vue
+++ b/frontend/src/components/DataSourceTable.vue
@@ -20,54 +20,36 @@
<template v-slot:header>
<BBTableHeaderCell
:leftPadding="4"
- class="w-4 table-cell"
+ class="w-24"
: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"
- />
- <BBTableHeaderCell
- class="w-4 table-cell"
- :title="columnList[6].title"
- />
+ <BBTableHeaderCell class="w-24" :title="columnList[1].title" />
+ <BBTableHeaderCell class="w-4" :title="columnList[2].title" />
+ <BBTableHeaderCell class="w-8" :title="columnList[3].title" />
+ <BBTableHeaderCell class="w-8" :title="columnList[4].title" />
+ <BBTableHeaderCell class="w-16" :title="columnList[5].title" />
+ <BBTableHeaderCell class="w-16" :title="columnList[6].title" />
</template>
<template v-slot:body="{ rowData: dataSource }">
- <BBTableCell :leftPadding="4" class="w-4 table-cell text-gray-500">
+ <BBTableCell :leftPadding="4" class="text-gray-500">
<span class="">{{ dataSource.name }}</span>
</BBTableCell>
- <BBTableCell class="w-24 table-cell">
- {{ dataSource.database }}
+ <BBTableCell>
+ {{ dataSource.database ? dataSource.database.name : "*" }}
</BBTableCell>
- <BBTableCell class="w-24 table-cell">
+ <BBTableCell>
{{ dataSource.type }}
</BBTableCell>
- <BBTableCell class="w-24 table-cell">
+ <BBTableCell>
{{ dataSource.username }}
</BBTableCell>
- <BBTableCell class="w-24 table-cell">
+ <BBTableCell>
{{ dataSource.password }}
</BBTableCell>
- <BBTableCell class="w-24 table-cell">
+ <BBTableCell>
{{ humanizeTs(dataSource.lastUpdatedTs) }}
</BBTableCell>
- <BBTableCell class="w-24 table-cell">
+ <BBTableCell>
{{ humanizeTs(dataSource.createdTs) }}
</BBTableCell>
</template>
@@ -76,13 +58,13 @@
</template>
<script lang="ts">
-import { computed, reactive, watchEffect, PropType } from "vue";
+import { computed, reactive, PropType } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
-import { BBTableColumn, BBTableSectionDataSource } from "../bbkit/types";
+import { BBTableColumn } from "../bbkit/types";
-import { idFromSlug, dataSourceSlug, instanceSlug } from "../utils";
-import { Instance, DataSource } from "../types";
+import { dataSourceSlug, instanceSlug } from "../utils";
+import { Instance } from "../types";
const columnList: BBTableColumn[] = [
{
@@ -129,19 +111,6 @@ export default {
searchText: "",
});
- const prepareDataSourceList = () => {
- store
- .dispatch(
- "dataSource/fetchDataSourceListByInstanceId",
- props.instance.id
- )
- .catch((error) => {
- console.log(error);
- });
- };
-
- watchEffect(prepareDataSourceList);
-
const dataSourceSectionList = computed(() => {
const adminList = [];
const readWriteList = [];
diff --git a/frontend/src/components/ProvideInstanceContext.vue b/frontend/src/components/ProvideInstanceContext.vue
new file mode 100644
index 0000000000..3bf3d1ac01
--- /dev/null
+++ b/frontend/src/components/ProvideInstanceContext.vue
@@ -0,0 +1,31 @@
+<template>
+ <slot />
+</template>
+
+<script lang="ts">
+import { useStore } from "vuex";
+import { idFromSlug } from "../utils";
+
+export default {
+ name: "ProvideInstanceContext",
+ props: {
+ instanceSlug: {
+ required: true,
+ type: String,
+ },
+ },
+ async setup(props) {
+ const store = useStore();
+
+ await store.dispatch(
+ "database/fetchDatabaseListByInstanceId",
+ idFromSlug(props.instanceSlug)
+ );
+ // This depends on database/fetchDatabaseListByInstanceId to convert its database id to database object.
+ await store.dispatch(
+ "dataSource/fetchDataSourceListByInstanceId",
+ idFromSlug(props.instanceSlug)
+ );
+ },
+};
+</script>
diff --git a/frontend/src/layouts/InstanceLayout.vue b/frontend/src/layouts/InstanceLayout.vue
new file mode 100644
index 0000000000..67f418a2cc
--- /dev/null
+++ b/frontend/src/layouts/InstanceLayout.vue
@@ -0,0 +1,38 @@
+<template>
+ <!-- Suspense is experimental, be aware of the potential change -->
+ <Suspense>
+ <template #default>
+ <ProvideInstanceContext :instanceSlug="instanceSlug">
+ <!-- This if-lese looks weired because the router-view can be both-->
+ <router-view
+ v-if="dataSourceSlug"
+ :instanceSlug="instanceSlug"
+ :dataSourceSlug="dataSourceSlug"
+ />
+ <router-view v-else :instanceSlug="instanceSlug" />
+ </ProvideInstanceContext>
+ </template>
+ <template #fallback>
+ <span>Loading instance...</span>
+ </template>
+ </Suspense>
+</template>
+
+<script lang="ts">
+import ProvideInstanceContext from "../components/ProvideInstanceContext.vue";
+
+export default {
+ name: "InstanceLayout",
+ components: { ProvideInstanceContext },
+ props: {
+ instanceSlug: {
+ required: true,
+ type: String,
+ },
+ dataSourceSlug: {
+ type: String,
+ },
+ },
+ setup(props, ctx) {},
+};
+</script>
diff --git a/frontend/src/miragejs/models/index.js b/frontend/src/miragejs/models/index.js
index 32250ea7c6..58aff84fdb 100644
--- a/frontend/src/miragejs/models/index.js
+++ b/frontend/src/miragejs/models/index.js
@@ -54,11 +54,12 @@ export default {
dataSource: Model.extend({
instance: belongsTo(),
- database: hasMany(),
+ database: belongsTo(),
}),
database: Model.extend({
instance: belongsTo(),
+ database: hasMany(),
}),
batchUpdate: Model,
diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts
index 1e3669c2dc..f23ca636e0 100644
--- a/frontend/src/router/index.ts
+++ b/frontend/src/router/index.ts
@@ -8,6 +8,7 @@ import {
import SplashLayout from "../layouts/SplashLayout.vue";
import DashboardLayout from "../layouts/DashboardLayout.vue";
import BodyLayout from "../layouts/BodyLayout.vue";
+import InstanceLayout from "../layouts/InstanceLayout.vue";
import DashboardSidebar from "../views/DashboardSidebar.vue";
import Home from "../views/Home.vue";
import Signin from "../views/auth/Signin.vue";
@@ -246,43 +247,48 @@ const routes: Array<RouteRecordRaw> = [
{
path: "instance/:instanceSlug",
name: "workspace.instance.detail",
- meta: {
- title: (route: RouteLocationNormalized) => {
- const slug = route.params.instanceSlug as string;
- if (slug.toLowerCase() == "new") {
- return "New";
- }
- return store.getters["instance/instanceById"](idFromSlug(slug))
- .name;
- },
- allowBookmark: true,
- },
components: {
- content: () => import("../views/InstanceDetail.vue"),
+ content: InstanceLayout,
leftSidebar: DashboardSidebar,
},
props: { content: true },
- },
- {
- path: "instance/:instanceSlug/ds/:dataSourceSlug",
- name: "workspace.datasource.detail",
- meta: {
- title: (route: RouteLocationNormalized) => {
- const slug = route.params.dataSourceSlug as string;
- if (slug.toLowerCase() == "new") {
- return "New";
- }
- return store.getters["dataSource/dataSourceById"](
- idFromSlug(slug)
- ).name;
+ children: [
+ {
+ path: "",
+ name: "workspace.instance.detail",
+ meta: {
+ title: (route: RouteLocationNormalized) => {
+ const slug = route.params.instanceSlug as string;
+ if (slug.toLowerCase() == "new") {
+ return "New";
+ }
+ return store.getters["instance/instanceById"](
+ idFromSlug(slug)
+ ).name;
+ },
+ },
+ component: () => import("../views/InstanceDetail.vue"),
+ props: true,
},
- allowBookmark: true,
- },
- components: {
- content: () => import("../views/DataSourceDetail.vue"),
- leftSidebar: DashboardSidebar,
- },
- props: { content: true },
+ {
+ path: "ds/:dataSourceSlug",
+ name: "workspace.instance.datasource.detail",
+ meta: {
+ title: (route: RouteLocationNormalized) => {
+ const slug = route.params.dataSourceSlug as string;
+ if (slug.toLowerCase() == "new") {
+ return "New";
+ }
+ return store.getters["dataSource/dataSourceById"](
+ idFromSlug(slug)
+ ).name;
+ },
+ allowBookmark: true,
+ },
+ component: () => import("../views/DataSourceDetail.vue"),
+ props: true,
+ },
+ ],
},
{
path: "task/:taskSlug",
diff --git a/frontend/src/store/modules/dataSource.ts b/frontend/src/store/modules/dataSource.ts
index 59b673295c..a7b0d105c2 100644
--- a/frontend/src/store/modules/dataSource.ts
+++ b/frontend/src/store/modules/dataSource.ts
@@ -7,12 +7,22 @@ import {
DataSourceType,
InstanceId,
ResourceObject,
+ ResourceIdentifier,
} from "../../types";
-function convert(dataSource: ResourceObject): DataSource {
+function convert(dataSource: ResourceObject, rootGetters: any): DataSource {
+ const databaseId = dataSource.relationships!.database.data
+ ? (dataSource.relationships!.database.data as ResourceIdentifier).id
+ : undefined;
+ const instanceId = (dataSource.relationships!.instance
+ .data as ResourceIdentifier).id;
+
return {
id: dataSource.id,
- ...(dataSource.attributes as Omit<DataSource, "id">),
+ database: databaseId
+ ? rootGetters["database/databaseById"](databaseId, instanceId)
+ : undefined,
+ ...(dataSource.attributes as Omit<DataSource, "id" | "database">),
};
}
@@ -51,13 +61,13 @@ const getters = {
const actions = {
async fetchDataSourceListByInstanceId(
- { commit }: any,
+ { commit, rootGetters }: any,
instanceId: InstanceId
) {
const dataSourceList = (
await axios.get(`/api/instance/${instanceId}/datasource`)
).data.data.map((datasource: ResourceObject) => {
- return convert(datasource);
+ return convert(datasource, rootGetters);
});
commit("setDataSourceListByInstanceId", { instanceId, dataSourceList });
@@ -66,7 +76,7 @@ const actions = {
},
async fetchDataSourceById(
- { commit }: any,
+ { commit, rootGetters }: any,
{
instanceId,
dataSourceId,
@@ -77,7 +87,8 @@ const actions = {
await axios.get(
`/api/instance/${instanceId}/datasource/${dataSourceId}`
)
- ).data.data
+ ).data.data,
+ rootGetters
);
commit("setDataSourceById", {
@@ -88,7 +99,7 @@ const actions = {
},
async createDataSource(
- { commit }: any,
+ { commit, rootGetters }: any,
{
instanceId,
newDataSource,
@@ -102,7 +113,8 @@ const actions = {
attributes: newDataSource,
},
})
- ).data.data
+ ).data.data,
+ rootGetters
);
commit("appendDataSourceByInstanceId", {
@@ -114,7 +126,7 @@ const actions = {
},
async patchDataSource(
- { commit }: any,
+ { commit, rootGetters }: any,
{
instanceId,
dataSource,
@@ -135,7 +147,8 @@ const actions = {
},
}
)
- ).data.data
+ ).data.data,
+ rootGetters
);
commit("setDataSourceById", {
diff --git a/frontend/src/store/modules/database.ts b/frontend/src/store/modules/database.ts
index 500c301b0f..dc2eaf1c65 100644
--- a/frontend/src/store/modules/database.ts
+++ b/frontend/src/store/modules/database.ts
@@ -1,11 +1,14 @@
import axios from "axios";
import {
Database,
+ DatabaseId,
InstanceId,
DatabaseState,
ResourceObject,
+ Instance,
} from "../../types";
import { isDevOrDemo, randomString } from "../../utils";
+import instance from "./instance";
function convert(database: ResourceObject, rootGetters: any): Database {
const instance = rootGetters["instance/instanceById"](
@@ -28,6 +31,41 @@ const getters = {
): Database[] => {
return state.databaseListByInstanceId.get(instanceId) || [];
},
+
+ databaseById: (state: DatabaseState) => (
+ databaseId: DatabaseId,
+ instanceId?: InstanceId
+ ): Database => {
+ let database = undefined;
+ if (instanceId) {
+ const list = state.databaseListByInstanceId.get(instanceId) || [];
+ database = list.find((item) => item.id == databaseId);
+ } else {
+ for (let [_, list] of state.databaseListByInstanceId) {
+ database = list.find((item) => item.id == databaseId);
+ if (database) {
+ break;
+ }
+ }
+ }
+ if (database) {
+ return database;
+ }
+ return {
+ id: "-1",
+ name: "<<Unknown database>>",
+ createdTs: 0,
+ lastUpdatedTs: 0,
+ syncStatus: "NOT_FOUND",
+ fingerprint: "",
+ instance: {
+ id: "-1",
+ name: "<<Unknown instance>>",
+ environmentId: "-1",
+ host: "",
+ },
+ };
+ },
};
const actions = {
diff --git a/frontend/src/types/bytebase.ts b/frontend/src/types/bytebase.ts
index ae976d569e..1cf21572f8 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 DatabaseId = string;
+
export type CommandId = string;
export type CommandRegisterId = string;
@@ -233,6 +235,7 @@ export type DataSourceType = "ADMIN" | "RW" | "RO";
// Data Source
export type DataSource = {
id: string;
+ instanceId: InstanceId;
name: string;
createdTs: number;
lastUpdatedTs: number;
@@ -261,7 +264,7 @@ export type DataSourceNew = {
export type DatabaseSyncStatus = "OK" | "MISMATCH" | "NOT_FOUND";
// Database
export type Database = {
- id: string;
+ id: DatabaseId;
name: string;
createdTs: number;
lastUpdatedTs: number;
diff --git a/frontend/src/views/DataSourceForm.vue b/frontend/src/views/DataSourceForm.vue
new file mode 100644
index 0000000000..3caa84cc2d
--- /dev/null
+++ b/frontend/src/views/DataSourceForm.vue
@@ -0,0 +1,75 @@
+<template>
+ <form
+ class="px-4 space-y-6 divide-y divide-gray-200"
+ @submit.prevent="$emit('submit', state.environment)"
+ >
+ <div class="pt-6 grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-6">
+ <div class="sm:col-span-2">
+ <label for="name" class="text-lg leading-6 font-medium text-control">
+ Instance Name <span class="text-red-600">*</span>
+ </label>
+ <input
+ required
+ id="name"
+ name="name"
+ type="text"
+ class="textfield mt-4 w-full"
+ :value="state.dataSource.name"
+ @input="updateDataSource('name', $event.target.value)"
+ />
+ </div>
+ </div>
+ <!-- Create button group -->
+ <div class="flex justify-end pt-5">
+ <button
+ type="button"
+ class="btn-normal py-2 px-4"
+ @click.prevent="$emit('cancel')"
+ >
+ Cancel
+ </button>
+ <button
+ type="submit"
+ class="btn-primary ml-3 inline-flex justify-center py-2 px-4"
+ >
+ Create
+ </button>
+ </div>
+ </form>
+</template>
+
+<script lang="ts">
+import { computed, reactive, PropType } from "vue";
+import cloneDeep from "lodash-es/cloneDeep";
+import isEqual from "lodash-es/isEqual";
+import { DataSourceNew } from "../types";
+
+interface LocalState {
+ dataSource: DataSourceNew;
+}
+
+export default {
+ name: "DataSourceForm",
+ emits: ["submit", "cancel", "delete"],
+ props: {},
+ setup(props, ctx) {
+ const state = reactive<LocalState>({
+ dataSource: {
+ name: "New Data Source",
+ type: "RW",
+ },
+ });
+
+ const updateDataSource = (field: string, value: string) => {
+ if (state.dataSource) {
+ (state.dataSource as any)[field] = value;
+ }
+ };
+
+ return {
+ state,
+ updateDataSource,
+ };
+ },
+};
+</script>
diff --git a/frontend/src/views/InstanceDetail.vue b/frontend/src/views/InstanceDetail.vue
index 00243d79bd..204da7725f 100644
--- a/frontend/src/views/InstanceDetail.vue
+++ b/frontend/src/views/InstanceDetail.vue
@@ -298,7 +298,7 @@ input[type="number"] {
</template>
<script lang="ts">
-import { computed, onMounted, onUnmounted, reactive, watchEffect } from "vue";
+import { computed, onMounted, onUnmounted, reactive } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import cloneDeep from "lodash-es/cloneDeep";
@@ -367,19 +367,6 @@ export default {
document.removeEventListener("keydown", escHandler);
});
- const prepareDatabaseList = () => {
- store
- .dispatch(
- "database/fetchDatabaseListByInstanceId",
- idFromSlug(props.instanceSlug)
- )
- .catch((error) => {
- console.log(error);
- });
- };
-
- watchEffect(prepareDatabaseList);
-
const assignInstance = (instance: Instance | InstanceNew) => {
if (!state.new) {
state.originalInstance = instance as Instance;
@@ -432,22 +419,12 @@ export default {
// because the operation is async, we need to have a init object to avoid
// adding v-if="state.adminDataSource" guard
assignAdminDataSource(cloneDeep(INIT_DATA_SOURCE));
- store
- .dispatch(
- "dataSource/fetchDataSourceListByInstanceId",
- idFromSlug(props.instanceSlug)
- )
- .then(() => {
- const dataSource = store.getters[
- "dataSource/adminDataSourceByInstanceId"
- ](idFromSlug(props.instanceSlug));
- if (dataSource) {
- assignAdminDataSource(dataSource);
- }
- })
- .catch((error) => {
- console.log(error);
- });
+ const dataSource = store.getters[
+ "dataSource/adminDataSourceByInstanceId"
+ ](idFromSlug(props.instanceSlug));
+ if (dataSource) {
+ assignAdminDataSource(dataSource);
+ }
}
const valueChanged = computed(() => {