0Pricing
Elixir & Phoenix: Scalable Backend Development · 课时

分页、筛选与 API 版本管理

通过查询分页、灵活筛选和清晰的版本管理策略,让 Phoenix API 更具扩展性且更易维护。

分页、筛选与 API 版本管理 是 CoddyKit 上的免费 Elixir & Phoenix: Scalable Backend Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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 版本管理」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Elixir & Phoenix: Scalable Backend Development 课程的其余内容,请升级到 CoddyKit PRO。 Elixir & Phoenix: Scalable Backend Development 课程共包含 4 节课。

「分页、筛选与 API 版本管理」这节课中我会学到什么?

通过查询分页、灵活筛选和清晰的版本管理策略,让 Phoenix API 更具扩展性且更易维护。 你通过在浏览器中直接运行的动手代码来练习 Elixir & Phoenix: Scalable Backend Development,全天候 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