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

Does `AshPostgres` support partitions? i.e. Schema-based multitenancy

I checked the docs, but didn't see any reference to partitions. When you use `global? True for schema-based multitenancy, what happens? 1. The global table is partitioned, and then use the tenant partitions for the query? ...
Solution:
It creates a global table allowing you to have both tenanted and non tenanted data

How to add authorization to filter nested data

I'm running a graphql query with the following filter ``` { teams: { id: {...

Multiple Tenant Resources

In my app, I have two tenant resources: Organization and Establishment. I'm using the new scope feature and the value for current_tenant is a map containing both tenants. I need to provide an Ash.ToTenant implementation that extracts the correct tenant depending on the resource being queried. I've tried this: ```elixir...

Ash.Reactor - Transform results for `initial` or `argument`

I'm calling an API and then creating records from the response. Is there a way to transform the API result into the record attrs in the create or bulk_create step? Or is it better to have a separate transform step and use its result as the initial for create or bulk_create step? API Call step -> Transform Results to Resource attrs -> Create or Bulk Create step...
Solution:
I think you'd want to do a separate step

Is it possible to list and select the relationship in the Admin resource creation form?

For example: I'd like to list all registered users and select one to associate with the Pet I'm creating. Is there something ready to do this?
No description

ash notifier pubsub question

I've got a scenario where pub sub from one resource works, and then another resource where it does not. in the live view we're subscribed and handling: ```elixir...

JSON API raw response

I have a json API route to an action that returns a raw binary which is already gzipped in advance with :zlib.gzip(). But Ash JSON API tries to encode it with Jason and raises an error. How do I tell it to return the raw response as is?...

AshPhoenixForm not executing action on submit

Hey, i'm not sure why but when i submit a form for my update action it is not executing any update query ```elixir form = AshPhoenix.Form.for_update(data, :update, actor: actor) |> to_form() .... def handle_event("save", params, socket) do...
Solution:
shouldnt it be like this? ``` def handle_event("save", %{"form" => form_data}, socket) do case AshPhoenix.Form.submit(socket.assigns.form,...

Understanding indexes

I come from the Node.js/MongoDB world and I'm learning Elixir and Ash. I'm trying to ensure my resources/tables are correctly indexed (using AshPostgres). For that I have the following questions when using Ash: 1. Is the primary :id attribute always indexed? 2. If one would like to make a partial index like [:user_id, :is_active] we should use custom_indexes inside the postgres definition, right? - would this then also be usable by queries that only filter on :user_id (but not filtering on the :is_active )?...
Solution:
1. In Postgres at least, yes. Primary keys get a unique index. 2. This is more of a Postgres question than an ash question, I'd suggest looking at their docs. IIRC you're describing a composite index not a partial one (a partial one would be one with a where clause). 3. Yes. The identities have other uses but they imply a unique constraint and the migration generator will create one 4. Yes, that is how you'd do it 🙂...

indeterminate_datatype with fragment using the PosGIS

i need determine if a record its inside a polygon that its a param from the call, but returns ERROR 42P18 (indeterminate_datatype) could not determine data type of parameter $1 and dont really know why its happening, i tried on direct sql query and works well with the format and returns the polygon, so dont know why fails with on the format i think that maybe its injecting like POLYGON(("latitude longitude,latitude longitude") and maybe quotes its making some conflict but dont really know, same query with same data but in direct sql its working ```elixir...

How do I use a schema macro in an extension?

I want to make an Ash extension that adds a :uuid_v7_primary_key attribute to a resource. Adding an attribute via a macro: This is an antipattern but it’s a small example....
Solution:
YOu can do Ash.Resource.Builder.add_new_attribute(dsl_state, ....)

Advice for policy simplicity

Hey, I'm just getting into policies and wanted some advice on how to make this easy to work with. So I want all code interface actions to permit calling any function from the application itself or IEx. But I want all external callers to be verified for access. Is this a good start? ```elixir ...
Solution:
This option: define :foo, default_options: [authorize?: false]

Ash AI vector_cosine_distance

When attempting to use vector_cosine_distance as shown here https://github.com/ash-project/ash_ai?tab=readme-ov-file#using-the-vectors, the compiler can't find the function. I also can't find anything with that name in the ash_ai repo. How does this work?

How to call actions on an embedded resource with code interface?

I've started working on my custom authentication in my app and the first step is to make a resource representing a logged in user. This resource is embedded cause I plan to only use it as an actor. I won't save it anywhere for now. So as usual, I made some actions and a code interface for them, but I can't call them from IEx. So how do I call them? ...
Solution:
```elixir code_interface do domain YOur.Domain define :create define :from_token...

Igniter crash

I'm running into an issue on Windows where Igniter crashes with the following error during ash_authentication setup, getting an arguement error {:error, :enoent}. It seems to be coming from a call to System.cmd("tty", []) in the tty?/0 function, which fails because tty doesn’t exist on Windows....

How to compose read actions?

I have a read action with a small prepare that selects a few attributes and filters. I want to make a new read action that uses the same query as the previous read action, but also loads a calculation and a few relationships. How do I "compose" this with the old read action?...
Solution:
You don't compose them together, you share a preparation between the two actions

Custom error strings

Some of the error messages that show up on form validation read a bit awkwardly in my opinion (such as "is required"), so I would like to provide my own. Is there any way to do this other than intercepting them in the CoreComponents.translate_error/1 function in the Phoenix 1.7 project template?
Solution:
You can use the transform_errors option: https://hexdocs.pm/ash_phoenix/AshPhoenix.Form.html...

Sorting error: NoSuchField

Getting this error when trying to sort documents that have been uploaded by users:
(MatchError) no match of right hand side value: {:error, %Ash.Error.Invalid{errors: [%Ash.Error.Query.NoSuchField{resource: MyApp.Ash.Accounts.UserUpload, field: "uploaded_file_name", splode: Ash.Error, bread_crumbs: [], vars: [], path: [:sort], stacktrace: #Splode.Stacktrace<>, class: :invalid}]}}
(MatchError) no match of right hand side value: {:error, %Ash.Error.Invalid{errors: [%Ash.Error.Query.NoSuchField{resource: MyApp.Ash.Accounts.UserUpload, field: "uploaded_file_name", splode: Ash.Error, bread_crumbs: [], vars: [], path: [:sort], stacktrace: #Splode.Stacktrace<>, class: :invalid}]}}
Function that results in this error:...

Calculation returning array of strings

I have a calculation that returns an array of strings. ```elixir calculations do calculate :namevariations, {:array, :string}, fn , _ -> ["hello", "world"]...
Solution:
```elixir calculations do calculate :namevariations, {:array, :string}, fn records, -> Enum.map(records, fn _ -> ["hello", "world"] end) end...