Database routing
What it does
TenantAdapter.modelConstructorClient() is called by Lucid every time a TenantBaseModel query starts. The adapter:
- Reads the active tenant via
tenancy.currentId()(orHttpContext.tenantfor HTTP-driven flows). - Asks the active
IsolationDriverfor the connection name:tenant_<uuid>forschema-pg, the per-tenant database name fordatabase-pg, the shared connection plus atenant_idfilter forrowscope-pg. - Returns the connection so the query routes there.
Configuration
Configured indirectly through isolation.driver. See Data isolation.
Why it isn't a bootstrapper
Bootstrappers run on the enter / leave cycle of a tenant context. Database routing happens per query, not per context. Adapter calls are synchronous, frequent, and have to work even for code paths that never call tenancy.run() (controller calls during HTTP requests rely on HttpContext.tenant, not the bootstrapper registry).
Folding database routing into the bootstrapper registry would add overhead to every query and make the dependency direction circular, since bootstrappers depend on the adapter for their own state.
Error mode
If no driver matches the configured isolation.driver, the adapter throws on the first query with the active driver name in the message. The request fails with a 5xx. This is louder than silently falling back to a default and worth the noise.