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

Complex resource generation?

Hey guys, I'm getting a bit of chicken & egg problem when I try to create my resources using generators. As most of them are somehow related to one another, I get all sorts of compile time errors, no matter which ones I try to run first. How do you handle these situations yourselves? Is it at all possible to build my app using generators, or do I have to manually write most of them? For example, a Subtitle belongs to a Video, and a Video has many Subtitles. Whenever I try to crate one, the othe must be already present, otherwise I get an error. Maybe I'm doing something wrong? ```bash...

Any reason expressions are not supported in validations?

I would like to prevent an update action to be executed when a particular "sub-resource" has an attribute set to a particular value. For example "mark order as complete only if its shipment is complete". So it would be nice to write it like this: ```elixir update :mark_complete do...

Policy references

As shown in the diagram, I'm in a scenario where a container resource has a policy that permits access by either being the creator or a collaborator. ```elixir policy action(:read) do authorize_if relates_to_actor_via(:creator)...
Solution:
My suggestion is to use calculations for this.
No description

Best way to consist between attributes validation and Postgres level

Hi, sorry, I just try to learn and compare my code with the best ways that i can be able to find. I just want to know, is there auto way to create constraints? For example based on attributes constraints? or it should be written by manual check_constraints ? ```elixir...
Solution:
It needs to be written manually with check constraints as you have done it. We may add that feature in the future

Atomic array removal

Greetings! I'm attempting to remove an element from a string array attribute from all records that have it, but do it atomically with psql's array_remove. I have the following change: ``` attribute :thing_ids, {:array, :string}, public?: true...

Is there an alternative way to select certain fields without using Ash.Query?

Currently I'm using it like: ```elixir MyApp.Domain.Resource |> Ash.Query.limit(1) |> Ash.Query.select([:slug])...

Custom query run in action

I'm wondering how to run a regular query (SELECT ...) in an action for example using Repo.query! 🤔 my attempts fail. Seems I can't use the Repo module in an action 😬
Solution:
I literally showed you how to do it. ```elixir action :activity_thismonth, :map do run fn input, -> """...

How to wrap multiple bulk updates in transaction

I have a list of maps that I want to use in a bulk update in my database. The list looks something like this:
[%{id: 1, exposed: true}, %{id: 2, exposed: false}, %{id: 3, exposed: true}, ...]
[%{id: 1, exposed: true}, %{id: 2, exposed: false}, %{id: 3, exposed: true}, ...]
...
Solution:
Yep 🙂

AshRateLimiter - warning that unrelated update actions cannot be done atomically

I'm playing around with AshRateLimiter and have something like this: ```elixir rate_limit do hammer MyApp.Hammer...

Unable to create a resource with an array attribute

```elixir defmodule Galenic.Categorization.Request do use Ash.Resource, domain: Galenic.Categorization, data_layer: AshPostgres.DataLayer...
Solution:
your create action doesn't accept the columns attribute

Access calculation from relation caluclation.

I got a resource A the defines ``` calculate :access_count, :integer, Accounts.User.Calculations.AccessCount do public? true end...

Supports callback in Ash Validations

Is the supports/1 callback for the validation behaviour no longer available in Ash? Copied and pasted this email validation example from the Ash docs https://hexdocs.pm/ash/validations.html#supporting-queries-in-custom-validations ```elixir...
Solution:
Latest version of Ash only.

How to use one Enum value for REST/GraphQL, serialized as another to the database

Hello all, I'm still new to Elixir and the Ash framework. I'm trying to create a set of values to use for both the REST and GraphQL interfaces of my API to have values spelt out such as north, north_east, etc. spelt out. However when I read or write that field to the database, I'd like to have it serialized as string values such as N, NE, etc. Here's the code I have so far: ```elixir defmodule My.Direction do use Ash.Type.Enum values: [...
Solution:
You can override the cast_stored and dump_to_native callbacks on the type

Having trouble with AshGraphql subscriptions dsl

I created a subscription: ```elixir subscriptions do pubsub __.Endpoint subscribe :resource_name_updated do...

How to revalidate a nested AshPhoenix.Form update from an handle_event

I have this handle_event to update the value of a single nested for which has buttons to reset the input/set 50%/set100%: ```elixir def handle_event( "set_text", %{"form_path" => form_path, "field_name" => field_name, "text" => text},...
Solution:
That looks like how you'd do it currently to me

What should be the behavior of `after_action`?

The function in after_action is being called before prepare build(load: [:roles]). Is this normal behavior? ```elixir read :get_by_subject do description "Get a user by the subject claim in a JWT"...

Access magic link token as string for testing

I'm trying to write a test where a user first requests a magic link and then uses the token to log in. How can I get the generated JWT token in clear text? When doing something like this I get a token struct: ```elixir test "It is possible to sign in with token" do admin = generate(user(role: :admin))...
Solution:
I've used the test mailer for this from swoosh

Installer crash

I'm trying to install ash with some goodies but I hit an unrecoverable error and everything crashes. I'm using OTP 27 per Zach's recommendation relevant logs...

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