wpgx

package module
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2023 License: MIT Imports: 16 Imported by: 6

README

Wrapped pgx

  1. env config for pgxpool.

Documentation

Index

Constants

View Source
const (
	HighQPSMaxOpenConns = 100
	DefaultEnvPrefix    = "postgres"
	AppNameLengthMax    = 32
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Username         string        `default:"postgres"`
	Password         string        `default:"my-secret"`
	Host             string        `default:"localhost"`
	Port             int           `default:"5432"`
	DBName           string        `default:"wpgx_test_db"`
	MaxConns         int32         `default:"10"`
	MinConns         int32         `default:"0"`
	MaxConnLifetime  time.Duration `default:"6h"`
	MaxConnIdleTime  time.Duration `default:"30m"`
	EnablePrometheus bool          `default:"true"`
	EnableTracing    bool          `default:"true"`
	AppName          string        `default:""`
}

func ConfigFromEnv

func ConfigFromEnv() *Config

func ConfigFromEnvPrefix

func ConfigFromEnvPrefix(prefix string) *Config

func (*Config) String

func (c *Config) String() string

func (*Config) Valid

func (c *Config) Valid() error

type Pool

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

Pool is the wrapped pgx pool that registers Prometheus.

func NewPool

func NewPool(ctx context.Context, config *Config) (*Pool, error)

func (*Pool) Close

func (p *Pool) Close()

func (*Pool) Ping

func (p *Pool) Ping(ctx context.Context) error

func (*Pool) RawPool

func (p *Pool) RawPool() *pgxpool.Pool

func (*Pool) Transact

func (p *Pool) Transact(ctx context.Context, txOptions pgx.TxOptions, fn TxFunc) (resp interface{}, err error)

Transact is a wrapper of pgx.Transaction It acquires a connection from the Pool and starts a transaction with pgx.TxOptions determining the transaction mode. The context will be used when executing the transaction control statements (BEGIN, ROLLBACK, and COMMIT), and when if tracing is enabled, the context with transaction span will be passed down to @p fn.

func (*Pool) WConn

func (p *Pool) WConn() *WConn

type PostExecFunc

type PostExecFunc = func() error

PostExecFunc is the function that must be ran after a successful CRUD. NOTE: context should have been captured into the function.

type TxFunc

type TxFunc = func(ctx context.Context, tx *WTx) (any, error)

TxFunc is the body of a transaction. ctx must be used to generate proper tracing spans. If not, you might see incorrect parallel spans.

type WConn

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

func (*WConn) PostExec

func (c *WConn) PostExec(fn PostExecFunc) error

func (*WConn) WCopyFrom added in v0.0.3

func (c *WConn) WCopyFrom(
	ctx context.Context, name string, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (n int64, err error)

func (*WConn) WExec

func (c *WConn) WExec(ctx context.Context, name string, unprepared string, args ...interface{}) (cmd pgconn.CommandTag, err error)

func (*WConn) WQuery

func (c *WConn) WQuery(ctx context.Context, name string, unprepared string, args ...interface{}) (r pgx.Rows, err error)

func (*WConn) WQueryRow

func (c *WConn) WQueryRow(ctx context.Context, name string, unprepared string, args ...interface{}) pgx.Row

type WGConn

type WGConn interface {
	WQuery(
		ctx context.Context, name string, unprepared string, args ...interface{}) (pgx.Rows, error)
	WQueryRow(
		ctx context.Context, name string, unprepared string, args ...interface{}) pgx.Row
	WExec(
		ctx context.Context, name string, unprepared string, args ...interface{}) (pgconn.CommandTag, error)
	WCopyFrom(
		ctx context.Context, name string, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)

	PostExec(f PostExecFunc) error
}

WGConn is the abstraction over wrapped connections and transactions.

type WTx

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

WTx is a wrapped pgx.Tx. The main reason is to overwrite the PostExec method that run all of them until the transaction is successfully committed.

func (*WTx) Commit

func (t *WTx) Commit(ctx context.Context) error

func (*WTx) PostExec

func (t *WTx) PostExec(f PostExecFunc) error

func (*WTx) Rollback

func (t *WTx) Rollback(ctx context.Context) error

func (*WTx) WCopyFrom added in v0.0.3

func (t *WTx) WCopyFrom(
	ctx context.Context, name string, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (n int64, err error)

func (*WTx) WExec

func (t *WTx) WExec(ctx context.Context, name string, unprepared string, args ...interface{}) (cmd pgconn.CommandTag, err error)

func (*WTx) WQuery

func (t *WTx) WQuery(ctx context.Context, name string, unprepared string, args ...interface{}) (rows pgx.Rows, err error)

func (*WTx) WQueryRow

func (t *WTx) WQueryRow(ctx context.Context, name string, unprepared string, args ...interface{}) pgx.Row

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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