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

mark Ash.Resource extension attributes as public

Other than redefining the extension attributes in my own resource, is there a better way?
extensions: [AshDoubleEntry.Transfer, AshGraphql.Resource]
extensions: [AshDoubleEntry.Transfer, AshGraphql.Resource]
...
Solution:
🤔 you could write your own extension that would mark amount as public after AshDoubleEntry.Transfer creates it

Query load and sort

I have a table locations with a geometry field :geom (using ash_geo + geo_postgis). When a user lands on a page and geolocates, the list of locations should sort ASC based on the user location and show the distance away in miles. My current query is: ``` query...
Solution:
Aha I found the issue was a Enum.reduce flipping the order later on

Working with AshArchival

I'd like to to implement a filter that allows the user to read their archived records. I'm trying this:
Ash.Query.filter_input(query, %{"archived_at" => %{"is_nil" => false}})
Ash.Query.filter_input(query, %{"archived_at" => %{"is_nil" => false}})
But for some reason I get no results...
Solution:
So if you make like an :include_archived action for example, exclude that action in the ash archival DSL, you can then pass action: :include_archived to Ash.read

Forbidden magic link

Can't get magic link to work all of a sudden. I receive the magic link in dev/mailbox but when I click it I'm taken to a page that has a "sign in" button, and once I click that, I get: ``` [warning] Authentication failed: Bread Crumbs:
Error returned from: MyApp.Accounts.User.sign_in_with_magic_link...

Accessing_from policy not authorizing read preparation

I got stuck not really understanding how to use accessing_from I have a read action on my user with this prepare: ```...

Setting up a has_many relationship through another resource?

For instance, how do you translate this Ecto query?
has_many(:comments, through: [:posts, :comments])
has_many(:comments, through: [:posts, :comments])
...

Ash Policies around :change_password action

I have a User resource with (mostly) the default update :change_password action generated by the igniter installer, ```elixir update :change_password do # Use this action to allow users to change their password by providing # their current password and a new password....
Solution:
Yes, you could set up a policy like that 👍

Global Analytics

I'm trying to generate global analytics from my resources: - my first attempt was to define aggregates on the resources itself but in my understanding those only work within relationships - now I'm working my way through defining a dataless resource which has a bunch of no_attributes? true relationships but I'm getting some errors including ``` errors: [...

Multiple confirmation strategies with interaction

It appears to be impossible to use require_interaction? true with more than one confirmation strategy, because using the confirm_route macro more than once produces a compile-time error.
confirm_route MyApp.Accounts.User, :confirm_new_user, auth_routes_prefix: "/auth"
confirm_route MyApp.Accounts.User, :confirm_email_change, auth_routes_prefix: "/auth"
confirm_route MyApp.Accounts.User, :confirm_new_user, auth_routes_prefix: "/auth"
confirm_route MyApp.Accounts.User, :confirm_email_change, auth_routes_prefix: "/auth"
...
Solution:
but yes, the two routes need to be disambiguated, via the as option

Optional argument with defaults for actions

It feels like it should be something really simple but its just not working out. I have a resource with a non-null string attribute that defaults to an empty string ```elixir attributes do...
Solution:
You can also constraints: [allow_empty?: true]

Typed array attributes from `ash.gen.resource`

Hey guys, can array attributes be typed in Ash via generators? When I run this: ```shell mix ash.gen.resource MyApp.MyDomain.MyThing --attribute my_list:{array,string}:public...

Setting attributes after getting data in before_transaction - calling validation in change_attribute

I have a User resource which requires email and a name. I want to expose a create type action on this resource, but all I have available when calling the action is a token. That token is in a 3rd party system, which returns the name and email. This resource has global validation on name and email, which I'd rather not change (or mark as before_action?: true) since I do want them to be run up front for other actions. What I was hoping to do is in a before_transaction block, make the API call to get the data and set the attributes with change_attribute, or put them in a context and call set_attribute in a change block. However, when I do this, since the initial changeset doesn't have an email, it fails validation and the before_transaction never runs. If I pass in a placeholder email, I can get the before_transaction to run, but then I get warnings when calling change_attribute because the changeset has already been validated, and it's actually possible to pass in a bad email here and it'll get written to the database. Another idea I had was to try and do this as a generic action, and then in the run block I would make the API call, get the user data, and then manually call the existing :create . However, I need to return metadata (access tokens) for the user, but it seems like generic actions don't allow for metadata blocks, and since this action will be exposed via GraphQL, I'd want that typing of the metadata attributes on the query/mutation....

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 🙂