base

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MIT Imports: 7 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsureSelects added in v0.13.10

func EnsureSelects(tx *gorm.DB, fieldNames ...string)

EnsureSelects 在使用 Select 限定插入/更新字段时,确保必要字段被包含 典型场景:在钩子中设置 CreatedAt/UpdatedAt 等字段,但外部使用了 Select 导致未持久化 函数名:EnsureSelects 参数:

  • tx *gorm.DB:当前事务上下文
  • fieldNames ...string:结构体字段名列表(如 "UpdatedAt")

返回值:无(直接修改 tx.Statement.Selects) 异常:无(只在 Schema 可用时生效) 使用示例:

EnsureSelects(tx, "CreatedAt", "UpdatedAt")

func SetFieldIfExist added in v0.10.4

func SetFieldIfExist(model interface{}, fieldName string, value string)

SetFieldIfExist 设置字段值(如果字段存在且可设置) 支持字符串类型和 time.Time 类型字段的自动设置 函数名:SetFieldIfExist 参数:

  • model interface{}:模型对象或其指针
  • fieldName string:结构体字段名(非列名)
  • value string:用于设置的字符串值(时间格式:2006-01-02 15:04:05)

返回值:无 异常:无(内部忽略不可设或类型不匹配的字段) 使用示例:

SetFieldIfExist(&user, "UpdatedAt", "2025-01-01 00:00:00")

Types

type CommonBaseModel added in v0.13.22

type CommonBaseModel struct{}

CommonBaseModel 跨数据库通用基础模型 不绑定具体数据库驱动,字段由业务模型自行定义。 提供 CRUD trait 所需的 ModelParse 方法以及 GORM 创建/更新钩子。

func (*CommonBaseModel) BeforeCreate added in v0.13.22

func (b *CommonBaseModel) BeforeCreate(tx *gorm.DB) (err error)

BeforeCreate 创建前钩子 GORM 在创建记录前自动调用。若模型中不存在对应字段,设置操作会被静默忽略。

参数:

  • tx *gorm.DB: 当前 GORM 事务上下文,包含 Statement.Dest 与 Statement.Selects

返回值:

  • err error: 错误信息,当前实现始终返回 nil

异常:

  • 无显式异常,内部通过反射安全设置字段值

使用示例:

type User struct {
    base.CommonBaseModel
    Username string `gorm:"column:username" json:"username"`
}
db.Create(&user) // 自动触发 BeforeCreate,设置 CreatedAt/UpdatedAt 等字段

func (*CommonBaseModel) BeforeUpdate added in v0.13.22

func (b *CommonBaseModel) BeforeUpdate(tx *gorm.DB) (err error)

BeforeUpdate 更新前钩子 GORM 在更新记录前自动调用。若模型中不存在对应字段,设置操作会被静默忽略。

参数:

  • tx *gorm.DB: 当前 GORM 事务上下文,包含 Statement.Dest 与 Statement.Selects

返回值:

  • err error: 错误信息,当前实现始终返回 nil

异常:

  • 无显式异常,内部通过反射安全设置字段值

使用示例:

type User struct {
    base.CommonBaseModel
    Username string `gorm:"column:username" json:"username"`
}
db.Model(&user).Updates(map[string]interface{}{"username": "new"}) // 自动触发 BeforeUpdate

func (*CommonBaseModel) GetConfigAlias added in v0.13.22

func (b *CommonBaseModel) GetConfigAlias(model interface{}) string

GetConfigAlias 获取数据库配置别名 若模型实现了 ConfigAlias() string 方法,则使用模型自定义的别名;否则返回默认别名 "db"。

参数:

  • model: 模型实例

返回值:

  • string: 配置别名,对应环境变量中的 jc_mysql_<alias> 或 jc_sql_lite_<alias>

func (*CommonBaseModel) ModelParse added in v0.13.22

func (b *CommonBaseModel) ModelParse(modelType reflect.Type) (tableName string, fields []string, softDeleteField string, softDeleteCondition string)

ModelParse 解析模型信息(供 CRUD trait 使用) 参数:

  • modelType: 具体模型的反射类型

返回值:

  • tableName: 数据表名称,优先使用 TableName() 方法
  • fields: 模型字段列表(包含匿名嵌入结构体的字段)
  • softDeleteField: 软删除字段名
  • softDeleteCondition: 软删除条件

type MysqlBaseModel

type MysqlBaseModel struct {
	CommonBaseModel
}

MysqlBaseModel gorm基础模型 继承 CommonBaseModel 的 BeforeCreate、BeforeUpdate 等方法, 默认配置别名为 "db"。

func (*MysqlBaseModel) ModelParse

func (b *MysqlBaseModel) ModelParse(modelType reflect.Type) (tableName string, fields []string, softDeleteField string, softDeleteCondition string)

ModelParse 解析模型信息(供 CRUD trait 使用) 复用 CommonBaseModel 的解析逻辑,默认优先读取 MySQL 配置。

参数:

  • modelType: 具体模型的反射类型

返回值:

  • tableName: 数据表名称
  • fields: 模型字段列表
  • softDeleteField: 软删除字段名
  • softDeleteCondition: 软删除条件

type SqliteBaseModel

type SqliteBaseModel struct {
	CommonBaseModel
}

SqliteBaseModel gorm基础模型 继承 CommonBaseModel 的 BeforeCreate、BeforeUpdate 等方法, 默认配置别名为 "main"。

func (*SqliteBaseModel) GetConfigAlias added in v0.11.1

func (b *SqliteBaseModel) GetConfigAlias(model interface{}) string

GetConfigAlias 获取数据库配置别名 SQLite 默认使用 "main" 作为配置别名。

参数:

  • model: 模型实例

返回值:

  • string: 配置别名

func (*SqliteBaseModel) ModelParse

func (b *SqliteBaseModel) ModelParse(modelType reflect.Type) (tableName string, fields []string, softDeleteField string, softDeleteCondition string)

ModelParse 解析模型信息(供 CRUD trait 使用) 复用 CommonBaseModel 的解析逻辑,默认优先读取 SQLite 配置。

参数:

  • modelType: 具体模型的反射类型

返回值:

  • tableName: 数据表名称
  • fields: 模型字段列表
  • softDeleteField: 软删除字段名
  • softDeleteCondition: 软删除条件

Jump to

Keyboard shortcuts

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