Thuta Learning
API Tutorial
IntermediateWeb Developmentအလယ်အလတ်

Pagination/Filtering/Sorting

API Tutorialသင်ခန်းစာ 1335 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • List endpoint တွေက page တစ်ခုချင်း ပြန်ပေးရတဲ့ အကြောင်းနဲ့ limit, offset, cursor ရဲ့ အဓိပ္ပာယ်ကို ရှင်းပြနိုင်ဖို့
  • Data ပြောင်းနေချိန် offset pagination က row တွေ ထပ်ပြီး ဒါမှမဟုတ် ကျော်သွားတာကို ပြပြီး cursor (keyset) pagination က ဘာကြောင့် မဖြစ်လဲ ရှင်းပြနိုင်ဖို့
  • next_cursor ကို null မဖြစ်မချင်း လိုက်ပြီး server မှားနေရင် ရပ်တဲ့ loop ရေးနိုင်ဖို့
  • Filter နဲ့ sort parameter တွေကို allowlist, တည်ငြိမ်တဲ့ tie-breaker နဲ့ အများဆုံး page size ပါအောင် လုံခြုံစွာ လက်ခံနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

Pagination ကို ရိုးရိုးလေး ပြောရရင်

GET /v1/users လို list endpoint တစ်ခုရဲ့ နောက်မှာ row ငါးသောင်း ရှိနေနိုင်ပါတယ်။ အဲဒါအားလုံးကို response တစ်ခုထဲမှာ ပို့လိုက်ရင် server အတွက် နှေးတယ်၊ network အတွက် လေးတယ်၊ တစ်ကြိမ်မှာ နှစ်ဆယ်ပဲ ပြတဲ့ screen အတွက်လည်း အသုံးမဝင်ပါဘူး။ ဒါကြောင့် API က list ကို page တစ်ခုချင်း ပေးပါတယ်။

စာကြည့်တိုက် search ရလဒ်လို မြင်ကြည့်ပါ။ ပထမဆုံး match ဆယ်ခုကို မြင်ရပြီး "နောက်တစ်ခု" ခလုတ်ပါပါတယ်။ Response တစ်ခုစီမှာ page တစ်ခုနဲ့အတူ နောက် page ကို တောင်းဖို့ လိုတာ (offset ဒါမှမဟုတ် cursor လို့ခေါ်တဲ့ bookmark) ပါလာပါတယ်။ Filter (?role=admin) က ဘယ် row တွေ list ထဲ ပါမလဲ ရွေးပါတယ်၊ sort (?sort=-id) က အစီအစဉ်ကို ဆုံးဖြတ်ပါတယ်၊ pagination က အဲဒီ list ကို page တွေအဖြစ် ဖြတ်ပေးတာပါ။

Problem → Why → Solution

ပြဿနာ: data ပြောင်းနေတဲ့ list ကို page နံပါတ်နဲ့ ဖတ်ခြင်း

အောက်က client က page အလွတ်တစ်ခု ပြန်လာတဲ့အထိ loop ပတ်ပါတယ်။ လူမရှိတဲ့ test database မှာတော့ အလုပ်လုပ်ပါတယ်။ ဒါပေမဲ့ production မှာ သင် page ဖတ်နေစဉ် user အသစ်တွေ sign up လုပ်နေပါတယ်။ အပေါ်ဆုံးမှာ row အသစ်တစ်ခု ဝင်လာရင် နောက်က row အားလုံး တစ်ကွက်စီ အောက်ရွေ့သွားလို့ page 2 ရဲ့ ပထမ row က page 1 ရဲ့ နောက်ဆုံး row ပဲ ဖြစ်နေပါတယ်။ User တစ်ယောက်ကို နှစ်ကြိမ် လုပ်မိသွားပါတယ်၊ row ဖျက်တာဆိုရင်တော့ user တွေကို ကျော်သွားပါတယ်။ Loop မှာလည်း ရပ်ဖို့ အခြေအနေက empty page တစ်ခုတည်းပါ၊ server က data အတူတူကိုပဲ ပြန်ပေးနေရင် အဆုံးမရှိ ပတ်နေပါလိမ့်မယ်။

offset-loop.js
// Looks fine, but assumes the data never changes while we read it
const all = [];
let offset = 0;
while (true) {
  const res = await fetch("/v1/users?limit=50&offset=" + offset);
  const { items } = await res.json();
  if (items.length === 0) break; // the only stop condition
  all.push(...items);            // may contain duplicates
  offset += 50;
}

