Repository contracts

Abstract repository classes and query DTOs in the domain.

The domain layer defines what the repository should do, without exposing how data is persisted. Handlers depend on these contracts; infrastructure provides concrete implementations.

typescript
// src/domain/repositories/iperson.repository.ts
import { ListResponse } from '@/core/types';
import { PersonQueryDto } from '../dtos/person-query.dto';
import { Person } from '../entities/person/person';

export abstract class IPersonRepository {
  abstract findMany(query: PersonQueryDto): Promise<ListResponse<Person>>;
  abstract findById(id: number): Promise<Person | null>;
  abstract save(person: Person): Promise<Person>;
  abstract delete(person: Person): Promise<void>;
}

Query DTOs live in src/domain/dtos and extend PaginationDto when applicable:

typescript
export class PersonQueryDto extends PaginationDto {
  @AutoMap()
  name?: string;
}

The handler maps the validated request to the DTO before calling the repository:

typescript
const query = AutoMapper.map(
  new ReadManyPersonValidator(req).validate(),
  ReadManyPersonRequest,
  PersonQueryDto,
);

Handlers receive the abstract class via constructor:

typescript
@Injectable()
export class DeletePersonHandler implements RequestHandlerBase<number, void> {
  constructor(private readonly repository: IPersonRepository) {}

  async handle(id: number): Promise<void> {
    const person = await this.repository.findById(id);

    if (!person) {
      throw new NotFoundException('Pessoa não encontrada');
    }

    await this.repository.delete(person);
  }
}
typescript
@Module({
  imports: [DatabaseModule],
  providers: [{ provide: IPersonRepository, useClass: PersonRepository }],
  exports: [DatabaseModule, IPersonRepository],
})
export class RepositoryModule {}
  1. Define the abstract class in src/domain/repositories/i<resource>.repository.ts.
  2. Create the query DTO in src/domain/dtos/ if there are filtered listings.
  3. Implement the concrete class in src/infra/repositories/.
  4. Register the provider in RepositoryModule.

Koala Nest

A facilitator for building NestJS APIs with DDD architecture. Code copied into your repository — readable, adaptable, and under your control.

Creator

igordrangel.com.br

Design, back-end, and product strategy.

Quick Commands

Global CLI and scripts in the generated project

  • bun install -g @koalarx/nest
  • kl-nest new
  • kl-nest add cache
  • bun run migration:run # CRUD template
  • kl-nest --help
© 2026 Koala NestBuilt for NestJS developers and AI-assisted workflows.