Ash FrameworkAF
Ash Framework3y ago
3 replies
stephan8843

How to define custom sort?

I'm trying to get started with Ash but I'm really struggling with the basics. I'm trying to do something like this:

defmodule MyResource do
  use Ash.Resource, data_layer: AshPostgres.DataLayer

  actions do
    read :custom_sort do
      argument :sort, :atom do
        constraints [one_of: [:newest, :oldest]]
        allow_nil? false
      end
      
      build(sort: [inserted_at: expr(^arg(:sort) == :newest do :asc else :desc end)])
    end
  end
end

but this doesnt work. Nevermind if it makes sense. I'm really just trying to find out how I can supply an argument to an action and do something with it. I have looked through the docs to no avail. The docs on expr don't deal with conditions and also apparently args can't be used in sort which is also not covered in the docs, so I'm really lost. Any help would be appreciated.
Was this page helpful?