Documentation
¶
Overview ¶
* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 21:13:15 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-28 13:15:15 * @FilePath: \go-sqlbuilder\repository\base.go * @Description: * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-23 08:15:15 * @FilePath: \go-sqlbuilder\repository\filter.go * @Description: 过滤器和查询构建 - Filter、FilterGroup、Query等查询条件 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-23 22:55:38 * @FilePath: \go-sqlbuilder\repository\helpers.go * @Description: 仓储辅助工具 - 软删除、查询辅助等功能 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-23 00:00:00 * @FilePath: \go-sqlbuilder\repository\interfaces.go * @Description: 核心接口定义 - Repository、Transaction、Query 等 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-23 22:50:00 * @FilePath: \go-sqlbuilder\model.go * @Description: 基础模型定义 - BaseModel、AuditModel、UUIDModel 等 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-25 08:06:51 * @FilePath: \go-sqlbuilder\repository\paging.go * @Description: 分页工具 - Pagination分页元数据和辅助方法 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-23 13:29:12 * @FilePath: \go-sqlbuilder\repository\query.go * @Description: 类型定义 - Filter、Pagination、QueryCondition 等 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
Index ¶
- func ConvertToInterfaceSlice(value interface{}) []interface{}
- func GetDeleted[T any](ctx context.Context, db *gorm.DB, query *Query) ([]*T, error)
- func GetNonDeleted[T any](ctx context.Context, db *gorm.DB, query *Query) ([]*T, error)
- func GetTodayRange() (startTime, endTime time.Time)
- func IsSliceType(value interface{}) bool
- func IsToday(t *time.Time) bool
- func IsTodayFromProto(t *timestamppb.Timestamp) bool
- func IsTodayRange(startTime, endTime *time.Time) bool
- func PermanentlyDelete[T any](ctx context.Context, db *gorm.DB, id interface{}) error
- func PermanentlyDeleteBatch[T any](ctx context.Context, db *gorm.DB, ids []interface{}) error
- func RestoreDeleted[T any](ctx context.Context, db *gorm.DB, id interface{}) error
- func RestoreDeletedBatch[T any](ctx context.Context, db *gorm.DB, ids []interface{}) error
- type AuditModel
- type AuditableModel
- type BaseModel
- func (m *BaseModel) BeforeUpdate(tx *gorm.DB) error
- func (m *BaseModel) Disable()
- func (m *BaseModel) Enable()
- func (m *BaseModel) GetID() uint
- func (m *BaseModel) GetVersion() int
- func (m *BaseModel) IsDeleted() bool
- func (m *BaseModel) IsEnabled() bool
- func (m *BaseModel) IsNew() bool
- func (m *BaseModel) SetCreatedAt(t time.Time)
- func (m *BaseModel) SetRemark(remark string)
- func (m *BaseModel) SetUpdatedAt(t time.Time)
- type BaseRepository
- func (r *BaseRepository[T]) BulkCreate(ctx context.Context, entities []*T, batchSize ...int) error
- func (r *BaseRepository[T]) Count(ctx context.Context, filters ...*Filter) (int64, error)
- func (r *BaseRepository[T]) CountByField(ctx context.Context, field string) (map[interface{}]int64, error)
- func (r *BaseRepository[T]) Create(ctx context.Context, entity *T) (*T, error)
- func (r *BaseRepository[T]) CreateBatch(ctx context.Context, entities ...*T) error
- func (r *BaseRepository[T]) CreateIfNotExists(ctx context.Context, entity *T, uniqueFields ...string) (*T, bool, error)
- func (r *BaseRepository[T]) CreateOrUpdate(ctx context.Context, entity *T, uniqueFields ...string) (*T, bool, error)
- func (r *BaseRepository[T]) DBHandler() db.Handler
- func (r *BaseRepository[T]) Delete(ctx context.Context, id interface{}) error
- func (r *BaseRepository[T]) DeleteBatch(ctx context.Context, ids ...interface{}) error
- func (r *BaseRepository[T]) DeleteByFilters(ctx context.Context, filters ...*Filter) error
- func (r *BaseRepository[T]) Distinct(ctx context.Context, field string, filters ...*Filter) ([]interface{}, error)
- func (r *BaseRepository[T]) Exists(ctx context.Context, filters ...*Filter) (bool, error)
- func (r *BaseRepository[T]) Find(ctx context.Context, options *FindOptions) ([]*T, error)
- func (r *BaseRepository[T]) FindOne(ctx context.Context, filters ...*Filter) (*T, error)
- func (r *BaseRepository[T]) First(ctx context.Context, filters ...*Filter) (*T, error)
- func (r *BaseRepository[T]) Get(ctx context.Context, id interface{}) (*T, error)
- func (r *BaseRepository[T]) GetAll(ctx context.Context) ([]*T, error)
- func (r *BaseRepository[T]) GetByFields(ctx context.Context, fields map[string]interface{}) (*T, error)
- func (r *BaseRepository[T]) GetByFilter(ctx context.Context, filter *Filter) (*T, error)
- func (r *BaseRepository[T]) GetByFilters(ctx context.Context, filters ...*Filter) (*T, error)
- func (r *BaseRepository[T]) GetWithPreloads(ctx context.Context, id interface{}, preloads ...string) (*T, error)
- func (r *BaseRepository[T]) Last(ctx context.Context, filters ...*Filter) (*T, error)
- func (r *BaseRepository[T]) List(ctx context.Context, query *Query) ([]*T, error)
- func (r *BaseRepository[T]) ListWithPagination(ctx context.Context, query *Query, page *Pagination) ([]*T, *Pagination, error)
- func (r *BaseRepository[T]) ListWithPreloads(ctx context.Context, query *Query, preloads ...string) ([]*T, error)
- func (r *BaseRepository[T]) Pluck(ctx context.Context, field string, filters ...*Filter) ([]interface{}, error)
- func (r *BaseRepository[T]) Restore(ctx context.Context, id interface{}, field string, restoreValue interface{}) error
- func (r *BaseRepository[T]) RestoreBatch(ctx context.Context, ids []interface{}, field string, restoreValue interface{}) error
- func (r *BaseRepository[T]) SoftDelete(ctx context.Context, id interface{}, field string, value interface{}) error
- func (r *BaseRepository[T]) SoftDeleteBatch(ctx context.Context, ids []interface{}, field string, value interface{}) error
- func (r *BaseRepository[T]) SoftDeleteByFilters(ctx context.Context, field string, value interface{}, filters ...*Filter) error
- func (r *BaseRepository[T]) Table() string
- func (r *BaseRepository[T]) Transaction(ctx context.Context, fn func(tx Transaction[T]) error) error
- func (r *BaseRepository[T]) TransactionWithRawDB(ctx context.Context, fn func(tx *gorm.DB) error) error
- func (r *BaseRepository[T]) Update(ctx context.Context, entity *T) (*T, error)
- func (r *BaseRepository[T]) UpdateBatch(ctx context.Context, entities ...*T) error
- func (r *BaseRepository[T]) UpdateByFilters(ctx context.Context, entity *T, filters ...*Filter) error
- func (r *BaseRepository[T]) UpdateFields(ctx context.Context, id interface{}, fields map[string]interface{}) error
- func (r *BaseRepository[T]) UpdateFieldsByFilters(ctx context.Context, fields map[string]interface{}, filters ...*Filter) error
- type ComparedColumn
- type ComparedValue
- type Condition
- type ContextFieldExtractor
- type EnhancedRepository
- func (r *EnhancedRepository[T]) BatchUpdateByField(ctx context.Context, field string, values []interface{}, ...) error
- func (r *EnhancedRepository[T]) CountByField(ctx context.Context, field string, value interface{}) (int64, error)
- func (r *EnhancedRepository[T]) CreateIfNotExists(ctx context.Context, entity *T, checkField string) (*T, bool, error)
- func (r *EnhancedRepository[T]) DecrementField(ctx context.Context, whereField string, whereValue interface{}, ...) error
- func (r *EnhancedRepository[T]) DeleteByField(ctx context.Context, field string, value interface{}) error
- func (r *EnhancedRepository[T]) ExistsBy(ctx context.Context, field string, value interface{}) (bool, error)
- func (r *EnhancedRepository[T]) FindByField(ctx context.Context, field string, value interface{}) ([]*T, error)
- func (r *EnhancedRepository[T]) FindByFieldWithCursor(ctx context.Context, field string, value interface{}, cursorField string, ...) ([]*T, bool, error)
- func (r *EnhancedRepository[T]) FindByFieldWithPagination(ctx context.Context, field string, value interface{}, limit, offset int) ([]*T, int64, error)
- func (r *EnhancedRepository[T]) FindByFields(ctx context.Context, conditions map[string]interface{}) ([]*T, error)
- func (r *EnhancedRepository[T]) FindByTimeRange(ctx context.Context, timeField string, startTime, endTime interface{}) ([]*T, error)
- func (r *EnhancedRepository[T]) FindInField(ctx context.Context, field string, values []interface{}) ([]*T, error)
- func (r *EnhancedRepository[T]) FindOneByField(ctx context.Context, field string, value interface{}) (*T, error)
- func (r *EnhancedRepository[T]) FindWithOrder(ctx context.Context, whereField string, whereValue interface{}, ...) ([]*T, error)
- func (r *EnhancedRepository[T]) GetDistinctValues(ctx context.Context, field string) ([]interface{}, error)
- func (r *EnhancedRepository[T]) IncrementField(ctx context.Context, whereField string, whereValue interface{}, ...) error
- func (r *EnhancedRepository[T]) UpdateByField(ctx context.Context, field string, value interface{}, ...) error
- func (r *EnhancedRepository[T]) UpdateSingleField(ctx context.Context, whereField string, whereValue interface{}, ...) error
- type Filter
- func NewBetweenFilter(field string, min, max interface{}) *Filter
- func NewContainsFilter(field string, value string) *Filter
- func NewEndsWithFilter(field string, value string) *Filter
- func NewEqFilter(field string, value interface{}) *Filter
- func NewFindInSetFilter(field string, value interface{}) *Filter
- func NewGtFilter(field string, value interface{}) *Filter
- func NewGteFilter(field string, value interface{}) *Filter
- func NewInFilter(field string, values ...interface{}) *Filter
- func NewInFilterSlice(field string, values []interface{}) *Filter
- func NewIsNotNullFilter(field string) *Filter
- func NewIsNullFilter(field string) *Filter
- func NewLikeFilter(field string, value string) *Filter
- func NewLtFilter(field string, value interface{}) *Filter
- func NewLteFilter(field string, value interface{}) *Filter
- func NewNeqFilter(field string, value interface{}) *Filter
- func NewNotInFilter(field string, values ...interface{}) *Filter
- func NewNotInFilterSlice(field string, values []interface{}) *Filter
- func NewNotLikeFilter(field string, value string) *Filter
- func NewStartsWithFilter(field string, value string) *Filter
- type FilterGroup
- type FindOptions
- type FullFeaturedModel
- type LightModel
- type ModelInterface
- type Order
- type OrderBy
- type Pagination
- type Query
- func (q *Query) AddBetween(field string, start, end interface{}) *Query
- func (q *Query) AddEndsWith(field, suffix string) *Query
- func (q *Query) AddEqual(field string, value interface{}) *Query
- func (q *Query) AddFilter(filter *Filter) *Query
- func (q *Query) AddFilterIfNotEmpty(field string, value interface{}) *Query
- func (q *Query) AddFilters(filters ...*Filter) *Query
- func (q *Query) AddGreaterEqual(field string, value interface{}) *Query
- func (q *Query) AddGreaterThan(field string, value interface{}) *Query
- func (q *Query) AddGroupBy(fields ...string) *Query
- func (q *Query) AddHaving(filter *Filter) *Query
- func (q *Query) AddIn(field string, values ...interface{}) *Query
- func (q *Query) AddInFilterIfNotEmpty(field string, values interface{}) *Query
- func (q *Query) AddIsNotNull(field string) *Query
- func (q *Query) AddIsNull(field string) *Query
- func (q *Query) AddLessEqual(field string, value interface{}) *Query
- func (q *Query) AddLessThan(field string, value interface{}) *Query
- func (q *Query) AddLike(field, keyword string) *Query
- func (q *Query) AddLikeFilterIfNotEmpty(field, keyword string) *Query
- func (q *Query) AddNotEqual(field string, value interface{}) *Query
- func (q *Query) AddNotIn(field string, values ...interface{}) *Query
- func (q *Query) AddOrder(field, direction string) *Query
- func (q *Query) AddOrderAsc(field string) *Query
- func (q *Query) AddOrderDesc(field string) *Query
- func (q *Query) AddSafeOrder(sortBy, sortOrder, defaultField, defaultDirection string, ...) *Query
- func (q *Query) AddStartsWith(field, prefix string) *Query
- func (q *Query) AddThisMonth(field string) *Query
- func (q *Query) AddThisWeek(field string) *Query
- func (q *Query) AddThisYear(field string) *Query
- func (q *Query) AddTimeAfter(field string, t time.Time) *Query
- func (q *Query) AddTimeBefore(field string, t time.Time) *Query
- func (q *Query) AddTimeBetween(field string, start, end time.Time) *Query
- func (q *Query) AddTimeRangeFilter(field string, startTime, endTime interface{}) *Query
- func (q *Query) AddToday(field string) *Query
- func (q *Query) GetAllFilters() []*Filter
- func (q *Query) HasFilters() bool
- func (q *Query) Limit(limit int) *Query
- func (q *Query) Offset(offset int) *Query
- func (q *Query) Page(page, pageSize int) *Query
- func (q *Query) SetDistinct() *Query
- func (q *Query) Skip(offset int) *Query
- func (q *Query) Take(limit int) *Query
- func (q *Query) WithDistinct(distinct bool) *Query
- func (q *Query) WithFilterGroup(group *FilterGroup) *Query
- func (q *Query) WithPaging(page, pageSize int) *Query
- type QueryCondition
- type RemarkableModel
- type Repository
- type RepositoryFactory
- type RepositoryOption
- func WithBatchSize[T any](size int) RepositoryOption[T]
- func WithDefaultOrder[T any](order string) RepositoryOption[T]
- func WithDefaultPreloads[T any](preloads ...string) RepositoryOption[T]
- func WithLogger[T any](log gologger.ILogger) RepositoryOption[T]
- func WithReadOnly[T any]() RepositoryOption[T]
- func WithTimeout[T any](seconds int) RepositoryOption[T]
- type RepositoryWithSoftDelete
- func (r *RepositoryWithSoftDelete[T]) ListDeleted(ctx context.Context, query *Query) ([]*T, error)
- func (r *RepositoryWithSoftDelete[T]) ListDeletedByIsDeleted(ctx context.Context, query *Query) ([]*T, error)
- func (r *RepositoryWithSoftDelete[T]) ListNotDeleted(ctx context.Context, query *Query) ([]*T, error)
- func (r *RepositoryWithSoftDelete[T]) ListNotDeletedByIsDeleted(ctx context.Context, query *Query) ([]*T, error)
- func (r *RepositoryWithSoftDelete[T]) RestoreBatchWithDeletedAt(ctx context.Context, ids []interface{}) error
- func (r *RepositoryWithSoftDelete[T]) RestoreBatchWithIsDeleted(ctx context.Context, ids []interface{}) error
- func (r *RepositoryWithSoftDelete[T]) RestoreWithDeletedAt(ctx context.Context, id interface{}) error
- func (r *RepositoryWithSoftDelete[T]) RestoreWithIsDeleted(ctx context.Context, id interface{}) error
- func (r *RepositoryWithSoftDelete[T]) SoftDeleteBatchWithDeletedAt(ctx context.Context, ids []interface{}) error
- func (r *RepositoryWithSoftDelete[T]) SoftDeleteBatchWithIsDeleted(ctx context.Context, ids []interface{}) error
- func (r *RepositoryWithSoftDelete[T]) SoftDeleteByFiltersWithDeletedAt(ctx context.Context, filters ...*Filter) error
- func (r *RepositoryWithSoftDelete[T]) SoftDeleteByFiltersWithIsDeleted(ctx context.Context, filters ...*Filter) error
- func (r *RepositoryWithSoftDelete[T]) SoftDeleteWithDeletedAt(ctx context.Context, id interface{}) error
- func (r *RepositoryWithSoftDelete[T]) SoftDeleteWithIsDeleted(ctx context.Context, id interface{}) error
- type SimpleModel
- type SoftDeletableModel
- type SoftDeleteHelper
- type StatusModel
- type TimestampModel
- type Transaction
- type UUIDModel
- type VersionedModel
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConvertToInterfaceSlice ¶ added in v0.3.1
func ConvertToInterfaceSlice(value interface{}) []interface{}
ConvertToInterfaceSlice 将任意切片类型转换为 []interface{}
func GetDeleted ¶ added in v0.1.7
GetDeleted 获取已删除的记录(使用deleted_at字段)
func GetNonDeleted ¶ added in v0.1.7
GetNonDeleted 获取未删除的记录(使用deleted_at字段)
func GetTodayRange ¶ added in v0.3.1
GetTodayRange 获取今天的时间范围(00:00:00 - 23:59:59)
func IsTodayFromProto ¶ added in v0.3.1
func IsTodayFromProto(t *timestamppb.Timestamp) bool
IsTodayFromProto 判断 protobuf Timestamp 是否为今天
func IsTodayRange ¶ added in v0.3.1
IsTodayRange 判断时间范围是否包含今天
func PermanentlyDelete ¶ added in v0.1.7
PermanentlyDelete 永久删除记录(从数据库中完全删除)
func PermanentlyDeleteBatch ¶ added in v0.1.7
PermanentlyDeleteBatch 批量永久删除记录(从数据库中完全删除)
func RestoreDeleted ¶ added in v0.1.7
RestoreDeleted 恢复单个已删除记录(将deleted_at设为NULL)
Types ¶
type AuditModel ¶ added in v0.1.2
type AuditModel struct {
BaseModel
CreatedBy uint `json:"created_by,omitempty" gorm:"index;comment:创建人ID"`
UpdatedBy uint `json:"updated_by,omitempty" gorm:"index;comment:更新人ID"`
}
AuditModel 审计模型(包含创建人和更新人信息) 适用于需要追踪操作人的业务场景
func (*AuditModel) GetCreatedBy ¶ added in v0.1.2
func (m *AuditModel) GetCreatedBy() uint
func (*AuditModel) GetUpdatedBy ¶ added in v0.1.2
func (m *AuditModel) GetUpdatedBy() uint
func (*AuditModel) SetCreatedBy ¶ added in v0.1.2
func (m *AuditModel) SetCreatedBy(userID uint)
func (*AuditModel) SetUpdatedBy ¶ added in v0.1.2
func (m *AuditModel) SetUpdatedBy(userID uint)
type AuditableModel ¶ added in v0.1.2
type AuditableModel interface {
ModelInterface
SetCreatedBy(userID uint)
SetUpdatedBy(userID uint)
GetCreatedBy() uint
GetUpdatedBy() uint
}
AuditableModel 支持审计的模型接口
type BaseModel ¶ added in v0.1.2
type BaseModel struct {
ID uint `json:"id" gorm:"primaryKey;autoIncrement;comment:自增主键"`
Version int `json:"version" gorm:"default:1;comment:版本号"`
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime;comment:创建时间"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime;comment:更新时间"`
DeletedAt gorm.DeletedAt `json:"deleted_at,omitempty" gorm:"index;comment:删除时间"`
Remark string `json:"remark,omitempty" gorm:"type:varchar(500);comment:备注"`
Status int8 `json:"status" gorm:"default:1;index;comment:状态(1:启用 0:禁用)"`
}
BaseModel 公共基础模型(带软删除、状态管理、版本控制)
func (*BaseModel) BeforeUpdate ¶ added in v0.1.2
BeforeUpdate GORM更新前钩子
func (*BaseModel) GetVersion ¶ added in v0.1.2
GetVersion 获取版本号
func (*BaseModel) SetCreatedAt ¶ added in v0.1.2
func (*BaseModel) SetUpdatedAt ¶ added in v0.1.2
type BaseRepository ¶
type BaseRepository[T any] struct { // contains filtered or unexported fields }
BaseRepository 基础仓储实现,包含通用的 CRUD 操作
func NewBaseRepository ¶
func NewBaseRepository[T any](dbHandler db.Handler, logger gologger.ILogger, table string, options ...RepositoryOption[T]) *BaseRepository[T]
NewBaseRepository 创建基础仓储
func (*BaseRepository[T]) BulkCreate ¶ added in v0.1.7
func (r *BaseRepository[T]) BulkCreate(ctx context.Context, entities []*T, batchSize ...int) error
BulkCreate 高性能批量创建
func (*BaseRepository[T]) CountByField ¶ added in v0.1.1
func (r *BaseRepository[T]) CountByField(ctx context.Context, field string) (map[interface{}]int64, error)
CountByField 按字段计数(GROUP BY)
func (*BaseRepository[T]) Create ¶
func (r *BaseRepository[T]) Create(ctx context.Context, entity *T) (*T, error)
Create 创建单个记录
func (*BaseRepository[T]) CreateBatch ¶
func (r *BaseRepository[T]) CreateBatch(ctx context.Context, entities ...*T) error
CreateBatch 批量创建记录
func (*BaseRepository[T]) CreateIfNotExists ¶ added in v0.1.7
func (r *BaseRepository[T]) CreateIfNotExists(ctx context.Context, entity *T, uniqueFields ...string) (*T, bool, error)
CreateIfNotExists 如果不存在则创建
func (*BaseRepository[T]) CreateOrUpdate ¶ added in v0.1.7
func (r *BaseRepository[T]) CreateOrUpdate(ctx context.Context, entity *T, uniqueFields ...string) (*T, bool, error)
CreateOrUpdate 创建或更新记录
func (*BaseRepository[T]) DBHandler ¶ added in v0.1.6
func (r *BaseRepository[T]) DBHandler() db.Handler
DBHandler 获取数据库处理器
func (*BaseRepository[T]) Delete ¶
func (r *BaseRepository[T]) Delete(ctx context.Context, id interface{}) error
Delete 删除单个记录
func (*BaseRepository[T]) DeleteBatch ¶
func (r *BaseRepository[T]) DeleteBatch(ctx context.Context, ids ...interface{}) error
DeleteBatch 批量删除记录
func (*BaseRepository[T]) DeleteByFilters ¶
func (r *BaseRepository[T]) DeleteByFilters(ctx context.Context, filters ...*Filter) error
DeleteByFilters 按过滤条件删除记录
func (*BaseRepository[T]) Distinct ¶ added in v0.1.1
func (r *BaseRepository[T]) Distinct(ctx context.Context, field string, filters ...*Filter) ([]interface{}, error)
Distinct 获取去重后的字段值列表
func (*BaseRepository[T]) Find ¶ added in v0.1.5
func (r *BaseRepository[T]) Find(ctx context.Context, options *FindOptions) ([]*T, error)
Find 通用查询方法,兼容旧的API调用方式
func (*BaseRepository[T]) FindOne ¶ added in v0.1.1
func (r *BaseRepository[T]) FindOne(ctx context.Context, filters ...*Filter) (*T, error)
FindOne 查找单条记录(不存在返回 nil)
func (*BaseRepository[T]) First ¶ added in v0.1.1
func (r *BaseRepository[T]) First(ctx context.Context, filters ...*Filter) (*T, error)
First 获取第一条记录
func (*BaseRepository[T]) Get ¶
func (r *BaseRepository[T]) Get(ctx context.Context, id interface{}) (*T, error)
Get 获取单个记录
func (*BaseRepository[T]) GetAll ¶ added in v0.1.1
func (r *BaseRepository[T]) GetAll(ctx context.Context) ([]*T, error)
GetAll 获取所有记录(不分页)
func (*BaseRepository[T]) GetByFields ¶ added in v0.1.7
func (r *BaseRepository[T]) GetByFields(ctx context.Context, fields map[string]interface{}) (*T, error)
GetByFields 根据多个字段获取记录
func (*BaseRepository[T]) GetByFilter ¶
func (r *BaseRepository[T]) GetByFilter(ctx context.Context, filter *Filter) (*T, error)
GetByFilter 按单个过滤条件获取记录
func (*BaseRepository[T]) GetByFilters ¶
func (r *BaseRepository[T]) GetByFilters(ctx context.Context, filters ...*Filter) (*T, error)
GetByFilters 按多个过滤条件获取记录
func (*BaseRepository[T]) GetWithPreloads ¶ added in v0.1.7
func (r *BaseRepository[T]) GetWithPreloads(ctx context.Context, id interface{}, preloads ...string) (*T, error)
GetWithPreloads 获取单个记录并指定预加载关联
func (*BaseRepository[T]) Last ¶ added in v0.1.1
func (r *BaseRepository[T]) Last(ctx context.Context, filters ...*Filter) (*T, error)
Last 获取最后一条记录
func (*BaseRepository[T]) List ¶
func (r *BaseRepository[T]) List(ctx context.Context, query *Query) ([]*T, error)
List 列表查询
func (*BaseRepository[T]) ListWithPagination ¶
func (r *BaseRepository[T]) ListWithPagination(ctx context.Context, query *Query, page *Pagination) ([]*T, *Pagination, error)
ListWithPagination 分页列表查询
func (*BaseRepository[T]) ListWithPreloads ¶ added in v0.1.7
func (r *BaseRepository[T]) ListWithPreloads(ctx context.Context, query *Query, preloads ...string) ([]*T, error)
ListWithPreloads 列表查询并指定预加载关联
func (*BaseRepository[T]) Pluck ¶ added in v0.1.1
func (r *BaseRepository[T]) Pluck(ctx context.Context, field string, filters ...*Filter) ([]interface{}, error)
Pluck 提取单个字段的值列表
func (*BaseRepository[T]) Restore ¶ added in v0.1.1
func (r *BaseRepository[T]) Restore(ctx context.Context, id interface{}, field string, restoreValue interface{}) error
Restore 恢复软删除的记录 field: 软删除字段名,如 "deleted_at", "is_deleted" 等 restoreValue: 恢复时的值,如 nil, 0 等
func (*BaseRepository[T]) RestoreBatch ¶ added in v0.1.1
func (r *BaseRepository[T]) RestoreBatch(ctx context.Context, ids []interface{}, field string, restoreValue interface{}) error
RestoreBatch 批量恢复软删除的记录 field: 软删除字段名,如 "deleted_at", "is_deleted" 等 restoreValue: 恢复时的值,如 nil, 0 等
func (*BaseRepository[T]) SoftDelete ¶ added in v0.1.1
func (r *BaseRepository[T]) SoftDelete(ctx context.Context, id interface{}, field string, value interface{}) error
SoftDelete 软删除(需要指定删除标记字段和值) field: 软删除字段名,如 "deleted_at", "is_deleted" 等 value: 软删除标记值,如 time.Now(), 1 等
func (*BaseRepository[T]) SoftDeleteBatch ¶ added in v0.1.1
func (r *BaseRepository[T]) SoftDeleteBatch(ctx context.Context, ids []interface{}, field string, value interface{}) error
SoftDeleteBatch 批量软删除 field: 软删除字段名,如 "deleted_at", "is_deleted" 等 value: 软删除标记值,如 time.Now(), 1 等
func (*BaseRepository[T]) SoftDeleteByFilters ¶ added in v0.1.1
func (r *BaseRepository[T]) SoftDeleteByFilters(ctx context.Context, field string, value interface{}, filters ...*Filter) error
SoftDeleteByFilters 按过滤条件软删除 field: 软删除字段名,如 "deleted_at", "is_deleted" 等 value: 软删除标记值,如 time.Now(), 1 等
func (*BaseRepository[T]) Table ¶ added in v0.1.1
func (r *BaseRepository[T]) Table() string
Table 获取表名
func (*BaseRepository[T]) Transaction ¶
func (r *BaseRepository[T]) Transaction(ctx context.Context, fn func(tx Transaction[T]) error) error
Transaction 事务支持
func (*BaseRepository[T]) TransactionWithRawDB ¶ added in v0.3.2
func (r *BaseRepository[T]) TransactionWithRawDB(ctx context.Context, fn func(tx *gorm.DB) error) error
TransactionWithRawDB 通用事务支持(可以操作任意模型)
func (*BaseRepository[T]) Update ¶
func (r *BaseRepository[T]) Update(ctx context.Context, entity *T) (*T, error)
Update 更新单个记录
func (*BaseRepository[T]) UpdateBatch ¶
func (r *BaseRepository[T]) UpdateBatch(ctx context.Context, entities ...*T) error
UpdateBatch 批量更新记录
func (*BaseRepository[T]) UpdateByFilters ¶
func (r *BaseRepository[T]) UpdateByFilters(ctx context.Context, entity *T, filters ...*Filter) error
UpdateByFilters 按过滤条件更新记录
func (*BaseRepository[T]) UpdateFields ¶ added in v0.1.1
func (r *BaseRepository[T]) UpdateFields(ctx context.Context, id interface{}, fields map[string]interface{}) error
UpdateFields 更新指定字段
func (*BaseRepository[T]) UpdateFieldsByFilters ¶ added in v0.1.1
func (r *BaseRepository[T]) UpdateFieldsByFilters(ctx context.Context, fields map[string]interface{}, filters ...*Filter) error
UpdateFieldsByFilters 按过滤条件更新指定字段
type ComparedColumn ¶ added in v0.1.7
type ComparedValue ¶ added in v0.1.7
type ComparedValue struct {
Value interface{}
IsRaw bool
}
type ContextFieldExtractor ¶ added in v0.1.7
ContextFieldExtractor context字段提取器函数类型 用于从context中提取需要记录到日志的字段
type EnhancedRepository ¶ added in v0.1.3
type EnhancedRepository[T any] struct { *BaseRepository[T] // contains filtered or unexported fields }
EnhancedRepository 增强版仓储实现
func NewEnhancedRepository ¶ added in v0.1.3
func NewEnhancedRepository[T any](dbHandler db.Handler, logger gologger.ILogger, tableName string) *EnhancedRepository[T]
NewEnhancedRepository 创建增强版仓储实例
func NewEnhancedRepositoryWithDB ¶ added in v0.1.3
func NewEnhancedRepositoryWithDB[T any](gormDB *gorm.DB, logger gologger.ILogger, tableName string) *EnhancedRepository[T]
NewEnhancedRepositoryWithDB 使用GORM DB直接创建增强版仓储
func (*EnhancedRepository[T]) BatchUpdateByField ¶ added in v0.1.3
func (r *EnhancedRepository[T]) BatchUpdateByField(ctx context.Context, field string, values []interface{}, updates map[string]interface{}) error
BatchUpdateByField 根据字段批量更新
func (*EnhancedRepository[T]) CountByField ¶ added in v0.1.3
func (r *EnhancedRepository[T]) CountByField(ctx context.Context, field string, value interface{}) (int64, error)
CountByField 根据字段统计数量
func (*EnhancedRepository[T]) CreateIfNotExists ¶ added in v0.1.3
func (r *EnhancedRepository[T]) CreateIfNotExists(ctx context.Context, entity *T, checkField string) (*T, bool, error)
CreateIfNotExists 如果不存在则创建
func (*EnhancedRepository[T]) DecrementField ¶ added in v0.1.3
func (r *EnhancedRepository[T]) DecrementField(ctx context.Context, whereField string, whereValue interface{}, decrementField string, step int64) error
DecrementField 字段自减
func (*EnhancedRepository[T]) DeleteByField ¶ added in v0.1.3
func (r *EnhancedRepository[T]) DeleteByField(ctx context.Context, field string, value interface{}) error
DeleteByField 根据字段删除记录
func (*EnhancedRepository[T]) ExistsBy ¶ added in v0.1.3
func (r *EnhancedRepository[T]) ExistsBy(ctx context.Context, field string, value interface{}) (bool, error)
ExistsBy 检查记录是否存在
func (*EnhancedRepository[T]) FindByField ¶ added in v0.1.3
func (r *EnhancedRepository[T]) FindByField(ctx context.Context, field string, value interface{}) ([]*T, error)
FindByField 根据单个字段查找记录
func (*EnhancedRepository[T]) FindByFieldWithCursor ¶ added in v0.1.3
func (r *EnhancedRepository[T]) FindByFieldWithCursor(ctx context.Context, field string, value interface{}, cursorField string, lastCursor interface{}, limit int) ([]*T, bool, error)
FindByFieldWithCursor 根据字段查找记录(游标分页)
func (*EnhancedRepository[T]) FindByFieldWithPagination ¶ added in v0.1.3
func (r *EnhancedRepository[T]) FindByFieldWithPagination(ctx context.Context, field string, value interface{}, limit, offset int) ([]*T, int64, error)
FindByFieldWithPagination 根据字段查找记录(带分页)
func (*EnhancedRepository[T]) FindByFields ¶ added in v0.1.3
func (r *EnhancedRepository[T]) FindByFields(ctx context.Context, conditions map[string]interface{}) ([]*T, error)
FindByFields 根据多个字段查找记录
func (*EnhancedRepository[T]) FindByTimeRange ¶ added in v0.1.3
func (r *EnhancedRepository[T]) FindByTimeRange(ctx context.Context, timeField string, startTime, endTime interface{}) ([]*T, error)
FindByTimeRange 根据时间范围查找
func (*EnhancedRepository[T]) FindInField ¶ added in v0.1.3
func (r *EnhancedRepository[T]) FindInField(ctx context.Context, field string, values []interface{}) ([]*T, error)
FindInField 根据字段的IN查询
func (*EnhancedRepository[T]) FindOneByField ¶ added in v0.1.3
func (r *EnhancedRepository[T]) FindOneByField(ctx context.Context, field string, value interface{}) (*T, error)
FindOneByField 根据单个字段查找单条记录
func (*EnhancedRepository[T]) FindWithOrder ¶ added in v0.1.3
func (r *EnhancedRepository[T]) FindWithOrder(ctx context.Context, whereField string, whereValue interface{}, orderField string, orderDirection string) ([]*T, error)
FindWithOrder 根据条件查找并排序
func (*EnhancedRepository[T]) GetDistinctValues ¶ added in v0.1.3
func (r *EnhancedRepository[T]) GetDistinctValues(ctx context.Context, field string) ([]interface{}, error)
GetDistinctValues 获取字段的不同值
func (*EnhancedRepository[T]) IncrementField ¶ added in v0.1.3
func (r *EnhancedRepository[T]) IncrementField(ctx context.Context, whereField string, whereValue interface{}, incrementField string, step int64) error
IncrementField 字段自增
func (*EnhancedRepository[T]) UpdateByField ¶ added in v0.1.3
func (r *EnhancedRepository[T]) UpdateByField(ctx context.Context, field string, value interface{}, updates map[string]interface{}) error
UpdateByField 根据字段更新记录
func (*EnhancedRepository[T]) UpdateSingleField ¶ added in v0.1.3
func (r *EnhancedRepository[T]) UpdateSingleField(ctx context.Context, whereField string, whereValue interface{}, updateField string, updateValue interface{}) error
UpdateSingleField 更新单个字段
type Filter ¶
type Filter struct {
Field string // 字段名
Operator constants.Operator // 操作符
Value interface{} // 值
}
Filter 过滤条件
func NewBetweenFilter ¶
NewBetweenFilter 创建 BETWEEN 过滤条件
func NewContainsFilter ¶ added in v0.1.1
NewContainsFilter 创建包含匹配过滤条件(与 NewLikeFilter 相同)
func NewEndsWithFilter ¶ added in v0.1.1
NewEndsWithFilter 创建后缀匹配过滤条件
func NewFindInSetFilter ¶ added in v0.1.7
NewFindInSetFilter 创建 FIND_IN_SET 过滤条件(MySQL特定)
func NewGteFilter ¶
NewGteFilter 创建大于等于过滤条件
func NewInFilter ¶
NewInFilter 创建 IN 过滤条件
func NewInFilterSlice ¶ added in v0.3.1
NewInFilterSlice 创建 IN 过滤条件(使用切片参数)
func NewIsNotNullFilter ¶ added in v0.1.1
NewIsNotNullFilter 创建 IS NOT NULL 过滤条件
func NewIsNullFilter ¶ added in v0.1.1
NewIsNullFilter 创建 IS NULL 过滤条件
func NewLikeFilter ¶
NewLikeFilter 创建 LIKE 过滤条件
func NewLteFilter ¶
NewLteFilter 创建小于等于过滤条件
func NewNeqFilter ¶
NewNeqFilter 创建不等于过滤条件
func NewNotInFilter ¶ added in v0.1.1
NewNotInFilter 创建 NOT IN 过滤条件
func NewNotInFilterSlice ¶ added in v0.3.1
NewNotInFilterSlice 创建 NOT IN 过滤条件(使用切片参数)
func NewNotLikeFilter ¶ added in v0.1.7
NewNotLikeFilter 创建 NOT LIKE 过滤条件
func NewStartsWithFilter ¶ added in v0.1.1
NewStartsWithFilter 创建前缀匹配过滤条件
type FilterGroup ¶ added in v0.1.7
type FilterGroup struct {
Filters []*Filter // 过滤条件列表
Groups []*FilterGroup // 嵌套条件组
LogicOp constants.Operator // 逻辑操作符:AND/OR
}
FilterGroup 过滤条件组,支持逻辑操作
func NewFilterGroup ¶ added in v0.1.7
func NewFilterGroup(logicOp constants.Operator) *FilterGroup
NewFilterGroup 创建新的过滤条件组
func (*FilterGroup) AddFilter ¶ added in v0.1.7
func (fg *FilterGroup) AddFilter(filter *Filter) *FilterGroup
AddFilter 向条件组添加过滤条件
func (*FilterGroup) AddFilters ¶ added in v0.1.7
func (fg *FilterGroup) AddFilters(filters ...*Filter) *FilterGroup
AddFilters 向条件组批量添加过滤条件
func (*FilterGroup) AddGroup ¶ added in v0.1.7
func (fg *FilterGroup) AddGroup(group *FilterGroup) *FilterGroup
AddGroup 向条件组添加嵌套条件组
func (*FilterGroup) IsEmpty ¶ added in v0.1.7
func (fg *FilterGroup) IsEmpty() bool
IsEmpty 检查条件组是否为空
type FindOptions ¶ added in v0.1.5
FindOptions 兼容旧API的查询选项结构
type FullFeaturedModel ¶ added in v0.1.2
type FullFeaturedModel interface {
ModelInterface
VersionedModel
SoftDeletableModel
AuditableModel
StatusModel
RemarkableModel
}
FullFeaturedModel 全功能模型接口(组合所有特性)
type LightModel ¶ added in v0.1.2
type LightModel struct {
ID uint `json:"id" gorm:"primaryKey;autoIncrement;comment:自增主键"`
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime;comment:创建时间"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime;comment:更新时间"`
Status int8 `json:"status" gorm:"default:1;index;comment:状态(1:启用 0:禁用)"`
}
LightModel 轻量级模型(仅包含基本字段,无软删除、无版本控制)
func (*LightModel) Disable ¶ added in v0.1.2
func (m *LightModel) Disable()
func (*LightModel) Enable ¶ added in v0.1.2
func (m *LightModel) Enable()
func (*LightModel) GetID ¶ added in v0.1.2
func (m *LightModel) GetID() uint
func (*LightModel) IsEnabled ¶ added in v0.1.2
func (m *LightModel) IsEnabled() bool
func (*LightModel) IsNew ¶ added in v0.1.2
func (m *LightModel) IsNew() bool
type ModelInterface ¶ added in v0.1.2
type ModelInterface interface {
IsNew() bool
}
ModelInterface 模型通用接口
type Pagination ¶ added in v0.1.7
type Pagination struct {
Page int32 `json:"page"` // 当前页码(从1开始)
PageSize int32 `json:"page_size"` // 每页记录数
Offset int32 `json:"offset"` // 数据库偏移量
Limit int32 `json:"limit"` // 查询限制数
Total int64 `json:"total"` // 总记录数
}
Pagination 分页元数据
func (*Pagination) GetLimit ¶ added in v0.1.7
func (p *Pagination) GetLimit() int
GetLimit 获取查询限制数(自动应用默认值和最大值限制)
func (*Pagination) GetOffset ¶ added in v0.1.7
func (p *Pagination) GetOffset() int
GetOffset 计算数据库偏移量
func (*Pagination) GetTotalPages ¶ added in v0.1.7
func (p *Pagination) GetTotalPages() int64
GetTotalPages 计算总页数
func (*Pagination) HasNextPage ¶ added in v0.1.7
func (p *Pagination) HasNextPage() bool
HasNextPage 是否有下一页
func (*Pagination) HasPrevPage ¶ added in v0.1.7
func (p *Pagination) HasPrevPage() bool
HasPrevPage 是否有上一页
type Query ¶
type Query struct {
Filters []*Filter // 简单过滤条件
FilterGroup *FilterGroup // 复合过滤条件组
Orders []Order // 排序条件
Pagination *Pagination // 分页信息
LimitValue *int // 限制数量
OffsetValue *int // 偏移量
Distinct bool // 是否去重
GroupBy []string // 分组字段
Having []*Filter // HAVING 条件
}
Query 查询条件
func (*Query) AddBetween ¶ added in v0.3.1
AddBetween 添加BETWEEN条件
func (*Query) AddEndsWith ¶ added in v0.3.1
AddEndsWith 添加后缀匹配条件
func (*Query) AddFilterIfNotEmpty ¶ added in v0.3.1
AddFilterIfNotEmpty 添加过滤条件(仅当值不为空时) 支持泛型自动处理不同类型的值
func (*Query) AddFilters ¶
AddFilters 批量添加过滤条件
func (*Query) AddGreaterEqual ¶ added in v0.3.1
AddGreaterEqual 添加大于等于条件
func (*Query) AddGreaterThan ¶ added in v0.3.1
AddGreaterThan 添加大于条件
func (*Query) AddGroupBy ¶ added in v0.1.7
AddGroupBy 添加分组字段
func (*Query) AddInFilterIfNotEmpty ¶ added in v0.3.1
AddInFilterIfNotEmpty 添加 IN 过滤条件(仅当切片不为空时)
func (*Query) AddIsNotNull ¶ added in v0.3.1
AddIsNotNull 添加IS NOT NULL条件
func (*Query) AddLessEqual ¶ added in v0.3.1
AddLessEqual 添加小于等于条件
func (*Query) AddLessThan ¶ added in v0.3.1
AddLessThan 添加小于条件
func (*Query) AddLikeFilterIfNotEmpty ¶ added in v0.3.1
AddLikeFilterIfNotEmpty 添加 LIKE 过滤条件(仅当关键词不为空时)
func (*Query) AddNotEqual ¶ added in v0.3.1
AddNotEqual 添加不等于条件
func (*Query) AddOrderAsc ¶ added in v0.3.1
AddOrderAsc 添加升序排序
func (*Query) AddOrderDesc ¶ added in v0.3.1
AddOrderDesc 添加降序排序
func (*Query) AddSafeOrder ¶ added in v0.3.1
func (q *Query) AddSafeOrder(sortBy, sortOrder, defaultField, defaultDirection string, allowedFields ...[]string) *Query
AddSafeOrder 安全地添加排序条件 参数:
- sortBy: 排序字段(可选,为空时使用defaultField)
- sortOrder: 排序方向(可选,仅支持"ASC"/"DESC",为空时使用defaultDirection)
- defaultField: 默认排序字段
- defaultDirection: 默认排序方向
- allowedFields: 允许的字段白名单(可选,为空则不限制)
示例:
query.AddSafeOrder(filter.SortBy, filter.SortOrder, "created_at", constants.Desc, []string{"created_at", "updated_at", "id"})
func (*Query) AddStartsWith ¶ added in v0.3.1
AddStartsWith 添加前缀匹配条件
func (*Query) AddThisMonth ¶ added in v0.3.1
AddThisMonth 添加本月条件
func (*Query) AddThisWeek ¶ added in v0.3.1
AddThisWeek 添加本周条件
func (*Query) AddThisYear ¶ added in v0.3.1
AddThisYear 添加今年条件
func (*Query) AddTimeAfter ¶ added in v0.3.1
AddTimeAfter 添加时间晚于条件
func (*Query) AddTimeBefore ¶ added in v0.3.1
AddTimeBefore 添加时间早于条件
func (*Query) AddTimeBetween ¶ added in v0.3.1
AddTimeBetween 添加时间范围条件
func (*Query) AddTimeRangeFilter ¶ added in v0.3.1
AddTimeRangeFilter 添加时间范围过滤条件 自动过滤掉nil和零值时间,避免生成无效的SQL条件
func (*Query) GetAllFilters ¶ added in v0.1.7
GetAllFilters 获取所有过滤条件(扁平化)
func (*Query) SetDistinct ¶ added in v0.3.1
SetDistinct 设置去重(简化版)
func (*Query) WithDistinct ¶ added in v0.1.7
WithDistinct 设置去重查询
func (*Query) WithFilterGroup ¶ added in v0.1.7
func (q *Query) WithFilterGroup(group *FilterGroup) *Query
WithFilterGroup 设置复合过滤条件组
type QueryCondition ¶ added in v0.1.7
type QueryCondition struct {
Filters []Filter
Orders []OrderBy
Pagination *Pagination
}
type RemarkableModel ¶ added in v0.1.2
type RemarkableModel interface {
ModelInterface
SetRemark(remark string)
}
RemarkableModel 支持备注的模型接口
type Repository ¶
type Repository[T any] interface { // 创建 Create(ctx context.Context, entity *T) (*T, error) CreateBatch(ctx context.Context, entities ...*T) error // 读取 Get(ctx context.Context, id interface{}) (*T, error) GetByFilter(ctx context.Context, filter *Filter) (*T, error) GetByFilters(ctx context.Context, filters ...*Filter) (*T, error) GetAll(ctx context.Context) ([]*T, error) First(ctx context.Context, filters ...*Filter) (*T, error) Last(ctx context.Context, filters ...*Filter) (*T, error) FindOne(ctx context.Context, filters ...*Filter) (*T, error) List(ctx context.Context, query *Query) ([]*T, error) ListWithPagination(ctx context.Context, query *Query, page *Pagination) ([]*T, *Pagination, error) // 更新 Update(ctx context.Context, entity *T) (*T, error) UpdateBatch(ctx context.Context, entities ...*T) error UpdateByFilters(ctx context.Context, entity *T, filters ...*Filter) error UpdateFields(ctx context.Context, id interface{}, fields map[string]interface{}) error UpdateFieldsByFilters(ctx context.Context, fields map[string]interface{}, filters ...*Filter) error // 删除 Delete(ctx context.Context, id interface{}) error DeleteBatch(ctx context.Context, ids ...interface{}) error DeleteByFilters(ctx context.Context, filters ...*Filter) error SoftDelete(ctx context.Context, id interface{}, field string, value interface{}) error SoftDeleteBatch(ctx context.Context, ids []interface{}, field string, value interface{}) error SoftDeleteByFilters(ctx context.Context, field string, value interface{}, filters ...*Filter) error Restore(ctx context.Context, id interface{}, field string, restoreValue interface{}) error RestoreBatch(ctx context.Context, ids []interface{}, field string, restoreValue interface{}) error // 事务 Transaction(ctx context.Context, fn func(tx Transaction[T]) error) error TransactionWithRawDB(ctx context.Context, fn func(tx *gorm.DB) error) error // 工具方法 Count(ctx context.Context, filters ...*Filter) (int64, error) Exists(ctx context.Context, filters ...*Filter) (bool, error) CountByField(ctx context.Context, field string) (map[interface{}]int64, error) Pluck(ctx context.Context, field string, filters ...*Filter) ([]interface{}, error) Distinct(ctx context.Context, field string, filters ...*Filter) ([]interface{}, error) }
Repository 通用仓储接口
type RepositoryFactory ¶
type RepositoryFactory[T any] interface { // 创建仓储 CreateRepository(table string) Repository[T] // 关闭工厂 Close() error }
RepositoryFactory 仓储工厂接口
type RepositoryOption ¶ added in v0.1.7
type RepositoryOption[T any] func(*BaseRepository[T])
RepositoryOption 仓储配置选项
func WithBatchSize ¶ added in v0.1.7
func WithBatchSize[T any](size int) RepositoryOption[T]
WithBatchSize 设置批处理大小
func WithDefaultOrder ¶ added in v0.1.7
func WithDefaultOrder[T any](order string) RepositoryOption[T]
WithDefaultOrder 设置默认排序
func WithDefaultPreloads ¶ added in v0.1.7
func WithDefaultPreloads[T any](preloads ...string) RepositoryOption[T]
WithDefaultPreloads 设置默认预加载关联
func WithLogger ¶ added in v0.1.7
func WithLogger[T any](log gologger.ILogger) RepositoryOption[T]
WithLogger 设置日志记录器
func WithReadOnly ¶ added in v0.1.7
func WithReadOnly[T any]() RepositoryOption[T]
WithReadOnly 设置为只读模式
func WithTimeout ¶ added in v0.1.7
func WithTimeout[T any](seconds int) RepositoryOption[T]
WithTimeout 设置查询超时时间
type RepositoryWithSoftDelete ¶ added in v0.1.1
type RepositoryWithSoftDelete[T any] struct { *BaseRepository[T] }
RepositoryWithSoftDelete 带软删除功能的仓储
func NewRepositoryWithSoftDelete ¶ added in v0.1.1
func NewRepositoryWithSoftDelete[T any](repo *BaseRepository[T]) *RepositoryWithSoftDelete[T]
NewRepositoryWithSoftDelete 创建带软删除功能的仓储
func (*RepositoryWithSoftDelete[T]) ListDeleted ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) ListDeleted(ctx context.Context, query *Query) ([]*T, error)
ListDeleted 查询已删除的记录(deleted_at 字段)
func (*RepositoryWithSoftDelete[T]) ListDeletedByIsDeleted ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) ListDeletedByIsDeleted(ctx context.Context, query *Query) ([]*T, error)
ListDeletedByIsDeleted 查询已删除的记录(is_deleted 字段)
func (*RepositoryWithSoftDelete[T]) ListNotDeleted ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) ListNotDeleted(ctx context.Context, query *Query) ([]*T, error)
ListNotDeleted 查询未删除的记录(deleted_at 字段)
func (*RepositoryWithSoftDelete[T]) ListNotDeletedByIsDeleted ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) ListNotDeletedByIsDeleted(ctx context.Context, query *Query) ([]*T, error)
ListNotDeletedByIsDeleted 查询未删除的记录(is_deleted 字段)
func (*RepositoryWithSoftDelete[T]) RestoreBatchWithDeletedAt ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) RestoreBatchWithDeletedAt(ctx context.Context, ids []interface{}) error
RestoreBatchWithDeletedAt 使用 deleted_at 字段批量恢复
func (*RepositoryWithSoftDelete[T]) RestoreBatchWithIsDeleted ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) RestoreBatchWithIsDeleted(ctx context.Context, ids []interface{}) error
RestoreBatchWithIsDeleted 使用 is_deleted 字段批量恢复
func (*RepositoryWithSoftDelete[T]) RestoreWithDeletedAt ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) RestoreWithDeletedAt(ctx context.Context, id interface{}) error
RestoreWithDeletedAt 使用 deleted_at 字段恢复
func (*RepositoryWithSoftDelete[T]) RestoreWithIsDeleted ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) RestoreWithIsDeleted(ctx context.Context, id interface{}) error
RestoreWithIsDeleted 使用 is_deleted 字段恢复
func (*RepositoryWithSoftDelete[T]) SoftDeleteBatchWithDeletedAt ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) SoftDeleteBatchWithDeletedAt(ctx context.Context, ids []interface{}) error
SoftDeleteBatchWithDeletedAt 使用 deleted_at 字段批量软删除
func (*RepositoryWithSoftDelete[T]) SoftDeleteBatchWithIsDeleted ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) SoftDeleteBatchWithIsDeleted(ctx context.Context, ids []interface{}) error
SoftDeleteBatchWithIsDeleted 使用 is_deleted 字段批量软删除
func (*RepositoryWithSoftDelete[T]) SoftDeleteByFiltersWithDeletedAt ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) SoftDeleteByFiltersWithDeletedAt(ctx context.Context, filters ...*Filter) error
SoftDeleteByFiltersWithDeletedAt 使用 deleted_at 字段按条件软删除
func (*RepositoryWithSoftDelete[T]) SoftDeleteByFiltersWithIsDeleted ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) SoftDeleteByFiltersWithIsDeleted(ctx context.Context, filters ...*Filter) error
SoftDeleteByFiltersWithIsDeleted 使用 is_deleted 字段按条件软删除
func (*RepositoryWithSoftDelete[T]) SoftDeleteWithDeletedAt ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) SoftDeleteWithDeletedAt(ctx context.Context, id interface{}) error
SoftDeleteWithDeletedAt 使用 deleted_at 字段软删除
func (*RepositoryWithSoftDelete[T]) SoftDeleteWithIsDeleted ¶ added in v0.1.1
func (r *RepositoryWithSoftDelete[T]) SoftDeleteWithIsDeleted(ctx context.Context, id interface{}) error
SoftDeleteWithIsDeleted 使用 is_deleted 字段软删除
type SimpleModel ¶ added in v0.1.2
type SimpleModel struct {
ID uint `json:"id" gorm:"primaryKey;autoIncrement;comment:自增主键"`
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime;comment:创建时间"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime;comment:更新时间"`
}
SimpleModel 简化模型(不带软删除)
func (*SimpleModel) GetID ¶ added in v0.1.2
func (m *SimpleModel) GetID() uint
func (*SimpleModel) IsNew ¶ added in v0.1.2
func (m *SimpleModel) IsNew() bool
type SoftDeletableModel ¶ added in v0.1.2
type SoftDeletableModel interface {
ModelInterface
IsDeleted() bool
}
SoftDeletableModel 支持软删除的模型接口
type SoftDeleteHelper ¶ added in v0.1.1
type SoftDeleteHelper[T any] interface { // 使用 deleted_at 字段的软删除 SoftDeleteWithDeletedAt(ctx context.Context, id interface{}) error SoftDeleteBatchWithDeletedAt(ctx context.Context, ids []interface{}) error SoftDeleteByFiltersWithDeletedAt(ctx context.Context, filters ...*Filter) error RestoreWithDeletedAt(ctx context.Context, id interface{}) error RestoreBatchWithDeletedAt(ctx context.Context, ids []interface{}) error // 使用 is_deleted 字段的软删除 SoftDeleteWithIsDeleted(ctx context.Context, id interface{}) error SoftDeleteBatchWithIsDeleted(ctx context.Context, ids []interface{}) error SoftDeleteByFiltersWithIsDeleted(ctx context.Context, filters ...*Filter) error RestoreWithIsDeleted(ctx context.Context, id interface{}) error RestoreBatchWithIsDeleted(ctx context.Context, ids []interface{}) error }
SoftDeleteHelper 软删除辅助接口
type StatusModel ¶ added in v0.1.2
type StatusModel interface {
ModelInterface
Enable()
Disable()
IsEnabled() bool
}
StatusModel 支持状态管理的模型接口
type TimestampModel ¶ added in v0.1.2
type TimestampModel struct {
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime;comment:创建时间"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime;comment:更新时间"`
}
TimestampModel 仅包含时间戳的模型
type Transaction ¶
type Transaction[T any] interface { // 执行事务内的操作 Create(ctx context.Context, entity *T) error CreateBatch(ctx context.Context, entities ...*T) error Update(ctx context.Context, entity *T) error UpdateBatch(ctx context.Context, entities ...*T) error Delete(ctx context.Context, entity *T) error DeleteBatch(ctx context.Context, entities ...*T) error }
Transaction 事务接口
type UUIDModel ¶ added in v0.1.2
type UUIDModel struct {
ID string `json:"id" gorm:"primaryKey;type:char(36);comment:UUID主键"`
Version int `json:"version" gorm:"default:1;comment:版本号"`
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime;comment:创建时间"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime;comment:更新时间"`
DeletedAt gorm.DeletedAt `json:"deleted_at,omitempty" gorm:"index;comment:删除时间"`
}
UUIDModel UUID作为主键的模型
func (*UUIDModel) BeforeUpdate ¶ added in v0.1.2
func (*UUIDModel) GetVersion ¶ added in v0.1.7
type VersionedModel ¶ added in v0.1.2
type VersionedModel interface {
ModelInterface
GetVersion() int
}
VersionedModel 支持版本控制的模型接口