fxgorm

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2025 License: MIT Imports: 13 Imported by: 0

README

Dynamic GORM Module

A highly configurable and dynamic GORM module for the UTOL module system that supports multiple database types, connection pooling, logging, and more.

Features

  • Multi-Database Support: PostgreSQL, MySQL, SQLite, and SQL Server
  • Dynamic Configuration: Runtime configuration through YAML files and environment variables
  • Connection Pooling: Configurable connection pool settings
  • Advanced Logging: Customizable logging levels and slow query detection
  • Debug Mode: Dry run mode for development and testing
  • Dependency Injection: Seamless integration with Uber FX

Supported Database Types

PostgreSQL
database:
  type: "postgres"
  host: "localhost"
  port: 5432
  user: "postgres"
  password: "password"
  dbname: "utol_db"
  sslmode: "disable"
MySQL
database:
  type: "mysql"
  host: "localhost"
  port: 3306
  user: "root"
  password: "password"
  dbname: "utol_db"
  charset: "utf8mb4"
  parse_time: true
  loc: "Local"
SQLite
database:
  type: "sqlite"
  file: "./data/utol.db"
SQL Server
database:
  type: "sqlserver"
  host: "localhost"
  port: 1433
  user: "sa"
  password: "password"
  dbname: "utol_db"

Configuration Options

Connection Pool Settings
database:
  pool:
    max_idle_conns: 10        # Maximum number of idle connections
    max_open_conns: 100       # Maximum number of open connections
    conn_max_lifetime: 3600   # Maximum lifetime of connections (seconds)
    conn_max_idle_time: 600   # Maximum idle time of connections (seconds)
Logging Configuration
database:
  log:
    level: 4                                    # Log level (1=Silent, 2=Error, 3=Warn, 4=Info)
    slow_threshold: 5000                       # Slow query threshold (milliseconds)
    colorful: true                             # Enable colored output
    ignore_record_not_found_error: true        # Ignore "record not found" errors
Debug Mode
database:
  debug: false  # Enable dry run mode (no actual database operations)

Usage

Basic Usage
package main

import (
    "github.com/UTOL-s/module/fxGorm"
    "go.uber.org/fx"
)

func main() {
    app := fx.New(
        fxGorm.FxGorm,
        // ... other modules
    )
    
    app.Run()
}
Advanced Usage with Custom Configuration
package main

import (
    "github.com/UTOL-s/module/fxGorm"
    "go.uber.org/fx"
)

type App struct {
    DB *gorm.DB
}

func NewApp(db *gorm.DB) *App {
    return &App{DB: db}
}

func main() {
    app := fx.New(
        fxGorm.FxGorm,
        fx.Provide(NewApp),
        fx.Invoke(func(app *App) {
            // Your application logic here
        }),
    )
    
    app.Run()
}

Environment Variables

All configuration options can be overridden using environment variables:

# Database type
export DATABASE_TYPE=postgres

# Connection settings
export DATABASE_HOST=localhost
export DATABASE_PORT=5432
export DATABASE_USER=postgres
export DATABASE_PASSWORD=password
export DATABASE_DBNAME=utol_db

# Pool settings
export DATABASE_POOL_MAX_IDLE_CONNS=10
export DATABASE_POOL_MAX_OPEN_CONNS=100

# Logging settings
export DATABASE_LOG_LEVEL=4
export DATABASE_LOG_SLOW_THRESHOLD=5000

Default Values

If configuration options are not specified, the following defaults are used:

  • Database Type: PostgreSQL (if not specified)
  • Max Idle Connections: 10
  • Max Open Connections: 100
  • Connection Max Lifetime: 1 hour
  • Connection Max Idle Time: 10 minutes
  • Log Level: Info (4)
  • Slow Threshold: 5 seconds
  • Debug Mode: false

Error Handling

The module includes comprehensive error handling:

  • Database connection failures
  • Configuration validation
  • Connection pool setup errors
  • Database ping failures

All errors are wrapped with context for better debugging.

Testing

The module includes built-in connection testing:

  • Automatic connection validation on startup
  • Database ping verification
  • Configuration validation

Migration from Previous Version

If you're upgrading from the previous version, update your configuration:

# Old configuration
database:
  host: "localhost"
  port: 5432
  # ... other settings

# New configuration
database:
  type: "postgres"  # Add this line
  host: "localhost"
  port: 5432
  # ... other settings

Dependencies

The module requires the following GORM drivers (automatically added to go.mod):

  • gorm.io/driver/postgres - PostgreSQL support
  • gorm.io/driver/mysql - MySQL support
  • gorm.io/driver/sqlite - SQLite support
  • gorm.io/driver/sqlserver - SQL Server support

