Custom complex SQL query in Ash

Hello,

I need to execute a very complex SQL query for a resource. It’s a Booking resource, and I want a read action like get_available_slots(date, time, duration)

The SQL is very complex, with several CTEs and window functions. What I want as a result is not an instance of a resource, but a simple list of available times.

Ex:

 available_start_time 
══════════════════════
 09:00:00
 09:15:00
 09:30:00
 09:45:00
 10:00:00
 10:15:00
 10:30:00
 10:45:00
 11:00:00
 11:15:00
 11:30:00
 11:45:00
 12:00:00
 14:30:00
 ...

What is the best approach to achieve this in Ash?

Thanks!

I found this: Manual Actions — ash v3.4.56

I guess I have to execute the query in the function:

def read(ash_query, ecto_query, _opts, _context) do
    ...
    {:ok, query_results} | {:error, error}
  end

Is this correct?

Look into generic actions, instead of manual actions :slight_smile:

https://hexdocs.pm/ash/generic-actions.html

1 Like