0Pricing
AI Powered SaaS: Stripe + Auth + Billing + Deploy · Lekcja

Paginacja, filtrowanie i sortowanie API

Zapewnij skalowanie API aplikacji SaaS, zwracając duże zbiory danych stronami, pozwalając klientom filtrować i sortować wyniki oraz udostępniając spójne parametry zapytań.

Paginacja, filtrowanie i sortowanie API to bezpłatna lekcja AI Powered SaaS: Stripe + Auth + Billing + Deploy na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej AI Powered SaaS: Stripe + Auth + Billing + Deploy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs AI Powered SaaS: Stripe + Auth + Billing + Deploy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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=20

Implementing 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/take or cursors
  • Return total and page metadata
  • Build dynamic where clauses for filtering
  • Whitelist sort fields and clamp the limit

Your API now handles large datasets efficiently.

Często zadawane pytania

Czy lekcja „Paginacja, filtrowanie i sortowanie API” jest bezpłatna?

Tak — pełny tekst „Paginacja, filtrowanie i sortowanie API” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu AI Powered SaaS: Stripe + Auth + Billing + Deploy, przejdź na CoddyKit PRO. Kurs AI Powered SaaS: Stripe + Auth + Billing + Deploy zawiera 4 lekcji w sumie.

Co nauczysz się w „Paginacja, filtrowanie i sortowanie API”?

Zapewnij skalowanie API aplikacji SaaS, zwracając duże zbiory danych stronami, pozwalając klientom filtrować i sortować wyniki oraz udostępniając spójne parametry zapytań. Ćwiczysz AI Powered SaaS: Stripe + Auth + Billing + Deploy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć AI Powered SaaS: Stripe + Auth + Billing + Deploy?

Nie wymagamy żadnego doświadczenia. AI Powered SaaS: Stripe + Auth + Billing + Deploy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Paginacja, filtrowanie i sortowanie API”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji AI Powered SaaS: Stripe + Auth + Billing + Deploy?

Tak. Każda lekcja AI Powered SaaS: Stripe + Auth + Billing + Deploy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Zasady projektowania RESTful API
  2. Schemat bazy danych i ORM
  3. Pierwsze endpointy API
  4. Paginacja, filtrowanie i sortowanie API
← Powrót do AI Powered SaaS: Stripe + Auth + Billing + Deploy