MartinVolerich

MartinVolerich

I have an Ash Domain called Flow.Model, that has a resource called Warehouse, that in turn has a relationship to many Tank(s).

The resource has relationships properly defined.

I want to provide a function in the domain that responds with the tanks for a warehouse.

A simple implementation is (of course):

 def get_tanks_from_warehouse(warehouse) when is_struct(warehouse, Flow.Model.Warehouse) do
    warehouse = warehouse |> Ash.load!(:tanks)
    warehouse.tanks
  end

Is this the best approach to use or is there a code_gen approach that can call an action to respond with the list of the “many” child resources?

Is it “wrong” to Ash.load the relationship this way in the plain Elixir function in the domain?

I’m keen to learn the best patterns to be using for the future.

Thanks
Martin

Showing Posts 1 to 8

mindok

mindok

I don’t know what would be considered best practice, but I’d almost certainly implement this as a read action against the tanks resource using a warehouse (or warehouse id) as an argument, then expose the action via a code interface.

I happened to have this page open on another browser tab: Actions — ash v3.29.3

barnabasJ

barnabasJ

Ash Core Team

when you define a read action as a code interface you can also pass in a load

e.g. if you have

defmodule Domain do

  resource do
    resource Warehouse do
      define :get_warehouse, action: read, get_by: :id
    end
  end
end


Domain.get_warehouse(id, load: [:tanks]) 

That will give you the warehouse including the tanks

barnabasJ

barnabasJ

Ash Core Team

I’ve personally used the load option to get all the data in one place. Or if i really needed to load data later down the line I called Ash.load(resource, :rel) directly.

MartinVolerich

MartinVolerich OP

I realized that @zachdaniel had already given me pointers on this a while back - using a generic action.

My issue is that “read” actions are essentially queries that start with a universe of resource records and narrow it down & refine the set based on the filters, preparations, etc.

In this case, I simply want a functional way to get into the internals of a resource struct without flavoring the use of that function with Ash and other internal structural knowledge.

So a generic action paired with a define seems to work.

In the domain:

define :get_tanks_in_warehouse, action: :get_tanks, args: [:wh]

And in the resource:

  action :get_tanks, {:array, :struct} do
      constraints items: [instance_of: Flow.Model.Tank]

      argument :wh, :struct, allow_nil?: false, constraints: [instance_of: __MODULE__]

      run fn input, _ ->
        {:ok, wh} =
          input.params.wh
          |> Ash.load([:tanks])

        {:ok, wh.tanks}
      end
    end

This seems worthwhile as I intend to use it with ash_json_api.

Keen to hear if others think that makes sense and if there is a better approach to take.

Thanks
Martin

zachdaniel

zachdaniel

Creator of Ash

Using generic actions like this can be good, but it won’t work with ash_json_api because it’s not possible to provide a struct as input to an action. So to put this over an API you’d want the argument to be an id.

Separately, there is a thing called related routes that is exactly for this purpose that uses the primary reads of the relevant actions and then there is no need for the action at all (for ash_json_api).

MartinVolerich

MartinVolerich OP

Thanks. I found the related routes “feature” a few weeks ago.

So, in general… should I be wary of writing pure Elixir functions in the “Domain” module that simply use the defined functions and Ash semantics as needed to create the wanted behavior. So in this case, I am not sure that it was much of an improvement from the origin al simple function to the generic action wrapped via the define. Obviously the latter gives me all of the ! variants and the “can” helpers, but I am keen to get a sense of how often you (and others) simply extend the domain with plain Elixir functions.

Thanks again to you and the others in this forum for creating such a vibrant and supportive community.

Martin

zachdaniel

zachdaniel

Creator of Ash

Nothing wrong with writing those functions, just up to you if you need the benefits of a generic action or not :smile: Functions are a simple tool and often using a simple tool is good. Since you’re aware of what you can get from generic actions, you can ultimately decide whether or not you need a generic action or not.

I personally error on the side of making generic actions and when I need functions they typically exist for a reason and I group them in some way to indicate that reason. Like if I have common helpers for building a UI I might put those in their own module MyApp.MyDomain.LiveViewHelpers or something along those lines. But when in doubt, finding a way to either extend an existing domain action or create a new one often yields better fruit down in the long run.

MartinVolerich

MartinVolerich OP

Very clear and practical. Many thanks again.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews