pgdb

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: May 8, 2024 License: MPL-2.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Qb adaptedStatementBuilder = adaptedStatementBuilder{sq.StatementBuilder.PlaceholderFormat(sq.Dollar)}

Qb is exported squirrel query builder initiated with dollar format for convenience

View Source
var QueryExecerCtxKey queryExecerCtxKeyType
View Source
var StdDb *sql.DB

StdDb is pgx's stadard library SQL adaptor, initiated by ConnectStdLib. Do not use before the initialization.

Functions

func ConnectPgxPool

func ConnectPgxPool(ctx context.Context, c Config) (err error)

ConnectPgxPool initiate the exported DbPool with database connection using pgx pool connection

func ConnectStdLib

func ConnectStdLib(ctx context.Context, c Config) error

ConnectStdLib initiate the exported StdDb with database connection using pgx stdlib

func Exec added in v0.0.6

func Exec(ctx context.Context, sql string, args ...interface{}) (pgconn.CommandTag, error)

Exec runs the statement using the default DbPool.

func MigrateEntryPoint

func MigrateEntryPoint(config Config, optionally1EmbeddedDirs ...embed.FS)

MigrateEntryPoint is the entrypoint for DB migration commands. If config.MigrationFromEmbedded is true, the (optional) embedded migration directory must then be set (only 1 is accepted)

The simplest way to include the embed:

import "embed"
//go:embed migrations
var embeddedMigrations embed.FS

func MigrateGetDbDriver added in v0.0.4

func MigrateGetDbDriver(ctx context.Context, c Config) (pgDatabaseDriver database.Driver, err error)

MigrateGetDbDriver exposes the lower-level migration integration

func MigrateGetInstance added in v0.0.4

func MigrateGetInstance(ctx context.Context, c Config, embeddedDirs ...embed.FS) (migrateInstance *migrate.Migrate, err error)

MigrateGetInstance exposes the lower-level migration integration

func MigrateGetSource added in v0.0.4

func MigrateGetSource(ctx context.Context, c Config, embeddedDirs ...embed.FS) (migrationSourceDriver source.Driver, err error)

MigrateGetSource exposes the lower-level migration integration

func MigrateGetSourceFs added in v0.0.5

func MigrateGetSourceFs(ctx context.Context, c Config, embeddedDirs ...embed.FS) (migrationFS fs.FS, err error)

MigrateGetSourceFs exposes the lower-level migration integration

func QbExec added in v0.0.6

func QbExec(ctx context.Context, builtQuery sq.Sqlizer) (c pgconn.CommandTag, err error)

QbExec takes a finished Squirrel's query builder and executes the resulting statement using the default DbPool

func QbQuery added in v0.0.6

func QbQuery(ctx context.Context, builtQuery sq.Sqlizer) (pgx.Rows, error)

QbQuery takes a finished Squirrel's query builder and executes the resulting query using the default DbPool

func QbQueryRow added in v0.0.6

func QbQueryRow(ctx context.Context, builtQuery sq.Sqlizer) (pgx.Row, error)

QbQueryRow takes a finished Squirrel's query builder and executes the resulting query using the default DbPool

func Query added in v0.0.6

func Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)

Query runs the query using the default DbPool.

func QueryRow added in v0.0.6

func QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row

QueryRow runs the (single row) query using the default DbPool retrieving 1 row.

Types

type Config

type Config struct {
	// If set, URL will override other connection parameters
	//
	// Use this parameter to specify more advanced connection parameters such as SSL
	URL                              string    `yaml:"URL" env:"PG_URL"`
	Host                             string    `yaml:"Host" env:"PG_HOST"`
	Port                             int       `yaml:"Port" env:"PG_PORT"`
	Database                         string    `yaml:"Database" env:"PG_DATABASE"`
	Username                         string    `yaml:"Username" env:"PG_USERNAME"`
	Password                         string    `yaml:"Password" env:"PG_PASSWORD"`
	SSL                              string    `yaml:"SSL" env:"PG_SSL"`
	PoolParam                        PoolParam `yaml:"PoolParam"`
	MigrationStatementTimeoutSeconds int       `yaml:"MigrationStatementTimeoutSeconds" env:"PG_MIGRATION_STATEMENT_TIMEOUT_SECONDS"`
	MigrationFromEmbedded            bool      `yaml:"MigrationFromEmbedded" env:"PG_MIGRATION_FROM_EMBEDDED"`
	MigrationDirectory               string    `yaml:"MigrationDirectory" env:"PG_MIGRATION_DIRECTORY"`
}

func (Config) Connect added in v0.1.0

func (c Config) Connect(ctx context.Context) (p *EnhancedDbPool, err error)

Connect initialize a Config into a pgxpool.Pool, enhanced with context-aware Acquire/Begin and Query/Exec methods.

func (Config) GetDsn

func (c Config) GetDsn() string

type EnhancedDbPool added in v0.1.0

type EnhancedDbPool struct{ *pgxpool.Pool }
var DbPool *EnhancedDbPool

DbPool is PGX pool connection, initiated by ConnectPgxPool. Do not use before the initialization.

func (*EnhancedDbPool) AcquireCtx added in v0.1.0

func (p *EnhancedDbPool) AcquireCtx(c context.Context) (atx *ctxConn, err error)

Acquire returns *pgx.Conn wrapped together with context.Context. As such, there is no need to pass Conn to DB calls that might optionally be called with a Conn.

func (*EnhancedDbPool) BeginCtx added in v0.1.0

func (p *EnhancedDbPool) BeginCtx(c context.Context) (atx *ctxTx, err error)

BeginCtx returns pgx.Tx wrapped together with context.Context. As such, there is no need to pass Tx to DB calls that might optionally be called with Tx.

func (*EnhancedDbPool) BeginTxCtx added in v0.1.0

func (p *EnhancedDbPool) BeginTxCtx(c context.Context, t pgx.TxOptions) (atx *ctxTx, err error)

BeginCtx returns pgx.Tx wrapped together with context.Context. As such, there is no need to pass Tx to DB calls that might optionally be called with Tx.

func (*EnhancedDbPool) Exec added in v0.1.0

func (p *EnhancedDbPool) Exec(ctx context.Context, sql string, args ...interface{}) (pgconn.CommandTag, error)

Exec tries to find QueryExecer from ctx, and if not found, executes the query using its pool

func (*EnhancedDbPool) QbExec added in v0.1.0

func (p *EnhancedDbPool) QbExec(ctx context.Context, builtQuery sq.Sqlizer) (c pgconn.CommandTag, err error)

QbExec takes a finished Squirrel's query builder and executes the resulting statement

func (*EnhancedDbPool) QbQuery added in v0.1.0

func (p *EnhancedDbPool) QbQuery(ctx context.Context, builtQuery sq.Sqlizer) (pgx.Rows, error)

QbQuery takes a finished Squirrel's query builder and executes the resulting query

func (*EnhancedDbPool) QbQueryRow added in v0.1.0

func (p *EnhancedDbPool) QbQueryRow(ctx context.Context, builtQuery sq.Sqlizer) (pgx.Row, error)

QbQueryRow takes a finished Squirrel's query builder and executes the resulting query

func (*EnhancedDbPool) Query added in v0.1.0

func (p *EnhancedDbPool) Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)

Query tries to find QueryExecer from ctx, and if not found, executes the query using its pool

func (*EnhancedDbPool) QueryRow added in v0.1.0

func (p *EnhancedDbPool) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row

QueryRow tries to find QueryExecer from ctx, and if not found, executes the query using its pool

type PoolParam added in v0.1.0

type PoolParam struct {
	// Non-zero values will be passed to DbPool initialization. Defaults to 0.
	MinConns int `yaml:"MinConns" env:"pg_min_conns"`
	// Non-zero values will be passed to DbPool initialization. Defaults to max(numCPU, 4)
	MaxConns int `yaml:"MaxConns" env:"pg_max_conns"`
	// Non-zero values will be passed to DbPool initialization. Set with time units (s, m, h). Defaults to 2 mins.
	ConnectTimeout string `yaml:"ConnectTimeout" env:"pg_connect_timeout"`
	// Non-zero values will be passed to DbPool initialization. Set with time units (s, m, h). Defaults to 30 mins.
	MaxConnIdleTime string `yaml:"MaxConnIdleTime" env:"pg_max_conn_idle_time"`
	// Non-zero values will be passed to DbPool initialization. Set with time units (s, m, h). Defaults to 1 hour.
	MaxConnLifetime string `yaml:"MaxConnLifetime" env:"pg_max_conn_lifetime"`
	// Non-zero values will be passed to DbPool initialization. Set with time units (s, m, h). Defaults to 60s.
	HealthCheckPeriod string `yaml:"HealthCheckPeriod" env:"pg_health_check_period"`
}

type QueryExecer added in v0.1.0

type QueryExecer interface {
	Exec(ctx context.Context, sql string, arguments ...any) (commandTag pgconn.CommandTag, err error)
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
}

Jump to

Keyboard shortcuts

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