1.1.2 • Published 1 year ago

@ntheanh201/nestjs-sequelize-pagination v1.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@ntheanh201/nestjs-sequelize-pagination

A pagination module for NestJS and Sequelize, inspired by dw-nest-sequelize-pagination with deeper customization

Installation

$ npm install @ntheanh201/nestjs-sequelize-pagination
$ yarn add @ntheanh201/nestjs-sequelize-pagination

Getting Started

Import

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

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:

NameDescriptionTypeDefault
isGlobalUse module globallybooleantrue
limitThe number of rows returnednumber10
pageThe page to start pagination, one-based indexingnumber1
orderByThe key sorting returned datastringnull
orderDirectionThe sorting direction (ASC, DESC, NULLS FIRST, ...)stringnull

Service

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

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

@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.

Stay in touch

The Anh Nguyen (Facebook)

1.1.2

1 year ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago