Help us shape the future of Go! Take the 2026 Go Developer Survey

ormweb

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package ormweb serves an orm.Databases in a github.com/vuka-lang/web app: the databases open when the app starts (migrated in development) and close gracefully when it stops; *orm.Databases and the connection handles (@orm.Database) are dependencies; each request's context carries the databases and a read-your-writes Scope; GET /healthz reports every database's state (and, in development, its pool and errors); a model or handle naming a database the app doesn't declare fails the app's Build. In development, the app's toolbar lists each request's statements in its SQL panel.

app := web.New(
	ormweb.Databases(
		orm.Declare("main", os.Getenv("DATABASE_URL"), orm.AsDefault()),
		orm.Declare("analytics", os.Getenv("ANALYTICS_URL"), orm.ReadOnly()),
	).MigrateOnStart(true),
	web.Use(ormweb.UsingFrom(tenantOf)), // a tenant's database per request
)
app.Run("")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DetailedHealthHandler added in v0.4.0

func DetailedHealthHandler(s *orm.Databases) http.Handler

DetailedHealthHandler answers with s's whole Report: the summary, and each database's pool, breaker, last error (the driver's text) and replicas (their redacted URLs). Serve it to operators only.

func HealthHandler

func HealthHandler(s *orm.Databases) http.Handler

HealthHandler answers with s's Summary as JSON: 200, or 503 when the default database is down. It says nothing of errors, pools or replicas, so it can be public.

func Middleware

func Middleware(s *orm.Databases) web.Middleware

Middleware puts s and a read-your-writes Scope on each request's context.

func UsingFrom

func UsingFrom(name func(*http.Request) string) web.Middleware

UsingFrom sends each request's queries to the database name gives for it (a tenant's, by host or header); "" leaves them routed as they are.

app.Use(ormweb.UsingFrom(func(r *http.Request) string { return "tenant_" + r.Header.Get("X-Tenant") }))

Types

type DBState added in v0.4.0

type DBState struct {
	Name    string    `json:"name"`
	Default bool      `json:"default"`
	State   orm.State `json:"state"`
}

DBState is a database's state, as Summary reports it.

type Plugin

type Plugin struct {
	web.Option
	// contains filtered or unexported fields
}

Plugin is the web.Option Databases and Set return; its methods tune it before web.New applies it.

func Databases

func Databases(opts ...orm.OpenOption) *Plugin

Databases serves the databases opts declare (orm.Declare), with their routers and settings (orm.Routers, orm.Logger, orm.OnOpen, …); with none declared, the app's start reads DATABASE_URL and DATABASE_URL_<NAME>.

func Set

func Set(s *orm.Databases, opts ...orm.OpenOption) *Plugin

Set serves a Databases made elsewhere (orm.NewDatabases), opts applied to it, opened by the app's start.

func (*Plugin) Health

func (p *Plugin) Health(path string) *Plugin

Health serves the databases' health at path (GET /healthz by default; "" serves none).

func (*Plugin) HealthDetails added in v0.4.0

func (p *Plugin) HealthDetails(on bool) *Plugin

HealthDetails has the health route answer each database's full Report (pool statistics, breaker, last error, replicas) rather than its state alone. By default it does only in development (web.IsDev, by the real environment): a driver's error text and replica addresses are for operators, not whoever reaches the route.

func (*Plugin) MigrateOnStart

func (p *Plugin) MigrateOnStart(on bool) *Plugin

MigrateOnStart applies pending migrations once the databases open (orm.Migrate). By default it does only in development (web.IsDev: VUKA_ENV=development in the program's real environment, never an env file). Production migrates when this says so, or with the orm migrate command.

type Report

type Report struct {
	Status    string        `json:"status"`
	Databases []orm.DBStats `json:"databases"`
}

Report is what the health endpoint answers: Status "ok", "degraded" (a database or replica isn't answering) or "down" (the default database isn't), and each database's stats.

func Check

func Check(s *orm.Databases) Report

Check is s's health now.

type Summary added in v0.4.0

type Summary struct {
	Status    string    `json:"status"`
	Databases []DBState `json:"databases"`
}

Summary is a Report without the details: the status, and each database's state.

func Summarize added in v0.4.0

func Summarize(rep Report) Summary

Summarize is rep without its details.

Jump to

Keyboard shortcuts

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