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.
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 ¶
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 ¶
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 ¶
Health serves the databases' health at path (GET /healthz by default; "" serves none).
func (*Plugin) MigrateOnStart ¶
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).