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

Installation

Define the Database Schema

Create src/infrastructure/database/schema.ts:

Initialize the Database

Create src/infrastructure/database/db.ts:

Define Domain Models

Create Mappers

ToDomain Mapper

ToPersistence Mapper

Create Repository

Usage

Setup UnitOfWork and Repository

Create

Create with Nested Entities

Find with Criteria

Update

Connect / Disconnect (N:N Reference)

Remove Owned Entity from Collection

Delete

Transactions


Key Differences from Prisma


Limitations

Criteria dot-field paths are not supported. Filters, ordering, and search fields must reference top-level columns of the primary table. Using "profile.name", "posts.title", or any dotted path throws a DrizzleAdapterError. Add custom repository methods with explicit JOINs for cross-table queries.

Transactional Outbox (optional)

If your aggregates emit domain events, pass an optional outboxStore in DrizzleRepositoryConfig. 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 (outboxTable, event bus decorator, background publisher). Repository wiring is documented under Drizzle Integration → Transactional Outbox.

Next Steps

Drizzle Integration

Deep dive into all Drizzle 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

Schema Registry

Full Schema Registry reference