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

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

Multiple checkbox form

I'm trying to implement https://fly.io/phoenix-files/making-a-checkboxgroup-input/ for a field that is a {:array, ;uuid}. When generating the form, using AshPhoenix, I can see that form.data and form.source have values inside the field that will be used in the multi checkbox field. However @value is always empty, so I can't tell if a given checkbox should be checked or not. I circumvented the problem by passing defining the checked flag as a third element in the options array....

How to list calculation without raw queries

I have a resource with attributes scheme and url, and a calculation :full_url. The calculation basically just concats the two with "://" in between. If I want to get a list of full urls for all my resources, I learned I can do this MyResource |> Ash.Query.load(:full_url) |> Ash.list!(:full_url). But I can't figure out how to do this as an aggregate or an action. Is it possible? Is this the right way to do it? I think aggregates always require a relationship.

Relying on metadata on "public" API an anti-pattern?

Is it generally considered an anti-pattern to rely on data that's stuffed inside metadata on a resource, especially in a "public" API? When I say public API, I mean generally following the pattern of Resource actions being used only internally in their domain and Domain actions being used externally of the domain. For example, say I have an MyApp.Accounts.Session resource that puts a token in the metadata during the create action since the token is hashed before inserting into the DB. The public API on the domain exposes it through an Accounts.create_session action. Do we really want callers to be doing Resource.get_metadata on the result in that public context to get the token or is that an anti-pattern? Is there a better way to be surfacing that information? For example: ```elixir def my_caller_func do # ... other code...

How to use field policies to show/hide fields in a UI?

I am diving into field policies to make some data available for certain subscription plans. When the field returns %ForbiddenField{}, Phoenix HTML doesn't know how to deal with it. What's the best practice for doing this in the UI? The only thing I can think of is to use is_struct(resource.field, Ash.ForbiddenField everywhere....

How to load a nested relationship with a tenant?

I'm a beginner with Ash and essentially I'd like to lookup an organization while loading some relationships, something like this: ``` Sites.Organization |> Ash.Query.load(user_roles: [:user])...

ash read_one bug?

https://hexdocs.pm/ash/Ash.html#read_one/2-examples Ash.read_one(Submission, id: submission_id) does not work ```...

Single attribute join via belongs_to

What is the correct way to join a subset of attributes to a resource through a belongs_to relation? For instance, with post has many users and post belongs_to user, i want only the public?(true) fields joined onto the post struct does this belong in a prepare() maybe? or is it a calculation?...
Solution:
you can do that with a calculation ```elixir defmodule Post do ...

Confirmation

I'm a bit confused with confirmation, following https://hexdocs.pm/ash_authentication/AshAuthentication.AddOn.Confirmation.html I have ``` confirmation :confirm do monitor_fields [:email]...

AshAuthentication trusted audiences

Hello, I'm in the process of deploying a multi-tenant application internally in my company. I'm authenticating users against Microsoft Entra ID using the OAuth2 strategy. We have two tenants that I need to allow users to authenticate from and wish to use the trusted_audiences to ensure that a user signs in from one of the tenants, but reject all others. I have set it up as described in the docs, but when I test it locally by only allowing a "fake" audience in the list of trusted audiences, I'm still authorized to login. Do I have to do anything else to make this work, or have I misunderstood something? ```elixir...
Solution:
So it seems like my options are: 1. Change to using OIDC strategy. Supports trusted_audiences but struggles with using the /common/.well-known/openid-configuration endpoint because it contains a placeholder {tenantid} in its issuer which is not supported natively by assent. This cause the issuer validation to fail, because the issuer in my token contains my real tenant-id. Could work, but requires a custom strategy? 2. Continue with using OAuth2 strategy. Works out of the box with multi-tenant applications, but needs to implement a custom trusted_audience handler either as a change function or in auth controller success callback (and then deny access unless audience matches allow-list)....

where's the best place to run possibly long running queries on the DB after an insert / bulk insert?

I have a materialized view which I need to refresh when I either do a bulk load on a table via bulk_create, or an individual insert on it. My initial idea would be to trigger an oban job via an after_action, but just want to clarify how this works for upserts etc....