xdb

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: MIT Imports: 14 Imported by: 1

README

xdb

A multi-database connection wrapper library built on GORM, supporting MySQL, PostgreSQL, SQLite, SQL Server, and ClickHouse.

Installation

go get github.com/sk-pkg/xdb

Quick Start

Single Database Connection
package main

import (
    "log"

    "github.com/sk-pkg/xdb"
    "gorm.io/gorm"
)

type Product struct {
    gorm.Model
    Code  string
    Price uint
}

func main() {
    // Create database connection using DBConfig (recommended)
    db, err := xdb.New(xdb.WithDBConfig(xdb.DBConfig{
        Driver:   xdb.MySQL,
        Host:     "127.0.0.1",
        Port:     3306,
        User:     "root",
        Password: "secret",
        DBName:   "mydb",
    }))
    if err != nil {
        log.Fatal("failed to connect database:", err)
    }

    // Auto migrate schema
    db.AutoMigrate(&Product{})

    // Create
    db.Create(&Product{Code: "D42", Price: 100})

    // Read
    var product Product
    db.First(&product, 1)
    db.First(&product, "code = ?", "D42")

    // Update
    db.Model(&product).Update("Price", 200)

    // Delete
    db.Delete(&product, 1)
}
Multiple Database Connections
package main

import (
    "log"

    "github.com/sk-pkg/xdb"
)

func main() {
    // Create multiple database connections
    dbs, err := xdb.NewMulti(
        []xdb.Option{
            xdb.WithHost("127.0.0.1"),
            xdb.WithPort(3306),
            xdb.WithUser("root"),
            xdb.WithPassword("secret"),
            xdb.WithDBName("master_db"),
        },
        []xdb.Option{
            xdb.WithHost("127.0.0.1"),
            xdb.WithPort(3307),
            xdb.WithUser("root"),
            xdb.WithPassword("secret"),
            xdb.WithDBName("slave_db"),
        },
    )
    if err != nil {
        log.Fatal("failed to connect database:", err)
    }
    defer xdb.CloseAll(dbs)

    // Use specific database (key is dbName)
    masterDB := dbs["master_db"]
    slaveDB := dbs["slave_db"]

    // Perform operations...
    _ = masterDB
    _ = slaveDB
}

Supported Databases

Driver Constant Default Port
MySQL xdb.MySQL 3306
PostgreSQL xdb.PostgreSQL 5432
SQLite xdb.SQLite -
SQL Server xdb.SQLServer 1433
ClickHouse xdb.ClickHouse 9000
PostgreSQL Example
db, err := xdb.New(
    xdb.WithDriver(xdb.PostgreSQL),
    xdb.WithHost("localhost"),
    xdb.WithPort(5432),
    xdb.WithUser("postgres"),
    xdb.WithPassword("secret"),
    xdb.WithDBName("mydb"),
    xdb.WithSSLMode("disable"),
    xdb.WithTimezone("Asia/Shanghai"),
)
SQLite Example
db, err := xdb.New(
    xdb.WithDriver(xdb.SQLite),
    xdb.WithHost("./data.db"), // File path
)

Configuration Options

WithDBConfig allows you to set all options at once using a struct:

db, err := xdb.New(xdb.WithDBConfig(xdb.DBConfig{
    Driver:          xdb.MySQL,
    Host:            "127.0.0.1",
    Port:            3306,
    User:            "root",
    Password:        "secret",
    DBName:          "mydb",
    Charset:         "utf8mb4",
    MaxIdleConns:    10,
    MaxOpenConns:    100,
    ConnMaxLifetime: 3 * time.Hour,
}))
Connection Options
Option Description Example
WithDBConfig(cfg) Set all options at once See above
WithDriver(driver) Database driver type xdb.MySQL
WithHost(host) Database host address "127.0.0.1"
WithPort(port) Database port 3306
WithUser(user) Username "root"
WithPassword(pwd) Password "secret"
WithDBName(name) Database name "mydb"
Driver-Specific Options
Option Description Applicable Drivers
WithCharset(charset) Character set (default: utf8mb4) MySQL
WithSSLMode(mode) SSL mode PostgreSQL
WithTimezone(tz) Timezone PostgreSQL, ClickHouse
Connection Pool Options
Option Description Default
WithMaxIdleConns(n) Maximum idle connections 10
WithMaxOpenConns(n) Maximum open connections 50
WithConnMaxLifetime(d) Maximum connection lifetime 3 hours
WithConnMaxIdleTime(d) Maximum connection idle time 0 (unlimited)
GORM Configuration
db, err := xdb.New(
    xdb.WithHost("localhost"),
    xdb.WithDBName("mydb"),
    xdb.WithGormConfig(gorm.Config{
        SkipDefaultTransaction: true,
        Logger: customLogger,
    }),
)

Logging

xdb provides a GORM logger adapter that integrates with sk-pkg/logger.

Creating a Logger
import (
    "github.com/sk-pkg/logger"
    "github.com/sk-pkg/xdb"
    "gorm.io/gorm"
)

// Initialize logger manager
manager, err := logger.New()
if err != nil {
    log.Fatal(err)
}

// Create database logger
dbLogger := xdb.NewLog(manager,
    xdb.WithLevel("info"),
    xdb.WithSlowThreshold(200 * time.Millisecond),
    xdb.WithIgnoreRecordNotFoundError(true),
)

// Use with database
db, err := xdb.New(
    xdb.WithHost("localhost"),
    xdb.WithDBName("mydb"),
    xdb.WithGormConfig(gorm.Config{
        Logger: dbLogger,
    }),
)
Logger Options
Option Description Default
WithLevel(level) Log level: "info", "warn", "error", "silent" "warn"
WithSlowThreshold(d) Slow query threshold 200ms
WithIgnoreRecordNotFoundError(b) Ignore record not found errors true
Context Tracing
// Add trace_id to context
ctx := context.WithValue(context.Background(), "trace_id", "123456")
db = db.WithContext(ctx)

// Subsequent queries will include trace_id in logs
db.First(&product, 1)

Utility Functions

Health Check
db, err := xdb.New(...)
if err != nil {
    log.Fatal(err)
}

// Check if connection is healthy
if err := xdb.Ping(db); err != nil {
    log.Fatal("database connection failed:", err)
}
Closing Connections
// Close single connection
if err := xdb.Close(db); err != nil {
    log.Error("failed to close connection:", err)
}

// Close multiple connections
if err := xdb.CloseAll(dbs); err != nil {
    log.Error("failed to close connections:", err)
}
Connection Pool Stats
stats, err := xdb.Stats(db)
if err != nil {
    log.Error(err)
}

fmt.Printf("Open connections: %d\n", stats.OpenConnections)
fmt.Printf("In use: %d\n", stats.InUse)
fmt.Printf("Idle: %d\n", stats.Idle)

Notes

  1. Set appropriate log levels in production to avoid excessive logging
  2. Configure slow query threshold based on your application requirements
  3. Using WithIgnoreRecordNotFoundError(true) reduces unnecessary error logs
  4. SQLite does not support connection pool settings

Documentation

License

MIT

Documentation

Overview

Package xdb provides functionality for creating and managing database connections using the GORM library. It supports multiple database types including MySQL, PostgreSQL, SQLite, SQL Server, and ClickHouse. It uses the functional options pattern for flexible configuration of database connections with customizable connection pool settings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close(db *gorm.DB) error

Close closes the database connection.

Example:

if err := Close(db); err != nil {
    log.Error("failed to close connection:", err)
}

func CloseAll

func CloseAll(dbs map[string]*gorm.DB) error

CloseAll closes all database connections in the map.

Example:

if err := CloseAll(dbs); err != nil {
    log.Error("failed to close connections:", err)
}

func New

func New(opts ...Option) (*gorm.DB, error)

New initializes and returns a database connection instance.

Example:

db, err := New(
    WithDriver(MySQL),
    WithHost("localhost"),
    WithPort(3306),
    WithUser("root"),
    WithPassword("secret"),
    WithDBName("mydb"),
    WithMaxIdleConns(10),
    WithMaxOpenConns(100),
)

func NewLog

func NewLog(manager *sklogger.Manager, opts ...LoggerOption) gormlogger.Interface

NewLog creates and returns a new logger instance with the given options.

Parameters:

  • manager: A pointer to the sklogger.Manager to use for logging.
  • opts: A variadic list of LoggerOption functions to configure the logger.

Returns:

  • A new gormlogger.Interface configured with the provided options.

Example:

logger := NewLog(manager, WithLevel("info"), WithSlowThreshold(300*time.Millisecond))

func NewMulti

func NewMulti(configs ...[]Option) (map[string]*gorm.DB, error)

NewMulti creates multiple database connections and returns them as a map. Each connection is identified by its dbName from the Options.

Example:

dbs, err := NewMulti(
    []Option{
        WithHost("localhost"),
        WithDBName("master_db"),
    },
    []Option{
        WithHost("localhost"),
        WithDBName("slave_db"),
    },
)
masterDB := dbs["master_db"]
slaveDB := dbs["slave_db"]

func Ping

func Ping(db *gorm.DB) error

Ping checks if the database connection is alive.

Example:

if err := Ping(db); err != nil {
    log.Fatal("database connection failed:", err)
}

func Stats

func Stats(db *gorm.DB) (sql.DBStats, error)

Stats returns the connection pool statistics.

Example:

stats, err := Stats(db)
if err != nil {
    log.Error(err)
}
fmt.Printf("Open connections: %d\n", stats.OpenConnections)

Types

type DBConfig

type DBConfig struct {
	Driver          Driver        // Database driver type
	Host            string        // Database host (for SQLite, this is the file path)
	Port            int           // Database port
	User            string        // Database user
	Password        string        // Database password
	DBName          string        // Database name
	SSLMode         string        // SSL mode (for PostgreSQL)
	Timezone        string        // Timezone (for PostgreSQL and ClickHouse)
	Charset         string        // Character set (for MySQL)
	MaxIdleConns    int           // Maximum number of idle connections
	MaxOpenConns    int           // Maximum number of open connections
	ConnMaxLifetime time.Duration // Maximum connection lifetime
	ConnMaxIdleTime time.Duration // Maximum idle time for connections
	GormConfig      *gorm.Config  // GORM configuration
}

DBConfig holds all the configuration options for database connections. Use this with WithDBConfig to set multiple options at once.

Example:

db, err := New(WithDBConfig(DBConfig{
    Driver:       MySQL,
    Host:         "localhost",
    Port:         3306,
    User:         "root",
    Password:     "secret",
    DBName:       "mydb",
    MaxIdleConns: 10,
    MaxOpenConns: 100,
}))

type Driver

type Driver string

Driver represents the database driver type.

const (
	// MySQL driver
	MySQL Driver = "mysql"
	// PostgreSQL driver
	PostgreSQL Driver = "postgres"
	// SQLite driver
	SQLite Driver = "sqlite"
	// SQLServer driver
	SQLServer Driver = "sqlserver"
	// ClickHouse driver
	ClickHouse Driver = "clickhouse"
)

type LoggerOption

type LoggerOption func(*logger)

LoggerOption is a function type used to configure the logger.

func WithIgnoreRecordNotFoundError

func WithIgnoreRecordNotFoundError(ignore bool) LoggerOption

WithIgnoreRecordNotFoundError returns a LoggerOption that sets whether to ignore "record not found" errors.

Parameters:

  • ignore: A boolean indicating whether to ignore "record not found" errors.

Returns:

  • A LoggerOption function that sets the ignore flag when applied.

Example:

logger := NewLog(manager, WithIgnoreRecordNotFoundError(true))

func WithLevel

func WithLevel(level string) LoggerOption

WithLevel returns a LoggerOption that sets the log level for the logger.

Parameters:

  • level: A string representing the desired log level ("debug", "info", "warn", or "silent").

