database

package
v0.0.0-...-f616806 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2020 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MigrateSchemas

func MigrateSchemas(db *gorm.DB, logger *zap.Logger) 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

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) ComparePasswords

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

func (db *Db) CreateUser(ctx context.Context, user *models.User) (*models.UserORM, error)

CreateUser creates a user record in the backend database

func (*Db) DeleteUser

func (db *Db) DeleteUser(ctx context.Context, userID uint32) error

DeleteUser deletes a user account from the backend database

func (*Db) GetUser

func (db *Db) GetUser(ctx context.Context, userID uint32) (*models.UserORM, error)

GetUser queries the database and obtains a user record by id

func (*Db) GetUserIfExists

func (db *Db) GetUserIfExists(ctx context.Context, userID uint32, username, email string) (bool, *models.UserORM, error)

GetUserIfExists checks that a given user exists based on user id

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

func (*Db) UpdateUser

func (db *Db) UpdateUser(ctx context.Context, user *models.User) (*models.UserORM, error)

UpdateUser updates a user account in the database

func (*Db) ValidateAndHashPassword

func (db *Db) ValidateAndHashPassword(mdl models.IHashable) (string, error)

ValidateAndHashPassword validates, hashes and salts a password

type IDatabase

type IDatabase interface {
	CreateUser(ctx context.Context, user *models.User) (*models.UserORM, error)
	UpdateUser(ctx context.Context, user *models.User) (*models.UserORM, error)
	DeleteUser(ctx context.Context, userID uint32) error
	GetUser(ctx context.Context, userID uint32) (*models.UserORM, error)
	GetUserIfExists(ctx context.Context, userID uint32, username, email string) (bool, *models.UserORM, error)
	ValidateAndHashPassword(mdl models.IHashable) (string, error)

	ComparePasswords(hashedPwd string, plainPwd []byte) bool
	// contains filtered or unexported methods
}

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