Data Isolation Paradigms in Enterprise SaaS
Preventing cross-tenant data leaks is the single most critical engineering requirement in high-compliance B2B SaaS applications. At Kling Digital, we employ a dynamic multi-tenant architecture with per-tenant PostgreSQL databases and strict schema boundaries.
Dynamic Prisma Client Connection Pooling
Incoming edge requests resolve the tenant context and borrow a cached client from our dynamic pool:
export async function getPrismaClientByTenantId(tenantId: string | null) {
if (!tenantId) return masterPrisma;
if (tenantClients.has(tenantId)) return tenantClients.get(tenantId)!;
const tenantConfig = await masterPrisma.tenant.findUnique({ where: { id: tenantId } });
const client = new PrismaClient({ datasourceUrl: tenantConfig.databaseUrl });
tenantClients.set(tenantId, client);
return client;
}

