About a year ago, a simple missed tenant filter in a cleanup script erased a whole test tenant’s data. Half a day restoring later, we knew “just remember to add WHERE tenant_id = ?” wasn’t enough.
So we built a 3-layer safeguard in our NestJS + TypeORM SaaS:
1. Base entity with tenantId column enforced in code reviews
2. Tenant context tied to each request that throws if missing
3. Custom repository wrapping queries to always filter by tenant
Edge cases like background jobs needed explicit tenant context runners.
Not perfect—indexes hit performance and raw SQL bypasses checks—but zero cross-tenant leaks in a year.
Anyone tried schema-per-tenant with NestJS? Pooling looks tricky at scale.
#nestjs #typeorm #saas #multitenancy #webdev
https://github.com/sayahweb2-png/saas-starter-lite
