Database (SQL)
GORM-backed SQL. core.NewDatabase(env) opens a pooled connection; ctx.DB() returns a *gorm.DB already bound to the request context (deadline/cancel/trace). Most code uses the repository on top of this.
Connect & wire
go
db, err := core.NewDatabase(env,
core.WithMaxOpenConns(20),
core.WithMaxIdleConns(5),
core.WithConnMaxLifetime(time.Hour),
)
app, _ := core.NewApp(env,
core.WithSQL("default", db),
core.WithSQL("readonly", replicaDB),
)Supported drivers: postgres, mysql (DB_DRIVER). Configure via a single URI or discrete fields — see env.md:
DB_CONNECTION_STRING=postgres://user:pass@host:5432/db?sslmode=disable
# or
DB_DRIVER=postgres
DB_HOST=... DB_PORT=... DB_USER=... DB_PASSWORD=... DB_NAME=...A
mysql://...URI is auto-converted to the Go DSN gorm expects, withparseTime=True&charset=utf8mb4&loc=UTCdefaults.
Using it
Prefer the repository:
go
user, err := repository.New[User](ctx).Where("id = ?", id).FindOne()Or the raw *gorm.DB (already ctx-bound):
go
var users []User
if err := ctx.DB().Where("status = ?", "active").Find(&users).Error; err != nil {
return ctx.NewError(err, errmsgs.DBError)
}
// named connection
ctx.DBS("readonly").Find(&users)Notes
- Timestamps default to UTC (
NowFunc). - The connection pool lives on the
App;app.Shutdown()closes it. - Every query runs with the request's context, so client cancellation and timeouts propagate to the driver automatically.