Documentation
¶
Overview ¶
Package bunrepo provides a repository pattern implementation for SQL.
Index ¶
- func BuildQuery(p any) (func(bun.QueryBuilder) bun.QueryBuilder, error)
- func For(q *bun.SelectQuery, f dbz.For) *bun.SelectQuery
- func List(q *bun.SelectQuery, p dbz.ListParams) *bun.SelectQuery
- func ParseColonOrders(orders ...string) (res []string)
- func ParseColonOrdersParams(p dbz.ListParams) dbz.ListParams
- func WithColonOrders(p dbz.ListParams, orders ...string) dbz.ListParams
- type AddGetUpdOption
- type AddOption
- type DelOption
- type GetOption
- type QueryOption
- type Repo
- func (repo *Repo[T]) Add(ctx context.Context, entity *T, opts ...AddOption) (res sql.Result, err error)
- func (repo *Repo[T]) Addf(ctx context.Context, entity *T, f func(q *bun.InsertQuery) *bun.InsertQuery) (res sql.Result, err error)
- func (repo *Repo[T]) Addm(ctx context.Context, entities []*T, opts ...AddOption) (res sql.Result, err error)
- func (repo *Repo[T]) Del(ctx context.Context, entity *T, opts ...DelOption) (res sql.Result, err error)
- func (repo *Repo[T]) Delf(ctx context.Context, entity *T, f func(q *bun.DeleteQuery) *bun.DeleteQuery) (sql.Result, error)
- func (repo *Repo[T]) Delm(ctx context.Context, entities []*T, opts ...DelOption) (res sql.Result, err error)
- func (repo *Repo[T]) Get(ctx context.Context, entity *T, opts ...GetOption) (err error)
- func (repo *Repo[T]) Getf(ctx context.Context, entity *T, f func(q *bun.SelectQuery) *bun.SelectQuery) (err error)
- func (repo *Repo[T]) Getm(ctx context.Context, lp dbz.ListParams, p any, opts ...GetOption) (res []*T, n int, err error)
- func (repo *Repo[T]) Upd(ctx context.Context, entity *T, opts ...UpdOption) (res sql.Result, err error)
- func (repo *Repo[T]) Updf(ctx context.Context, entity *T, f func(q *bun.UpdateQuery) *bun.UpdateQuery) (sql.Result, error)
- func (repo *Repo[T]) Updm(ctx context.Context, entities []*T, opts ...UpdOption) (sql.Result, error)
- type RepoOption
- type Tuple
- type UpdDelOption
- type UpdOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildQuery ¶
func BuildQuery(p any) (func(bun.QueryBuilder) bun.QueryBuilder, error)
BuildQuery builds a bun.QueryBuilder func from a query struct, which can be passed to the `ApplyQueryBuilder' func.
It will ignore zero value fields and use the `fp` tag as the column name. If no tag, it will generate column names from struct field names by underscoring them. For example, struct field `UserID` gets column name `user_id`. If the `fp` tag is set to "-", the field will be skipped.
func For ¶
func For(q *bun.SelectQuery, f dbz.For) *bun.SelectQuery
For sets row locking for Bun ORM.
func List ¶
func List(q *bun.SelectQuery, p dbz.ListParams) *bun.SelectQuery
func ParseColonOrders ¶
ParseColonOrders parses orders with colons like `name:desc` or `age:asc:nulls:first`.
func ParseColonOrdersParams ¶
func ParseColonOrdersParams(p dbz.ListParams) dbz.ListParams
func WithColonOrders ¶
func WithColonOrders(p dbz.ListParams, orders ...string) dbz.ListParams
Types ¶
type AddGetUpdOption ¶
func Columns ¶
func Columns(cols ...string) AddGetUpdOption
Columns specifies the columns which will be modified in the update.
func ExcludeColumns ¶
func ExcludeColumns(cols ...string) AddGetUpdOption
ExcludeColumns specifies the columns which will be excluded from the query.
type QueryOption ¶
func Where ¶
func Where(query string, args ...any) QueryOption
func WherePK ¶
func WherePK(cols ...string) QueryOption
WherePK specifies the columns which will be used in where clause, default to primary key.
type Repo ¶
type Repo[T any] struct { // contains filtered or unexported fields }
Repo is the repository. T is the entity type.
func (*Repo[T]) Add ¶
func (repo *Repo[T]) Add(ctx context.Context, entity *T, opts ...AddOption, ) (res sql.Result, err error)
Add adds a new entity to the repository.
func (*Repo[T]) Addf ¶
func (repo *Repo[T]) Addf( ctx context.Context, entity *T, f func(q *bun.InsertQuery) *bun.InsertQuery, ) (res sql.Result, err error)
Addf provides more customizations for addition via function.
func (*Repo[T]) Addm ¶
func (repo *Repo[T]) Addm(ctx context.Context, entities []*T, opts ...AddOption, ) (res sql.Result, err error)
Addm adds multiple entities to the repository.
func (*Repo[T]) Del ¶
func (repo *Repo[T]) Del(ctx context.Context, entity *T, opts ...DelOption, ) (res sql.Result, err error)
Del deletes the entity from the repository.
func (*Repo[T]) Delf ¶
func (repo *Repo[T]) Delf( ctx context.Context, entity *T, f func(q *bun.DeleteQuery) *bun.DeleteQuery, ) (sql.Result, error)
Delf provides more customizations for deletion via function.
func (*Repo[T]) Delm ¶
func (repo *Repo[T]) Delm(ctx context.Context, entities []*T, opts ...DelOption, ) (res sql.Result, err error)
Delm deletes multiple entities from the repository.
func (*Repo[T]) Getf ¶
func (repo *Repo[T]) Getf( ctx context.Context, entity *T, f func(q *bun.SelectQuery) *bun.SelectQuery, ) (err error)
func (*Repo[T]) Getm ¶
func (repo *Repo[T]) Getm(ctx context.Context, lp dbz.ListParams, p any, opts ...GetOption, ) (res []*T, n int, err error)
Getm gets multiple entities.
func (*Repo[T]) Upd ¶
func (repo *Repo[T]) Upd(ctx context.Context, entity *T, opts ...UpdOption, ) (res sql.Result, err error)
Upd updates an entity.
type RepoOption ¶
func WithReturning ¶
func WithReturning[T any](r string) RepoOption[T]
type UpdDelOption ¶
func Returning ¶
func Returning(q string, args ...any) UpdDelOption