core_database

package module
v0.0.0-...-eeb1421 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2023 License: MIT Imports: 14 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 Connect

func Connect(params *Parameters) (*gorm.DB, error)

Connect attempts to connect to the database using retries

func DeleteCreatedEntities

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

DeleteCreatedEntities 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 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 DatabaseConn

type DatabaseConn struct {
	Engine                    *gorm.DB
	QueryTimeout              *time.Duration
	MaxConnectionRetries      *int
	MaxConnectionRetryTimeout *time.Duration
	RetrySleep                *time.Duration
	ConnectionString          *string
}

func NewDatabaseConn

func NewDatabaseConn(params *Parameters) *DatabaseConn

NewDatabaseConn obtains a reference to a database connection object

func (*DatabaseConn) ComparePasswords

func (db *DatabaseConn) ComparePasswords(hashedPwd string, plainPwd []byte) bool

ComparePasswords compares a hashed password and a plaintext password and returns a boolean stating wether they are equal or not

func (*DatabaseConn) PerformComplexTransaction

func (db *DatabaseConn) 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 (*DatabaseConn) PerformTransaction

func (db *DatabaseConn) 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 (*DatabaseConn) ValidateAndHashPassword

func (db *DatabaseConn) ValidateAndHashPassword(password string) (string, error)

ValidateAndHashPassword validates, hashes and salts a password

type Parameters

type Parameters struct {
	QueryTimeout              *time.Duration
	MaxConnectionRetries      *int
	MaxConnectionRetryTimeout *time.Duration
	RetrySleep                *time.Duration
	ConnectionString          *string
}

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