27 Eylül 2023 Çarşamba

Pagination Yöntemleri

1. Offset pagination
Açıklaması şöyle
In this type of pagination, the client sends a request specifying the page number and the number of items per page. Ultimately, this translates into an SQL query using limit and offset.
Örnek
Şöyle bir istek gönderilir
GET /api/v1/bookings?page=1&size=3
2. Token pagination
Açıklaması şöyle
Token pagination can be implemented in different ways. I’ll show you one of them.

In the initial request, we send a typical search query. The response contains the token to retrieve the next portion of data.

In following requests, we send only the next token.

To avoid storing unnecessary states on the backend, we can embed the search query and the ID of the last returned item into a token. We can then compress the data using gzip or snappy and convert it, for instance, to base62.
Örnek
İlk istek ve cevabı şöyledir
/api/v1/logs?categories=bookings,orders&size=100&sort=createdAt:desc

Response
{ 
  data: [...], 
  next: "2H83GdysPu"
}
İkinci istek ve cevabı şöyledir
Request
/api/v1/logs?next=2H83GdysPu

Response
{ 
  data: [...], 
  next: "v95Gdkta3d"
}
Örnek - Token pagination from different sources
Açıklaması şöyle. Yani booking_logs ve  order_logs kayıtlarından kaldığı yerden 3 tane daha alırız ve toplam 6 kayıt göndeririz. Son gönderdiğimiz kayıtların ID'lerini de cevaba ilave ederiz.
Let’s say we have multiple sources: booking_logs and order_logs, and we need to return combined data from both sources.

With token pagination, we can embed the last returned ID from each source into the token. When executing a request for 3 items, we select 3 items from each source, and then sort them on the backend to determine which specific items to return.



Hiç yorum yok:

Yorum Gönder