store

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	NeverExpires time.Duration = 0
)

Functions

func GetExpireTime

func GetExpireTime(expireTime time.Duration) *time.Duration

func GetTX

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

GetTX 从上下文中获取 GORM 事务 DB 实例。 如果上下文中没有事务 DB,则返回 nil。

func NewRepository

func NewRepository[T any](dbProvider DBProviderInterface) *repository[T]

NewRepository 创建一个新的通用 Repository 实例。 它接收一个 DBProviderInterface,使得 Repository 能够获取到正确的 GORM DB 实例(可能是主 DB 或事务 DB)。

Types

type ApiStorer

type ApiStorer interface {
	Create(ctx context.Context, obj *model.Api) error
	CreateBatch(ctx context.Context, objs []*model.Api) error // 批量创建
	Update(ctx context.Context, obj *model.Api, opts ...Option) error
	Delete(ctx context.Context, obj *model.Api, opts ...Option) error // 增加选项,支持where条件删除
	Query(ctx context.Context, opts ...Option) (*model.Api, error)
	List(ctx context.Context, page, pageSize int, colum, oder string, opts ...Option) (total int64, objs []*model.Api, err error)
}

func NewApiStore

func NewApiStore(dbProvider DBProviderInterface) ApiStorer

type CacheStore

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

func NewCacheStore

func NewCacheStore(redisClient *redis.Client) (*CacheStore, func(), error)

func (*CacheStore) DelKey

func (c *CacheStore) DelKey(ctx context.Context, cacheType CacheType, cacheKey any) error

func (*CacheStore) GetSet

func (c *CacheStore) GetSet(ctx context.Context, cacheType CacheType, cacheKey any) ([]string, error)

func (*CacheStore) NormalizeCacheKey

func (c *CacheStore) NormalizeCacheKey(cacheKey any) (string, error)

NormalizeCacheKey 将常用类型的 cacheKey 转换为 string

func (*CacheStore) SetSet

func (c *CacheStore) SetSet(ctx context.Context, cacheType CacheType, cacheKey any, cacheValue []any, expireTime *time.Duration) error

type CacheStorer

type CacheStorer interface {
	DelKey(ctx context.Context, cacheType CacheType, cacheKey any) error
	GetSet(ctx context.Context, cacheType CacheType, cacheKey any) ([]string, error)
	SetSet(ctx context.Context, cacheType CacheType, cacheKey any, cacheValue []any, expireTime *time.Duration) error
}

type CacheType

type CacheType string
const (
	RoleType CacheType = "role"
	TestType CacheType = "test"
)

type CasbinStorer

type CasbinStorer interface {
	Create(ctx context.Context, obj *model.CasbinRule) error
	CreateBatch(ctx context.Context, objs []*model.CasbinRule) error // 批量创建
	Update(ctx context.Context, obj *model.CasbinRule, opts ...Option) error
	Delete(ctx context.Context, obj *model.CasbinRule, opts ...Option) error         // 增加选项,支持where条件删除
	DeleteBatch(ctx context.Context, objs []*model.CasbinRule, opts ...Option) error // 批量删除
	Query(ctx context.Context, opts ...Option) (*model.CasbinRule, error)
	List(ctx context.Context, page, pageSize int, colum, oder string, opts ...Option) (total int64, objs []*model.CasbinRule, err error)
}

func NewCasbinStore

func NewCasbinStore(dbProvider DBProviderInterface) CasbinStorer

type DBProvider

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

DBProvider 是 DBProviderInterface 的实现。

func NewDBProvider

func NewDBProvider(db *gorm.DB) *DBProvider

NewDBProvider 创建一个新的 DBProvider 实例。

type DBProviderInterface

type DBProviderInterface interface {
	// contains filtered or unexported methods
}

DBProviderInterface 数据库连接提供接口。

type FeiShuUserStorer

type FeiShuUserStorer interface {
	Create(ctx context.Context, obj *model.FeiShuUser) error
	CreateBatch(ctx context.Context, objs []*model.FeiShuUser) error
	Update(ctx context.Context, obj *model.FeiShuUser, opts ...Option) error
	Delete(ctx context.Context, obj *model.FeiShuUser, opts ...Option) error
	Query(ctx context.Context, opts ...Option) (*model.FeiShuUser, error)
	List(ctx context.Context, page, pageSize int, colum, oder string, opts ...Option) (total int64, objs []*model.FeiShuUser, err error)
	AppendAssociation(ctx context.Context, model *model.FeiShuUser, objName string, obj any) error
	ReplaceAssociation(ctx context.Context, model *model.FeiShuUser, objName string, obj any) error
	ClearAssociation(ctx context.Context, model *model.FeiShuUser, objName string) error
}

