dao

package
v1.4.21 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package dao is the data access object, the code for database operation is unified here.

Index

Constants

View Source
const (
	// MaxCacheableIDs 最大可缓存的 ID 数量,超过此值则不缓存 ID 列表,直接查库
	MaxCacheableIDs = 10000

	// MaxBatchSize 批量操作的最大批次大小,超过此值则分批处理
	MaxBatchSize = 1000

	// MaxCacheableRecords 最大可缓存的记录数,超过此值则不缓存或仅警告
	MaxCacheableRecords = 1000

	// DelayedDeleteInterval 延迟双删的等待时间,用于确保主从同步完成
	// 默认值:100ms,可通过配置文件 database.cacheDelayedDeleteInterval 覆盖
	DelayedDeleteInterval = 100 * time.Millisecond
)

缓存和数据量阈值常量(避免硬编码)

Variables

This section is empty.

Functions

This section is empty.

Types

type QueryOption added in v1.4.15

type QueryOption func(*queryOptions)

QueryOption 查询选项函数类型(使用选项模式替代变长布尔参数)

func WithForceMaster added in v1.4.15

func WithForceMaster() QueryOption

WithForceMaster 强制使用主库查询 示例:dao.GetByID(ctx, id, dao.WithForceMaster())

type UserExampleDao

type UserExampleDao interface {
	Create(ctx context.Context, table *model.UserExample) error
	CreateInBatches(ctx context.Context, tables []*model.UserExample, batchSize int) error
	CreateByTx(ctx context.Context, tx *gorm.DB, table *model.UserExample) (uint64, error)
	CreateByInBatchesTx(ctx context.Context, tx *gorm.DB, tables []*model.UserExample, batchSize int) error

	DeleteByID(ctx context.Context, id uint64) error
	DeleteByIDs(ctx context.Context, ids []uint64) error
	DeleteByCondition(ctx context.Context, c *query.Conditions) error
	DeleteByTx(ctx context.Context, tx *gorm.DB, id uint64) error
	DeleteByIDsTx(ctx context.Context, tx *gorm.DB, ids []uint64) error
	DeleteByTxCondition(ctx context.Context, tx *gorm.DB, c *query.Conditions) error
	ClearCache(ctx context.Context) error

	UpdateByID(ctx context.Context, table *model.UserExample) error
	UpdateByCondition(ctx context.Context, c *query.Conditions, updates *model.UserExample) error
	UpdateByTx(ctx context.Context, tx *gorm.DB, table *model.UserExample) error
	UpdateByConditionTx(ctx context.Context, tx *gorm.DB, c *query.Conditions, updates *model.UserExample) error
	ExecByCustomFunc(ctx context.Context, updateFunc func(*gorm.DB) *gorm.DB) error

	GetByID(ctx context.Context, id uint64, opts ...QueryOption) (*model.UserExample, error)
	GetByColumns(ctx context.Context, params *query.Params, opts ...QueryOption) ([]*model.UserExample, int64, error)
	GetOneByColumns(ctx context.Context, params *query.Params, opts ...QueryOption) (*model.UserExample, error)
	GetByCondition(ctx context.Context, c *query.Conditions, opts ...QueryOption) (ids []uint64, err error)
	GetByIDs(ctx context.Context, ids []uint64, opts ...QueryOption) (map[uint64]*model.UserExample, error)
	CountByCondition(ctx context.Context, c *query.Conditions, opts ...QueryOption) (int64, error)
	ExistsByCondition(ctx context.Context, c *query.Conditions, opts ...QueryOption) (bool, error)
	GetByCustomQuery(ctx context.Context, queryFunc func(*gorm.DB) *gorm.DB, result interface{}, page, limit int, opts ...QueryOption) (int64, error)
}

UserExampleDao defining the dao interface(所有查询方法支持选项模式)

func NewUserExampleDao

func NewUserExampleDao(db *gorm.DB, xCache cache.UserExampleCache) UserExampleDao

NewUserExampleDao creating the dao interface

Jump to

Keyboard shortcuts

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