Database Infrastructure Constraints¶
This spec describes the shared Prisma and database expectations that every feature touching persistent storage must honor.
1. Prisma Client Usage¶
- Singleton pattern: Import
prismafrombackend/src/lib/prisma.tsonly. That module manages a singlePrismaClientattached toglobalThisin non-prod environments, preventing connection storms during hot reloads or lambda-style invocations. - Avoid re-instantiation: Do not call
new PrismaClient()elsewhere. If a test or script needs a different Prisma instance, isolate it in a dedicated setup file and clean it up afterwards. - Logging scope: By default, Prisma logs
['error', 'warn']. Feature code should not enable query logging globally; add per-operationlogoverrides only when debugging a specific issue, and remove them before checking in.
2. Connection Resilience¶
- Env and pooling: The deployed services rely on
DATABASE_URLto carry both the host credentials and connection tuning parameters. Embed options such as?connection_limit=20&pool_timeout=20in the URL when provisioning databases instead of hardcoding them in code. - Bootstrap check: Call
testConnection()frombackend/src/lib/prisma.tsduring startup health probes or CI smoke tests to ensure the database is reachable. Capture failures in the service logs (logger.error) so infrastructure alerting can trigger. - Transactions & retries: Use
prisma.$transactionfor multi-step updates that must succeed atomically. Surface Prisma Errors to the caller so service layers can decide whether to retry, and log enough context to diagnose deadlocks/timeouts. - Read-replica awareness: If a route needs strongly consistent reads after writes (e.g., role changes, permission grants), force the query to the primary node via intents or use immediate transaction scopes rather than relying on eventual replication.
3. Schema & Migration Hygiene¶
- Schema updates: A feature that touches DB schema must update the Prisma schema file and run
npx prisma migrate devlocally. Commit both the schema and migration files. - Migration strategy: Deployments should run
npx prisma migrate deployinstead ofdb pushwith--accept-data-loss. Document this constraint in deployment scripts and avoid destructive flags in prod manifests. - Seed data alignment: Whenever new roles or default entities are introduced, ensure seed scripts (demo, admin, etc.) and Kratos sync scripts populate matching records so new features have a fully provisioned environment.
References¶
backend/src/lib/prisma.tsbackend/src/lib/types.tsbackend/src/routes/auth.tsgateway/opa/policies/rbac.rego