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.
type Driver ¶
type Driver string
Driver picks the backing dialect. Each value has a corresponding Dialector that Config.Dialector() returns.
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 ¶
NewManager builds a Manager without connecting. Call Open or Start next.
func Open ¶
Open constructs a Manager and establishes the initial connection synchronously. Call Start() afterwards to enable the background reconnect + health-check loop.
func (*Manager) IsConnected ¶
IsConnected returns true if a live connection is currently held.
type Option ¶
type Option func(*Manager)
Option tweaks a Manager at construction time.
func WithExecutor ¶
WithExecutor swaps in a custom failsafe executor. Useful if you want a different retry/circuit-breaker profile (the defaults match oats exactly).
func WithLogger ¶
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.