dbctx

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2026 License: AGPL-3.0, AGPL-3.0-or-later Imports: 2 Imported by: 0

Documentation

Overview

Package dbctx provides context helpers for database transactions. It enables RLS (Row Level Security) by storing transactions in context.Context, allowing repositories to transparently use either a transaction or raw DB connection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TxFromContext

func TxFromContext(ctx context.Context) *sql.Tx

TxFromContext extracts the transaction from the context if present. Returns nil if no transaction is stored in the context.

func WithTx

func WithTx(ctx context.Context, tx *sql.Tx) context.Context

WithTx returns a new context containing the given transaction. This is used by the RLS middleware to propagate the transaction through the request lifecycle.

Types

type Querier

type Querier interface {
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}

Querier is a common interface for *sql.DB and *sql.Tx. It allows repositories to work transparently with either a raw DB connection or a transaction, enabling RLS isolation via transactional set_config.

func GetQuerier

func GetQuerier(ctx context.Context, db *sql.DB) Querier

GetQuerier returns the Querier to use for database operations. If a transaction is present in the context (set by RLS middleware), it returns the transaction. Otherwise, it returns the raw DB connection.

This allows repositories to transparently benefit from RLS isolation when called within a transactional context, while still working correctly for operations that bypass RLS (e.g., migrations, admin tasks).

Jump to

Keyboard shortcuts

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