torepettersen
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?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
jimsynz
Hi @torepettersen
Hrm. This is a tricky one and not something that I think was a foreseen use case.
Regarding the repo, I think that the only thing you can do is define a repo in your library codebase and require them to add configuration for it.
Regarding the
ash_apisconfiguration - it’s currently hard coded to look in the application environment for this configuration which is actually done while validating resources, so there is the possible workaround of passingvalidate_api_inclusion?: falseas an option to youruse Ash.Resourceline in each resource.Can you tell us more about your use case? Perhaps there’s a better alternative.
torepettersen
Hi @jimsynz,
Thanks for the quick reply.
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?: falsewould help with theash_apisissue.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.
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
This is interesting. I think the
validate_api_inclusion?: falseis 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?torepettersen
Yes, adding
validate_api_inclusion?: falsetouse Ash.Resourcehelped with the missing configuration error. I also needed to addvalidate_config_inclusion?: falsetouse Ash.Apito 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
zachdaniel
I’ve just pushed something to main that will allow providing a function to
repo, something like this should do what you want:EDIT: keep in mind its a bit experimental though.
torepettersen
Thanks for pushing this change so quickly.
Hovever seems like it is not working for me. Wondering if I am doing something wrong.
I pulled the latest
ash_postgresfrom github:I added the repo function like you describe to the resource:
But I am getting an error like this:
Any clue what could be wrong?
zachdaniel
was an oversight. Just pushed up a fix.
torepettersen
Thank you very much!
That seems to work like a charm
I noticed I still got the error when compiling, but now I can work around that by providing a dummy repo when compile checks are done.
Then setting the variable when my application is starting.
zachdaniel
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.