AggregateRepository
Defined in: packages/ddd-mikro-orm/src/repository/aggregate.repository.ts:58
Base class for the command repositories that load and save an existing aggregate.
Provides authoritative aggregate hydration via findById:
- Bypasses read-side caches (
@FromCache) to prevent stale reads on mutation paths. - Forces MikroORM
{ refresh: true }to avoid returning stale Unit-of-Work identity-map entities. - Translates low-level driver failures through mapPersistenceError into application-neutral transient signals.
Concrete repositories extend this class, inject dependencies into super(...), and provide their
decorated save() method, usually through @PersistedWrite.
The store is any IEntityManagerSource, typically the application’s own
store; its em is also what save() passes to optimisticUpdate or optimisticDelete.
Example
Section titled “Example”// A NestJS provider; STORE is the application's IEntityManagerSource token.@Injectable()export class UpdateUserCommandRepository extends AggregateRepository< UserSnapshot, User, UserSnapshot> { constructor( @Inject(CACHE_TOKEN) cache: ICache<UserSnapshot>, @Inject(STORE) store: IEntityManagerSource, ) { super(cache, store, User, User.aggregateName, User.fromJSON); }
@PersistedWrite<User>({ unique: { email: (user) => new UniqueEmailException(user) }, }) async save(user: User): Promise<UserSnapshot> { ... }}Extends
Section titled “Extends”CommandRepository<TEntity,TResult,TSnapshot>
Type Parameters
Section titled “Type Parameters”TSnapshot
Section titled “TSnapshot”TSnapshot extends Partial<RootEntitySnapshot>
Snapshot structure of the aggregate.
TEntity
Section titled “TEntity”TEntity extends RootEntity<TSnapshot>
Domain aggregate class extending RootEntity.
TResult
Section titled “TResult”TResult = unknown
Persisted result type returned by save().
Implements
Section titled “Implements”IWriteSideAggregateRepository<TEntity>
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new AggregateRepository<
TSnapshot,TEntity,TResult>(cache,store,entityClass,aggregateName,hydrateFn):AggregateRepository<TSnapshot,TEntity,TResult>
Defined in: packages/ddd-mikro-orm/src/repository/aggregate.repository.ts:66
Parameters
Section titled “Parameters”ICache<TSnapshot>
entityClass
Section titled “entityClass”EntityName<TEntity>
aggregateName
Section titled “aggregateName”string
hydrateFn
Section titled “hydrateFn”(snapshot) => TEntity
Returns
Section titled “Returns”AggregateRepository<TSnapshot, TEntity, TResult>
Overrides
Section titled “Overrides”Properties
Section titled “Properties”aggregateName
Section titled “aggregateName”
protectedreadonlyaggregateName:string
Defined in: packages/ddd-mikro-orm/src/repository/aggregate.repository.ts:70
protectedreadonlycache:ICache<TSnapshot>
Defined in: packages/ddd-core/dist/persistence/command-repository.abstract.d.ts:35
Inherited from
Section titled “Inherited from”entityClass
Section titled “entityClass”
protectedreadonlyentityClass:EntityName<TEntity>
Defined in: packages/ddd-mikro-orm/src/repository/aggregate.repository.ts:69
hydrateFn
Section titled “hydrateFn”
protectedreadonlyhydrateFn: (snapshot) =>TEntity
Defined in: packages/ddd-mikro-orm/src/repository/aggregate.repository.ts:71
Parameters
Section titled “Parameters”snapshot
Section titled “snapshot”TSnapshot
Returns
Section titled “Returns”TEntity
protectedreadonlystore:IEntityManagerSource
Defined in: packages/ddd-mikro-orm/src/repository/aggregate.repository.ts:68
Methods
Section titled “Methods”findById()
Section titled “findById()”findById(
id):Promise<TEntity|null>
Defined in: packages/ddd-mikro-orm/src/repository/aggregate.repository.ts:99
Loads the authoritative aggregate directly from persistence.
Enforces { refresh: true } so that if the entity was already loaded in the active
EntityManager’s identity map, its columns are re-fetched from the database before
domain mutations and optimistic concurrency checks take place. The manager comes
from store.em, read for this call.
Parameters
Section titled “Parameters”string
The aggregate identifier.
Returns
Section titled “Returns”Promise<TEntity | null>
Rehydrated aggregate instance, or null if not found.
Throws
Section titled “Throws”If a retryable database connection or driver error occurs.
Implementation of
Section titled “Implementation of”IWriteSideAggregateRepository.findById
save()
Section titled “save()”
abstractsave(entity):Promise<TResult|null>
Defined in: packages/ddd-core/dist/persistence/command-repository.abstract.d.ts:37
Persists the aggregate/payload.
Returning null is the conventional deletion result used by the DDD cache
decorator to trigger eviction/barrier behavior.
Parameters
Section titled “Parameters”entity
Section titled “entity”TEntity
Returns
Section titled “Returns”Promise<TResult | null>
Implementation of
Section titled “Implementation of”IWriteSideAggregateRepository.save