# @bit-architect/nestjs-paginator

> ## Installation

Latest version **1.0.3** (published 2023-08-23) · 0 weekly downloads

## Install

```sh
npm install @bit-architect/nestjs-paginator
pnpm add @bit-architect/nestjs-paginator
yarn add @bit-architect/nestjs-paginator
bun add @bit-architect/nestjs-paginator
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2023-08-23 |
| First published | 2023-08-22 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 12.7 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | johannes.schobel |

## Links

- npm: https://www.npmjs.com/package/@bit-architect/nestjs-paginator
- npm.io page: https://npm.io/package/@bit-architect/nestjs-paginator

## Dependencies (2)

- [tslib](https://npm.io/package/tslib.md) ^2.3.0
- [@nestjs/common](https://npm.io/package/@nestjs/common.md) 10.1.3

## Recent versions

- 1.0.3 (latest) — 2023-08-23
- 0.0.1 — 2023-08-22

## README

# nestjs-paginator

## Installation

Add the library via

```
npm i @bit-architect/nestjs-paginator
```

or

```
yarn add @bit-architect/nestjs-paginator
```

## Usage

The library provides a handy decorator that can be added to any NestJS controller. Consider the following example:

```ts
import { Paginator, PaginatorQuery } from '@bit-architect/nestjs-paginator';
import { Controller, Get } from '@nestjs/common';
import { UserService } from './user.service';

@Controller('users')
export class UserController {
  constructor(private service: UserService) {}

  @Get()
  getUsers(@Paginator() query: PaginatorQuery) {
    // console.log(query);
    return await this.service.getUsers(query);
  }
}
```

Currently, the `Paginator` supports the following query parameters:

- `page`: number
- `limit`: number
- `sort`: string
- `filter`: object

Now, you can make a HTTP Call to the endpoint and pass the params as follows:

```curl
http://api.example.com/users?limit=20&page=3&sort=-email,username
```

This will return

- 20 users
- from the 3rd page (i.e., entry 21-30 from the database according to your `sort` and `filter` parameters)
- ordered by email descending (note the `-`) and username ascending

### Different Format

The `Paginator` is designed to be ORM agnostic, i.e., it can be easily extended / adapted to work with common ORMs, like `TypeORM` or `Prisma`.

The easiest approach would be to simply create a `PrismaPaginator` Decorator and call the `PaginatorService`, and adapt the response as required.
This may look like this:

```ts
import { PaginatorService, PaginatorOptions } from '@bit-architect/nestjs-paginator';
import { createParamDecorator, ExecutionContext } from '@nestjs/common';

import { PrismaPaginatorQuery } from './prisma-paginator-query.model';

export const PrismaPaginator = createParamDecorator((data: Partial<PaginatorOptions>, ctx: ExecutionContext) => {
  const request = ctx.switchToHttp().getRequest();
  const query = new PaginatorService().parseQuery(request.query, data);

  const prismaQuery: PrismaPaginatorQuery = {
    skip: query.skip,
    take: query.take,
    orderBy: query.sort, // note that prisma uses the orderBy instead of sort parameter!
  };

  return prismaQuery;
});
```

Now you can just use the `PrismaPaginator` instead of the default `Paginator` in your controllers.

---
_Source: https://npm.io/package/@bit-architect/nestjs-paginator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
