Question library
Explained questions for the decisions developers make.
What are modules, controllers and services?
Nest modules define feature boundaries and register the controllers and providers that belong to a feature. Controllers handle transport concerns, while services usually contain business rules. This separation makes code easier to reuse and test without tying every decision to HTTP.
How does dependency injection work?
Dependency injection means a class receives the objects it needs instead of constructing them itself. Nest's container uses registered provider tokens to build and connect these dependencies. This centralizes construction and lets tests or configuration replace an implementation without rewriting its consumers.
Why does Nest say it cannot resolve a dependency?
A dependency-resolution error means Nest cannot find or create a required provider in the current module context. The class may exist in source code but still be invisible to the consumer's module. Troubleshooting should follow the token, registration, export and import path.
What are custom providers and provider scopes?
A provider is a dependency that Nest can supply, such as a class instance, value or factory result. Custom providers control how that dependency is created, and scope controls how long its instances are shared. These choices affect testability, state isolation and allocation cost.
Dynamic module vs global module?
A dynamic module creates its module definition from supplied configuration, making a feature reusable in different environments. A global module makes its exported providers broadly available after registration. Dynamic describes configuration behavior; global describes visibility, so these are independent properties.
What is the Nest request lifecycle?
The request lifecycle is the order in which Nest processes an incoming request and produces a response. Different extension points handle different concerns, such as access checks, input conversion and response wrapping. Knowing the order helps explain why code in one stage cannot rely on work that happens later.
How do global, controller and route handlers interact?
Nest can attach request handlers at global, controller and route scopes. Scope determines which requests a handler applies to, while handler type determines execution order. Interceptors wrap inner work and unwind on the response path; exception filters select a matching handler rather than form a normal middleware chain.
Guard vs pipe vs interceptor vs filter?
Guards, pipes, interceptors and filters are separate request extension points with different jobs. A guard decides whether work may proceed; a pipe validates or transforms arguments; an interceptor wraps execution; a filter handles uncaught errors. Choosing the correct point avoids mixing authentication, validation and formatting into business methods.
Why use a DTO and ValidationPipe?
A DTO describes the data a boundary accepts or returns, while ValidationPipe checks and optionally transforms incoming values at runtime. TypeScript types alone cannot validate a network payload because they are erased. Decorated DTO classes make the rules available to the validation mechanism.
How should authentication and permissions work?
Authentication establishes who the caller is, and permissions determine which actions and records that caller may access. In Nest, guards commonly apply these checks before controller execution, often using Passport strategies for authentication. Resource-level checks still need enough context to enforce ownership and tenant rules.
TypeORM vs Sequelize vs Mongoose?
TypeORM and Sequelize are object-relational mappers for relational databases, while Mongoose models documents in MongoDB. They provide convenient data-access APIs but do not replace database knowledge. The generated queries, constraints and transaction behavior still determine correctness and performance.
Repository vs QueryBuilder vs raw SQL?
A repository exposes common operations around an entity, QueryBuilder assembles more flexible database queries, and raw SQL gives direct control over SQL text. These are levels of access rather than a ranking from bad to good. Use the simplest approach that expresses the required query clearly and efficiently.
How should a TypeORM transaction work?
A transaction groups database changes so they can commit together or roll back together. In TypeORM, the transaction callback receives a manager tied to that transaction's connection. Queries issued through an unrelated global repository may not participate, even if they appear inside the same JavaScript callback.
What are N+1, eager loading and lazy loading?
N+1 is a data-access pattern where fetching N parent records leads to roughly N more queries for related records. Eager loading fetches configured relations up front, while lazy loading waits until the relation is accessed. Both loading styles can waste work if they do not match the endpoint's real needs.
Why use migrations instead of automatic schema sync?
A migration is a versioned database change that can be reviewed and applied deliberately. Automatic schema synchronization tries to match the schema to entity definitions and may make changes without a safe production rollout plan. Migrations make data changes, compatibility and deployment order explicit.
How should Nest handle jobs and scheduled tasks?
A background job moves work out of the immediate request path, while a scheduled task starts work at a chosen time. Nest can integrate with queues and schedulers, but reliable execution still requires durable state and coordination. Multiple app instances can run the same schedule unless one owner or a distributed coordination mechanism is used.
What are WebSockets, microservices and CQRS used for?
WebSockets maintain a two-way connection for ongoing messages, microservices split an application into separately operated services, and CQRS separates command/write handling from query/read handling. These solve different problems and can be used independently. Each adds complexity that should be justified by a concrete requirement.
How do I test and deploy a Nest app?
Testing a Nest application means verifying business rules, framework wiring and real integrations at suitable levels. Deployment then packages that verified application with configuration, health checks and lifecycle handling. Mock-only tests cannot prove the database constraints or authentication chain behave correctly.
Difficulty is an editorial estimate. Completion means you chose to mark a question as understood.