Multi-Tenant Database
Multi-tenancy implementation with row-level data isolation, tenant context management, scoped queries, Prisma extensions, and PostgreSQL Row-Level Security.
What's Included#
- Prisma schema with Organization, User, and Project models with tenant foreign keys
- Tenant context using AsyncLocalStorage for automatic scoping
- Tenant-scoped query helpers that enforce organizationId on all operations
- Prisma client extension for automatic tenant filtering on all models
- PostgreSQL Row-Level Security (RLS) policies for database-level isolation
- Middleware integration for extracting tenant info from session
- Organization switching with cookie-based active organization tracking
- Tenant-aware API route example
Usage#
Via CLI#
Loading code block...
Via AI Assistant#
Ask your AI assistant:
- "Use the multi-tenant database pattern from Bootspring"
- "Apply the Bootspring multi-tenant pattern to my project"
Key Considerations#
- Always scope queries to the current tenant; never allow cross-tenant data leakage
- Use database-level Row-Level Security as an additional safety net beyond application-level checks
- Validate tenant access on every request, not just at the middleware level
- Add database indexes on organizationId columns for query performance
- Monitor per-tenant resource usage and implement quotas where appropriate
Related Patterns#
- Prisma - Prisma setup and basics
- RBAC - Role-based access control
- Session Management - Session handling