persistence

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

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

Go to latest
Published: Feb 2, 2026 License: BSD-3-Clause Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func ApplyMigrations(ctx context.Context, db *DB, models ...any) error

ApplyMigrations applies database migrations

func IsNotExist

func IsNotExist(err error) bool

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 NewDB

func NewDB(opts ...Option) (*DB, error)

NewDB creates a new database connection with the given options

func (*DB) Close

func (db *DB) Close(ctx context.Context) error

Close closes the database connection.

func (*DB) ShutdownCleanup

func (db *DB) ShutdownCleanup(ctx context.Context) error

ShutdownCleanup implements lifecycle.ShutdownCleanup.

func (*DB) WithContext

func (db *DB) WithContext(ctx context.Context) *gorm.DB

WithContext returns a GORM DB instance with the provided context.

type DBType

type DBType string

DBType represents the type of database to use

const (
	// SQLite database type
	SQLite DBType = "sqlite"
	// InMemory SQLite database
	InMemory DBType = "memory"
	// PostgreSQL database type
	PostgreSQL DBType = "postgres"
	// MySQL database type
	MySQL DBType = "mysql"
)

func (DBType) String

func (db DBType) String() string

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

func GetOptions() ([]Option, error)

GetOptions builds a list of options from flags/envs variable via viper. See DatabaseConfig for available values

func WithConnMaxLifetime

func WithConnMaxLifetime(d time.Duration) Option

WithConnMaxLifetime sets the maximum lifetime of a connection

func WithDBLogLevel

func WithDBLogLevel(level logger.LogLevel) Option

WithDBLogLevel sets the log level for the database

func WithDBLogger

func WithDBLogger(gormLogger logger.Interface) Option

WithDBLogger provides the db with a new logger, ignores WithDBLogLevel

func WithDSN

func WithDSN(dsn string) Option

WithDSN sets the data source name

func WithMaxIdleConns

func WithMaxIdleConns(n int) Option

WithMaxIdleConns sets the maximum number of idle connections

func WithMaxOpenConns

func WithMaxOpenConns(n int) Option

WithMaxOpenConns sets the maximum number of open connections

func WithType

func WithType(dbType DBType) Option

WithType sets the database type

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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