# @ntheanh201/nestjs-sequelize-pagination

> NestJS Sequelize Pagination

Latest version **1.1.2** (published 2023-03-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install @ntheanh201/nestjs-sequelize-pagination
pnpm add @ntheanh201/nestjs-sequelize-pagination
yarn add @ntheanh201/nestjs-sequelize-pagination
bun add @ntheanh201/nestjs-sequelize-pagination
```

## Health

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

Positive: no vulnerabilities; high maintenance score.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2023-03-23 |
| First published | 2022-09-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 20 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | The Anh Nguyen |
| Maintainers | ntheanh201 |
| Keywords | nestjs  sequelize  pagination  ntheanh201 |

## Links

- npm: https://www.npmjs.com/package/@ntheanh201/nestjs-sequelize-pagination
- Repository: https://github.com/ntheanh201/nestjs-sequelize-pagination
- Homepage: https://github.com/ntheanh201/nestjs-sequelize-pagination#readme
- Issues: https://github.com/ntheanh201/nestjs-sequelize-pagination
- npm.io page: https://npm.io/package/@ntheanh201/nestjs-sequelize-pagination

## Dependencies (2)

- [@nestjs/swagger](https://npm.io/package/@nestjs/swagger.md) ^6.1.2
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13

## Recent versions

- 1.1.2 (latest) — 2023-03-23
- 1.1.1 — 2022-09-28
- 1.1.0 — 2022-09-28
- 1.0.9 — 2022-09-28
- 1.0.8 — 2022-09-28

## README

<h1 align="center">
</h1>
<div align="center">
  <a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="120" alt="Nest Logo" /></a>
</div>
<h3 align="center"> 
  NestJS Sequelize Pagination
</h3>

<div align="center">
  <a href="https://nestjs.com" target="_blank">
    <img src="https://img.shields.io/badge/built%20with-NestJs-red.svg" alt="Built with NestJS">
  </a>
</div>

## @ntheanh201/nestjs-sequelize-pagination

A pagination module for NestJS and Sequelize, inspired by **[dw-nest-sequelize-pagination](https://www.npmjs.com/package/dw-nest-sequelize-pagination)** with deeper customization


## Installation

```bash
$ npm install @ntheanh201/nestjs-sequelize-pagination
```

```bash
$ yarn add @ntheanh201/nestjs-sequelize-pagination
```

## Getting Started

### Import

Import and add StripeModule to the imports section of the consuming module (most likely AppModule).

```ts
import { PaginationModule } from '@ntheanh201/nestjs-sequelize-pagination';

@Module({
  imports: [PaginationModule.forRoot({ isGlobal: true })],
})
export class AppModule {}
```

### Configuration

This module support forRoot patterns for configuration, with values:

| Name           | Description                                         | Type      | Default |
| -------------- | --------------------------------------------------- | --------- | ------- |
| isGlobal       | Use module globally                                 | _boolean_ | `true`  |
| limit          | The number of rows returned                         | _number_  | `10`    |
| page           | The page to start pagination, one-based indexing    | _number_  | `1`     |
| orderBy        | The key sorting returned data                       | _string_  | `null`  |
| orderDirection | The sorting direction (ASC, DESC, NULLS FIRST, ...) | _string_  | `null`  |

### Service
Sequelize service override findAll method from Sequelize and allow you to handle pagination automaticaly.


```ts
import { Injectable } from '@nestjs/common';
import { Includeable, Op } from 'sequelize';
import {
  PaginationQuery,
  PaginationResponse,
  PaginationService,
} from '@ntheanh201/nestjs-sequelize-pagination';

@Injectable()
export class ProductService {
  constructor(private paginationService: PaginationService) {}

  findAll(
    paginationOptions: PaginationQuery,
    include: Includeable | Includeable[] = [],
  ): Promise<PaginationResponse<Product>> {
    let whereCondition;
    const keySearch = paginationOptions?.searchKey;
    if (keySearch) {
      whereCondition = {
        [Op.or]: [
          { sku: { [Op.like]: `%${keySearch}%` } },
          { barcode: { [Op.like]: `%${keySearch}%` } },
          { name: { [Op.like]: `%${keySearch}%` } },
        ],
      };
    }

    return this.paginationService.findAll(
      {
        ...paginationOptions,
        model: Product,
      },
      {
        where: whereCondition,
        include,
      },
    );
  }
}
```

### Controller

```ts
@Controller('/products')
export class ProductController {
  constructor(private readonly productService: ProductService) {}

  @Get()
  @ApiOperation({ summary: 'Get products' })
  getProducts(
    @Pagination({
      limit: 10,
      page: 0,
      orderBy: 'createdAt',
      orderDirection: 'DESC',
      searchKey: 'pro',
    })
    pagination: PaginationQuery,
  ): Promise<PaginationResponse<Product>> {
    return this.productService.findAll(pagination);
  }
}
```

## Contributing

Contributions welcome! See [Contributing](CONTRIBUTING.md).

## Stay in touch

**The Anh Nguyen ([Facebook](https://facebook.com/ntheanh201))**

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