Bylaw.Credo.Check.Phoenix.NoRepoInController (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 higher and works with any version of Elixir.

Explanation

Disallows calling Repo directly from controller modules.

Keeping persistence access in contexts gives controllers a thin web boundary and centralizes authorization, query composition, and domain policy. Direct Repo calls make those rules easy to bypass and duplicate across endpoints.

Examples

Controllers should delegate data access to context modules (e.g. Conversations, Runs) rather than calling Repo directly. This enforces a boundary between the web layer and the persistence layer. Avoid:

  defmodule MyAppWeb.ThingController do
    def show(conn, %{"id" => id}) do
      thing = Repo.get!(Thing, id)
      render(conn, :show, thing: thing)
    end
  end

Prefer:

  defmodule MyAppWeb.ThingController do
    def show(conn, %{"id" => id}) do
      thing = Things.get_thing!(id)
      render(conn, :show, thing: thing)
    end
  end

Notes

This check uses static AST analysis, so it favors clear source-level patterns over runtime behavior.

Options

This check has no check-specific options. Configure it with an empty option list.

Usage

Add this check to Credo's checks: list in .credo.exs:

%{
  configs: [
    %{
      name: "default",
      checks: [
        {Bylaw.Credo.Check.Phoenix.NoRepoInController, []}
      ]
    }
  ]
}

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.