Page နံပါတ်တွေ ဘာကြောင့် ကျိုးလွယ်လဲ

Offset ဆိုတာ "အခု list ပုံစံအတိုင်း ပထမ row N ခုကို ကျော်ပါ" ဆိုတဲ့ အဓိပ္ပာယ်ပါ။ Row တစ်ခုကို ညွှန်းတာ မဟုတ်လို့ ဒီနေရာမတိုင်ခင် insert ဒါမှမဟုတ် delete ဖြစ်တာနဲ့ အဲဒီနံပါတ်ရဲ့ အဓိပ္ပာယ် ပြောင်းသွားပါတယ်။ Cursor ကတော့ row တစ်ခုကို ညွှန်းပါတယ် ("u_5 ရဲ့ နောက်က အားလုံး")၊ ဒါကြောင့် တခြားနေရာမှာ row တွေ ထပ်ထည့်ဖျက်ပေမဲ့ အဖြေ မှန်နေပါတယ်။

Core concept: filter, sort, page နဲ့ bookmark

Paginated list တစ်ခုက မေးခွန်းသုံးခုကို အစဉ်တကျ ဖြေပါတယ်။ Filtering က ဘယ် row တွေ အရည်အချင်းပြည့်မီလဲ ဆုံးဖြတ်ပါတယ် (ဒီ caller ကြည့်ခွင့်ရှိတဲ့ row တွေလည်း ပါပါတယ်)။ Sorting က အဲဒီ row တွေကို အစီအစဉ်တစ်ခုနဲ့ စီပါတယ်။ Pagination ကမှ စီပြီးသား list ကို page တွေအဖြစ် ဖြတ်ပါတယ်။ Server က paginate မလုပ်ခင် permission အရ filter အရင်လုပ်ရပါမယ်၊ မဟုတ်ရင် page ထဲမှာ caller မဖတ်ရတဲ့ row တွေ ပါလာနိုင်သလို total count က ဖျောက်ထားတဲ့ row ဘယ်လောက်ရှိလဲဆိုတာကိုပါ ပေါက်ကြားစေနိုင်ပါတယ်။

Offset pagination က ?limit=20&offset=40 လို့ ပို့ပါတယ်၊ ပထမ row လေးဆယ်ကို ကျော်ပြီး နောက်နှစ်ဆယ် ပေးပါလို့ ဆိုလိုတာပါ။ တည်ဆောက်ရလွယ်ပြီး user က page ခုနစ်ကို တန်းခုန်သွားလို့ ရပါတယ်။ အားနည်းချက်က offset ဟာ တည်နေရာ မဟုတ်ဘဲ အရေအတွက် ဖြစ်နေတာပါ: အဲဒီနံပါတ်မတိုင်ခင် row တစ်ခု insert ဒါမှမဟုတ် delete ဖြစ်ရင် နောက် page တွေ အားလုံး ရွေ့သွားပြီး row တစ်ခု ထပ်တာ ဒါမှမဟုတ် ကျော်တာ ဖြစ်ပါတယ်။ Page နက်နက်မှာလည်း နှေးပါတယ်၊ database က ကျော်ရမယ့် row အားလုံးကို ဖြတ်လျှောက်ပြီးမှ တစ်ခုချင်း ပြန်ပေးနိုင်လို့ပါ။

Cursor (keyset) pagination က ?limit=20&cursor=abc လို့ ပို့ပါတယ်။ Cursor ထဲမှာ သင်နောက်ဆုံးမြင်ခဲ့တဲ့ row ကို encode လုပ်ထားပြီး server က "ဒီ row ရဲ့ နောက်က row တွေ" လို့ ဖြေပါတယ်၊ ပုံမှန်အားဖြင့် WHERE id < 5 ORDER BY id DESC LIMIT 20 ပုံစံပါ။ Index နဲ့ ဖြေလို့ page တစ်မှာရော page တစ်သောင်းမှာပါ အချိန်တူပါတယ်။ Row အရေအတွက် မရေဘဲ row ကို ညွှန်းတာမို့ တခြားနေရာက insert တွေက သင့်နေရာကို မရွှေ့နိုင်ပါဘူး။ ပေးရတဲ့ တန်ဖိုးက page ခုနစ်ကို မခုန်နိုင်တာနဲ့ sort က ပြည့်စုံရမယ်ဆိုတာပါ: id လို unique column ကို နောက်ဆုံး tie-breaker အဖြစ် ထည့်ပါ၊ မဟုတ်ရင် sort တန်ဖိုးတူတဲ့ row တွေ ကျော်သွားနိုင်ပါတယ်။

