pghelpers

package module
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2024 License: MIT Imports: 7 Imported by: 3

README

go-pghelpers

Golang helpers for Postgres.

Installation

go get github.com/neighborly/go-pghelpers

Usage

PostgresConfig

PostgresConfig holds the information for postgres database connection. For example

var postgresConfig = pghelpers.PostgresConfig{
	Host:       "localhost",
	Port:       5432,
	Username:   "postgres",
	Password:   "",
	Database:   "postgres",
}
Connect Postgres

pghelpers.ConnectPostgres function returns db object and error if any. You can use pghelpers.ConnectPostgres as follows

db, err := pghelpers.ConnectPostgres(postgresConfig)
GenerateAddress

GenerateAddress returns a Postgres connection string, which can be used for sql.open().

addr := postgresConfig.GenerateAddress()

License

This project is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConnectPostgres

func ConnectPostgres(c PostgresConfig) (*sql.DB, error)

ConnectPostgres connects to postgres

func ExecInTx added in v0.6.0

func ExecInTx(db *sql.DB, fn ExecInTxFunc) error

ExecInTx executes the provided function within a database transaction. The must return true for the transaction to be commit. Returning false will rollback the transaction. To pass variables into the transaction function or to return variables out, use a closure.

func NewTxInstance

func NewTxInstance(instanceID string) *sql.DB

NewTxInstance creates a new instance of tx-db. Make sure you Register first

func RegisterPostgresTxDb

func RegisterPostgresTxDb(pgConfig PostgresConfig)

RegisterPostgresTxDb registers postgres to txdb

func ReleaseSavepoint added in v0.9.0

func ReleaseSavepoint(name string, tx *sql.Tx) error

ReleaseSavepoint releases a named savepoint previously set in the transaction. This allows the commands executed after the savepoint to be committed.

func RollbackToSavepoint added in v0.9.0

func RollbackToSavepoint(name string, tx *sql.Tx) error

RollbackToSavepoint rolls back the transaction to the named savepoint.

func SetSavepoint added in v0.9.0

func SetSavepoint(name string, tx *sql.Tx) error

SetSavepoint sets a named savepoint in the current transaction.

Types

type ExecInTxFunc added in v0.6.0

type ExecInTxFunc func(tx *sql.Tx) (commit bool)

ExecInTxFunc defines a function type for the ExecInTx function argument.

type PostgresConfig

type PostgresConfig struct {
	ApplicationName string `mapstructure:"application_name"`
	Host            string
	Port            int
	Username        string
	Password        string
	Database        string
	SSLEnabled      bool   `mapstructure:"ssl_enabled"`
	MigrationsTable string `mapstructure:"migrations_table"`
	// MaxOpenConnections sets the maximum size of the connection pool. Default 10.
	MaxOpenConnections int `mapstructure:"max_open_connections"`
}

PostgresConfig holds the information for connecting with a postgres database

func (*PostgresConfig) GenerateAddress

func (c *PostgresConfig) GenerateAddress() string

GenerateAddress returns a string with DB connection information

func (PostgresConfig) URL added in v0.7.0

func (c PostgresConfig) URL() string

URL returns a 'postgres://user:pass@host:port/database?sslmode=' style address

Jump to

Keyboard shortcuts

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