Skip to content

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

feat: create table group in UI (#6320)

boojack4f847eee05b0
diff --git a/frontend/src/components/DatabaseGroup/DatabaseGroupForm.vue b/frontend/src/components/DatabaseGroup/DatabaseGroupForm.vue
index 4ca877b531..9a731679f2 100644
--- a/frontend/src/components/DatabaseGroup/DatabaseGroupForm.vue
+++ b/frontend/src/components/DatabaseGroup/DatabaseGroupForm.vue
@@ -3,18 +3,11 @@
<div class="w-full grid grid-cols-3 gap-x-6 pb-6 mb-4 border-b">
<div>
<p class="text-lg mb-2">Name</p>
- <input
- v-model="state.databasePlaceholder"
- required
- type="text"
- class="textfield w-full"
- />
- <ResourceIdField
- ref="resourceIdField"
- resource-type="database-group"
- :readonly="!isCreating"
+ <ResourceIdInput
+ ref="resourceIdInput"
:value="state.resourceId"
- :resource-title="state.databasePlaceholder"
+ :resource-type="resourceIdType"
+ :readonly="!isCreating"
:validate="validateResourceId"
/>
</div>
@@ -29,7 +22,7 @@
"
/>
</div>
- <div>
+ <div v-if="resourceType === 'DATABASE_GROUP'">
<p class="text-lg mb-2">Project</p>
<input
required
@@ -40,6 +33,18 @@
class="textfield w-full"
/>
</div>
+ <div v-if="resourceType === 'SCHEMA_GROUP'">
+ <p class="text-lg mb-2">Database group</p>
+ <DatabaseGroupSelect
+ :project-id="project.name"
+ :selected-id="state.selectedDatabaseGroupId"
+ @select-database-group-id="
+ (id) => {
+ state.selectedDatabaseGroupId = id;
+ }
+ "
+ />
+ </div>
</div>
<div class="w-full grid grid-cols-5 gap-x-6">
<div class="col-span-3">
@@ -47,12 +52,19 @@
<ExprEditor
:expr="state.expr"
:allow-admin="true"
- :allow-high-level-factors="false"
+ :resource-type="resourceType"
/>
</div>
<div class="col-span-2">
- <p class="text-lg mb-2">Database</p>
+ <p class="text-lg mb-2">{{ matchedSectionTitle }}</p>
<MatchedDatabaseView
+ v-if="resourceType === 'DATABASE_GROUP'"
+ :project="project"
+ :environment-id="state.environmentId || ''"
+ :expr="state.expr"
+ />
+ <MatchedTableView
+ v-if="resourceType === 'SCHEMA_GROUP'"
:project="project"
:environment-id="state.environmentId || ''"
:expr="state.expr"
@@ -63,34 +75,35 @@
</template>
<script lang="ts" setup>
+import { Status } from "nice-grpc-web";
import { computed, onMounted, reactive, ref } from "vue";
+import { useI18n } from "vue-i18n";
import { ConditionGroupExpr, resolveCELExpr, wrapAsGroup } from "@/plugins/cel";
-
import { Expr as CELExpr } from "@/types/proto/google/api/expr/v1alpha1/syntax";
import ExprEditor from "./common/ExprEditor";
import { ResourceType } from "./common/ExprEditor/context";
-import { DatabaseGroup } from "@/types/proto/v1/project_service";
+import { DatabaseGroup, SchemaGroup } from "@/types/proto/v1/project_service";
import { ComposedProject, ResourceId, ValidatedMessage } from "@/types";
-import EnvironmentSelect from "../EnvironmentSelect.vue";
-import MatchedDatabaseView from "./MatchedDatabaseView.vue";
import { convertDatabaseGroupExprFromCEL } from "@/utils/databaseGroup/cel";
import { useDBGroupStore, useEnvironmentV1Store } from "@/store";
-import ResourceIdField from "@/components/v2/Form/ResourceIdField.vue";
-import { useI18n } from "vue-i18n";
import { getErrorCode } from "@/utils/grpcweb";
-import { Status } from "nice-grpc-web";
+import EnvironmentSelect from "../EnvironmentSelect.vue";
+import MatchedDatabaseView from "./MatchedDatabaseView.vue";
+import MatchedTableView from "./MatchedTableView.vue";
+import ResourceIdInput from "../v2/Form/ResourceIdInput.vue";
+import DatabaseGroupSelect from "./DatabaseGroupSelect.vue";
const props = defineProps<{
project: ComposedProject;
- databaseGroup?: DatabaseGroup;
+ resourceType: ResourceType;
+ databaseGroup?: DatabaseGroup | SchemaGroup;
}>();
type LocalState = {
resourceId: string;
- databasePlaceholder: string;
environmentId?: string;
+ selectedDatabaseGroupId?: string;
expr: ConditionGroupExpr;
- resourceType: ResourceType;
};
const { t } = useI18n();
@@ -98,28 +111,38 @@ const environmentStore = useEnvironmentV1Store();
const dbGroupStore = useDBGroupStore();
const state = reactive<LocalState>({
resourceId: "",
- databasePlaceholder: props.databaseGroup?.databasePlaceholder ?? "",
expr: wrapAsGroup(resolveCELExpr(CELExpr.fromJSON({}))),
- resourceType: "DATABASE_GROUP",
});
-const resourceIdField = ref<InstanceType<typeof ResourceIdField>>();
+const resourceIdInput = ref<InstanceType<typeof ResourceIdInput>>();
const isCreating = computed(() => props.databaseGroup === undefined);
+const resourceIdType = computed(() =>
+ props.resourceType === "DATABASE_GROUP" ? "database-group" : "schema-group"
+);
+
+const matchedSectionTitle = computed(() =>
+ props.resourceType === "DATABASE_GROUP" ? "Database" : "Table"
+);
+
onMounted(async () => {
const databaseGroup = props.databaseGroup;
if (!databaseGroup) {
return;
}
- const convertResult = await convertDatabaseGroupExprFromCEL(
- databaseGroup.databaseExpr?.expression ?? ""
- );
+ let expression = "";
+ if (props.resourceType === "DATABASE_GROUP") {
+ expression =
+ (databaseGroup as DatabaseGroup).databaseExpr?.expression ?? "";
+ } else {
+ expression = (databaseGroup as SchemaGroup).tableExpr?.expression ?? "";
+ }
+ const convertResult = await convertDatabaseGroupExprFromCEL(expression);
const environment = environmentStore.getEnvironmentByName(
convertResult.environmentId
);
state.resourceId = databaseGroup.name.split("/").pop() || "";
- state.databasePlaceholder = databaseGroup.databasePlaceholder;
state.environmentId = environment?.uid;
state.expr = convertResult.conditionGroupExpr;
});
@@ -131,16 +154,31 @@ const validateResourceId = async (
return [];
}
- try {
- const dbGroup = await dbGroupStore.getOrFetchDBGroupById(
+ let request = undefined;
+ if (props.resourceType === "DATABASE_GROUP") {
+ request = dbGroupStore.getOrFetchDBGroupById(
`${props.project.name}/databaseGroups/${resourceId}`
);
- if (dbGroup) {
+ } else if (props.resourceType === "SCHEMA_GROUP") {
+ if (state.selectedDatabaseGroupId) {
+ request = dbGroupStore.getOrFetchSchemaGroupById(
+ `${state.selectedDatabaseGroupId}/schemaGroups/${resourceId}`
+ );
+ }
+ }
+
+ if (!request) {
+ return [];
+ }
+
+ try {
+ const data = await request;
+ if (data) {
return [
{
type: "error",
message: t("resource-id.validation.duplicated", {
- resource: t("resource.environment"),
+ resource: t(`resource.${resourceIdType.value}`),
}),
},
];
@@ -150,6 +188,7 @@ const validateResourceId = async (
throw error;
}
}
+
return [];
};
@@ -157,7 +196,7 @@ defineExpose({
getFormState: () => {
return {
...state,
- resourceId: resourceIdField.value?.resourceId || "",
+ resourceId: resourceIdInput.value?.resourceId || "",
};
},
});
diff --git a/frontend/src/components/DatabaseGroup/DatabaseGroupPanel.vue b/frontend/src/components/DatabaseGroup/DatabaseGroupPanel.vue
index 5219d4fd7f..5240902859 100644
--- a/frontend/src/components/DatabaseGroup/DatabaseGroupPanel.vue
+++ b/frontend/src/components/DatabaseGroup/DatabaseGroupPanel.vue
@@ -13,14 +13,22 @@
<DatabaseGroupForm
ref="formRef"
:project="project"
+ :resource-type="resourceType"
:database-group="props.databaseGroup"
/>
<template #footer>
- <div class="flex items-center justify-end gap-x-2">
- <NButton @click="$emit('close')">{{ $t("common.cancel") }}</NButton>
- <NButton type="primary" @click="doConfirm">
- {{ $t("common.confirm") }}
- </NButton>
+ <div class="w-full flex justify-between items-center">
+ <div>
+ <NButton type="error" @click="doDelete">{{
+ $t("common.delete")
+ }}</NButton>
+ </div>
+ <div class="flex flex-row justify-end items-center gap-x-2">
+ <NButton @click="$emit('close')">{{ $t("common.cancel") }}</NButton>
+ <NButton type="primary" @click="doConfirm">
+ {{ $t("common.confirm") }}
+ </NButton>
+ </div>
</div>
</template>
</NDrawerContent>
@@ -28,24 +36,29 @@
</template>
<script lang="ts" setup>
-import { NButton, NDrawer, NDrawerContent } from "naive-ui";
+import { NButton, NDrawer, NDrawerContent, useDialog } from "naive-ui";
import { computed, ref } from "vue";
+import { useI18n } from "vue-i18n";
import { ComposedProject } from "@/types";
-import { DatabaseGroup } from "@/types/proto/v1/project_service";
-import DatabaseGroupForm from "./DatabaseGroupForm.vue";
-import { useDBGroupStore, useEnvironmentV1Store } from "@/store";
-import { stringifyDatabaseGroupExpr } from "@/utils/databaseGroup/cel";
+import { DatabaseGroup, SchemaGroup } from "@/types/proto/v1/project_service";
import { Expr } from "@/types/proto/google/type/expr";
+import { stringifyDatabaseGroupExpr } from "@/utils/databaseGroup/cel";
+import { useDBGroupStore, useEnvironmentV1Store } from "@/store";
+import { ResourceType } from "./common/ExprEditor/context";
+import DatabaseGroupForm from "./DatabaseGroupForm.vue";
const props = defineProps<{
project: ComposedProject;
- databaseGroup?: DatabaseGroup;
+ resourceType: ResourceType;
+ databaseGroup?: DatabaseGroup | SchemaGroup;
}>();
const emit = defineEmits<{
(event: "close"): void;
}>();
+const { t } = useI18n();
+const dialog = useDialog();
const environmentStore = useEnvironmentV1Store();
const dbGroupStore = useDBGroupStore();
const formRef = ref<InstanceType<typeof DatabaseGroupForm>>();
@@ -53,51 +66,125 @@ const formRef = ref<InstanceType<typeof DatabaseGroupForm>>();
const isCreating = computed(() => props.databaseGroup === undefined);
const title = computed(() => {
- return isCreating.value ? "Create database group" : "Edit database group";
+ if (props.resourceType === "DATABASE_GROUP") {
+ return isCreating.value ? "Create database group" : "Edit database group";
+ } else if (props.resourceType === "SCHEMA_GROUP") {
+ return isCreating.value ? "Create table group" : "Edit table group";
+ } else {
+ throw new Error("Unknown resource type");
+ }
});
+const doDelete = () => {
+ dialog.error({
+ title: "Confirm to delete",
+ positiveText: t("common.confirm"),
+ negativeText: t("common.cancel"),
+ onPositiveClick: async () => {
+ if (props.resourceType === "DATABASE_GROUP") {
+ await dbGroupStore.deleteDatabaseGroup(
+ props.databaseGroup as DatabaseGroup
+ );
+ } else if (props.resourceType === "SCHEMA_GROUP") {
+ await dbGroupStore.deleteSchemaGroup(
+ props.databaseGroup as SchemaGroup
+ );
+ } else {
+ throw new Error("Unknown resource type");
+ }
+ emit("close");
+ },
+ });
+};
+
const doConfirm = async () => {
const formState = formRef.value?.getFormState();
if (!formState) {
return;
}
- if (isCreating.value) {
- const environment = environmentStore.getEnvironmentByUID(
- formState.environmentId || ""
- );
- const celString = stringifyDatabaseGroupExpr({
- environmentId: environment.name,
- conditionGroupExpr: formState.expr,
- });
- const resourceId = formState.resourceId;
- await dbGroupStore.createDatabaseGroup(
- props.project.name,
- {
- name: `${props.project.name}/databaseGroups/${resourceId}`,
- databasePlaceholder: formState.databasePlaceholder,
+ if (props.resourceType === "DATABASE_GROUP") {
+ if (isCreating.value) {
+ const environment = environmentStore.getEnvironmentByUID(
+ formState.environmentId || ""
+ );
+ const celString = stringifyDatabaseGroupExpr({
+ environmentId: environment.name,
+ conditionGroupExpr: formState.expr,
+ });
+ const resourceId = formState.resourceId;
+ await dbGroupStore.createDatabaseGroup(
+ props.project.name,
+ {
+ name: `${props.project.name}/databaseGroups/${resourceId}`,
+ databasePlaceholder: resourceId,
+ databaseExpr: Expr.fromJSON({
+ expression: celString,
+ }),
+ },
+ resourceId
+ );
+ } else {
+ const environment = environmentStore.getEnvironmentByUID(
+ formState.environmentId || ""
+ );
+ const celString = stringifyDatabaseGroupExpr({
+ environmentId: environment.name,
+ conditionGroupExpr: formState.expr,
+ });
+ await dbGroupStore.updateDatabaseGroup({
+ ...props.databaseGroup!,
+ databasePlaceholder: "",
databaseExpr: Expr.fromJSON({
expression: celString,
}),
- },
- resourceId
- );
- emit("close");
+ });
+ }
+ } else if (props.resourceType === "SCHEMA_GROUP") {
+ if (isCreating.value) {
+ if (!formState.selectedDatabaseGroupId) {
+ return;
+ }
+
+ const environment = environmentStore.getEnvironmentByUID(
+ formState.environmentId || ""
+ );
+ const celString = stringifyDatabaseGroupExpr({
+ environmentId: environment.name,
+ conditionGroupExpr: formState.expr,
+ });
+ const resourceId = formState.resourceId;
+ await dbGroupStore.createSchemaGroup(
+ formState.selectedDatabaseGroupId,
+ {
+ name: `${formState.selectedDatabaseGroupId}/schemaGroups/${resourceId}`,
+ tablePlaceholder: resourceId,
+ tableExpr: Expr.fromJSON({
+ expression: celString,
+ }),
+ },
+ resourceId
+ );
+ } else {
+ const environment = environmentStore.getEnvironmentByUID(
+ formState.environmentId || ""
+ );
+ const celString = stringifyDatabaseGroupExpr({
+ environmentId: environment.name,
+ conditionGroupExpr: formState.expr,
+ });
+ await dbGroupStore.updateSchemaGroup({
+ ...props.databaseGroup!,
+ tablePlaceholder: "",
+ tableExpr: Expr.fromJSON({
+ expression: celString,
+ }),
+ });
+ }
} else {
- const environment = environmentStore.getEnvironmentByUID(
- formState.environmentId || ""
- );
- const celString = stringifyDatabaseGroupExpr({
- environmentId: environment.name,
- conditionGroupExpr: formState.expr,
- });
- await dbGroupStore.updateDatabaseGroup({
- ...props.databaseGroup!,
- databasePlaceholder: formState.databasePlaceholder,
- databaseExpr: Expr.fromJSON({
- expression: celString,
- }),
- });
+ throw new Error("Unknown resource type");
}
+
+ emit("close");
};
</script>
diff --git a/frontend/src/components/DatabaseGroup/DatabaseGroupSelect.vue b/frontend/src/components/DatabaseGroup/DatabaseGroupSelect.vue
new file mode 100644
index 0000000000..6a653e063e
--- /dev/null
+++ b/frontend/src/components/DatabaseGroup/DatabaseGroupSelect.vue
@@ -0,0 +1,71 @@
+<template>
+ <BBSelect
+ :selected-item="state.selectedDatabaseGroup"
+ :item-list="dbGroupList"
+ :disabled="disabled"
+ :placeholder="'Select Database Group'"
+ :show-prefix-item="true"
+ @select-item="(item: DatabaseGroup) => $emit('select-database-group-id', item.name)"
+ >
+ <template #menuItem="{ item }">
+ {{ getDatabaseGroupTitle(item.name) }}
+ </template>
+ </BBSelect>
+</template>
+
+<script lang="ts" setup>
+import { computed, reactive, watch } from "vue";
+import { useDBGroupStore } from "@/store";
+import { DatabaseGroup } from "@/types/proto/v1/project_service";
+
+interface LocalState {
+ selectedDatabaseGroup?: DatabaseGroup;
+}
+
+const props = defineProps<{
+ selectedId?: string;
+ projectId: string;
+ disabled?: boolean;
+}>();
+
+const emit = defineEmits<{
+ (event: "select-database-group-id", name?: string): void;
+}>();
+
+const state = reactive<LocalState>({
+ selectedDatabaseGroup: undefined,
+});
+
+const dbGroupStore = useDBGroupStore();
+
+const dbGroupList = computed(() => {
+ return dbGroupStore.getDBGroupListByProjectName(props.projectId);
+});
+
+const getDatabaseGroupTitle = (databaseGroupName: string): string => {
+ return databaseGroupName.split("/").pop() || "";
+};
+
+const invalidateSelectionIfNeeded = () => {
+ if (
+ state.selectedDatabaseGroup &&
+ !dbGroupList.value.find(
+ (item: DatabaseGroup) => item.name == state.selectedDatabaseGroup?.name
+ )
+ ) {
+ state.selectedDatabaseGroup = undefined;
+ emit("select-database-group-id", undefined);
+ }
+};
+
+watch(
+ () => props.selectedId,
+ (selectedId) => {
+ invalidateSelectionIfNeeded();
+ state.selectedDatabaseGroup = dbGroupList.value.find(
+ (item) => item.name === selectedId
+ );
+ },
+ { immediate: true }
+);
+</script>
diff --git a/frontend/src/components/DatabaseGroup/DatabaseGroupTable.vue b/frontend/src/components/DatabaseGroup/DatabaseGroupTable.vue
index 76d94c89c9..ebbc675656 100644
--- a/frontend/src/components/DatabaseGroup/DatabaseGroupTable.vue
+++ b/frontend/src/components/DatabaseGroup/DatabaseGroupTable.vue
@@ -8,7 +8,7 @@
>
<template #item="{ item }: { item: FormatedDatabaseGroup }">
<div class="bb-grid-cell">
- {{ item.databasePlaceholder }}
+ {{ item.resourceId }}
</div>
<div class="bb-grid-cell">{{ item.environment }}</div>
<div class="bb-grid-cell gap-x-2">
@@ -29,7 +29,7 @@ import { computed, ref, watch } from "vue";
import { useI18n } from "vue-i18n";
interface FormatedDatabaseGroup {
- name: string;
+ resourceId: string;
databasePlaceholder: string;
environment: string;
databaseGroup: DatabaseGroup;
@@ -75,7 +75,7 @@ watch(
convertResult.environmentId
);
list.push({
- name: databaseGroup.name,
+ resourceId: databaseGroup.name.split("/").pop() || "",
databasePlaceholder: databaseGroup.databasePlaceholder,
environment: environment?.title || "",
databaseGroup,
diff --git a/frontend/src/components/DatabaseGroup/MatchedTableView.vue b/frontend/src/components/DatabaseGroup/MatchedTableView.vue
new file mode 100644
index 0000000000..410debc5a5
--- /dev/null
+++ b/frontend/src/components/DatabaseGroup/MatchedTableView.vue
@@ -0,0 +1,112 @@
+<template>
+ <div class="w-full border min-h-[20rem]">
+ <div
+ class="w-full flex flex-row justify-between items-center px-2 py-1 bg-gray-100 border-b"
+ >
+ <div>
+ <span>Matched table</span>
+ <span class="ml-1 text-gray-400">({{ matchedTableList.length }})</span>
+ </div>
+ <button @click="state.showMatchedTableList = !state.showMatchedTableList">
+ <heroicons-outline:chevron-right
+ v-if="!state.showMatchedTableList"
+ class="w-5 h-auto"
+ />
+ <heroicons-outline:chevron-down v-else class="w-5 h-auto" />
+ </button>
+ </div>
+ <div v-show="state.showMatchedTableList" class="w-full">
+ <div
+ v-for="database in matchedTableList"
+ :key="database.name"
+ class="w-full flex flex-row justify-between items-center px-2 py-1 gap-x-2"
+ >
+ <span>{{ database.databaseName }}</span>
+ <div class="flex flex-row justify-end items-center">
+ <InstanceV1EngineIcon :instance="database.instanceEntity" />
+ <span class="ml-1 text-sm text-gray-400">{{
+ database.instanceEntity.title
+ }}</span>
+ </div>
+ </div>
+ </div>
+ <div
+ class="w-full flex flex-row justify-between items-center px-2 py-1 bg-gray-100 border-b"
+ :class="[state.showMatchedTableList && 'border-t']"
+ >
+ <div>
+ <span>Unmatched table</span>
+ <span class="ml-1 text-gray-400"
+ >({{ unmatchedTableList.length }})</span
+ >
+ </div>
+ <button
+ @click="state.showUnmatchedTableList = !state.showUnmatchedTableList"
+ >
+ <heroicons-outline:chevron-right
+ v-if="!state.showUnmatchedTableList"
+ class="w-5 h-auto"
+ />
+ <heroicons-outline:chevron-down v-else class="w-5 h-auto" />
+ </button>
+ </div>
+ <div v-show="state.showUnmatchedTableList">
+ <div
+ v-for="database in matchedTableList"
+ :key="database.name"
+ class="w-full flex flex-row justify-between items-center"
+ >
+ <span>{{ database.databaseName }}</span>
+ <div class="flex flex-row justify-end items-center">
+ <InstanceV1EngineIcon :instance="database.instanceEntity" />
+ <span class="ml-1 text-sm text-gray-400">{{
+ database.instanceEntity.title
+ }}</span>
+ </div>
+ </div>
+ </div>
+ </div>
+</template>
+
+<script lang="ts" setup>
+import { computed, ref, watch, reactive } from "vue";
+import { ConditionGroupExpr } from "@/plugins/cel";
+import { useDatabaseV1Store } from "@/store";
+import { ComposedDatabase, ComposedProject } from "@/types";
+import { sortDatabaseV1List } from "@/utils";
+import { InstanceV1EngineIcon } from "../v2";
+
+interface LocalState {
+ showMatchedTableList: boolean;
+ showUnmatchedTableList: boolean;
+}
+
+const props = defineProps<{
+ project: ComposedProject;
+ environmentId: string;
+ expr: ConditionGroupExpr;
+}>();
+
+const state = reactive<LocalState>({
+ showMatchedTableList: false,
+ showUnmatchedTableList: false,
+});
+const matchedTableList = ref<ComposedDatabase[]>([]);
+const unmatchedTableList = ref<ComposedDatabase[]>([]);
+const databaseList = computed(() => {
+ const list = useDatabaseV1Store().databaseListByProject(props.project.name);
+ return sortDatabaseV1List(list);
+});
+
+watch(
+ () => [databaseList.value, props.expr],
+ async () => {
+ // TODO: fetch matched and unmatched table list with expr.
+ matchedTableList.value = databaseList.value;
+ unmatchedTableList.value = [];
+ },
+ {
+ immediate: true,
+ }
+);
+</script>
diff --git a/frontend/src/components/DatabaseGroup/ProjectDatabaseGroupPanel.vue b/frontend/src/components/DatabaseGroup/ProjectDatabaseGroupPanel.vue
index 7f4c0939a3..37c5bf5f83 100644
--- a/frontend/src/components/DatabaseGroup/ProjectDatabaseGroupPanel.vue
+++ b/frontend/src/components/DatabaseGroup/ProjectDatabaseGroupPanel.vue
@@ -3,10 +3,8 @@
<div class="w-full flex flex-row justify-between items-center">
<span>Database groups</span>
<div class="flex flex-row gap-x-2">
- <NButton>New table group</NButton>
- <NButton @click="state.showDatabaseGroupPanel = true"
- >New database group</NButton
- >
+ <NButton @click="handleCreateSchemaGroup">New table group</NButton>
+ <NButton @click="handleCreateDatabaseGroup">New database group</NButton>
</div>
</div>
<div class="mt-4">
@@ -20,6 +18,7 @@
<DatabaseGroupPanel
v-if="state.showDatabaseGroupPanel"
:project="project"
+ :resource-type="state.resourceType"
:database-group="state.editingDatabaseGroup"
@close="state.showDatabaseGroupPanel = false"
/>
@@ -33,9 +32,11 @@ import { ComposedProject } from "@/types";
import DatabaseGroupTable from "./DatabaseGroupTable.vue";
import DatabaseGroupPanel from "./DatabaseGroupPanel.vue";
import { DatabaseGroup } from "@/types/proto/v1/project_service";
+import { ResourceType } from "./common/ExprEditor/context";
interface LocalState {
showDatabaseGroupPanel: boolean;
+ resourceType: ResourceType;
editingDatabaseGroup?: DatabaseGroup;
}
@@ -46,6 +47,7 @@ const props = defineProps<{
const dbGroupStore = useDBGroupStore();
const state = reactive<LocalState>({
showDatabaseGroupPanel: false,
+ resourceType: "DATABASE_GROUP",
});
const databaseGroupList = computed(() => {
@@ -56,8 +58,20 @@ onMounted(async () => {
await dbGroupStore.getOrFetchDBGroupListByProjectName(props.project.name);
});
+const handleCreateDatabaseGroup = () => {
+ state.resourceType = "DATABASE_GROUP";
+ state.editingDatabaseGroup = undefined;
+ state.showDatabaseGroupPanel = true;
+};
+
const handleConfigureDatabaseGroup = (databaseGroup: DatabaseGroup) => {
state.editingDatabaseGroup = cloneDeep(databaseGroup);
state.showDatabaseGroupPanel = true;
};
+
+const handleCreateSchemaGroup = () => {
+ state.resourceType = "SCHEMA_GROUP";
+ state.editingDatabaseGroup = undefined;
+ state.showDatabaseGroupPanel = true;
+};
</script>
diff --git a/frontend/src/components/v2/Form/ResourceIdInput.vue b/frontend/src/components/v2/Form/ResourceIdInput.vue
new file mode 100644
index 0000000000..2c73a3ca68
--- /dev/null
+++ b/frontend/src/components/v2/Form/ResourceIdInput.vue
@@ -0,0 +1,142 @@
+<template>
+ <input
+ v-model="state.resourceId"
+ required
+ class="textfield w-full"
+ type="text"
+ :disabled="readonly"
+ :readonly="readonly"
+ />
+
+ <ul
+ v-if="state.validatedMessages.length > 0"
+ class="w-full my-2 space-y-2 list-disc list-outside pl-4"
+ >
+ <li
+ v-for="validateMessage in state.validatedMessages"
+ :key="validateMessage.message"
+ class="break-words w-full text-xs"
+ :class="[
+ validateMessage.type === 'warning' && 'text-yellow-600',
+ validateMessage.type === 'error' && 'text-red-600',
+ ]"
+ >
+ {{ validateMessage.message }}
+ </li>
+ </ul>
+</template>
+
+<script lang="ts" setup>
+import { computed, reactive, watch } from "vue";
+import { useI18n } from "vue-i18n";
+
+import type { ResourceId, ValidatedMessage } from "@/types";
+
+const resourceIdPattern = /^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$/;
+
+interface LocalState {
+ resourceId: string;
+ manualEdit: boolean;
+ validatedMessages: ValidatedMessage[];
+}
+
+type ResourceType = "database-group" | "schema-group";
+
+const props = withDefaults(
+ defineProps<{
+ value?: string;
+ resourceType: ResourceType;
+ readonly?: boolean;
+ validate?: (resourceId: ResourceId) => Promise<ValidatedMessage[]>;
+ }>(),
+ {
+ value: "",
+ readonly: false,
+ validate: () => Promise.resolve([]),
+ }
+);
+
+const emit = defineEmits<{
+ (event: "update:value", value: string): void;
+}>();
+
+const { t } = useI18n();
+const state = reactive<LocalState>({
+ resourceId: props.value,
+ manualEdit: false,
+ validatedMessages: [],
+});
+
+const resourceName = computed(() => {
+ return t(`resource.${props.resourceType}`);
+});
+
+const handleResourceIdChange = async (newValue: string) => {
+ if (props.readonly) {
+ return;
+ }
+
+ state.validatedMessages = [];
+
+ emit("update:value", newValue);
+
+ // if the resource id is empty, do not validate.
+ if (newValue === "" && !state.manualEdit) {
+ return;
+ }
+
+ // common validation for resource id (min length, max length, pattern).
+ if (state.resourceId.length < 1) {
+ state.validatedMessages.push({
+ type: "error",
+ message: t("resource-id.validation.minlength", {
+ resource: resourceName.value,
+ }),
+ });
+ } else if (state.resourceId.length > 64) {
+ state.validatedMessages.push({
+ type: "error",
+ message: t("resource-id.validation.overflow", {
+ resource: resourceName.value,
+ }),
+ });
+ } else if (!resourceIdPattern.test(state.resourceId)) {
+ state.validatedMessages.push({
+ type: "error",
+ message: t("resource-id.validation.pattern", {
+ resource: resourceName.value,
+ }),
+ });
+ }
+
+ // custom validation for resource id. (e.g. check if the resource id is already used)
+ if (props.validate) {
+ const messages = await props.validate(state.resourceId);
+ state.validatedMessages.push(...messages);
+ }
+};
+
+watch(
+ () => props.value,
+ (newValue) => {
+ state.resourceId = newValue;
+ }
+);
+
+watch(
+ () => state.resourceId,
+ (newValue) => {
+ handleResourceIdChange(newValue);
+ },
+ {
+ immediate: true,
+ }
+);
+
+defineExpose({
+ resourceId: computed(() => state.resourceId),
+ isValidated: computed(() => {
+ return state.validatedMessages.length === 0;
+ }),
+});
+</script>
diff --git a/frontend/src/components/v2/Form/index.ts b/frontend/src/components/v2/Form/index.ts
index 6f9c127d0b..0b424e4b5f 100644
--- a/frontend/src/components/v2/Form/index.ts
+++ b/frontend/src/components/v2/Form/index.ts
@@ -2,5 +2,12 @@ import SpinnerSwitch from "./SpinnerSwitch.vue";
import SpinnerButton from "./SpinnerButton.vue";
import SearchBox from "./SearchBox.vue";
import ResourceIdField from "./ResourceIdField.vue";
+import ResourceIdInput from "./ResourceIdInput.vue";
-export { SpinnerSwitch, SpinnerButton, SearchBox, ResourceIdField };
+export {
+ SpinnerSwitch,
+ SpinnerButton,
+ SearchBox,
+ ResourceIdField,
+ ResourceIdInput,
+};
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index a45b12f84c..cc34336e1c 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -2183,7 +2183,8 @@
"project": "Project",
"idp": "Identity Provider",
"role": "Role",
- "database-group": "Database Group"
+ "database-group": "Database Group",
+ "schema-group": "Table Group"
},
"resource-id": {
"self": "{resource} ID",
diff --git a/frontend/src/store/modules/dbGroup.ts b/frontend/src/store/modules/dbGroup.ts
index 590ed01183..f2167a7c7a 100644
--- a/frontend/src/store/modules/dbGroup.ts
+++ b/frontend/src/store/modules/dbGroup.ts
@@ -1,11 +1,12 @@
-import { projectServiceClient } from "@/grpcweb";
-import { DatabaseGroup } from "@/types/proto/v1/project_service";
import { isEqual } from "lodash-es";
import { defineStore } from "pinia";
import { ref } from "vue";
+import { projectServiceClient } from "@/grpcweb";
+import { DatabaseGroup, SchemaGroup } from "@/types/proto/v1/project_service";
export const useDBGroupStore = defineStore("db-group", () => {
const dbGroupMapById = ref<Map<string, DatabaseGroup>>(new Map());
+ const schemaGroupMapById = ref<Map<string, SchemaGroup>>(new Map());
const cachedProjectNameSet = ref<Set<string>>(new Set());
const getOrFetchDBGroupById = async (dbGroupId: string) => {
@@ -48,6 +49,8 @@ export const useDBGroupStore = defineStore("db-group", () => {
databaseGroup: DatabaseGroup,
databaseGroupId: string
) => {
+ // Note: use resource id as placeholder right now.
+ databaseGroup.databasePlaceholder = databaseGroupId;
const createdDatabaseGroup = await projectServiceClient.createDatabaseGroup(
{
parent: projectName,
@@ -65,14 +68,6 @@ export const useDBGroupStore = defineStore("db-group", () => {
throw new Error("Database group not found");
}
const updateMask: string[] = [];
- if (
- !isEqual(
- rawDatabaseGroup.databasePlaceholder,
- databaseGroup.databasePlaceholder
- )
- ) {
- updateMask.push("database_placeholder");
- }
if (!isEqual(rawDatabaseGroup.databaseExpr, databaseGroup.databaseExpr)) {
updateMask.push("database_expr");
}
@@ -86,11 +81,82 @@ export const useDBGroupStore = defineStore("db-group", () => {
return updatedDatabaseGroup;
};
+ const deleteDatabaseGroup = async (databaseGroup: DatabaseGroup) => {
+ await projectServiceClient.deleteDatabaseGroup({
+ name: databaseGroup.name,
+ });
+ dbGroupMapById.value.delete(databaseGroup.name);
+ };
+
+ const getOrFetchSchemaGroupById = async (schemaGroupId: string) => {
+ const cached = schemaGroupMapById.value.get(schemaGroupId);
+ if (cached) return cached;
+
+ const schemaGroup = await projectServiceClient.getSchemaGroup({
+ name: schemaGroupId,
+ });
+ schemaGroupMapById.value.set(schemaGroupId, schemaGroup);
+ return schemaGroup;
+ };
+
+ const getOrFetchSchemaListByDBGroupName = async (dbGroupName: string) => {
+ const { schemaGroups } = await projectServiceClient.listSchemaGroups({
+ parent: dbGroupName,
+ });
+ return schemaGroups;
+ };
+
+ const createSchemaGroup = async (
+ dbGroupName: string,
+ schemaGroup: SchemaGroup,
+ schemaGroupId: string
+ ) => {
+ // Note: use resource id as placeholder right now.
+ schemaGroup.tablePlaceholder = schemaGroupId;
+ const createdSchemaGroup = await projectServiceClient.createSchemaGroup({
+ parent: dbGroupName,
+ schemaGroup,
+ schemaGroupId,
+ });
+ schemaGroupMapById.value.set(createdSchemaGroup.name, createdSchemaGroup);
+ return createdSchemaGroup;
+ };
+
+ const updateSchemaGroup = async (schemaGroup: SchemaGroup) => {
+ const rawSchemaGroup = schemaGroupMapById.value.get(schemaGroup.name);
+ if (!rawSchemaGroup) {
+ throw new Error("Schema group not found");
+ }
+ const updateMask: string[] = [];
+ if (!isEqual(rawSchemaGroup.tableExpr, schemaGroup.tableExpr)) {
+ updateMask.push("table_expr");
+ }
+ const updatedSchemaGroup = await projectServiceClient.updateSchemaGroup({
+ schemaGroup,
+ updateMask,
+ });
+ schemaGroupMapById.value.set(updatedSchemaGroup.name, updatedSchemaGroup);
+ return updatedSchemaGroup;
+ };
+
+ const deleteSchemaGroup = async (schemaGroup: SchemaGroup) => {
+ await projectServiceClient.deleteSchemaGroup({
+ name: schemaGroup.name,
+ });
+ schemaGroupMapById.value.delete(schemaGroup.name);
+ };
+
return {
getOrFetchDBGroupById,
getOrFetchDBGroupListByProjectName,
getDBGroupListByProjectName,
createDatabaseGroup,
updateDatabaseGroup,
+ deleteDatabaseGroup,
+ getOrFetchSchemaGroupById,
+ getOrFetchSchemaListByDBGroupName,
+ createSchemaGroup,
+ updateSchemaGroup,
+ deleteSchemaGroup,
};
});
diff --git a/frontend/src/utils/databaseGroup/cel.ts b/frontend/src/utils/databaseGroup/cel.ts
index 78c7dbbefa..62f9adf3bd 100644
--- a/frontend/src/utils/databaseGroup/cel.ts
+++ b/frontend/src/utils/databaseGroup/cel.ts
@@ -4,7 +4,6 @@ import {
SimpleExpr,
convertToCELString,
emptySimpleExpr,
- isConditionGroupExpr,
resolveCELExpr,
wrapAsGroup,
} from "@/plugins/cel";
@@ -43,7 +42,7 @@ export const convertDatabaseGroupExprFromCEL = async (
}
const simpleExpr = resolveCELExpr(celExpr.expr);
- const [environmentId, conditionGroupExpr] =
+ const [environmentId, ...conditionGroupExpr] =
getEnvironmentIdAndConditionExpr(simpleExpr);
if (!environmentId) {
throw new Error("Invalid CEL expression");
@@ -51,7 +50,7 @@ export const convertDatabaseGroupExprFromCEL = async (
return {
environmentId,
- conditionGroupExpr,
+ conditionGroupExpr: wrapAsGroup(...conditionGroupExpr),
};
};
@@ -62,9 +61,17 @@ const getEnvironmentIdAndConditionExpr = (
throw ["", emptySimpleExpr()];
}
- const [left, right] = expr.args;
- return [
- left.args[1] as string,
- isConditionGroupExpr(right) ? right : wrapAsGroup(right),
- ];
+ const [left, ...right] = expr.args;
+ const environmentName = left.args[1] as string;
+ if (Array.isArray(right)) {
+ return [
+ environmentName,
+ {
+ operator: "_&&_",
+ args: right,
+ },
+ ];
+ } else {
+ return [environmentName, right];
+ }
};