torepettersen

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?

Showing Posts 1 to 10

jimsynz

jimsynz

Ash Core Team

Hi @torepettersen :wave:

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_apis configuration - 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 passing validate_api_inclusion?: false as an option to your use Ash.Resource line in each resource.

Can you tell us more about your use case? Perhaps there’s a better alternative.

torepettersen

torepettersen OP

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?: 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.

mindok

mindok

The “Ash Way” is to accidentally publish v1.0 to hex before it’s ready: https://youtu.be/c4iou77kOFc?si=wpDiK3jKOHtSh8vp&t=793 :joy:

zachdaniel

zachdaniel

Creator of Ash

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?

torepettersen

torepettersen OP

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 :blush:

zachdaniel

zachdaniel

Creator of Ash

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.

torepettersen

torepettersen OP

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_postgres from github:

{:ash_postgres, github: "ash-project/ash_postgres"},

I added the repo function like you describe to the resource:

postgres do
  table "sessions"
  repo &MyApp.Helpers.repo/2
end

But I am getting an error like this:

** (EXIT from #PID<0.99.0>) an exception was raised:
    ** (RuntimeError) Exception in transformer AshPostgres.Transformers.PreventMultidimensionalArrayAggregates on MyLib.Tracker.Session 

no function clause matching in Code.ensure_compiled/1
        (elixir 1.15.6) lib/code.ex:1858: Code.ensure_compiled(&MyLib.Helpers.repo/2)
        (ash_postgres 1.3.60) lib/transformers/prevent_multidimensional_array_aggregates.ex:41: AshPostgres.Transformers.PreventMultidimensionalArrayAggregates.transform/1
        (spark 1.1.51) lib/spark/dsl/extension.ex:577: anonymous fn/4 in Spark.Dsl.Extension.run_transformers/4
        (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

Any clue what could be wrong?

zachdaniel

zachdaniel

Creator of Ash

was an oversight. Just pushed up a fix.

torepettersen

torepettersen OP

Thank you very much!

That seems to work like a charm :blush:

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.

def repo(_, _) do
  case Application.get_env(:my_lib, :started, false) do
    true -> Application.get_env!(:my_lib, :ecto_repo)
    false -> MyLib.Repo
  end
end

Then setting the variable when my application is starting.

defmodule MyLib.Application do
  use Application
  def start(_type, _args) do
    Application.put_env(:my_lib, :started, true)
    # ...
  end
end
zachdaniel

zachdaniel

Creator of Ash

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.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews