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

How to get tenant in LiveView after setting it using `PlugHelpers.set_tenant`

I'm currently setting my tenant in my controller like this:
conn
|> put_session(:tenant, tenant_id)
conn
|> put_session(:tenant, tenant_id)
...
Solution:
ok I think we are going in the right direction, so the authcontroller code is called when you sign in, but then it redirects and there you get a new conn and the browser pipeline only loads the user from the session. You need another plug/controller that you put after load_from_session, where you put this code ```elixir tenant = if user.is_super_admin? do...

install is not the latest version

mix archive.install hex igniter_new only install 0.5.29 instead of 0.6.10...

Translating Ash Error messages

What are my options for translating error messages returned by actions? The first thing that comes to mind is translating the errors in the controller like this: ```elixir...

Is it possible to convert policies expression to Ecto expressions?

Kinda of a weird request but I'm wondering if it's possible to resolve the policies expressions so I can reuse them in a regular Ecto query. I started with this code but I'm new to Ash and a bit lost here: ```...

Wrapping external API - get one object

Hey 👋 I'm starting to learn Ash and I'm trying to use Ash.Resource to wrap an API. Looking at the example at https://hexdocs.pm/ash/wrap-external-apis.html, I can see how I would do that for a paginated list endpoint. Now I'm trying to figure out how to do something similar that returns one object and not a list. The list example uses Stream.resource and Ash.Query.apply_to, so it's not clear to me how to proceed....
Solution:
Read actions return multiple results always

Help with multi-step resource creation in Ash (Phoenix + React)

I’m building a multi‑tenant app with this structure: user → organization → establishment → establishment_user In my flow, a user creates both an organization and an establishment, and the system automatically creates the establishment_user behind the scenes. ...
Solution:
1. after_action was the right choice, you can't set a different tenant with manage_relationship AFAIK 2. all run in the same transaction, the first action call opens the transaction and all the after action starts the next action in the same transaction 3 . all accessing_from is checking is a certain value in the context, so you could just manually set the context when calling the other actions inside after_action...

Using Pgvector and Ash.Vector

Greetings! I have a few Ecto schemas using Pgvector.Ecto.Type. I've been attempting to slowly move them over to Ash resources using the Ash.Type.Vector but I've been unable to get both extensions to coexist. I tried globally defining both: Pgvector.extensions() ++ [AshPostgres.Extensions.Vector] ++ Ecto.Adapters.Postgres.extensions() , but depending on that list order one or the other breaks. I also tried matching on type to get around it in a custom extension, which works for encoding: ```...

Version 3.5.25 generates wrong SQL

``` * ** (Postgrex.Error) ERROR 3F000 (invalid_schema_name) schema "excluded" does not exist query: INSERT INTO "tokens" AS t0 ("subject","purpose","created_at","expires_at","jti","updated_at") VALUES ($1,$2,$3,$4,$5,$6) ON CONFLICT ("jti") DO UPDATE SET "subject" = EXCLUDED.identifier('subject'), "purpose" = EXCLUDED.identifier('purpose'), "expires_at" = EXCLUDED.identifier('expires_at'), "updated_at" = COALESCE(EXCLUDED.identifier('updated_at'), $7) RETURNING "updated_at","created_at","extra_data","purpose","expires_at","subject","jti" ...
Solution:
updating only ecto causes this issue

Query across many tenants

I am developing an application with postgres attribute multitenancy and there is one query that I want to make across multiple tenants (tenants will be provided as argument) I was wandering if there is a way to do that somewhat simpler and more idiomatic, then just making a custom query and running a query multiple times. I also have nested fields to resolve for each element, so I would need to resolve every dependency somehow too....
Solution:
You can make specific read actions allow bypassing multitenancy with multitenancy :bypass

