Skip to content

Conversation

@code-troll
Copy link

@code-troll code-troll commented Dec 10, 2025

Summary

  • Fix pagination counts with joined query builders by relying on TypeORM's getCount() so totals are de-duplicated like getMany.
  • Add a regression test that covers a join producing duplicate rows to ensure totals match the distinct root entities.

Problem

  • The library cloned the query builder and ran SELECT COUNT(*) FROM (<join query>) for totals.
  • When joins multiply rows per entity (e.g., many-to-many or one-to-many), the page results were distinct but the total used the raw row count, inflating totalItems/totalPages and creating empty pages.

Change

  • Clear pagination/order clauses on the cloned builder and call getCount(); TypeORM injects the same DISTINCT-on-PK logic used for paged selects.
  • Added paginate.query.builder.distinct-count.spec.ts to prove joined queries now report correct totals.

Side Effects / Compatibility

  • Totals on joined queries will drop to the correct distinct-entity count; if any consumers relied on the old overcounted totals, they will see smaller numbers (desired fix).
  • Behavior now follows TypeORM getCount() semantics (e.g., honors groupBy the same way TypeORM does) and still issues one count query plus one data query.

Signed-off-by: Franco J. Carreras <francoca87@gmail.com>
@code-troll code-troll force-pushed the fix/pagination-counts-with-joined-query-builders/1 branch from 0dc8988 to 35230ea Compare December 10, 2025 13:10
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

Successfully merging this pull request may close these issues.

1 participant