Documentation
¶
Index ¶
- Variables
- func FromPtrSlice[T any](data []*T) []T
- func GetGormTx(ctx context.Context) *gorm.DB
- func GetNativeCollection[T any](repo Repo[T]) (*mongo.Collection, bool)
- func GetNativeDB[T any](repo Repo[T]) (*gorm.DB, bool)
- func NewOptions[T any](opts ...IList[T]) *T
- func ToAnySlice[T any](data []T) []any
- func ToPtrSlice[T any](data []T) []*T
- func WithGormTx(ctx context.Context, tx *gorm.DB) context.Context
- type DeleteResult
- type FindOptions
- type FindOptionsBuilder
- func (f *FindOptionsBuilder) List() []func(*FindOptions)
- func (f *FindOptionsBuilder) SetLimit(limit int64) *FindOptionsBuilder
- func (f *FindOptionsBuilder) SetReturnFields(fields ...string) *FindOptionsBuilder
- func (f *FindOptionsBuilder) SetSkip(skip int64) *FindOptionsBuilder
- func (f *FindOptionsBuilder) SetSort(sort *Sort) *FindOptionsBuilder
- type GormRepo
- func (r *GormRepo[T]) Count(ctx context.Context, filter any, opts ...IList[FindOptions]) (int64, error)
- func (r *GormRepo[T]) Create(ctx context.Context, entity *T) error
- func (r *GormRepo[T]) CreateMany(ctx context.Context, entities []*T) error
- func (r *GormRepo[T]) DeleteMany(ctx context.Context, filter any) (*DeleteResult, error)
- func (r *GormRepo[T]) DeleteOne(ctx context.Context, filter any) (*DeleteResult, error)
- func (r *GormRepo[T]) Find(ctx context.Context, filter any, opts ...IList[FindOptions]) ([]*T, error)
- func (r *GormRepo[T]) FindOne(ctx context.Context, filter any, opts ...IList[FindOptions]) (*T, error)
- func (r *GormRepo[T]) Incr(ctx context.Context, filter any, incr map[string]int, ...) error
- func (r *GormRepo[T]) Native() *gorm.DB
- func (r *GormRepo[T]) Transaction(ctx context.Context, fn TxFunc) error
- func (r *GormRepo[T]) Update(ctx context.Context, entity *T) error
- func (r *GormRepo[T]) UpdateMany(ctx context.Context, filter any, update map[string]any, ...) (*UpdateResult, error)
- func (r *GormRepo[T]) UpdateOne(ctx context.Context, filter any, update map[string]any, ...) (*UpdateResult, error)
- func (r *GormRepo[T]) UpsertOne(ctx context.Context, create T, opt UpsertOptions) (bool, error)
- type ICreator
- type IDeleter
- type IFinder
- type IList
- type INative
- type ITransaction
- type IUpdater
- type MongoRepo
- func (r *MongoRepo[T]) Count(ctx context.Context, filter any, opts ...IList[FindOptions]) (int64, error)
- func (r *MongoRepo[T]) Create(ctx context.Context, entity *T) error
- func (r *MongoRepo[T]) CreateMany(ctx context.Context, entities []*T) error
- func (r *MongoRepo[T]) DeleteMany(ctx context.Context, filter any) (*DeleteResult, error)
- func (r *MongoRepo[T]) DeleteOne(ctx context.Context, filter any) (*DeleteResult, error)
- func (r *MongoRepo[T]) Find(ctx context.Context, filter any, opts ...IList[FindOptions]) ([]*T, error)
- func (r *MongoRepo[T]) FindOne(ctx context.Context, filter any, opts ...IList[FindOptions]) (*T, error)
- func (r *MongoRepo[T]) Incr(ctx context.Context, filter any, incr map[string]int, ...) error
- func (r *MongoRepo[T]) Native() *mongo.Collection
- func (r *MongoRepo[T]) Transaction(ctx context.Context, fn TxFunc) error
- func (r *MongoRepo[T]) Update(ctx context.Context, entity *T) error
- func (r *MongoRepo[T]) UpdateMany(ctx context.Context, filter any, update map[string]any, ...) (*UpdateResult, error)
- func (r *MongoRepo[T]) UpdateOne(ctx context.Context, filter any, update map[string]any, ...) (*UpdateResult, error)
- func (r *MongoRepo[T]) UpsertOne(ctx context.Context, create T, opt UpsertOptions) (bool, error)
- type Repo
- type Sort
- type SortField
- type SortOrder
- type TxFunc
- type UpdateOptions
- type UpdateOptionsBuilder
- type UpdateResult
- type UpsertOptions
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 GetNativeCollection ¶
func GetNativeCollection[T any](repo Repo[T]) (*mongo.Collection, bool)
func NewOptions ¶
func ToAnySlice ¶
func ToPtrSlice ¶
func ToPtrSlice[T any](data []T) []*T
Types ¶
type DeleteResult ¶
type DeleteResult struct {
DeleteCount int64
}
type FindOptions ¶
FindOptions 存储查询配置
type FindOptionsBuilder ¶
type FindOptionsBuilder struct {
Opts []func(*FindOptions)
}
FindOptionsBuilder 链式构建器
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 (*GormRepo[T]) Count ¶
func (r *GormRepo[T]) Count(ctx context.Context, filter any, opts ...IList[FindOptions]) (int64, error)
Count 统计记录数
func (*GormRepo[T]) CreateMany ¶
CreateMany 批量创建记录
func (*GormRepo[T]) DeleteMany ¶
DeleteMany 删除多条记录
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]) Transaction ¶
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]) UpdateMany ¶
func (r *GormRepo[T]) UpdateMany(ctx context.Context, filter any, update map[string]any, opts ...IList[UpdateOptions]) (*UpdateResult, error)
UpdateMany 更新多条记录
type INative ¶
type INative[C any] interface { Native() C }
INative 提供访问底层数据库连接的能力 对于 GORM 返回 *gorm.DB 对于 MongoDB 返回 *mongo.Collection
type ITransaction ¶
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 NewMongoRepo ¶
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]) CreateMany ¶
CreateMany 批量创建记录
func (*MongoRepo[T]) DeleteMany ¶
DeleteMany 删除多条记录
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]) Native ¶
func (r *MongoRepo[T]) Native() *mongo.Collection
Native 返回底层 *mongo.Collection
func (*MongoRepo[T]) Transaction ¶
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]) UpdateMany ¶
func (r *MongoRepo[T]) UpdateMany(ctx context.Context, filter any, update map[string]any, opts ...IList[UpdateOptions]) (*UpdateResult, error)
UpdateMany 更新多条记录
type UpdateOptionsBuilder ¶
type UpdateOptionsBuilder struct {
Opts []func(*UpdateOptions)
}
UpdateOptionsBuilder 链式构建器
func (*UpdateOptionsBuilder) List ¶
func (u *UpdateOptionsBuilder) List() []func(*UpdateOptions)
List 返回所有配置函数
type UpdateResult ¶
type UpdateResult struct {
UpdateCount int64
}
Click to show internal directories.
Click to hide internal directories.