Set custom field for keyset pagination

My resource has a primary key string attribute that is generated manually and is a hash of some other stuff.

That hash is not sortable, so I added the sortable?: false option to it and it shouldn’t be used for sorting and keyset pagination.

I also have another field that is not a primary key but it does have a UUID V7 in it and can be used for sorting the data.

What I want to do is, somehow, configure Ash to use that field instead of the primary key when doing keyset pagination.

Right now I will just get a Ash.Error.Query.UnsortableField error since, of course, the primary key is not sortable.

Is there some way to customize that so Ash would use a custom field for pagination instead of the primary key?

Hey there! So there are ways to do this currently, which is to add a sort to your action that uniquely identifies a record while paginating. However, I’ve added a configuration to the pagination config to set this explicitly, instead of having it default to “add the primary key if the sort is not stable”.

read :read do
  pagination do
    ...
    stable_sort [:your_custom_field]
  end
end

If there is not a stable sort (a set of fields that uniquely identify the record, i.e primary keys or identities) then that sort will be added. If not set, then the primary key is used. You can try this out in main if you like :slight_smile:

It will be released next week.

I just tested it, working great! Thanks!