dao

package
v2.0.11 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheConfig added in v2.0.8

type CacheConfig struct {
	Enabled bool
	Store   cacheStore.ICache
	TTL     time.Duration
	Prefix  string
}

CacheConfig controls result caching for the DAO query methods.

type Dao

type Dao[E any] interface {
	Insert(entity *E) error
	Where(query interface{}, args ...interface{}) *gorm.DB
	Find(query interface{}, args ...interface{}) ([]E, error)
	FindById(id interface{}) (E, error)
	Update(entity *E) error
	Delete(entity *E) error
	Count() (int64, error)
}

Dao[E] 是 mgin v2 扩展后的泛型 DAO 接口。

v1 时代仅包含 Insert(*E) error 一个方法,v2 扩展为与 db/dao/mysql.go、postgres.go、 clickhouse.go、mgo.go 等具体实现对齐的最小方法集。已有具体实现(MySQLDao 等)的方法 集合已经超过这个接口,因此把它们"补齐签名"以满足本接口即可。

注意:v2 Dao 接口仍然保持小而精,避免在接口里强加大量可选方法。具体实现可以 拥有接口之外的扩展方法(如 MySQLDao.Debug()、MySQLDao.WithContext()), 业务需要时直接断言到具体类型调用。

type MgoDao

type MgoDao[E any] struct {
	CollectionName string
	Tag            func() string
}

MgoDao 注意使用前必须先将CollectionName赋值

func (MgoDao[E]) All

func (m MgoDao[E]) All(query bson.M) ([]E, error)

All mongo动态查询数据

func (MgoDao[E]) Delete

func (m MgoDao[E]) Delete(query bson.M) error

Delete mongo动态删除数据

func (MgoDao[E]) Insert

func (m MgoDao[E]) Insert(entity *E) error

Insert mongo动态插入数据

func (MgoDao[E]) One

func (m MgoDao[E]) One(query bson.M) (*E, error)

One mongo动态查询一条数据

func (MgoDao[E]) Pager

func (m MgoDao[E]) Pager(query bson.M, sort []string, page, size int) ([]E, *models.ResultPage, error)

Pager mongo简单分页查询数据

func (MgoDao[E]) Update added in v2.0.8

func (m MgoDao[E]) Update(selector bson.M, update bson.M) error

Update mongo动态更新数据

func (MgoDao[E]) Updates

func (m MgoDao[E]) Updates(id primitive.ObjectID, doc E) error

Updates mongo动态更新数据

type MySQLDao

type MySQLDao[E schema.Tabler] struct {
	Tag func() string
	// contains filtered or unexported fields
}

func (*MySQLDao[E]) Alias added in v2.0.7

func (receiver *MySQLDao[E]) Alias(alias string) *gorm.DB

func (*MySQLDao[E]) All

func (receiver *MySQLDao[E]) All(entity E, opts ...QueryOption) ([]E, error)

All mysql动态查询数据

func (*MySQLDao[E]) Count

func (receiver *MySQLDao[E]) Count(entity E) (int64, error)

Count mysql统计记录数

func (*MySQLDao[E]) Create

func (receiver *MySQLDao[E]) Create(entity *E) error

Create mysql动态插入数据

func (*MySQLDao[E]) DB added in v2.0.7

func (receiver *MySQLDao[E]) DB() *gorm.DB

func (*MySQLDao[E]) Debug

func (m *MySQLDao[E]) Debug() *MySQLDao[E]

func (*MySQLDao[E]) Delete

func (receiver *MySQLDao[E]) Delete(entity E) error

Delete mysql动态删除数据

func (*MySQLDao[E]) Exists

func (receiver *MySQLDao[E]) Exists(entity E) bool

Exists mysql动态查询是否存在数据

func (*MySQLDao[E]) JOIN added in v2.0.7

func (receiver *MySQLDao[E]) JOIN(alias, query string, args ...interface{}) *gorm.DB

func (*MySQLDao[E]) MultiCreate

func (receiver *MySQLDao[E]) MultiCreate(entities []*E) error

MultiCreate mysql动态插入多条数据

func (*MySQLDao[E]) One

func (receiver *MySQLDao[E]) One(entity E) (*E, error)

One mysql动态查询一条数据

func (*MySQLDao[E]) Pager

func (receiver *MySQLDao[E]) Pager(conn *gorm.DB, page, size int) ([]E, *models.ResultPage, error)

Pager mysql简单分页查询数据

func (*MySQLDao[E]) Save

func (receiver *MySQLDao[E]) Save(entity *E) error

Save mysql动态保存数据

func (*MySQLDao[E]) Updates

func (receiver *MySQLDao[E]) Updates(entity *E) error

Updates mysql动态更新数据

func (*MySQLDao[E]) Where

func (m *MySQLDao[E]) Where(query interface{}, args ...interface{}) *gorm.DB

Where mysql动态查询数据

func (*MySQLDao[E]) WithCache added in v2.0.8

func (m *MySQLDao[E]) WithCache(config CacheConfig) *MySQLDao[E]

WithCache enables configurable result caching for this DAO.

func (*MySQLDao[E]) WithContext

func (m *MySQLDao[E]) WithContext(ctx *context.Context) *MySQLDao[E]

type QueryOption

type QueryOption struct {
	Preloads []string
	OrderBy  []string
}

type RedisCache added in v2.0.8

type RedisCache struct {
	Client redis.UniversalClient
}

RedisCache adapts a go-redis client to the cache store used by MySQLDao.

func NewRedisCache added in v2.0.8

func NewRedisCache(client redis.UniversalClient) *RedisCache

func (*RedisCache) Add added in v2.0.8

func (c *RedisCache) Add(key interface{}, value interface{}, lifeSpan time.Duration)

func (*RedisCache) Clear added in v2.0.8

func (c *RedisCache) Clear() bool

func (*RedisCache) Close added in v2.0.8

func (c *RedisCache) Close()

func (*RedisCache) Delete added in v2.0.8

func (c *RedisCache) Delete(key interface{})

func (*RedisCache) Get added in v2.0.8

func (c *RedisCache) Get(key interface{}) (interface{}, bool)

func (*RedisCache) IsExist added in v2.0.8

func (c *RedisCache) IsExist(key interface{}) bool

func (*RedisCache) Range added in v2.0.8

func (c *RedisCache) Range(f func(key, value interface{}) bool)

func (*RedisCache) Set added in v2.0.8

func (c *RedisCache) Set(key interface{}, value interface{}, duration time.Duration)

func (*RedisCache) Value added in v2.0.8

func (c *RedisCache) Value(key interface{}) (interface{}, bool)

Jump to

Keyboard shortcuts

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