မေးခွန်းoffset / limitcursor / keyset
Page 7 ကို တန်းခုန်လို့ရလားရတယ်၊ offset=120မရဘူး၊ နောက်တစ်ခုပဲ
ဖတ်နေစဉ် row ထပ်ဝင်ရင်row ထပ်တာ ဒါမှမဟုတ် ကျော်တာတည်ငြိမ်တယ်၊ မထပ်ဘူး
Page နက်ရင် ကုန်ကျစရိတ်offset နဲ့အတူ တက်တယ်page တစ်နဲ့ တူတယ်
သင့်တော်တဲ့နေရာadmin table၊ data နည်းfeed, export, data များ

Cursor ကို opaque string တစ်ခုအဖြစ်သာ သဘောထားပါ။ ဒီနေ့ {"id":5} ရဲ့ base64 ဖြစ်နေနိုင်ပေမဲ့ မနက်ဖြန် server က sort field ထည့်တာ ဒါမှမဟုတ် signature ထိုးတာ လုပ်နိုင်ပါတယ်။ Cursor ကို client ကိုယ်တိုင် ဆောက်ရင် ဒါမှမဟုတ် ပြင်ရင် အဲဒီနေ့မှာ ပျက်ပါလိမ့်မယ်။ စာရင်းဆုံးသွားပြီဆိုတာကိုလည်း response က ရှင်းရှင်းပြောရပါမယ်: null ဖြစ်သွားတဲ့ next_cursor၊ has_more flag ဒါမှမဟုတ် rel="next" ပါတဲ့ Link header ပါ။ Total count က optional ပါ၊ filter လုပ်ထားတဲ့ table ကြီးကို ရေတွက်ရတာ စျေးကြီးပြီး ရောက်လာတဲ့အချိန်မှာ ဂဏန်းက အဟောင်းဖြစ်နေပြီမို့ပါ။ Server က limit ကို အများဆုံး page size အထိ ဖြတ်ထားနိုင်လို့ client က တောင်းတဲ့အရေအတွက်ကို မယူဆဘဲ ပြန်ရလာတာကိုပဲ ဖတ်ပါ။

Page နံပါတ် မဟုတ်ဘဲ bookmark
  1. Page တစ်ခု တောင်း: GET ?limit=20
  2. Server က items + next_cursor ပေး
  3. next_cursor ကို ?cursor= အဖြစ် ပြန်ပို့
  4. next_cursor null ဖြစ်ရင် ရပ်

Cursor က ကျော်ခဲ့တဲ့ row အရေအတွက်ကို မဟုတ်ဘဲ သင်ရပ်ခဲ့တဲ့နေရာကို မှတ်ထားတာမို့ တခြားနေရာက row တွေ ထပ်ဝင်ဖျက်လို့ သင့်နေရာ မရွှေ့နိုင်ပါဘူး။

အထွေထွေ ပုံစံ

text
# offset style
GET /v1/users?limit=<n>&offset=<rows to skip>
200 OK  { "items": [ ... ], "total": <count> }

# cursor style (omit cursor on the first request)
GET /v1/users?limit=<n>&cursor=<opaque string>&role=<filter>
200 OK  { "items": [ ... ], "next_cursor": "<opaque string>" | null }

Offset style က ကျော်မယ့် row အရေအတွက်ကို ပို့ပါတယ်၊ cursor style က server ပေးခဲ့တဲ့ opaque string ကို ပြန်ပို့ပါတယ်။ ဘယ်ဟာသုံးသုံး response က list ကို ဆက်ဖတ်ဖို့ လမ်းညွှန်ချက် (total ဒါမှမဟုတ် next_cursor) ပါလာရပါမယ်။

Worked example: offset လွဲတာနဲ့ cursor လမ်းလျှောက်ခြင်း

