ProficientNowTechRFCs
Product RFCs/DB Ops/RFC DBOPS 0002

3. System Architecture

RFC-DBOPS-0002                                                    Section 3
Category: Standards Track                              System Architecture

3.1 System Overview

The target architecture replaces Prisma Client's runtime query generation with Kysely for all tenant SQL operations while preserving the existing service topology, migration tooling, and pooling infrastructure. Application services continue to connect through PgBouncer for database access. Kysely compiles queries to parameterized SQL and executes them over a shared pg.Pool, with per-query schema qualification via withSchema() replacing the per-tenant client model.

During the migration period, both query paths coexist. Each endpoint operates on either the Prisma path or the Kysely path, controlled by a routing mechanism at the service layer. The Prisma path remains available for rollback until the Kysely path is validated.

The target state eliminates the Prisma Engine intermediary for tenant queries. Application services invoke Kysely's query API, which compiles to parameterized SQL and dispatches through the shared pg.Pool to PgBouncer and then to the tenant database. Migration tools continue to use session pooling for schema operations.


3.2 Authority Domains

DomainAuthorityScope
Query GenerationKysely query builder (target); Prisma Client (sunset)Translates application query intent into parameterized SQL
Schema DefinitionPrisma schema files in @pnats/schema-managerAuthoritative source for table structures and relation definitions
Type GenerationPrisma-generated types (current); Kysely type derivation (target)Provides compile-time TypeScript types for query parameters
Connection Managementpg.Pool (target); Prisma Engine (sunset)Manages database connections and pool lifecycle
Schema MigrationPrisma Migrate and AtlasApplies DDL changes to metadata and tenant databases
Tenant Schema ScopingwithSchema() per query (target); search_path mutation (sunset)Qualifies table references with the tenant schema identifier
Configuration Authority@pnats/schema-manager PrismaModule and NestJS ConfigServiceProvides database connection URLs and tenant configuration

3.3 Trust Boundaries

BoundaryDescription
Service → Query BuilderTenant module services pass tenant IDs and query parameters to the query builder; the query builder MUST scope all queries to the specified schema
Query Builder → Connection PoolThe query builder submits parameterized SQL to the connection pool; the pool MUST NOT retain schema context between queries from different tenants
Connection Pool → PgBouncerThe pg.Pool connects to PgBouncer transaction pooler; connection state MUST NOT leak between pooled connections
PgBouncer → Tenant DatabasePgBouncer mediates authenticated connections to PostgreSQL; authentication and pooling rules MUST continue to enforce access boundaries
Migration Tools → Session PoolerPrisma Migrate and Atlas use session pooling for DDL operations; migration connections are separate from query connections

3.4 Stable Interfaces

The following interfaces remain stable through the migration:

InterfaceDescription
HTTP API contractsAll endpoint request/response shapes, status codes, and pagination behavior remain unchanged
PgBouncer SQL protocolApplication traffic continues through PgBouncer transaction pooler via PostgreSQL wire protocol
Session pooler for migrationsSchema migration tooling continues to use session pooler for DDL operations
x-tenant-id headerTenant identification continues via the x-tenant-id HTTP header validated by TenantGuard
@pnats/schema-manager module APIPrismaModule continues to export database configuration; Kysely integration extends rather than replaces
Prisma schema as source of truthThe multi-file Prisma schema in prisma/tenants/schema/ remains the authoritative schema definition

On this page