Skip to content

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

chore: support the simplest semantic type template (#9154)

ecmadao11738fbcf41f
diff --git a/frontend/src/components/SensitiveData/SemanticTypesView.vue b/frontend/src/components/SensitiveData/SemanticTypesView.vue
index cc85e4c57d..8bd47b53a0 100644
--- a/frontend/src/components/SensitiveData/SemanticTypesView.vue
+++ b/frontend/src/components/SensitiveData/SemanticTypesView.vue
@@ -1,6 +1,6 @@
<template>
<div class="w-full mt-4 space-y-4">
- <div class="flex items-center justify-end">
+ <div class="flex items-center justify-end space-x-2">
<NButton
type="primary"
:disabled="!hasPermission || !hasSensitiveDataFeature"
@@ -8,6 +8,12 @@
>
{{ $t("settings.sensitive-data.semantic-types.add-type") }}
</NButton>
+ <NButton
+ :disabled="!hasPermission || !hasSensitiveDataFeature"
+ @click="state.showTemplateDrawer = true"
+ >
+ {{ $t("settings.sensitive-data.semantic-types.add-from-template") }}
+ </NButton>
</div>
<div class="space-y-5 divide-y-2 pb-10 divide-gray-100">
<SemanticTypesTable
@@ -20,6 +26,11 @@
/>
</div>
</div>
+ <SemanticTemplateDrawer
+ :show="state.showTemplateDrawer"
+ @apply="onTemplateApply"
+ @dismiss="state.showTemplateDrawer = false"
+ />
</template>
<script lang="ts" setup>
import { NButton } from "naive-ui";
@@ -41,12 +52,14 @@ import SemanticTypesTable, {
interface LocalState {
semanticItemList: SemanticItem[];
processing: boolean;
+ showTemplateDrawer: boolean;
}
const { t } = useI18n();
const state = reactive<LocalState>({
semanticItemList: [],
processing: false,
+ showTemplateDrawer: false,
});
const settingStore = useSettingV1Store();
@@ -59,20 +72,23 @@ const hasPermission = computed(() => {
});
const hasSensitiveDataFeature = featureToRef("bb.feature.sensitive-data");
+const semanticTypeSettingValue = computed(() => {
+ const semanticTypeSetting = settingStore.getSettingByName(
+ "bb.workspace.semantic-types"
+ );
+ return semanticTypeSetting?.value?.semanticTypeSettingValue?.types ?? [];
+});
+
onMounted(async () => {
- const semanticTypeSetting = await settingStore.getOrFetchSettingByName(
- "bb.workspace.semantic-types",
- true
+ state.semanticItemList = semanticTypeSettingValue.value.map(
+ (semanticType) => {
+ return {
+ dirty: false,
+ item: semanticType,
+ mode: "NORMAL",
+ };
+ }
);
- state.semanticItemList = (
- semanticTypeSetting?.value?.semanticTypeSettingValue?.types ?? []
- ).map((semanticType) => {
- return {
- dirty: false,
- item: semanticType,
- mode: "NORMAL",
- };
- });
});
const onAdd = () => {
@@ -121,11 +137,21 @@ const onConfirm = async (index: number) => {
mode: "NORMAL",
};
+ await onUpsert(
+ state.semanticItemList.map((data) => data.item),
+ t(`common.${item.mode === "CREATE" ? "created" : "updated"}`)
+ );
+};
+
+const onUpsert = async (
+ semanticItemList: SemanticTypeSetting_SemanticType[],
+ notification: string
+) => {
await settingStore.upsertSetting({
name: "bb.workspace.semantic-types",
value: {
semanticTypeSettingValue: {
- types: state.semanticItemList.map((data) => data.item),
+ types: semanticItemList,
},
},
});
@@ -133,7 +159,7 @@ const onConfirm = async (index: number) => {
pushNotification({
module: "bytebase",
style: "SUCCESS",
- title: t(`common.${item.mode === "CREATE" ? "created" : "updated"}`),
+ title: notification,
});
};
@@ -159,4 +185,20 @@ const onCancel = (index: number) => {
};
}
};
+
+const onTemplateApply = async (template: SemanticTypeSetting_SemanticType) => {
+ const semanticItem: SemanticItem = {
+ dirty: false,
+ mode: "NORMAL",
+ item: SemanticTypeSetting_SemanticType.fromPartial({
+ ...template,
+ id: uuidv4(),
+ }),
+ };
+ state.semanticItemList.push(semanticItem);
+ await onUpsert(
+ [...semanticTypeSettingValue.value, semanticItem.item],
+ t("common.created")
+ );
+};
</script>
diff --git a/frontend/src/components/SensitiveData/components/SemanticTemplateDrawer.vue b/frontend/src/components/SensitiveData/components/SemanticTemplateDrawer.vue
new file mode 100644
index 0000000000..d7f09b1e87
--- /dev/null
+++ b/frontend/src/components/SensitiveData/components/SemanticTemplateDrawer.vue
@@ -0,0 +1,56 @@
+<template>
+ <Drawer :show="show" @close="$emit('dismiss')">
+ <DrawerContent
+ :title="$t('settings.sensitive-data.semantic-types.add-from-template')"
+ >
+ <SemanticTypesTable
+ :readonly="true"
+ :row-clickable="true"
+ :semantic-item-list="semanticTemplateList"
+ @on-select="onApply($event)"
+ />
+ <template #footer>
+ <div class="w-full flex justify-between items-center">
+ <div class="w-full flex justify-end items-center gap-x-3">
+ <NButton @click.prevent="$emit('dismiss')">
+ {{ $t("common.cancel") }}
+ </NButton>
+ </div>
+ </div>
+ </template>
+ </DrawerContent>
+ </Drawer>
+</template>
+
+<script setup lang="ts">
+import { computed } from "vue";
+import { Drawer, DrawerContent } from "@/components/v2";
+import { getSemanticTemplateList } from "@/types";
+import { SemanticTypeSetting_SemanticType } from "@/types/proto/v1/setting_service";
+import SemanticTypesTable, { SemanticItem } from "./SemanticTypesTable.vue";
+
+defineProps<{
+ show: boolean;
+}>();
+
+const emit = defineEmits<{
+ (event: "dismiss"): void;
+ (event: "apply", item: SemanticTypeSetting_SemanticType): void;
+}>();
+
+const semanticTemplateList = computed((): SemanticItem[] =>
+ getSemanticTemplateList().map((item) => ({
+ dirty: false,
+ item,
+ mode: "NORMAL",
+ }))
+);
+
+const onApply = (id: string) => {
+ const data = semanticTemplateList.value.find((data) => data.item.id === id);
+ if (data) {
+ emit("apply", data.item);
+ }
+ emit("dismiss");
+};
+</script>
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 00a616cbcf..bae795b0e0 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -680,12 +680,45 @@
"semantic-types": {
"self": "Semantic Types",
"add-type": "Add type",
+ "add-from-template": "Add from template",
"table": {
"semantic-type": "Semantic type",
"description": "Description",
"full-masking-algorithm": "Full-masking algorithm",
"partial-masking-algorithm": "Partial-masking algorithm",
"delete": "Delete this semantic type?"
+ },
+ "template": {
+ "address": {
+ "title": "Address"
+ },
+ "secret_key": {
+ "title": "Encryption Key"
+ },
+ "card_number": {
+ "title": "Bank card, credit card, etc"
+ },
+ "email": {
+ "title": "Email address"
+ },
+ "id_number": {
+ "title": "Personal ID number"
+ },
+ "ipv4_address": {
+ "title": "IPV4 address"
+ },
+ "license_plate_number": {
+ "title": "Plate number"
+ },
+ "mac_address": {
+ "title": "MAC address"
+ },
+ "password": {
+ "title": "Password"
+ },
+ "phone": {
+ "title": "Phone number"
+ }
}
},
"column-detail": {
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index 11d7d21cee..60c1a9f876 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -679,12 +679,45 @@
"semantic-types": {
"self": "Tipos semánticos",
"add-type": "Agregar tipo",
+ "add-from-template": "Agregar desde plantilla",
"table": {
"semantic-type": "Tipo semántico",
"description": "Descripción",
"full-masking-algorithm": "Algoritmo de enmascaramiento completo",
"partial-masking-algorithm": "Algoritmo de enmascaramiento parcial",
"delete": "¿Eliminar este tipo semántico?"
+ },
+ "template": {
+ "address": {
+ "title": "La dirección"
+ },
+ "secret_key": {
+ "title": "Clave de encriptación"
+ },
+ "card_number": {
+ "title": "Tarjeta bancaria, tarjeta de crédito, etc."
+ },
+ "email": {
+ "title": "Dirección de correo electrónico"
+ },
+ "id_number": {
+ "title": "Número de identificación personal"
+ },
+ "ipv4_address": {
+ "title": "Dirección IPV4"
+ },
+ "license_plate_number": {
+ "title": "Número de placa"
+ },
+ "mac_address": {
+ "title": "Dirección MAC"
+ },
+ "password": {
+ "title": "Contraseña"
+ },
+ "phone": {
+ "title": "Número de teléfono"
+ }
}
},
"column-detail": {
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index e384a43b6f..8bed08b149 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -678,12 +678,45 @@
"semantic-types": {
"self": "语义类型",
"add-type": "新增类型",
+ "add-from-template": "从模板添加",
"table": {
"semantic-type": "语义类型",
"description": "描述",
"full-masking-algorithm": "全脱敏算法",
"partial-masking-algorithm": "半脱敏算法",
"delete": "删除这个语义类型?"
+ },
+ "template": {
+ "address": {
+ "title": "中文详细地址"
+ },
+ "secret_key": {
+ "title": "加密 Key"
+ },
+ "card_number": {
+ "title": "银行卡、信用卡"
+ },
+ "email": {
+ "title": "电子邮箱"
+ },
+ "id_number": {
+ "title": "身份证号码"
+ },
+ "ipv4_address": {
+ "title": "IPV4 地址"
+ },
+ "license_plate_number": {
+ "title": "车牌号码"
+ },
+ "mac_address": {
+ "title": "MAC 地址"
+ },
+ "password": {
+ "title": "密码"
+ },
+ "phone": {
+ "title": "电话号码"
+ }
}
},
"column-detail": {
diff --git a/frontend/src/types/index.ts b/frontend/src/types/index.ts
index 40b5c7ed1c..ae844cc285 100644
--- a/frontend/src/types/index.ts
+++ b/frontend/src/types/index.ts
@@ -44,5 +44,6 @@ export * from "./dbGroup";
export * from "./quickAction";
export * from "./setting";
export * from "./changeHistory";
+export * from "./semanticTypes";
export * from "./v1";
diff --git a/frontend/src/types/semantic-types.yaml b/frontend/src/types/semantic-types.yaml
new file mode 100644
index 0000000000..a51e327f23
--- /dev/null
+++ b/frontend/src/types/semantic-types.yaml
@@ -0,0 +1,10 @@
+- id: address
+- id: secret_key
+- id: card_number
+- id: email
+- id: id_number
+- id: ipv4_address
+- id: license_plate_number
+- id: mac_address
+- id: password
+- id: phone
diff --git a/frontend/src/types/semanticTypes.ts b/frontend/src/types/semanticTypes.ts
new file mode 100644
index 0000000000..e178d537c2
--- /dev/null
+++ b/frontend/src/types/semanticTypes.ts
@@ -0,0 +1,27 @@
+import { v4 as uuidv4 } from "uuid";
+import { useI18n } from "vue-i18n";
+import { SemanticTypeSetting_SemanticType } from "@/types/proto/v1/setting_service";
+import buildInSemanticTypes from "./semantic-types.yaml";
+
+interface BuildInSemantic {
+ id: string;
+ fullMaskAlgorithmId?: string;
+ partialMaskAlgorithmId?: string;
+}
+
+export const getSemanticTemplateList = () => {
+ const { t } = useI18n();
+ return (buildInSemanticTypes as BuildInSemantic[]).map((buildInSemantic) =>
+ SemanticTypeSetting_SemanticType.fromPartial({
+ id: uuidv4(),
+ title: t(
+ `settings.sensitive-data.semantic-types.template.${buildInSemantic.id}.title`
+ ),
+ description: t(
+ `settings.sensitive-data.semantic-types.template.${buildInSemantic.id}.description`
+ ),
+ fullMaskAlgorithmId: buildInSemantic.fullMaskAlgorithmId,
+ partialMaskAlgorithmId: buildInSemantic.partialMaskAlgorithmId,
+ })
+ );
+};