ဒီ sample က ကိုယ့်စက်ထဲ random port တစ်ခုမှာ သေးသေးလေး API တစ်ခု စတင်ပါတယ် (network မလိုပါဘူး၊ key မလိုပါဘူး)။ အဲဒီ API က user ခြောက်ယောက်ကို အသစ်ဆုံးအရင် ပေးပြီး နှိုင်းယှဉ်လို့ရအောင် style နှစ်မျိုးလုံး ပြောနိုင်ပါတယ်။ Client က offset နဲ့ page 1 ဖတ်ပြီး user အသစ်တစ်ယောက် sign up လုပ်ပါတယ်၊ ပြီးမှ page 2 ဖတ်တော့ u_4 က နှစ်ခါ ပေါ်ပါတယ်။ ပြီးတဲ့အခါ fetchAll က cursor နဲ့ list ကို လမ်းလျှောက်ပါတယ်၊ လမ်းလျှောက်နေစဉ် တစ်ယောက် ထပ် sign up လုပ်ပေမဲ့ ရလဒ်မှာ duplicate မရှိပါဘူး။

pagination.mjs
import http from "node:http";

// A stand-in for https://api.example.com, newest users first.
const users = ["Ana", "Ben", "Cho", "Dev", "Eli", "Fay"].map((name, i) => ({
  id: "u_" + (i + 1),
  name,
}));
const seq = (user) => Number(user.id.slice(2)); // "u_5" -> 5
const signUp = (name) => users.push({ id: "u_" + (users.length + 1), name });

// A cursor is opaque to clients. Here it is base64 of {"id": <last id seen>}.
const encode = (id) => Buffer.from(JSON.stringify({ id })).toString("base64url");
const decode = (cursor) => JSON.parse(Buffer.from(cursor, "base64url").toString()).id;

const server = http.createServer((req, res) => {
  const url = new URL(req.url, "http://localhost");
  const limit = Math.min(Number(url.searchParams.get("limit") ?? 3), 5); // max page size
  const rows = [...users].reverse();
  let body;
  if (url.searchParams.has("offset")) {
    const offset = Number(url.searchParams.get("offset"));
    body = { items: rows.slice(offset, offset + limit), total: rows.length };
  } else {
    const cursor = url.searchParams.get("cursor");
    const rest = cursor ? rows.filter((u) => seq(u) < decode(cursor)) : rows;
    const items = rest.slice(0, limit);
    const next = rest.length > limit ? encode(seq(items.at(-1))) : null;
    body = { items, next_cursor: next };
    const target = "/v1/users?limit=" + limit + "&cursor=" + next;
    if (next) res.setHeader("Link", "<" + target + '>; rel="next"');
  }
  res.setHeader("Content-Type", "application/json");
  res.end(JSON.stringify(body));
});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
const base = "http://127.0.0.1:" + server.address().port;
const ids = (items) => items.map((u) => u.id).join(" ");

// 1. Offset paging while the data changes
const getJson = async (path) => (await fetch(base + path)).json();
const page1 = await getJson("/v1/users?limit=3&offset=0");
console.log("offset page 1:", ids(page1.items), "(total " + page1.total + ")");
signUp("Gus"); // someone registers between our two requests
const page2 = await getJson("/v1/users?limit=3&offset=3");
const repeated = page2.items.filter((u) => page1.items.some((p) => p.id === u.id));
console.log("offset page 2:", ids(page2.items), "| seen twice:", ids(repeated));

