
Spec
数据库 proto 生成
本说明描述在 .proto 中如何使用 domain 目录下的定义,将领域模型映射为关系型数据库中的表、列和视图,可作为人和大模型共同参考的约定。
主要依赖:
apihug/protobuf/domain/persistence.protoapihug/protobuf/domain/view.protoapihug/protobuf/domain/annotations.protoapihug/protobuf/extend/common.proto.proto 文件描述。syntax = "proto3";
package your.domain;
import "apihug/protobuf/domain/annotations.proto";
message Pet { ... }。(hope.persistence.table) 声明表信息:
message Pet {
option (hope.persistence.table) = {
name: "PET"; // 表名,推荐大写下划线
description: "宠物"; // 表用途说明
// 可按需设置:catalog, schema
// 可按需设置:unique_constraints, indexes
// 可按需设置:views, wires, liquibase
};
// 字段定义见下一节
}
name 建议与实际数据库表名保持一致;description 用自然语言简要说明业务含义。(hope.persistence.column) 元数据:
string name = 1 [(hope.persistence.column) = {
name: "NAME";
description: "宠物名称";
nullable: FALSE;
type: VARCHAR;
}];
id:是否为主键列,类型为 hope.common.BoolType。generated_value:主键生成策略,包含 strategy(如 UUID、SEQUENCE 等)和 generator 名称。unique:是否唯一键。nullable:是否允许为 NULL。insertable / updatable:是否参与插入 / 更新。searchable / sortable:是否用于过滤 / 排序。type:使用 Column.Type 指定 SQL 类型,如 VARCHAR、INTEGER、DATE 等。length(google.protobuf.Int32Value)。precision 和 scale 控制精度。enum_type:STRING 或 CODE,分别表示持久化哪种枚举表示。table:当字段位于辅助表时,标明具体表名。column_definition:自定义 SQL 片段,仅在确有需要时使用。transient:标记字段为非持久化,只用于视图或计算。非用户指定需要,勿添加view!!
Table.views 中定义视图:
option (hope.persistence.table) = {
name: "PET";
description: "宠物";
views: [
{
name: "summary";
description: "列表视图";
includes: ["id", "name", "category"];
excludes: ["description"];
}
];
};
includes:显式包含的字段列表,优先级高于 excludes。excludes:需要从默认字段集中排除的字段。AggregatedField:
aggregated_fields: [{
type: COUNT;
alias: "total";
}];
ReferenceView:
references: [{
entity: "Category"; // 目标实体名
view: "summary"; // 目标实体上的视图名
join_type: INNER;
left_field: "category"; // 当前实体字段
right_field: "name"; // 目标实体字段
}];
wires 用于表示是否应用平台内置的一些通用领域特性(例如审计、逻辑删除、多租户等),使用方式示例:
wires: [ALL];
wires: [AUDITABLE, DELETABLE];
wires: [NONE];
具体可用值以 Table.Wire 枚举为准。
(hope.persistence.table)。(hope.persistence.column),补充必要的约束与类型信息。View/AggregatedField/ReferenceView 建模视图,而不是复制多个实体。Liquibase 条目,而不是直接修改已有版本记录。