การแบ่งหน้า การกรอง และการเรียงลำดับ API
ทำให้ API ของ SaaS รองรับการขยายได้ด้วยการส่งชุดข้อมูลขนาดใหญ่เป็นหน้า เปิดให้ไคลเอนต์กรองและเรียงลำดับผลลัพธ์ และเปิดเผยพารามิเตอร์คำค้นที่สอดคล้องกัน
การแบ่งหน้า การกรอง และการเรียงลำดับ API เป็นบทเรียน AI Powered SaaS: Stripe + Auth + Billing + Deploy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน AI Powered SaaS: Stripe + Auth + Billing + Deploy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส AI Powered SaaS: Stripe + Auth + Billing + Deploy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Not Return Everything?
An endpoint that returns 50,000 rows is slow, heavy, and crashes clients. Pagination returns data in manageable chunks, while filtering and sorting let clients ask for exactly what they need.
Offset Pagination
The simplest approach uses page and limit. You skip (page - 1) * limit rows and take limit rows.
GET /api/users?page=2&limit=20Implementing Offset in Prisma
Prisma exposes skip and take for offset pagination.
const users = await prisma.user.findMany({
skip: (page - 1) * limit,
take: limit
});Returning Metadata
Clients need to know how many pages exist. Return the data plus a total count and computed page info.
const total = await prisma.user.count();
return Response.json({
data: users,
page,
totalPages: Math.ceil(total / limit)
});The Offset Problem
Large offsets are slow because the database still scans skipped rows, and inserts can shift items between pages. For big or live datasets, prefer cursor pagination.
Cursor Pagination
A cursor points to the last item seen. The next request fetches rows after that cursor — fast and stable even as data changes.
const users = await prisma.user.findMany({
take: limit,
skip: 1,
cursor: { id: lastId },
orderBy: { id: 'asc' }
});Filtering by Query Params
Read filter params from the URL and build a where clause. Use contains for search-style filters.
const q = searchParams.get('search');
const where = q ? { name: { contains: q, mode: 'insensitive' } } : {};Combining Multiple Filters
Build the where object conditionally so only provided params apply. Prisma combines them with AND by default.
const where = {};
if (status) where.status = status;
if (minPrice) where.price = { gte: Number(minPrice) };Sorting Safely
Accept a sort field and direction, but whitelist allowed fields so clients cannot order by arbitrary or sensitive columns.
const allowed = ['name', 'createdAt', 'price'];
const field = allowed.includes(sort) ? sort : 'createdAt';
const orderBy = { [field]: dir === 'desc' ? 'desc' : 'asc' };Validating Query Params
Coerce and clamp inputs: cap limit so a client cannot request a million rows, and default page to 1.
const limit = Math.min(Number(searchParams.get('limit')) || 20, 100);
const page = Math.max(Number(searchParams.get('page')) || 1, 1);Best Practices
Design scalable list endpoints:
- Always paginate large collections
- Use cursor pagination for big or live data
- Return metadata (total, pages)
- Whitelist sort fields and clamp limits
Quick Check
Test your pagination knowledge.
Recap
You learned to build scalable list APIs:
- Paginate with
skip/takeor cursors - Return total and page metadata
- Build dynamic
whereclauses for filtering - Whitelist sort fields and clamp the limit
Your API now handles large datasets efficiently.
เรียนรู้ AI Powered SaaS: Stripe + Auth + Billing + Deploy ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การแบ่งหน้า การกรอง และการเรียงลำดับ API” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การแบ่งหน้า การกรอง และการเรียงลำดับ API” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AI Powered SaaS: Stripe + Auth + Billing + Deploy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AI Powered SaaS: Stripe + Auth + Billing + Deploy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การแบ่งหน้า การกรอง และการเรียงลำดับ API”
ทำให้ API ของ SaaS รองรับการขยายได้ด้วยการส่งชุดข้อมูลขนาดใหญ่เป็นหน้า เปิดให้ไคลเอนต์กรองและเรียงลำดับผลลัพธ์ และเปิดเผยพารามิเตอร์คำค้นที่สอดคล้องกัน คุณปฏิบัติ AI Powered SaaS: Stripe + Auth + Billing + Deploy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AI Powered SaaS: Stripe + Auth + Billing + Deploy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน AI Powered SaaS: Stripe + Auth + Billing + Deploy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การแบ่งหน้า การกรอง และการเรียงลำดับ API” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน AI Powered SaaS: Stripe + Auth + Billing + Deploy นี้ได้ไหม
ได้ บทเรียน AI Powered SaaS: Stripe + Auth + Billing + Deploy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- หลักการออกแบบ API แบบ RESTful
- โครงสร้างฐานข้อมูลและ ORM
- จุดปลาย API แรก
- การแบ่งหน้า การกรอง และการเรียงลำดับ API