repox

package
v0.0.2-beta.1 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DataNotFound = errors.New("data not found")

Functions

func FromPtrSlice

func FromPtrSlice[T any](data []*T) []T

func GetGormTx

func GetGormTx(ctx context.Context) *gorm.DB

GetGormTx 从 context 获取 GORM 事务 DB,如果不存在则返回 nil

func GetNativeCollection

func GetNativeCollection[T any](repo Repo[T]) (*mongo.Collection, bool)

func GetNativeDB

func GetNativeDB[T any](repo Repo[T]) (*gorm.DB, bool)

func NewOptions

func NewOptions[T any](opts ...IList[T]) *T

func ToAnySlice

func ToAnySlice[T any](data []T) []any

func ToPtrSlice

func ToPtrSlice[T any](data []T) []*T

func WithGormTx

func WithGormTx(ctx context.Context, tx *gorm.DB) context.Context

WithGormTx 将 GORM 事务 DB 注入到 context

Types

type DeleteResult

type DeleteResult struct {
	DeleteCount int64
}

type FindOptions

type FindOptions struct {
	ReturnFields []string
	Skip         int64
	Limit        int64
	Sort         *Sort
}

FindOptions 存储查询配置

type FindOptionsBuilder

type FindOptionsBuilder struct {
	Opts []func(*FindOptions)
}

FindOptionsBuilder 链式构建器

func Find

func Find() *FindOptionsBuilder

Find 创建新的构建器

func (*FindOptionsBuilder) List

func (f *FindOptionsBuilder) List() []func(*FindOptions)

List 返回所有配置函数

func (*FindOptionsBuilder) SetLimit

func (f *FindOptionsBuilder) SetLimit(limit int64) *FindOptionsBuilder

func (*FindOptionsBuilder) SetReturnFields

func (f *FindOptionsBuilder) SetReturnFields(fields ...string) *FindOptionsBuilder

func (*FindOptionsBuilder) SetSkip

func (f *FindOptionsBuilder) SetSkip(skip int64) *FindOptionsBuilder

func (*FindOptionsBuilder) SetSort

func (f *FindOptionsBuilder) SetSort(sort *Sort) *FindOptionsBuilder

type GormRepo

type GormRepo[T any] struct {
	// contains filtered or unexported fields
}

GormRepo GORM 通用仓库实现(基于 gorm.G 泛型 API)

func AsGormRepo

func AsGormRepo[T any](repo Repo[T]) (*GormRepo[T], bool)

func NewGormRepo

func NewGormRepo[T any](db *gorm.DB) *GormRepo[T]

NewGormRepo 创建 GORM 仓库

func (*GormRepo[T]) Count

func (r *GormRepo[T]) Count(ctx context.Context, filter any, opts ...IList[FindOptions]) (int64, error)

Count 统计记录数

func (*GormRepo[T]) Create

func (r *GormRepo[T]) Create(ctx context.Context, entity *T) error

Create 创建单条记录

func (*GormRepo[T]) CreateMany

func (r *GormRepo[T]) CreateMany(ctx context.Context, entities []*T) error

CreateMany 批量创建记录

func (*GormRepo[T]) DeleteMany

func (r *GormRepo[T]) DeleteMany(ctx context.Context, filter any) (*DeleteResult, error)

DeleteMany 删除多条记录

func (*GormRepo[T]) DeleteOne

func (r *GormRepo[T]) DeleteOne(ctx context.Context, filter any) (*DeleteResult, error)

DeleteOne 删除单条记录

func (*GormRepo[T]) Find

func (r *GormRepo[T]) Find(ctx context.Context, filter any, opts ...IList[FindOptions]) ([]*T, error)

Find 查询多条记录

func (*GormRepo[T]) FindOne

func (r *GormRepo[T]) FindOne(ctx context.Context, filter any, opts ...IList[FindOptions]) (*T, error)

FindOne 查询单条记录

func (*GormRepo[T]) Incr

func (r *GormRepo[T]) Incr(ctx context.Context, filter any, incr map[string]int, opts ...IList[UpdateOptions]) error

func (*GormRepo[T]) Native

