database

package
v0.0.0-...-99e5d07 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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 Initialize

func Initialize(connSettings string) *database.Db

Initialize creates a singular connection to the backend database instance

func MigrateSchemas

func MigrateSchemas(db *gorm.DB, logger *zap.Logger, models ...interface{}) error

MigrateSchemas creates or updates a given set of models based on a schema if it does not exist or migrates the model schemas to the latest version

func Setup

func Setup() *database.Db

Setup sets up database connection prior to testing

func SetupTests

func SetupTests() *database.Db

init initializes a connection to the database initially and performs package level cleanup handler initialization

Types

type CmplxTx

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

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

type Db

type Db struct {
	Engine *gorm.DB
	Logger *zap.Logger
}

Db witholds connection to a postgres database as well as a logging handler

func New

func New(connString string, logger *zap.Logger) (*Db, error)

New creates a database connection and returns the connection object

func (*Db) PerformComplexTransaction

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

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

type IDatabase

type IDatabase interface {
}

IDatabase provides an interface which any database tied to this service should implement

type Tx

type Tx func(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