saas

sql
-- Pattern 1: Shared schema with tenant_id column

CREATE TABLE tenants (
  id SERIAL PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  slug VARCHAR(50) UNIQUE NOT NULL,

Multi-tenancy database patterns and strategies

multi-tenancy saas row-level-security
by Maria Garcia 2 tabs
ruby
Apartment.configure do |config|
  config.excluded_models = %w[Tenant User]
  config.tenant_names = -> { Tenant.pluck(:schema_name) }
  config.use_schemas = true
end

Multi-tenancy with apartment gem

rails multi-tenancy postgresql
by Alex Kumar 3 tabs
python
import threading
from contextlib import contextmanager

_state = threading.local()

Per-Tenant Data Isolation in Django with a Thread-Local Current Tenant Manager

django multi-tenancy orm
by codesnips 4 tabs
php
<?php

namespace App\Models\Scopes;

use App\Support\TenantContext;
use Illuminate\Database\Eloquent\Builder;

Automatic Multi-Tenant Scoping in Laravel with a Global Scope and Auth Context

laravel multi-tenancy eloquent
by codesnips 4 tabs
java
package com.example.demo.multitenancy;

public class TenantContext {
    private static final ThreadLocal<String> CURRENT_TENANT = new ThreadLocal<>();

    public static void setTenantId(String tenantId) {

Multi-tenancy for SaaS applications

java multi-tenancy saas
by David Kumar 4 tabs
typescript
import { Injectable, NestMiddleware, UnauthorizedException } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
import { TenantService } from './tenant.service';

@Injectable()
export class TenantContextMiddleware implements NestMiddleware {

Bind Tenant Context Per Request in NestJS With a Custom @TenantId() Decorator

nestjs multi-tenancy decorators
by codesnips 4 tabs