repository

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 21:13:15 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-20 13:33:13 * @FilePath: \go-sqlbuilder\repository\base.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

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

func (m *BaseModel) BeforeUpdate(tx *gorm.DB) error

BeforeUpdate GORM更新前钩子

func (*BaseModel) Disable added in v0.1.2

func (m *BaseModel) Disable()

Disable 禁用

func (*BaseModel) Enable added in v0.1.2

func (m *BaseModel) Enable()

Enable 启用

func (*BaseModel) GetID added in v0.1.2

func (m *BaseModel) GetID() uint

GetID 获取主键ID

func (*BaseModel) GetVersion added in v0.1.2

func (m *BaseModel) GetVersion() int

GetVersion 获取版本号

func (*BaseModel) IsDeleted added in v0.1.2

func (m *BaseModel) IsDeleted() bool

IsDeleted 判断是否已删除

func (*BaseModel) IsEnabled added in v0.1.2

func (m *BaseModel) IsEnabled() bool

IsEnabled 判断是否启用

func (*BaseModel) IsNew added in v0.1.2

func (m *BaseModel) IsNew() bool

IsNew 判断是否为新记录

func (*BaseModel) SetCreatedAt added in v0.1.2

func (m *BaseModel) SetCreatedAt(t time.Time)

func (*BaseModel) SetRemark added in v0.1.2

func (m *BaseModel) SetRemark(remark string)

SetRemark 设置备注

func (*BaseModel) SetUpdatedAt added in v0.1.2

func (m *BaseModel) SetUpdatedAt(t time.Time)

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]) Count

func (r *BaseRepository[T]) Count(ctx context.Context, filters ...*Filter) (int64, error)

Count 计数

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]) 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]) Exists

func (r *BaseRepository[T]) Exists(ctx context.Context, filters ...*Filter) (bool, error)

Exists 检查记录是否存在

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]) 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 CacheConfig

type CacheConfig struct {
	Enabled bool
	TTL     time.Duration
}

CacheConfig 缓存配置

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 Condition added in v0.1.5

type Condition struct {
	Field string
	Op    constant.Operator
	Value interface{}
}

Condition 查询条件结构

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, tableName string) *EnhancedRepository[T]

NewEnhancedRepository 创建增强版仓储实例

func NewEnhancedRepositoryWithDB added in v0.1.3

func NewEnhancedRepositoryWithDB[T any](gormDB *gorm.DB, 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 = core.Filter

Filter 过滤条件(复用 core.Filter)

func NewBetweenFilter

func NewBetweenFilter(field string, min, max interface{}) *Filter

NewBetweenFilter 创建 BETWEEN 过滤条件

func NewContainsFilter added in v0.1.1

func NewContainsFilter(field string, value string) *Filter

NewContainsFilter 创建包含匹配过滤条件(与 NewLikeFilter 相同)

func NewEndsWithFilter added in v0.1.1

func NewEndsWithFilter(field string, value string) *Filter

NewEndsWithFilter 创建后缀匹配过滤条件

func NewEqFilter

func NewEqFilter(field string, value interface{}) *Filter

NewEqFilter 创建等于过滤条件

func NewGtFilter

func NewGtFilter(field string, value interface{}) *Filter

NewGtFilter 创建大于过滤条件

func NewGteFilter

func NewGteFilter(field string, value interface{}) *Filter

NewGteFilter 创建大于等于过滤条件

func NewInFilter

func NewInFilter(field string, values ...interface{}) *Filter

NewInFilter 创建 IN 过滤条件

func NewIsNotNullFilter added in v0.1.1

func NewIsNotNullFilter(field string) *Filter

NewIsNotNullFilter 创建 IS NOT NULL 过滤条件

func NewIsNullFilter added in v0.1.1

func NewIsNullFilter(field string) *Filter

NewIsNullFilter 创建 IS NULL 过滤条件

func NewLikeFilter

func NewLikeFilter(field string, value string) *Filter

NewLikeFilter 创建 LIKE 过滤条件

func NewLtFilter

func NewLtFilter(field string, value interface{}) *Filter

NewLtFilter 创建小于过滤条件

func NewLteFilter

func NewLteFilter(field string, value interface{}) *Filter

NewLteFilter 创建小于等于过滤条件

func NewNeqFilter

func NewNeqFilter(field string, value interface{}) *Filter

NewNeqFilter 创建不等于过滤条件

func NewNotInFilter added in v0.1.1

func NewNotInFilter(field string, values ...interface{}) *Filter

NewNotInFilter 创建 NOT IN 过滤条件

func NewStartsWithFilter added in v0.1.1

func NewStartsWithFilter(field string, value string) *Filter

NewStartsWithFilter 创建前缀匹配过滤条件

type FindOptions added in v0.1.5

type FindOptions struct {
	Conditions []Condition
	Orders     []OrderBy
	Limit      int
	Offset     int
}

FindOptions 兼容旧API的查询选项结构

type FullFeaturedModel added in v0.1.2

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 Order

type Order struct {
	Field     string
	Direction string // "ASC" or "DESC"
}

Order 排序条件

type OrderBy added in v0.1.5

type OrderBy struct {
	Field     string
	Direction string
}

OrderBy 排序条件结构

type Query

type Query struct {
	Filters     []*Filter
	Orders      []Order
	Pagination  *meta.Paging
	LimitValue  *int
	OffsetValue *int
}

Query 查询条件

func NewQuery

func NewQuery() *Query

NewQuery 创建查询条件

func (*Query) AddFilter

func (q *Query) AddFilter(filter *Filter) *Query

AddFilter 添加过滤条件

func (*Query) AddFilters

func (q *Query) AddFilters(filters ...*Filter) *Query

AddFilters 批量添加过滤条件

func (*Query) AddOrder

func (q *Query) AddOrder(field, direction string) *Query

AddOrder 添加排序条件

func (*Query) Limit added in v0.1.4

func (q *Query) Limit(limit int) *Query

Limit 设置查询限制数量

func (*Query) Offset added in v0.1.4

func (q *Query) Offset(offset int) *Query

Offset 设置查询偏移量

func (*Query) WithPaging

func (q *Query) WithPaging(page, pageSize int) *Query

WithPaging 设置分页条件

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

func (m *UUIDModel) BeforeUpdate(tx *gorm.DB) error

func (*UUIDModel) GetID added in v0.1.2

func (m *UUIDModel) GetID() string

func (*UUIDModel) IsNew added in v0.1.2

func (m *UUIDModel) IsNew() bool

type VersionedModel added in v0.1.2

type VersionedModel interface {
	ModelInterface
	GetVersion() int
}

VersionedModel 支持版本控制的模型接口

Jump to

Keyboard shortcuts

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