License

This module is part of the UTOL project and follows the same license terms.

Documentation

Index

Examples

Constants

This section is empty.

Variables

FxGorm provides the GORM module for dependency injection

Functions

func NewGormDB

func NewGormDB(p Params) (*gorm.DB, error)

NewGormDB creates a new GORM database instance with dynamic configuration

Types

type DatabaseConfig

type DatabaseConfig struct {
	Type      DatabaseType `mapstructure:"type"`
	Host      string       `mapstructure:"host"`
	Port      int          `mapstructure:"port"`
	User      string       `mapstructure:"user"`
	Password  string       `mapstructure:"password"`
	DBName    string       `mapstructure:"dbname"`
	SSLMode   string       `mapstructure:"sslmode"`
	Charset   string       `mapstructure:"charset"`
	ParseTime bool         `mapstructure:"parse_time"`
	Loc       string       `mapstructure:"loc"`
	File      string       `mapstructure:"file"` // For SQLite
}

DatabaseConfig holds database-specific configuration

type DatabaseManager

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

DatabaseManager handles database operations

func NewDatabaseManager

func NewDatabaseManager(config *GormConfig) *DatabaseManager

NewDatabaseManager creates a new database manager

func NewDatabaseManagerWithConfig

func NewDatabaseManagerWithConfig(config *GormConfig) *DatabaseManager

NewDatabaseManagerWithConfig creates a new database manager with the given configuration

func (*DatabaseManager) Close

func (dm *DatabaseManager) Close() error

Close closes the database connection

func (*DatabaseManager) Connect

func (dm *DatabaseManager) Connect() error

Connect establishes a database connection with validation

func (*DatabaseManager) GetDB

func (dm *DatabaseManager) GetDB() *gorm.DB

GetDB returns the database instance

func (*DatabaseManager) GetPoolConfig

func (dm *DatabaseManager) GetPoolConfig() PoolConfig

GetPoolConfig returns the current pool configuration

func (*DatabaseManager) GetPoolStats

func (dm *DatabaseManager) GetPoolStats() (*sql.DBStats, error)

GetPoolStats returns current connection pool statistics

func (*DatabaseManager) SetPoolConfig

func (dm *DatabaseManager) SetPoolConfig(config PoolConfig) error

SetPoolConfig updates the connection pool configuration

type DatabaseType

type DatabaseType string

DatabaseType represents supported database types

const (
	PostgreSQL DatabaseType = "postgres"
	MySQL      DatabaseType = "mysql"
	SQLite     DatabaseType = "sqlite"
	SQLServer  DatabaseType = "sqlserver"
)

type GormConfig

type GormConfig struct {
	Database DatabaseConfig `mapstructure:"database"`
	Pool     PoolConfig     `mapstructure:"pool"`
	Log      LogConfig      `mapstructure:"log"`
	Debug    bool           `mapstructure:"debug"`
}

GormConfig holds the complete GORM configuration

func NewGormConfig

func NewGormConfig(config *fxconfig.Config) *GormConfig

NewGormConfig creates a new GORM configuration from the main config

Example

ExampleGormConfig demonstrates how to create a GORM configuration

// This would typically come from your main config
config := &fxconfig.Config{}

// Create GORM configuration
gormConfig := NewGormConfig(config)
_ = gormConfig

func (*GormConfig) SetDefaults

func (gc *GormConfig) SetDefaults()

SetDefaults sets default values for unconfigured settings

func (*GormConfig) Validate

func (gc *GormConfig) Validate() error

Validate validates the configuration

type LogConfig

type LogConfig struct {
	Level                     logger.LogLevel `mapstructure:"level"`
	SlowThreshold             time.Duration   `mapstructure:"slow_threshold"`
	Colorful                  bool            `mapstructure:"colorful"`
	IgnoreRecordNotFoundError bool            `mapstructure:"ignore_record_not_found_error"`
}

LogConfig holds logging configuration

type Params

type Params struct {
	Config *fxconfig.Config
}

Params holds the dependency injection parameters

type PoolConfig

type PoolConfig struct {
	MaxIdleConns    int           `mapstructure:"max_idle_conns"`
	MaxOpenConns    int           `mapstructure:"max_open_conns"`
	ConnMaxLifetime time.Duration `mapstructure:"conn_max_lifetime"`
	ConnMaxIdleTime time.Duration `mapstructure:"conn_max_idle_time"`
}

PoolConfig holds connection pool configuration

Jump to

Keyboard shortcuts

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