Skip to content

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:

LocationPackageVersionOutput
packages/database@emergexyz/db6.16.3Default (node_modules/.prisma/client)
apps/miniappLocal6.13.0/6.15.0lib/generated/prisma

Problems

  1. Schema Drift: Two schemas that should be identical can diverge
  2. Migration Conflicts: Two migration histories for the same database
  3. Duplicate Dependencies: Prisma installed twice in the monorepo
  4. Inconsistent Versions: miniapp on 6.13, packages/database on 6.16
  5. 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

  1. Remove miniapp's local Prisma:

    • Delete apps/miniapp/prisma/ directory
    • Remove prisma and @prisma/client from miniapp's dependencies
    • Remove postinstall: prisma generate script
  2. Add dependency on shared package:

    json
    {
      "dependencies": {
        "@emergexyz/db": "workspace:*"
      }
    }
  3. 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";
  4. 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

  1. Diff the two schemas to identify any differences
  2. Merge any miniapp-only models into packages/database
  3. Run migration if schema changes needed

Phase 2: Miniapp Migration

  1. Add @emergexyz/db dependency to miniapp
  2. Update all Prisma imports (find/replace)
  3. Update lib/prisma.ts to re-export from shared package
  4. Remove local Prisma setup

Phase 3: Cleanup

  1. Delete apps/miniapp/prisma/ directory
  2. Delete apps/miniapp/lib/generated/ directory
  3. Remove Prisma dev dependencies from miniapp
  4. 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 exported

Rollback Plan

If issues arise:

  1. Restore miniapp's prisma/ directory from git
  2. Revert package.json changes
  3. Run prisma generate in miniapp

References

Released under the MIT License.