mixin

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SwitchStatusOff = "OFF"
	SwitchStatusOn  = "ON"
)

支持的状态值

Variables

This section is empty.

Functions

func OptimisticUpdate

func OptimisticUpdate(tx *gorm.DB, model interface{}, oldVersion uint32, updates map[string]interface{}) error

OptimisticUpdate 是一个简单的辅助函数示例:在单个事务内 使用 WHERE version = oldVersion 执行更新并检查 RowsAffected, 若为 0 则表示版本冲突(需要重试或返回错误)。

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

func (AutoIncrementID) GormDBDataType(db *gorm.DB, _ *schema.Field) string

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

func (*CreatedAt) BeforeCreate

func (m *CreatedAt) BeforeCreate(tx *gorm.DB) (err error)

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

func (*DeletedAt) BeforeDelete

func (m *DeletedAt) BeforeDelete(tx *gorm.DB) (err error)

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。

func (*IsEnabled) BeforeCreate

func (m *IsEnabled) BeforeCreate(tx *gorm.DB) (err error)

BeforeCreate 在创建时如果未显式设置,保证默认启用。

type Metadata

type Metadata struct {
	Metadata datatypes.JSONMap `gorm:"column:metadata;type:json" json:"metadata,omitempty"`
}

Metadata 存任意 JSON 键值对

func (*Metadata) BeforeSave

func (m *Metadata) BeforeSave(tx *gorm.DB) (err error)

type OperatorID

type OperatorID struct {
	CreatedBy
	UpdatedBy
	DeletedBy
}

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,并在数据库中建立索引。

func (*ParentID) BeforeCreate

func (m *ParentID) BeforeCreate(tx *gorm.DB) (err error)

func (*ParentID) BeforeSave

func (m *ParentID) BeforeSave(tx *gorm.DB) (err error)

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

type SoftDelete struct {
	DeletedAt
	DeletedBy
}

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。

func (*SortOrder) BeforeCreate

func (m *SortOrder) BeforeCreate(tx *gorm.DB) (err error)

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)

func (*Status) BeforeCreate

func (m *Status) BeforeCreate(_ *gorm.DB) (err error)

type StringID

type StringID struct {
	ID string `gorm:"column:id;type:varchar(25);primaryKey" json:"id,omitempty"`
}

StringID 是 GORM 可复用的 mixin,提供长度上限为 25 的字符串主键字段。 嵌入到模型后会在创建前自动生成 ID(当为空时),并校验格式与长度。

func (*StringID) BeforeCreate

func (m *StringID) BeforeCreate(_ *gorm.DB) (err error)

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 序列化器存储。

func (*Tag) BeforeCreate

func (m *Tag) BeforeCreate(tx *gorm.DB) (err error)

func (*Tag) BeforeSave

func (m *Tag) BeforeSave(tx *gorm.DB) (err error)

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 中需在业务层或更复杂的钩子中处理)。

func (*TenantID) BeforeCreate

func (m *TenantID) BeforeCreate(tx *gorm.DB) (err error)

func (*TenantID) BeforeSave

func (m *TenantID) BeforeSave(tx *gorm.DB) (err error)

type Time

type Time struct {
	CreateTime
	UpdateTime
	DeleteTime
}

Time (CreateTime + UpdateTime + DeleteTime)

type TimeAt

type TimeAt struct {
	CreatedAt
	UpdatedAt
	DeletedAt
}

TimeAt (CreatedAt + UpdatedAt + DeletedAt)

type Timestamp

type Timestamp struct {
	CreateTimestamp
	UpdateTimestamp
	DeleteTimestamp
}

Timestamp (create_time + update_time + delete_time)

type TimestampAt

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

func (*UpdatedAt) BeforeCreate

func (m *UpdatedAt) BeforeCreate(tx *gorm.DB) (err error)

func (*UpdatedAt) BeforeSave

func (m *UpdatedAt) BeforeSave(tx *gorm.DB) (err error)

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

type UuidID struct {
	ID uuid.UUID `gorm:"column:id;type:char(36);primaryKey" json:"id,omitempty"`
}

UuidID 是 GORM 可复用的 mixin,表示主键 UUID(字符串形式)。 使用 char(36) 存储,可在 BeforeCreate/BeforeSave 钩子中确保默认值。

func (*UuidID) BeforeCreate

func (m *UuidID) BeforeCreate(tx *gorm.DB) (err error)

func (*UuidID) BeforeSave

func (m *UuidID) BeforeSave(tx *gorm.DB) (err error)

type Version

type Version struct {
	Version uint32 `gorm:"column:version;type:int unsigned;default:1;not null;index" json:"version"`
}

Version 是 GORM 可复用的 mixin,表示版本号/乐观锁。 注意:钩子只负责在创建时设置初始版本。乐观锁的检查与递增 需要在更新操作时在仓储/业务层使用带 WHERE version = ? 的更新语句来保证原子性。

func (*Version) BeforeCreate

func (m *Version) BeforeCreate(tx *gorm.DB) (err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL