What is the best use for the repo module in a phoenix

So I recall someone saying something along the lines of that you should put your query logic in the Repo module.

I’m wondering if that is still a true statement.

For example. I have a blog context where I want to get only published blog posts. so I have a function like so.

def list_publised_posts do
      p in Digitalcakes.Blog.Post,
      where: p.published_at <= ^Timex.now(),
      where: p.draft == false,
      order_by: [desc: :published_at],
      select: p
    |> Repo.all()

Does it really make sense to move that logic into the Repo vs the keeping it in the context of the blog module?
If so what I’m I gaining from doing that?

And if not then what is a good use for the Repo module?

Uh, I keep queries in the context modules. I reserve Repo only for functions and helper functions of accessing the database itself (hmm, I should publish my helpers sometime…). :slight_smile:


Yeah I’d leave that in a context module.

I do have some “universal” queries i just have in a generic MyApp.Query module (for now), though.

stuff like:

def by_company(query, company_id) do
  from q in query, where: q.company_id == ^company_id

Yeah I use the repo module also only for helpers, e.g. some of the ecto generated functions return something like result | nil and I’ve helpers, which return the same data, but wrapped in error tuples, which are way nicer to pattern match e.g. in with expressions.