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?
Sounds moving the Repo to the library could be an alternative. I see that is not how most libraries are doing it (e.g. Oban or Kaffy), but might still be an option.
Will also see if adding validate_api_inclusion?: false would help with the ash_apis issue.
What I am trying to build is a tracking library, a bit similar to Ahoy for Ruby. It will also have a dashboard, that is inspired by Phoenix Live Dashboard and Plausible Analytics.
If you want me to, I can add you as a collaborator and can give you a look. I don’t want to make it public until I have uploaded it to hex.pm. In case someone wants to steal the name.
This is interesting. I think the validate_api_inclusion?: false is the way to go for the other issue, but the more interesting one is the repo. We’ve already discussed and will likely implement soon the option to provide a function to determine the repo at runtime, in which case we can’t do those validations at compile time. That ought to resolve the issue for this case I think?
Yes, adding validate_api_inclusion?: false to use Ash.Resource helped with the missing configuration error. I also needed to add validate_config_inclusion?: false to use Ash.Api to stop the same error for the Api.
I also tried to move the repo to the library application. It works, but it does not feel very optimal. Now I have two repos, but only the one in the app is handeling the migrations. So it might be that the repo in the libray thinks it is ready. Althoug it might not be ready, since it needs to wait for the migrations to be run by the repo in the app.
So a function to determine the repo at runtime would be very welcome
Ah, yeah interesting. I don’t think we need to do that check TBH. I’ve removed it for now. That alone might solve your issue, but supporting a functional callback will also let you get some configured repo for a given resource.