ash_postgres/test/support/resources/comment.ex

57 lines
1.5 KiB
Elixir
Raw Normal View History

defmodule AshPostgres.Test.Comment do
@moduledoc false
use Ash.Resource,
data_layer: AshPostgres.DataLayer
postgres do
table "comments"
2022-08-19 06:56:36 +12:00
repo(AshPostgres.TestRepo)
references do
reference(:post, on_delete: :delete, on_update: :update, name: "special_name_fkey")
end
end
actions do
2022-04-20 03:08:28 +12:00
defaults([:read, :update, :destroy])
2021-02-01 10:39:59 +13:00
create :create do
primary?(true)
argument(:rating, :map)
2021-02-01 10:39:59 +13:00
2021-02-23 17:53:18 +13:00
change(manage_relationship(:rating, :ratings, on_missing: :ignore, on_match: :create))
2021-02-01 10:39:59 +13:00
end
end
attributes do
2021-01-13 14:22:28 +13:00
uuid_primary_key(:id)
attribute(:title, :string)
2021-04-05 08:05:41 +12:00
attribute(:likes, :integer)
2021-11-14 07:55:49 +13:00
attribute(:arbitrary_timestamp, :utc_datetime_usec)
create_timestamp(:created_at, writable?: true)
end
aggregates do
first(:post_category, :post, :category)
count(:co_popular_comments, [:post, :popular_comments])
count(:count_of_comments_containing_title, [:post, :comments_containing_title])
list(:posts_for_comments_containing_title, [:post, :comments_containing_title, :post], :title)
end
relationships do
2021-01-13 14:22:28 +13:00
belongs_to(:post, AshPostgres.Test.Post)
belongs_to(:author, AshPostgres.Test.Author)
has_many(:ratings, AshPostgres.Test.Rating,
2022-08-19 06:56:36 +12:00
destination_attribute: :resource_id,
2021-05-14 17:20:10 +12:00
relationship_context: %{data_layer: %{table: "comment_ratings"}}
)
has_many(:popular_ratings, AshPostgres.Test.Rating,
2022-08-19 06:56:36 +12:00
destination_attribute: :resource_id,
relationship_context: %{data_layer: %{table: "comment_ratings"}},
filter: expr(score > 5)
)
end
end