pub struct Tenant<DB: Database = DefaultTenantDb> {
pub org: Org,
/* private fields */
}Expand description
Finds the request’s tenant and gives the handler a connection
scoped to it. DB defaults to Postgres, so fn handler(t: Tenant)
means Tenant<sqlx::Postgres>. Borrow the connection with
Tenant::conn for ORM calls.
pub async fn my_handler(mut t: Tenant) -> Result<Json<Vec<Post>>, StatusCode> {
let posts = Post::objects().fetch_on(t.conn()).await?;
Ok(Json(posts))
}Fields§
§org: OrgImplementations§
Source§impl<DB: Database> Tenant<DB>
impl<DB: Database> Tenant<DB>
Sourcepub async fn pool_conn(
&mut self,
) -> Result<&mut PoolConnection<DB>, TenancyError>
pub async fn pool_conn( &mut self, ) -> Result<&mut PoolConnection<DB>, TenancyError>
Borrow the tenant’s connection, taking one from the pool on
first use in database mode. Use it with the sqlx query macros
when the code must work on any backend. PG-only code can use
Tenant::conn, which derefs to &mut PgConnection and so
works with the _on helpers directly.
§Errors
The pool acquire can fail when the connection was deferred.
Sourcepub async fn into_conn(self) -> Result<TenantConn<DB>, TenancyError>
pub async fn into_conn(self) -> Result<TenantConn<DB>, TenancyError>
Take the connection out, so the pool gets it back when it is dropped. Use it in a handler that is done with the database but still has slow work to do. Takes a connection first if the extractor deferred one.
§Errors
The pool acquire can fail when the connection was deferred.
Sourcepub fn pool(&self) -> &Pool
pub fn pool(&self) -> &Pool
Borrow the tenant’s crate::sql::Pool. Use it for ORM calls
such as fetch or save_pool, which work the same on every
backend.
The pool is tenant-scoped in both storage modes. In schema
mode TenantPools::scoped_pool puts search_path in the
connect options of a dedicated pool, so it never borrows from
the shared registry pool and cannot pick up another tenant’s
session state. Use Tenant::conn instead when you want the
request’s one pinned connection. A handler that only uses the
pool should take TenantScope, which holds no connection.
Cost in schema mode: one pool build on the first miss,
then free — until the cache fills. scoped_pool caches per
tenant slug up to max_cached_scoped_pools, 64 by default.
Past that it warns and returns the new pool without caching
it, so every tenant outside the cache rebuilds a pool on every
request. Raise the cap if you have many tenants. Database mode
reuses the tenant’s own pool throughout.
Sourcepub fn for_test(org: Org, conn: TenantConn<DB>, pool: Pool) -> Self
👎Deprecated since 0.61.6: conn and pool can name different tenants; use Tenant::for_test_pools, Tenant::for_test_sqlite or Tenant::for_test_mysql
pub fn for_test(org: Org, conn: TenantConn<DB>, pool: Pool) -> Self
conn and pool can name different tenants; use Tenant::for_test_pools, Tenant::for_test_sqlite or Tenant::for_test_mysql
For tests only. Build a Tenant from an Org and a
connection you already hold, skipping the extractor. It is
behind the test_utils feature so a production build cannot
reach it.
Nothing checks that conn and pool are the same tenant’s:
prefer for_test_pools, for_test_sqlite or for_test_mysql.
Source§impl Tenant<Sqlite>
impl Tenant<Sqlite>
Sourcepub fn for_test_sqlite(org: Org, pool: SqlitePool) -> Self
pub fn for_test_sqlite(org: Org, pool: SqlitePool) -> Self
For tests only. org’s Tenant over its own pool, built
as the extractor does, so its connection and pool() agree.
Source§impl Tenant<MySql>
impl Tenant<MySql>
Sourcepub fn for_test_mysql(org: Org, pool: MySqlPool) -> Self
pub fn for_test_mysql(org: Org, pool: MySqlPool) -> Self
For tests only. As Tenant::for_test_sqlite, on MySQL.
Source§impl Tenant<Postgres>
impl Tenant<Postgres>
Sourcepub async fn for_test_pools(
org: Org,
pools: &TenantPools<Postgres>,
) -> Result<Self, TenancyError>
pub async fn for_test_pools( org: Org, pools: &TenantPools<Postgres>, ) -> Result<Self, TenancyError>
Source§impl Tenant<Postgres>
impl Tenant<Postgres>
Sourcepub fn conn(&mut self) -> &mut PgConnection
pub fn conn(&mut self) -> &mut PgConnection
Borrow the connection as &mut PgConnection, the type sqlx
and the fetch_on / get_on helpers want. Postgres only; on
other backends use Tenant::pool_conn.
Trait Implementations§
Source§impl<'a, DB: Database> From<&'a Tenant<DB>> for LinkTarget<'a>
Available on crate features passwords and tenancy only.
impl<'a, DB: Database> From<&'a Tenant<DB>> for LinkTarget<'a>
passwords and tenancy only.Source§impl<S> FromRequestParts<S> for Tenant<Postgres>
Available on crate feature postgres only.
impl<S> FromRequestParts<S> for Tenant<Postgres>
postgres only.Source§impl<S> FromRequestParts<S> for Tenant<Sqlite>
Available on crate feature sqlite only.
impl<S> FromRequestParts<S> for Tenant<Sqlite>
sqlite only.Source§impl<S> FromRequestParts<S> for Tenant<MySql>
Available on crate feature mysql only.
impl<S> FromRequestParts<S> for Tenant<MySql>
mysql only.Auto Trait Implementations§
impl<DB = Postgres> !RefUnwindSafe for Tenant<DB>
impl<DB = Postgres> !UnwindSafe for Tenant<DB>
impl<DB> Freeze for Tenant<DB>where
TenantConnCell<DB>: Freeze,
impl<DB> Send for Tenant<DB>where
TenantConnCell<DB>: Send,
impl<DB> Sync for Tenant<DB>where
TenantConnCell<DB>: Sync,
impl<DB> Unpin for Tenant<DB>where
TenantConnCell<DB>: Unpin,
impl<DB> UnsafeUnpin for Tenant<DB>where
TenantConnCell<DB>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<S, T> FromRequest<S, ViaParts> for T
impl<S, T> FromRequest<S, ViaParts> for T
Source§type Rejection = <T as FromRequestParts<S>>::Rejection
type Rejection = <T as FromRequestParts<S>>::Rejection
Source§fn from_request(
req: Request<Body>,
state: &S,
) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>
fn from_request( req: Request<Body>, state: &S, ) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more