Skip to content

CommandRepository

Defined in: packages/ddd-core/persistence/command-repository.abstract.ts:37

Base class for write-side (command) repositories.

Persists an entity or aggregate state via the abstract save method and receives an ICache instance. Subclasses annotate save() with the @Cache decorator to perform automatic write-through caching or cache eviction upon mutation.

Creating a command repository with the Cache decorator

@Injectable()
export class CreateUserCommandRepository extends CommandRepository<User, UserSnapshot> {
constructor(
@Inject(CACHE_TOKEN) protected readonly cache: ICache<UserSnapshot>,
@Inject(MIKRO_ORM_CLIENT) private readonly store: MikroOrmStore,
) {
super(cache);
}
@Cache<User, UserSnapshot>(
(user) => cacheKey(User.aggregateName, { id: user.id }),
)
async save(user: User): Promise<UserSnapshot> {
const created = await this.store.em.upsert(User, user);
return created.toJSON();
}
}

TEntity = unknown

The aggregate entity type accepted by save.

TResult = unknown | null

The persisted result type returned by save.

TCache = unknown

new CommandRepository<TEntity, TResult, TCache>(cache): CommandRepository<TEntity, TResult, TCache>

Defined in: packages/ddd-core/persistence/command-repository.abstract.ts:43

ICache<TCache>

CommandRepository<TEntity, TResult, TCache>

protected readonly cache: ICache<TCache>

Defined in: packages/ddd-core/persistence/command-repository.abstract.ts:43

abstract save(entity): Promise<TResult | null>

Defined in: packages/ddd-core/persistence/command-repository.abstract.ts:45

Persists the aggregate/payload.

Returning null is the conventional deletion result used by the DDD cache decorator to trigger eviction/barrier behavior.

TEntity

Promise<TResult | null>

ICommandRepository.save