> For the complete documentation index, see [llms.txt](https://kinesis-school-of-programming.gitbook.io/nestjs-unleashed/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kinesis-school-of-programming.gitbook.io/nestjs-unleashed/core-module-backend-development-with-nestjs/pagination.md).

# Pagination

Here, we'll learn how to fetch only chunks (pages) of data from the database instead of all of the data available.

**Pagination** is about fetching only a portion of the data available in the database, as otherwise it could be a very costly operation. We will use **offset-based pagination**, which is simpler and well suited to our case.

To use pagination, let's first create a DTO to represent the data related to **current position** - <mark style="color:blue;">`offset`</mark>, and **page size** - <mark style="color:blue;">`limit`</mark>. We'll create it in <mark style="color:purple;">common</mark>/<mark style="color:purple;">dto</mark>/<mark style="color:purple;">pagination.dto</mark>.

```typescript
export class PaginationDto {
  @IsOptional()
  @IsInt()
  @IsPositive()
  readonly limit: number;

  @IsOptional()
  @IsInt()
  @IsPositive()
  readonly offset: number;
}
```

Then, let's return to the file <mark style="color:purple;">common.constants</mark> to have a constant for the default page sizes.

```typescript
export const DefaultPageSize = {
  USERS: 10,
} as const satisfies Record<string, number>;
```

{% hint style="info" %}
The <mark style="color:blue;">`satisfies`</mark> keyword forces a certain typing on an object. In this case, its fields must be of type <mark style="color:blue;">`number`</mark>.
{% endhint %}

Now, in the <mark style="color:blue;">`UsersController`</mark>, in the <mark style="color:blue;">`findAll()`</mark> route, add the following parameter and then pass it along as argument to the homonym method's call just below.

```typescript
@Query() paginationDto: PaginationDto
```

Finally, in the <mark style="color:blue;">`UsersService`</mark>, add the parameter also in its own <mark style="color:blue;">`findAll()`</mark>.

```typescript
paginationDto: PaginationDto
```

At the start of the method, extract the fields from the object.

```typescript
const { limit, offset } = paginationDto;
```

And then, in the <mark style="color:blue;">`find()`</mark> invocation of the repository, create an options object. In it, associate the extracted fields with the pagination options.

```typescript
{
  skip: offset,
  take: limit ?? DefaultPageSize.USERS,
}
```

{% hint style="info" %}
If <mark style="color:blue;">`offset`</mark> is not sent, the search will start from the **beginning**. If <mark style="color:blue;">`limit`</mark> is not sent, the **default page size** will be used.
{% endhint %}

With this, we have basic pagination working.

{% hint style="info" %}
In a real-world application, when searching for many records in the database, generally there should also be:

* Pagination metadata
* Filtering and sorting functionality

We'll learn how to implement and architect these features in the **Extra module 5** - **Advanced Querying**.
{% endhint %}

<mark style="color:green;">**Commit**</mark> - Implementing pagination
