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

Error tenant required in calculation on tenant resource

Hello! I'm getting an error I don't really know how to solve. It might be a dumb error from my side. I'm trying to count the number of rows of a resource inside a tenant (the 'items' table) and have it as a calculation on my tenant (the 'project' resource). This way I can know from the outside of the multi-tenancy world how many items are inside a project....

Convenience functions for many_to_many relationships?

I'm trying to wrap my head around creating convenience functions for many_to_many relationships. Given the resources below, what I'd like to do is have an add_to_hub action in the User module that takes in a Hub record and creates a corresponding UsersToHubs record. How would I go about setting up that action, and is placing a function like this inside of my User resource module at all idiomatic for Ash? I cut off the TestApp.Hubs.Hub module due to character limit, but I set it up the same way as my User module...
Solution:
I appreciate the very kind "read the manual" haha. Definitely overlooked the many_to_many example when I was reading the docs earlier. So it's basically just: ```elixir update :assign_to_hub do...

Is it possible to prevent any direct use of Ash resource?

Hi sorry i could not this in document, I want to create a policy (not ash policy) to notice developers!! they just must call domain and any query with resource in out of resource for example in LiveView is forbidden! is there any option? it is like create boundary Thank you in advance...
Solution:
No, there is no real way to prevent directly calling the resource.

Form vs simple_form

Hi guys, I see that in the Ash Framework book forms are based on simple_form but when using Ash phoenix liveview generators, the regular form is used by default. Is there a reason to use one vs the other? I incline to follow what the book does but I want to make sure this is the right decision.
Solution:
I'd also trust the book over the generators.

Global hooks

Is there any mechanism to run a before hook for any action of any resource? I have a tenant that is an org, and an actor that is a membership. Membership also has a team association I want to load in the context of any action....

Using add_form with a many_to_many relationship

Trying to use add_form on a many_to_many relationship. Here is my action and relationship. ```elixir create :create_campaign_target_list do primary? true...

🔹 How to Regenerate a Specific Resource Using `ash.codegen` Without Affecting Others?

Hi, I've read the ash.codegen (https://hexdocs.pm/ash/Mix.Tasks.Ash.Codegen.html) documentation but couldn't find an option related to this. I'm still in the early stages of development and sometimes make mistakes while defining resources (e.g. when generating multiple tables). When that happens, I want to regenerate just a specific resource from scratch, not create a new migration to fix it. However, ash.codegen seems to regenerate all active resources at once. To avoid that, I have to manually remove the files of the resource I want to regenerate and then run the generator again. ...
Solution:
I don't think so. migrations are for one version of your database to the next. your app won't run if your migrations are incomplete and your resources don't match your database

Subscription to automated resource creation

I have a question regarding subscriptions. I've set up subscriptions in my application and I have one subscription endpoint that works perfectly. I have a 2nd one that isn't working at all. However, the 2nd one monitors a resource that is created in an automated way from the backend by an ash notifier when a different resource is created. Example: Let's pretend we have a Message Resource and an Alert Resource. We have a Ash.Notifier that on create checks if the message has the keyword "foo". If it is, we create an Alert . If I have a subscription to the Alert resource, should I expect that subscription to work even though it wasn't created from the frontend? I would assume yes but that is not what I'm experiencing....

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