typescript
57 lines · 7 steps
Type-safe pagination queries in NestJS
A validated DTO turns raw query strings into safe pagination inputs and a structured paginated response.
Explained by
highlit
1import { IsInt, IsOptional, Max, Min } from 'class-validator';
2import { Type } from 'class-transformer';
3
4export class PaginationQueryDto {
5 @IsOptional()
6 @Type(() => Number)
7 @IsInt()
8 @Min(1)
9 page: number = 1;
10
11 @IsOptional()
12 @Type(() => Number)
13 @IsInt()
14 @Min(1)
15 @Max(100)
16 limit: number = 20;
17
18 get skip(): number {
19 return (this.page - 1) * this.limit;
20 }
21}
22
23export interface PaginatedResult<T> {
24 data: T[];
25 meta: {
26 total: number;
27 page: number;
28 limit: number;
29 pageCount: number;
30 };
31}
32
33@Controller('articles')
34export class ArticlesController {
35 constructor(private readonly articlesService: ArticlesService) {}
36
37 @Get()
38 async findAll(
39 @Query() { page, limit, skip }: PaginationQueryDto,
40 ): Promise<PaginatedResult<Article>> {
41 const [data, total] = await this.articlesRepository.findAndCount({
42 order: { publishedAt: 'DESC' },
43 skip,
44 take: limit,
45 });
46
47 return {
48 data,
49 meta: {
50 total,
51 page,
52 limit,
53 pageCount: Math.ceil(total / limit),
54 },
55 };
56 }
57}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Decorator-based DTOs let you validate and coerce query parameters declaratively instead of writing manual parsing logic.
- 2A computed getter on the DTO keeps derived values like the SQL offset next to the fields they depend on.
- 3Returning a generic result envelope gives every list endpoint a consistent, typed shape of data plus metadata.
Related explainers
typescript
import { registerLocaleData } from '@angular/common'; import localeFr from '@angular/common/locales/fr'; import localeFrExtra from '@angular/common/locales/extra/fr'; import localeDe from '@angular/common/locales/de';
Locale-aware bootstrapping in Angular
i18n
localization
dependency-injection
Intermediate
8 steps
typescript
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import * as Joi from 'joi';
Validating env config at boot in NestJS
configuration
schema-validation
environment-variables
Intermediate
8 steps
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
typescript
import { Inject, Injectable, Logger } from '@nestjs/common'; import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { Cache } from 'cache-manager'; import { InjectRepository } from '@nestjs/typeorm';
A cache-aside country lookup in NestJS
cache-aside
dependency-injection
batch-lookup
Intermediate
8 steps
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
Intermediate
8 steps
typescript
import { Injectable, effect, signal, computed } from '@angular/core'; interface Preferences { theme: 'light' | 'dark';
A signal-based preferences store in Angular
signals
state-management
persistence
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/type-safe-pagination-queries-in-nestjs-explained-typescript-fc13/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.