Ash.Actions.Read.AsyncLimiter - Process is not alive or there`s no process .....

Hi! I am at a loss here with trying to debug the origin of this error. Everything works when I perform a normal Ash.read, but as soon as I use Ash.page I get the following error....

Is it possible to disable pagination entirely for a read action?

I'm using AshGraphQL, and I have a pretty bespoke read action that through some reasonably complex logic related to some custom arguments kind of "self-paginates" - the implementation and design reasons for this are more than is worth explaining here, and yes, I wish it were different than it is and may even refactor away from this in the future, but that will take some time to do... for now, suffice to say that when generating my graphql schema, I'd really love to be able to not show the pesky limit and offset params in queries/relationship loads, since the bespoke logic of the action kind of already accomplishes what pagination is doing, so it's confusing for the front end devs that consume my API to see those pagination params that are functionally redundant. I found paginate_with and paginate_relationship_with, which seem to imply that I might be able to pass nil to disable pagination, but afaict that doesn't seem to do it. Is this something that's supported?...
Solution:
You'll have to add a :none atom

Is it possible to "pluck" a sibling using an expr?

I have resources - Group -> has_many formations - Formation (group_id, lithostrat_order) I would like to have a calc on formation that goes via parent group to select a formation with lithostrat_order + 1 ...
Solution:
so I've done stuff like this before with a relationship. eg. this is for a set of records called OperationExecutions: ```elixir has_one :previous_execution, MODULE do no_attributes? true...

type `vector` can not be handled by the types module Postgrex.DefaultTypes

It seems when I try to save embeddings into db, it shows this error. Here are my settings: 1. lib/postgrex_types.ex ```elixir Postgrex.Types.define(...
Solution:
just the relevant parts for now

Can I pass an argument to a custom policy check

I have the following resources: publisher, book, chapter, user, permissions. My relationships look like the following: Users are given permissions to a publisher. These permissions are an array of arbitrary strings. ...

AshOban and Policies

I noticed that on initial project which has policies: ``` policies do bypass AshAuthentication.Checks.AshAuthenticationInteraction do authorize_if always()...
Solution:
bypass AshObanInteraction do
authorize_if always()
end
bypass AshObanInteraction do
authorize_if always()
end
...

Short-lived Tokens in Ets-backed Resource

I was wondering if it makes sense to store short-lived tokens in an ets-backed resource. The tokens are created -> read -> deleted in usually < 2 minutes and they should have ttl of 5 minutes. I could just do everything in postgres but perhaps it makes sense to do it in a cache in memory. Initially I thought of using ets and use it directly through changes/preparations (with the set_result thing) or just use a normal phoeinx controller and deal with those endpoints myself. Then I remembered about the ETS data layer but the notice of not recommended in production kinda scared me. So I have like these 4 directions to take (ets-backed resource, phoenix controller using ets, ets via changes/preparations on a postgres-backed resource, just postgres) and not sure which I should do. So just seeking some advice 😅...

How can I return vector_cosine_distance from search results?

I'm currently testing AshAi's embeddings support, and implemented a search based on the docs. I struggle to return the result of vector_cosine_distance(full_text_vector, ^search_vector) for debugging purposes. I tried to use a calculation, but I won't have access to search_vector. In Ecto I'd use a virtual field and use select_merge. What is the correct approach in Ash here? ```elixir read :search do argument :query, :string, allow_nil?: false...
Solution:
You can use calculations with arguments, and load that calculation in this preparation

How to annotate generic action returning records

If I have a generic action on a resource that returns a single record of that resource, how do I annotate the return type of the action? __MODULE__ is not recognized so for now I'm using :struct but I don't think that's completely right.

Issue when attempting to encrypt union fields with AshCloak

I get an error during compilation when attempting to encrypt a union field with AshCloak. I have a minimal example here. https://github.com/evhinesdev/ash-union-cloak-problem-example/tree/main This was generated from the ash-hq.com generator with the Example.Test resource added. Is this an issue with how I am using the extension or are unions not valid targets of AshCloak?...