Open jonasraoni opened 3 years ago
If the idea is welcome I can create a PR, I already have a working code that I've written to fix my issues (a kind of chunkById
that uses a generator to pass through the pages and supports multiple columns + variable sorting).
In case someone has the same needs as me, I've created an extension package for myself with the features I needed, I hope it will become deprecated later 👀 Package: https://packagist.org/packages/jonasraoni/query-builder-extensions Repository: https://github.com/jonasraoni/query-builder-extensions
Problem
Right now there are methods that provide this feature, such as the
forPageAfterId
,forPageBeforeId
andchunkById
, but I had issues with them:forPageAfterId
/forPageBeforeId
: As it requires me to pass the last key, the code becomes a bit verbose.chunkById
: This method addresses the previous complaint, but the use of a callback makes it not that great to use. I think this should have been implemented as agenerator
/iterator
, then I'd be able to process it on demand instead of moving all the logic inside a callback, which in my specific case isn't feasible (I've left my use case below).ORDER BY section_id, post_id
, I'd have to go with tricks such asCONCAT(section_id, '-', post_id)
... But this is limited (I'm unable to to sort one column byASC
and another byDESC
) and slower, as the database won't be able to use indexes.chunkById
use case:I'm using an open-source library that processes tasks in parallel, after feeding an initial set of data to it, I can call a
$pool->wait($callback)
which internally is awhile(!$finished) callback(...)
sending updates about the processing... Whenever a task is finished, I want to feed in more data, and it would be great to use thechunkById
, but:wait
(I've created an issue in the other repository to allow it), feed more data, then callwait
again (which would allow me to use the callback)chunkById
.Solution
generator
on thechunkById
(it could be a safe deprecation, such as introducing a new methodchunkByIds
)chunkById
,forPageAfterId
andforPageBeforeId
by allowing the user to specify more columns + sorting (thenforPageAfterId
andforPageBeforeId
could be even merged into a single method).