Skip to content

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

refactor: migrate vcs store to pinia (#1065)

Yunwei Xiao7bd69d6b97f6
diff --git a/frontend/src/components/RepositoryVCSProviderPanel.vue b/frontend/src/components/RepositoryVCSProviderPanel.vue
index d929b74023..27b8c939eb 100644
--- a/frontend/src/components/RepositoryVCSProviderPanel.vue
+++ b/frontend/src/components/RepositoryVCSProviderPanel.vue
@@ -38,12 +38,11 @@ export default { name: "RepositoryVCSProviderPanel" };
</script>
<script setup lang="ts">
-import { useStore } from "vuex";
import { reactive, computed, watchEffect, onUnmounted, onMounted } from "vue";
import isEmpty from "lodash-es/isEmpty";
import { OAuthWindowEventPayload, openWindowForOAuth, VCS } from "../types";
import { isOwner } from "../utils";
-import { pushNotification, useCurrentUser } from "@/store";
+import { pushNotification, useCurrentUser, useVCSStore } from "@/store";
interface LocalState {
selectedVCS?: VCS;
@@ -55,13 +54,13 @@ const emit = defineEmits<{
(event: "set-code", payload: string): void;
}>();
-const store = useStore();
+const vcsStore = useVCSStore();
const state = reactive<LocalState>({});
const currentUser = useCurrentUser();
const prepareVCSList = () => {
- store.dispatch("vcs/fetchVCSList");
+ vcsStore.fetchVCSList();
};
watchEffect(prepareVCSList);
@@ -74,7 +73,7 @@ onUnmounted(() => {
});
const vcsList = computed(() => {
- return store.getters["vcs/vcsList"]();
+ return vcsStore.getVCSList();
});
const eventListener = (event: Event) => {
diff --git a/frontend/src/components/VCSSetupWizard.vue b/frontend/src/components/VCSSetupWizard.vue
index ac9d73549a..a9f510bdb5 100644
--- a/frontend/src/components/VCSSetupWizard.vue
+++ b/frontend/src/components/VCSSetupWizard.vue
@@ -34,7 +34,6 @@ import {
defineComponent,
} from "vue";
import { useRouter } from "vue-router";
-import { useStore } from "vuex";
import isEmpty from "lodash-es/isEmpty";
import { BBStepTabItem } from "../bbkit/types";
import VCSProviderBasicInfoPanel from "./VCSProviderBasicInfoPanel.vue";
@@ -51,7 +50,7 @@ import {
} from "../types";
import { isUrl } from "../utils";
import { useI18n } from "vue-i18n";
-import { pushNotification, useOAuthStore } from "@/store";
+import { pushNotification, useOAuthStore, useVCSStore } from "@/store";
const BASIC_INFO_STEP = 0;
const OAUTH_INFO_STEP = 1;
@@ -72,8 +71,8 @@ export default defineComponent({
},
setup() {
const { t } = useI18n();
- const store = useStore();
const router = useRouter();
+ const vcsStore = useVCSStore();
const stepList: BBStepTabItem[] = [
{ title: t("version-control.setting.add-git-provider.basic-info.self") },
@@ -205,7 +204,7 @@ export default defineComponent({
const vcsCreate: VCSCreate = {
...state.config,
};
- store.dispatch("vcs/createVCS", vcsCreate).then((vcs: VCS) => {
+ vcsStore.createVCS(vcsCreate).then((vcs: VCS) => {
allowChangeCallback();
router.push({
name: "setting.workspace.version-control",
diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts
index 3f8f228bd3..a383b2a8cf 100644
--- a/frontend/src/router/index.ts
+++ b/frontend/src/router/index.ts
@@ -26,7 +26,7 @@ import Signin from "../views/auth/Signin.vue";
import Signup from "../views/auth/Signup.vue";
import DashboardSidebar from "../views/DashboardSidebar.vue";
import Home from "../views/Home.vue";
-import { useTabStore, hasFeature } from "@/store";
+import { useTabStore, hasFeature, useVCSStore } from "@/store";
const HOME_MODULE = "workspace.home";
const AUTH_MODULE = "auth";
@@ -323,7 +323,7 @@ const routes: Array<RouteRecordRaw> = [
meta: {
title: (route: RouteLocationNormalized) => {
const slug = route.params.vcsSlug as string;
- return store.getters["vcs/vcsById"](idFromSlug(slug)).name;
+ return useVCSStore().getVCSById(idFromSlug(slug)).name;
},
},
component: () =>
@@ -1122,8 +1122,8 @@ router.beforeEach((to, from, next) => {
}
if (vcsSlug) {
- store
- .dispatch("vcs/fetchVCSById", idFromSlug(vcsSlug))
+ useVCSStore()
+ .fetchVCSById(idFromSlug(vcsSlug))
.then(() => {
next();
})
diff --git a/frontend/src/store/modules/repository.ts b/frontend/src/store/modules/repository.ts
index e29b476a81..128a3a582b 100644
--- a/frontend/src/store/modules/repository.ts
+++ b/frontend/src/store/modules/repository.ts
@@ -13,6 +13,7 @@ import {
VCSId,
} from "../../types";
import { getPrincipalFromIncludedList } from "../pinia";
+import { useVCSStore } from "@/store";
function convert(
repository: ResourceObject,
@@ -31,7 +32,7 @@ function convert(
for (const item of includedList || []) {
if (item.type == "vcs" && item.id == vcsId) {
- vcs = rootGetters["vcs/convert"](item);
+ vcs = useVCSStore().convert(item, includedList || []);
}
if (item.type == "project" && item.id == projectId) {
project = rootGetters["project/convert"](item, includedList);
diff --git a/frontend/src/store/modules/vcs.ts b/frontend/src/store/modules/vcs.ts
deleted file mode 100644
index 6aeccb679b..0000000000
--- a/frontend/src/store/modules/vcs.ts
+++ /dev/null
@@ -1,183 +0,0 @@
-import axios from "axios";
-import {
- VCSId,
- VCS,
- VCSCreate,
- VCSState,
- ResourceObject,
- unknown,
- VCSPatch,
- empty,
- EMPTY_ID,
-} from "../../types";
-import { getPrincipalFromIncludedList } from "../pinia";
-
-function convert(vcs: ResourceObject, includedList: ResourceObject[]): VCS {
- return {
- ...(vcs.attributes as Omit<VCS, "id" | "creator" | "updater">),
- id: parseInt(vcs.id),
- creator: getPrincipalFromIncludedList(
- vcs.relationships!.creator.data,
- includedList
- ),
- updater: getPrincipalFromIncludedList(
- vcs.relationships!.updater.data,
- includedList
- ),
- };
-}
-
-const state: () => VCSState = () => ({
- vcsById: new Map(),
- repositoryListByVCSId: new Map(),
-});
-
-const getters = {
- convert:
- (
- state: VCSState,
- getters: any,
- includedList: any,
- rootState: any,
- rootGetters: any
- ) =>
- (vcs: ResourceObject, includedList: ResourceObject[]): VCS => {
- return convert(vcs, includedList);
- },
-
- vcsList: (state: VCSState) => (): VCS[] => {
- const list = [];
- for (const [_, vcs] of state.vcsById) {
- list.push(vcs);
- }
- return list;
- },
-
- vcsById:
- (state: VCSState) =>
- (vcsId: VCSId): VCS => {
- if (vcsId == EMPTY_ID) {
- return empty("VCS") as VCS;
- }
-
- return state.vcsById.get(vcsId) || (unknown("VCS") as VCS);
- },
-};
-
-const actions = {
- async fetchVCSList({ commit }: any) {
- const path = "/api/vcs";
- const data = (await axios.get(path)).data;
- const vcsList = data.data
- .map((vcs: ResourceObject) => {
- return convert(vcs, data.included);
- })
- .sort((a: VCS, b: VCS) => {
- return b.createdTs - a.createdTs;
- });
-
- commit("setVCSList", vcsList);
-
- return vcsList;
- },
-
- async fetchVCSById({ commit }: any, vcsId: VCSId) {
- const data = (await axios.get(`/api/vcs/${vcsId}`)).data;
- const vcs = convert(data.data, data.included);
-
- commit("setVCSById", {
- vcsId,
- vcs,
- });
- return vcs;
- },
-
- async createVCS({ commit }: any, newVCS: VCSCreate) {
- const data = (
- await axios.post(`/api/vcs`, {
- data: {
- type: "VCSCreate",
- attributes: newVCS,
- },
- })
- ).data;
- const createdVCS = convert(data.data, data.included);
-
- commit("setVCSById", {
- vcsId: createdVCS.id,
- vcs: createdVCS,
- });
-
- return createdVCS;
- },
-
- async patchVCS(
- { commit }: any,
- {
- vcsId,
- vcsPatch,
- }: {
- vcsId: VCSId;
- vcsPatch: VCSPatch;
- }
- ) {
- const data = (
- await axios.patch(`/api/vcs/${vcsId}`, {
- data: {
- type: "VCSPatch",
- attributes: vcsPatch,
- },
- })
- ).data;
- const updatedVCS = convert(data.data, data.included);
-
- commit("setVCSById", {
- vcsId: updatedVCS.id,
- vcs: updatedVCS,
- });
-
- return updatedVCS;
- },
-
- async deleteVCSById(
- { commit }: { state: VCSState; commit: any },
- vcsId: VCSId
- ) {
- await axios.delete(`/api/vcs/${vcsId}`);
-
- commit("deleteVCSById", vcsId);
- },
-};
-
-const mutations = {
- setVCSList(state: VCSState, vcsList: VCS[]) {
- vcsList.forEach((vcs) => {
- state.vcsById.set(vcs.id, vcs);
- });
- },
-
- setVCSById(
- state: VCSState,
- {
- vcsId,
- vcs,
- }: {
- vcsId: VCSId;
- vcs: VCS;
- }
- ) {
- state.vcsById.set(vcsId, vcs);
- },
-
- deleteVCSById(state: VCSState, vcsId: VCSId) {
- state.vcsById.delete(vcsId);
- },
-};
-
-export default {
- namespaced: true,
- state,
- getters,
- actions,
- mutations,
-};
diff --git a/frontend/src/store/pinia-modules/index.ts b/frontend/src/store/pinia-modules/index.ts
index 5445a916f3..e84fdb827c 100644
--- a/frontend/src/store/pinia-modules/index.ts
+++ b/frontend/src/store/pinia-modules/index.ts
@@ -19,3 +19,4 @@ export * from "./setting";
export * from "./subscription";
export * from "./tab";
export * from "./uistate";
+export * from "./vcs";
diff --git a/frontend/src/store/pinia-modules/vcs.ts b/frontend/src/store/pinia-modules/vcs.ts
new file mode 100644
index 0000000000..e6064cf11d
--- /dev/null
+++ b/frontend/src/store/pinia-modules/vcs.ts
@@ -0,0 +1,139 @@
+import { defineStore } from "pinia";
+import axios from "axios";
+import {
+ VCSId,
+ VCS,
+ VCSCreate,
+ VCSState,
+ ResourceObject,
+ unknown,
+ VCSPatch,
+ empty,
+ EMPTY_ID,
+} from "../../types";
+import { getPrincipalFromIncludedList } from "./principal";
+
+function convert(vcs: ResourceObject, includedList: ResourceObject[]): VCS {
+ return {
+ ...(vcs.attributes as Omit<VCS, "id" | "creator" | "updater">),
+ id: parseInt(vcs.id),
+ creator: getPrincipalFromIncludedList(
+ vcs.relationships!.creator.data,
+ includedList
+ ),
+ updater: getPrincipalFromIncludedList(
+ vcs.relationships!.updater.data,
+ includedList
+ ),
+ };
+}
+
+export const useVCSStore = defineStore("vcs", {
+ state: (): VCSState => ({
+ vcsById: new Map(),
+ // repositoryListByVCSId: new Map(),
+ }),
+
+ actions: {
+ convert(vcs: ResourceObject, includedList: ResourceObject[]): VCS {
+ return convert(vcs, includedList);
+ },
+
+ getVCSList(): VCS[] {
+ const list = [];
+ for (const [_, vcs] of this.vcsById) {
+ list.push(vcs);
+ }
+ return list;
+ },
+
+ getVCSById(vcsId: VCSId): VCS {
+ if (vcsId == EMPTY_ID) {
+ return empty("VCS") as VCS;
+ }
+
+ return this.vcsById.get(vcsId) || (unknown("VCS") as VCS);
+ },
+
+ setVCSList(vcsList: VCS[]) {
+ vcsList.forEach((vcs) => {
+ this.vcsById.set(vcs.id, vcs);
+ });
+ },
+
+ setVCSById({ vcsId, vcs }: { vcsId: VCSId; vcs: VCS }) {
+ this.vcsById.set(vcsId, vcs);
+ },
+
+ async fetchVCSList() {
+ const path = "/api/vcs";
+ const data = (await axios.get(path)).data;
+ const vcsList = data.data
+ .map((vcs: ResourceObject) => {
+ return convert(vcs, data.included);
+ })
+ .sort((a: VCS, b: VCS) => {
+ return b.createdTs - a.createdTs;
+ });
+
+ this.setVCSList(vcsList);
+
+ return vcsList;
+ },
+
+ async fetchVCSById(vcsId: VCSId) {
+ const data = (await axios.get(`/api/vcs/${vcsId}`)).data;
+ const vcs = convert(data.data, data.included);
+
+ this.setVCSById({
+ vcsId,
+ vcs,
+ });
+ return vcs;
+ },
+
+ async createVCS(newVCS: VCSCreate) {
+ const data = (
+ await axios.post(`/api/vcs`, {
+ data: {
+ type: "VCSCreate",
+ attributes: newVCS,
+ },
+ })
+ ).data;
+ const createdVCS = convert(data.data, data.included);
+
+ this.setVCSById({
+ vcsId: createdVCS.id,
+ vcs: createdVCS,
+ });
+
+ return createdVCS;
+ },
+
+ async patchVCS({ vcsId, vcsPatch }: { vcsId: VCSId; vcsPatch: VCSPatch }) {
+ const data = (
+ await axios.patch(`/api/vcs/${vcsId}`, {
+ data: {
+ type: "VCSPatch",
+ attributes: vcsPatch,
+ },
+ })
+ ).data;
+ const updatedVCS = convert(data.data, data.included);
+
+ this.setVCSById({
+ vcsId: updatedVCS.id,
+ vcs: updatedVCS,
+ });
+
+ return updatedVCS;
+ },
+
+ async deleteVCSById(vcsId: VCSId) {
+ await axios.delete(`/api/vcs/${vcsId}`);
+
+ this.vcsById.delete(vcsId);
+ },
+ },
+});
diff --git a/frontend/src/views/SettingWorkspaceVCS.vue b/frontend/src/views/SettingWorkspaceVCS.vue
index c6fb8db85c..292554e3f2 100644
--- a/frontend/src/views/SettingWorkspaceVCS.vue
+++ b/frontend/src/views/SettingWorkspaceVCS.vue
@@ -41,8 +41,7 @@ import { reactive, computed, watchEffect, defineComponent } from "vue";
import { useRouter } from "vue-router";
import VCSCard from "../components/VCSCard.vue";
import VCSSetupWizard from "../components/VCSSetupWizard.vue";
-import { useStore } from "vuex";
-import { featureToRef } from "@/store";
+import { featureToRef, useVCSStore } from "@/store";
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface LocalState {}
@@ -54,18 +53,18 @@ export default defineComponent({
VCSSetupWizard,
},
setup() {
- const store = useStore();
+ const vcsStore = useVCSStore();
const router = useRouter();
const state = reactive<LocalState>({});
const prepareVCSList = () => {
- store.dispatch("vcs/fetchVCSList");
+ vcsStore.fetchVCSList();
};
watchEffect(prepareVCSList);
const vcsList = computed(() => {
- return store.getters["vcs/vcsList"]();
+ return vcsStore.getVCSList();
});
const addVCSProvider = () => {
diff --git a/frontend/src/views/SettingWorkspaceVCSDetail.vue b/frontend/src/views/SettingWorkspaceVCSDetail.vue
index 6dd79077ef..133033453a 100644
--- a/frontend/src/views/SettingWorkspaceVCSDetail.vue
+++ b/frontend/src/views/SettingWorkspaceVCSDetail.vue
@@ -157,7 +157,7 @@ import {
OAuthWindowEventPayload,
OAuthToken,
} from "../types";
-import { pushNotification, useOAuthStore } from "@/store";
+import { pushNotification, useOAuthStore, useVCSStore } from "@/store";
interface LocalState {
name: string;
@@ -177,10 +177,11 @@ export default defineComponent({
},
setup(props) {
const store = useStore();
+ const vcsStore = useVCSStore();
const router = useRouter();
const vcs = computed((): VCS => {
- return store.getters["vcs/vcsById"](idFromSlug(props.vcsSlug));
+ return vcsStore.getVCSById(idFromSlug(props.vcsSlug));
});
const state = reactive<LocalState>({
@@ -266,8 +267,8 @@ export default defineComponent({
if (!isEmpty(state.secret)) {
vcsPatch.secret = state.secret;
}
- store
- .dispatch("vcs/patchVCS", {
+ vcsStore
+ .patchVCS({
vcsId: vcs.value.id,
vcsPatch,
})
@@ -300,8 +301,8 @@ export default defineComponent({
const vcsPatch: VCSPatch = {
name: state.name,
};
- store
- .dispatch("vcs/patchVCS", {
+ vcsStore
+ .patchVCS({
vcsId: vcs.value.id,
vcsPatch,
})
@@ -323,7 +324,7 @@ export default defineComponent({
const deleteVCS = () => {
const name = vcs.value.name;
- store.dispatch("vcs/deleteVCSById", vcs.value.id).then(() => {
+ vcsStore.deleteVCSById(vcs.value.id).then(() => {
pushNotification({
module: "bytebase",
style: "SUCCESS",