Skip to main content

Tenant

Struct Tenant 

Source
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: Org

Implementations§

Source§

impl<DB: Database> Tenant<DB>

Source

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.

Source

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.

Source

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.

Source

pub 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

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>

Source

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>

Source

pub fn for_test_mysql(org: Org, pool: MySqlPool) -> Self

For tests only. As Tenant::for_test_sqlite, on MySQL.

Source§

impl Tenant<Postgres>

Source

pub async fn for_test_pools( org: Org, pools: &TenantPools<Postgres>, ) -> Result<Self, TenancyError>

For tests only. org’s Tenant from pools, built as the extractor does, so its connection and pool() cannot disagree.

ⓘ
let mut t = Tenant::for_test_pools(org, &pools).await?;
my_function_under_test(&mut t).await?;
§Errors

The pool lookup or acquire failing.

Source§

impl Tenant<Postgres>

Source

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.
Source§

fn from(tenant: &'a Tenant<DB>) -> Self

Converts to this type from the input type.
Source§

impl<DB: Database> From<&Tenant<DB>> for LinkScope

Available on crate feature tenancy only.
Source§

fn from(tenant: &Tenant<DB>) -> Self

Converts to this type from the input type.
Source§

impl<DB: Database> From<Tenant<DB>> for TenantScope

Source§

fn from(t: Tenant<DB>) -> Self

Converts to this type from the input type.
Source§

impl<S> FromRequestParts<S> for Tenant<Postgres>
where S: Send + Sync,

Available on crate feature postgres only.
Source§

type Rejection = TenantRejection

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
Source§

async fn from_request_parts( parts: &mut Parts, _state: &S, ) -> Result<Self, Self::Rejection>

Perform the extraction.
Source§

impl<S> FromRequestParts<S> for Tenant<Sqlite>
where S: Send + Sync,

Available on crate feature sqlite only.
Source§

type Rejection = TenantRejection

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
Source§

async fn from_request_parts( parts: &mut Parts, _state: &S, ) -> Result<Self, Self::Rejection>

Perform the extraction.
Source§

impl<S> FromRequestParts<S> for Tenant<MySql>
where S: Send + Sync,

Available on crate feature mysql only.
Source§

type Rejection = TenantRejection

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
Source§

async fn from_request_parts( parts: &mut Parts, _state: &S, ) -> Result<Self, Self::Rejection>

Perform the extraction.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S, T> FromRequest<S, ViaParts> for T
where S: Send + Sync, T: FromRequestParts<S>,

Source§

type Rejection = <T as FromRequestParts<S>>::Rejection

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
Source§

fn from_request( req: Request<Body>, state: &S, ) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>

Perform the extraction.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more