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

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.

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.