Documentation
¶
Index ¶
- func AppendWhereFromStruct(db *gorm.DB, s any)
- func GetModelsByIDs[T any, ID comparable](ids []ID, setID func(*T, ID)) []T
- func InitDB(opt *Options, log log.Logger, plugins ...gorm.Plugin) (*gorm.DB, error)
- func NewPgSqlGetter(opt *Options) *pgsqlConnector
- func NewSqliteGetter(opt *Options) *sqliteGetter
- func NewharborGormLogger(l logContract.Logger) *harborGormLogger
- func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB
- type BaseModel
- type BaseRepository
- func (i *BaseRepository[T, ID]) ChangeStatus(req *ChangeStatus) error
- func (i *BaseRepository[T, ID]) Delete(ids ...ID) error
- func (i *BaseRepository[T, ID]) Detail(id ID) (*T, error)
- func (i *BaseRepository[T, ID]) Retrieve(page, pageSize int, fn func(db *gorm.DB)) (count int64, list []T, err error)
- func (i *BaseRepository[T, ID]) Save(data *T) error
- func (i *BaseRepository[T, ID]) Unique(id ID, fn func(db *gorm.DB)) (*T, bool)
- func (i *BaseRepository[T, ID]) WithContext(ctx context.Context) *BaseRepository[T, ID]
- type ChangeStatus
- type Connector
- type CustomReplacer
- type DBServiceProvider
- func (i *DBServiceProvider) Boot(ctx context.Context) error
- func (i *DBServiceProvider) Conf() map[string]string
- func (i *DBServiceProvider) Description() string
- func (i *DBServiceProvider) HealthCheck(ctx context.Context) error
- func (i *DBServiceProvider) Register(ctx context.Context) error
- func (i *DBServiceProvider) SetApplication(app foundation.Application)
- func (i *DBServiceProvider) Shutdown(ctx context.Context) error
- type Driver
- type MysqlConnector
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendWhereFromStruct ¶
AppendWhereFromStruct 根据结构体中的字段名,生成 where 条件 NameLike = name like AgeGt = where age > AgeLt = where age <
func GetModelsByIDs ¶
func GetModelsByIDs[T any, ID comparable](ids []ID, setID func(*T, ID)) []T
GetModelsByIDs 构造仅含主键的模型切片,供 GORM 关联写入。 setID 由调用方写入主键,避免反射:
db.GetModelsByIDs(ids, func(m *Menu, id uint) { m.ID = id })
func NewharborGormLogger ¶ added in v1.0.1
func NewharborGormLogger(l logContract.Logger) *harborGormLogger
Types ¶
type BaseModel ¶
type BaseModel struct {
ID uint `gorm:"primarykey" json:"id,string"`
CreatedAt *time.Time `json:"createdAt,omitempty"`
UpdatedAt *time.Time `json:"updatedAt,omitempty"`
DeletedAt *gorm.DeletedAt `gorm:"index" json:"deletedAt,omitempty"` // 删除时间
CreatorID string `gorm:"comment:创建人" json:"creatorID,omitempty"` // 创建人
CreatorName string `gorm:"comment:创建人名称" json:"creatorName,omitempty"` // 创建人名称
UpdaterID string `gorm:"comment:修改人" json:"updaterID,omitempty"` // 修改人
UpdaterName string `gorm:"comment:修改人名称" json:"updaterName,omitempty"` // 修改人名称
}
type BaseRepository ¶
type BaseRepository[T any, ID comparable] struct { // contains filtered or unexported fields }
BaseRepository 泛型 CRUD 基座;ID 为主键类型(如 uint、string)。
func NewBaseRepository ¶
func NewBaseRepository[T any, ID comparable](db *gorm.DB) BaseRepository[T, ID]
func (*BaseRepository[T, ID]) ChangeStatus ¶
func (i *BaseRepository[T, ID]) ChangeStatus(req *ChangeStatus) error
ChangeStatus 更改状态(启用/禁用等)。
func (*BaseRepository[T, ID]) Delete ¶
func (i *BaseRepository[T, ID]) Delete(ids ...ID) error
Delete 按主键批量删除。
func (*BaseRepository[T, ID]) Detail ¶
func (i *BaseRepository[T, ID]) Detail(id ID) (*T, error)
Detail 按主键获取详情。
func (*BaseRepository[T, ID]) Retrieve ¶
func (i *BaseRepository[T, ID]) Retrieve(page, pageSize int, fn func(db *gorm.DB)) (count int64, list []T, err error)
Retrieve 分页列表。
func (*BaseRepository[T, ID]) Save ¶
func (i *BaseRepository[T, ID]) Save(data *T) error
Save 保存或者更新,save 会保存所有字段,包括零值字段。
func (*BaseRepository[T, ID]) Unique ¶
func (i *BaseRepository[T, ID]) Unique(id ID, fn func(db *gorm.DB)) (*T, bool)
Unique 唯一性判断;id 为零值时不排除自身。
func (*BaseRepository[T, ID]) WithContext ¶
func (i *BaseRepository[T, ID]) WithContext(ctx context.Context) *BaseRepository[T, ID]
type ChangeStatus ¶
type Connector ¶
type CustomReplacer ¶
type CustomReplacer struct {
// contains filtered or unexported fields
}
func (CustomReplacer) Replace ¶
func (r CustomReplacer) Replace(name string) string
type DBServiceProvider ¶
type DBServiceProvider struct {
// contains filtered or unexported fields
}
func (*DBServiceProvider) Boot ¶
func (i *DBServiceProvider) Boot(ctx context.Context) error
func (*DBServiceProvider) Conf ¶
func (i *DBServiceProvider) Conf() map[string]string
func (*DBServiceProvider) Description ¶
func (i *DBServiceProvider) Description() string
func (*DBServiceProvider) HealthCheck ¶ added in v1.1.0
func (i *DBServiceProvider) HealthCheck(ctx context.Context) error
func (*DBServiceProvider) Register ¶
func (i *DBServiceProvider) Register(ctx context.Context) error
func (*DBServiceProvider) SetApplication ¶ added in v1.1.0
func (i *DBServiceProvider) SetApplication(app foundation.Application)
func (*DBServiceProvider) Shutdown ¶ added in v1.1.0
func (i *DBServiceProvider) Shutdown(ctx context.Context) error
type Driver ¶
type Driver string
const ClickHouse Driver = "clickhouse"
const DM Driver = "dm" // 达梦数据库
const Mysql Driver = "mysql"
const Oracle Driver = "oracle"
const Pgsql Driver = "pgsql"
const Sqlite Driver = "sqlite"
type MysqlConnector ¶
type MysqlConnector struct {
// contains filtered or unexported fields
}
func NewMysqlConnector ¶
func NewMysqlConnector(opt *Options) *MysqlConnector
func (*MysqlConnector) DefaultOptions ¶
func (i *MysqlConnector) DefaultOptions() *Options
func (*MysqlConnector) GetDSN ¶
func (i *MysqlConnector) GetDSN() string
func (*MysqlConnector) Options ¶
func (i *MysqlConnector) Options() *Options
type Options ¶
type Options struct {
Username string `json:"username"`
Password string `json:"password"`
Host string `json:"host"`
Port int `json:"port"`
Driver Driver `json:"driver"` // 数据库类型
Database string `json:"database"`
Schema string `json:"schema"`
Charset string `json:"charset"`
Query string `json:"query"`
MaxIdleConns int `json:"max-idle-conns"`
MaxConns int `json:"max-conns"`
TimeZone string `json:"time-zone"`
}
Source Files
¶
- base_model.go
- base_repository.go
- base_scope.go
- connector.go
- connector_mysql.go
- connector_pgsql.go
- connector_sqlite.go
- const.go
- db_service_provider.go
- gorm_logger.go
- helper.go
- init_db.go
Click to show internal directories.
Click to hide internal directories.