API-Paginierung, Filtern und Sortieren
Machen Sie Ihre SaaS-API skalierbar, indem Sie große Datensätze seitenweise zurückgeben, Clients das Filtern und Sortieren von Ergebnissen ermöglichen und konsistente Query-Parameter bereitstellen
API-Paginierung, Filtern und Sortieren ist eine kostenlose AI Powered SaaS: Stripe + Auth + Billing + Deploy-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des AI Powered SaaS: Stripe + Auth + Billing + Deploy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der AI Powered SaaS: Stripe + Auth + Billing + Deploy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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.
Häufig gestellte Fragen
Ist die Lektion „API-Paginierung, Filtern und Sortieren“ kostenlos?
Ja — der vollständige Text von „API-Paginierung, Filtern und Sortieren“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des AI Powered SaaS: Stripe + Auth + Billing + Deploy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der AI Powered SaaS: Stripe + Auth + Billing + Deploy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „API-Paginierung, Filtern und Sortieren“?
Machen Sie Ihre SaaS-API skalierbar, indem Sie große Datensätze seitenweise zurückgeben, Clients das Filtern und Sortieren von Ergebnissen ermöglichen und konsistente Query-Parameter bereitstellen Du übst AI Powered SaaS: Stripe + Auth + Billing + Deploy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um AI Powered SaaS: Stripe + Auth + Billing + Deploy zu starten?
Keine Vorkenntnisse erforderlich. AI Powered SaaS: Stripe + Auth + Billing + Deploy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „API-Paginierung, Filtern und Sortieren“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser AI Powered SaaS: Stripe + Auth + Billing + Deploy-Lektion Code schreiben und ausführen?
Ja. Jede AI Powered SaaS: Stripe + Auth + Billing + Deploy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Grundlagen des RESTful-API-Designs
- Datenbankschema und ORM
- Erste API-Endpunkte
- API-Paginierung, Filtern und Sortieren