forked from ash-project/ash_postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.ex
More file actions
75 lines (63 loc) · 1.75 KB
/
chat.ex
File metadata and controls
75 lines (63 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# SPDX-FileCopyrightText: 2019 ash_postgres contributors <https://github.com/ash-project/ash_postgres/graphs.contributors>
#
# SPDX-License-Identifier: MIT
defmodule AshPostgres.Test.Chat do
@moduledoc false
use Ash.Resource,
domain: AshPostgres.Test.Domain,
data_layer: AshPostgres.DataLayer
postgres do
table("chats")
repo(AshPostgres.TestRepo)
end
actions do
default_accept(:*)
defaults([:create, :read, :destroy, :update])
end
attributes do
uuid_primary_key(:id, writable?: true)
attribute(:name, :string, public?: true)
end
calculations do
calculate(
:last_unread_message_formatted_fn,
:string,
expr(last_unread_message.formatted_content_fn)
)
end
relationships do
belongs_to :last_read_message, AshPostgres.Test.Message do
allow_nil?(true)
public?(true)
attribute_writable?(true)
end
has_many :messages, AshPostgres.Test.Message do
public?(true)
end
has_one :last_message, AshPostgres.Test.Message do
public?(true)
from_many?(true)
sort(sent_at: :desc)
end
has_one :last_unread_message, AshPostgres.Test.Message do
public?(true)
from_many?(true)
filter(expr(is_nil(read_at)))
sort(sent_at: :desc)
end
has_many :unread_messages, AshPostgres.Test.Message do
public?(true)
no_attributes?(true)
filter(expr(is_nil(parent(last_read_message_id)) or id > parent(last_read_message_id)))
end
end
aggregates do
count :unread_message_count, :messages do
public?(true)
filter(expr(is_nil(parent(last_read_message_id)) or id > parent(last_read_message_id)))
end
count :unread_messages_count_alt, :unread_messages do
public?(true)
end
end
end