nestjs-paginate icon indicating copy to clipboard operation
nestjs-paginate copied to clipboard

Filter does not work with QueryBuilder

Open MaximilianOtto opened this issue 2 years ago • 3 comments

If I use a querybuilder, the filters doesnt work. Is this not supported by default?

MaximilianOtto avatar Jun 24 '22 07:06 MaximilianOtto

The problem here is that the filter is attached to the where clause at the end. Using an orWhere can lead to problems. As a workaround, I only used a where clause and separated it from the added filter with addittional brackets.

MaximilianOtto avatar Jun 24 '22 21:06 MaximilianOtto

hi there, filter using query builder isn't currently covered by any test - but it might work. could you elaborate in more detail the issue? (code lines + generated SQL)

ppetzold avatar Jun 27 '22 07:06 ppetzold

// does not work

const query = this.getBasicDocumentQuery();
    query
      .where('document.id IN (' + this.someRandomCheck()+ ')', {
        id,
      })
      .orWhere(
        'document.id IN (' +
          this.someOtherRandomCheck() +
          ')',
        { id },
      );
// is working

const query = this.getBasicDocumentQuery();
    query.where(
      '(document.id IN (' +
        this.someRandomCheck() +
        ') OR document.id IN (' +
        this.someOtherRandomCheck() +
        '))',
      {
        id,
      },
    );

This happened because the filter just add a "AND filterField = filter" at the end of the query. Just add the extra brackets and it will work. For me this workaround is fine for now. Not sure if I find the time for a pull request

MaximilianOtto avatar Jun 27 '22 08:06 MaximilianOtto

works. see https://github.com/ppetzold/nestjs-paginate/commit/ab24b887025313a4e5d12ac532ab0bad1e13c7d9

ppetzold avatar Mar 24 '23 18:03 ppetzold