db

package
v0.1.27 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultQueryTimeout = 30 * time.Second
View Source
var LogLevel string

LogLevel is the log level for gorm logger

Functions

func BindGoFlags

func BindGoFlags()

BindGoFlags binds database flags to the provided flag set

func DefaultGormConfig

func DefaultGormConfig() *gormDriver.Config

DefaultGormConfig returns the default GORM configuration

func Delete

func Delete(ctx dbContext.Context, model Table) error

Delete performs a soft delete on the given model

func EnsurePerformanceDiagnostics added in v0.1.23

func EnsurePerformanceDiagnostics(ctx context.Context, dsn string) error

EnsurePerformanceDiagnostics validates server-level settings before installing pg_stat_statements in the selected database. Server settings are checked first so an externally managed instance fails without partial DDL.

func FreePort added in v0.1.7

func FreePort() (int, error)

FreePort binds :0 to discover a free TCP port. Public so callers can reuse it for adjacent services (e.g. postgrest) that need an unclaimed port.

func InitDB

func InitDB(config api.Config) (*dbContext.Context, error)

InitDB creates a context with database connections Note: Does NOT check for migrations - consumers should verify migrations separately

func NewDB

func NewDB(connection string) (*sql.DB, error)

NewDB creates a new sql.DB connection with a bounded pool. Callers that need different bounds apply their own PoolConfig to the returned handle.

func NewGorm

func NewGorm(connection string, config *gorm.Config) (*gorm.DB, error)

NewGorm creates a new Gorm DB connection using the provided connection string

func NewPgxPool

func NewPgxPool(connection string) (*pgxpool.Pool, error)

NewPgxPool creates a new pgx connection pool with OpenTelemetry tracing

func Now

func Now() clause.Expr

Now returns a SQL expression for the current timestamp

func ScanRows added in v0.1.13

func ScanRows[T ~map[string]any](rows *sql.Rows) ([]T, error)

ScanRows scans all rows of a *sql.Rows into a slice of map-like records, keyed by column name. Ported from duty/db so SQL data providers can return generic rows without a typed model.

func SetupDB

func SetupDB(connectionString string, logName string) (gormDB *gormDriver.DB, pgxPool *pgxpool.Pool, err error)

SetupDB creates database connections (gorm.DB and pgxpool.Pool) Note: This does NOT run migrations - consumers should handle migrations separately

func StartEmbedded added in v0.1.7

func StartEmbedded(cfg EmbeddedConfig) (string, func() error, error)

StartEmbedded launches PostgreSQL under cfg.DataDir. Reused servers and servers configured with KeepRunning are never stopped by the returned closer.

Types

type EmbeddedConfig added in v0.1.7

type EmbeddedConfig struct {
	// DataDir is the root for the data, runtime, and bin subdirectories.
	// The caller owns this choice; StartEmbedded does not pick a default path.
	DataDir string
	// Database created on first start. Defaults to "postgres" when blank.
	Database string
	// Port to bind. Zero picks a free port via FreePort().
	Port uint32
	// Username / Password. Default to "postgres"/"postgres" — fine for the
	// localhost-only instances this helper is meant for.
	Username, Password string
	// Logger receives the embedded-postgres / pg_ctl diagnostic output. Defaults
	// to os.Stderr (the library default is os.Stdout, which would corrupt callers
	// that write binary or structured data to stdout).
	Logger io.Writer
	// PerformanceDiagnostics preloads pg_stat_statements, enables I/O timing,
	// installs the extension, and fails startup if any part is unavailable.
	PerformanceDiagnostics bool
	// KeepRunning leaves a server started by this call running when stop is
	// invoked. Reused servers are always left running.
	KeepRunning bool
	// FastTesting trades durability for speed and raises the connection ceiling.
	// It is only suitable for disposable local test data.
	FastTesting bool
}

EmbeddedConfig configures StartEmbedded.

type PoolConfig added in v0.1.23

type PoolConfig struct {
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxIdleTime time.Duration
	ConnMaxLifetime time.Duration
}

PoolConfig bounds a database/sql pool. The zero value is deliberately not useful: an unbounded pool turns a burst of goroutines into a burst of backends contending for the same rows, and database/sql's default ceiling of two idle connections then reconnects for every one of them.

func DefaultPoolConfig added in v0.1.23

func DefaultPoolConfig() PoolConfig

DefaultPoolConfig mirrors the floor NewPgxPool already enforces. Idle equals open so a bursty workload reuses connections rather than reconnecting, and the lifetime cap keeps a long-lived process from pinning server-side state.

func (PoolConfig) Apply added in v0.1.23

func (c PoolConfig) Apply(db *sql.DB)

type RowScanner added in v0.1.15

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

RowScanner scans the current database/sql row into a JSON-friendly map while retaining only one decoded row in memory.

func NewRowScanner added in v0.1.15

func NewRowScanner(rows *sql.Rows) (*RowScanner, error)

func (*RowScanner) Err added in v0.1.15

func (s *RowScanner) Err() error

func (*RowScanner) Next added in v0.1.15

func (s *RowScanner) Next() bool

func (*RowScanner) Row added in v0.1.15

func (s *RowScanner) Row() map[string]any

type Table

type Table interface {
	TableName() string
}

Table interface for models with TableName

Jump to

Keyboard shortcuts

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