dbhelper

package
v0.0.0-...-7b190fc Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	DBDriverUndefined = iota
	DBDriverSQLite
	DBDriverPostgres
)

Variables

This section is empty.

Functions

func Connect

func Connect(dialector gorm.Dialector, options ...ConnOption) (*gorm.DB, error)

Connect establishes a connection to the database using the given dialector and connection parameters.

Types

type ConfigPostgres

type ConfigPostgres struct {
	Host     string
	User     string
	Password string
	DBName   string
	Port     int
	SslMode  bool
}

func (ConfigPostgres) GetDSN

func (c ConfigPostgres) GetDSN() string

GetDSN creates the DSN (Data Source Name) string for the postgres database connection.

func (ConfigPostgres) GetGormDialector

func (c ConfigPostgres) GetGormDialector() (gorm.Dialector, error)

type ConfigSQLite

type ConfigSQLite struct {
	ResolvePathCreateDir bool
	DSN                  string
	// contains filtered or unexported fields
}

func (ConfigSQLite) GetGormDialector

func (c ConfigSQLite) GetGormDialector() (gorm.Dialector, error)

type ConnOption

type ConnOption func(*connOptions)

func WithLogger

func WithLogger(writer logger.Writer, config logger.Config) ConnOption

WithLogger configures the logger instance used by the database manager. writer instance allows setting up the log destination. By default, we set it to standard `log.Default()`. config is the gorm logger config that allows setting the log level, etc. by default, we set log level to Warning.

func WithRetries

func WithRetries(maxRetries, retryIntervalSec, jitterLimitSec int) ConnOption

WithRetries configures db connection retries. maxRetries represents number of times the connection request should be retried on failure. one attempt is done regardless. and then re-attempt is done based on this number. i.e. <1 means total 1 attempt. >1 means n+1 attempt in total. retryIntervalSec represents wait time (in seconds) before each retry attempt. jitterLimitSec represents jitter time limit (in seconds), such that a random time interval between 0 and this number is selected and added to the retry interval on each retry attempt to avoid high retry traffic on exact same time from all servers.

type DBDriver

type DBDriver uint

func DBDriverFromStr

func DBDriverFromStr(dbType string) DBDriver

func (DBDriver) String

func (dbType DBDriver) String() string

type DialectorGetter

type DialectorGetter interface {
	GetGormDialector() (gorm.Dialector, error)
}

type DialectorSwitcher

type DialectorSwitcher struct {
	ConfigPostgres
	ConfigSQLite
}

func (DialectorSwitcher) Connect

func (conf DialectorSwitcher) Connect(dbType DBDriver, ops ...ConnOption) (*gorm.DB, error)

func (DialectorSwitcher) Switch

func (conf DialectorSwitcher) Switch(dbType DBDriver) (gorm.Dialector, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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