Ash Framework

AF

Ash Framework

The Elixir backend framework for unparalleled productivity. Declarative tools that let you stop wasting time. Use with Phoenix LiveView or build APIs in minutes for your front-end of choice.

Join

support

showcase

today-i-learned

ash json patch with composite primary key

I have a resource with 2 primary keys ``` json_api do type "client_data" ...

Auxiliary Resources OK?

Hey guys, is this correct / good practice? I have 3 main categories of knowledge: Facts, Methods (or recipes, techniques), and Preferences I want to search across the three via a simple system of tags. I started with a free-form independent array of tags for each one of my main resources, but I ended up entangling myself, so I went ahead and created a Tags resource with many::many relationships in an attempt to be able to search all resources like so: ?filter[tags.name]=elixir....

How to paginate related data in json API?

This is just an example. Say I have Client and Data in many to many relationship. I have a json API to read the clients which defines a related route to read a client's related Data. Since the Data can be quite a lot, I want to force paginating it. I have a paginated read action defined in both Client and Data. However, when I call http://localhost:4000/api/v1/client/{id}/data the data is not paginated. Is this a bug or am I missing something? Here's the relevant code ```elixir...
Solution:
You can define a read action on the target resource

AshAI json schema usage

Any examples passing in json_schema to: ``` action :parse_raw, :map do argument :raw_content, :string, allow_nil?: false ...

Ash Ai connected with external MCP Servers

Hi 👋 is there an easy way to plug in mcp servers (over proxy or sse) into Ash AI?...
Solution:
You'd need to do something lower level w/ langchain.

the more tests I write, the more deadlocks I run into

The LLM wants to add max_cases to ExUnit.start(max_cases: 2) and shrink the config/test.ex App.Repo pool_size. I feel like these are bandaids and there's something fundamentally wrong with my test suite. Any tips/tricks for things to look for? Some anti-patterns in my generators.ex functions? ```bash...
Solution:
Like email: StreamData.repeatedly(fn -> "email#{System.unique_integer()}@example.com" end)

Use magic_link for both invitations and sign-in

I'd like to use the magic_link authentication strategy to both invite users, and to let existing users log in. My thinking is that in order to do that, I'll 1. Create a create action on my user resource which is limited to actor_attribute_equals(:role, :admin), and 2. Have a read action for the sign in logic. ...
Solution:
Okay so I think the way to do it would be to have two separate magic link strategies on the resource - one for sign in and one for invites. The invite one can have a longer token lifetime. When your admin is inviting someone you can trigger the request action for the invite in an after action hook.

Argument in many to many filter

I have a 2 resources Client and Data in a many to many relationship through a join resource client_data. The join resource has 2 UUID columns for the clients and data, but it also has a 3rd boolean column exposed?. When reading clients through a read action, I want to provide an arg that says to load only the exposed related Data of a Client or the non exposed or both. So I've set an argument on the read action, but now how do I access it in the filter expr on the many to many relationship?...
Solution:
```elixir prepare fn query, _ -> case query.arguments[:visibility] do :exposed -> Ash.Query.load(query, data: Ash.Query.filter(Data, parent(client_data.exposed?))) end...

Is it safe to log AuthController failures or can it leak private info?

When debugging auth problems it is helpful to log the errors in the failure function, can this lean any info or is it safe? ```elixir def failure(conn, activity, reason) do Logger.error( "Authentication activity #{inspect(activity)} failed with error: #{inspect(reason, pretty: true)}"...
Solution:
Any time you see inspect in a logger call you're likely risking leaking private data

How to get DataLayer transformer to run after optional Extension

Hi I’m working on an Ash DataLayer for Neo4j and I’ve noticed that the AshStateMachine has a transformer which adds the extended resource’s state_attribute to attributes so it introspects using Ash.Resource.Info, however this seems to only happen after my DataLayer transformer has already been run for the resource. How can I ensure in my DataLayer that my transformer runs after the optional Extension transformer?...
Solution:
You can use def after and def before to order transformers

--install igniter option clarification

Is passing --install foo,bar --install baz,quux equivalent to --install foo,bar,baz,quux?
Solution:
yes

Enforce each item is unique in attribute array

Hi, is there a way to set a constraint on this array attribute, that enforces each item to be unique? For example it should not allow [:en, :en] . ```...

Use policies to limit allowed values of Enum in create action

This is a follow-up to my question yesterday. Again, I have this Enum: ```elixir defmodule Dreng.Accounts.Role do use Ash.Type.Enum, values: [:superadmin, :admin, :farmer, :farmhand] end...
Solution:
Give the SimpleCheck a go and let us know if it helps

what is the possiblity that ash code gen does not generate migration

what is the possiblity that ash code gen does not generate migration and reporting migrations already up, I have new resources, but it is not generating migrations

newbie: incorrect code in AshPostgres examples?

https://hexdocs.pm/ash_postgres/get-started-with-ash-postgres.html#aggregates the last example there is about loading aggregates on demand, and includes in particular: ```elixir representatives = Helpdesk.Support.read!(Helpdesk.Support.Representative)...
Solution:
Just to add that you can have code_interfaces on your domain, that have functions you can call. But in that case you won't need to pass the Resource. https://hexdocs.pm/ash/code-interfaces.html

How to access property of a join resource in filter?

I have 2 resources in a many to many relationship through a 3rd join resource. However, this join resource has an additional boolean column. So now when I'm doing a filter, how do I select only those related resources whose boolean is true or false in the join_through resource?
Solution:
Nvm figured it out. Apparently this was exactly what the parent() expression is for https://hexdocs.pm/ash/expressions.html#many-to-many-relationships. I didn't know I had to give a custom name to my join_relationship or otherwise use the default _join_assoc name when accessing this field

error in creating new project

below is the command mix igniter.new it_toolbox_backend --with phx.new --install ash,ash_phoenix --install ash_graphql,ash_json_api --install ash_postgres,ash_authentication --install ash_authentication_phoenix,ash_csv --install ash_admin,ash_oban --install oban_web,ash_state_machine --install ash_events,ash_money --install ash_double_entry,ash_archival --install live_debugger,mishka_chelekom --install ash_paper_trail,cloak --install ash_cloak --auth-strategy password --auth-strategy magic_link --auth-strategy api_key --yes below is the output * creating......

Custom queries in migration

I want to execute some custom queries in my postgres migration to populate a new table. Does Ash postgres support a way to do this, or should I just edit the generated migration files?
Solution:
But editing the migration is perfectly reasonable for data migrations

Potential bug with relationship `parent` references

Bit of a gnarly one ahead... 😅 I'm running onto what I think is a bug with parent in relationship filters. My setup looks something like this: 1. A resource called GuideLevel with a has_many relationship with no_attributes? true and a filter. This relationship is called ancestors and uses an AshPostgres.Ltree column to get the ancestors of the current record, although idt the ltree behavior is relevant to the bug. The filter expression looks like: filter expr(fragment("? @> ? AND ? < ?", level, parent(level), nlevel, parent(nlevel))). Note that this references an nlevel calculation which just calculates the nlevel of the ltree column: calculate :nlevel, :integer, expr(fragment("nlevel(?)", level))...
Solution:
@Jesse Williams thanks for the tests, I've got a fix

Testing `Ash.Resource.Change` in Isolation

I'm starting to break up my resources into separate modules using Ash.Resource.Change and it's occurring to me it shouldn't be too difficult to test those modules in isolation. If I have an Ash.Resource.Change that just runs an after_transaction hook, how do I trigger that function directly?...
Solution:
At that point I might just make the function you pass to after_action a public function on the module and test that. At least I don't know of a good way to trigger the after action without running the whole action