Search code examples
javascriptnestjsbackendtypeorm

How to add pagination in TypeORM nestjs?


As of now below method returning all the record. I need to return at a time 10 records. Please suggest what modification I need to do.

public async allSuppliersForBid(
    filter: FilterSupplierDto,
  ): Promise<[Supplier[], number]> {
    const { limit, page, query } = filter;
    const supplier = await this.supplierRepos.query(
      `SELECT * FROM suppliers WHERE (suppliers."fullName" ILIKE '%${query}%' OR suppliers."companyName" ILIKE '%${query}%' OR suppliers.phone LIKE '%${query}%') AND (suppliers.status = 'active' AND suppliers."isVerified" = 'true' AND suppliers."isEnabled" = 'true' AND "assignCluster"<>'{}'::jsonb)`,
    );
    const count = supplier.length;
    return [supplier, count];
  }

this is Pagination file.

export class Pagination {
  @IsOptional()
  @IsPositive()
  @IsNumber()
  page: number = 1;

  @IsOptional()
  @IsPositive()
  @IsNumber()
  limit: number = 10;
}

I need to introduce pagination in this method. Any help would be really helpful.


Solution

  • controller.ts

    @ApiBearerAuth()
        @ApiQuery({
            name: 'key',
            type: String,
            required: false,
          })
        @ApiOkResponse({
            status: HttpStatus.OK,
            description: "",
        })
        @HttpCode(HttpStatus.OK)
        @Get("inactive-user-pagination")
        findAllInActiveUser(
        @Query('limit', new IntValidationPipe()) limit: number,
        @Query('page', new IntValidationPipe()) page: number,
        @Query("key") key: string | ""
        ): Promise<ResponseDto> {
        const userDto = this.userService.findAllInActiveUser(
          page,
          limit,
          key
          );
        return this.responseService.toPaginationResponse(
            HttpStatus.OK,
            'Successful',
            page,
            limit,
            userDto,
          )
        }
    

    service.ts

    findAllInActiveUser =  async (page: number, limit: number,key:string): Promise<any> => {
            try {
              key = key ? key : "";
              let users = await this.userRepository.query(
                  "rcon_get_all_inactive_users_pagination @page='" +
                  page +
                  "', @limit='" +
                  limit +
                  "', @key='" +
                  key +
                  "'"
                );
                    users.map((data) => {
                        delete data.password;
                    });
                const count = await this.userRepository.find({
                    where: {...isInActive}
                });
                
                return this.conversionService.toPagination<
                UserEntity,
                UserDto
              >([users,count.length]);
    
                } catch (error) {
                    throw new SystemException(error);
                }
            };
    

    You can get a view of how to create a pagination