Closed nikicc closed 2 years ago
Totals | |
---|---|
Change from base Build 197: | 0.2% |
Covered Lines: | 362 |
Relevant Lines: | 372 |
I have no further questions and I think this PR is ready to merge. 💪 You can check again if there is anything missing. If everything is ok, then I will merge this PR. 🎉
I just pushed one more tiny fix in one of the comments. With that, I think this is ready to be merged 🎉
It is fabulous to support new custom type without affecting current users.
I'm excited as well. Will start using this for our JSON pagination as soon as the release is out.
@nikicc Thanks you for this great work 💪
Thank you for all the feedback & reviewing 🙏
No problem. Let's merge it and do an upgrade 😎
Fixes #42.
Description
This is a WIP implementation of enabling pagination across data inside JSON columns. @pilagod I wanted to get your thoughts on this to see if this is something you are comfortable proceeding with. Please let me know 🙏
If we are happy to proceed, I would add the tests for the new functionality and document it as well in the readme.
How to test
1. Define the Custom Type
First we need to define our type for JSON which also implements the
CustomTypePaginator
interface. Something like this should do:2. Define GORM model & data structure
Let our struct be like this:
And let the data field in the DB contains JSONs like this:
3. Define pagination rules
To paginate on
k1
ascending, we would need a rule like this:To paginate on
k2
descending, we would need a rule like this:Concerns
CustomType
struct needs a few extra information. In order to be able to type cast in the encoder, decoder and SQL, we need to provide:meta
: the meta attribute path on the custom type objects (in case of JSONs, this would be the key)type
: Golang type for the decoded to be able to cast it to a correct typeSQLType
: to make type casting in SQL. By default, the when you use JSON operators like#>>
the returned type will be JSON (see the note in the docs), hence in order for comparisons to work correctly we need to type cast it to the correct type.