You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If params.knex is set, params.query is bypassed, and the knex query is executed insted. However, for the count query (ie total), the params.query is taken into account (instead of params.knex), and results in a wrong total.
I think this is inconsistant, shouldn't params.knex, if set, should be used to compute the count query?
No, that does not work. params.knex can be a complicated request, wheres this.db(params)) is just select * from table. The count on params.knex is not working.
Not really, as $search is not in the query params anymore and you can't really add it later for count since you no longer have the info. I suggest we do something like:
_find(params,count,getFilter=filter){const{ filters }=getFilter(params.query||{});constq=params.knex||this.createQuery(params);// define q2 before $limit or $skipconstq2=q.clone();// Handle $limitif(filters.$limit){q.limit(filters.$limit);}// Handle $skipif(filters.$skip){q.offset(filters.$skip);}letexecuteQuery=total=>{returnq.then(data=>{return{total: parseInt(total,10),limit: filters.$limit,skip: filters.$skip||0,
data
};});};if(filters.$limit===0){executeQuery=total=>{returnPromise.resolve({total: parseInt(total,10),limit: filters.$limit,skip: filters.$skip||0,data: []});};}if(count){letcountQuery=this.db(params).count(`${this.id} as total`);// use the exact same query as subquery and countcountQuery.from(q2.as(`${this.table}`));returncountQuery.then(count=>count[0].total).then(executeQuery);}returnexecuteQuery().catch(errorHandler);}
Just ran into this also. If I add a simple where clause to the query in a Before hook (as recommended by the readme) the total property returned is still the total number of records, not the number of records that would be returned by the filtered query.
Activity
daffl commentedon Oct 6, 2017
That makes sense. Can you try if it works changing https://github.com/feathersjs/feathers-knex/blob/master/src/index.js#L167 to
sylvainlap commentedon Oct 18, 2017
No, that does not work.
params.knexcan be a complicated request, wheresthis.db(params))is justselect * from table. Thecountonparams.knexis not working.daffl commentedon Oct 18, 2017
What is
.countreturning for a custom query? If that doesn't work I don't see how we can supporttotalat all in that case.sylvainlap commentedon Oct 19, 2017
Ok, you should precise that pagination (total, limit, skip) is not supported when params.knex is set.
I close the issue.
daffl commentedon Oct 19, 2017
I'd leave it open. We have to check if the query is customized and skip the count. Do you have an example customized query where the count is wrong?
sylvainlap commentedon Oct 20, 2017
Here is an example.
The hook code:
The result:
daffl commentedon Oct 20, 2017
I haven't tried it yet, but shouldn't
params.knex.count(${this.id} as total);return the correct total in this case?paflopes commentedon Nov 2, 2017
Not really, as
$searchis not in the query params anymore and you can't really add it later forcountsince you no longer have the info. I suggest we do something like:green3g commentedon Jan 23, 2018
Just ran into this also. If I add a simple where clause to the query in a Before hook (as recommended by the readme) the total property returned is still the total number of records, not the number of records that would be returned by the filtered query.
I'm using this as a simple case:
response:
{ "total": 5, "limit": 10, "skip": 0, "data": [{ "id": 4, "details": "This is a workorder", }] }carcinocron commentedon Feb 2, 2018
Here's what I've found.
I need this to work:
But pagination breaks, unless you also overide
_find:omit(params.query, ['$sort']),andclearSelect()being imporant,omitbeing from lodash.Why doesn't count use
params.knex || this.createQuery? That makes getting a count for a slightly customized query into a nightmare.daffl commentedon Feb 2, 2018
No reason. A pull request to change that would be very welcome.
Fix count query when using params.knex
Fix count query when using params.knex
Fix count query when using params.knex (#145)
roelvan commentedon May 3, 2018
Hi guys,
I still get a wrong total when I combine a sub query with a $sort:
I get a total of 1 while there are 5 documents. My request is:
localhost:3030/orders?$sort[totalInCents]=-1&$search=roel