repo

package
v1.8.94 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2024 License: LGPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateUserPaginator

func CreateUserPaginator(cursor paginator.Cursor, order *paginator.Order, limit *int) *paginator.Paginator

Types

type BaseRepo

type BaseRepo[T any] struct{ Orm *gorm.DB }

func NewBaseRepo

func NewBaseRepo[T any]() *BaseRepo[T]

NewBaseRepo 暂未使用接口返回

func (*BaseRepo[T]) CountAll

func (r *BaseRepo[T]) CountAll() (int64, error)

CountAll 指定表无条件统计全部数量

func (*BaseRepo[T]) DeleteBy

func (r *BaseRepo[T]) DeleteBy(condition map[string]interface{}, hardDelete bool) error

DeleteBy 根据给定条件删除记录,可选是否硬删除(仅对于有软删除的表)

func (*BaseRepo[T]) DeleteById

func (r *BaseRepo[T]) DeleteById(id uint64) error

DeleteById 按照ID删除一条

func (*BaseRepo[T]) Exec

func (r *BaseRepo[T]) Exec(sql string, values ...interface{}) (*gorm.DB, error)

Exec 执行原生sql语句

func (*BaseRepo[T]) FindBy

func (r *BaseRepo[T]) FindBy(condition map[string]interface{}) (*T, error)

FindBy 根据条件查找一条记录

func (*BaseRepo[T]) FindByID

func (r *BaseRepo[T]) FindByID(id uint64) (*T, error)

FindByID 按照ID读取一条记录 本repo的find均代表找1条记录 select均代表多条记录

func (*BaseRepo[T]) FindByLock

func (r *BaseRepo[T]) FindByLock(condition map[string]interface{}) (*T, error)

FindByLock 根据条件查找一条记录,并使用 SELECT FOR UPDATE 锁定 (需要配合外部事务)

func (*BaseRepo[T]) GetByPage

func (r *BaseRepo[T]) GetByPage(pageNum uint32, pageSize uint32, opts ...SelOpt) (*Pagination[T], error)

GetByPage 根据分页获取记录

func (*BaseRepo[T]) Insert

func (r *BaseRepo[T]) Insert(model *T) error

Insert 插入一条记录

func (*BaseRepo[T]) InsertOrIgnore

func (r *BaseRepo[T]) InsertOrIgnore(model *T, condition map[string]interface{}) (int64, error)

InsertOrIgnore 无显式事务 先查找,存在则忽略,否则插入 (并发性也可以由数据库相同的unique key来保证)

func (*BaseRepo[T]) InsertOrUpdate

func (r *BaseRepo[T]) InsertOrUpdate(insertItem *T, condition map[string]interface{}, updateValues *T) error

InsertOrUpdate 事务版本 先查找,不存在则插入,存在则更新

func (*BaseRepo[T]) SelectBy

func (r *BaseRepo[T]) SelectBy(condition map[string]interface{}, results *[]*T, opts ...SelOpt) error

SelectBy 按照条件查找多条 可使用链式方法添加order和limit等参数

func (*BaseRepo[T]) UpdateBy

func (r *BaseRepo[T]) UpdateBy(condition map[string]interface{}, updateValues map[string]interface{}) (int64, error)

UpdateBy 根据条件更新记录

func (*BaseRepo[T]) UpdateById

func (r *BaseRepo[T]) UpdateById(id uint64, updateValues map[string]interface{}) error

UpdateById 按照ID更新一条

func (*BaseRepo[T]) Upsert

func (r *BaseRepo[T]) Upsert(model *T, condition map[string]interface{}) error

Upsert 非显式事务(onConflict和clauses),根据condition查找记录,如果存在则更新,如果不存在则创建

func (*BaseRepo[T]) UpsertByID

func (r *BaseRepo[T]) UpsertByID(model *T, updateFields []string) error

UpsertByID 非显式事务(onConflict和clauses),固定根据id查找记录,如果存在则更新,如果不存在则创建

type Cursor

type Cursor struct {
	After  *string `json:"after" query:"after"`
	Before *string `json:"before" query:"before"`
}

type Pagination

type Pagination[T any] struct {
	TotalRecords int64  `json:"totalRecords"`
	TotalPages   int64  `json:"totalPages"`
	CurrentPage  uint32 `json:"currentPage"`
	PageSize     uint32 `json:"pageSize"`
	Records      []*T   `json:"records"`
}

Pagination 定义

type SelOpt

type SelOpt func(*gorm.DB) *gorm.DB

SelOpt 查询可选选项

func WithLimit

func WithLimit(limit int) SelOpt

func WithOrderBy

func WithOrderBy(orderBy string) SelOpt

func WithSelect

func WithSelect(columns []string) SelOpt

func WithWhere

func WithWhere(conditions map[string]interface{}) SelOpt

type User

type User struct {
	ID       int
	JoinedAt time.Time `gorm:"column:created_at"`
}

Jump to

Keyboard shortcuts

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