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).unwrap();
assert_eq!(tenant.id(), "acme-corp");§Errors
Returns a String error if the tenant_id claim is missing or not a string.
Sourcepub fn where_clause(&self) -> String
pub fn where_clause(&self) -> String
Generate a WHERE clause for tenant filtering.
Returns a WHERE clause that restricts data to this tenant. Can be combined with existing WHERE clauses using AND.
§Panics
Panics if the tenant ID contains characters outside the safe set
([A-Za-z0-9._-]). Tenant IDs are validated at context creation
so this should never trigger in practice.
§Security
Prefer where_clause_postgresql or where_clause_parameterized
for production query execution. This method embeds the tenant ID
directly into SQL and is only safe because the ID is strictly validated.
§Example
let tenant = TenantContext::new("acme-corp");
let clause = tenant.where_clause(); // "tenant_id = 'acme-corp'"
assert_eq!(clause, "tenant_id = 'acme-corp'");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"
assert_eq!(clause, "tenant_id = $1");Sourcepub fn where_clause_parameterized(&self) -> String
pub fn where_clause_parameterized(&self) -> String
Generate a parameterized WHERE clause for MySQL/SQLite.
For use with parameterized queries to prevent SQL injection.
§Example
let tenant = TenantContext::new("acme-corp");
let clause = tenant.where_clause_parameterized(); // "tenant_id = ?"
assert_eq!(clause, "tenant_id = ?");Trait Implementations§
Source§impl Clone for TenantContext
impl Clone for TenantContext
Source§fn clone(&self) -> TenantContext
fn clone(&self) -> TenantContext
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more