database

package
v0.0.0-...-625faaf Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

ProviderSet provides database-related dependencies

Functions

func GetConnMaxIdleTime

func GetConnMaxIdleTime(maxIdleTime int) time.Duration

GetConnMaxIdleTime returns ConnMaxIdleTime as time.Duration from common config

func GetConnMaxLifetime

func GetConnMaxLifetime(maxLifetime int) time.Duration

GetConnMaxLifetime returns ConnMaxLifetime as time.Duration from common config

func ProvideMySQL

func ProvideMySQL(manager Manager) *gorm.DB

ProvideMySQL provides MySQL database instance from Manager

func Read

func Read() dbresolver.Operation

Read forces the query to use replicas (read-only) Usage: db.Clauses(Read()).Find(&users)

func ReadDB

func ReadDB(db *gorm.DB) *gorm.DB

ReadDB returns a DB instance configured for read operations (replicas) This is a convenience method for read-only queries

func Use

func Use(name string) clause.Expression

Use specifies which resolver to use (for named resolvers) Usage: db.Clauses(Use("secondary")).Find(&orders)

func UseResolver

func UseResolver(db *gorm.DB, resolverName string) *gorm.DB

UseResolver returns a DB instance configured to use a specific named resolver This is useful when you have multiple resolvers registered

func UseResolverRead

func UseResolverRead(db *gorm.DB, resolverName string) *gorm.DB

UseResolverRead returns a DB instance configured to use a specific named resolver in read mode

func UseResolverWrite

func UseResolverWrite(db *gorm.DB, resolverName string) *gorm.DB

UseResolverWrite returns a DB instance configured to use a specific named resolver in write mode

func Write

func Write() dbresolver.Operation

Write forces the query to use sources (write) Usage: db.Clauses(Write()).First(&user)

func WriteDB

func WriteDB(db *gorm.DB) *gorm.DB

WriteDB returns a DB instance configured for write operations (sources) This is a convenience method for write operations

Types

type Database

type Database struct {
	// Driver is the database driver: "mysql" or "sqlite". Defaults to "mysql" when empty.
	Driver string `mapstructure:"driver"`
	// Data source configurations (only the one matching driver is required)
	MySQL  MySQLConfig  `mapstructure:"mysql"`
	SQLite SQLiteConfig `mapstructure:"sqlite"`
	// Common options for connection pool and logging
	Options Options `mapstructure:"options"`
}

Database represents the database configuration

type GormLoggerAdapter

type GormLoggerAdapter struct {
	Config logger.Config
	Level  logger.LogLevel
}

func NewGormLoggerAdapter

func NewGormLoggerAdapter(config logger.Config, logLevel logger.LogLevel) *GormLoggerAdapter

func (*GormLoggerAdapter) Error

func (l *GormLoggerAdapter) Error(_ context.Context, msg string, data ...interface{})

func (*GormLoggerAdapter) Info

func (l *GormLoggerAdapter) Info(_ context.Context, msg string, data ...interface{})

func (*GormLoggerAdapter) LogMode

func (l *GormLoggerAdapter) LogMode(level logger.LogLevel) logger.Interface

func (*GormLoggerAdapter) Trace

func (l *GormLoggerAdapter) Trace(_ context.Context, begin time.Time, fc func() (string, int64), err error)

func (*GormLoggerAdapter) Warn

func (l *GormLoggerAdapter) Warn(_ context.Context, msg string, data ...interface{})

type IDatabase

type IDatabase interface {
	// Database returns the default *gorm.DB (MySQL or SQLite)
	Database() *gorm.DB
}

IDatabase defines database interface for backward compatibility It provides access to the default database connection (MySQL or SQLite per config)

func NewDatabaseAdapter

func NewDatabaseAdapter(manager Manager) IDatabase

NewDatabaseAdapter creates an IDatabase adapter from Manager

func ProvideIDatabase

func ProvideIDatabase(manager Manager) IDatabase

ProvideIDatabase provides IDatabase interface instance for backward compatibility

type Manager

type Manager interface {
	// MySQL returns the MySQL database connection (may be nil if using SQLite only)
	MySQL() *gorm.DB
	// SQLite returns the SQLite database connection (may be nil if using MySQL only)
	SQLite() *gorm.DB
	// Default returns the default database connection for the configured driver
	Default() *gorm.DB
	// Close closes all database connections
	Close() error
}

Manager defines the unified database interface for managing database connections

func NewManager

func NewManager(cfg Database) (Manager, error)

NewManager creates a new database manager with MySQL and/or SQLite connections

func ProvideManager

func ProvideManager(conf Database, _ *log.Logger) (Manager, error)

ProvideManager creates and returns a database Manager instance

type MySQLConfig

type MySQLConfig struct {
	DSN string `mapstructure:"dsn"`
}

MySQLConfig represents MySQL data source configuration (DSN only)

type Options

type Options struct {
	OutPut       bool `mapstructure:"output"`
	MaxOpenConns int  `mapstructure:"maxOpenConns"`
	MaxIdleConns int  `mapstructure:"maxIdleConns"`
	MaxLifetime  int  `mapstructure:"maxLifeTime"`
	MaxIdleTime  int  `mapstructure:"maxIdleTime"`
}

Options holds common connection pool and logging options

type SQLiteConfig

type SQLiteConfig struct {
	DSN string `mapstructure:"dsn"`
}

SQLiteConfig represents SQLite data source configuration (DSN only)

Jump to

Keyboard shortcuts

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