ADR: Prisma Schema Consolidation
Status: Accepted Date: 2026-01-12 Deciders: Architecture Team Related: Prisma 7 Migration
Context
Current State
The monorepo has two separate Prisma setups:
| Location | Package | Version | Output |
|---|---|---|---|
packages/database | @emergexyz/db | 6.16.3 | Default (node_modules/.prisma/client) |
apps/miniapp | Local | 6.13.0/6.15.0 | lib/generated/prisma |
Problems
- Schema Drift: Two schemas that should be identical can diverge
- Migration Conflicts: Two migration histories for the same database
- Duplicate Dependencies: Prisma installed twice in the monorepo
- Inconsistent Versions: miniapp on 6.13, packages/database on 6.16
- Maintenance Burden: Schema changes must be made in two places
Schema Comparison
Both schemas share the same models (api_key, assignment, chat, etc.) but differ in:
prisma
# packages/database
generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "rhel-openssl-3.0.x"] # For deployment
}
# apps/miniapp
generator client {
provider = "prisma-client-js"
output = "../lib/generated/prisma" # Custom output path
}Decision
Consolidate all Prisma usage to packages/database (@emergexyz/db).
Changes Required
Remove miniapp's local Prisma:
- Delete
apps/miniapp/prisma/directory - Remove
prismaand@prisma/clientfrom miniapp's dependencies - Remove
postinstall: prisma generatescript
- Delete
Add dependency on shared package:
json{ "dependencies": { "@emergexyz/db": "workspace:*" } }Update imports:
typescript// Before import { PrismaClient } from "@/lib/generated/prisma"; // After import { prisma } from "@emergexyz/db"; // Or for types: import type { User, Output } from "@emergexyz/db";Update packages/database exports (if needed):
- Ensure all types are exported
- Add any miniapp-specific models to shared schema
Implementation
Phase 1: Schema Sync Verification
- Diff the two schemas to identify any differences
- Merge any miniapp-only models into packages/database
- Run migration if schema changes needed
Phase 2: Miniapp Migration
- Add
@emergexyz/dbdependency to miniapp - Update all Prisma imports (find/replace)
- Update
lib/prisma.tsto re-export from shared package - Remove local Prisma setup
Phase 3: Cleanup
- Delete
apps/miniapp/prisma/directory - Delete
apps/miniapp/lib/generated/directory - Remove Prisma dev dependencies from miniapp
- Verify build succeeds
Consequences
Positive
- Single Source of Truth: One schema, one migration history
- Consistent Types: All apps use same generated types
- Easier Maintenance: Schema changes in one place
- Smaller Bundle: No duplicate Prisma client in miniapp
- Version Consistency: All consumers on same Prisma version
Negative
- Migration Effort: Need to update all imports in miniapp
- Build Dependency: miniapp build depends on packages/database being built first
Neutral
- Turbo Pipeline: Already handles package dependencies correctly
Files Affected
apps/miniapp/
├── package.json # Remove prisma deps, add @emergexyz/db
├── lib/prisma.ts # Update to use shared package
├── lib/generated/ # DELETE entirely
├── prisma/ # DELETE entirely
└── **/*.ts # Update imports
packages/database/
├── prisma/schema.prisma # May need new models from miniapp
└── src/index.ts # Ensure all types exportedRollback Plan
If issues arise:
- Restore miniapp's
prisma/directory from git - Revert package.json changes
- Run
prisma generatein miniapp
