Setting up functions using resource - define

I have a question regarding use of define in the resources section to setup api functions to simplify access to domain data. In general, this involves selecting, filtering, sorting,etc., data from the domain. I am having problems in getting the “define” forms working.
Appears to be syntax related. Could you provide a few examples of idiomatic use of Ash “define” to do things such as a function to retrieve the list of all bids for a single company.

Any references you could provide to documentation, would be helpful. I have looked at the docs related to Ash actions (read, update, etc); but, still have not been able to figure this out.

I was able to create a function on the Example.Bids.Bid list_bids_by_company(company)
which takes a company struct and filters bids by a single company’s id. This function works fine.
However, I have not been able to get any of the shortcut forms outlined in the
Ash docs to work. Any examples, advice would be greatly appreciated. I expect I am not
thinking about it properly.

Thanks in advance for your help.

================================================================================

Assume I have a Company and a Bid.
A Company has_many bids and a Bid belongs_to one company.

The application name is Example and the resources are part of a Bids Ash Domain.
There are two resources “company.ex” and “bid.ex”.
There is also a “Bids.ex” which defines the domain and lists the resources in the domain. The “resources” section of “Bids.ex” also defines some convenience functions for accessing the domain items.

Ash Resource definitions are as follows:

  1. Company Resource in the “company.ex” file

Company.ex
defmodule Example.Bids.Company do
use Ash.Resource,
domain: Example.Bids,
data_layer: AshPostgres.DataLayer

postgres do
table “companies”
repo Example.Repo
end

resource do
plural_name :companies
end

attributes do
uuid_primary_key :id

attribute :name, :string do
  description "A short, unique name for this Company"

  constraints max_length: 60,
              min_length: 1

  public? true
  allow_nil? false
end

attribute :status, :atom do
  description "Notes whether the Company is Active or Inactive"
  default :active
  constraints one_of: [:active, :inactive]
  allow_nil? false
  public? true
end

timestamps()

end

identities do
identity :uniq_company, [:name]
end

relationships do
has_many :bids, Example.Bids.Bid
end

actions do
default_accept [:*]
defaults [:create, :read, :update, :destroy]
end
end

  1. The Bid Resource in the “bid.ex” file.

Bid.ex
defmodule Example.Bids.Bid do
use Ash.Resource,
domain: Example.Bids,
data_layer: AshPostgres.DataLayer

require Ash.Query

postgres do
table “bids”
repo Example.Repo
end

resource do
plural_name :bids
end

attributes do
uuid_primary_key :id

attribute :name, :string do
  description "A short unique name for this Bid"

  constraints max_length: 60,
              min_length: 1

  public? true
  allow_nil? false
end

attribute :status, :atom do
  description "An Indicator of the Bid's Status or Progress"
  default :requested
  constraints one_of: [:requested, :preparing, :awaiting_award, :awarded, :not_awarded]
  allow_nil? false
  public? true
end

attribute :company_id, :uuid do
  public? true
  allow_nil? false
  writable? true
end

timestamps()

end

identities do
identity :uniq_bid, [:company_id, :name]
end

relationships do
belongs_to :company, Example.Bids.Company
end

actions do
default_accept [:*]
defaults [:create, :read, :update, :destroy]
end

def list_bids_by_company(company) do
Example.Bids.Bid
|> Ash.Query.filter(company_id: [eq: company.id])
|> Ash.read!()
end
end

Hello! The code samples are pretty difficult to read, if you could look into formatting them better that would be much appreciated.

As for what’s not working with the code interfaces, I think you’ll need to provide specific examples of what you tried and in what way it didn’t work to move forward.

Will do. Thanks.