Documentation
¶
Index ¶
- Constants
- func OptimisticUpdate(tx *gorm.DB, model interface{}, oldVersion uint32, ...) error
- type AutoIncrementID
- type CreateBy
- type CreateTime
- type CreateTimestamp
- type CreatedAt
- type CreatedAtTimestamp
- type CreatedBy
- type CreatorID
- type DeleteBy
- type DeleteTime
- type DeleteTimestamp
- type DeletedAt
- type DeletedAtTimestamp
- type DeletedBy
- type Description
- type IsEnabled
- type Metadata
- type OperatorID
- type ParentID
- type Remark
- type SnowflakeID
- type SoftDelete
- type SortOrder
- type Status
- type StringID
- type SwitchStatus
- type Tag
- type TenantID
- type Time
- type TimeAt
- type Timestamp
- type TimestampAt
- type Tree
- type UpdateBy
- type UpdateTime
- type UpdateTimestamp
- type UpdatedAt
- type UpdatedAtTimestamp
- type UpdatedBy
- type UuidID
- type Version
Constants ¶
const ( SwitchStatusOff = "OFF" SwitchStatusOn = "ON" )
支持的状态值
Variables ¶
This section is empty.
Functions ¶
Types ¶
type AutoIncrementID ¶
type AutoIncrementID struct {
ID uint32 `gorm:"column:id;primaryKey;autoIncrement" json:"id,omitempty"`
}
AutoIncrementID 是 GORM 可复用的 mixin,包含自增主键字段。 在模型中嵌入:
type User struct {
mixin.AutoIncrementID
Name string
}
func (AutoIncrementID) GormDBDataType ¶
type CreateBy ¶
type CreateBy struct {
CreateBy *uint32 `gorm:"column:create_by;index;<-:create" json:"create_by,omitempty"`
}
CreateBy 表示创建操作的操作者 ID(只允许在 Create 时写入)。
type CreateTime ¶
type CreateTime struct {
CreateTime *time.Time `gorm:"column:create_time;type:datetime" json:"create_time,omitempty"`
}
CreateTime (create_time)
func (*CreateTime) BeforeCreate ¶
func (m *CreateTime) BeforeCreate(tx *gorm.DB) (err error)
type CreateTimestamp ¶
type CreateTimestamp struct {
CreateTime *int64 `gorm:"column:create_time;type:bigint" json:"create_time,omitempty"`
}
CreateTimestamp (create_time)
func (*CreateTimestamp) BeforeCreate ¶
func (m *CreateTimestamp) BeforeCreate(tx *gorm.DB) (err error)
type CreatedAt ¶
type CreatedAt struct {
CreatedAt *time.Time `gorm:"column:created_at;type:datetime" json:"created_at,omitempty"`
}
CreatedAt created_at
type CreatedAtTimestamp ¶
type CreatedAtTimestamp struct {
CreatedAt *int64 `gorm:"column:created_at;type:bigint" json:"created_at,omitempty"`
}
CreatedAtTimestamp (created_at)
func (*CreatedAtTimestamp) BeforeCreate ¶
func (m *CreatedAtTimestamp) BeforeCreate(tx *gorm.DB) (err error)
type CreatedBy ¶
type CreatedBy struct {
CreatedBy *uint32 `gorm:"column:created_by;index;<-:create" json:"created_by,omitempty"`
}
CreatedBy 与 CreateBy 等价,按需使用不同列名。
type CreatorID ¶
type CreatorID struct {
CreatorID *uint32 `gorm:"column:creator_id;index;<-:create" json:"creator_id,omitempty"`
}
CreatorID 是 GORM 风格的 mixin,可被模型通过嵌入复用。 - 使用指针类型支持 null/nil。 - `gorm:"<-:create"` 只允许在 Create 时写入(GORM 层面不可在 Update 时修改)。 - `index` 为该列创建索引。
type DeleteBy ¶
type DeleteBy struct {
DeleteBy *uint32 `gorm:"column:delete_by;index" json:"delete_by,omitempty"`
}
DeleteBy 表示删除操作的操作者 ID。
type DeleteTime ¶
type DeleteTime struct {
DeleteTime *time.Time `gorm:"column:delete_time;type:datetime;index" json:"delete_time,omitempty"`
}
DeleteTime (delete_time)
func (*DeleteTime) BeforeDelete ¶
func (m *DeleteTime) BeforeDelete(tx *gorm.DB) (err error)
type DeleteTimestamp ¶
type DeleteTimestamp struct {
DeleteTime *int64 `gorm:"column:delete_time;type:bigint;index" json:"delete_time,omitempty"`
}
DeleteTimestamp (delete_time)
func (*DeleteTimestamp) BeforeDelete ¶
func (m *DeleteTimestamp) BeforeDelete(tx *gorm.DB) (err error)
type DeletedAt ¶
type DeletedAt struct {
DeletedAt *time.Time `gorm:"column:deleted_at;type:datetime;index" json:"deleted_at,omitempty"`
}
DeletedAt deleted_at
type DeletedAtTimestamp ¶
type DeletedAtTimestamp struct {
DeletedAt *int64 `gorm:"column:deleted_at;type:bigint;index" json:"deleted_at,omitempty"`
}
DeletedAtTimestamp (deleted_at)
func (*DeletedAtTimestamp) BeforeDelete ¶
func (m *DeletedAtTimestamp) BeforeDelete(tx *gorm.DB) (err error)
type DeletedBy ¶
type DeletedBy struct {
DeletedBy *uint32 `gorm:"column:deleted_by;index" json:"deleted_by,omitempty"`
}
DeletedBy 与 DeleteBy 等价,按需使用不同列名。
type Description ¶
type Description struct {
Description *string `gorm:"column:description;type:text" json:"description,omitempty"`
}
Description 是 GORM 可复用的 mixin,支持 nullable 描述字段。
type IsEnabled ¶
type IsEnabled struct {
IsEnabled *bool `gorm:"column:is_enabled;type:boolean;default:true;index" json:"is_enabled,omitempty"`
}
IsEnabled 是 GORM 可复用的 mixin,表示是否启用。 可嵌入到任意模型:
type User struct {
mixin.IsEnabled
}
字段使用指针以支持 nullable;`index` 创建索引;BeforeCreate 在未设置时保证为 true。
type Metadata ¶
type Metadata struct {
Metadata datatypes.JSONMap `gorm:"column:metadata;type:json" json:"metadata,omitempty"`
}
Metadata 存任意 JSON 键值对
type OperatorID ¶
OperatorID 组合已创建/已更新/已删除的操作者 ID 字段,方便在模型中嵌入复用。
type ParentID ¶
type ParentID struct {
ParentID *uint32 `gorm:"column:parent_id;type:int unsigned;index" json:"parent_id,omitempty"`
}
ParentID 是 GORM 可复用的 mixin,表示父节点 ID(可为空)。 使用指针以支持 nullable,并在数据库中建立索引。
type Remark ¶
type Remark struct {
Remark *string `gorm:"column:remark;type:text" json:"remark,omitempty"`
}
Remark 是 GORM 可复用的 mixin,支持 nullable 的备注字段。
type SnowflakeID ¶
type SnowflakeID struct {
ID uint64 `gorm:"column:id;primaryKey;autoIncrement:false;type:bigint" json:"id,omitempty"`
}
SnowflakeID 是 GORM 可复用的 mixin,用于使用 Sonyflake 生成的 uint64 主键。 嵌入到模型后会在创建前自动填充 ID(如果为 0)。
func (*SnowflakeID) BeforeCreate ¶
func (m *SnowflakeID) BeforeCreate(tx *gorm.DB) (err error)
BeforeCreate 在创建记录前如果 ID 为 0 则使用 Sonyflake 填充。
type SoftDelete ¶
SoftDelete 组合 DeletedAt 与 DeletedBy,供实体直接嵌入使用。
type SortOrder ¶
type SortOrder struct {
SortOrder *int32 `gorm:"column:sort_order;type:int;default:0;index" json:"sort_order,omitempty"`
}
SortOrder 是 GORM 可复用的 mixin,表示排序序号(值越小越靠前)。 字段使用指针以支持 nullable,带有 gorm 标签和 json 标签。 BeforeCreate 在创建时如果未显式设置,保证默认为 0。
type Status ¶
type Status struct {
Status uint8 `gorm:"column:status;type:tinyint unsigned;default:1;not null;index" json:"status"`
}
Status 表示业务状态(例如 0: disabled, 1: enabled)
type StringID ¶
type StringID struct {
ID string `gorm:"column:id;type:varchar(25);primaryKey" json:"id,omitempty"`
}
StringID 是 GORM 可复用的 mixin,提供长度上限为 25 的字符串主键字段。 嵌入到模型后会在创建前自动生成 ID(当为空时),并校验格式与长度。
type SwitchStatus ¶
type SwitchStatus struct {
Status *string `gorm:"column:status;type:varchar(10);default:ON;index" json:"status,omitempty"`
}
SwitchStatus 是 GORM 可复用的 mixin,表示开关状态(可空)。 字段使用指针以支持 nullable,带有 gorm 标签与 json 标签。 BeforeCreate/BeforeSave 钩子用于填充默认值并校验合法枚举。
func (*SwitchStatus) BeforeCreate ¶
func (m *SwitchStatus) BeforeCreate(tx *gorm.DB) (err error)
func (*SwitchStatus) BeforeSave ¶
func (m *SwitchStatus) BeforeSave(tx *gorm.DB) (err error)
type Tag ¶
type Tag struct {
Tags *[]string `gorm:"column:tags;type:json;serializer:json" json:"tags,omitempty"`
}
Tag 是 GORM 可复用的 mixin,表示对象关联的标签。 使用指针切片以支持 nullable,并通过 gorm 的 json 序列化器存储。
type TenantID ¶
type TenantID struct {
TenantID *uint32 `gorm:"column:tenant_id;type:int unsigned;index" json:"tenant_id,omitempty"`
}
TenantID 是 GORM 可复用的 mixin,表示租户 ID(可为空)。 使用指针以支持 nullable,并在数据库中建立索引。 不在钩子中强制不可变性(ent 的 Immutable 在 GORM 中需在业务层或更复杂的钩子中处理)。
type Time ¶
type Time struct {
CreateTime
UpdateTime
DeleteTime
}
Time (CreateTime + UpdateTime + DeleteTime)
type Timestamp ¶
type Timestamp struct {
CreateTimestamp
UpdateTimestamp
DeleteTimestamp
}
Timestamp (create_time + update_time + delete_time)
type TimestampAt ¶
type TimestampAt struct {
CreatedAtTimestamp
UpdatedAtTimestamp
DeletedAtTimestamp
}
TimestampAt (created_at + updated_at + deleted_at)
type Tree ¶
type Tree[T any] struct { ParentID // Parent 指向父节点(可为 nil) Parent *T `gorm:"foreignKey:ParentID;references:ID" json:"parent,omitempty"` // Children 列表,使用子表的 parent_id 字段作为外键 Children []T `gorm:"foreignKey:ParentID;references:ID" json:"children,omitempty"` }
Tree 是通用的 GORM mixin,嵌入 ParentID 并提供 Parent/Children 关系。 T 应该是包含 `ID` 字段的实体类型(GORM 在运行时通过反射匹配字段)。
type UpdateBy ¶
type UpdateBy struct {
UpdateBy *uint32 `gorm:"column:update_by;index" json:"update_by,omitempty"`
}
UpdateBy 表示更新操作的操作者 ID。
type UpdateTime ¶
type UpdateTime struct {
UpdateTime *time.Time `gorm:"column:update_time;type:datetime" json:"update_time,omitempty"`
}
UpdateTime (update_time)
func (*UpdateTime) BeforeCreate ¶
func (m *UpdateTime) BeforeCreate(tx *gorm.DB) (err error)
func (*UpdateTime) BeforeSave ¶
func (m *UpdateTime) BeforeSave(tx *gorm.DB) (err error)
type UpdateTimestamp ¶
type UpdateTimestamp struct {
UpdateTime *int64 `gorm:"column:update_time;type:bigint" json:"update_time,omitempty"`
}
UpdateTimestamp (update_time)
func (*UpdateTimestamp) BeforeCreate ¶
func (m *UpdateTimestamp) BeforeCreate(tx *gorm.DB) (err error)
func (*UpdateTimestamp) BeforeSave ¶
func (m *UpdateTimestamp) BeforeSave(tx *gorm.DB) (err error)
type UpdatedAt ¶
type UpdatedAt struct {
UpdatedAt *time.Time `gorm:"column:updated_at;type:datetime" json:"updated_at,omitempty"`
}
UpdatedAt updated_at
type UpdatedAtTimestamp ¶
type UpdatedAtTimestamp struct {
UpdatedAt *int64 `gorm:"column:updated_at;type:bigint" json:"updated_at,omitempty"`
}
UpdatedAtTimestamp (updated_at)
func (*UpdatedAtTimestamp) BeforeCreate ¶
func (m *UpdatedAtTimestamp) BeforeCreate(tx *gorm.DB) (err error)
func (*UpdatedAtTimestamp) BeforeSave ¶
func (m *UpdatedAtTimestamp) BeforeSave(tx *gorm.DB) (err error)
type UpdatedBy ¶
type UpdatedBy struct {
UpdatedBy *uint32 `gorm:"column:updated_by;index" json:"updated_by,omitempty"`
}
UpdatedBy 与 UpdateBy 等价,按需使用不同列名。
type UuidID ¶
UuidID 是 GORM 可复用的 mixin,表示主键 UUID(字符串形式)。 使用 char(36) 存储,可在 BeforeCreate/BeforeSave 钩子中确保默认值。