bunrepo

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package bunrepo provides a repository pattern implementation for SQL.

Index

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 ParseColonOrders

func ParseColonOrders(orders ...string) (res []string)

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

type AddGetUpdOption interface {
	AddOption
	GetOption
	UpdOption
}

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 AddOption

type AddOption interface {
	ApplyAdd(*addOption)
}

func On

func On(q string) AddOption

On performs an upsert operation.

type DelOption

type DelOption interface {
	ApplyDel(o *delOption)
}

type GetOption

type GetOption interface {
	ApplyGet(o *getOption)
}

func Count

func Count() GetOption

Count count the query when call [Getm].

func ForUpdate

func ForUpdate() GetOption

ForUpdate locks the rows for an update.

type QueryOption

type QueryOption interface {
	GetOption
	UpdOption
	DelOption
}

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 New

func New[T any](db bun.IDB, opts ...RepoOption[T]) *Repo[T]

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]) Get

func (repo *Repo[T]) Get(ctx context.Context, entity *T, opts ...GetOption) (err error)

Get returns the entity by id.

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.

func (*Repo[T]) Updf

func (repo *Repo[T]) Updf(
	ctx context.Context,
	entity *T,
	f func(q *bun.UpdateQuery) *bun.UpdateQuery,
) (sql.Result, error)

Updf provides more customizations for updation via function.

func (*Repo[T]) Updm

func (repo *Repo[T]) Updm(ctx context.Context, entities []*T, opts ...UpdOption,
) (sql.Result, error)

type RepoOption

type RepoOption[T any] func(*Repo[T])

func WithReturning

func WithReturning[T any](r string) RepoOption[T]

type Tuple

type Tuple[A any, B any] struct {
	A A
	B B
}

Tuple is a group of 2 elements.

type UpdDelOption

type UpdDelOption interface {
	UpdOption
	DelOption
}

func Returning

func Returning(q string, args ...any) UpdDelOption

type UpdOption

type UpdOption interface {
	ApplyUpd(o *updOption)
}

func IncludeZero

func IncludeZero() UpdOption

IncludeZero will include zero fields when updating.

Jump to

Keyboard shortcuts

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