Skip to content

Add Repo.count/2 and Repo.count_by/3 - #4818

Closed
wojtekmach wants to merge 2 commits into
masterfrom
wm-repo-count
Closed

wojtekmach wants to merge 2 commits into
masterfrom
wm-repo-count

Conversation

@wojtekmach

@wojtekmach wojtekmach commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

No description provided.

@josevalim

Copy link
Copy Markdown
Member

I believe we added aggregate and aggregate_by specifically so we didn't have to add count, sum, etc. Any reason why we need the additional ones?

@wojtekmach

Copy link
Copy Markdown
Member Author

Yeah I was mostly after Repo.count_by and for some reason it didn't occur to me we should have aggregate_by instead. I'm closing this in favour of adding that: #4820.

I think Repo.count,count_by comes up so often that a built-in shorthand would be really convenient but I concede it's very easy to write one ourselves:

defmodule Repo do
  use Ecto.Repo, adapter: Ecto.Adapters.Postgres, otp_app: :myapp

  def count(queryable, opts \\ []) when is_list(opts) do
    aggregate(queryable, :count, opts)
  end

  def count_by(queryable, clauses, opts \\ []) when is_list(opts) do
    aggregate_by(queryable, :count, clauses, opts)
  end
end

@wojtekmach wojtekmach closed this Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants