Skip to main content

DatabaseRouter

Trait DatabaseRouter 

Source
pub trait DatabaseRouter: Send + Sync {
    // Provided methods
    fn db_for_read(&self, model: &ModelMeta, ctx: &RouteContext) -> Alias { ... }
    fn db_for_write(&self, model: &ModelMeta, ctx: &RouteContext) -> Alias { ... }
    fn allow_relation(&self, a: &ModelMeta, b: &ModelMeta) -> bool { ... }
    fn allow_migrate(&self, alias: &str, model: &ModelMeta) -> bool { ... }
    fn schema_for(&self, ctx: &RouteContext) -> Option<Schema> { ... }
    fn schema_for_table(
        &self,
        ctx: &RouteContext,
        table: &str,
    ) -> Option<Schema> { ... }
}
Expand description

Swappable routing policy. Every decision umbral makes about which database/relation/migration target, plus the optional per-request schema, flows through this trait. The default methods reproduce today’s behavior; install a custom impl via App::builder().router(MyRouter).

Provided Methods§

Source

fn db_for_read(&self, model: &ModelMeta, ctx: &RouteContext) -> Alias

Alias of the database to read model from for this request.

Source

fn db_for_write(&self, model: &ModelMeta, ctx: &RouteContext) -> Alias

Alias of the database to write model to for this request.

Source

fn allow_relation(&self, a: &ModelMeta, b: &ModelMeta) -> bool

May a relation (FK) span these two models? Default: same alias only (the #22 cross-DB FK guard).

Source

fn allow_migrate(&self, alias: &str, model: &ModelMeta) -> bool

Should model be migrated on database alias? Default: yes when alias is the model’s assigned alias.

Source

fn schema_for(&self, ctx: &RouteContext) -> Option<Schema>

The Postgres schema to scope this request’s queries to. Default: None (no qualification — today’s behavior). Some(schema) makes the SQL builder schema-qualify table references.

Source

fn schema_for_table(&self, ctx: &RouteContext, table: &str) -> Option<Schema>

The Postgres schema to scope queries against a specific table to, for this request. Default: delegates to schema_for (the per-request schema, table-agnostic). Override to vary the schema per table — the seam schema-per-tenant needs so a SHARED_APPS table (the Tenant registry, auth, etc.) stays in public (None) while a tenant-owned table routes to the active tenant’s schema. table is the bare SQL table name (ModelMeta::table).

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§