Ash FrameworkAF
Ash Frameworkโ€ข3y agoโ€ข
17 replies
frankdugan3

Better way to do this query?

I have a simple search action for an autocomplete, and I want to have it skip the filter if the search term is "" in order to just return the top sorted options. I have a pretty simple solution, but was wondering if there's a cleaner/more idiomatic way of doing it:
    read :autocomplete do
      argument :search, :ci_string

      prepare fn query, _ ->
        search_string = Ash.Query.get_argument(query, :search)

        query
        |> Ash.Query.filter(
          if ^search_string != "", do: contains(name_email, ^search_string), else: true
        )
        |> Ash.Query.load(:name_email)
        |> Ash.Query.sort(:name_email)
        |> Ash.Query.limit(10)
      end
    end

If not, no problem, this works fine. ๐Ÿ™‚
Was this page helpful?