Pagination issue - not getting the “before” and “after” cursors

I’m trying to add pagination to the resources.

There’s an action for the ResourceA

read :list do
  pagination(keyset?: true, default_limit: 1, countable: true)
end

Calling API.read_all!(ResourceA) returns all 10+ records from db.

Calling ResourceA.list!(), I get the following result:

%Ash.Page.Keyset{
  results: [
    #ResourceA<
      __meta__: #Ecto.Schema.Metadata<:loaded, "patios">,
      id: "37d49fa9-d8d1-4288-b9c0-57a1a2be77b7",
      inserted_at: ~U[2024-02-07 13:38:58.000000Z],
      updated_at: ~U[2024-02-07 13:38:58.000000Z],
      aggregates: %{},
      calculations: %{},
      ...
    >
  ],
  count: nil,
  before: nil,
  after: nil,
  limit: 1,
  rerun: {#Ash.Query<
     resource: ResourceA,
     sort: [id: :asc],
     limit: 251,
     select: [:id, :inserted_at, :updated_at]
   >,
   [
     page: [limit: 1],
     verbose?: false,
     actor: nil,
     authorize?: false,
     return_query?: false
   ]},
  more?: true
}

Calling ResourceA.list!(page: [limit: 1]), I get the following result:

%Ash.Page.Keyset{
  results: [
    #ResourceA<
      __meta__: #Ecto.Schema.Metadata<:loaded, "patios">,
      id: "37d49fa9-d8d1-4288-b9c0-57a1a2be77b7",
      inserted_at: ~U[2024-02-07 13:38:58.000000Z],
      updated_at: ~U[2024-02-07 13:38:58.000000Z],
      aggregates: %{},
      calculations: %{},
      ...
    >
  ],
  count: nil,
  before: nil,
  after: nil,
  limit: 1,
  rerun: {#Ash.Query<
     resource: ResourceA,
     sort: [id: :asc],
     limit: 2,
     select: [:id, :inserted_at, :updated_at]
   >,
   [
     page: [limit: 1],
     verbose?: false,
     actor: nil,
     authorize?: false,
     return_query?: false
   ]},
  more?: true
}

Why am I not getting the “before” and “after” cursors? What am I doing wrong?

Thanks.

The before and after attributes contain the originally passed in before or after (admittedly a bit confusing). If you want to get the keyset for the next page, it would be the last record in the results list .__metadata__.keyset. Keep in mind if you want to paginate in memory, you can use Api.page(page, :next) to avoid this work yourself.

1 Like

Thanks, @zachdaniel. Is the API.page function new? I didn’t know about that. In fact, I just read about that in the docs.

Not new, but I’m not sure if it’s in any guides or not.

It is in Pagination — ash v2.18.2.


I don’t know why I missed that in the first time I read the docs.