I’m wondering how everyone else solves this problem.
Say I have a very complicated schema in a big Phoenix application.
The schema describes a project which has tons of associations running up to 3 levels deep.
I return results to my frontend through channels, which are encoded by Phoenix through Poison.
My problem is that I need so many different versions of this data. Sometimes I just want the project, sometimes I want the project and the project members, sometimes I want everything etc. So that kind of ruins the @derive
approach, since it changes so much.
The way I do it now is by nil
ing out associations I don’t need on a case by case basis, but it just seems like it’s the wrong way to do it.
For instance:
with {:ok, company} <- Companies.get_company(company_id),
{:ok, _} <- Permissions.can?(user, :admin, company),
{:ok, relationship} <- Relationships.get_relationship(partner_id, company_id),
{:ok, relationship} <- Relationships.update_custom_price_list(relationship, price_list) do
relationship =
relationship
|> Repo.preload([:partner, :provider])
|> put_in([Access.key(:partner), Access.key(:price_list)], nil)
|> put_in([Access.key(:provider), Access.key(:price_list)], nil)
{:reply, {:ok, %{code: 200, price_list: price_list, relationship: relationship}}, socket}
end
Any tips on how you folks manage stuff like this?
TIA!