
Scaling RBAC to Multi-Tenancy: Open-sourced architectural boilerplate for NestJS and PostgreSQL (MT-URBAC)
Hey everyone,
After standardizing a top-down grouping model (User -> Group -> Role -> Privilege) to escape the three-way junction table mess in single-tenant applications (URBAC), the next logical challenge was scaling that pattern to multi-tenancy.
I wanted to share the architectural pattern I standardized to solve this, along with the boilerplate implementation as MT-URBAC (NestJS, TypeORM, PostgreSQL, and Angular).
The backend architecture:
1- Tenant-Aware Resolution: The application handles both path-based routing (for local development without touching a hosts file) and subdomain-based routing seamlessly.
2- Strict Role Escalation Safeguards: Horizontal privilege escalation is blocked at the guard level. Roles have a numeric level (e.g., Admin = 50, Super Admin = 100), and custom JWT guards intercept requests to ensure lower-level users can't grant higher privileges regardless of permissions.
3- Clean Decorator-Based Protection:
@Post ('create')
@RequirePermissions('user:create')
async createNewUser() { ... }
The repo includes everything packaged up with a database seed script to instantly provision a PostgreSQL database and a Super Admin account so you can test the guards right away.
If you want to adopt this pattern, review the entity structures, or just skip the auth setup on your next build, I open-sourced the repository here:
https://github.com/kasoir/mt-urbac
Curious how others here handle tenant context switching and guard-level role hierarchies in NestJS at scale. Let me know your thoughts!