Reference · Tenants
tenant
tenant() -> str | nullWhat it does
The name of the tenant the code runs for, or null. A request's tenant comes from the
rule tenant_from set (the subdomain, or a function of the request); code run with
with_tenant(name, f) has that name. It works in any function the request runs. Outside
a request, and in a program without tenants, it is null. See Many
tenants.
Examples
tenant_from("example.com") // acme.example.com is tenant "acme"db.open("data/{tenant}.db")route GET / = <h1>Welcome, {tenant()}</h1>fn settings() = cache[tenant()] ?? load_settings()print(tenant())print(with_tenant("acme", () => tenant()))nullacmeEdge cases
- No tenant is null, not the empty string: a request to the bare domain, a job, a
script.
??gives a name to use for those. - Inside
with_tenantit is that name, nested ones innermost first, and the outer value is back afterwards. - A name is lowercase letters, digits,
-and_, at most 63, starting with a letter or a digit, so it is safe in a file name or a database name. - Global variables are shared by every tenant; keep a tenant's data in its database or
in maps keyed by
tenant(). A view that callstenant()is never served from the view cache.
print(tenant() ?? "shared")print(with_tenant("acme", () => [tenant(), with_tenant("beta", () => tenant()), tenant()]))print(tenant())shared["acme", "beta", "acme"]nullErrors
It takes no arguments and never fails; a request for a host that names no tenant
gives null here and an error value from db.* calls, since there is no database to
open for it.
print(tenant() == null)trueChaining
A name for a key or a path:
print(with_tenant("acme", () => `data/${tenant()}.db`))print(with_tenant("acme", () => tenant() | upper))data/acme.dbACMESee also
tenant_from, with_tenant, db.open, db.tenants
Thread 0 posts
On tenant(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo