Skip to content

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

Use a new Instance type which is decoupled from the underlying API model

Tianzhou Chen0bce36afb701
diff --git a/frontend/src/components/InstanceTable.vue b/frontend/src/components/InstanceTable.vue
index bf4fae0d41..7a102a09da 100644
--- a/frontend/src/components/InstanceTable.vue
+++ b/frontend/src/components/InstanceTable.vue
@@ -10,25 +10,21 @@
<img class="h-10 w-auto" src="../assets/db-mysql.svg" />
</BBTableCell>
<BBTableCell :leftPadding="4" class="w-24">
- {{ environmentName(instance.attributes.environmentId) }}
+ {{ environmentName(instance.environmentId) }}
</BBTableCell>
<BBTableCell class="w-48">
- {{ instance.attributes.name }}
+ {{ instance.name }}
</BBTableCell>
<BBTableCell class="w-64">
- <template v-if="instance.attributes.port"
- >{{ instance.attributes.host }}:{{
- instance.attributes.port
- }}</template
- ><template v-else>{{ instance.attributes.host }}</template>
+ <template v-if="instance.port"
+ >{{ instance.host }}:{{ instance.port }}</template
+ ><template v-else>{{ instance.host }}</template>
</BBTableCell>
<BBTableCell class="w-4">
<button
- v-if="instance.attributes.externalLink?.trim().length != 0"
+ v-if="instance.externalLink?.trim().length != 0"
class="btn-icon"
- @click.stop="
- window.open(urlfy(instance.attributes.externalLink), '_blank')
- "
+ @click.stop="window.open(urlfy(instance.externalLink), '_blank')"
>
<svg
class="w-5 h-5"
@@ -47,7 +43,7 @@
</button>
</BBTableCell>
<BBTableCell class="w-8">
- {{ humanizeTs(instance.attributes.createdTs) }}
+ {{ humanizeTs(instance.createdTs) }}
</BBTableCell>
</template>
</BBTable>
@@ -108,8 +104,8 @@ export default {
const instance = props.instanceList[row];
router.push(
`/instance/${instanceSlug(
- environmentName(instance.attributes.environmentId),
- instance.attributes.name,
+ environmentName(instance.environmentId),
+ instance.name,
instance.id
)}`
);
diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts
index ca690d6191..78f3f1233a 100644
--- a/frontend/src/router/index.ts
+++ b/frontend/src/router/index.ts
@@ -209,7 +209,7 @@ const routes: Array<RouteRecordRaw> = [
return "New";
}
return store.getters["instance/instanceById"](idFromSlug(slug))
- .attributes.name;
+ .name;
},
},
components: {
diff --git a/frontend/src/store/modules/instance.ts b/frontend/src/store/modules/instance.ts
index 23f33c6154..725438562f 100644
--- a/frontend/src/store/modules/instance.ts
+++ b/frontend/src/store/modules/instance.ts
@@ -1,5 +1,22 @@
import axios from "axios";
-import { InstanceId, Instance, InstanceNew, InstanceState } from "../../types";
+import {
+ InstanceId,
+ Instance,
+ InstanceNew,
+ InstanceState,
+ ResourceObject,
+} from "../../types";
+
+function convert(instance: ResourceObject): Instance {
+ return {
+ id: instance.id,
+ name: instance.attributes.name as string,
+ environmentId: instance.attributes.environmentId as string,
+ externalLink: instance.attributes.externalLink as string,
+ host: instance.attributes.host as string,
+ port: instance.attributes.port as string,
+ };
+}
const state: () => InstanceState = () => ({
instanceList: [],
@@ -18,7 +35,11 @@ const getters = {
const actions = {
async fetchInstanceList({ commit }: any) {
- const instanceList = (await axios.get(`/api/instance`)).data.data;
+ const instanceList = (await axios.get(`/api/instance`)).data.data.map(
+ (instance: ResourceObject) => {
+ return convert(instance);
+ }
+ );
commit("setInstanceList", instanceList);
@@ -26,7 +47,9 @@ const actions = {
},
async fetchInstanceById({ commit }: any, instanceId: InstanceId) {
- const instance = (await axios.get(`/api/instance/${instanceId}`)).data.data;
+ const instance = convert(
+ (await axios.get(`/api/instance/${instanceId}`)).data.data
+ );
commit("setInstanceById", {
instanceId,
instance,
@@ -35,11 +58,18 @@ const actions = {
},
async createInstance({ commit }: any, newInstance: InstanceNew) {
- const createdInstance = (
- await axios.post(`/api/instance`, {
- data: newInstance,
- })
- ).data.data;
+ const createdInstance = convert(
+ (
+ await axios.post(`/api/instance`, {
+ data: {
+ type: "instance",
+ attributes: {
+ ...newInstance,
+ },
+ },
+ })
+ ).data.data
+ );
commit("appendInstance", createdInstance);
@@ -47,11 +77,19 @@ const actions = {
},
async patchInstance({ commit }: any, instance: Instance) {
- const updatedInstance = (
- await axios.patch(`/api/instance/${instance.id}`, {
- data: instance,
- })
- ).data.data;
+ const { id, ...attrs } = instance;
+ const updatedInstance = convert(
+ (
+ await axios.patch(`/api/instance/${instance.id}`, {
+ data: {
+ type: "instance",
+ attributes: {
+ ...attrs,
+ },
+ },
+ })
+ ).data.data
+ );
commit("replaceInstanceInList", updatedInstance);
diff --git a/frontend/src/types/bytebase.ts b/frontend/src/types/bytebase.ts
index 73c755e052..ca84c3b827 100644
--- a/frontend/src/types/bytebase.ts
+++ b/frontend/src/types/bytebase.ts
@@ -204,22 +204,22 @@ export type Environment = {
export type EnvironmentNew = Omit<Environment, "id">;
// Instance
-export type Instance = ResourceObject & {
- attributes: {
- name: string;
- environmentId: string;
- externalLink?: string;
- host: string;
- port?: string;
- };
+export type Instance = {
+ id: InstanceId;
+ name: string;
+ environmentId: string;
+ externalLink?: string;
+ host: string;
+ port?: string;
};
export type InstanceNew = Omit<Instance, "id">;
+export type DataSourceType = "ADMIN" | "NORMAL";
// Data Source
export type DataSource = {
id: string;
name: string;
- type: "ADMIN" | "NORMAL";
+ type: DataSourceType;
// In mysql, username can be empty which means anonymous user
username?: string;
password?: string;
diff --git a/frontend/src/views/InstanceDetail.vue b/frontend/src/views/InstanceDetail.vue
index ea1a33eb99..ce08c0866f 100644
--- a/frontend/src/views/InstanceDetail.vue
+++ b/frontend/src/views/InstanceDetail.vue
@@ -26,7 +26,7 @@ input[type="number"] {
name="name"
type="text"
class="textfield mt-1 w-full"
- :value="state.instance.attributes.name"
+ :value="state.instance.name"
@input="updateInstance('name', $event.target.value)"
/>
</div>
@@ -38,7 +38,7 @@ input[type="number"] {
<EnvironmentSelect
id="environment"
name="environment"
- :selectedId="state.instance.attributes.environmentId"
+ :selectedId="state.instance.environmentId"
@select-environment-id="
(environmentId) => {
updateInstance('environmentId', environmentId);
@@ -59,7 +59,7 @@ input[type="number"] {
name="host"
placeholder="e.g. 127.0.0.1 | localhost | /tmp/mysql.sock"
class="textfield mt-1 w-full"
- :value="state.instance.attributes.host"
+ :value="state.instance.host"
@input="updateInstance('host', $event.target.value)"
/>
</div>
@@ -74,7 +74,7 @@ input[type="number"] {
name="port"
placeholder="3306"
class="textfield mt-1 w-full"
- :value="state.instance.attributes.port"
+ :value="state.instance.port"
@input="updateInstance('port', $event.target.value)"
/>
</div>
@@ -85,14 +85,9 @@ input[type="number"] {
<span class="">External Link</span>
<button
class="ml-1 btn-icon"
- :disabled="
- state.instance.attributes.externalLink?.trim().length == 0
- "
+ :disabled="state.instance.externalLink?.trim().length == 0"
@click.prevent="
- window.open(
- urlfy(state.instance.attributes.externalLink),
- '_blank'
- )
+ window.open(urlfy(state.instance.externalLink), '_blank')
"
>
<svg
@@ -117,7 +112,7 @@ input[type="number"] {
name="externallink"
type="text"
class="textfield mt-1 w-full"
- :value="state.instance.attributes.externalLink"
+ :value="state.instance.externalLink"
@input="updateInstance('externalLink', $event.target.value)"
/>
</div>
@@ -271,7 +266,7 @@ input[type="number"] {
v-if="state.showDeleteModal"
:style="'CRITICAL'"
:okText="'Delete'"
- :title="'Delete instance \'' + state.instance.attributes.name + '\' ?'"
+ :title="'Delete instance \'' + state.instance.name + '\' ?'"
@ok="
() => {
state.showDeleteModal = false;
@@ -309,8 +304,9 @@ import EnvironmentSelect from "../components/EnvironmentSelect.vue";
import { Instance, InstanceNew, DataSource, DataSourceNew } from "../types";
const INIT_DATA_SOURCE: DataSourceNew = {
- type: "ADMIN",
name: "Admin Data Source",
+ type: "ADMIN",
+ username: "root",
};
interface LocalState {
@@ -382,7 +378,7 @@ export default {
};
const updateInstance = (field: string, value: string) => {
- state.instance!.attributes[field] = value;
+ (state.instance as any)[field] = value;
};
const updateDataSource = (field: string, value: string) => {
@@ -401,13 +397,9 @@ export default {
// IF block.
if (state.new) {
state.instance = {
- type: "instance",
- attributes: {
- name: "New Instance",
- environmentId: "",
- host: "127.0.0.1",
- username: "root",
- },
+ name: "New Instance",
+ environmentId: "",
+ host: "127.0.0.1",
};
state.adminDataSource = INIT_DATA_SOURCE;
} else {
@@ -473,7 +465,7 @@ export default {
store.dispatch("notification/pushNotification", {
module: "bytebase",
style: "SUCCESS",
- title: `Successfully created instance '${instance.attributes.name}'.`,
+ title: `Successfully created instance '${instance.name}'.`,
});
router.push({
name: "workspace.instance",
@@ -507,7 +499,7 @@ export default {
store.dispatch("notification/pushNotification", {
module: "bytebase",
style: "SUCCESS",
- title: `Successfully updated instance '${updatedInstance.attributes.name}'.`,
+ title: `Successfully updated instance '${updatedInstance.name}'.`,
});
router.push({
name: "workspace.instance",
@@ -536,7 +528,7 @@ export default {
module: "bytebase",
style: "SUCCESS",
title: `Successfully deleted instance '${
- state.originalInstance!.attributes.name
+ state.originalInstance!.name
}'.`,
});
router.push({