// project.build = "prj-002" · built by scarif labs
Internal Infrastructure · Data Layer

Substrate

Clean, secure multi-tenant data layer that eliminates months of backend database plumbing for SaaS platforms.

Identifier: prj-002Built by Scarif LabsStatus: active

A lightweight schema and access layer engineered to handle isolation, migrations, and tenant boundaries cleanly across complex SaaS platforms.

TypeScriptPostgreSQLZodSQLPrisma
01 / challenge

The architectural challenge

Every new SaaS platform faces the exact same recurring database architectural challenges: establishing strict tenant data isolation, managing granular role-based permissions, executing non-destructive schema migrations, and preventing cross-tenant data leaks. Startups either waste months re-inventing basic access controls or create fragile queries that risk catastrophic data leaks.

Constraint 01

Avoiding query performance penalties when joining across multiple tenant-scoped relational tables.

Constraint 02

Handling global administration queries and cross-tenant aggregations without bypassing audit controls.

Constraint 03

Supporting custom tenant-specific fields without resorting to unstructured, un-indexable JSON blobs.

02 / role

Scarif Labs' role & execution

Scarif Labs designed and built Substrate as our internal foundation to accelerate B2B SaaS engineering while guaranteeing enterprise-grade tenant isolation.

Product & Interface Design
  • Designed declarative schema definition interfaces that bind domain models directly to validation schemas and database migrations.
  • Built tenant impersonation and debugging inspection consoles with mandatory audit logging.
  • Designed clear error reporting semantics that explain authorization rejections without leaking database topology.
Systems & Software Engineering
  • Engineered PostgreSQL Row-Level Security (RLS) policies driven by cryptographically signed session tokens.
  • Built an automated schema validation pipeline using Zod and TypeScript AST generation to ensure compile-time tenant scoping.
  • Created an automated zero-downtime migration orchestrator enforcing expand/contract database change patterns.
03 / architecture

System architecture & solution

Substrate sits between application handlers and PostgreSQL. Every query automatically executes within a scoped transaction where `app.current_tenant_id` is set from verified JWT claims. Row-Level Security policies at the PostgreSQL engine level reject any query attempting to cross tenant boundaries, providing hardware-level guarantees against data leaks.

Engineering Solution:

Utilized composite primary keys `(tenant_id, id)` and tenant-partitioned indexes across all core tables, ensuring the PostgreSQL query planner eliminates foreign partitions before executing scans.

04 / outcomes

Verified results & benchmarks

Benchmark Metric

Eliminated 6 to 8 weeks of repetitive authorization and database setup on every new SaaS build.

Benchmark Metric

Zero cross-tenant data leak vulnerability surface verified by automated penetration tests.

Benchmark Metric

Maintained sub-5ms query overhead across complex multi-table joins under heavy concurrency.

// lessons_learned
Security boundaries are only reliable when enforced at the lowest possible layer in the stack. Application-level `where tenantId = x` clauses are human-fallible; database engine RLS policies are mathematical guarantees.

Related capabilities & technical research

06 / intake

Need custom systems built with this level of craft?

We engineer scalable infrastructure, real-time sync, and production web platforms from scratch.