datastore

package
v0.0.0-...-9a2df54 Latest Latest
Warning

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

Go to latest
Published: May 17, 2023 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Postgresql driver
	DriverPostgresql = "pg"
	// SQlite driver
	DriverSqlite = sqliteshim.ShimName
)

Variables

View Source
var (
	Marshal   func(v any) ([]byte, error)
	Unmarshal func(data []byte, vPtr any) error
)
View Source
var (
	ErrNotFoundGoCache = errors.New("item not found")
)

Functions

func IsErrNotFound

func IsErrNotFound(err error) bool

IsErrNotFound checks if the error returned from any of the datastore library used here e.g redis, database etc is an error not found type

func NewDBConnection

func NewDBConnection(dbDriver, dsn string, dbPoolMax int, printQueriesToStdout bool) *bun.DB

NewDBConnection establishes a database connection

Types

type DBHelper

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

func NewDBHelper

func NewDBHelper(db *bun.DB) *DBHelper

func (*DBHelper) Close

func (helper *DBHelper) Close() error

func (*DBHelper) DeleteByColumn

func (helper *DBHelper) DeleteByColumn(ctx context.Context, modelsPtr, colName string, colValue any) error

func (*DBHelper) DeleteByPKey

func (helper *DBHelper) DeleteByPKey(ctx context.Context, modelsPtr any) error

func (*DBHelper) FindByColumn

func (helper *DBHelper) FindByColumn(
	ctx context.Context, modelsPtr any, columnName string, columnValue any,
) error

func (*DBHelper) FindByPKey

func (helper *DBHelper) FindByPKey(ctx context.Context, modelsPtr any) error

func (*DBHelper) Insert

func (helper *DBHelper) Insert(ctx context.Context, modelsPtr any, ignoreDupicates bool) error

func (*DBHelper) ListAll

func (helper *DBHelper) ListAll(ctx context.Context, modelsPtr any) error

func (*DBHelper) ListByColumn

func (helper *DBHelper) ListByColumn(
	ctx context.Context, modelsPtr any, columnName string, columnValue any,
) error

func (*DBHelper) Migrate

func (helper *DBHelper) Migrate(ctx context.Context, modelsPtr ...any) error

Usage: Migrate(ctx, (*StructModel1)(nil), (*StructModel2)(nil), .....)

func (*DBHelper) NewWithTx

func (helper *DBHelper) NewWithTx(tx bun.Tx) IDBHelper

NewWithTx returns a clone of DBHelper, HOWEVER OVERRIDING the dbConnection with a db-Transaction conn as the new dbConnection

func (*DBHelper) Transactional

func (helper *DBHelper) Transactional(ctx context.Context, fn func(ctx context.Context, tx bun.Tx) error) error

Transactional simplifies transactions code, by automatically: starting a transaction, rolling back the transaction if an error is encountered & finally commiting the transaction if no error.

func Example_transactionUsage(ctx context.Context) {
	var example IDBHelper = nil
	example.Transactional(
		ctx,
		func(ctx context.Context, tx bun.Tx) error {
			example.NewWithTx(tx).FindByPKey(ctx, nil)
			example.NewWithTx(tx).UpdateByPKey(ctx, nil)
			return nil
		},
	)
}

func (*DBHelper) UpdateByPKey

func (helper *DBHelper) UpdateByPKey(ctx context.Context, modelsPtr any) error

func (*DBHelper) UpsertByPKey

func (helper *DBHelper) UpsertByPKey(ctx context.Context, modelsPtr any) error

type GoCache

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

func NewGoCache

func NewGoCache() *GoCache

func (*GoCache) Clear

func (s *GoCache) Clear(ctx context.Context) error

func (*GoCache) Close

func (s *GoCache) Close() error

func (*GoCache) Del

func (s *GoCache) Del(ctx context.Context, key string) error

func (*GoCache) Get

func (s *GoCache) Get(ctx context.Context, key string, dest any) error

func (*GoCache) Has

func (s *GoCache) Has(ctx context.Context, key string) bool

func (*GoCache) Marshal

func (s *GoCache) Marshal(v any) ([]byte, error)

func (*GoCache) Set

func (s *GoCache) Set(ctx context.Context, key string, val any, ttl time.Duration) error

func (*GoCache) Unmarshal

func (s *GoCache) Unmarshal(data []byte, vPtr any) error

