5 min read
NestJS 12 and the end of glue code
NestJS 12 shipped the foundations; the weeks since brought the fun part. An official Drizzle module, a fetch-based HTTP client, and an adapter on plain `node:http`, announced for the next minor release, that benchmarks about 50% faster than Express.
NestJS 12 landed at the end of August, and the release itself is mostly foundations:
every core package now ships as ESM, route decorators accept Standard Schema validators
like Zod and Valibot, the CLI was rebuilt around a new nest upgrade command, and there
is a first-party observability SDK. All good. But the part I keep coming back to arrived
after the release: two new official modules and one announced adapter that, together,
replace the glue code every Nest project used to carry.
DrizzleModule: Drizzle, the Nest way
Drizzle keeps the schema in TypeScript and the queries close to SQL. Until now, wiring it
into Nest meant writing a provider by hand or picking one of several community wrappers.
@nestjs/drizzle is the official version, published by the Nest team in September.
import { drizzle } from 'drizzle-orm/node-postgres';
import { DrizzleModule } from '@nestjs/drizzle';
@Module({
imports: [
DrizzleModule.forRoot({
drizzle,
connection: process.env.DATABASE_URL!,
}),
],
})
export class AppModule {}The drizzle option is whichever driver's drizzle() function you want: node-postgres
here, but MySQL, SQLite, Neon, Turso and D1 go through the same option. The database is
global, so there is no forFeature() step. A service just asks for it:
@Injectable()
export class UsersService {
constructor(@InjectDrizzle() private readonly db: NodePgDatabase) {}
findOne(id: number) {
return this.db.select().from(users).where(eq(users.id, id));
}
}A few details show it was designed by people who have been bitten:
forRootAsync()works withConfigService, which matters becauseforRoot()options are evaluated at import time, possibly before your env file is loaded.- Multiple databases are just named imports, injected with
@InjectDrizzle('analytics'). - Testing is a custom provider on
getDrizzleToken(). - The docs say it plainly: don't run migrations on startup when several instances boot
together; run
drizzle-kit migrateas a release step.
It targets the Drizzle v1 release candidate and also supports v0.35 and later. It is at 0.0.2 on npm, so expect it to move.
HttpClientModule: fetch, plus everything you always end up writing
@nestjs/axios wraps Axios and returns RxJS Observables, which means a
firstValueFrom() around nearly every call. @nestjs/http-client is built on Node's
native fetch instead, has no third-party dependencies, and returns Promises.
@Module({
imports: [
HttpClientModule.register({
name: 'github',
baseUrl: 'https://api.github.com',
timeout: '5s',
retry: 3,
}),
],
})
export class ReposModule {}
@Injectable()
export class ReposService {
constructor(@InjectHttpClient('github') private readonly github: HttpClient) {}
async find(owner: string, repo: string) {
const { data } = await this.github.get<Repo>('/repos/:owner/:repo', {
params: { owner, repo },
});
return data;
}
}What I like is that the things every project re-implements are now options: per-attempt
timeouts as duration strings, retries with backoff, :name path parameters, one named
client per upstream API, and interceptors written as plain functions or as classes.
Errors are typed (HttpResponseError, HttpTimeoutError, HttpNetworkError and
HttpParseError, all extending HttpClientError), and toHttpException() maps them to
Nest exceptions: an upstream failure becomes a 502, a timeout a 504. That is exactly the
mapping I would otherwise write by hand in every gateway service.
@nestjs/axios is not going anywhere; the docs ship a migration table, not a
deprecation notice.
platform-node: coming in the next minor, and the one I'm most excited about
Nest has two HTTP adapters. Express is the default and the slower one; Fastify is fast,
but moving to it means rewriting Express-style middleware and @Res() code against
Fastify's own API. On October 9, Kamil Myśliwiec opened a pull request for a third:
@nestjs/platform-node, which sits directly on node:http. Three days later the official
NestJS account announced it for the next minor release, asking what Nest would be if it
were "just Nest".
const app = await NestFactory.create(AppModule, new NodeAdapter());Requests and responses are subclasses of Node's own IncomingMessage and
ServerResponse, carrying the Express-style helpers that middleware actually calls. The
PR reports that helmet, compression, express-session, passport and others (21 popular
middleware packages in all) behaved the same as on the Express adapter. Body parsing is
built in, and it rejects prototype poisoning the way Fastify does.
The benchmark in the PR is the headline. One round of wrk on an M1 Pro with Node 24,
in requests per second:
GET / |
GET /users/:id |
POST JSON | |
|---|---|---|---|
| Nest + Express | 37.1k | 34.5k | 28.2k |
| Nest + Fastify | 52.4k | 48.4k | 40.2k |
| Nest + Node | 55.1k | 51.0k | 42.4k |
About 50% faster than Express and slightly ahead of Fastify, without leaving the Express
programming model. It does differ from Express in a few places worth knowing before you
switch: routes match by specificity rather than declaration order, paths are
case-sensitive, and strings returned from handlers go out as text/plain.
As I write this it is not on npm yet; the pull request is still open. Once the next minor lands, switching is the one line above.
Where this leaves NestJS
Put together, a new Nest app can handle persistence, outbound HTTP and, soon, its HTTP server with first-party packages only, on ESM, validated with whatever schema library you already use. The framework is absorbing the glue code I used to copy from project to project, and that is the best kind of release.