dbi

package
v0.0.0-...-d0f02a6 Latest Latest
Warning

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

Go to latest
Published: May 9, 2026 License: MIT Imports: 8 Imported by: 1

Documentation

Overview

Package dbi - database abstraction with transaction management and pagination helpers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(ctx context.Context, db DBI, val any, query string, vals ...any) error

func Paginate

func Paginate[T any](ctx context.Context, db DBI, page core.PageReq, out *core.Page[T], cursorFn func(T) string, query string, args ...any) error

Paginate runs a paginated query and returns a Page. Use $cursor and $limit placeholders in the query; they are replaced with positional placeholders based on the number of args. The cursor function extracts the cursor value from the last item.

func Read

func Read[T any](ctx context.Context, db *sql.DB, fn func(tx DBI) (T, error)) (T, error)

func ReadOnly

func ReadOnly(ctx context.Context, db *sql.DB, fn func(tx DBI) error) error

ReadOnly executes fn within a read-only transaction. The transaction is committed if fn returns nil, otherwise rolled back.

func ReadWrite

func ReadWrite(ctx context.Context, db *sql.DB, fn func(tx DBI) error) error

ReadWrite executes fn within a read-write transaction. The transaction is committed if fn returns nil, otherwise rolled back.

func TranslateNotFound

func TranslateNotFound(from, to error) error

func WithTx

func WithTx(ctx context.Context, db *sql.DB, opts *sql.TxOptions, fn func(tx DBI) error) error

func Write

func Write[T any](ctx context.Context, db *sql.DB, fn func(tx DBI) (T, error)) (T, error)

Types

type DBI

type DBI interface {
	Query(context.Context, string, ...any) (*sql.Rows, error)
	QueryRow(context.Context, string, ...any) *sql.Row
	Exec(context.Context, string, ...any) (sql.Result, error)
}

func WrapTx

func WrapTx(tx *sql.Tx) DBI

WrapTx wraps a *sql.Tx as a DBI.

type Factory

type Factory[T any] func(DBI) T

Source Files

  • dbi.go
  • get.go
  • scan.go

Jump to

Keyboard shortcuts

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