postgres

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: MIT Imports: 8 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnableGetTransactionFromContext = errors.New("unable get transaction from context")
	ErrNotInContextualTxStatement      = errors.New("unable to commit transaction statement - not in tx statement")
)

Functions

func EmptyOrError

func EmptyOrError(err error, errorMessage string) error

Types

type Connection

type Connection struct {
	Dbx *sqlx.DB
	// contains filtered or unexported fields
}

Connection struct to store and manipulate postgres database connection

func NewConnection

func NewConnection(ctx context.Context, cfg DbConfig, logger *zap.Logger) *Connection

NewConnection to postgres db

func (*Connection) BeginContextualTxStatement

func (c *Connection) BeginContextualTxStatement(ctx context.Context) (context.Context, error)

BeginContextualTxStatement ....

func (*Connection) BeginReadCommittedTxRollbackOnError added in v0.0.5

func (c *Connection) BeginReadCommittedTxRollbackOnError(ctx context.Context,
	callback func(txStmtCtx context.Context) error,
) error

func (*Connection) BeginReadUncommittedTxRollbackOnError added in v0.0.5

func (c *Connection) BeginReadUncommittedTxRollbackOnError(ctx context.Context,
	callback func(txStmtCtx context.Context) error,
) error

func (*Connection) BeginTx

func (c *Connection) BeginTx() (*sqlx.Tx, error)

BeginTx ....

func (*Connection) BeginTxWithRollbackOnError

func (c *Connection) BeginTxWithRollbackOnError(ctx context.Context,
	callback func(txStmtCtx context.Context) error,
) error

BeginTxWithRollbackOnError ....

func (*Connection) Close

func (c *Connection) Close() error

func (*Connection) CommitContextualTxStatement

func (c *Connection) CommitContextualTxStatement(ctx context.Context) error

CommitContextualTxStatement ....

func (*Connection) Connect

func (c *Connection) Connect() (*Connection, error)

Connect to postgres database

func (*Connection) IsHealed added in v0.0.6

func (c *Connection) IsHealed(ctx context.Context) bool

func (*Connection) MustWithTransaction

func (c *Connection) MustWithTransaction(ctx context.Context, fn func(stmt *sqlx.Tx) error) error

func (*Connection) RollbackContextualTxStatement

func (c *Connection) RollbackContextualTxStatement(ctx context.Context) error

RollbackContextualTxStatement ....

func (*Connection) TryWithTransaction

func (c *Connection) TryWithTransaction(ctx context.Context, fn func(stmt sqlx.Ext) error) error

type DbConfig

type DbConfig interface {
	GetDbHost() string
	GetDbPort() uint16
	GetDbName() string
	GetDbUser() string
	GetDbPassword() string
	GetDbTLSMode() string
	GetDbRetryCount() uint8
	GetDbConnectTimeOut() uint16

	GetDbMaxOpenConns() uint8
	GetDbMaxIdleConns() uint8

	IsDebug() bool
}

type PostgresConfig

type PostgresConfig struct {
	DbHost         string `envconfig:"POSTGRESQL_SERVICE_HOST"`
	DbPort         uint16 `envconfig:"POSTGRESQL_SERVICE_PORT"`
	DbName         string `envconfig:"POSTGRESQL_DATABASE_NAME" secret:"true"`
	DbUsername     string `envconfig:"POSTGRESQL_USERNAME" secret:"true"`
	DbPassword     string `envconfig:"POSTGRESQL_PASSWORD" secret:"true"`
	DbSSLMode      string `envconfig:"POSTGRESQL_SSL_MODE" default:"prefer"`
	DbMaxOpenConns uint8  `envconfig:"POSTGRESQL_MAX_OPEN_CONNECTIONS" default:"8"`
	DbMaxIdleConns uint8  `envconfig:"POSTGRESQL_MAX_IDLE_CONNECTIONS" default:"8" `
	// DbConnectRetryCount is the maximum number of reconnection tries. If 0 - infinite loop
	DbConnectRetryCount uint8 `envconfig:"POSTGRESQL_CONNECTION_RETRY_COUNT" default:"0"`
	// DbConnectTimeOut is the timeout in millisecond to connect between connection tries
	DbConnectTimeOut uint16 `envconfig:"POSTGRESQL_CONNECTION_RETRY_TIMEOUT" default:"5000"`
}

func (*PostgresConfig) GetDatabaseDSN

func (c *PostgresConfig) GetDatabaseDSN() string

func (*PostgresConfig) GetDbConnectTimeOut

func (c *PostgresConfig) GetDbConnectTimeOut() uint16

func (*PostgresConfig) GetDbHost

func (c *PostgresConfig) GetDbHost() string

func (*PostgresConfig) GetDbMaxIdleConns

func (c *PostgresConfig) GetDbMaxIdleConns() uint8

func (*PostgresConfig) GetDbMaxOpenConns

func (c *PostgresConfig) GetDbMaxOpenConns() uint8

func (*PostgresConfig) GetDbName

func (c *PostgresConfig) GetDbName() string

func (*PostgresConfig) GetDbPassword

func (c *PostgresConfig) GetDbPassword() string

func (*PostgresConfig) GetDbPort

func (c *PostgresConfig) GetDbPort() uint16

func (*PostgresConfig) GetDbRetryCount

func (c *PostgresConfig) GetDbRetryCount() uint8

func (*PostgresConfig) GetDbTLSMode

func (c *PostgresConfig) GetDbTLSMode() string

func (*PostgresConfig) GetDbUser

func (c *PostgresConfig) GetDbUser() string

func (*PostgresConfig) Prepare

func (c *PostgresConfig) Prepare() error

Jump to

Keyboard shortcuts

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