Documentation
¶
Overview ¶
Package pgrepo provides a configurable postgres pool.
It builds a connection pool to a postgres database, with an instrumented driver for tracing.
Index ¶
- Constants
- Variables
- func CreateDB(parentCtx context.Context, dbName string, opts ...Option) (bool, error)
- func DefaultSettings() *viper.Viper
- func DropDB(parentCtx context.Context, dbName string, opts ...Option) (bool, error)
- func EnsureDB(ctx context.Context, dbName string, opts ...Option) (db *sqlx.DB, created bool, err error)
- func SetDefaults(opts ...Option)
- type DBOption
- type Option
- func SettingsFromViper(cfg *viper.Viper, opts ...Option) (Option, error)
- func WithDatabaseSettings(alias string, opts ...DBOption) Option
- func WithDefaultPoolOptions(opts ...PoolOption) Option
- func WithLogger(lg *zap.Logger) Option
- func WithName(app string) Option
- func WithViper(cfg *viper.Viper) Option
- type PoolOption
- func WithConnMaxIdleTime(connMaxIdleTime time.Duration) PoolOption
- func WithConnMaxLifeTime(connMaxLifeTime time.Duration) PoolOption
- func WithLogLevel(level string) PoolOption
- func WithMaxIdleConns(maxIdleConns int) PoolOption
- func WithMaxOpenConns(maxOpenConns int) PoolOption
- func WithPingTimeout(timeout time.Duration) PoolOption
- func WithSetClause(param, value string) PoolOption
- func WithTracing(enabled bool) PoolOption
- type Repository
- func (r Repository) ConnConfig(u string, lg log.Factory, app string) *pgx.ConnConfig
- func (r *Repository) DB() *sqlx.DB
- func (r Repository) DBURL() string
- func (r *Repository) HealthCheck() error
- func (r Repository) LogLevel() tracelog.LogLevel
- func (r Repository) Logger() log.Factory
- func (r Repository) RedactedURL() string
- func (r Repository) SetPool(db *sql.DB)
- func (r *Repository) Start() error
- func (r *Repository) Stop() error
- func (r *Repository) SwitchDB(dbName string) error
- func (r Repository) TraceOptions(u string) []ocsql.TraceOption
- func (r Repository) Validate() error
Constants ¶
const ( // DefaultURL points to a local test DB with user postgres. // // It is defined for a simple, workable demo setup. DefaultURL = "postgresql://postgres@localhost:5432/testdb?sslmode=disable" // DefaultDBAlias is the default configuration alias for your DB. // // This is suited for most single-DB configurations. DefaultDBAlias = "default" // DefaultLogLevel is the default log level for the database driver. // // The default is set to warn, as the pgx driver is pretty verbose in "info" mode. DefaultLogLevel = "warn" )
Variables ¶
Functions ¶
func CreateDB ¶
CreateDB creates a database "dbName".
NOTE: credentials to connect to the database must be sufficient to create the database.
func DefaultSettings ¶
DefaultSettings returns all defaults for this package as a viper registry.
This is primarily intended for documentation & help purpose.
The configuration is hierarchized like so:
databases:
postgres: default: url: postgres://localhost:5432/test user: $PG_USER password: $PG_PASSWORD pgconfig: # pool settings for this database maxIdleConns: 25 maxOpenConns: 50 connMaxLifetime: 5m pingTimeout: 10s log: level: warn trace: enabled: false otherDB: url: postgres://user:password@localhost:5433/other config: # pool settings for a postgres databases maxIdleConns: 55 pgconfig: # pool settings for this database maxIdleConns: 2 maxOpenConns: 0
func DropDB ¶
DropDB drops the database "dbName".
NOTE: credentials to connect to the database must be sufficient to drop the database.
func EnsureDB ¶
func EnsureDB(ctx context.Context, dbName string, opts ...Option) (db *sqlx.DB, created bool, err error)
EnsureDB ensures that database "dbName" is created and returns a connection pool.
The "created" flag indicates if the database had to be freshly created or not.
NOTE: credentials to connect to the database must be sufficient to create the database.
Types ¶
type DBOption ¶
type DBOption func(*databaseSettings)
Option controls database settings
func WithPassword ¶
func WithPoolSettings ¶
func WithPoolSettings(opts ...PoolOption) DBOption
type Option ¶
type Option func(*settings)
Option controls settings for a set of databases, as well as a default pool configuration
func SettingsFromViper ¶
SettingsFromViper builds settings from a *viper.Viper configuration registry.
Extra options (e.g. WithLogger, ...) can be added.
It returns an error if the configuration cannot be unmarshalled into settings.
func WithDatabaseSettings ¶
func WithDefaultPoolOptions ¶
func WithDefaultPoolOptions(opts ...PoolOption) Option
func WithLogger ¶
WithLogger injects a parent logger for logging the pgx driver and tracing operations.
type PoolOption ¶
type PoolOption func(*poolSettings)
PoolOption controls pool settings for a database
func WithConnMaxIdleTime ¶
func WithConnMaxIdleTime(connMaxIdleTime time.Duration) PoolOption
func WithConnMaxLifeTime ¶
func WithConnMaxLifeTime(connMaxLifeTime time.Duration) PoolOption
func WithLogLevel ¶
func WithLogLevel(level string) PoolOption
func WithMaxIdleConns ¶
func WithMaxIdleConns(maxIdleConns int) PoolOption
func WithMaxOpenConns ¶
func WithMaxOpenConns(maxOpenConns int) PoolOption
func WithPingTimeout ¶
func WithPingTimeout(timeout time.Duration) PoolOption
func WithSetClause ¶
func WithSetClause(param, value string) PoolOption
func WithTracing ¶
func WithTracing(enabled bool) PoolOption
type Repository ¶
type Repository struct {
// contains filtered or unexported fields
}
Repository knows how to handle a postgres backend database.
The database driver is instrumented for tracing.
func New ¶
func New(dbAlias string, opts ...Option) *Repository
Newcreates a new postgres repository for one DB alias declared in the settings.
The new repository needs to be started wih Start() in order to create the connection pool.
func (Repository) ConnConfig ¶
ConnConfig builds a pgx configuration from the URL and additional settings.
Under the hood, pgx merges standard pg parameters such as env variables and pgpass file.
func (*Repository) HealthCheck ¶
func (r *Repository) HealthCheck() error
HealthCheck pings the database
func (Repository) RedactedURL ¶
func (r Repository) RedactedURL() string
func (*Repository) Start ¶
func (r *Repository) Start() error
Start a connection pool to a database, plus possibly another one to the read-only version of it
func (*Repository) Stop ¶
func (r *Repository) Stop() error
Stop the repository and close all connection pools.
Stop may be called safely even if the database connection failed to start properly.
func (Repository) TraceOptions ¶
func (r Repository) TraceOptions(u string) []ocsql.TraceOption
TraceOptions returns the trace options for the opencensus driver wrapper