You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
At the moment, I just can think in decorate each relation by hand. Somethig like this:
defpreload(users_query,preloads)dousers_query|>Repo.all()|>Repo.preload(preloads)|>Enum.map(&decorate_preloads(&1,preloads))enddefpdecorate_preloads(struct,preloads)doEnum.reduce(preloads,struct,,fn(preload,struct)->decorate_preload(struct,preload)end)enddefpdecorate_preload(struct,preload)doMap.put(struct,preload,do_decorate_preload(Map.get(preload)))end# If the preload is a list, decorate each elementdefpdo_decorate_preload(preloads)whenis_list(preloads)doEnum.map(preloads,&do_decorate_preload/1)end# If custom decorations are neededdefpdo_decorate_preload(%Post{}=post)doDecoratex.decorate(post,except: :comments_count)end# Default decorationsdefpdo_decorate_preload(preload)doDecoratex.decorate(preload)end
But you will need more pattern matching and complexity if you use keyword lists for nested preloads.
Hi all, thank you for the hard work. I have a question.
Suppose that I have a decorated field in the Post schema, but then I do something like
where preloads is dynamic, and could be i.e.
[:posts, :comments, :likes, ...]
Which is the correct way to work in cases where you preload the relations, and those relations have decorated fields?
Thank you!
The text was updated successfully, but these errors were encountered: