db

package
v0.21.4 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package db is nexus's driver-agnostic GORM manager. It mirrors the shape of oats_admin_backend/database.DBManager (Start/Stop/GetDB/IsConnected) and keeps the same failsafe-go retry + circuit-breaker behavior, but handles PostgreSQL, MySQL, and SQLite behind a single Config.Driver field.

Typical usage:

m, err := db.Open(db.Config{
    Driver:   db.SQLite,
    Database: ":memory:",
})
if err != nil { return err }
m.Start()                     // background reconnect loop
defer m.Stop()

gdb := m.GetDB()              // *gorm.DB — chain real queries
gdb.AutoMigrate(&User{})
gdb.Create(&User{Name: "A"})

Multi-DB? Wrap multiple Managers in a multi.Registry[*db.Manager] and call .Using(name).GetDB() from resolvers — see examples/graphapp.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Driver   Driver
	Host     string
	Port     string
	User     string
	Password string
	Database string
	SSLMode  string // "disable" / "require" / ... — postgres only
	TimeZone string // IANA TZ — postgres only
}

Config is the full connection spec. Host/Port/User/Password/SSLMode/TimeZone apply to postgres+mysql; Database is the db name for pg/mysql and the file path (":memory:" for in-memory) for sqlite.

func (Config) DSN

func (c Config) DSN() string

DSN returns the connection string for the configured Driver.

func (Config) Dialector

func (c Config) Dialector() gorm.Dialector

Dialector returns the gorm dialector matching Driver.

type Driver

type Driver string

Driver picks the backing dialect. Each value has a corresponding Dialector that Config.Dialector() returns.

const (
	Postgres Driver = "postgres"
	MySQL    Driver = "mysql"
	SQLite   Driver = "sqlite"
)

type Manager

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

Manager owns one *gorm.DB, reconnects in the background, and exposes the same method set as oats_admin_backend/database.DatabaseManager so it can drop in wherever that interface is expected.

func NewManager

func NewManager(cfg Config, opts ...Option) *Manager

NewManager builds a Manager without connecting. Call Open or Start next.

func Open

func Open(cfg Config, opts ...Option) (*Manager, error)

Open constructs a Manager and establishes the initial connection synchronously. Call Start() afterwards to enable the background reconnect + health-check loop.

func (*Manager) ConnectionString added in v0.4.1

func (m *Manager) ConnectionString() string

ConnectionString returns the DSN for this manager's configured driver (same string the Dialector is built from). Useful for diagnostics and for tooling that needs to reconnect outside GORM — migration CLIs, ad-hoc shell scripts, etc. Contains credentials in the Postgres/MySQL forms; don't log it in production.

func (*Manager) Driver

func (m *Manager) Driver() Driver

Driver returns the configured driver (useful for dashboards).

func (*Manager) GetCtx added in v0.4.2

func (m *Manager) GetCtx() context.Context

GetCtx returns the manager's internal context. It's canceled by Stop(), so goroutines that should die alongside the manager can bind to it:

go func() {
    <-mgr.GetCtx().Done()
    // manager has stopped; unwind our side here
}()

func (*Manager) GetDB

func (m *Manager) GetDB() *gorm.DB

GetDB returns the current *gorm.DB, or nil if disconnected.

func (*Manager) IsConnected

func (m *Manager) IsConnected() bool

IsConnected returns true if a live connection is currently held.

func (*Manager) Ping

func (m *Manager) Ping(ctx context.Context) error

Ping runs a bounded health check against the current connection.

func (*Manager) Start

func (m *Manager) Start()

Start begins the background maintenance loop (5s health check + reconnect). Idempotent-ish: calling Start twice starts two loops, which is a bug but rarely fatal since they both probe the same state.

func (*Manager) Stop

func (m *Manager) Stop()

Stop cancels the maintenance loop and closes the underlying *sql.DB.

type Option

type Option func(*Manager)

Option tweaks a Manager at construction time.

func WithExecutor

func WithExecutor(e failsafe.Executor[*gorm.DB]) Option

WithExecutor swaps in a custom failsafe executor. Useful if you want a different retry/circuit-breaker profile (the defaults match oats exactly).

func WithLogger

func WithLogger(l *zap.Logger) Option

WithLogger attaches a zap logger. Without one, Manager runs silently.

func WithPool

func WithPool(p PoolConfig) Option

WithPool overrides the default connection-pool sizing.

type PoolConfig

type PoolConfig struct {
	MaxIdle     int
	MaxOpen     int
	ConnMaxLife time.Duration
	ConnMaxIdle time.Duration
}

PoolConfig tunes the underlying *sql.DB pool. Defaults match oats's values for postgres/mysql; sqlite :memory: gets MaxOpen=1 to keep one in-memory database shared across goroutines.

Jump to

Keyboard shortcuts

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