// 2. Cursor paging: follow next_cursor until it is null, with safety guards
async function fetchAll(maxPages = 10) {
  const all = [];
  const usedCursors = new Set();
  let cursor = null;
  for (let page = 1; page <= maxPages; page++) {
    const query = "?limit=3" + (cursor ? "&cursor=" + cursor : "");
    const res = await fetch(base + "/v1/users" + query);
    const body = await res.json();
    const link = res.headers.get("link");
    console.log("cursor page " + page + ":", ids(body.items), "| link:", link);
    all.push(...body.items);
    cursor = body.next_cursor;
    if (cursor === null) return all; // the server says there is nothing more
    if (usedCursors.has(cursor)) throw new Error("server repeated a cursor");
    usedCursors.add(cursor);
    if (page === 1) signUp("Hal"); // new data arrives in the middle of the walk
  }
  throw new Error("stopped after " + maxPages + " pages");
}
const all = await fetchAll();
const duplicates = all.length - new Set(all.map((u) => u.id)).size;
console.log("collected:", ids(all), "| duplicates:", duplicates);
server.close();

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Opaque cursorconst encode = (id) => Buffer.from(JSON.stringify({ id })).toString("base64url");

    Cursor က နောက်ဆုံးမြင်ခဲ့တဲ့ id ကို JSON ပြောင်းပြီး base64url လုပ်ထားတာပါ။ Client က ဒီစာကြောင်းကို ဘယ်တော့မှ ဖတ်စရာ မလိုပါဘူး၊ ပြန်ပို့ရုံပဲ။

  2. Maximum page sizeconst limit = Math.min(Number(url.searchParams.get("limit") ?? 3), 5); // max page size

    Client က limit=100 တောင်းလည်း server က ငါးခုထက် မပေးပါဘူး။ ဒါကြောင့် client က body ထဲက items ကို ဖတ်ရမှာ ဖြစ်ပြီး တောင်းခဲ့တဲ့ ဂဏန်းကို မယူဆရပါဘူး။

  3. Offset pagebody = { items: rows.slice(offset, offset + limit), total: rows.length };

    Offset က array ထဲက အစကနေ ကျော်ရမယ့် အရေအတွက်ပဲ ဖြစ်ပါတယ်။ ဒီစာကြောင်းက row တစ်ခုကို မညွှန်းတာမို့ signUp ကြားဝင်ရင် page 2 က u_4 ကို ထပ်ပေးပါတယ်။

  4. Keyset pageconst rest = cursor ? rows.filter((u) => seq(u) < decode(cursor)) : rows;

    Cursor မှာ ပါတဲ့ id ထက် ငယ်တဲ့ row တွေကိုပဲ ယူပါတယ်၊ SQL ရဲ့ WHERE id < last ပါပဲ။ အပေါ်ဆုံးမှာ user အသစ်ဝင်လာလည်း ဒီ filter ရလဒ် မပြောင်းပါဘူး။

  5. Stop signalif (cursor === null) return all; // the server says there is nothing more

    စာရင်းဆုံးကြောင်းကို server က next_cursor: null နဲ့ ပြောတာမို့ "page တိုတယ်" လို့ ခန့်မှန်းစရာ မလိုပါဘူး။

  6. Loop guardsif (usedCursors.has(cursor)) throw new Error("server repeated a cursor");

    Cursor တစ်ခုတည်း ထပ်ပေါ်ရင် server မှားနေပြီ။ ဒီ guard နဲ့ maxPages ကန့်သတ်ချက်ကြောင့် bug ရှိတဲ့ server ကို အဆုံးမရှိ မလိုက်မိတော့ပါဘူး။

You should see
offset page 1: u_6 u_5 u_4 (total 6)
offset page 2: u_4 u_3 u_2 | seen twice: u_4
cursor page 1: u_7 u_6 u_5 | link: </v1/users?limit=3&cursor=eyJpZCI6NX0>; rel="next"
cursor page 2: u_4 u_3 u_2 | link: </v1/users?limit=3&cursor=eyJpZCI6Mn0>; rel="next"
cursor page 3: u_1 | link: null
collected: u_7 u_6 u_5 u_4 u_3 u_2 u_1 | duplicates: 0
Cursor list ကို လမ်းလျှောက်ပုံ
  1. ပထမ page ကို cursor မပါဘဲ တောင်းပါ
  2. ရလာတဲ့ items တွေကို သုံးပါ
  3. next_cursor က null လား စစ်ပါ
  4. မဟုတ်ရင် ?cursor= နဲ့ ပြန်တောင်းပါ

next_cursor က null

The walk is complete

Cursor ထပ်တာ ဒါမှမဟုတ် page ကန့်သတ်ချက်ပြည့်

Abort and report a server problem instead of looping forever

Filter နဲ့ sort ကို အပေါက်မဖွင့်ဘဲ လက်ခံခြင်း

Query parameter တွေက client ကလာတာမို့ မယုံကြည်ရတဲ့ input အဖြစ် သဘောထားပါ။ sort ဒါမှမဟုတ် filter တန်ဖိုးကို SQL ထဲ တိုက်ရိုက်မကပ်ပါနဲ့၊ injection ဖြစ်နိုင်သလို index မရှိတဲ့ column နဲ့ sort ခိုင်းပြီး database ကို ဖိစီးနိုင်ပါတယ်။ Sort ကို allowlist (ဥပမာ id, name) ထဲက ပဲ လက်ခံပြီး column ကို server က ကိုယ်တိုင် map လုပ်ပါ၊ နောက်ဆုံးမှာ unique id ကို tie-breaker အဖြစ် အမြဲထည့်ပါ။ မသိတဲ့ sort field ကို တိတ်တိတ်ပုံမှန်လုပ်မနေဘဲ 422 နဲ့ ခွင့်ပြုတဲ့ တန်ဖိုးတွေကို ပြန်ပြောပါ၊ မဟုတ်ရင် client က sort ပြီးသားလို့ ထင်နေပါလိမ့်မယ်။

