pilagod / gorm-cursor-paginator

A paginator doing cursor-based pagination based on GORM
https://github.com/pilagod/gorm-cursor-paginator
MIT License
192 stars 44 forks source link

How to paginate a gorm.Association? #40

Closed pabloos closed 2 years ago

pabloos commented 2 years ago

Is there any way to paginate gorm associations? Associations also uses Find to query for results, but the type isn't compatible (as the Paginate method requires a gorm.DB).

pilagod commented 2 years ago

Hello @pabloos,

This is a good question. Honestly, I'm not familiar with the usage of Association. As far as I know, Paginator will dynamically append cursor information into db query through Where chaining, but Association doesn't seem to support this ability.

I have two ideas for your reference:

  1. I see there is a DB field on Association, which has the type *gorm.DB. I'm not sure if this DB keeps all the settings for the query and associations added before. Maybe you can give it a try to see if it works.
  2. Use paginator to find only primary keys first, then use these primary keys to query full data. This is a trick when you have a complicate query that doesn't fit into paginator properly. Although this costs two queries, the second one can leverage primary key index to reduce the overhead.

Please let me know if any of these ideas solves your situations 🙂

pilagod commented 2 years ago

I would close this issue first. Feel free to reopen it for further discussion 😃