04 / 12

What is the difference between a service and a repository in NestJS?

Difficulty: 5/10
service layer, repository pattern, dependency injection

A service contains business logic and orchestrates calls to repositories, other services, or external APIs. A repository is a data-access abstraction that handles persistence concerns such as queries and transactions. Services are injected with repositories — this keeps business logic and data access cleanly separated.

Service using an injected repository
Separation of concerns:
  1. 1

    Service — owns business rules, validation logic, and orchestration.

  2. 2

    Repository — owns data access: queries, inserts, updates, transactions.

  3. 3

    This separation makes services easier to unit test (mock the repository).

  4. 4

    TypeORM provides Repository<Entity> via @InjectRepository() out of the box.

Scenario Questions

0-2 years experience

  1. 1You need to fetch a list of users in a NestJS controller. Would you call the repository directly or go through a service, and why?
  2. 2If you accidentally inject a repository into a controller instead of a service, what runtime issues might you encounter?
  3. 3Describe how you would set up a simple CRUD module using both a service and a repository.

2-5 years experience

  1. 1We have a feature where creating an order must also write an audit entry. How would you organize the service and repository layers to keep responsibilities clear?
  2. 2During a code review you notice a service directly using TypeORM's EntityManager for queries. What concerns would you raise and how would you refactor it?
  3. 3Our integration tests are failing because the repository is instantiated outside Nest's DI container. Walk me through how you'd fix the wiring.

5-8 years experience

  1. 1Our microservice handles thousands of requests per second and we see a DB‑access bottleneck. How would you decide whether to move logic between service and repository to improve performance, and what patterns would you apply?
  2. 2We need to support both Postgres and MongoDB for the same domain. How would you design the service/repository abstraction to accommodate multiple databases without leaking DB specifics into business logic?
  3. 3A legacy module mixes business logic inside repository classes, causing circular dependencies. How would you restructure the codebase to enforce a clean separation and plan the migration?

8+ years experience

  1. 1Our organization is consolidating several NestJS services into a shared library. What guidelines would you set for when to create a service versus a repository in that library, considering cross‑team reuse and versioning?
  2. 2We plan to replace TypeORM repositories with a custom data‑access layer while keeping existing services stable. How would you orchestrate this migration to minimize risk and ensure backward compatibility?
  3. 3Discuss the long‑term trade‑offs of embedding caching logic inside services versus repositories, especially when multiple teams consume the same data‑access layer.

Follow-up Questions

  • Can you give an example of a method that belongs in a service versus one that belongs in a repository?
  • How does this separation affect your unit testing approach?
  • What would change if you added a caching layer to the data access flow?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.