Skip to content

Commit

Permalink
Fix schema permission (#18)
Browse files Browse the repository at this point in the history
## [1.3.2] - 2024-11-13
#### [@rickypid](https://github.com/rickypid)

Improve documentations

### Fixed

* Fixed schema `chats` permission after view creation
  • Loading branch information
rickypid authored Nov 13, 2024
1 parent 45b70ee commit 05cb266
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 5 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## [1.3.2] - 2024-11-13
#### [@rickypid](https://github.com/rickypid)

Improve documentations

### Fixed

* Fixed schema `chats` permission after view creation

## [1.3.1] - 2024-11-12
#### [@rickypid](https://github.com/rickypid)

Expand Down
2 changes: 2 additions & 0 deletions doc/docs/guides/supabse-indexes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ id: supabase-indexes
title: Database Indexes
---

## `chats.messages` indexes

These indexes are added to improve the performance of foreign keys in database tables:

```sql
Expand Down
8 changes: 7 additions & 1 deletion doc/docs/guides/supabse-trigges.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ id: supabase-triggers
title: Database Triggers
---

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

This is a trigger that sets room's `lastMessages` to the most recent message sent once recieved in Supabase.

```sql
CREATE OR REPLACE FUNCTION chats.update_last_messages()
Expand All @@ -29,6 +31,8 @@ CREATE TRIGGER update_last_messages_trigger
EXECUTE FUNCTION chats.update_last_messages();
```

## Set message status to sent

"This trigger, on the other hand, is responsible for setting the message status to `sent` when it is added to the `messages` table:

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

## Handle new user from `auth.users`

"This trigger, is responsible for replicate `auth.users` table rows in `chats.users` table, this is to avoid exposing user data :

```sql
Expand Down
34 changes: 34 additions & 0 deletions doc/docs/guides/supabse-views.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
id: supabase-views
title: Database Views
---

## Rooms view

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.

```sql
DROP VIEW IF EXISTS chats.rooms_l;
create view chats.rooms_l
WITH (security_invoker='on') as
select
r.id,
r."imageUrl",
r.metadata,
case
when r.type = 'direct' and auth.uid() is not null then
(select coalesce(u."firstName", '') || ' ' || coalesce(u."lastName", '')
from chats.users u
where u.id = any(r."userIds") and u.id <> auth.uid()
limit 1)
else
r.name
end as name,
r.type,
r."userIds",
r."lastMessages",
r."userRoles",
r."createdAt",
r."updatedAt"
from chats.rooms r;
```
2 changes: 1 addition & 1 deletion doc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flutter-supabase-chat-core",
"version": "1.3.1",
"version": "1.3.2",
"private": true,
"scripts": {
"docusaurus": "docusaurus",
Expand Down
1 change: 1 addition & 0 deletions doc/sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const sidebars: SidebarsConfig = {
'guides/supabase-usage',
'guides/supabase-security',
'guides/supabase-triggers',
'guides/supabase-views',
'guides/supabase-indexes',
],
},
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: example
description: A new Flutter project.
publish_to: 'none'

version: 1.3.1
version: 1.3.2

environment:
sdk: '>=3.4.0 <4.0.0'
Expand Down
1 change: 1 addition & 0 deletions example/utils/prepare.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ Invoke-Expression "$psqlCommand -f .\sql\02_database_trigger.sql"
Invoke-Expression "$psqlCommand -f .\sql\03_database_policy.sql"
Invoke-Expression "$psqlCommand -f .\sql\04_storage.sql"
Invoke-Expression "$psqlCommand -f .\sql\05_database_view.sql"
Invoke-Expression "$psqlCommand -f .\sql\99_database_schema_permission.sql"
3 changes: 2 additions & 1 deletion example/utils/prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ psql -U $user -h $hostname -p $port -d $database -f ./sql/01_database_schema.sql
psql -U $user -h $hostname -p $port -d $database -f ./sql/02_database_trigger.sql
psql -U $user -h $hostname -p $port -d $database -f ./sql/03_database_policy.sql
psql -U $user -h $hostname -p $port -d $database -f ./sql/04_storage.sql
psql -U $user -h $hostname -p $port -d $database -f ./sql/05_database_view.sql
psql -U $user -h $hostname -p $port -d $database -f ./sql/05_database_view.sql
psql -U $user -h $hostname -p $port -d $database -f ./sql/99_database_schema_permission.sql
7 changes: 7 additions & 0 deletions example/utils/sql/99_database_schema_permission.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
GRANT USAGE ON SCHEMA chats TO anon, authenticated, service_role;
GRANT ALL ON ALL TABLES IN SCHEMA chats TO anon, authenticated, service_role;
GRANT ALL ON ALL ROUTINES IN SCHEMA chats TO anon, authenticated, service_role;
GRANT ALL ON ALL SEQUENCES IN SCHEMA chats TO anon, authenticated, service_role;
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA chats GRANT ALL ON TABLES TO anon, authenticated, service_role;
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA chats GRANT ALL ON ROUTINES TO anon, authenticated, service_role;
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA chats GRANT ALL ON SEQUENCES TO anon, authenticated, service_role;
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: flutter_supabase_chat_core
description: >
Actively maintained, community-driven Supabase BaaS for chat applications
with an optional chat UI.
version: 1.3.1
version: 1.3.2
homepage: https://flutter-supabase-chat-core.insideapp.it
repository: https://github.com/insideapp-srl/flutter_supabase_chat_core

Expand Down

0 comments on commit 05cb266

Please sign in to comment.