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

database

package
v0.0.0-...-87568c4 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Conn

func Conn(ctx context.Context) *sqlx.DB

Conn returns the *sqlx.DB for the current tenant, or defaultDB when the context is not tenant-scoped. It returns a zero sqlx.DB pointer if neither is registered, allowing callers to check for nil.

func InitDefault

func InitDefault(db *sqlx.DB)

InitDefault sets the global fallback connection used when ctx has no tenant.

func Open

func Open(dsn string) (*sqlx.DB, error)

func OpenProvider

func OpenProvider(ctx context.Context, dsn DSNProvider, opts Options) (*sqlx.DB, error)

func OpenWithOptions deprecated

func OpenWithOptions(ctx context.Context, dsn string, opts Options) (*sqlx.DB, error)

Deprecated: migrate to OpenProvider for rotational credentials.

func OpenWithPool

func OpenWithPool(dsn string, maxOpen, maxIdle int) (*sqlx.DB, error)

func Raw

func Raw(db *sqlx.DB) *sql.DB

Raw returns the underlying *sql.DB to interoperate with packages that do not understand sqlx.

func Rebind

func Rebind(query string) string

Rebind rewrites a QUESTION-bind query (`?`) for the active SQL dialect.

func RegisterTenant

func RegisterTenant(tenantID string, db *sqlx.DB)

RegisterTenant associates tenantID with db. Call once during tenant bootstrap (e.g., after migrations).

func SetEngine

func SetEngine(engine string) error

SetEngine configures global SQL dialect details used by OpenProvider and Rebind().

func WithTenant

func WithTenant(ctx context.Context, tenantID string) context.Context

WithTenant returns a child context tagged with tenantID.

Types

type DSNProvider

type DSNProvider func() string

DSNProvider returns a DSN string. Callers may fetch secrets from Vault or rotate credentials dynamically.

type Engine

type Engine string

Engine identifies which SQL dialect/driver should be used.

const (
	EnginePostgres  Engine = "postgres"
	EngineCockroach Engine = "cockroach"
	EngineMariaDB   Engine = "mariadb"
	EngineMySQL     Engine = "mysql"
)

type Options

type Options struct {
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime time.Duration
	Retries         int
	RetryBackoff    time.Duration
}

Options tunes pool behaviour and retry policy. Zero values fall back to sensible defaults (see defaultOpts).

Jump to

Keyboard shortcuts

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