repo

package
v0.0.0-...-04f3e75 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBackend    = errors.New("repo: backend error")
	ErrNotFound   = errors.New("repo: model not found")
	ErrDuplicated = errors.New("repo: duplicated model")
)
View Source
var (
	// I represent a schema, table, column or any combination separated by "."
	I = goqu.I
	// C represents a column
	C = goqu.C
	// L represents a literal value
	L = goqu.L
	// And represent multiple AND operations together
	And = goqu.And
	// Or represent multiple OR operations together
	Or = goqu.Or
)

Functions

func AttachRelation

func AttachRelation[T, U model.Modelable](
	ctx context.Context,
	entries []T,
	getRelationId func(m T) *int64,
	setRelation func(m T, r U),
	listByIds func(ctx context.Context, ids []int64) (*response.ListResponse[U], error),
) error

func NewRepoError

func NewRepoError(err, internal error) error

Types

type AttachFunc

type AttachFunc[T model.Modelable] func(ctx context.Context, results []T, include string) error

type Connection

type Connection struct {
	Db    sql.Database
	Store sql.Executor
	// contains filtered or unexported fields
}

func NewTestConnection

func NewTestConnection(t *testing.T, withTransaction bool) *Connection

func (*Connection) Close

func (c *Connection) Close(t *testing.T)

type Ex

type Ex = goqu.Ex

type ExOr

type ExOr = goqu.ExOr

type Expression

type Expression = goqu.Expression

type GenericStore

type GenericStore[T model.Modelable] interface {
	First(ctx context.Context, expr Expression, order ...OrderedExpression) (T, error)
	Find(ctx context.Context, dest T, id int64) error
	CountBy(ctx context.Context, expr Expression) (int64, error)
	Get(ctx context.Context, id int64) (T, error)
	GetBy(ctx context.Context, expr Expression) (T, error)
	GetForUpdate(ctx context.Context, expr Expression, order ...OrderedExpression) (T, error)
	Exists(ctx context.Context, expr Expression) (bool, error)
	List(ctx context.Context, opts ...clause.FilterOption) (*response.ListResponse[T], error)
	ListBy(ctx context.Context, expr Expression, opts ...clause.FilterOption) (*response.ListResponse[T], error)
	ListByIds(ctx context.Context, ids []int64) (*response.ListResponse[T], error)
	ListEach(ctx context.Context, fn func(item T) error, opts ...clause.FilterOption) error
	ListByEach(ctx context.Context, expr Expression, fn func(item T) error, opts ...clause.FilterOption) error
	Insert(ctx context.Context, req T) error
	// Upsert inserts a new record in the database, if the target column has a conflict then updates the fields instead
	Upsert(ctx context.Context, req T, target string) (bool, error)
	// Update updates a record on the repository
	Update(ctx context.Context, req T) error
	// UpdateMap updates a record from the repository, only updates the specified fields in the map
	UpdateMap(ctx context.Context, id int64, req map[string]any) error
	// UpdateMapBy updates the records matched by the expression, only updates the specified fields in the map
	UpdateMapBy(ctx context.Context, req map[string]any, expr Expression) (int64, error)
	// Delete removes a record from the repository, returns ErrNotFound if the ID doesn't exist
	Delete(ctx context.Context, id int64) error
	// DeleteBy removes the records matched by the expression, returns the deleted count on success
	DeleteBy(ctx context.Context, expr Ex) (int64, error)
}

type GenericStoreImpl

type GenericStoreImpl[T model.Modelable] struct {
	Conn    sql.Executor
	Builder goqu.DialectWrapper
	Table   string
	// contains filtered or unexported fields
}

func NewStore

func NewStore[T model.Modelable](conn sql.Executor, opts ...StoreOption[T]) *GenericStoreImpl[T]

func (*GenericStoreImpl[T]) AttachFunc

func (s *GenericStoreImpl[T]) AttachFunc(fn AttachFunc[T])

func (*GenericStoreImpl[T]) CountBy

func (s *GenericStoreImpl[T]) CountBy(ctx context.Context, expr Expression) (int64, error)

func (*GenericStoreImpl[T]) Delete

func (s *GenericStoreImpl[T]) Delete(ctx context.Context, id int64) error

func (*GenericStoreImpl[T]) DeleteBy

func (s *GenericStoreImpl[T]) DeleteBy(ctx context.Context, expr Ex) (int64, error)