Parameterရည်ရွယ်ချက်Server စည်းကမ်း
limitPage sizedefault 20၊ အများဆုံး 100၊ 0 နဲ့ အနုတ်ကို ပယ်
cursorဆက်ဖတ်ရမယ့်နေရာopaque၊ မမှန်ရင် 400 invalid_cursor
sortအစီအစဉ်allowlist + unique id tie-breaker
roleList ကို ကျဉ်းစေခွင့်ပြုတဲ့ တန်ဖိုးနဲ့သာ တူညီမှု

Raw HTTP နဲ့ Link header

Link header က next page ရဲ့ URL ကို body အပြင်ဘက်မှာ ပေးတဲ့ နည်းပါ။ Client က URL ကို ကိုယ်တိုင် မဆောက်ရဘဲ rel="next" ပါတဲ့ link ကို လိုက်ရုံပဲ ဖြစ်ပါတယ်။ အောက်က response ကို ကြည့်ပါ: body မှာ next_cursor ရှိပြီး header မှာလည်း တူညီတဲ့ ညွှန်းချက် ရှိပါတယ်။ API တွေက တစ်ခုခုကို (ဒါမှမဟုတ် နှစ်ခုလုံးကို) သုံးကြပါတယ်၊ ဒါကြောင့် provider ရဲ့ docs ကို အရင်ကြည့်ပါ။

text
GET /v1/users?limit=3 HTTP/1.1
Host: api.example.com
Accept: application/json

HTTP/1.1 200 OK
Content-Type: application/json
Link: </v1/users?limit=3&cursor=eyJpZCI6NH0>; rel="next"

{ "items": [ { "id": "u_6" }, { "id": "u_5" }, { "id": "u_4" } ],
  "next_cursor": "eyJpZCI6NH0" }

လက်တွေ့ project တွေမှာ

လက်တွေ့ API တွေမှာ pagination

Provider တွေက ဒီ idea တွေကို နာမည်မတူဘဲ သုံးကြပါတယ်။ GitHub REST API က per_page (အများဆုံး 100) နဲ့ Link header ကို သုံးပါတယ်၊ Stripe က limit, starting_after နဲ့ has_more ကို သုံးပါတယ်၊ Slack က cursor parameter နဲ့ response_metadata ထဲက next_cursor ကို သုံးပါတယ်။ Parameter အမည် အတိအကျကို အမြဲ provider docs မှာ စစ်ပါ။

  • Infinite scroll feed နဲ့ activity timeline တွေက cursor သုံးပါတယ်၊ row အသစ်တွေ အမြဲဝင်နေလို့ offset သုံးရင် ထပ်တာတွေ တွေ့ရမှာပါ။
  • Admin table တွေက ဒေတာ နည်းပြီး "page 7 သွားမယ်" လိုချင်တာမို့ offset နဲ့ total ကို ဆက်သုံးကြတာ များပါတယ်။
  • Export ဒါမှမဟုတ် sync job တွေက next_cursor ကို null အထိ လိုက်ပါတယ်၊ ဒါကြောင့် page ရာပေါင်းများစွာ ရှိနိုင်ပြီး request တိုင်းက rate limit ထဲ ပါသွားပါတယ်။ အများဆုံး page size ကို သုံးပြီး guard ထည့်ပါ။

အမှားများ

Pagination မှာ ဖြစ်လေ့ရှိတဲ့ အမှားများ

ပြောင်းနေတဲ့ data ကို offset နဲ့ ဖတ်ခြင်း

Feed ဒါမှမဟုတ် user list လို data ပြောင်းနေတဲ့ list ကို offset loop နဲ့ ဖတ်ရင် row ထပ်တာ၊ ကျော်တာတွေ ဖြစ်ပါတယ်။ Test မှာ မမြင်ရဘဲ production မှာမှ ပေါ်လို့ ရှာရခက်ပါတယ်။ Provider က cursor ပေးထားရင် cursor ကို သုံးပါ။

