postgres

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractTxQueryKey

func ExtractTxQueryKey(ctx context.Context) (string, bool)

func InjectTX

func InjectTX(ctx context.Context, tx Tx) context.Context

func NewClient

func NewClient(ctx context.Context, log *slog.Logger, cfg *Config) (pool *pgxpool.Pool, err error)

NewClient создает и конфигурирует пул соединений.

Types

type Config

type Config struct {
	Username string
	Password string
	Host     string
	Port     string
	Database string

	ConnAmount        *int32
	MinConnAmount     *int32
	MaxConnIdleTime   *time.Duration
	MaxConnLifetime   *time.Duration
	HealthCheckPeriod *time.Duration
	AcquireTimeout    time.Duration
	// contains filtered or unexported fields
}

func NewConfig

func NewConfig(username, password, host, port, database string) *Config

func (*Config) WithAcquireTimeout

func (c *Config) WithAcquireTimeout(d time.Duration) *Config

func (*Config) WithConnAmount

func (c *Config) WithConnAmount(amount int32) *Config

func (*Config) WithHealthCheckPeriod

func (c *Config) WithHealthCheckPeriod(d time.Duration) *Config

func (*Config) WithMaxConnAttempts

func (c *Config) WithMaxConnAttempts(attempts int) *Config

func (*Config) WithMaxConnIdleTime

func (c *Config) WithMaxConnIdleTime(d time.Duration) *Config

func (*Config) WithMaxConnLifetime

func (c *Config) WithMaxConnLifetime(d time.Duration) *Config

func (*Config) WithMinConnAmount

func (c *Config) WithMinConnAmount(amount int32) *Config

func (*Config) WithRetryConnDelay

func (c *Config) WithRetryConnDelay(delay time.Duration) *Config

func (*Config) WithTracer

func (c *Config) WithTracer(tracer pgx.QueryTracer) *Config

type Handler

type Handler func(ctx context.Context) error

type Migrator

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

func NewMigrator

func NewMigrator(db *pgxpool.Pool, migrationsDir string) (*Migrator, error)

func (*Migrator) Up

type Postgres

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

	BeginTx(ctx context.Context, txOptions pgx.TxOptions) (Tx, error)

	Pool() *pgxpool.Pool

	Close()
}

func NewPostgres

func NewPostgres(ctx context.Context, log *slog.Logger, cfg *Config) (Postgres, error)

type Tx

type Tx interface {
	Begin(ctx context.Context) (pgx.Tx, error)

	Commit(ctx context.Context) error

	Rollback(ctx context.Context) error

	CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)
	SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults
	LargeObjects() pgx.LargeObjects

	Prepare(ctx context.Context, name, sql string) (*pgconn.StatementDescription, error)

	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

	Conn() *pgx.Conn
}

func ExtractTX

func ExtractTX(ctx context.Context) (Tx, bool)

type TxManager

type TxManager interface {
	ReadCommitted(ctx context.Context, handler Handler) error
	RepeatableRead(ctx context.Context, retryAmount uint, handler Handler) error
	Serializable(ctx context.Context, retryAmount uint, handler Handler) error
}

func NewTxManager

func NewTxManager(db Postgres) TxManager

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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