postgres

package
v1.16.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package database witholds database specific utilities for the core database libary This package mainly deals with interactions with the postgresql database.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteCreatedEntitiesAfterTest

func DeleteCreatedEntitiesAfterTest(db *gorm.DB) func()

DeleteCreatedEntitiesAfterTest sets up GORM `onCreate` hook and return a function that can be deferred to remove all the entities created after the hook was set up You can use it as

func TestSomething(t *testing.T){
    db, _ := gorm.Open(...)

    cleaner := DeleteCreatedEntities(db)
    defer cleaner()

}

func GenerateRandomString

func GenerateRandomString(n int) string

GenerateRandomString generates a random string based on the size specified by the client

Types

type Client

type Client struct {
	// `Engine` is a field of the `Client` struct that holds a pointer to a `gorm.DB` object, which is a
	// database connection object provided by the GORM library. This field is used to store the connection
	// to the PostgreSQL database and is set when the `connect` method is called during the creation of a
	// new `Client` instance.
	Engine *gorm.DB
	// `QueryTimeout` is a field of the `Client` struct that holds a pointer to a `time.Duration` value
	// representing the maximum amount of time to wait for a query to complete before timing out. This
	// field can be set using options when creating a new `Client` instance and is used to set the query
	// timeout for the database connection. If a query takes longer than the specified timeout, an error
	// will be returned.
	QueryTimeout *time.Duration
	// `MaxConnectionRetries` is a field of the `Client` struct that holds a pointer to an integer value
	// representing the maximum number of times to retry connecting to the PostgreSQL database in case of a
	// connection failure. It is used in the `connect` method to set the maximum number of retries for the
	// retry mechanism.
	MaxConnectionRetries *int
	// `MaxConnectionRetryTimeout` is a field of the `Client` struct that holds a pointer to a
	// `time.Duration` value representing the maximum amount of time to wait for a successful connection to
	// the PostgreSQL database before giving up and returning an error. It is used in the `connect` method
	// to set the maximum timeout for the retry mechanism.
	MaxConnectionRetryTimeout *time.Duration
	// `RetrySleep` is a field of the `Client` struct that holds a pointer to a `time.Duration` value
	// representing the amount of time to wait between retries when attempting to connect to the PostgreSQL
	// database. It is used in the `connect` method to set the sleep time for the retry mechanism.
	RetrySleep *time.Duration
	// `ConnectionString` is a field of the `Client` struct that holds a pointer to a string value
	// representing the connection string used to connect to the PostgreSQL database. It is used in the
	// `connect` method to open a new connection to the database using the `gorm.Open` function provided by
	// the GORM library. The connection string typically includes information such as the database name,
	// host, port, username, and password required to establish a connection to the database.
	ConnectionString *string
	// `MaxIdleConnections` is a field of the `Client` struct that holds a pointer to an integer value
	// representing the maximum number of idle connections in the connection pool. It is used to set the
	// maximum number of idle connections that can be kept in the pool and is passed to the
	// `SetMaxIdleConns` function of the `sql.DB` object obtained from the `gorm.DB` object. This helps to
	// optimize the performance of the database connection by limiting the number of idle connections that
	// are kept open.
	MaxIdleConnections *int
	// `MaxOpenConnections` is a field of the `Client` struct that holds a pointer to an integer value
	// representing the maximum number of open connections in the connection pool. It is used to set the
	// maximum number of open connections that can be kept in the pool and is passed to the
	// `SetMaxOpenConns` function of the `sql.DB` object obtained from the `gorm.DB` object. This helps to
	// optimize the performance of the database connection by limiting the number of open connections that
	// are kept open.
	MaxOpenConnections *int
	// `MaxConnectionLifetime` is a field of the `Client` struct that holds a pointer to a `time.Duration`
	// value representing the maximum amount of time a connection can remain open before it is closed and
	// removed from the connection pool. It is used to set the maximum lifetime of a connection and is
	// passed to the `SetConnMaxLifetime` function of the `sql.DB` object obtained from the `gorm.DB`
	// object. This helps to optimize the performance of the database connection by limiting the amount of
	// time a connection can remain open and reducing the risk of resource leaks or other issues that can
	// arise from long-lived connections.
	MaxConnectionLifetime *time.Duration
	// `InstrumentationClient` is a field of the `Client` struct that holds a pointer to an instance of the
	// `instrumentation.Client` struct. This field is used to pass an instrumentation client to the
	// `Client` instance, which can be used to collect metrics and traces related to the database
	// operations performed by the client. The `instrumentation.Client` struct provides methods for
	// collecting metrics and traces, which can be used to monitor the performance and behavior of the
	// database connection.
	InstrumentationClient *instrumentation.Client
	// `Logger *zap.Logger` is a field of the `Client` struct that holds a pointer to an instance of the
	// `zap.Logger` struct. This field is used to pass a logger to the `Client` instance, which can be used
	// to log messages related to the database operations performed by the client. The `zap.Logger` struct
	// provides methods for logging messages at different levels of severity, which can be used to monitor
	// the behavior of the database connection and diagnose issues that may arise.
	Logger *zap.Logger
}