Guard မပါတဲ့ loop

while (true) နဲ့ next page ကို လိုက်ပြီး ရပ်တာကို server ပေါ်ပဲ မှီခိုရင် server bug တစ်ခု (cursor မပြောင်းတာ၊ null မဖြစ်တာ) နဲ့ process က အဆုံးမရှိ ပတ်ပြီး rate limit ကုန်သွားနိုင်ပါတယ်။ maxPages နဲ့ ပြီးသား cursor တွေကို မှတ်ထားတဲ့ Set ကို ထည့်ပါ။

Cursor ကို client က ဆောက်တာ ဒါမှမဟုတ် ပြင်တာ

Cursor ကို decode လုပ်ပြီး id ကို ကိုယ်တိုင် တိုးချင်တာမျိုး မလုပ်ပါနဲ့။ Format က version အလိုက် ပြောင်းနိုင်ပြီး မမှန်တဲ့ cursor ကို server က 400 နဲ့ ငြင်းပါလိမ့်မယ်။

limit နဲ့ sort ကို မယုံကြည်ဘဲ မကန့်သတ်ခြင်း

limit=1000000 လာရင် server က row အားလုံးကို memory ထဲ ဆွဲတင်ပါလိမ့်မယ်။ အများဆုံး page size ကို ကန့်သတ်ပြီး sort ကို allowlist နဲ့ ထိန်းပါ။

Best Practices

Best practices

  • ပြောင်းနေတဲ့ ဒါမှမဟုတ် ကြီးမားတဲ့ list တွေအတွက် cursor pagination ကို အရင်ရွေးပါ၊ offset ကို data နည်းပြီး page ခုန်ဖို့ လိုမှ သုံးပါ။
  • List တိုင်းမှာ default နဲ့ အများဆုံး limit သတ်မှတ်ပြီး တောင်းတာထက် နည်းနည်းပေးရင် client က ပြန်ရလာတာကိုပဲ ယုံပါ။
  • Total count ကို တကယ်လိုမှ ပေးပါ၊ ပေးရင်လည်း "ခန့်မှန်း" လို့ docs မှာ ရေးပါ။

Mini challenge

Cursor pager တစ်ခု ကိုယ်တိုင် ရေးကြည့်ပါ

id အလိုက် အကြီးဆုံးအရင် စီထားတဲ့ row တွေအတွက် pageAfter(rows, limit, cursor) ကို ရေးပါ။ { items, next_cursor } ကို ပြန်ပေးရမယ်၊ မမှန်တဲ့ limit ဒါမှမဟုတ် cursor ဆိုရင် throw လုပ်ရမယ်၊ limit ကို အများဆုံး 5 အထိ ဖြတ်ရမယ်။ ပြီးရင် next_cursor ကို လိုက်ပြီး page guard ပါတဲ့ collectAll() ကို ရေးပါ။

  • limit ကို 1 ကနေ 5 အထိ ဖြတ်ပါ၊ integer မဟုတ်ရင် ပယ်ပါ
  • Cursor ကို decode လုပ်ပြီး ဆက်ဖတ်ရမယ့်နေရာကို ရှာပါ၊ အမှိုက်ဆိုရင် Error("invalid cursor") ပစ်ပါ
  • နောက်ဆုံး page မှာ next_cursor က null ဖြစ်ရမယ်
  • collectAll က maxPages ကျော်ရင် error ပစ်ရမယ်
pager.js
const MAX_LIMIT = 5;
const rows = [8, 7, 6, 5, 4, 3, 2, 1].map((n) => ({ id: n }));

// A cursor is an opaque string. You choose how to encode it.
function encodeCursor(id) {
  // TODO: turn the last id on this page into an opaque string
}

function decodeCursor(cursor) {
  // TODO: turn it back into an id, or throw new Error("invalid cursor")
}

function pageAfter(rows, limit, cursor = null) {
  // TODO: validate limit and clamp it to MAX_LIMIT
  // TODO: keep only the rows after the cursor (rows are newest first)
  // TODO: return { items, next_cursor }, with next_cursor null on the last page
}

function collectAll(maxPages) {
  // TODO: call pageAfter(rows, 3, cursor) until next_cursor is null
  // TODO: throw an Error if maxPages is exceeded
}

