SpoonX / sails-hook-wetland

A sails hook that enables the use of wetland ORM.
MIT License
9 stars 4 forks source link

Getting no countInResponse value if offset is given as option #22

Closed brniamut closed 6 years ago

brniamut commented 6 years ago

How to recreate this bug:

  1. Use 'mySQL' as database
  2. Set the countInResponse: option (For instance X-Total-Count)
  3. Create a Entity (For instance users) in Sails
  4. Create a Controller (For instance `usersController) in Sails
  5. Seed the users table with 100 users
  6. Try a GET call on the users resource (http://local.dev/users?limit=10). 😃 Yay it works, I get a value in the header
  7. Try a GET call on the users resource with an offset (http://local.dev/users?limit=10&offset=20) 😭, The header value is null

Why is this happening:

In the https://github.com/SpoonX/sails-hook-wetland/blob/master/lib/count.js file on line 13 options are added. If offset is set on one of those options you get the result null.

When the query is generated it's select COUNT(*) from "user" as "user" limit 10 offset 10, mySQL then decides that it doesn't like this and gives you no results.

Can you solve it? This seems to be more a problem with wetland itself (or knex.js). One solution is to use SQL_CALC_FOUND_ROWS instead of Count when using mySQL. But I have no idea where to start.

How did you solve it now I just take the options object and remove the offset attribute form it. This seems like a really hacky solution and I don't think it's PR worthy. Maybe the god maintainer of this project can assist me in writing a fix.

brniamut commented 6 years ago

My shitty fix btw:

https://github.com/brniamut/sails-hook-wetland/commit/b29dbc0fc62cc44d2d4fa0eba57885b81272bccd

RWOverdijk commented 6 years ago
let countOptions = new Homefront();

countOptions.merge(options);

countOptions
  .remove('limit')
  .remove('offset');

repository.applyOptions(queryBuilder, countOptions);