Help us shape the future of Go! Take the 2026 Go Developer Survey

database

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package database provides PostgreSQL connection management with multi-tenant RLS support.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetUserID

func GetUserID(ctx context.Context) (uuid.UUID, bool)

GetUserID retrieves the user ID from the context.

func IsSuperadmin

func IsSuperadmin(ctx context.Context) bool

IsSuperadmin checks if the context has superadmin privileges.

func WithSuperadmin

func WithSuperadmin(ctx context.Context) context.Context

WithSuperadmin marks the context as having superadmin privileges.

func WithUser

func WithUser(ctx context.Context, userID uuid.UUID) context.Context

WithUser adds a user ID to the context.

Types

type DB

type DB struct {
	// contains filtered or unexported fields
}

DB wraps a pgx connection pool with tenant-aware operations.

func New

func New(ctx context.Context, cfg config.DatabaseConfig) (*DB, error)

New creates a new database connection pool.

func NewWithConfig

func NewWithConfig(ctx context.Context, poolConfig *pgxpool.Config) (*DB, error)

NewWithConfig creates the pool from a ready pgx config, for callers that need a custom one (tests attach a pgx.QueryTracer to count statements).

func (*DB) Close

func (db *DB) Close()

Close closes the database connection pool.

func (*DB) ExecWithTimeout

func (db *DB) ExecWithTimeout(ctx context.Context, timeout time.Duration, sql string, args ...any) error

ExecWithTimeout executes a query with a timeout.

func (*DB) GetConn

func (db *DB) GetConn(ctx context.Context) (*pgxpool.Conn, error)

GetConn returns a connection with tenant context configured for RLS. This is the primary way to acquire database connections.

func (*DB) Ping

func (db *DB) Ping(ctx context.Context) error

Ping verifies the database connection.

func (*DB) Pool

func (db *DB) Pool() *pgxpool.Pool

Pool returns the underlying connection pool.

func (*DB) WithTx

func (db *DB) WithTx(ctx context.Context, fn func(tx pgx.Tx) error) error

WithTx executes a function within a transaction with tenant context.

Jump to

Keyboard shortcuts

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