Bylaw.Credo.Check.Ecto.PreferOrderByOverRepoAllEnumSort (bylaw_credo v0.4.0)

Copy Markdown View Source

Basics

This check is disabled by default.

Learn how to enable it via .credo.exs.

This check has a base priority of high and works with any version of Elixir.

Explanation

Prefer ordering Ecto queries in the database over sorting rows in memory.

Database-side ordering avoids materializing all matching rows before sorting them in Elixir, reducing application memory and work. It also lets the database combine ordering with filtering, limits, and indexes before rows cross the database boundary.

Examples

Avoid:

query
|> Repo.all()
|> Enum.sort()

query
|> Repo.all()
|> Enum.sort_by(& &1.inserted_at)

Prefer adding order_by to the query before calling Repo.all.

This check reports only direct Repo.all followed by Enum.sort or Enum.sort_by. Sorting after an intermediate transformation may have different semantics and is outside this check's scope.

This check uses static AST analysis and cannot infer the equivalent order_by expression, so it does not provide an automatic rewrite.

Check-Specific Parameters

There are no specific parameters for this check.

General Parameters

Like with all checks, general params can be applied.

Parameters can be configured via the .credo.exs config file.