type ICache

type ICache interface {
	// Has checks to see if a key exists in the cache
	Has(ctx context.Context, key string) bool
	// Get gets a key from the cache
	Get(ctx context.Context, key string, dest any) error
	// Set sets a key to the cache
	Set(ctx context.Context, key string, val any, ttl time.Duration) error
	// Del deletes a key from the cache
	Del(ctx context.Context, key string) error
	// Clear used to flush/clear the cache
	Clear(ctx context.Context) error
	// Close closes the connection
	Close() error
}

ICache is an interface that guides & ensure the use of different external cache library, in a way thats easy to swap.

type IDBHelper

type IDBHelper interface {
	// Close: closes the connection
	Close() error
	// Migration create ONE OR MORE tables ONLY when they dont exists.
	Migrate(ctx context.Context, modelsPtr ...any) error
	// UpdateByPKey updates ONE OR MORE record by their primary-key (set in struct)
	UpdateByPKey(ctx context.Context, modelsPtr any) error
	// UpsertByPKey updates ONE OR MORE record by their primary-key and if the record
	// doesn't exist, it inserts it.
	UpsertByPKey(ctx context.Context, modelsPtr any) error
	// Insert inserts ONE OR MORE record.
	Insert(ctx context.Context, modelsPtr any, ignoreDupicates bool) error
	// FindByPKey gets ONE record by primary-key (set in struct). [limit 1]
	FindByPKey(ctx context.Context, modelsPtr any) error
	// FindByColumn gets a record via supplied column-name & column-value.  [limit 1]
	FindByColumn(ctx context.Context, modelsPtr any, columnName string, columnValue any) error
	// List all records of a table. Useful for loading settings from db
	ListAll(ctx context.Context, modelsPtr any) error
	// List records of a table via supplied column and column value
	ListByColumn(ctx context.Context, modelsPtr any, columnName string, columnValue any) error
	// DeleteByPKey deletes a record using primary key in struct
	DeleteByPKey(ctx context.Context, modelsPtr any) error
	// DeleteByColumn deletes ONE OR MORE record via supplied column-name & column-value
	DeleteByColumn(ctx context.Context, modelsPtr, columnName string, columnValue any) error

	// NewWithTx returns a clone of DBHelper, HOWEVER OVERRIDING the dbConnection with a db-Transaction conn
	// as the new dbConnection
	NewWithTx(tx bun.Tx) IDBHelper
	// Transactional simplifies transactions code, by automatically:
	//
	// starting a transaction, rolling back the transaction if an error
	// is encountered & finally commiting the transaction if no error.
	// func Example_transactionUsage(ctx context.Context) {
	// 	var example IDBHelper = nil
	// 	example.Transactional(
	// 		ctx,
	// 		func(ctx context.Context, tx bun.Tx) error {
	// 			example.NewWithTx(tx).FindByPKey(ctx, nil)
	// 			example.NewWithTx(tx).UpdateByPKey(ctx, nil)
	// 			return nil
	// 		},
	// 	)
	// }
	Transactional(ctx context.Context, fn func(ctx context.Context, tx bun.Tx) error) error
}

IDBHelper is an interface that provides quick helpers for handling database operations

type OrmDB

type OrmDB = *bun.DB

type OrmDbTx

type OrmDbTx = bun.IDB

type Rueidis

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

func NewRueidis

func NewRueidis(redisURLs []string, password string, disableCache bool) *Rueidis

NewRueidis establishes a redis-cache connection via redis/Rueidis library

func (*Rueidis) Clear

func (s *Rueidis) Clear(ctx context.Context) error

func (*Rueidis) Close

func (s *Rueidis) Close() error

func (*Rueidis) Del

func (s *Rueidis) Del(ctx context.Context, key string) error

func (*Rueidis) Get

func (s *Rueidis) Get(ctx context.Context, key string, dest any) error

func (*Rueidis) Has

func (s *Rueidis) Has(ctx context.Context, key string) bool

func (*Rueidis) Marshal

func (s *Rueidis) Marshal(v any) ([]byte, error)

func (*Rueidis) Set

func (s *Rueidis) Set(ctx context.Context, key string, val any, ttl time.Duration) error

func (*Rueidis) Unmarshal

func (s *Rueidis) Unmarshal(data []byte, vPtr any) error

Jump to

Keyboard shortcuts

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