database

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package database provides database connection management.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect() (*gorm.DB, error)

Connect establishes a database connection using environment configuration.

func ConnectWithConfig

func ConnectWithConfig(cfg DBConfig) (*gorm.DB, error)

ConnectWithConfig establishes a database connection using the provided configuration.

func WithTransaction

func WithTransaction(db *gorm.DB, fn TxFunc) error

WithTransaction wraps fn in a database transaction. If fn returns nil the transaction commits. If fn returns an error or panics the transaction rolls back.

Types

type DBConfig

type DBConfig struct {
	Driver          string
	Host            string
	Port            string
	Name            string
	User            string
	Password        string
	SSLMode         string
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime time.Duration
	ConnMaxIdleTime time.Duration
}

DBConfig holds database connection configuration.

func NewDBConfig

func NewDBConfig() DBConfig

NewDBConfig reads database configuration from environment variables.

func NewReadDBConfig

func NewReadDBConfig() DBConfig

NewReadDBConfig reads read-replica configuration from DB_READ_* environment variables. Each setting falls back to the corresponding DB_* value, then to the same defaults used by NewDBConfig.

func (DBConfig) DSN

func (cfg DBConfig) DSN() string

DSN returns the data source name for the configured driver.

type Resolver

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

Resolver holds separate database connections for write and read operations.

func NewResolver

func NewResolver(writer, reader *gorm.DB) *Resolver

NewResolver creates a Resolver with the given writer and reader connections. If reads should go to the same database, pass the same *gorm.DB for both.

func (*Resolver) Reader

func (r *Resolver) Reader() *gorm.DB

Reader returns the read (replica) database connection.

func (*Resolver) Writer

func (r *Resolver) Writer() *gorm.DB

Writer returns the write (primary) database connection.

type TxFunc

type TxFunc func(tx *gorm.DB) error

TxFunc is the callback signature for transactional operations. Return nil to commit, return an error to rollback.

Directories

Path Synopsis
Package migrations provides a file-based migration engine for GORM.
Package migrations provides a file-based migration engine for GORM.
Package seeders provides a registry-based database seeding system.
Package seeders provides a registry-based database seeding system.

Jump to

Keyboard shortcuts

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