Skip to content

Conversation

@ks16
Copy link

@ks16 ks16 commented Apr 13, 2022

We want to use UUIDs for our cursors, whilst they are string formatted for use in URLs and the pagination parameters, they are stored as binary in our database for efficiency. The casting back and forth is ordinarily handled by model casts, but this paginator passes the string value to the database query without casting.

Proposed Solution:
Add the ability to define a callable to decode/deserialise the incoming cursor values before they are passed to the database queries.

Example:

CursorPagination::make()->withCursorColumn('uuid')->withKeyDecoder(Uuid::fromString($value)->getBytes())

@lindyhopchris
Copy link
Contributor

@ks16 sorry for the slow response.

Thanks for submitting this, it's a really good idea. I think though it can be simpler. The ID field in the schema already has a decode() method on it if it is decodable. As the cursor paginator is using IDs, I think it's best we inject the ID Field from the schema into the paginator. That way it could then decode the ids as you're suggesting.

Does that sound like an acceptable approach? If you're not sure how to update your PR to do that, let me know as I can probably implement it very quickly!

@lindyhopchris
Copy link
Contributor

@ks16 just wanted to check in with you about this PR.

I believe you should be using ID encoding/decoding to handle converting string JSON:API ids to binary values when querying the database. That's documented here:
https://laraveljsonapi.io/docs/2.0/schemas/identifier.html#custom-id-encoding

If you are, then I've put together a feature branch in this repo that adds handling ID encoding/decoding to the cursor paginator. To use in your schema, you would just need to do the following:

public function pagination(): CursorPagination
{
    return CursorPagination::make($this->id());
}

To give it a go, run the following:

composer require "laravel-json-api/cursor-pagination:dev-feature/id-decoding"

Let me know whether this works for your scenario.

@lindyhopchris
Copy link
Contributor

I released the id decoding in 2.1.0. Note that 3.0.0 will make it compulsory to pass the id field to the paginator.

@SofLesc SofLesc deleted the feat/add_support_for_cursor_key_decoder_callback branch July 20, 2023 12:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants