Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AuditModel ¶
type AuditModel struct {
Model
CreatedBy string `gorm:"size:64"` // 创建人标识(用户 ID 或账号)
UpdatedBy string `gorm:"size:64"` // 更新人标识(用户 ID 或账号)
}
AuditModel 在基础字段上增加创建人/更新人审计字段。 审计字段需要业务在写入时显式赋值(例如通过中间件从用户上下文填充)。
type Model ¶
type Model struct {
ID int `gorm:"primarykey"` // 主键
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"` // 逻辑删除
}
Model 默认表需携带的必须字段(GORM 惯例字段名)。
type OrgFields ¶ added in v1.10.0
type OrgFields struct {
OrgID int64 `gorm:"column:org_id;index"` // 组织 ID
DeptID int64 `gorm:"column:dept_id;index"` // 部门 ID
}
OrgFields 组织/部门公共字段(多租户与数据权限基础)。 叠加在任意 ID 模型之上;由业务侧在写入时显式赋值 (例如从 JWT 上下文或组织中间件填充),脚手架不自动维护。
type Order struct {
model.SnowflakeModel
model.OrgFields
OrderNo string `gorm:"column:order_no;uniqueIndex:uk_order_no"`
}
type SnowflakeModel ¶ added in v1.10.0
type SnowflakeModel struct {
ID int64 `gorm:"primarykey;column:id"` // 雪花主键(自动生成)
GmtCreate time.Time `gorm:"column:gmt_create"` // 创建时间(自动维护)
GmtModified time.Time `gorm:"column:gmt_modified"` // 修改时间(自动维护)
IsDeleted int `gorm:"column:is_deleted;default:0;index"` // 逻辑删除 0/1
}
SnowflakeModel 使用雪花算法 ID(int64)的公共模型。 嵌入后主键由脚手架在 BeforeCreate 自动生成(仅当 ID 为零值), 字段命名对齐阿里手册:gmt_create / gmt_modified / is_deleted。
type Order struct {
model.SnowflakeModel
OrderNo string `gorm:"column:order_no;uniqueIndex:uk_order_no"`
}
分布式部署时通过 id.SetNode(节点号) 为每个实例分配不同节点, 也可在启动前设置环境变量 GOBLACKBOX_SNOWFLAKE_NODE。
func (*SnowflakeModel) BeforeCreate ¶ added in v1.10.0
func (m *SnowflakeModel) BeforeCreate(_ *gorm.DB) error
BeforeCreate 自动生成雪花 ID(仅零值时)并填充时间。
func (*SnowflakeModel) BeforeUpdate ¶ added in v1.10.0
func (m *SnowflakeModel) BeforeUpdate(tx *gorm.DB) error
BeforeUpdate 更新时自动维护 gmt_modified。
type StandardModel ¶ added in v1.9.0
type StandardModel struct {
ID int64 `gorm:"primarykey;column:id"` // 主键
GmtCreate time.Time `gorm:"column:gmt_create"` // 创建时间(自动维护)
GmtModified time.Time `gorm:"column:gmt_modified"` // 修改时间(自动维护)
IsDeleted int `gorm:"column:is_deleted;default:0;index"` // 逻辑删除 0/1
}
StandardModel 遵循阿里开发手册(泰山版)数据库字段命名规范:
- 主键: id
- 创建时间: gmt_create(禁止 create_time 混用)
- 修改时间: gmt_modified(更新记录必须同步维护)
- 逻辑删除: is_deleted(0 未删除 / 1 已删除),布尔属性在模型中去掉 is 前缀
时间字段由 GORM hooks 自动维护,无需业务赋值。 索引命名规范:普通索引 idx_ 前缀、唯一索引 uk_ 前缀(如 uk_order_no)。
func (*StandardModel) BeforeCreate ¶ added in v1.9.0
func (m *StandardModel) BeforeCreate(_ *gorm.DB) error
BeforeCreate 写入时自动填充创建/修改时间。
func (*StandardModel) BeforeUpdate ¶ added in v1.9.0
func (m *StandardModel) BeforeUpdate(tx *gorm.DB) error
BeforeUpdate 更新时自动维护修改时间。 使用 SetColumn 强制写入,保证 map 与 struct 两种更新方式下 gmt_modified 都会被刷新 (GORM 的 map 更新会忽略 hook 中直接修改的字段值)。
type StringIDModel ¶ added in v1.10.0
type StringIDModel struct {
ID string `gorm:"primarykey;column:id;size:36"` // UUID 主键(自动生成)
GmtCreate time.Time `gorm:"column:gmt_create"` // 创建时间(自动维护)
GmtModified time.Time `gorm:"column:gmt_modified"` // 修改时间(自动维护)
IsDeleted int `gorm:"column:is_deleted;default:0;index"` // 逻辑删除 0/1
}
StringIDModel 使用 UUID v4 字符串主键的公共模型。 适合跨系统合并、客户端生成 ID、不可枚举等场景; 主键由脚手架在 BeforeCreate 自动生成(仅当 ID 为空)。
type Order struct {
model.StringIDModel
OrderNo string `gorm:"column:order_no;uniqueIndex:uk_order_no"`
}
func (*StringIDModel) BeforeCreate ¶ added in v1.10.0
func (m *StringIDModel) BeforeCreate(_ *gorm.DB) error
BeforeCreate 自动生成 UUID(仅空值时)并填充时间。
func (*StringIDModel) BeforeUpdate ¶ added in v1.10.0
func (m *StringIDModel) BeforeUpdate(tx *gorm.DB) error
BeforeUpdate 更新时自动维护 gmt_modified。