func (r *GormRepo[T]) Native() *gorm.DB

Native 返回底层 *gorm.DB

func (*GormRepo[T]) Transaction

func (r *GormRepo[T]) Transaction(ctx context.Context, fn TxFunc) error

Transaction 执行事务 使用示例:

err := repo.Transaction(ctx, func(ctx context.Context) error {
    // 直接使用原有的 repo,会自动使用事务 DB
    if err := userRepo.Create(ctx, user); err != nil {
        return err
    }
    return orderRepo.Create(ctx, order)
})

func (*GormRepo[T]) Update

func (r *GormRepo[T]) Update(ctx context.Context, entity *T) error

Update 更新整个实体(通过主键)

func (*GormRepo[T]) UpdateMany

func (r *GormRepo[T]) UpdateMany(ctx context.Context, filter any, update map[string]any, opts ...IList[UpdateOptions]) (*UpdateResult, error)

UpdateMany 更新多条记录

func (*GormRepo[T]) UpdateOne

func (r *GormRepo[T]) UpdateOne(ctx context.Context, filter any, update map[string]any, opts ...IList[UpdateOptions]) (*UpdateResult, error)

UpdateOne 更新单条记录

func (*GormRepo[T]) UpsertOne

func (r *GormRepo[T]) UpsertOne(ctx context.Context, create T, opt UpsertOptions) (bool, error)

UpsertOne 插入或更新单条记录,返回是否是插入操作

type ICreator

type ICreator[T any] interface {
	Create(context.Context, *T) error
	CreateMany(context.Context, []*T) error
}

type IDeleter

type IDeleter[T any] interface {
	DeleteOne(ctx context.Context, filter any) (*DeleteResult, error)
	DeleteMany(ctx context.Context, filter any) (*DeleteResult, error)
}

type IFinder

type IFinder[T any] interface {
	FindOne(ctx context.Context, filter any, opts ...IList[FindOptions]) (*T, error)
	Find(ctx context.Context, filter any, opts ...IList[FindOptions]) ([]*T, error)
	Count(ctx context.Context, filter any, opts ...IList[FindOptions]) (int64, error)
}

type IList

type IList[T any] interface {
	List() []func(*T)
}

type INative

type INative[C any] interface {
	Native() C
}

INative 提供访问底层数据库连接的能力 对于 GORM 返回 *gorm.DB 对于 MongoDB 返回 *mongo.Collection

type ITransaction

type ITransaction[T any] interface {
	Transaction(ctx context.Context, fn TxFunc) error
}

type IUpdater

type IUpdater[T any] interface {
	Update(context.Context, *T) error
	Incr(ctx context.Context, filter any, incr map[string]int, opts ...IList[UpdateOptions]) error
	UpdateOne(ctx context.Context, filter any, update map[string]any, opts ...IList[UpdateOptions]) (*UpdateResult, error)
	UpsertOne(ctx context.Context, create T, opt UpsertOptions) (bool, error)
	UpdateMany(ctx context.Context, filter any, update map[string]any, opts ...IList[UpdateOptions]) (*UpdateResult, error)
}

type MongoRepo

type MongoRepo[T any] struct {
	// contains filtered or unexported fields
}

MongoRepo MongoDB 通用仓库实现

func AsMongoRepo

func AsMongoRepo[T any](repo Repo[T]) (*MongoRepo[T], bool)

func NewMongoRepo

func NewMongoRepo[T any](coll *mongo.Collection, client *mongo.Client) *MongoRepo[T]

NewMongoRepo 创建 MongoDB 仓库

func (*MongoRepo[T]) Count

func (r *MongoRepo[T]) Count(ctx context.Context, filter any, opts ...IList[FindOptions]) (int64, error)

Count 统计记录数

func (*MongoRepo[T]) Create

func (r *MongoRepo[T]) Create(ctx context.Context, entity *T) error

Create 创建单条记录

func (*MongoRepo[T]) CreateMany

func (r *MongoRepo[T]) CreateMany(ctx context.Context, entities []*T) error

CreateMany 批量创建记录

func (*MongoRepo[T]) DeleteMany