Returns:

  • A LoggerOption function that sets the log level when applied.

Example:

logger := NewLog(manager, WithLevel("info"))

func WithSlowThreshold

func WithSlowThreshold(threshold time.Duration) LoggerOption

WithSlowThreshold returns a LoggerOption that sets the threshold for slow query logging.

Parameters:

  • threshold: A time.Duration representing the slow query threshold.

Returns:

  • A LoggerOption function that sets the slow threshold when applied.

Example:

logger := NewLog(manager, WithSlowThreshold(500 * time.Millisecond))

type Option

type Option func(*option)

Option is a function type used to apply configuration options.

func WithCharset

func WithCharset(charset string) Option

WithCharset sets the character set (for MySQL, default: utf8mb4).

Example:

db, err := New(WithCharset("utf8mb4"), ...)

func WithConnMaxIdleTime

func WithConnMaxIdleTime(d time.Duration) Option

WithConnMaxIdleTime sets the maximum amount of time a connection may be idle.

Example:

db, err := New(WithConnMaxIdleTime(10 * time.Minute), ...)

func WithConnMaxLifetime

func WithConnMaxLifetime(d time.Duration) Option

WithConnMaxLifetime sets the maximum amount of time a connection may be reused.

Example:

db, err := New(WithConnMaxLifetime(time.Hour), ...)

func WithDBConfig

func WithDBConfig(cfg DBConfig) Option

WithDBConfig sets multiple database options from a DBConfig struct. Only non-zero values will be applied.

Example:

db, err := New(WithDBConfig(DBConfig{
    Driver:       MySQL,
    Host:         "localhost",
    Port:         3306,
    User:         "root",
    Password:     "secret",
    DBName:       "mydb",
    MaxIdleConns: 10,
    MaxOpenConns: 100,
}))

func WithDBName

func WithDBName(name string) Option

WithDBName sets the database name.

Example:

db, err := New(WithDBName("mydb"), ...)

func WithDriver

func WithDriver(driver Driver) Option

WithDriver sets the database driver type.

Example:

db, err := New(WithDriver(MySQL), WithHost("localhost"), ...)

func WithGormConfig

func WithGormConfig(cfg gorm.Config) Option

WithGormConfig sets the GORM configuration.

Example:

db, err := New(WithGormConfig(gorm.Config{SkipDefaultTransaction: true}), ...)

func WithHost

func WithHost(host string) Option

WithHost sets the database host address. For SQLite, this should be the file path.

Example:

db, err := New(WithHost("localhost"), ...)

func WithMaxIdleConns

func WithMaxIdleConns(n int) Option

WithMaxIdleConns sets the maximum number of idle connections in the pool.

Example:

db, err := New(WithMaxIdleConns(10), ...)

func WithMaxOpenConns

func WithMaxOpenConns(n int) Option

WithMaxOpenConns sets the maximum number of open connections to the database.

Example:

db, err := New(WithMaxOpenConns(100), ...)

func WithPassword

func WithPassword(password string) Option

WithPassword sets the database password.

Example:

db, err := New(WithPassword("secret"), ...)

func WithPort

func WithPort(port int) Option

WithPort sets the database port. If not specified, the default port for the driver will be used.

Example:

db, err := New(WithPort(3306), ...)

func WithSSLMode

func WithSSLMode(mode string) Option

WithSSLMode sets the SSL mode (for PostgreSQL). Valid values: disable, require, verify-ca, verify-full

Example:

db, err := New(WithDriver(PostgreSQL), WithSSLMode("require"), ...)

func WithTimezone

func WithTimezone(tz string) Option

WithTimezone sets the timezone (for PostgreSQL and ClickHouse).

Example:

db, err := New(WithTimezone("Asia/Shanghai"), ...)

func WithUser

func WithUser(user string) Option

WithUser sets the database user.

Example:

db, err := New(WithUser("root"), ...)

Directories

Path Synopsis
example
mysql command
postgres command

Jump to

Keyboard shortcuts

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