ormweb

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: MIT Imports: 8 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; 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 HealthHandler

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

HealthHandler answers with s's Report as JSON: 200, or 503 when the default database is down.

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 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) MigrateOnStart

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

MigrateOnStart applies pending migrations once the databases open (orm.Migrate); by default when VUKA_ENV=development as they open (so a .env file the app loads counts).

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.

Jump to

Keyboard shortcuts

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