Reference · Tenants

tenant

signature
tenant() -> str | null

What 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

in a program
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()))
output
nullacme

Edge cases

print(tenant() ?? "shared")print(with_tenant("acme", () => [tenant(), with_tenant("beta", () => tenant()), tenant()]))print(tenant())
output
shared["acme", "beta", "acme"]null

Errors

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)
output
true

Chaining

A name for a key or a path:

print(with_tenant("acme", () => `data/${tenant()}.db`))print(with_tenant("acme", () => tenant() | upper))
output
data/acme.dbACME

See also

tenant_from, with_tenant, db.open, db.tenants

Thread
7Useful? Like it, or add to its thread below.

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