Skip to main content
For a complete working example, see the fastify-with-prisma example in the repository.

Installation

Setup Prisma

Initialize and configure Prisma:
Define your schema in prisma/schema.prisma:
Generate Prisma Client:

Initialize Prisma Client

Define Domain Models

Domain Entities

Create Schema Types

Create Mappers

ToDomain Mapper

ToPersistence Mapper

Default onUpdate behavior (via PrismaBatchExecutor):
  • Root updates (User fields)
  • Owned entity changes (Post creates/updates/deletes)
  • Reference changes (Tag link/unlink in junction table)
Override onUpdate only when you need custom persistence logic. Use changes.without() to handle one entity manually and delegate the rest to super.onUpdate().
Collection Types:
  • owned: Entity belongs to and is lifecycle-managed by the aggregate (e.g., User → Posts). Created and deleted with the parent.
  • reference: Independent entity that exists separately (e.g., Post ↔ Tags). Only the relationship is managed via junction table.
Junction Table:
  • Only needed when you manually create the pivot table (like TagPost)
  • If using Prisma’s implicit many-to-many (@relation), no junction config needed

Create Repository

Usage

Generate from Schema (CLI)

Auto-generate all domain code from your Prisma schema:
This creates:
  • ✅ Aggregates/Entities
  • ✅ Value Objects
  • ✅ Repository interfaces
  • ✅ Repository implementations
  • ✅ Mappers (ToDomain & ToPersistence)
The CLI analyzes your Prisma schema relationships and generates proper DDD aggregates. See the CLI documentation for more details.

Transactional Outbox (optional)

If your aggregates emit domain events, pass an optional outboxStore as the fifth argument to PrismaRepository’s constructor. When set, save() persists uncommitted events to the outbox table in the same transaction as the aggregate — so events are not lost if the process crashes before dispatchAll(). See Transactional Outbox for the full setup (Prisma model, event bus decorator, background publisher). Repository wiring is documented under Prisma Integration → Transactional Outbox.

Next Steps

Prisma Integration

Deep dive into Prisma adapter features

Transactional Outbox

Guaranteed domain event delivery with the outbox pattern

Repository Pattern

Learn advanced repository patterns

Change Tracking

Understand how changes are tracked

CLI Reference

Complete CLI documentation