Documentation
¶
Index ¶
- Variables
- func BindGoFlags()
- func DefaultGormConfig() *gormDriver.Config
- func Delete(ctx dbContext.Context, model Table) error
- func EnsurePerformanceDiagnostics(ctx context.Context, dsn string) error
- func FreePort() (int, error)
- func InitDB(config api.Config) (*dbContext.Context, error)
- func NewDB(connection string) (*sql.DB, error)
- func NewGorm(connection string, config *gorm.Config) (*gorm.DB, error)
- func NewPgxPool(connection string) (*pgxpool.Pool, error)
- func Now() clause.Expr
- func ScanRows[T ~map[string]any](rows *sql.Rows) ([]T, error)
- func SetupDB(connectionString string, logName string) (gormDB *gormDriver.DB, pgxPool *pgxpool.Pool, err error)
- func StartEmbedded(cfg EmbeddedConfig) (string, func() error, error)
- type EmbeddedConfig
- type PoolConfig
- type RowScanner
- type Table
Constants ¶
This section is empty.
Variables ¶
var DefaultQueryTimeout = 30 * time.Second
var LogLevel string
LogLevel is the log level for gorm logger
Functions ¶
func DefaultGormConfig ¶
func DefaultGormConfig() *gormDriver.Config
DefaultGormConfig returns the default GORM configuration
func EnsurePerformanceDiagnostics ¶ added in v0.1.23
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
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 ¶
InitDB creates a context with database connections Note: Does NOT check for migrations - consumers should verify migrations separately
func NewDB ¶
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 NewPgxPool ¶
NewPgxPool creates a new pgx connection pool with OpenTelemetry tracing
func ScanRows ¶ added in v0.1.13
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