API reference — Tenancy

Nuvora.Nexus.Relay.Tenancy.EfCore

EF Core / PostgreSQL tenant storage strategies (shared-table RLS) for Nuvora Nexus Relay

dotnet add package Nuvora.Nexus.Relay.Tenancy.EfCore

Nuvora.Nexus.Relay.Tenancy.EfCore

ITenantStorageStrategy

public interface ITenantStorageStrategy

Scopes a unit of work to the ambient tenant for a given TenantStorageMode. Call ITenantStorageStrategy.ApplyAsync once a transaction/connection is open (its effect — e.g. a SET LOCAL — lasts for that transaction), before the work runs.

Properties

TenantStorageMode Mode { get; }

The isolation mode this strategy implements.

Methods

Task ApplyAsync(DbContext context, TenantContext tenant, CancellationToken cancellationToken = default(CancellationToken))

Applies tenant scoping to the open context for tenant.

ServiceCollectionExtensions

public static class ServiceCollectionExtensions

Registration helpers for the EF Core tenant storage strategies.

Methods

static IServiceCollection AddRelayTenancyEfCore(this IServiceCollection services, TenantStorageMode mode = SharedTable)

Registers the ITenantStorageStrategy for mode. Apply the row-level-security migration 0009_tenancy_rls.sql for TenantStorageMode.SharedTable, and run the background processors under a BYPASSRLS role so they drain every tenant.

SharedTableTenantStorageStrategy

public sealed class SharedTableTenantStorageStrategy : ITenantStorageStrategy

Shared-table isolation: publishes the ambient tenant to Postgres as the app.current_tenant session variable (SET LOCAL), so the row-level-security policies from 0009_tenancy_rls.sql limit the transaction to its own tenant’s rows plus global rows. Apply it at the start of each unit of work (the SET LOCAL lasts only for the current transaction).

Properties

TenantStorageMode Mode { get; }

The isolation mode this strategy implements.

Methods

Task ApplyAsync(DbContext context, TenantContext tenant, CancellationToken cancellationToken = default(CancellationToken))

Applies tenant scoping to the open context for tenant.

TenantRls

public static class TenantRls

Helpers for the shared-table row-level-security scheme. The ambient tenant is published to Postgres as a per-transaction session variable (TenantRls.SessionVariable); the RLS policies in 0009_tenancy_rls.sql read it via relay_current_tenant() to limit each session to its own tenant’s rows plus global (tenant_id IS NULL) rows.

Methods

static string BuildSetLocalSql(Guid? tenantId)

Builds the SET LOCAL statement that scopes the current transaction to tenantId (or to the global-only view when null). SET LOCAL does not accept bind parameters, so the value is inlined — safe because a Guid renders only as hex and dashes.

Fields

const string SessionVariable = "app.current_tenant"

The Postgres session variable that carries the ambient tenant id.

TenantRlsTransactionHook

public sealed class TenantRlsTransactionHook : IRelayTenantTransactionHook

Bridges the unit of work’s IRelayTenantTransactionHook to the configured ITenantStorageStrategy: as each command transaction opens, it scopes the connection to the ambient tenant (for shared-table, a SET LOCAL app.current_tenant that RLS reads).

Constructors

TenantRlsTransactionHook(IRelayTenantAccessor tenant, ITenantStorageStrategy strategy)

Methods

Task OnTransactionBeganAsync(DbContext context, CancellationToken cancellationToken = default(CancellationToken))

Called once per command transaction, just after it begins, before any work runs.

TenantStorageMode

public enum TenantStorageMode

How tenant data is physically isolated. Ordered by increasing isolation (and operational cost): one shared schema with a tenant_id column, a schema per tenant, or a database per tenant.

Values

  • SharedTable — All tenants share the relay tables; rows carry a tenant_id and isolation is enforced by Postgres row-level security keyed off the app.current_tenant session variable (migration 0009_tenancy_rls.sql).
  • SchemaPerTenant — One Postgres schema per tenant, selected per unit of work via search_path.
  • DatabasePerTenant — One database per tenant; the connection is routed per tenant and processors fan out.