Skip to content

GORM Examples

The official runnable project is ss-keel-examples/examples/08-gorm-postgres.

dbCfg := config.MustLoadConfig[database.Config]()
dbCfg.Logger = log
dbInstance, err := database.New(dbCfg)
if err != nil {
log.Error("failed to connect to database: %v", err)
os.Exit(1)
}
app.RegisterHealthChecker(database.NewHealthChecker(dbInstance))
if err := db.AutoMigrate(&Product{}); err != nil {
log.Error("migration failed: %v", err)
os.Exit(1)
}

The example uses AutoMigrate for local development convenience only. The repo comments explicitly recommend SQL migrations or external migration tooling for production.

httpx.POST("/products", func(c *httpx.Ctx) error {
var req CreateProductRequest
if err := c.ParseBody(&req); err != nil {
return err
}
product := Product{Name: req.Name, Description: req.Description, Price: req.Price, Stock: req.Stock}
if err := db.Create(&product).Error; err != nil {
return core.Internal("could not create product", err)
}
return c.Created(product)
})