Closed kingwill101 closed 3 years ago
Hello @glenfordwilliams, nice to hear from you.
Could you please provide some code snippets to show your use cases? With examples I can better understand your needs and come up more proper solution, thanks in advance 😃
Hi, thanks for responding. My goal is to be able to paginate data returned through a Raw union query.
unionQuery := backend.DB.Raw(
`
SELECT
*
FROM "table1"
WHERE
("table1"."recipient_id" = ?)
OR ("table1"."sender_id" = ?)
UNION ALL
SELECT
*
FROM "table2"
WHERE
("table2"."recipient_id" = ?)
OR ("table2"."sender_id" = ?)
UNION ALL
SELECT
*
FROM "table3"
WHERE
("table3"."recipient_id" = ?)
OR ("table3"."sender_id" = ?)
`,
ID, ID, ID, ID, ID, ID,
)
paginator := GetModelPaginator(*query)
result := paginator.PaginateScanned(unionQuery, &transactions)
cursor = paginator.GetNextCursor()
err = result.Error
i made a dirty solution to my problem https://github.com/pilagod/gorm-cursor-paginator/compare/master...glenfordwilliams:master
Hey @glenfordwilliams thanks for the code snippet! Did you find a solution or do we still need some work on this feature?
Hi, my modifications worked before i ended up having had to refactor. I imagine going forward it'll be something i'll need again.
Thanks @glenfordwilliams I ended refactoring mine as well to a Gorm like query. The reason why I went with a raw sql in the first place is because I needed to order by a calculated value and not a column name, which did not work with Gorm like queries. I eventually forked the package to include the possibility to override this value in the ORDER
section: https://github.com/mchaisse/gorm-cursor-paginator/commit/d0707cf6508a4001144edf714d2b648bd3073b9b This allowed me to eventually overcome every issue I had while trying to use "non-column" values in my paginated queries.
Note that this is a dirty patch, and I intent to come up with a cleaner approach to eventually open a PR.
cc @pilagod
Really appreciate @glenfordwilliams and @mchaisse's finding and help. Custom field name is definitely an important feature to support, especially for multiple tables joining together.
@mchaisse did provide a nice approach to this problem, an additional paginator configure method to override keys by each case is a really good idea.
I will keep thinking together with both of you to provide an easy-to-use interface for developers as much as possible.
I have just released v2 version and merged into master branch, please refer to new document on master branch to see if new api design fits your needs. Thanks!
One caveat here is that v2 version is based on GORM v2 instead of GORM v1. GORM v1 is not actively maintained, and maybe this is a chance to migrate to GORM v2 together.
Feel free to let me know any questions, thanks!
I have a use case where instead of
DB.Find
on the Model instance i useDB.scan