func (*GenericStoreImpl[T]) Exists

func (s *GenericStoreImpl[T]) Exists(ctx context.Context, expr Expression) (bool, error)

func (*GenericStoreImpl[T]) Find

func (s *GenericStoreImpl[T]) Find(ctx context.Context, dest T, id int64) error

Find returns a record from the database. If no record is found then a ErrNotFound is returned

func (*GenericStoreImpl[T]) First

func (s *GenericStoreImpl[T]) First(ctx context.Context, expr Expression, order ...OrderedExpression) (T, error)

func (*GenericStoreImpl[T]) Get

func (s *GenericStoreImpl[T]) Get(ctx context.Context, id int64) (T, error)

Get returns a record from the database. If no record is found then a ErrNotFound is returned

func (*GenericStoreImpl[T]) GetBy

func (s *GenericStoreImpl[T]) GetBy(ctx context.Context, expr Expression) (T, error)

GetBy returns the first record from the database matching the expression. If no record is found then a ErrNotFound is returned

func (*GenericStoreImpl[T]) GetForUpdate

func (s *GenericStoreImpl[T]) GetForUpdate(ctx context.Context, expr Expression, order ...OrderedExpression) (T, error)

func (*GenericStoreImpl[T]) Insert

func (s *GenericStoreImpl[T]) Insert(ctx context.Context, req T) error

func (*GenericStoreImpl[T]) List

func (*GenericStoreImpl[T]) ListBy

func (s *GenericStoreImpl[T]) ListBy(ctx context.Context, expr Expression, opts ...clause.FilterOption) (*response.ListResponse[T], error)

func (*GenericStoreImpl[T]) ListByEach

func (s *GenericStoreImpl[T]) ListByEach(ctx context.Context, expr Expression, fn func(item T) error, opts ...clause.FilterOption) error

func (*GenericStoreImpl[T]) ListByIds

func (s *GenericStoreImpl[T]) ListByIds(ctx context.Context, ids []int64) (*response.ListResponse[T], error)

func (*GenericStoreImpl[T]) ListEach

func (s *GenericStoreImpl[T]) ListEach(ctx context.Context, fn func(item T) error, opts ...clause.FilterOption) error

func (*GenericStoreImpl[T]) Update

func (s *GenericStoreImpl[T]) Update(ctx context.Context, req T) error

func (*GenericStoreImpl[T]) UpdateMap

func (s *GenericStoreImpl[T]) UpdateMap(ctx context.Context, id int64, req map[string]any) error

func (*GenericStoreImpl[T]) UpdateMapBy

func (s *GenericStoreImpl[T]) UpdateMapBy(ctx context.Context, req map[string]any, expr Expression) (int64, error)

func (*GenericStoreImpl[T]) Upsert

func (s *GenericStoreImpl[T]) Upsert(ctx context.Context, req T, target string) (bool, error)

func (*GenericStoreImpl[T]) WithTx

func (s *GenericStoreImpl[T]) WithTx(conn sql.Executor) *GenericStoreImpl[T]

type Op

type Op = goqu.Op

type OrderedExpression

type OrderedExpression = exp.OrderedExpression

type RepoError

type RepoError struct {
	Err error
	// contains filtered or unexported fields
}

func (*RepoError) Error

func (r *RepoError) Error() string

func (*RepoError) Unwrap

func (r *RepoError) Unwrap() error

type StoreOption

type StoreOption[T model.Modelable] func(c *GenericStoreImpl[T])

func WithExpressions

func WithExpressions[T model.Modelable](filters ...exp.Expression) StoreOption[T]

func WithFilters

func WithFilters[T model.Modelable](rules ...filter.Rule) StoreOption[T]

func WithIncludes

func WithIncludes[T model.Modelable](includes ...string) StoreOption[T]

func WithPaginatorOptions

func WithPaginatorOptions[T model.Modelable](opts ...paginator.Option) StoreOption[T]

func WithReturnFields

func WithReturnFields[T model.Modelable](fields ...any) StoreOption[T]

func WithSelectFields

func WithSelectFields[T model.Modelable](fields ...any) StoreOption[T]

func WithSortKeys

func WithSortKeys[T model.Modelable](keys ...string) StoreOption[T]

func WithTablePrefix

func WithTablePrefix[T model.Modelable](prefix string) StoreOption[T]

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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