Documentation
¶
Index ¶
- Variables
- func ApplyMigrations(ctx context.Context, db *DB, models ...any) error
- func IsNotExist(err error) bool
- type DB
- type DBType
- type DatabaseConfig
- type Option
- func GetOptions() ([]Option, error)
- func WithConnMaxLifetime(d time.Duration) Option
- func WithDBLogLevel(level logger.LogLevel) Option
- func WithDBLogger(gormLogger logger.Interface) Option
- func WithDSN(dsn string) Option
- func WithMaxIdleConns(n int) Option
- func WithMaxOpenConns(n int) Option
- func WithType(dbType DBType) Option
Constants ¶
This section is empty.
Variables ¶
var ( // DatabaseConfigSet is the configuration set for database settings. DatabaseConfigSet = config.New("database").WithValidate(validateDatabaseFlags) TypeFlag = DatabaseConfigSet.String("database.type", SQLite.String(), "The type of database to connect/use: supported values: sqlite, postgres, mysql. (For testing purposes there is also inmemory)") DSNFlag = DatabaseConfigSet.String("database.dsn", "db.sqlite", "A datasource name, depends on type of database, but usually referes to file name or the connection string") MaxIdleConnsFlag = DatabaseConfigSet.Int("database.max-idle-conss", 2, "Sets the maximum number of connections in the idle connection pool. If n <= 0, no idle connections are retained.") MaxOpenConnsFlag = DatabaseConfigSet.Int("database.max-open-conns", 0, "Sets the maximum number of open connections to the database. If n <= 0, then there is no limit on the number of open connections.") ConnMaxLifetimeFlag = DatabaseConfigSet.Duration("database.conn-max-lifetime", 1*time.Hour, "Sets the maximum amount of time a connection may be reused. If d <= 0, connections are not closed due to a connection's age.") )
Functions ¶
func ApplyMigrations ¶
ApplyMigrations applies database migrations
func IsNotExist ¶
IsNotExist checks if the error is a record not found error
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB wraps a GORM database connection.
func (*DB) ShutdownCleanup ¶
ShutdownCleanup implements lifecycle.ShutdownCleanup.
type DatabaseConfig ¶
type DatabaseConfig struct {
Type DBType
DSN string
MaxIdleConns int
MaxOpenConns int
ConnMaxLifetime time.Duration
LogLevel logger.LogLevel
Logger logger.Interface
}
DatabaseConfig holds the database configuration.
type Option ¶
type Option func(*DatabaseConfig)
Option is a function that modifies the Config
func GetOptions ¶
GetOptions builds a list of options from flags/envs variable via viper. See DatabaseConfig for available values
func WithConnMaxLifetime ¶
WithConnMaxLifetime sets the maximum lifetime of a connection
func WithDBLogLevel ¶
WithDBLogLevel sets the log level for the database
func WithDBLogger ¶
WithDBLogger provides the db with a new logger, ignores WithDBLogLevel
func WithMaxIdleConns ¶
WithMaxIdleConns sets the maximum number of idle connections
func WithMaxOpenConns ¶
WithMaxOpenConns sets the maximum number of open connections