Skip to content

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

refactor: re-implement deployment store module with pinia (#1034)

Liu Jie2641c26f4ac
diff --git a/frontend/src/components/AlterSchemaPrepForm/ProjectTenantView.vue b/frontend/src/components/AlterSchemaPrepForm/ProjectTenantView.vue
index 4ba927432c..f93d1af899 100644
--- a/frontend/src/components/AlterSchemaPrepForm/ProjectTenantView.vue
+++ b/frontend/src/components/AlterSchemaPrepForm/ProjectTenantView.vue
@@ -88,13 +88,11 @@
/* eslint-disable vue/no-mutating-props */
import { computed, watchEffect, watch, ref } from "vue";
-import { useStore } from "vuex";
+
import {
Database,
DatabaseId,
- DeploymentConfig,
Environment,
- Label,
LabelKeyType,
Project,
UNKNOWN_ID,
@@ -104,7 +102,7 @@ import { groupBy } from "lodash-es";
import { DeployDatabaseTable } from "../TenantDatabaseTable";
import { parseDatabaseNameByTemplate } from "../../utils";
import { getPipelineFromDeploymentSchedule } from "../../utils";
-import { useLabelStore } from "@/store";
+import { useDeploymentStore, useLabelStore } from "@/store";
import { storeToRefs } from "pinia";
export type State = {
@@ -123,16 +121,13 @@ defineEmits<{
(event: "dismiss"): void;
}>();
-const store = useStore();
+const deploymentStore = useDeploymentStore();
const labelStore = useLabelStore();
const fetchData = () => {
labelStore.fetchLabelList();
if (props.project) {
- store.dispatch(
- "deployment/fetchDeploymentConfigByProjectId",
- props.project.id
- );
+ deploymentStore.fetchDeploymentConfigByProjectId(props.project.id);
}
};
@@ -143,9 +138,7 @@ const { labelList } = storeToRefs(labelStore);
const deployment = computed(() => {
if (props.project) {
- return store.getters["deployment/deploymentConfigByProjectId"](
- props.project.id
- ) as DeploymentConfig;
+ return deploymentStore.getDeploymentConfigByProjectId(props.project.id);
} else {
return undefined;
}
diff --git a/frontend/src/components/ProjectDeploymentConfigPanel.vue b/frontend/src/components/ProjectDeploymentConfigPanel.vue
index 18e0052887..d8a5dde758 100644
--- a/frontend/src/components/ProjectDeploymentConfigPanel.vue
+++ b/frontend/src/components/ProjectDeploymentConfigPanel.vue
@@ -143,7 +143,7 @@ import { cloneDeep, isEqual } from "lodash-es";
import { useI18n } from "vue-i18n";
import { NPopover, useDialog } from "naive-ui";
import { generateDefaultSchedule, validateDeploymentConfig } from "../utils";
-import { useLabelStore } from "@/store";
+import { useDeploymentStore, useLabelStore } from "@/store";
import { storeToRefs } from "pinia";
type LocalState = {
@@ -168,6 +168,7 @@ export default defineComponent({
},
setup(props) {
const store = useStore();
+ const deploymentStore = useDeploymentStore();
const labelStore = useLabelStore();
const { t } = useI18n();
const dialog = useDialog();
@@ -222,10 +223,9 @@ export default defineComponent({
};
watchEffect(async () => {
- const dep = (await store.dispatch(
- "deployment/fetchDeploymentConfigByProjectId",
+ const dep = await deploymentStore.fetchDeploymentConfigByProjectId(
props.project.id
- )) as DeploymentConfig;
+ );
if (dep.id === UNKNOWN_ID) {
// if the project has no related deployment-config
@@ -301,7 +301,7 @@ export default defineComponent({
const deploymentConfigPatch: DeploymentConfigPatch = {
payload: JSON.stringify(state.deployment.schedule),
};
- await store.dispatch("deployment/patchDeploymentConfigByProjectId", {
+ await deploymentStore.patchDeploymentConfigByProjectId({
projectId: props.project.id,
deploymentConfigPatch,
});
diff --git a/frontend/src/store/modules/deployment.ts b/frontend/src/store/modules/deployment.ts
deleted file mode 100644
index f7515ec815..0000000000
--- a/frontend/src/store/modules/deployment.ts
+++ /dev/null
@@ -1,141 +0,0 @@
-import axios from "axios";
-import {
- ResourceObject,
- ProjectId,
- DeploymentState,
- DeploymentConfig,
- EMPTY_ID,
- empty,
- unknown,
- UNKNOWN_ID,
- DeploymentConfigPatch,
-} from "../../types";
-import { getPrincipalFromIncludedList } from "./principal";
-
-function convert(
- deployment: ResourceObject,
- includedList: ResourceObject[],
- rootGetters: any
-): DeploymentConfig {
- if (parseInt(deployment.id, 10) === EMPTY_ID) {
- return empty("DEPLOYMENT_CONFIG") as DeploymentConfig;
- }
-
- let schedule: DeploymentConfig["schedule"] = (
- unknown("DEPLOYMENT_CONFIG") as DeploymentConfig
- ).schedule;
- try {
- schedule = JSON.parse(deployment.attributes.payload as string);
- schedule.deployments.forEach((deployment) => {
- deployment.spec.selector.matchExpressions.forEach((rule) => {
- if (!rule.values) {
- rule.values = []; // empty values polyfill
- }
- });
- });
- } catch {
- // nothing
- }
-
- return {
- ...(deployment.attributes as Omit<
- DeploymentConfig,
- "id" | "schedule" | "creator" | "updater"
- >),
- id: parseInt(deployment.id, 10),
- creator: getPrincipalFromIncludedList(
- deployment.relationships!.creator.data,
- includedList
- ),
- updater: getPrincipalFromIncludedList(
- deployment.relationships!.updater.data,
- includedList
- ),
- schedule,
- };
-}
-
-const state: () => DeploymentState = () => ({
- deploymentConfigByProjectId: new Map(),
-});
-
-const getters = {
- deploymentConfigByProjectId:
- (state: DeploymentState) =>
- (projectId: ProjectId): DeploymentConfig => {
- if (projectId == EMPTY_ID) {
- return empty("DEPLOYMENT_CONFIG") as DeploymentConfig;
- }
-
- return (
- state.deploymentConfigByProjectId.get(projectId) ||
- (unknown("DEPLOYMENT_CONFIG") as DeploymentConfig)
- );
- },
-};
-
-const actions = {
- async fetchDeploymentConfigByProjectId(
- { commit, getters, rootGetters }: any,
- projectId: ProjectId
- ) {
- const data = (await axios.get(`/api/project/${projectId}/deployment`)).data;
-
- const deploymentConfig = convert(data.data, data.included, rootGetters);
- const { id } = deploymentConfig;
- if (id !== EMPTY_ID && id !== UNKNOWN_ID) {
- commit("setDeploymentConfigByProjectId", { projectId, deploymentConfig });
- }
- return getters["deploymentConfigByProjectId"](projectId);
- },
-
- async patchDeploymentConfigByProjectId(
- { commit, rootGetters }: any,
- {
- projectId,
- deploymentConfigPatch,
- }: { projectId: ProjectId; deploymentConfigPatch: DeploymentConfigPatch }
- ) {
- const data = (
- await axios.patch(`/api/project/${projectId}/deployment`, {
- data: {
- type: "deploymentConfigPatch",
- attributes: deploymentConfigPatch,
- },
- })
- ).data;
- const updatedDeploymentConfig = convert(
- data.data,
- data.included,
- rootGetters
- );
- commit("setDeploymentConfigByProjectId", {
- projectId,
- deploymentConfig: updatedDeploymentConfig,
- });
- return updatedDeploymentConfig;
- },
-};
-
-const mutations = {
- setDeploymentConfigByProjectId(
- state: DeploymentState,
- {
- projectId,
- deploymentConfig,
- }: {
- projectId: ProjectId;
- deploymentConfig: DeploymentConfig;
- }
- ) {
- state.deploymentConfigByProjectId.set(projectId, deploymentConfig);
- },
-};
-
-export default {
- namespaced: true,
- state,
- getters,
- actions,
- mutations,
-};
diff --git a/frontend/src/store/pinia-modules/deployment.ts b/frontend/src/store/pinia-modules/deployment.ts
new file mode 100644
index 0000000000..866acdc24c
--- /dev/null
+++ b/frontend/src/store/pinia-modules/deployment.ts
@@ -0,0 +1,119 @@
+import axios from "axios";
+import { defineStore } from "pinia";
+import {
+ ResourceObject,
+ ProjectId,
+ DeploymentState,
+ DeploymentConfig,
+ EMPTY_ID,
+ empty,
+ unknown,
+ UNKNOWN_ID,
+ DeploymentConfigPatch,
+} from "@/types";
+import { getPrincipalFromIncludedList } from "../modules/principal";
+
+function convert(
+ deployment: ResourceObject,
+ includedList: ResourceObject[]
+): DeploymentConfig {
+ if (parseInt(deployment.id, 10) === EMPTY_ID) {
+ return empty("DEPLOYMENT_CONFIG") as DeploymentConfig;
+ }
+
+ let schedule: DeploymentConfig["schedule"] = (
+ unknown("DEPLOYMENT_CONFIG") as DeploymentConfig
+ ).schedule;
+ try {
+ schedule = JSON.parse(deployment.attributes.payload as string);
+ schedule.deployments.forEach((deployment) => {
+ deployment.spec.selector.matchExpressions.forEach((rule) => {
+ if (!rule.values) {
+ rule.values = []; // empty values polyfill
+ }
+ });
+ });
+ } catch {
+ // nothing
+ }
+
+ return {
+ ...(deployment.attributes as Omit<
+ DeploymentConfig,
+ "id" | "schedule" | "creator" | "updater"
+ >),
+ id: parseInt(deployment.id, 10),
+ creator: getPrincipalFromIncludedList(
+ deployment.relationships!.creator.data,
+ includedList
+ ),
+ updater: getPrincipalFromIncludedList(
+ deployment.relationships!.updater.data,
+ includedList
+ ),
+ schedule,
+ };
+}
+
+export const useDeploymentStore = defineStore("deployment", {
+ state: (): DeploymentState => ({
+ deploymentConfigByProjectId: new Map(),
+ }),
+ actions: {
+ getDeploymentConfigByProjectId(projectId: ProjectId): DeploymentConfig {
+ if (projectId == EMPTY_ID) {
+ return empty("DEPLOYMENT_CONFIG") as DeploymentConfig;
+ }
+
+ return (
+ this.deploymentConfigByProjectId.get(projectId) ||
+ (unknown("DEPLOYMENT_CONFIG") as DeploymentConfig)
+ );
+ },
+ setDeploymentConfigByProjectId({
+ projectId,
+ deploymentConfig,
+ }: {
+ projectId: ProjectId;
+ deploymentConfig: DeploymentConfig;
+ }) {
+ this.deploymentConfigByProjectId.set(projectId, deploymentConfig);
+ },
+ async fetchDeploymentConfigByProjectId(projectId: ProjectId) {
+ const data = (await axios.get(`/api/project/${projectId}/deployment`))
+ .data;
+
+ const deploymentConfig = convert(data.data, data.included);
+ const { id } = deploymentConfig;
+ if (id !== EMPTY_ID && id !== UNKNOWN_ID) {
+ this.setDeploymentConfigByProjectId({
+ projectId,
+ deploymentConfig,
+ });
+ }
+ return this.getDeploymentConfigByProjectId(projectId);
+ },
+ async patchDeploymentConfigByProjectId({
+ projectId,
+ deploymentConfigPatch,
+ }: {
+ projectId: ProjectId;
+ deploymentConfigPatch: DeploymentConfigPatch;
+ }) {
+ const data = (
+ await axios.patch(`/api/project/${projectId}/deployment`, {
+ data: {
+ type: "deploymentConfigPatch",
+ attributes: deploymentConfigPatch,
+ },
+ })
+ ).data;
+ const updatedDeploymentConfig = convert(data.data, data.included);
+ this.setDeploymentConfigByProjectId({
+ projectId,
+ deploymentConfig: updatedDeploymentConfig,
+ });
+ return updatedDeploymentConfig;
+ },
+ },
+});
diff --git a/frontend/src/store/pinia-modules/index.ts b/frontend/src/store/pinia-modules/index.ts
index 136dc4931e..6d5cdfe142 100644
--- a/frontend/src/store/pinia-modules/index.ts
+++ b/frontend/src/store/pinia-modules/index.ts
@@ -2,6 +2,7 @@ export * from "./actuator";
export * from "./bookmark";
export * from "./command";
export * from "./debug";
+export * from "./deployment";
export * from "./label";
export * from "./oauth";
export * from "./router";