torepettersen
How to use Ash in a library?
Hi,
I am trying to use Ash in a library, and I am wondering if that is a good idea?
Seems like the biggest problem I have, is that my library does not compile as long as I use AshPostgres in the resources. The issue seems to be that the MyApp.Repo would not be compiled in time, since the repo is living in the app that will be compiled after the library.
The error I am getting is:
** (EXIT from #PID<0.99.0>) an exception was raised:
** (RuntimeError) Error while running transformer AshPostgres.Transformers.PreventMultidimensionalArrayAggregates: "Could not find repo module Elixir.Demo.Repo"
(spark 1.1.50) lib/spark/dsl/extension.ex:634: Spark.Dsl.Extension.raise_transformer_error/2
(elixir 1.15.6) lib/enum.ex:4830: Enumerable.List.reduce/3
(elixir 1.15.6) lib/enum.ex:2564: Enum.reduce_while/3
(elixir 1.15.6) lib/enum.ex:984: Enum."-each/2-lists^foreach/1-0-"/2
(elixir 1.15.6) lib/module/parallel_checker.ex:271: Module.ParallelChecker.check_module/3
(elixir 1.15.6) lib/module/parallel_checker.ex:82: anonymous fn/6 in Module.ParallelChecker.spawn/4
Is this something that would be possible to solve?
The second problem I face is defining ash_apis in the config. It does not seem like something the user of the library should be defining. Not the ideal solution, but I thought it could be possible to define a MyLibrary.ash_apis/0 that could be called in the config. That however also fails with this error:
** (UndefinedFunctionError) function MyLibrary.ash_apis/0 is undefined (module MyLibrary is not available)
MyLibrary.ash_apis()
/home/tore/workspace/demo/config/config.exs:41: (file)
(stdlib 5.1.1) erl_eval.erl:750: :erl_eval.do_apply/7
(stdlib 5.1.1) erl_eval.erl:1026: :erl_eval.expr_list/7
(stdlib 5.1.1) erl_eval.erl:292: :erl_eval.expr/6
(stdlib 5.1.1) erl_eval.erl:282: :erl_eval.expr/6
(stdlib 5.1.1) erl_eval.erl:283: :erl_eval.expr/6
That is something I can work around by having the user of the library list all the API’s in the config. Not ideal, but possible.
The last thing I notice is not a problem, but a more slight inconvenience for someone that uses the library and don’t want to have any traces of Ash in their own app. Since my library relies on the Repo defined by the app, the users of the library would need to replace Ecto.Repo with AshPostgres.Repo. As mentioned, not a showstopper, but would it be possible to just use the MyApp.Repo already defined in most Phoenix apps instead of the AshPostgres.Repo wrapper of it?
So I am wondering if these issues are something that can be resolved or if I should rather remove the usage of Ash from my library?
Most Liked
mindok
The “Ash Way” is to accidentally publish v1.0 to hex before it’s ready: https://youtu.be/c4iou77kOFc?si=wpDiK3jKOHtSh8vp&t=793 ![]()
zachdaniel
I’ve just pushed something to main that will allow providing a function to repo, something like this should do what you want:
repo &YourApp.Helpers.repo/2
defmodule YourApp.Helpers do
def repo(resource, _), do: YourDynamicRepo
end
EDIT: keep in mind its a bit experimental though.
zachdaniel
was an oversight. Just pushed up a fix.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








