Reference · Database
db.tenant_exists
db.tenant_exists(str) -> boolWhat it does
Whether a tenant has a database, when the program's database is one per tenant: the
SQLite file, PostgreSQL schema or MySQL database for that name exists. A tenant's
database is made the first time code runs for it with with_tenant(name, ...), so this
is the check before signing a name up, and the way to tell a real tenant from a
made-up host.
Examples
db.open("data/{tenant}.db")print(db.tenant_exists("acme"))with_tenant("acme", () => db.exec("create table settings (plan text)"))print(db.tenant_exists("acme"), db.tenant_exists("zed"))falsetrue falseroute POST /signup { form: { name: str } } { if db.tenant_exists(name) { reply 409 "That name is taken" } with_tenant(name, () => db.exec("insert into settings (plan) values ('free')")) redirect(`https://${name}.example.com/`)}Edge cases
- A name that cannot be a tenant's (anything but lowercase letters, digits,
-and_, at most 63, starting with a letter or a digit) isfalse, never an error: a request's host may say anything. - It works for any tenant and for none: it looks at the folder, not a tenant's database.
- With a function naming each tenant's database, it is
trueonce that database has been made. - It never makes the database: asking about
zedleavesdb.tenants()as it was.
db.open("data/{tenant}.db")with_tenant("a-b_1", () => db.query("select 1"))print(db.tenant_exists("a-b_1"), db.tenant_exists("Acme"), db.tenant_exists("../a-b_1"), db.tenant_exists(""))print(with_tenant("a-b_1", () => db.tenant_exists("zed")), db.tenants())let registry = db.connect("registry.db")registry.exec("create table tenants (name text, url text)")registry.exec("insert into tenants values ('acme', 'data/acme.db')")db.open((t) => registry.one("select url from tenants where name = ?", t).url)print(db.tenant_exists("acme"))with_tenant("acme", () => db.exec("create table t (a)"))print(db.tenant_exists("acme"), db.tenant_exists("zed"))true false false falsefalse ["a-b_1"]falsetrue falseErrors
A program whose database is not one per tenant gets a runtime error, as does a name that is not a string.
db.open("app.db")print(db.tenant_exists("acme"))line 2: `db.tenant_exists` needs a database per tenant: db.open("data/{tenant}.db")Chaining
db.open("data/{tenant}.db")with_tenant("acme", () => db.query("select 1"))print(["acme", "zed"] | filter(db.tenant_exists))print("acme" | db.tenant_exists)["acme"]trueSee also
db.tenants, with_tenant, tenant, tenant_from, db.open
Thread 0 posts
On db.tenant_exists(): 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