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

Best Practices: Code Interfaces vs Resource Actions

Hi, everyone! I have one question: Should we use domain code interfaces from within resource actions? what are the best practices regarding when to use each of them? I'm using code interfaces from Phoenix, but would like to know whether I should be using them from within resource actions too. I think it looks cleaner, but it feels weird to call the "outer" layer from an "inner" layer...
Solution:
Its fine if you call the public interface from the internals IMO.

Ash.DataLayer.Simple with AshJsonApi.Resource

I am stuck with how a Ash.DataLayer.Simple and ash_json_api should interop. Someone can provide an example how i could make a search route work that returns one or more Resources with data layer Ash.DataLayer.Simple?

Working with large integer values in AshJsonApi

Hi. I'm working with a database with tables that have integer columns being used as an alternative ID for legacy reasons, since the primary key is a UUID. Since the attributes corresponding to these fields are configured as an :integer in the Ash resource, AshJsonApi is returning them as a number type in the JSON responses that is getting truncated due to the length of the values. For example, a value like 953764064726843398 would get truncated to 953764064726843400 (notice how the 3 least significant digits changed). The way I've worked around this in other stacks is by treating them as a string value instead. I would like to do the same here, but I don't know if there's a straightforward way to achieve that using AshJsonApi . I basically want them to be parsed to and from a string while keeping the DB column as an integer, preferably without having to change the Ash resource attribute to a :string since that would affect more than just the exposed JSON:API....

AshJsonApi relationship fields

Hi. I’m using AshJsonApi with OpenAPI and I’m struggling to figure out how to specify which fields I would like to get from a relationship. For example, I have a hardware resource which has a belongs_to: :system relationship with a system resource which is on a different domain, and I would like to fetch the hardware with its system with specific fields. If I use the following value for the fields parameter, the attributes in the response include the system with the default_fields defined for that resource, which is expected: ```json...

Error "Module must be purged before deleting" after updating dependencies

Hey there, has anyone encountered an issue when upgrading the dependencies where the mix format command prints out all these errors? ```elixir 09:36:18.362 [error] Module 'Elixir.Phoenix.HTML.Safe' must be purged before deleting 09:36:18.362 [error] Module 'Elixir.Phoenix.HTML.Safe' must be purged before deleting...
Solution:
tried again today and it is working, idk if thats the reason but had come conflicts in my elixir versions between my terminal/vscode so might have been running in 1.18 actually with the new formatter that requires 1.19 🤔

How to prevent running underlying datalayer behaviour on delete? `set_result/2` isn't working.

What is the equivalent of Changeset.set_results/2 for destroy actions? The set_result is runing the underlying datalayer behavior on delete, but according to the documentation, it should not happen. https://hexdocs.pm/ash/Ash.Changeset.html#set_result/2 What am I missing?...
Solution:
I pull the latest version, removed _build and tests started passing. https://github.com/ash-project/ash/issues/2424...

unselected attribute used in relationship filter + aggregate results in column does not exist error

I'm not sure at what level of the stack this bug is happening, but I suspect it's at the ash_sql level. I have an ltree based resource. I decided to use relationships/calculations to abstract some of the ltree details away, so I have a children relationship that looks like this (level is the ltree column, and nlevel is a calculation that is just nlevel(level)): ```elixir...
Solution:
There is an open bug about this in ash_postgres

ash_graphql update without authorizing the read action

I have a user ash resource (not using ash_authentication here) whose read action is gated by policies. I'm adding a reset_password action, which will take a reset token we emailed to the user and their new password, and if the token matches, will update their password (all of this via ash_graphql from an SPA). Because of the nature of this workflow, there will necessarily be no actor present when this action is called. In direct elixir code (such as in unit tests), this works great: ```elixir user |> Ash.Changeset.for_update(:reset_password, %{ ... }, actor: nil)...

Ash is not triggering `before_action` hook callback function when updates are atomic

Ash is not triggering before_action hook’s callback function if the update action is atomic, but when I set require_atomic? false in the action, Ash calls thecallback function (request_approval/3) as expected. The create action works well. I am not sure why the update action is not working as expected on the atomic updates. What could I be getting wrong? Below is my change:...
Solution:
Responded on ElixirForum: atomic actions don't support before action hooks

Referencing action argument in policy

Hey all. I have a create action where the argument is an instance of my Maps.Map struct, like so ``` create :add_to_map do accept [:name] ...

Problem in using manage_relationship and Ash events with tenant

Hi, this is my update action, that i use for normal user which has tenant and site_id ```elixir update :update do primary? true require_atomic? false...
Solution:
Please do!

"Bug" with Ash.Seed.upsert on attribute multitenancy with identities.

I have an identity defined as: identity :unique_name [:name] on an attribute multitenant app. The migration is well generated and creates a unique_index on [:tenant_id, :name]. But when using upsert on Seed, when I specify identity: :unique_name it does not include the tenant_id which fails with error ERROR 42P10 (invalid_column_reference) there is no unique or exclusion constraint matching the ON CONFLICT specification. The current fix is to change my identity to identity :unique_name [:tenant_id, :name]. Is this what should be expected when difining unique_indexes in multi-tenant apps?...

Adding an association in an action based on argument

Hey, been a few minutes since I've done any work on an Ash project, hoping someone can jog my memory on how to accomplish something. Basically, I've got two resources, Maps.Map and Pin , where a Pin belongs to a Map. I want to pass an existing Map to the Pin as part of the create action. like so ``` usage (via code interface)...

Using an inline aggregate of unrelated resource filtering by related resources

I believe I could do this using a custom relationship and falling back to Ecto, but I was wondering if there was a "Ash way" of doing what I'm trying to do here. I have four resources/tables SceneWidget...

Correct way to pass authorization information to query in a custom change?

I want to delete some old records in the custom change.
Is this the right way to pass authorization information: ```elixir defmodule MyApp.Changes.RemoveOldRecords do...
Solution:
The best way is to use the scope option, and pass the callback's context as the scope

Need to ensure that only 1 row for a query criteria has a specific column value

I have a Campaign and that Campaign has multiple Crew Members. Only one of these Crew Members can have the is_leader column set to true. I think what I need is to be able to UPDATE crew_members SET is_leader = true WHERE campaign_id = $1, and then update the specific Crew Member. I haven't really seen the ideomatic way of doing this in Ash.

pass attribute to expression in calculation

I have the following expression in a calculation. This works if the resource has the attribute starts_at. But how to make this expression generic that any attribute can be used. ```elixir use Ash.Resource.Calculation ...

Ash Update Many with Different Values

Hi there! I'm a bit new to Ash so apologies if I am missing something, but I couldn't find this use case in Discord or Elixir Forum. Let's say I have a Todo List with many Items. The items are ordered 1..n and I have to maintain the order when any items are updated or deleted and the user can control the order. For example,...

Ash Typescript with React Native

Ash typescript sounds promising, but there is one thing I'm not sure if I understand. If you do a React frontend that live within your assets folder, this works great since the generated files are there. But if I have a React-Native app in a separate repo, would I have to take the generated typescript file and manually put it in my React-Native repo each time I codegen? ...

How to extract first element from `many_to_many`

I have a many_to_many relationship and i want to get the first element from it. I have also a has_many relationship and can get the first element using has_one with from_many? but this is only the "merging" Resource. Is there something similar for many_to_many ? I can't use calculations or aggregations, because they can't return a resource so has_one seems to be the only one left....