Skip to content

Commit 388364d

Browse files
author
riccardocucia
committed
Wip views
1 parent 30692a9 commit 388364d

File tree

2 files changed

+86
-8
lines changed

2 files changed

+86
-8
lines changed

doc/docs/guides/supabse-views.md

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ title: Database Views
55

66
## Rooms view
77

8-
This is a view of `rooms` table, this view allows you to obtain the name of the sender of the message dynamically in direct rooms, based on the logged-in user the name of the correspondent is displayed.
8+
This is a view of `rooms` table, this view allows you to obtain the name of the sender of the message dynamically in direct rooms, based on the logged-in user the name of the correspondent is displayed, it is also included the list of uses member objects.
99

1010
```sql
1111
DROP VIEW IF EXISTS chats.rooms_l;
12-
create view chats.rooms_l
13-
WITH (security_invoker='on') as
12+
create or replace view chats.rooms_l
13+
with (security_invoker='on') as
1414
select
1515
r.id,
1616
r."imageUrl",
@@ -29,6 +29,47 @@ select
2929
r."lastMessages",
3030
r."userRoles",
3131
r."createdAt",
32-
r."updatedAt"
32+
r."updatedAt",
33+
(select jsonb_agg(to_jsonb(u))
34+
from chats.users u
35+
where u.id = any(r."userIds")) as users
3336
from chats.rooms r;
3437
```
38+
39+
40+
## Messages view
41+
42+
This is a view of `messages` table, this view allows you to obtain the author user object and room object.
43+
44+
```sql
45+
DROP VIEW IF EXISTS chats.messages_l;
46+
create or replace view chats.messages_l as
47+
select
48+
m.id,
49+
m."createdAt",
50+
m.metadata,
51+
m.duration,
52+
m."mimeType",
53+
m.name,
54+
m."remoteId",
55+
m."repliedMessage",
56+
m."roomId",
57+
m."showStatus",
58+
m.size,
59+
m.status,
60+
m.type,
61+
m."updatedAt",
62+
m.uri,
63+
m."waveForm",
64+
m."isLoading",
65+
m.height,
66+
m.width,
67+
m."previewData",
68+
m."authorId",
69+
m.text,
70+
to_jsonb(u) as author,
71+
to_jsonb(r) as room
72+
from chats.messages m
73+
left join chats.users u on u.id = m."authorId"
74+
left join chats.rooms_l r on r.id = m."roomId";
75+
```

example/utils/sql/05_database_view.sql

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
DROP VIEW IF EXISTS chats.rooms_l;
2-
create view chats.rooms_l
3-
WITH (security_invoker='on') as
2+
create or replace view chats.rooms_l
3+
with (security_invoker='on') as
44
select
55
r.id,
66
r."imageUrl",
@@ -19,5 +19,42 @@ select
1919
r."lastMessages",
2020
r."userRoles",
2121
r."createdAt",
22-
r."updatedAt"
23-
from chats.rooms r;
22+
r."updatedAt",
23+
(select jsonb_agg(to_jsonb(u))
24+
from chats.users u
25+
where u.id = any(r."userIds")) as users
26+
from chats.rooms r;
27+
28+
DROP VIEW IF EXISTS chats.messages_l;
29+
create or replace view chats.messages_l as
30+
select
31+
m.id,
32+
m."createdAt",
33+
m.metadata,
34+
m.duration,
35+
m."mimeType",
36+
m.name,
37+
m."remoteId",
38+
m."repliedMessage",
39+
m."roomId",
40+
m."showStatus",
41+
m.size,
42+
m.status,
43+
m.type,
44+
m."updatedAt",
45+
m.uri,
46+
m."waveForm",
47+
m."isLoading",
48+
m.height,
49+
m.width,
50+
m."previewData",
51+
m."authorId",
52+
m.text,
53+
to_jsonb(u) as author,
54+
to_jsonb(r) as room
55+
from chats.messages m
56+
left join chats.users u on u.id = m."authorId"
57+
left join chats.rooms_l r on r.id = m."roomId";
58+
59+
60+

0 commit comments

Comments
 (0)