baserepo

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrRecordNotFound = gorm.ErrRecordNotFound

ErrRecordNotFound 记录未找到

Functions

func WithDB added in v1.1.0

func WithDB[T DBConfigurer](db *gorm.DB) func(T)

WithDB 注入DB

Types

type BaseRepository

type BaseRepository[T any] struct {
	Db *gorm.DB
}

BaseRepository 基础DAO层

func NewBaseRepository

func NewBaseRepository[T any](db *gorm.DB) *BaseRepository[T]

NewBaseRepository 创建基础DAO层

func (*BaseRepository[T]) Count

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

Count 统计记录数量

func (*BaseRepository[T]) Create

func (r *BaseRepository[T]) Create(ctx context.Context, item *T, opts ...CreateOption) error

Create 创建记录

func (*BaseRepository[T]) CreateBatch

func (r *BaseRepository[T]) CreateBatch(ctx context.Context, item []*T, opts ...CreateOption) error

CreateBatch 批量创建记录

func (*BaseRepository[T]) Delete

func (r *BaseRepository[T]) Delete(ctx context.Context, id any, opts ...DeleteOption) error

Delete 删除记录

func (*BaseRepository[T]) FindAll

func (r *BaseRepository[T]) FindAll(ctx context.Context, filter Filter, pagination Paginator, orders Orders, opts ...QueryOption) ([]*T, error)

FindAll 查询所有记录

func (*BaseRepository[T]) FindOne

func (r *BaseRepository[T]) FindOne(ctx context.Context, id any, opts ...QueryOption) (*T, error)

FindOne 根据主键查询记录

func (*BaseRepository[T]) Updates

func (r *BaseRepository[T]) Updates(ctx context.Context, item *T, updateData map[string]any, opts ...UpdateOption) error

Updates 更新记录

type CreateConfig

type CreateConfig struct {
	DBConfig
	// contains filtered or unexported fields
}

CreateConfig 创建配置

type CreateOption

type CreateOption func(*CreateConfig)

CreateOption 创建选项

func WithCreateBatchSize

func WithCreateBatchSize(batchSize int) CreateOption

WithCreateBatchSize 设置批量创建大小

type DBConfig added in v1.1.0

type DBConfig struct {
	// contains filtered or unexported fields
}

DBConfig DB配置

func (*DBConfig) GetDB added in v1.1.0

func (c *DBConfig) GetDB() *gorm.DB

GetDB 获取DB

func (*DBConfig) SetDB added in v1.1.0

func (c *DBConfig) SetDB(db *gorm.DB)

SetDB 设置DB

type DBConfigurer added in v1.1.0

type DBConfigurer interface {
	SetDB(db *gorm.DB)
	GetDB() *gorm.DB
}

DBConfigurer DB配置接口

type DeleteConfig

type DeleteConfig struct {
	DBConfig
	ForceDelete bool
}

DeleteConfig 删除配置

type DeleteOption

type DeleteOption func(*DeleteConfig)

DeleteOption 删除选项

func WithForceDelete

func WithForceDelete(force bool) DeleteOption

WithForceDelete 强制删除选项

type Filter

type Filter interface {
	Apply(db *gorm.DB) *gorm.DB
}

Filter 筛选器接口

type Order

type Order struct {
	Field string
	Sort  string
}

Order 排序参数

type Orders

type Orders []Order

Orders 排序参数切片

type Pagination

type Pagination struct {
	// contains filtered or unexported fields
}

Pagination 分页参数

func NewPagination

func NewPagination(page, limit int) *Pagination

NewPagination 创建分页参数

func (*Pagination) Limit

func (p *Pagination) Limit() int

Limit 获取每页数量

func (*Pagination) Offset

func (p *Pagination) Offset() int

Offset 计算偏移量

type Paginator

type Paginator interface {
	Offset() int
	Limit() int
}

Paginator 分页器接口

type QueryConfig

type QueryConfig struct {
	DBConfig
	// contains filtered or unexported fields
}

QueryConfig 查询配置

type QueryOption

type QueryOption func(*QueryConfig)

QueryOption 查询选项

func WithPreloads

func WithPreloads(preloads ...string) QueryOption

WithPreloads 添加预加载

func WithScopes

func WithScopes(scopes ...ScopeFunc) QueryOption

WithScopes 添加筛选

type ScopeFunc

type ScopeFunc func(*gorm.DB) *gorm.DB

ScopeFunc 筛选作用域方法

type UpdateConfig

type UpdateConfig struct {
	DBConfig
}

UpdateConfig 更新配置

type UpdateOption

type UpdateOption func(*UpdateConfig)

UpdateOption 更新选项

Jump to

Keyboard shortcuts

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