0Pricing
Elixir & Phoenix: Scalable Backend Development · レッスン

ページネーション、フィルタリング、APIバージョニング

クエリのページネーション、柔軟なフィルタリング、適切なバージョニング戦略によって、Phoenix APIをスケーラブルで保守しやすくします。

「ページネーション、フィルタリング、APIバージョニング」はCoddyKit上の無料Elixir & Phoenix: Scalable Backend Developmentレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはElixir & Phoenix: Scalable Backend Development学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Elixir & Phoenix: Scalable Backend Developmentコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Why Pagination Matters

Returning every record in one response is slow and memory-heavy. Pagination sends data in manageable pages.

Offset-Based Pagination

The classic approach uses limit and offset driven by query params like ?page=2&per_page=20.

from(p in Post, limit: ^per_page, offset: ^((page - 1) * per_page))

Returning Page Metadata

Clients need to know totals. Include metadata such as current page, page size, and total count alongside the data.

%{
  data: posts,
  meta: %{page: 2, per_page: 20, total: 137}
}

The Downside of Offset

Large offsets get slow because the database still scans skipped rows, and rows shifting between requests cause duplicates.

Cursor-Based Pagination

Cursor pagination uses the last seen value (often an id or timestamp) to fetch the next page, staying fast and stable.

from(p in Post,
  where: p.id > ^cursor,
  order_by: [asc: p.id],
  limit: ^limit)

Filtering Results

Let clients narrow results with query params. Build the query conditionally based on which params are present.

def filter(query, %{"status" => status}) do
  from(q in query, where: q.status == ^status)
end
def filter(query, _), do: query

Composing Multiple Filters

Reduce over a params map to apply each supported filter, keeping the query composable and safe from injection via parameter binding.

Enum.reduce(params, base_query, fn
  {"author", a}, q -> from(p in q, where: p.author == ^a)
  _, q -> q
end)

Sorting

Accept a sort param and map it to a safe whitelist of allowed columns to avoid arbitrary ordering.

order = case params["sort"] do
  "newest" -> [desc: :inserted_at]
  _ -> [asc: :id]
end

Why Version Your API?

Versioning lets you evolve the API without breaking existing clients when you change response shapes or behavior.

URL Path Versioning

The most common strategy puts the version in the path, scoped in the router.

scope "/api/v1", MyAppWeb.V1 do
  pipe_through :api
  resources "/posts", PostController
end

Header-Based Versioning

Alternatively, select the version from an Accept header so URLs stay stable. Choose ONE strategy and stay consistent.

# Accept: application/vnd.myapp.v2+json

Quick Check

Test your API scaling knowledge.

Recap

You made APIs scalable and durable:

  • Paginate with offset (simple) or cursor (fast, stable)
  • Return page metadata with the data
  • Apply composable, parameter-bound filters and whitelisted sorting
  • Version via URL path or Accept header, consistently

よくある質問

「ページネーション、フィルタリング、APIバージョニング」レッスンは無料ですか?

はい。「ページネーション、フィルタリング、APIバージョニング」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Elixir & Phoenix: Scalable Backend Developmentコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Elixir & Phoenix: Scalable Backend Developmentコースには全4レッスンが含まれています。

「ページネーション、フィルタリング、APIバージョニング」で何を学びますか?

クエリのページネーション、柔軟なフィルタリング、適切なバージョニング戦略によって、Phoenix APIをスケーラブルで保守しやすくします。 ブラウザで直接実行するハンズオンコードでElixir & Phoenix: Scalable Backend Developmentを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Elixir & Phoenix: Scalable Backend Developmentを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのElixir & Phoenix: Scalable Backend Developmentは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「ページネーション、フィルタリング、APIバージョニング」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このElixir & Phoenix: Scalable Backend Developmentレッスンでコードを書いて実行できますか?

はい。すべてのElixir & Phoenix: Scalable Backend Developmentレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. API設計の原則とベストプラクティス
  2. APIエンドポイントとシリアライズの実装
  3. 認証と認可の戦略
  4. ページネーション、フィルタリング、APIバージョニング
← Elixir & Phoenix: Scalable Backend Developmentに戻る