Several instances
Two databases of one kind in one application, told apart by a qualifier type.
An application can read from a primary and a replica, or write to two databases of one engine. Each is a store module imported once, from its own configuration section, and made distinct with .keyed::<Q>(), where Q is a type you declare.
This is examples/36-db-two-instances:
use ulo_db_sqlx::{Postgres, SqlxModule};
use ulo_health::HealthModule;
use ulo_health_http::HealthHttp;
/// The two qualifiers that tell the pools apart.
pub struct Primary;
pub struct Replica;
fn register(&self, m: &mut ModuleDef<'_>) {
// One store module, read from two sections and keyed twice: `Dep<PgPool, Primary>` and
// `Dep<PgPool, Replica>`, and two checks. Only the primary migrates; a replica's schema
// arrives by replication.
m.import(SqlxModule::<Postgres>::from_config(&self.config, "primary").migrate(sqlx::migrate!("./migrations")).keyed::<Primary>());
m.import(SqlxModule::<Postgres>::from_config(&self.config, "replica").keyed::<Replica>());
m.import(HealthModule::new());
m.import(HealthHttp::new().detail(true));
...
}keyed::<Q>() requalifies the module's exports as T @ Q. A provider asks for the one it wants:
use ulo::{Dep, injectable};
use ulo_db_sqlx::sqlx::PgPool;
#[injectable]
pub struct PrimaryNotes {
pool: Dep<PgPool, Primary>,
}
#[injectable]
pub struct ReplicaNotes {
pool: Dep<PgPool, Replica>,
}The configuration names the sections, here from the environment:
APP_PRIMARY__URL=postgres://postgres:pw@127.0.0.1/postgres APP_PRIMARY__MAX_CONNECTIONS=5 \
APP_REPLICA__URL=postgres://postgres:pw@127.0.0.1/replica PORT=7000 cargo run -p ex-36-db-two-instancesWhat is kept apart
Each instance has its own pool and its own health check. The checks are named with the section and the qualifier, postgres@primary @ Primary and postgres@replica @ Replica, so a replica outage turns /health/ready to 503 with the replica's check failing and the primary's passing. A module built with from_options has an empty section path and names its check postgres @ Primary.
A wiring report prints the module as StoreModule<Postgres> @ Primary. Two qualifiers whose short names are equal, a::Primary and b::Primary, print in full and stay apart.
wire() reports the configuration problems of both sections in one report. A missing URL names the key of each.
The qualifier is read from the nearest keyed module on the import path: the store module's own keyed::<Q>(), or an application module's that imports it. A store module reached by several keyed importers is one instance and reads the key of the path that reached it first.
A bare SqlxModule::<Postgres>::from_config(..) beside a keyed one is two modules with two pools and two checks.
Different stores
Instances of different stores need no qualifier, since the client types differ: Dep<PgPool> and Dep<ConnectionManager> are different bindings.