NestJS Ninja logo

NestJS Ninja

Backend lessons & architecture notes

TypeScript servers

Build sharper NestJS systems.

Practical writing on modules, providers, queues, testing, microservices, and the decisions that keep backend codebases easy to change.

app.module.ts
@Module({
  imports: [
    ConfigModule.forRoot(),
    QueueModule,
    ObservabilityModule,
  ],
  controllers: [ArticlesController],
  providers: [ArticlesService],
})
export class AppModule {}

Featured

Start with the latest deep dive

Read featured post
NestJS and LangChain: Agents, Tools and Structured Output
NestJSLangChainAIZod
14 min read

NestJS and LangChain: Agents, Tools and Structured Output

LangChain v1 inside a NestJS application, built as a real support API: two models behind injection tokens so switching vendor is an environment variable, structured output with Zod, an LCEL drafting chain, cross-provider fallback, and an agent whose tools call your ordinary services. Plus the finding that changed how I write this code: withStructuredOutput parses the reply but does not validate it, so an invalid enum arrives typed and wrong. Every one of the 23 tests runs without an API key.

Latest

More recent posts

View all

Building an MCP Server with NestJS

An MCP server is how you hand your application to an AI client like Claude or ChatGPT without writing a chatbot. We build one on top of a normal NestJS service using @rekog/mcp-nest, expose tools, resources and prompts with Zod, and call every one of them with curl. Along the way we hit two things the README does not warn you about: your exceptions arrive at the model as "Internal server error", and your guards do not protect tools/list.

Optimistic vs Pessimistic Locking in NestJS with TypeORM and Postgres

Two concurrent requests read the same row, both decide it is safe to write, and one silently overwrites the other. We reproduce that lost update on purpose, then fix it two ways in NestJS and TypeORM: SELECT ... FOR UPDATE inside a transaction, and a single conditional UPDATE guarded by a version column. Proven with real concurrent races against Postgres, not a mock.

Public and Private Keys in NestJS: Signing JWTs and Publishing a JWKS

How the keys an SSO provider generates actually work, built in NestJS: sign access tokens with a private key, publish the matching public key at /.well-known/jwks.json, verify every request against it, and let an outside service or the browser verify with nothing but the URL. Then hand the trusted claims to the CASL authorization layer.

GraphQL Federation in NestJS: Subgraphs, Entities, and a Gateway

One GraphQL schema, many NestJS services. We take a code-first GraphQL API and split it into Apollo Federation v2 subgraphs — a users service that owns the User entity, an orders service that extends it — composed by a gateway that plans queries across both without the client ever knowing.

Validation in NestJS: Pipes, DTOs, and Errors That Fit Your API

Pipes are the front door of a NestJS request — they transform and validate input before it ever reaches a service. This post covers the ValidationPipe and DTOs, the options that actually matter (whitelist, transform), parsing and custom pipes, a Zod alternative, generating Swagger/OpenAPI docs from the same DTOs, and how to make validation failures come back in the same error shape as the rest of your API.