func NewFeiShuUserStore

func NewFeiShuUserStore(dbProvider DBProviderInterface) FeiShuUserStorer

type Option

type Option func(db *gorm.DB) *gorm.DB

Option 函数用于配置 GORM 查询。 它接收一个 *gorm.DB 实例并返回修改后的 *gorm.DB 实例, 从而允许链式调用 GORM 方法。

@example:

options := []store.Option{
    store.Where("id", req.ID),
    store.Where("status", model.UserStatusEnable),
    store.Preload("Profile"),
    store.Order("created_at desc"),
}

func In

func In(colum string, values any) Option

func Like

func Like(colum string, value any) Option

func Limit

func Limit(limit int) Option

Limit 用于限制查询结果的数量。

func Offset

func Offset(offset int) Option

Offset 用于设置查询的偏移量。

func Order

func Order(value string) Option

Order 用于指定排序条件。

func Preload

func Preload(modelName string, args ...any) Option

Preload 用于预加载关联模型。

func Scopes

func Scopes(funcs ...func(*gorm.DB) *gorm.DB) Option

Scopes 用于应用 GORM Scopes。

func Select

func Select(columns ...string) Option

Select 用于指定查询的列。

func Where

func Where(colum string, value any) Option

Where 用于添加 WHERE 条件。

type RoleStorer

type RoleStorer interface {
	Create(ctx context.Context, obj *model.Role) error
	CreateBatch(ctx context.Context, objs []*model.Role) error // 批量创建
	Update(ctx context.Context, obj *model.Role, opts ...Option) error
	Delete(ctx context.Context, obj *model.Role, opts ...Option) error // 增加选项,支持where条件删除
	Query(ctx context.Context, opts ...Option) (*model.Role, error)
	List(ctx context.Context, page, pageSize int, colum, oder string, opts ...Option) (total int64, objs []*model.Role, err error)
	AppendAssociation(ctx context.Context, model *model.Role, objName string, obj any) error
	ReplaceAssociation(ctx context.Context, model *model.Role, objName string, obj any) error
	ClearAssociation(ctx context.Context, model *model.Role, objName string) error
}

func NewRoleStore

func NewRoleStore(dbProvider DBProviderInterface) RoleStorer

type TxManager

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

TxManager 是 TxManagerInterface 的实现。

func NewTxManager

func NewTxManager(db *gorm.DB) *TxManager

NewTxManager 创建一个新的 TxManager 实例。

func (*TxManager) Transaction

func (s *TxManager) Transaction(ctx context.Context, fn func(ctx context.Context) error) error

Transaction 执行一个数据库事务。 如果 fn 返回错误,事务将回滚;否则,事务将提交。

type TxManagerInterface

type TxManagerInterface interface {
	// Transaction 在一个事务中执行给定的函数。
	// 函数内部可以通过 GetTX(ctx) 获取事务 DB 实例,从而确保所有操作都在同一事务内。
	Transaction(ctx context.Context, fn func(ctx context.Context) error) error
}

TxManagerInterface 事务管理器接口。

type UserStorer

type UserStorer interface {
	Create(ctx context.Context, obj *model.User) error
	CreateBatch(ctx context.Context, objs []*model.User) error // 批量创建
	Update(ctx context.Context, obj *model.User, opts ...Option) error
	Delete(ctx context.Context, obj *model.User, opts ...Option) error // 增加选项,支持where条件删除
	Query(ctx context.Context, opts ...Option) (*model.User, error)
	List(ctx context.Context, page, pageSize int, colum, oder string, opts ...Option) (total int64, objs []*model.User, err error)
	AppendAssociation(ctx context.Context, model *model.User, objName string, obj any) error
	ReplaceAssociation(ctx context.Context, model *model.User, objName string, obj any) error
	ClearAssociation(ctx context.Context, model *model.User, objName string) error
}

func NewUserStore

func NewUserStore(dbProvider DBProviderInterface) UserStorer

Jump to

Keyboard shortcuts

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