I have a conflict.
It seems that the scaffolds imply that your repo interactions should be in a context, and your controllers operate via the contexts.
This makes sense, except that most of the time, the controller knows more about what it wants from the repo.
- Do I want just the User record?
- Or maybe I want the User and all their posts.
- Or just the first 10 posts and the count on those posts comments.
- Maybe I just want the ID and email?
I could just keep adding to my get_user!(id)
function:
def get_user!(id) do
Repo.one(from u in User,
:preload [[posts: :comments], [credentials: :key], :roles]
but that starts to look like a real bad idea, really quickly.
Do I start writing get_user_with_posts
and get_user_with_posts_comments
? Maybe pass down to those after passing an options list to get_user
?
Really it feels like I should just be writing Ecto queries right in the controller, getting what I want for each action, but that feels like Im misbehaving.
Perhaps the cleanest way would be writing a lot of my own chainable āquery buildersā,
def query_for_user(id)
end
def query_user_posts(query)
end
...
query_for_user(1) |> query_user_posts() |> query_user_roles() |> query_run()
but that almost feels like Im just writing a bad abstraction around Ecto.Query, for maybe zero real gain.
I know the answer is most often ādO whaT fiTs youR proJectā, which isnāt a bad answer, but I guess Im just hoping for some more idiomatic guiding from better programmers.