database

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2025 License: BSD-3-Clause-Clear Imports: 13 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CommitTransactionForQuerier

func CommitTransactionForQuerier(ctx context.Context, db DBTX) error

CommitTransactionForQuerier contains the shared Commit() logic from both packages

func OpenDB

func OpenDB(ctx context.Context, options ConfigOptions) (*sql.DB, error)

OpenDB opens a database connection with the specified configuration options

func OpenDBMonitor

func OpenDBMonitor(ctx context.Context) (*sql.DB, error)

OpenDBMonitor opens a database connection with monitor-specific defaults This is a convenience function for Monitor package compatibility

func OpenDBWithConfigFile

func OpenDBWithConfigFile(ctx context.Context, configFile string) (*sql.DB, error)

OpenDBWithConfigFile opens a database connection using an explicit config file path This is a convenience function for API package compatibility

func RollbackTransactionForQuerier

func RollbackTransactionForQuerier(ctx context.Context, db DBTX) error

RollbackTransactionForQuerier contains the shared Rollback() logic from both packages

func WithReadOnlyTransaction

func WithReadOnlyTransaction[Q TX](ctx context.Context, db DB[Q], fn func(ctx context.Context, q Q) error) error

WithReadOnlyTransaction executes a read-only function within a transaction Always rolls back at the end (for consistent read isolation)

func WithTransaction

func WithTransaction[Q TX](ctx context.Context, db DB[Q], fn func(ctx context.Context, q Q) error) error

WithTransaction executes a function within a database transaction Handles proper rollback on error and commit on success

Types

type BaseBeginner

type BaseBeginner interface {
	Begin(context.Context) (sql.Tx, error)
}

Shared interface definitions that both packages use identically

type BaseQuerier

type BaseQuerier interface {
	WithTx(tx *sql.Tx) BaseQuerier
}

BaseQuerier provides basic query functionality This interface should be implemented by package-specific Queries types

type BaseQuerierTx

type BaseQuerierTx interface {
	BaseQuerier
	Begin(ctx context.Context) (BaseQuerierTx, error)
	Commit(ctx context.Context) error
	Rollback(ctx context.Context) error
}

BaseQuerierTx provides transaction functionality This interface should be implemented by package-specific Queries types

type BaseTx

type BaseTx interface {
	BaseBeginner
	Commit(ctx context.Context) error
	Rollback(ctx context.Context) error
}

type Config

type Config struct {
	// MySQL configuration (use this OR Postgres, not both)
	MySQL *MySQLConfig `yaml:"mysql,omitempty"`
	// Postgres configuration (use this OR MySQL, not both)
	Postgres *PostgresConfig `yaml:"postgres,omitempty"`

	// Legacy flat PostgreSQL format (deprecated, for backward compatibility only)
	// If neither MySQL nor Postgres is set, these fields will be used for PostgreSQL
	User    string `yaml:"user,omitempty"`
	Pass    string `yaml:"pass,omitempty"`
	Host    string `yaml:"host,omitempty"`
	Port    uint16 `yaml:"port,omitempty"`
	Name    string `yaml:"name,omitempty"`
	SSLMode string `yaml:"sslmode,omitempty"`
}

Config represents the database configuration structure

func (*Config) Validate added in v0.6.0

func (c *Config) Validate() error

Validate ensures the configuration is valid and unambiguous

type ConfigOptions

type ConfigOptions struct {
	// ConfigFiles is a list of config file paths to search for database configuration
	ConfigFiles []string

	// EnablePoolMonitoring enables connection pool metrics collection
	EnablePoolMonitoring bool

	// PrometheusRegisterer for metrics collection. If nil, no metrics are collected.
	PrometheusRegisterer prometheus.Registerer

	// Connection pool settings
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime time.Duration
}

ConfigOptions allows customization of database opening behavior

func DefaultConfigOptions

func DefaultConfigOptions() ConfigOptions

DefaultConfigOptions returns the standard configuration options used by API package

func MonitorConfigOptions

func MonitorConfigOptions() ConfigOptions

MonitorConfigOptions returns configuration options optimized for Monitor package

type CreateConnectorFunc

type CreateConnectorFunc func() (driver.Connector, error)

CreateConnectorFunc is a function that creates a database connector

type DB

type DB[Q any] interface {
	Begin(ctx context.Context) (Q, error)
}

DB interface for database operations that can begin transactions

type DBConfig

type DBConfig = MySQLConfig

DBConfig is a legacy alias for MySQLConfig

type DBTX

type DBTX interface {
	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
	PrepareContext(context.Context, string) (*sql.Stmt, error)
	QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
	QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}

DBTX matches the interface expected by SQLC-generated code This interface is implemented by both *sql.DB and *sql.Tx

func BeginTransactionForQuerier

func BeginTransactionForQuerier(ctx context.Context, db DBTX) (DBTX, error)

BeginTransactionForQuerier contains the shared Begin() logic from both packages

type DatabaseMetrics

type DatabaseMetrics struct {
	ConnectionsOpen         prometheus.Gauge
	ConnectionsIdle         prometheus.Gauge
	ConnectionsInUse        prometheus.Gauge
	ConnectionsWaitCount    prometheus.Counter
	ConnectionsWaitDuration prometheus.Histogram
	// contains filtered or unexported fields
}

DatabaseMetrics holds the Prometheus metrics for database connection pool monitoring

func NewDatabaseMetrics

func NewDatabaseMetrics(registerer prometheus.Registerer) *DatabaseMetrics

NewDatabaseMetrics creates a new set of database metrics and registers them

type Driver

type Driver struct {
	CreateConnectorFunc CreateConnectorFunc
}

Driver implements the sql/driver interface with dynamic configuration

func (Driver) Connect

func (d Driver) Connect(ctx context.Context) (driver.Conn, error)

Connect creates a new database connection using the dynamic connector

func (Driver) Driver

func (d Driver) Driver() driver.Driver

Driver returns the driver instance

func (Driver) Open

func (d Driver) Open(name string) (driver.Conn, error)

Open is not supported for dynamic configuration

type MySQLConfig added in v0.6.0

type MySQLConfig struct {
	DSN    string `yaml:"dsn" default:"" flag:"dsn" usage:"Database DSN"`
	User   string `yaml:"user" default:"" flag:"user"`
	Pass   string `yaml:"pass" default:"" flag:"pass"`
	DBName string `yaml:"name,omitempty"` // Optional database name override
}

MySQLConfig represents the MySQL database configuration

type PostgresConfig added in v0.6.0

type PostgresConfig struct {
	User    string `yaml:"user"`
	Pass    string `yaml:"pass"`
	Host    string `yaml:"host"`
	Port    uint16 `yaml:"port"`
	Name    string `yaml:"name"`
	SSLMode string `yaml:"sslmode"`
}

PostgresConfig represents the PostgreSQL database configuration

type TX

type TX interface {
	Commit(ctx context.Context) error
	Rollback(ctx context.Context) error
}

TX interface for transaction operations

type TransactionFunc

type TransactionFunc[Q any] func(ctx context.Context, q Q) error

TransactionFunc represents a function that operates within a database transaction This is used by the shared transaction helpers in transaction.go

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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