rgraff

rgraff

Ash Core Team

Ash Calculation on an array of maps

I have a resource that has an attribute that is an array of embedded resources.

I have module calculation that maps the embedded resources and returns a list of values. Pretty trivial in elixir. I’d like to also implement this as an Ash expression so that can be selected (and filtered against) at the data layer (postgres).

  def calculate(records, opts, _) do
    Enum.map(records, fn record -> 
      Map.get(record, opts[:embedded_resources])
      |> Enum.map(fn embedded_resource ->
        Map.get(embedded_resource, opts[:embedded_attribute])
      end)
    end)
  end

Is this possible with Ash expressions? Or should I just normalize this and have an attribute on record that is set on create/update?

I’ve experimented with doing this as a SQL fragment but that requires unnesting the embedded resource, lateral joining tables, and grouping. Technically possible, but doesn’t make for an Ash expression.

First Post!

zachdaniel

zachdaniel

Creator of Ash

There is nothing in Ash expressions that will currently let you do that unfortunately. You’ll either need to use a fragment or normalize the value you want calculated into an attribute.

Last Post!

adw632

adw632

If you’re using Postgres jsonb columns then you can do aggregates with one of the json aggregrate functions. Postgres aggregates are listed here.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement