postgres

package
v0.0.0-...-eaa3807 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2024 License: MIT Imports: 15 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	DBHost                string        // DBHost represents the database host
	DBPort                int           // DBPort is the database port
	DBName                string        // DBName is the database name
	DBUser                string        // DBUser is the database user used to connect
	DBPassword            string        // DBPassword is the database password
	DBSchema              string        // DBSchema represents the database schema
	MaxOpenConnections    int           // MaxOpenConnections represents the number of open connections in the pool
	MaxIdleConnections    int           // MaxIdleConnections represents the number of idle connections in the pool
	ConnectionMaxLifetime time.Duration // ConnectionMaxLifetime represents the connection max life time
}

type Postgres

type Postgres interface {
	// Connect connects to the underlying database
	Connect(ctx context.Context) error
	// Disconnect closes the underlying opened underlying connection database
	Disconnect(ctx context.Context) error
	// Select fetches a single row from the database and automatically scanned it into the dst.
	// It returns an error in case of failure. When there is no record no errors is return.
	Select(ctx context.Context, dst any, query string, args ...any) error
	// SelectAll fetches a set of rows as defined by the query and scanned those record in the dst.
	// It returns nil when there is no records to fetch.
	SelectAll(ctx context.Context, dst any, query string, args ...any) error
	// Exec executes an SQL statement against the database and returns the appropriate result or an error.
	Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
	// BeginTx helps start an SQL transaction. The return transaction object is expected to be used in
	// the subsequent queries following the BeginTx.
	BeginTx(ctx context.Context, txOptions *sql.TxOptions) (*sql.Tx, error)
}

Postgres will be implemented by concrete RDBMS store

func New

func New(config *Config) Postgres

New returns a store connecting to the given Postgres database.

type QueryBuilder

type QueryBuilder interface {
	// BuildQuery return the SQL statement and arguments to use to run the query
	// The args are used to handle prepared statement. Therefore, they must be provided in the order
	// of the various placeholder for smooth substitution.
	BuildQuery() (sqlStatement string, args []any, err error)
}

QueryBuilder interface generalizes the sql execution implementations

type TestContainer

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

TestContainer helps creates a Postgres docker container to run unit tests

func NewTestContainer

func NewTestContainer(dbName, dbUser, dbPassword string) *TestContainer

NewTestContainer create a Postgres test container useful for unit and integration tests This function will exit when there is an error.Call this function inside your SetupTest to create the container before each test.

func (TestContainer) Cleanup

func (c TestContainer) Cleanup()

Cleanup frees the resource by removing a container and linked volumes from docker. Call this function inside your TearDownSuite to clean-up resources after each test

func (TestContainer) GetTestDB

func (c TestContainer) GetTestDB() *TestDB

GetTestDB returns a Postgres TestDB that can be used in the tests to perform some database queries

func (TestContainer) Host

func (c TestContainer) Host() string

Host return the host of the test container

func (TestContainer) Port

func (c TestContainer) Port() int

Port return the port of the test container

func (TestContainer) Schema

func (c TestContainer) Schema() string

Schema return the test schema of the test container

type TestDB

type TestDB struct {
	Postgres
}

TestDB is used in test to perform some database queries

func (TestDB) Count

func (c TestDB) Count(ctx context.Context, tableName string) (int, error)

Count utility function to help count the number of rows in a Postgres table. tableName is in the format: <schemaName.tableName>. e.g: public.users It returns -1 when there is an error

func (TestDB) CreateSchema

func (c TestDB) CreateSchema(ctx context.Context, schemaName string) error

CreateSchema helps create a test schema in a Postgres database

func (TestDB) DropSchema

func (c TestDB) DropSchema(ctx context.Context, schemaName string) error

DropSchema utility function to drop a database schema

func (TestDB) DropTable

func (c TestDB) DropTable(ctx context.Context, tableName string) error

DropTable utility function to drop a database table

func (TestDB) SchemaExists

func (c TestDB) SchemaExists(ctx context.Context, schemaName string) (bool, error)

SchemaExists helps check the existence of a Postgres schema. Very useful when implementing tests

func (TestDB) TableExists

func (c TestDB) TableExists(ctx context.Context, tableName string) error

TableExists utility function to help check the existence of table in Postgres tableName is in the format: <schemaName.tableName>. e.g: public.users

type TxRunner

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

TxRunner helps run database queries in a safe database transaction. In case of errors the underlying database transaction is rolled back. When there is no errors the underlying database transaction is committed.

func NewTxRunner

func NewTxRunner(ctx context.Context, db Postgres) (*TxRunner, error)

NewTxRunner creates an instance of TxRunner

func (*TxRunner) AddQueryBuilder

func (r *TxRunner) AddQueryBuilder(v QueryBuilder) *TxRunner

AddQueryBuilder adds an QueryBuilder to the db transaction runner. The Builders queries will be executed in the database transaction in the order the builders have been added. Therefore, add builders according to the order of execution of the queries

func (*TxRunner) Execute

func (r *TxRunner) Execute() error

Execute executes the database queries returns resulting error(s). In case of errors the underlying database transaction is rolled back. When there is no errors the underlying database transaction is committed

Jump to

Keyboard shortcuts

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