r3bun

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package r3bun provides an r3.CRUD[T, ID] driver backed by Bun, a SQL-first Go ORM for PostgreSQL, MySQL, MSSQL, and SQLite.

Driver: github.com/uptrace/bun Source: https://github.com/uptrace/bun

Supported r3 features:

  • Full CRUD (Create, Get, List, Update, Delete)
  • Filters, Sorts, Pagination via the r3 SQL dialect
  • Preloads via Bun's Relation() (belongs-to, has-one, has-many, many-to-many)
  • IncludeTrashed via Bun's WhereAllWithDeleted() (requires soft-delete model setup)
  • Restore and HardDelete via the features/softdelete SoftDeleter interface
  • Thread-safe default queries (SetDefaultListQuery, SetDefaultGetQuery)
  • Raw escape hatch (BunRaw) for custom bun.SelectQuery usage

Limitations / notes:

  • Bun wraps database/sql natively; use db.DB (the underlying *sql.DB) for goose migrations or any raw database/sql usage.
  • Model structs must embed bun.BaseModel with a table tag and use `bun` struct tags.
  • Aggregate / custom-shape queries should use Raw().Scan() into a dedicated struct, since Raw().Find() scans into []T and Bun rejects unknown columns.
  • Bun is the recommended successor to go-pg by the same authors.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewBunQuerier

func NewBunQuerier[T any, ID comparable](db *bun.DB, opts ...r3.Option) r3.Querier[T, ID]

NewBunQuerier creates a read-only Bun-based repository. Returns r3.Querier — a compile-time guarantee of read-only access.

Types

type BunCRUD

type BunCRUD[T any, ID comparable] struct {
	enginesql.DefaultsManager

	Config r3.Config
	// contains filtered or unexported fields
}

BunCRUD is a CRUD repository based on Bun's *bun.DB. Internally it uses bun.IDB so that it can operate within transactions as well.

func NewBunCRUD

func NewBunCRUD[T any, ID comparable](db *bun.DB, opts ...r3.Option) *BunCRUD[T, ID]

NewBunCRUD creates a new Bun-based CRUD repository.

Accepts optional r3.Option values for framework-level configuration.

func (*BunCRUD[T, ID]) Aggregate added in v0.0.3

func (r *BunCRUD[T, ID]) Aggregate(ctx context.Context, qarg ...r3.Query) ([]r3.AggregateRow, error)

Aggregate computes grouped aggregates over the records matching the query. See r3.Aggregator for the query semantics.

func (*BunCRUD[T, ID]) BeginTx

func (r *BunCRUD[T, ID]) BeginTx(ctx context.Context) (r3.TxCRUD[T, ID], error)

BeginTx starts a Bun transaction and returns an r3.TxCRUD scoped to it. The caller must call Commit or Rollback on the returned TxCRUD.

func (*BunCRUD[T, ID]) Count

func (r *BunCRUD[T, ID]) Count(ctx context.Context, qarg ...r3.Query) (int64, error)

Count returns the number of records matching the query's filters.

func (*BunCRUD[T, ID]) Create

func (r *BunCRUD[T, ID]) Create(ctx context.Context, entity T) (T, error)

func (*BunCRUD[T, ID]) DB

func (r *BunCRUD[T, ID]) DB() *bun.DB

DB returns the underlying *bun.DB for advanced usage. Panics if BunCRUD is operating inside a transaction.

func (*BunCRUD[T, ID]) Delete

func (r *BunCRUD[T, ID]) Delete(ctx context.Context, id ID) error

func (*BunCRUD[T, ID]) Get

func (r *BunCRUD[T, ID]) Get(ctx context.Context, id ID, qarg ...r3.Query) (T, error)

func (*BunCRUD[T, ID]) HardDelete

func (r *BunCRUD[T, ID]) HardDelete(ctx context.Context, id ID) error

HardDelete permanently removes a record, bypassing Bun's soft-delete.

func (*BunCRUD[T, ID]) List

func (r *BunCRUD[T, ID]) List(ctx context.Context, qarg ...r3.Query) ([]T, int64, error)

func (*BunCRUD[T, ID]) Patch

func (r *BunCRUD[T, ID]) Patch(ctx context.Context, entity T, fields r3.Fields) (T, error)

func (*BunCRUD[T, ID]) Raw

func (r *BunCRUD[T, ID]) Raw() *BunRaw[T, ID]

Raw returns the BunRaw escape hatch for custom queries.

func (*BunCRUD[T, ID]) Restore

func (r *BunCRUD[T, ID]) Restore(ctx context.Context, id ID) error

Restore un-deletes a soft-deleted record by clearing its soft-delete column.

func (*BunCRUD[T, ID]) Update

func (r *BunCRUD[T, ID]) Update(ctx context.Context, entity T) (T, error)

type BunRaw

type BunRaw[T any, ID any] struct {
	// contains filtered or unexported fields
}

BunRaw is a Bun wrapper that allows calling any Bun query. Is considered to be embedded in BunCRUD.

func NewBunRaw

func NewBunRaw[T any, ID comparable](db bun.IDB) *BunRaw[T, ID]

NewBunRaw creates a new BunRaw instance.

func (*BunRaw[T, ID]) Find

func (r *BunRaw[T, ID]) Find(ctx context.Context, cb func(*bun.SelectQuery) *bun.SelectQuery) ([]T, error)

Find executes a custom select query and returns the results. The callback receives a *bun.SelectQuery to build a custom query.

func (*BunRaw[T, ID]) Scan

func (r *BunRaw[T, ID]) Scan(ctx context.Context, cb func(*bun.SelectQuery) *bun.SelectQuery, dest any) error

Scan executes a custom select query and scans results into the provided destination.

Jump to

Keyboard shortcuts

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