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 ¶
TxFromContext extracts the transaction from the context if present. Returns nil if no transaction is stored in the context.
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 ¶
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).