nestjsx / nestjs-typeorm-paginate

:page_with_curl: Pagination response object function + types for typeorm + nestjs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use paginate function with custom option parameters

lucasbbsdk opened this issue · comments

I want to be able to use function paginate, but being able to remain my current filters query params.

For instance, I have the following endpoint:

  @Get("/")
  async index(
    @Query('page', new DefaultValuePipe(1), ParseIntPipe) page: number = 1,
    @Query('limit', new DefaultValuePipe(10), ParseIntPipe) limit: number = 10,
    @Query('type') type: string,
  ): Promise<Pagination<Pokemons>> {
    limit = limit > 100 ? 100 : limit;
    return this.pokemonService.paginate({
      page,
      limit,
      type,
      route: 'http://localhost:3000/pokemons',
    });
  }

As you can see, I have added the option type over there...
But I cannot see this reflected on the links object when browsing in the api.

  "links": {
    "first": "http://localhost:3000/pokemons?limit=10",
    "previous": "",
    "next": "http://localhost:3000/pokemons?page=2&limit=10",
    "last": "http://localhost:3000/pokemons?page=7&limit=10"
  }

I would like to see it for example when applying the query params of type=fire:
"next": "http://localhost:3000/pokemons?page=2&limit=10&type=fire",

interface IOptions extends IPaginationOptions {
  type: string
}
  async paginate(options: IOptions): Promise<Pagination<Pokemons>> {
    const queryBuilder = this.repo
      .createQueryBuilder();
    if (options.type) {
      queryBuilder.where("(type_1 = :type OR type_2 = :type)", { type: options.type })
    }


    return paginate<Pokemons>(queryBuilder, options);
  }

What am I doing wrong here?

I see what you're trying to do. The links should really be added to the transform meta method so you can customise the links. For now you can do this manually but I'll look at add this in as a feature
https://github.com/nestjsx/nestjs-typeorm-paginate?tab=readme-ov-file#custom-meta-data-transformer