func (r *MongoRepo[T]) DeleteMany(ctx context.Context, filter any) (*DeleteResult, error)

DeleteMany 删除多条记录

func (*MongoRepo[T]) DeleteOne

func (r *MongoRepo[T]) DeleteOne(ctx context.Context, filter any) (*DeleteResult, error)

DeleteOne 删除单条记录

func (*MongoRepo[T]) Find

func (r *MongoRepo[T]) Find(ctx context.Context, filter any, opts ...IList[FindOptions]) ([]*T, error)

Find 查询多条记录

func (*MongoRepo[T]) FindOne

func (r *MongoRepo[T]) FindOne(ctx context.Context, filter any, opts ...IList[FindOptions]) (*T, error)

FindOne 查询单条记录

func (*MongoRepo[T]) Incr

func (r *MongoRepo[T]) Incr(ctx context.Context, filter any, incr map[string]int, opts ...IList[UpdateOptions]) error

func (*MongoRepo[T]) Native

func (r *MongoRepo[T]) Native() *mongo.Collection

Native 返回底层 *mongo.Collection

func (*MongoRepo[T]) Transaction

func (r *MongoRepo[T]) Transaction(ctx context.Context, fn TxFunc) error

Transaction 执行 MongoDB 事务 使用示例:

err := repo.Transaction(ctx, func(ctx context.Context) error {
    userRepo := repox.NewMongoRepo[User](userColl)
    orderRepo := repox.NewMongoRepo[Order](orderColl)
    // 执行事务操作,使用传入的 ctx(包含 session)
    return nil
})

func (*MongoRepo[T]) Update

func (r *MongoRepo[T]) Update(ctx context.Context, entity *T) error

Update 更新整个实体(通过 _id)

func (*MongoRepo[T]) UpdateMany

func (r *MongoRepo[T]) UpdateMany(ctx context.Context, filter any, update map[string]any, opts ...IList[UpdateOptions]) (*UpdateResult, error)

UpdateMany 更新多条记录

func (*MongoRepo[T]) UpdateOne

func (r *MongoRepo[T]) UpdateOne(ctx context.Context, filter any, update map[string]any, opts ...IList[UpdateOptions]) (*UpdateResult, error)

UpdateOne 更新单条记录

func (*MongoRepo[T]) UpsertOne

func (r *MongoRepo[T]) UpsertOne(ctx context.Context, create T, opt UpsertOptions) (bool, error)

UpsertOne 插入或更新单条记录,返回是否是插入操作

type Repo

type Repo[T any] interface {
	ICreator[T]
	IFinder[T]
	IUpdater[T]
	IDeleter[T]
	ITransaction[T]
}

type Sort

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

func NewSort

func NewSort() *Sort

func (*Sort) Asc

func (s *Sort) Asc(field string) *Sort

func (*Sort) Desc

func (s *Sort) Desc(field string) *Sort

func (*Sort) ToBson

func (s *Sort) ToBson() bson.D

func (*Sort) ToSqlStr

func (s *Sort) ToSqlStr() string

type SortField

type SortField struct {
	Field string
	Order SortOrder
}

type SortOrder

type SortOrder int
const (
	Asc SortOrder = iota + 1
	Desc
)

type TxFunc

type TxFunc func(ctx context.Context) error

TxFunc 事务函数类型

type UpdateOptions

type UpdateOptions struct {
}

UpdateOptions 存储更新配置

type UpdateOptionsBuilder

type UpdateOptionsBuilder struct {
	Opts []func(*UpdateOptions)
}

UpdateOptionsBuilder 链式构建器

func Update

func Update() *UpdateOptionsBuilder

Update 创建新的构建器

func (*UpdateOptionsBuilder) List

func (u *UpdateOptionsBuilder) List() []func(*UpdateOptions)

List 返回所有配置函数

type UpdateResult

type UpdateResult struct {
	UpdateCount int64
}

type UpsertOptions

type UpsertOptions struct {
	ConflictKvs map[string]any   // 冲突字段(唯一索引),用作filter
	Set         map[string]any   // 普通赋值
	Inc         map[string]int64 // 自增
}

Jump to

Keyboard shortcuts

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