ရလဒ်: Output လေးကြောင်းထွက်ပါမယ်: 8 7 6 5 4 3 2 1၊ ပြီးရင် 5 (limit 100 ကို ဖြတ်ထားလို့)၊ too many pages နဲ့ invalid cursor။

အရိပ်အမြွက် ကြည့်ရန်

next_cursor ထုတ်ရမလားဆိုတာ "ဒီ page ပြီးရင် row ကျန်သေးလား" နဲ့ ဆုံးဖြတ်ပါ: rest.length > size။ Cursor ထဲက id ထက် ငယ်တဲ့ row တွေကိုပဲ ယူပါ။ Buffer.from("!!!", "base64url") က error မပစ်ဘဲ empty string ထွက်တာကို သတိထားပြီး decode ပြီးရင် ဂဏန်းမှန်မမှန် စစ်ပါ။

အဖြေ ကြည့်ရန်
pager-solution.js
const MAX_LIMIT = 5;
const rows = [8, 7, 6, 5, 4, 3, 2, 1].map((n) => ({ id: n }));

const encodeCursor = (id) => Buffer.from(String(id)).toString("base64url");

function decodeCursor(cursor) {
  const text = Buffer.from(cursor, "base64url").toString();
  if (!/^[0-9]+$/.test(text)) throw new Error("invalid cursor");
  return Number(text);
}

function pageAfter(rows, limit, cursor = null) {
  if (!Number.isInteger(limit) || limit < 1) throw new Error("invalid limit");
  const size = Math.min(limit, MAX_LIMIT);
  const rest = cursor === null ? rows : rows.filter((r) => r.id < decodeCursor(cursor));
  const items = rest.slice(0, size);
  const hasMore = rest.length > size;
  return { items, next_cursor: hasMore ? encodeCursor(items.at(-1).id) : null };
}

function collectAll(maxPages) {
  const ids = [];
  let cursor = null;
  for (let page = 0; page < maxPages; page++) {
    const body = pageAfter(rows, 3, cursor);
    ids.push(...body.items.map((r) => r.id));
    if (body.next_cursor === null) return ids;
    cursor = body.next_cursor;
  }
  throw new Error("too many pages");
}

console.log(collectAll(10).join(" "));
console.log(pageAfter(rows, 100).items.length);
try {
  collectAll(2);
} catch (err) {
  console.log(err.message);
}
try {
  pageAfter(rows, 3, "!!!");
} catch (err) {
  console.log(err.message);
}

Quick Quiz 1 / 3

Client က အသစ်ဆုံးအရင် list ရဲ့ page 1 ကို limit=3&offset=0 နဲ့ ဖတ်ပါတယ်။ ပြီးတော့ row အသစ်တစ်ခု အပေါ်ဆုံးမှာ ဝင်လာပြီး client က offset=3 ကို တောင်းပါတယ်။ ဘာဖြစ်မလဲ။

Quick Quiz 2 / 3

Cursor နဲ့ paginate လုပ်ထားတဲ့ list တစ်ခုကို အဆုံးအထိ ဖတ်ဖို့ ဘယ် loop condition က အလုံခြုံဆုံးလဲ။

Quick Quiz 3 / 3

Sort key တွေရဲ့ နောက်ဆုံးမှာ unique id ကို ဘာကြောင့် ထည့်ရတာလဲ။

မှတ်ထားရမယ့် အချက်များ

  • List ကို page တစ်ခုချင်း ပေးပါတယ်: filter ကို permission အရ အရင်လုပ်၊ sort လုပ်၊ ပြီးမှ page ဖြတ်ပါ။
  • Offset က အရေအတွက်ပဲမို့ data ပြောင်းရင် row ထပ်/ကျော်တတ်ပြီး page နက်ရင် နှေးပါတယ်၊ cursor က row ကို ညွှန်းလို့ တည်ငြိမ်ပါတယ်။
  • next_cursor ကို null အထိ လိုက်ပါ၊ maxPages နဲ့ cursor ထပ်မထပ် guard ထည့်ပါ၊ cursor ကို opaque ထားပါ။
  • limit ကို ကန့်သတ်၊ sort/filter ကို allowlist နဲ့ထိန်း၊ unique id ကို tie-breaker အဖြစ် ထည့်ပါ။

RFC 9110 — HTTP Semantics — IETF

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Pagination/Filtering/Sorting | Thuta Learning