AgentStack
Browse Sign in
Browse Why AgentStack Sell Docs
Sign in
SKILL verified MIT Self-run

Multi Tenancy

skill-camilooscargbaptista-cto-toolkit-multi-tenancy · by camilooscargbaptista

Multi-tenant architecture patterns: row-level, schema-level, database-level isolation and tenant routing

No reviews yet
0 installs
18 views
0.0% view→install

Install

$ agentstack add skill-camilooscargbaptista-cto-toolkit-multi-tenancy

✓ scanned · ✓ verified, works with Claude Code, Cursor, and more.

Security review

✓ Passed

No issues found. Passed automated security review. · v0.1.0 How review works →

  • Prompt-injection patterns
  • Secret / credential exfiltration
  • Dangerous shell & filesystem operations
  • Untrusted network calls
  • Known-malicious package signatures

What it can access

  • Network access No
  • Filesystem access No
  • Shell / process execution No
  • Environment & secrets No
  • Dynamic code execution No

From automated source analysis of v0.1.0. “Used” means the capability is present in the source — more access means more to trust, not that it’s unsafe.

View the full security report →

Verified badge

Passed review? Show it. Paste this badge into your README, it links to the public security report.

AgentStack Verified badge Links to your public security report.
[![AgentStack Verified](https://agentstack.voostack.com/badges/verified.svg)](https://agentstack.voostack.com/security/report/skill-camilooscargbaptista-cto-toolkit-multi-tenancy)

Reliability & compatibility

Security review passed
0 installs to date
no reviews yet
4mo ago

Declared compatibility

Claude CodeClaude Desktop

Compatibility is declared by the source manifest. End-to-end runtime verification is coming, see below.

Preview Execution monitoring

We're building live execution health for every listing: tool-call success rate, median latency, uptime, and last-checked timestamps, measured, not self-reported. It isn't live yet, so we don't show numbers we can't stand behind.

How agent discovery & health will work →
Are you the author of Multi Tenancy? Claim this listing to set pricing, connect Stripe payouts, and keep 70% of every sale.
Sign up to claim

About

Multi-Tenancy Patterns

When to Use

  • Building SaaS that serves multiple organizations/clients
  • Designing data isolation between tenants (companies, stations, fleets)
  • Choosing the right isolation level for your compliance needs

Isolation Models

1. Row-Level (Shared Everything)

┌──────────────────────────────┐
│         Single Database       │
│  ┌────────────────────────┐  │
│  │     users table         │  │
│  │  tenant_id │ name       │  │
│  │  ──────────┼──────────  │  │
│  │  company_a │ Alice      │  │
│  │  company_b │ Bob        │  │
│  └────────────────────────┘  │
└──────────────────────────────┘

Pros: Simple, cheap, easy to maintain
Cons: Risk of data leakage, shared resources
Best for: Small/medium SaaS, cost-sensitive

Implementation — TypeORM Global Scope:

// Middleware injects tenant
@Injectable()
export class TenantMiddleware implements NestMiddleware {
  use(req: AuthRequest, res: Response, next: NextFunction) {
    req.tenantId = req.user?.companyId;
    next();
  }
}

// Repository automatically filters by tenant
@Injectable()
export class TenantAwareRepository {
  constructor(private repo: Repository) {}

  findAll(tenantId: string): Promise {
    return this.repo.find({ where: { tenant_id: tenantId } as any });
  }

  // CRITICAL: NEVER allow findAll without tenantId
}

// Global subscriber (safety net)
@EventSubscriber()
export class TenantSubscriber implements EntitySubscriberInterface {
  afterLoad(entity: any) {
    // Verify tenant match on every load (paranoia mode)
  }
  
  beforeInsert(event: InsertEvent) {
    // Auto-inject tenant_id
    if (event.entity && !event.entity.tenant_id) {
      event.entity.tenant_id = getCurrentTenantId();
    }
  }
}

2. Schema-Level (Shared Database, Separate Schemas)

┌──────────────────────────────┐
│         Single Database       │
│  ┌──────────┐ ┌──────────┐  │
│  │ schema_a  │ │ schema_b  │  │
│  │  users    │ │  users    │  │
│  │  orders   │ │  orders   │  │
│  └──────────┘ └──────────┘  │
└──────────────────────────────┘

Pros: Good isolation, shared infra cost
Cons: Schema migration complexity, connection pooling
Best for: Medium SaaS, regulated industries

3. Database-Level (Full Isolation)

┌──────────┐  ┌──────────┐  ┌──────────┐
│  DB_A     │  │  DB_B     │  │  DB_C     │
│  users    │  │  users    │  │  users    │
│  orders   │  │  orders   │  │  orders   │
└──────────┘  └──────────┘  └──────────┘

Pros: Maximum isolation, per-tenant backup/restore
Cons: Expensive, complex management
Best for: Enterprise, healthcare, financial

Tenant Routing

// Router that selects connection based on tenant
@Injectable()
export class TenantConnectionManager {
  private connections = new Map();

  async getConnection(tenantId: string): Promise {
    if (this.connections.has(tenantId)) {
      return this.connections.get(tenantId);
    }

    const config = await this.configService.getTenantDbConfig(tenantId);
    const connection = await createConnection({
      name: tenantId,
      ...config,
    });

    this.connections.set(tenantId, connection);
    return connection;
  }
}

Data Isolation Checklist

  • [ ] Every query filters by tenant_id (row-level)
  • [ ] No global admin endpoints return cross-tenant data without authorization
  • [ ] Indexes include tenant_id as first column
  • [ ] Foreign keys respect tenant boundaries
  • [ ] Bulk operations scoped to single tenant
  • [ ] Logging includes tenant context
  • [ ] Testing includes cross-tenant access attempts
  • [ ] Backup/restore can be done per-tenant

Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

Install and usage instructions live in the source repository linked above.

Reviews

No reviews yet, be the first.

Versions

  • v0.1.0 Imported from the upstream source.