ash_postgres/test/support/resources/post.ex

50 lines
1.1 KiB
Elixir
Raw Normal View History

defmodule AshPostgres.Test.Post do
@moduledoc false
use Ash.Resource,
data_layer: AshPostgres.DataLayer
postgres do
table "posts"
repo AshPostgres.TestRepo
end
actions do
2021-01-13 14:22:28 +13:00
read(:read)
create :create do
argument(:rating, :map)
2021-02-23 17:53:18 +13:00
change(manage_relationship(:rating, :ratings, on_missing: :ignore, on_match: :create))
end
end
attributes do
uuid_primary_key(:id, writable?: true)
2021-01-13 14:22:28 +13:00
attribute(:title, :string)
attribute(:score, :integer)
attribute(:public, :boolean)
2021-01-24 16:45:15 +13:00
attribute(:category, :ci_string)
end
relationships do
2021-01-13 14:22:28 +13:00
has_many(:comments, AshPostgres.Test.Comment, destination_field: :post_id)
has_many(:ratings, AshPostgres.Test.Rating,
destination_field: :resource_id,
context: %{data_layer: %{table: "post_ratings"}}
)
end
2020-12-29 13:26:04 +13:00
aggregates do
2021-01-13 14:22:28 +13:00
count(:count_of_comments, :comments)
2020-12-29 13:26:04 +13:00
count :count_of_comments_called_match, :comments do
2021-01-13 14:22:28 +13:00
filter(title: "match")
2020-12-29 13:26:04 +13:00
end
first :first_comment, :comments, :title do
2021-01-13 14:22:28 +13:00
sort(title: :asc_nils_last)
2020-12-29 13:26:04 +13:00
end
end
end