Part 2: Create Tenant Databases
Open src/Api.ts. The Worker creates, lists, and deletes tenant databases while it runs.
Give the Worker the group
Section titled “Give the Worker the group”Effect.gen(function* () { const tenants = yield* Turso.ManageDatabases(Tenants); return { fetch };}).pipe(Effect.provide(Turso.ManageDatabasesHttp))The Worker gets a Turso API token that only works on the Tenants group. It can create and delete databases there, and nowhere else.
Read the template’s name
Section titled “Read the template’s name”Effect.gen(function* () { const tenants = yield* Turso.ManageDatabases(Tenants); const template = yield* Template; const templateName = yield* template.name; return { fetch };})templateName gives you the template’s name inside a request.
Copy the template for a new tenant
Section titled “Copy the template for a new tenant”case "POST": { const database = yield* tenants.create(tenant, { seed: { type: "database", name: yield* templateName }, }); return yield* HttpServerResponse.json({ tenant: database });}POST /tenants/acme creates the acme database as a copy of the template. It has the notes table and no notes.
List tenants
Section titled “List tenants”if (tenant === undefined || tenant === "") { const databases = yield* tenants.list(); const template = yield* templateName; return yield* HttpServerResponse.json({ tenants: databases.map((d) => d.name).filter((name) => name !== template), });}list() returns every database in the group. Leave out the template.
Delete a tenant
Section titled “Delete a tenant”case "DELETE": { yield* tenants.delete(tenant); return yield* HttpServerResponse.json({ deleted: tenant });}This deletes the tenant’s database and all its notes.
Add the Worker to the Stack
Section titled “Add the Worker to the Stack”Effect.gen(function* () { const template = yield* Template; const api = yield* Api; return { url: api.url.as<string>(), template: template.name, };}),In Part 3, you’ll read and write each tenant’s notes.