Documentation
¶
Overview ¶
* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 21:13:15 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-13 11:07:41 * @FilePath: \go-sqlbuilder\persist\query_param.go * @Description: * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-17 15:40:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-17 16:00:00 * @FilePath: \go-sqlbuilder\repository\cache.go * @Description: Repository 缓存支持 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-17 15:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-17 15:30:00 * @FilePath: \go-sqlbuilder\repository\helpers.go * @Description: 仓储辅助方法 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-10 01:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-11 09:20:54 * @FilePath: \go-sqlbuilder\repository\interfaces.go * @Description: * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 18:17:15 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-17 16:26:44 * @FilePath: \go-sqlbuilder\repository\model.go * @Description: 基础模型定义模型 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
Index ¶
- 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]) 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]) DB() 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]) 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]) GetByFilter(ctx context.Context, filter *Filter) (*T, error)
- func (r *BaseRepository[T]) GetByFilters(ctx context.Context, filters ...*Filter) (*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 *meta.Paging) ([]*T, *meta.Paging, 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) 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 CacheConfig
- type CacheManager
- type CachedRepository
- 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 NewGtFilter(field string, value interface{}) *Filter
- func NewGteFilter(field string, value interface{}) *Filter
- func NewInFilter(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 NewStartsWithFilter(field string, value string) *Filter
- type FullFeaturedModel
- type LightModel
- type ModelInterface
- type Order
- type Query
- type RemarkableModel
- type Repository
- type RepositoryFactory
- type RepositoryWithCache
- func (r *RepositoryWithCache[T]) CreateWithCache(ctx context.Context, entity *T) (*T, error)
- func (r *RepositoryWithCache[T]) DeleteWithCache(ctx context.Context, id interface{}) error
- func (r *RepositoryWithCache[T]) GetOrLoad(ctx context.Context, cacheKey string, ttl time.Duration, ...) (*T, error)
- func (r *RepositoryWithCache[T]) GetWithCache(ctx context.Context, id interface{}) (*T, error)
- func (r *RepositoryWithCache[T]) InvalidateCache(ctx context.Context, keys ...string) error
- func (r *RepositoryWithCache[T]) InvalidateCacheByEntities(ctx context.Context, entities ...*T) error
- func (r *RepositoryWithCache[T]) ListWithCache(ctx context.Context, cacheKey string, ttl time.Duration, query *Query) ([]*T, error)
- func (r *RepositoryWithCache[T]) UpdateWithCache(ctx context.Context, entity *T) (*T, error)
- 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 ¶
This section is empty.
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, table string) *BaseRepository[T]
NewBaseRepository 创建基础仓储
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]) DB ¶ added in v0.1.1
func (r *BaseRepository[T]) DB() db.Handler
DB 获取数据库处理器
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]) 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]) 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]) 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 *meta.Paging) ([]*T, *meta.Paging, error)
ListWithPagination 分页列表查询
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) error) error
Transaction 事务支持
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 CacheManager ¶
type CacheManager interface {
// 获取
Get(ctx context.Context, key string) (interface{}, error)
// 设置
Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
// 删除
Delete(ctx context.Context, keys ...string) error
// 删除匹配的键
DeletePattern(ctx context.Context, pattern string) error
// 检查存在
Exists(ctx context.Context, key string) (bool, error)
// 获取 TTL
TTL(ctx context.Context, key string) (time.Duration, error)
// 批量操作
BatchGet(ctx context.Context, keys ...string) (map[string]interface{}, error)
BatchSet(ctx context.Context, items map[string]interface{}, ttl time.Duration) error
BatchDelete(ctx context.Context, keys ...string) error
}
CacheManager 缓存管理接口
type CachedRepository ¶ added in v0.1.1
type CachedRepository[T cache.Cacheable] interface { Repository[T] // 带缓存的操作 GetWithCache(ctx context.Context, id interface{}) (*T, error) CreateWithCache(ctx context.Context, entity *T) (*T, error) UpdateWithCache(ctx context.Context, entity *T) (*T, error) DeleteWithCache(ctx context.Context, id interface{}) error ListWithCache(ctx context.Context, cacheKey string, ttl time.Duration, query *Query) ([]*T, error) GetOrLoad(ctx context.Context, cacheKey string, ttl time.Duration, loadFn func() (*T, error)) (*T, error) // 缓存管理 InvalidateCache(ctx context.Context, keys ...string) error InvalidateCacheByEntities(ctx context.Context, entities ...*T) error }
CachedRepository 带缓存的仓储接口
type Filter ¶
Filter 过滤条件(复用 core.Filter)
func NewBetweenFilter ¶
NewBetweenFilter 创建 BETWEEN 过滤条件
func NewContainsFilter ¶ added in v0.1.1
NewContainsFilter 创建包含匹配过滤条件(与 NewLikeFilter 相同)
func NewEndsWithFilter ¶ added in v0.1.1
NewEndsWithFilter 创建后缀匹配过滤条件
func NewGteFilter ¶
NewGteFilter 创建大于等于过滤条件
func NewInFilter ¶
NewInFilter 创建 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 NewStartsWithFilter ¶ added in v0.1.1
NewStartsWithFilter 创建前缀匹配过滤条件
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 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 *meta.Paging) ([]*T, *meta.Paging, 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) 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 interface {
// 缓存管理
CacheManager() CacheManager
// 关闭工厂
Close() error
}
RepositoryFactory 仓储工厂接口
type RepositoryWithCache ¶ added in v0.1.1
type RepositoryWithCache[T cache.Cacheable] struct { *BaseRepository[T] // contains filtered or unexported fields }
RepositoryWithCache 带缓存的仓储
func NewRepositoryWithCache ¶ added in v0.1.1
func NewRepositoryWithCache[T cache.Cacheable](repo *BaseRepository[T], cacheWrapper *cache.Wrapper) *RepositoryWithCache[T]
NewRepositoryWithCache 创建带缓存的仓储
func (*RepositoryWithCache[T]) CreateWithCache ¶ added in v0.1.1
func (r *RepositoryWithCache[T]) CreateWithCache(ctx context.Context, entity *T) (*T, error)
CreateWithCache 创建记录并删除相关缓存
func (*RepositoryWithCache[T]) DeleteWithCache ¶ added in v0.1.1
func (r *RepositoryWithCache[T]) DeleteWithCache(ctx context.Context, id interface{}) error
DeleteWithCache 删除记录并删除缓存
func (*RepositoryWithCache[T]) GetOrLoad ¶ added in v0.1.1
func (r *RepositoryWithCache[T]) GetOrLoad(ctx context.Context, cacheKey string, ttl time.Duration, loadFn func() (*T, error)) (*T, error)
GetOrLoad 获取缓存,如果不存在则执行加载函数
func (*RepositoryWithCache[T]) GetWithCache ¶ added in v0.1.1
func (r *RepositoryWithCache[T]) GetWithCache(ctx context.Context, id interface{}) (*T, error)
GetWithCache 获取记录,优先从缓存读取
func (*RepositoryWithCache[T]) InvalidateCache ¶ added in v0.1.1
func (r *RepositoryWithCache[T]) InvalidateCache(ctx context.Context, keys ...string) error
InvalidateCache 使缓存失效
func (*RepositoryWithCache[T]) InvalidateCacheByEntities ¶ added in v0.1.1
func (r *RepositoryWithCache[T]) InvalidateCacheByEntities(ctx context.Context, entities ...*T) error
InvalidateCacheByEntities 根据实体使缓存失效
func (*RepositoryWithCache[T]) ListWithCache ¶ added in v0.1.1
func (r *RepositoryWithCache[T]) ListWithCache(ctx context.Context, cacheKey string, ttl time.Duration, query *Query) ([]*T, error)
ListWithCache 查询列表,支持缓存
func (*RepositoryWithCache[T]) UpdateWithCache ¶ added in v0.1.1
func (r *RepositoryWithCache[T]) UpdateWithCache(ctx context.Context, entity *T) (*T, error)
UpdateWithCache 更新记录并删除缓存
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 interface {
// 执行事务内的操作
Create(ctx context.Context, entity interface{}) error
CreateBatch(ctx context.Context, entities ...interface{}) error
Update(ctx context.Context, entity interface{}) error
UpdateBatch(ctx context.Context, entities ...interface{}) error
Delete(ctx context.Context, entity interface{}) error
DeleteBatch(ctx context.Context, entities ...interface{}) 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
type VersionedModel ¶ added in v0.1.2
type VersionedModel interface {
ModelInterface
GetVersion() int
}
VersionedModel 支持版本控制的模型接口