Client is defining a new struct type called `Client` which will be used to create instances of a client for connecting to a PostgreSQL database. The struct contains various fields that can be set using options, such as the database engine, query timeout, connection string, and instrumentation client. The struct also has a `connect` method that attempts to connect to the database using retries.

func New

func New(options ...Option) (*Client, error)

The New function creates a new client with optional configuration options.

func NewInMemoryTestDbClient

func NewInMemoryTestDbClient(models ...any) (*Client, error)

NewInMemoryTestDbClient creates a new in memory test db client This is useful only for unit tests

func (*Client) Close

func (c *Client) Close() error

Close closes the database connection

func (*Client) ConfigureNewTxCleanupHandlerForUnitTests

func (c *Client) ConfigureNewTxCleanupHandlerForUnitTests() *TestTxCleanupHandlerForUnitTests

ConfigureNewTxCleanupHandlerForUnitTests creates a new transaction with a save point and returns a handler that can be used to rollback to the save point. This is useful for unit tests that need to rollback a transaction to a save point.

func (*Client) PerformComplexTransaction

func (db *Client) PerformComplexTransaction(ctx context.Context, transaction CmplxTx) (interface{}, error)

PerformComplexTransaction takes as input an anonymous function witholding logic to perform within a transaction returning an abstract type. This function is then invoked within a transaction and depending on the occurrence of any specific errors, the transaction is either committed to the database or completely rolled back. This returns the result obtained from the invocation of the anonymous function as well as any error occuring throughout the transaction lifecycle.

func (*Client) PerformTransaction

func (db *Client) PerformTransaction(ctx context.Context, transaction Tx) error

PerformTransaction takes as input an anonymous function witholding logic to perform within a transaction. This function is then invoked within a transaction. if unsuccessful or any error is raised throughout the transaction, then, the transaction is rolled back. Returned is any error occuring throughout the transaction lifecycle

func (*Client) Validate

func (c *Client) Validate() error

Validate validates the client

type CmplxTx

type CmplxTx func(ctx context.Context, tx *gorm.DB) (interface{}, error)

CmplxTx is a type serving as a function decorator for complex database transactions

type Option

type Option func(*Client)

Option is a function that sets a parameter for the client

func WithConnectionString

func WithConnectionString(connectionString *string) Option

WithConnectionString sets the connection string

func WithInstrumentationClient

func WithInstrumentationClient(instrumentationClient *instrumentation.Client) Option

WithInstrumentationClient sets the instrumentation client

func WithLogger

func WithLogger(logger *zap.Logger) Option

WithLogger sets the logger

func WithMaxConnectionLifetime

func WithMaxConnectionLifetime(maxConnectionLifetime *time.Duration) Option

WithMaxConnectionLifetime sets the max connection lifetime

func WithMaxConnectionRetries

func WithMaxConnectionRetries(retries *int) Option

WithMaxConnectionRetries sets the max connection retries

func WithMaxConnectionRetryTimeout

func WithMaxConnectionRetryTimeout(timeout *time.Duration) Option

WithMaxConnectionRetryTimeout sets the max connection retry timeout

func WithMaxIdleConnections

func WithMaxIdleConnections(maxIdleConnections *int) Option

WithMaxIdleConnections sets the max idle connections

func WithMaxOpenConnections

func WithMaxOpenConnections(maxOpenConnections *int) Option

WithMaxOpenConnections sets the max open connections

func WithQueryTimeout

func WithQueryTimeout(timeout *time.Duration) Option

WithQueryTimeout sets the query timeout

func WithRetrySleep

func WithRetrySleep(sleep *time.Duration) Option

WithRetrySleep sets the retry sleep

type TestTxCleanupHandlerForUnitTests

type TestTxCleanupHandlerForUnitTests struct {
	Tx *gorm.DB
	// contains filtered or unexported fields
}

TestTxCleanupHandlerForUnitTests is a handler that can be used to rollback a transaction to a save point. This is useful for unit tests that need to rollback a transaction to a save point.

type Tx

type Tx func(ctx context.Context, tx *gorm.DB) error

Tx is a type serving as a function decorator for common database transactions

Jump to

Keyboard shortcuts

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