ppetzold / nestjs-paginate

Pagination and filtering helper method for TypeORM repositories or query builders using Nest.js framework :book::paperclip:
MIT License
428 stars 93 forks source link

NestJS decorators stop working if a module with Paginate decorator isn't imported first #864

Open renatosantos0611 opened 7 months ago

renatosantos0611 commented 7 months ago

I don't know if nestjs-paginate should work like this, but all controllers of any Module that don't have any Paginate decorators give undefined value in nestjs decorators like @Body, @Param, @Query and @Req ... when import first than Module that have Paginate decorators, I create a simple example of that

import { Controller, Get } from '@nestjs/common';
import { Paginate, PaginateQuery } from 'nestjs-paginate';

@Controller('with-paginate')
export class WithPaginateController {
  @Get()
  findAll(@Paginate() query: PaginateQuery) {
    return query;
  }
}
import { Body, Controller, Get, Param, Post } from '@nestjs/common';

@Controller('without-paginate')
export class WithoutPaginateController {
  @Post()
  create(@Body() body: any) {
    return body;
  }

  @Get(':id')
  findAll(@Param('id') id: string) {
    return id;
  }
}

in a app.module.ts

import { Module } from '@nestjs/common';
import { WithoutPaginateModule } from './without-paginate/without-paginate.module';
import { WithPaginateModule } from './with-paginate/with-paginate.module';

@Module({
  imports: [WithoutPaginateModule, WithPaginateModule],
})
export class AppModule {}

WithoutPaginateModule imports first than WithPaginateModule, so all nestjs decorators from WithoutPaginateModule controllers get undefined value, importing WithPaginateModule first, everything works. Should the sequence of imports really matter? Did I forget something?

here example https://github.com/renatosantos0611/nestjs-paginate-query-issue

Thanks for help ;D

Helveg commented 6 months ago

Please take a look at the README: https://github.com/ppetzold/nestjs-paginate?tab=readme-ov-file#code

In there you can see that there are 2 steps:

  1. In the controller, you MUST apply the @Paginate() query: PaginateQuery parameter decorator.
  2. You MUST use the paginate function, passing it the query, repository, and pagination configuration.

Your code does not seem to have these steps implemented properly. Feel free to ask more questions! :)

renatosantos0611 commented 6 months ago

Please take a look at the README: https://github.com/ppetzold/nestjs-paginate?tab=readme-ov-file#code

In there you can see that there are 2 steps:

  1. In the controller, you MUST apply the @Paginate() query: PaginateQuery parameter decorator.
  2. You MUST use the paginate function, passing it the query, repository, and pagination configuration.

Your code does not seem to have these steps implemented properly. Feel free to ask more questions! :)

I think you didn't understand the problem or maybe I didn't convey it clearly, I use this lib in all my projects and works good, but you can try it yourself or use my example, you need only to create a Module containing controllers without using this lib, and make that Module import first in your main Module, and see every controllers of your entire application that don't use this lib stopping work.

Helveg commented 6 months ago

Ok, could you show your example as a complete minimal reproducible example? I'll link an explanation on MREs https://stackoverflow.com/help/minimal-reproducible-example

In its current form I can't tell what you mean from the code, and a text description of code is hard to follow 😅

you need only to create a Module containing controllers without using this lib, and make that Module import first in your main Module, and see every controllers of your entire application that don't use this lib stopping work.

Could you do this for me and share a link to the repository, so that I can execute the code and see it in action? Thanks!