pub struct TenantContext { /* private fields */ }Expand description
Tenant context for row-level security and data isolation.
Represents a single tenant in a multi-tenant system. All queries executed with this context will be filtered to only include data belonging to this tenant.
Implementations§
Source§impl TenantContext
impl TenantContext
Sourcepub fn created_at_iso8601(&self) -> Option<&str>
pub fn created_at_iso8601(&self) -> Option<&str>
Get the creation timestamp in ISO 8601 format.
Sourcepub fn set_metadata(&mut self, key: impl Into<String>, value: impl Into<String>)
pub fn set_metadata(&mut self, key: impl Into<String>, value: impl Into<String>)
Set metadata key-value pair for the tenant.
Sourcepub fn get_metadata(&self, key: &str) -> Option<&str>
pub fn get_metadata(&self, key: &str) -> Option<&str>
Get metadata value by key.
Sourcepub fn from_jwt_claims(claims: &JsonValue) -> Result<Self, String>
pub fn from_jwt_claims(claims: &JsonValue) -> Result<Self, String>
Create a TenantContext from JWT claims.
Extracts the tenant_id from JWT claims and creates a new TenantContext.
§Arguments
claims- JWT claims as JSON object
§Errors
Returns an error if:
tenant_idclaim is missingtenant_idis not a string
§Example
ⓘ
use serde_json::json;
use fraiseql_core::tenancy::TenantContext;
let claims = json!({
"sub": "user123",
"tenant_id": "acme-corp",
"email": "alice@acme.com"
});
let tenant = TenantContext::from_jwt_claims(&claims)?;
assert_eq!(tenant.id(), "acme-corp");Sourcepub fn where_clause(&self) -> String
pub fn where_clause(&self) -> String
Sourcepub fn where_clause_postgresql(&self, param_index: usize) -> String
pub fn where_clause_postgresql(&self, param_index: usize) -> String
Generate a parameterized WHERE clause for PostgreSQL.
For use with parameterized queries to prevent SQL injection.
§Arguments
param_index- Parameter placeholder index (1-based for PostgreSQL)
§Example
ⓘ
let tenant = TenantContext::new("acme-corp");
let clause = tenant.where_clause_postgresql(1); // "tenant_id = $1"Sourcepub fn where_clause_parameterized(&self) -> String
pub fn where_clause_parameterized(&self) -> String
Trait Implementations§
Source§impl Clone for TenantContext
impl Clone for TenantContext
Source§fn clone(&self) -> TenantContext
fn clone(&self) -> TenantContext
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for TenantContext
impl RefUnwindSafe for TenantContext
impl Send for TenantContext
impl Sync for TenantContext
impl Unpin for TenantContext
impl UnsafeUnpin for TenantContext
impl UnwindSafe for TenantContext
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
Mutably borrows from an owned value. Read more