All Modules

SQL Tenant

Multi-Tenancy Platform

Supabase-style multi-tenancy for SQL Server and PostgreSQL with Clerk integration. Standardized Row-Level Security patterns that eliminate complexity while ensuring complete data isolation with full certainty.

How It Works

┌─────────────────────────────────────────────────────────────────┐
│                    APPLICATION LAYER                             │
│  ┌─────────────────────────────────────────────────────────────┐│
│  │  Frontend (React/Vue/Next.js)                               ││
│  │         │                                                   ││
│  │         ▼                                                   ││
│  │  ┌─────────────┐                                            ││
│  │  │    Clerk    │ ← Authentication & Tenant Context          ││
│  │  └──────┬──────┘                                            ││
│  └─────────┼───────────────────────────────────────────────────┘│
└────────────┼────────────────────────────────────────────────────┘
             │ JWT with tenant_id claim
             ▼
┌─────────────────────────────────────────────────────────────────┐
│                    SQL TENANT LAYER                              │
│  ┌────────────────────────────────────────────────────────────┐ │
│  │  1. Extract tenant_id from JWT                             │ │
│  │  2. Set session context                                    │ │
│  │  3. RLS policies automatically filter all queries          │ │
│  │  4. Audit all tenant data access                           │ │
│  └────────────────────────────────────────────────────────────┘ │
└───────────────────────────┬─────────────────────────────────────┘
                            │
                            ▼
┌─────────────────────────────────────────────────────────────────┐
│                    DATABASE (SQL Server / PostgreSQL)            │
│  ┌────────────────────────────────────────────────────────────┐ │
│  │  All tables with tenant_id column                          │ │
│  │  RLS policies enforce tenant isolation                     │ │
│  │  Indexes optimized for tenant queries                      │ │
│  └────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘

Automatic RLS Policies

PostgreSQL

-- Enable RLS on table
ALTER TABLE customers
ENABLE ROW LEVEL SECURITY;

-- Create policy
CREATE POLICY tenant_isolation
ON customers
USING (
  tenant_id = current_setting('app.tenant_id')::uuid
);

-- Set tenant context
SET app.tenant_id = 'abc-123-def';

-- All queries auto-filtered!
SELECT * FROM customers;
-- Internally: WHERE tenant_id = 'abc-123-def'

SQL Server (2016+)

-- Security predicate function
CREATE FUNCTION dbo.fn_TenantPredicate
  (@TenantId UNIQUEIDENTIFIER)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS result
WHERE @TenantId = CAST(
  SESSION_CONTEXT(N'tenant_id')
  AS UNIQUEIDENTIFIER
);

-- Security policy
CREATE SECURITY POLICY TenantPolicy
ADD FILTER PREDICATE
  dbo.fn_TenantPredicate(tenant_id)
  ON dbo.Customers
WITH (STATE = ON);

Clerk Integration

Clerk organizations map directly to tenants - authentication and authorization in one

// Generated middleware: tenantContext.ts
import { Clerk } from '@clerk/clerk-sdk-node';

export async function tenantContextMiddleware(req, res, next) {
  const token = req.headers.authorization?.replace('Bearer ', '');

  if (!token) {
    return res.status(401).json({ error: 'Unauthorized' });
  }

  try {
    // Verify JWT and extract tenant
    const session = await clerk.verifyToken(token);
    const tenantId = session.org_id;  // Clerk org = tenant

    if (!tenantId) {
      return res.status(403).json({ error: 'No tenant context' });
    }

    // Set tenant context on database connection
    await req.db.execute(
      `EXEC sp_set_session_context @key = 'tenant_id', @value = @tenantId`,
      { tenantId }
    );

    req.tenantId = tenantId;
    next();
  } catch (error) {
    return res.status(401).json({ error: 'Invalid token' });
  }
}

Isolation Patterns

Shared Database

Recommended

  • • All tenants in one database
  • • RLS enforces isolation
  • • Cost-effective
  • • Easy maintenance

Schema Per Tenant

Better isolation

  • • Separate schemas
  • • Per-tenant optimization
  • • Schema management overhead
  • • Middle ground

Database Per Tenant

Complete isolation

  • • Separate databases
  • • Maximum isolation
  • • Higher cost
  • • Enterprise compliance

Tenant Dashboard

╔══════════════════════════════════════════════════════════════════╗
║                    SQL TENANT DASHBOARD                           ║
╠══════════════════════════════════════════════════════════════════╣
║ TENANT OVERVIEW                                                   ║
║ ─────────────────────────────────────────────────────────────── ║
║ Total Tenants:      247                                           ║
║ Active (30d):       189                                           ║
║ Trial:              45                                            ║
║ Enterprise:         23                                            ║
╠══════════════════════════════════════════════════════════════════╣
║ TOP TENANTS (by data volume)                                      ║
║ ─────────────────────────────────────────────────────────────── ║
║ 1. Acme Corp         12.4 GB    45,000 customers                  ║
║ 2. Globex Inc         8.2 GB    32,000 customers                  ║
║ 3. Initech            5.1 GB    18,000 customers                  ║
╠══════════════════════════════════════════════════════════════════╣
║ ISOLATION STATUS                                                  ║
║ ─────────────────────────────────────────────────────────────── ║
║ ✓ All 15 tables have RLS policies                                ║
║ ✓ All queries filtered by tenant_id                               ║
║ ✓ No cross-tenant access detected                                 ║
╚══════════════════════════════════════════════════════════════════╝

Build Multi-Tenant Apps

Enterprise multi-tenancy with complete data isolation and zero complexity.

No credit card required • Free for individual developers