Overview
@woltz/rich-domain-typeorm provides full integration between rich-domain and TypeORM, bringing Domain-Driven Design patterns with automatic change tracking and batch operations.
Change Tracking
Automatic detection and persistence of aggregate changes
Batch Operations
Optimized bulk inserts, updates, and deletes
N:N Relations
Smart handling of owned (1:N) and reference (N:N) collections
Transaction Support
Full ACID compliance with @Transactional decorator
Transactional Outbox
Optional
outboxStore in repository config for guaranteed event deliveryFor a complete working example, see the fastify-with-typeorm example in the repository.
Quick Start
1. Setup DataSource and UnitOfWork
2. Define Domain Entity
3. Create TypeORM Entity
4. Create Mappers
5. Create Repository
6. Use It
TypeORMUnitOfWork
Manages transactions with per-request isolation using AsyncLocalStorage.Setup
Transaction Execution
Request Isolation
Each HTTP request gets its own transaction context:API Reference
@Transactional Decorator
Automatically wraps methods in transactions.Nested Transactions
The decorator is idempotent - if already in a transaction, it reuses it:With Explicit UoW
TypeORMRepository
Base class for repositories with full Criteria support.Configuration
Transactional Outbox
Pass an optionaloutboxStore in TypeORMRepositoryConfig. When set, save() automatically persists uncommitted domain events to the outbox table in the same database transaction as the aggregate write.
Use
TypeORMOutboxStore from @woltz/rich-domain-typeorm. For the full setup (entity registration, event bus decorator, background publisher), see Transactional Outbox.
Methods
Criteria Queries
Case-Insensitive Search
TypeORMToPersistence
Base class for mapping domain aggregates to persistence.Registry Configuration
Collection Types
TypeORMBatchExecutor
Executes batch operations from AggregateChanges.Execution Order
- Deletes (leaf → root by depth DESC)
- Owned: Delete entities
- Reference: Unlink from junction table
- Creates (root → leaf by depth ASC)
- Owned: Create entities
- Reference: Insert into junction table
- Updates (any order)
Direct Usage
Convenience Function
N:N Relations
Configuration
Usage
Error Handling
The adapter provides specific error types:Complete Example
See the fastify-with-typeorm example for a complete working application demonstrating:- User aggregate with Posts (1:N owned)
- Post with Tags (N:N reference via junction table)
- Case-insensitive search
- Transaction management
- CRUD operations
- Domain events with BullMQ