Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use paginate function with custom option parameters #797

Open
ghost opened this issue Oct 25, 2022 · 1 comment
Open

How to use paginate function with custom option parameters #797

ghost opened this issue Oct 25, 2022 · 1 comment

Comments

@ghost
Copy link

ghost commented Oct 25, 2022

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?

@ghost ghost changed the title How to use paginate function with other option parameters How to use paginate function with custom option parameters Oct 25, 2022
@bashleigh
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant