How to implement robust pagination with a RESTful

2019-03-28 01:12发布

I'm implementing a RESTful API which exposes Orders as a resource and supports pagination through the resultset:

GET /orders?start=1&end=30

where the orders to paginate are sorted by ordered_at timestamp, descending. This is basically approach #1 from the SO question Pagination in a REST web application.

If the user requests the second page of orders (GET /orders?start=31&end=60), the server simply re-queries the orders table, sorts by ordered_at DESC again and returns the records in positions 31 to 60.

The problem I have is: what happens if the resultset changes (e.g. a new order is added) while the user is viewing the records? In the case of a new order being added, the user would see the old order #30 in first position on the second page of results (because the same order is now #31). Worse, in the case of a deletion, the user sees the old order #32 in first position on the second page (#31) and wouldn't see the old order #31 (now #30) at all.

I can't see a solution to this without somehow making the RESTful server stateful (urg) or building some pagination intelligence into each client... What are some established techniques for dealing with this?

For completeness: my back-end is implemented in Scala/Spray/Squeryl/Postgres; I'm building two front-end clients, one in backbone.js and the other in Python Django.

5条回答
不美不萌又怎样
2楼-- · 2019-03-28 01:21

WebSockets can do this. You can use something like pusher.com to catch realtime changes to your database and pass the changes to the client. You can then bind different pusher events to work with models and collections.

查看更多
祖国的老花朵
3楼-- · 2019-03-28 01:22

Just Going to throw it out there. Please feel free to tell me if it's completely wrong and why so.

This approach is trying to use a left_off variable to sort through without using offsets.

Consider you need to make your result Ordered by timestamp order_at DESC. So when I ask for first result set it's

SELECT * FROM Orders ORDER BY order_at DESC LIMIT 25;

right? This is the case when you ask for the first page (in terms of URL probably the request that doesn't have any

yoursomething.com/orders?limit=25&left_off=$timestamp

Then When receiving your data set. just grab the timestamp of last viewed item. 2015-12-21 13:00:49

Now to Request next 25 items go to: yoursomething.com/orders?limit=25&left_off=2015-12-21 13:00:49 (to lastly viewed timestamp)

In Sql you would just make the same query and say where timestamp is equal or less than $left_off

SELECT * FROM (SELECT * FROM Orders ORDER BY order_at DESC) as a 
WHERE a.order_at < '2015-12-21 13:00:49' LIMIT 25;

You should get a next 25 items from the last seen item.

For those who sees this answer. Please comment if this approach is relevant or even possible in the first place. Thank you.

查看更多
Evening l夕情丶
4楼-- · 2019-03-28 01:33

The sad truth is that all the sites I see have pagination "broken" in that sense, so there must not be an easy way to achieve that.

A quick workaround could be reversing the ordering, so the position of the items is absolute and unchanging with new additions. From your front page you can give the latest indices to ensure consistent navigation from up there.

  • Pros: same url gives the same results
  • Cons: there's no evident way to get the latest elements... Maybe you could use negative indices and redirect the result page to the absolute indices.
查看更多
兄弟一词,经得起流年.
5楼-- · 2019-03-28 01:34

With a RESTFUL API, Application state should be in the client. Here the application state should some sort of time stamp or version number telling when you started looking at the data. On the server side, you will need some form of audit trail, which is properly server data, as it does not depend on whether there have been clients and what they have done. At the very least, it should know when the data last changed. No contradiction with REST here.

You could add a version parameter to your get. When the client first requires a page, it normally does not send a version. The server replies contains one. For instance, if there are links in the reply to next/other pages, those links contains &version=... The client should send the version when requiring another page.

When the server recieves some request with a version, it should at least know whether the data have changed since the client started looking and, dependending of what sort of audit trail you have, how they have changed. If they have not, it answer normally, transmitting the same version number. If they have, it may at least tell the client. And depending how much it knows on how the data have changed, it may taylor the reply accordingly.

Just as an example, suppose you get a request with start, end, version, and that you know that since version was up to date, 3 rows coming before start have been deleted. You might send a redirect with start-3, end-3, new version.

查看更多
We Are One
6楼-- · 2019-03-28 01:35

The way I'd do it, is to make the indices from old to new. So they never change. And then when querying without any start parameter, return the newest page. Also the response should contain an index indicating what elements are contained, so you can calculate the indices you need to request for the next older page. While this is not exactly what you want, it seems like the easiest and cleanest solution to me.

Initial request: GET /orders?count=30 returns:

{
  "start"=1039;
  "count"=30;
  ...//data
}

From this the consumer calculates that he wants to request:

Next requests: GET /orders?start=1009&count=30 which then returns:

{
  "start"=1009;
  "count"=30;
  ...//data
}

Instead of raw indices you could also return a link to the next page:

{
  "next"="/orders?start=1009&count=30";
}

This approach breaks if items get inserted or deleted in the middle. In that case you should use some auto incrementing persistent value instead of an index.

查看更多
登录 后发表回答