feathers
feathers copied to clipboard
fix(knex): Fix total calculation
Summary
- [X] Tell us about the problem your pull request is solving.
- When the query contains
DISTINCTorGROUP BY, incorrecttotalis being returned. This PR fixes it by calculating the count using a subquery. More details below: - When
DISTINCTclause is used,clearSelect()removes theDISTINCTclause in the countBuilder query, hence returning non-distinct count. For eg:-
SELECT DISTINCT `users.*` FROM `users` WHERE .... - is converted to
-
SELECT `*` FROM `users` WHERE ... - losing the distinct property of the query.
-
- When
GROUP BYclause is used,GROUP BYis applied onCOUNT(`${name}.${id}`), which is useless, because rows returned byCOUNTwill not contain any column other thantotaltoGROUP BY. Hence non-distinct rows are returned again. - Hence the solution is, to wrap the main query into a subquery, and count the rows returned by it, which is what this PR does.
- When the query contains
- [ ] Are there any open issues that are related to this?
- No. But this is a bug, I can create an issue if necessary.
- [ ] Is this PR dependent on PRs in other repos?
- No