repoutil

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: LGPL-2.1 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseConds added in v0.0.4

func ParseConds(conds []any) []any

ParseConds returns standard [sqlString, vars] format for query, powered by gowhere package (with default config)

func ParseSortParam added in v0.0.4

func ParseSortParam(s string) [][]string

ParseSortParam returns list of [column, direction] from comma separated sorting param

func ParseSortValue added in v0.0.4

func ParseSortValue(s string) (col, dir string)

ParseSortValue returns the column name and direction for sorting value like +column or -column

func WithPaging added in v0.0.4

func WithPaging(db *gorm.DB, page, perPage int) *gorm.DB

WithPaging an util function to set pagination conditions

func WithSorting added in v0.0.4

func WithSorting(db *gorm.DB, sort string, quoteCol func(name string) string) *gorm.DB

WithSorting an util function to set sorting conditions WARNING: SQL Injection vulnerability! `quoteCol` function must take care of quoting column name properly

Types

type Intf added in v0.0.6

type Intf[T any] interface {
	Create(ctx context.Context, input *T) error
	CreateInBatches(ctx context.Context, input []T, batchSize int) error
	Read(ctx context.Context, output *T, conds ...any) error
	ReadByID(ctx context.Context, output *T, id string) error
	ReadByUpdate(ctx context.Context, options string, output *T, conds ...any) error
	List(ctx context.Context, output interface{}, conds ...any) error
	Update(ctx context.Context, updates any, conds ...any) error
	Delete(ctx context.Context, conds ...any) error
	Count(ctx context.Context, count *int64, conds ...any) error
	Existed(ctx context.Context, conds ...any) (bool, error)
	ReadAllByCondition(ctx context.Context, output interface{}, count *int64, lqc *requestutil.ListQueryCondition) error
}

Intf represents the common interface for all repositories

type Repo

type Repo[T any] struct {
	GDB *gorm.DB
}

Repo represents the client for common usages

func NewRepo

func NewRepo[T any](db *gorm.DB) *Repo[T]

NewRepo creates new Repo instance

func (*Repo[T]) Count

func (d *Repo[T]) Count(ctx context.Context, count *int64, conds ...any) error

Count counts records that match given conditions

func (*Repo[T]) Create

func (d *Repo[T]) Create(ctx context.Context, input *T) error

Create creates a new record

func (*Repo[T]) CreateInBatches

func (d *Repo[T]) CreateInBatches(ctx context.Context, input []T, batchSize int) error

CreateInBatches creates multiple records in batches

func (*Repo[T]) Delete

func (d *Repo[T]) Delete(ctx context.Context, conds ...any) error

Delete deletes a record by conditions

func (*Repo[T]) Existed

func (d *Repo[T]) Existed(ctx context.Context, conds ...any) (bool, error)

Existed checks if a record exists by conditions

func (*Repo[T]) List

func (d *Repo[T]) List(ctx context.Context, output interface{}, conds ...any) error

List gets all records that match given conditions

func (*Repo[T]) QuoteCol added in v0.0.5

func (r *Repo[T]) QuoteCol(name string) string

QuoteCol quotes the column name

func (*Repo[T]) Read

func (d *Repo[T]) Read(ctx context.Context, output *T, conds ...any) error

Read get a record by conds

func (*Repo[T]) ReadAllByCondition

func (d *Repo[T]) ReadAllByCondition(ctx context.Context, output interface{}, count *int64, lqc *requestutil.ListQueryCondition) error

ReadAllByCondition retrieves a list of entities based on the provided query conditions.

func (*Repo[T]) ReadByID

func (d *Repo[T]) ReadByID(ctx context.Context, output *T, id string) error

ReadByID gets a record by primary key

func (*Repo[T]) ReadByUpdate

func (d *Repo[T]) ReadByUpdate(ctx context.Context, options string, output *T, conds ...any) error

ReadByUpdate gets a record and lock it for update

func (*Repo[T]) Update

func (d *Repo[T]) Update(ctx context.Context, updates any, conds ...any) error

Update updates a record by conditions

Jump to

Keyboard shortcuts

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