Skip to content

Commit 05cb266

Browse files
authored
Fix schema permission (#18)
## [1.3.2] - 2024-11-13 #### [@rickypid](https://github.com/rickypid) Improve documentations ### Fixed * Fixed schema `chats` permission after view creation
1 parent 45b70ee commit 05cb266

File tree

11 files changed

+66
-5
lines changed

11 files changed

+66
-5
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## [1.3.2] - 2024-11-13
2+
#### [@rickypid](https://github.com/rickypid)
3+
4+
Improve documentations
5+
6+
### Fixed
7+
8+
* Fixed schema `chats` permission after view creation
9+
110
## [1.3.1] - 2024-11-12
211
#### [@rickypid](https://github.com/rickypid)
312

doc/docs/guides/supabse-indexes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ id: supabase-indexes
33
title: Database Indexes
44
---
55

6+
## `chats.messages` indexes
7+
68
These indexes are added to improve the performance of foreign keys in database tables:
79

810
```sql

doc/docs/guides/supabse-trigges.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ id: supabase-triggers
33
title: Database Triggers
44
---
55

6-
This is an example of a triggers that sets room's `lastMessages` to the most recent message sent once recieved in Supabase.
6+
## Update room last message
7+
8+
This is a trigger that sets room's `lastMessages` to the most recent message sent once recieved in Supabase.
79

810
```sql
911
CREATE OR REPLACE FUNCTION chats.update_last_messages()
@@ -29,6 +31,8 @@ CREATE TRIGGER update_last_messages_trigger
2931
EXECUTE FUNCTION chats.update_last_messages();
3032
```
3133

34+
## Set message status to sent
35+
3236
"This trigger, on the other hand, is responsible for setting the message status to `sent` when it is added to the `messages` table:
3337

3438
```sql
@@ -48,6 +52,8 @@ CREATE TRIGGER update_status_before_insert
4852
FOR EACH ROW EXECUTE FUNCTION chats.set_message_status_to_sent();
4953
```
5054

55+
## Handle new user from `auth.users`
56+
5157
"This trigger, is responsible for replicate `auth.users` table rows in `chats.users` table, this is to avoid exposing user data :
5258

5359
```sql

doc/docs/guides/supabse-views.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
id: supabase-views
3+
title: Database Views
4+
---
5+
6+
## Rooms view
7+
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.
9+
10+
```sql
11+
DROP VIEW IF EXISTS chats.rooms_l;
12+
create view chats.rooms_l
13+
WITH (security_invoker='on') as
14+
select
15+
r.id,
16+
r."imageUrl",
17+
r.metadata,
18+
case
19+
when r.type = 'direct' and auth.uid() is not null then
20+
(select coalesce(u."firstName", '') || ' ' || coalesce(u."lastName", '')
21+
from chats.users u
22+
where u.id = any(r."userIds") and u.id <> auth.uid()
23+
limit 1)
24+
else
25+
r.name
26+
end as name,
27+
r.type,
28+
r."userIds",
29+
r."lastMessages",
30+
r."userRoles",
31+
r."createdAt",
32+
r."updatedAt"
33+
from chats.rooms r;
34+
```

doc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flutter-supabase-chat-core",
3-
"version": "1.3.1",
3+
"version": "1.3.2",
44
"private": true,
55
"scripts": {
66
"docusaurus": "docusaurus",

doc/sidebars.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const sidebars: SidebarsConfig = {
3333
'guides/supabase-usage',
3434
'guides/supabase-security',
3535
'guides/supabase-triggers',
36+
'guides/supabase-views',
3637
'guides/supabase-indexes',
3738
],
3839
},

example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: example
22
description: A new Flutter project.
33
publish_to: 'none'
44

5-
version: 1.3.1
5+
version: 1.3.2
66

77
environment:
88
sdk: '>=3.4.0 <4.0.0'

example/utils/prepare.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ Invoke-Expression "$psqlCommand -f .\sql\02_database_trigger.sql"
2020
Invoke-Expression "$psqlCommand -f .\sql\03_database_policy.sql"
2121
Invoke-Expression "$psqlCommand -f .\sql\04_storage.sql"
2222
Invoke-Expression "$psqlCommand -f .\sql\05_database_view.sql"
23+
Invoke-Expression "$psqlCommand -f .\sql\99_database_schema_permission.sql"

example/utils/prepare.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ psql -U $user -h $hostname -p $port -d $database -f ./sql/01_database_schema.sql
1414
psql -U $user -h $hostname -p $port -d $database -f ./sql/02_database_trigger.sql
1515
psql -U $user -h $hostname -p $port -d $database -f ./sql/03_database_policy.sql
1616
psql -U $user -h $hostname -p $port -d $database -f ./sql/04_storage.sql
17-
psql -U $user -h $hostname -p $port -d $database -f ./sql/05_database_view.sql
17+
psql -U $user -h $hostname -p $port -d $database -f ./sql/05_database_view.sql
18+
psql -U $user -h $hostname -p $port -d $database -f ./sql/99_database_schema_permission.sql
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
GRANT USAGE ON SCHEMA chats TO anon, authenticated, service_role;
2+
GRANT ALL ON ALL TABLES IN SCHEMA chats TO anon, authenticated, service_role;
3+
GRANT ALL ON ALL ROUTINES IN SCHEMA chats TO anon, authenticated, service_role;
4+
GRANT ALL ON ALL SEQUENCES IN SCHEMA chats TO anon, authenticated, service_role;
5+
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA chats GRANT ALL ON TABLES TO anon, authenticated, service_role;
6+
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA chats GRANT ALL ON ROUTINES TO anon, authenticated, service_role;
7+
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA chats GRANT ALL ON SEQUENCES TO anon, authenticated, service_role;

0 commit comments

Comments
 (0)