Skip to content

Commit 9be7c5a

Browse files
committed
address review: add powersync_role, rename to sync-streams.yaml, update README
1 parent 1ced517 commit 9be7c5a

3 files changed

Lines changed: 93 additions & 13 deletions

File tree

demos/react-supabase-todolist/README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,42 @@ This demo uses [Sync Streams](https://docs.powersync.com/usage/sync-streams). Bo
88

99
## Run Demo
1010

11-
Prerequisites:
12-
* To run this demo, you need to have properly configured Supabase and PowerSync projects. Follow the instructions in our Supabase<>PowerSync integration guide:
13-
* [Configure Supabase](https://docs.powersync.com/integration-guides/supabase-+-powersync#configure-supabase)
14-
* [Configure PowerSync](https://docs.powersync.com/integration-guides/supabase-+-powersync#configure-powersync)
11+
### 1. Supabase Setup
1512

16-
Switch into the demo's directory:
13+
Create a new Supabase project, then run the contents of [`database.sql`](./database.sql) in the [Supabase SQL editor](https://supabase.com/dashboard/project/_/sql). This will:
14+
15+
- Create the `lists` and `todos` tables
16+
- Enable Row Level Security (RLS) so users can only access their own data
17+
- Create a `powersync` publication for replication
18+
19+
### 2. PowerSync Setup
20+
21+
Create a new PowerSync instance connected to your Supabase project ([instructions here](https://docs.powersync.com/integration-guides/supabase-+-powersync#connect-powersync-to-your-supabase)).
22+
23+
In the PowerSync dashboard, go to **Sync Streams** (shown as **Sync Rules** if using legacy Sync Rules) and paste the contents of [`sync-streams.yaml`](./sync-streams.yaml).
24+
25+
### 3. Install Dependencies
1726

27+
Switch into the demo's directory:
1828
```bash
1929
cd demos/react-supabase-todolist
2030
```
2131

2232
Use [pnpm](https://pnpm.io/installation) to install dependencies:
23-
2433
```bash
2534
pnpm install
2635
```
2736

28-
Set up the Environment variables: Copy the `.env.local.template` file:
37+
### 4. Configure Environment Variables
2938

39+
Copy the `.env.local.template` file:
3040
```bash
3141
cp .env.local.template .env.local
3242
```
3343

34-
And then edit `.env.local` to insert your credentials for Supabase.
35-
36-
Run the development server:
44+
Edit `.env.local` to insert your Supabase URL, anon key, and PowerSync URL.
3745

46+
### 5. Run the Development Server
3847
```bash
3948
pnpm dev
4049
```
@@ -46,13 +55,11 @@ Open [http://localhost:5173](http://localhost:5173) with your browser to see the
4655
This demo is PWA compatible, and works fully offline. PWA is not available in development (watch) mode. The manifest and service worker is built using [vite-plugin-pwa](https://vite-pwa-org.netlify.app/).
4756

4857
Build the production codebase:
49-
5058
```bash
5159
pnpm build
5260
```
5361

5462
Run the production server:
55-
5663
```bash
5764
pnpm preview
5865
```
@@ -63,4 +70,4 @@ Open a browser on the served URL and install the PWA.
6370

6471
Check out [the PowerSync Web SDK on GitHub](https://github.com/powersync-ja/powersync-js/tree/main/packages/web) - your feedback and contributions are welcome!
6572

66-
To learn more about PowerSync, see the [PowerSync docs](https://docs.powersync.com).
73+
To learn more about PowerSync, see the [PowerSync docs](https://docs.powersync.com).
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
-- Create the lists table
2+
CREATE TABLE IF NOT EXISTS public.lists (
3+
id uuid NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
4+
created_at timestamp with time zone NOT NULL DEFAULT now(),
5+
name text NOT NULL,
6+
owner_id uuid NOT NULL REFERENCES auth.users (id) ON DELETE CASCADE
7+
);
8+
9+
-- Create the todos table
10+
CREATE TABLE IF NOT EXISTS public.todos (
11+
id uuid NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
12+
created_at timestamp with time zone NOT NULL DEFAULT now(),
13+
completed_at timestamp with time zone,
14+
description text NOT NULL,
15+
completed boolean NOT NULL DEFAULT false,
16+
list_id uuid NOT NULL REFERENCES public.lists (id) ON DELETE CASCADE,
17+
created_by uuid REFERENCES auth.users (id),
18+
completed_by uuid REFERENCES auth.users (id)
19+
);
20+
21+
-- Enable Row Level Security
22+
ALTER TABLE public.lists ENABLE ROW LEVEL SECURITY;
23+
ALTER TABLE public.todos ENABLE ROW LEVEL SECURITY;
24+
25+
-- RLS policies for lists: users can only access their own lists
26+
CREATE POLICY "Users can view their own lists" ON public.lists
27+
FOR SELECT USING (auth.uid() = owner_id);
28+
29+
CREATE POLICY "Users can insert their own lists" ON public.lists
30+
FOR INSERT WITH CHECK (auth.uid() = owner_id);
31+
32+
CREATE POLICY "Users can update their own lists" ON public.lists
33+
FOR UPDATE USING (auth.uid() = owner_id);
34+
35+
CREATE POLICY "Users can delete their own lists" ON public.lists
36+
FOR DELETE USING (auth.uid() = owner_id);
37+
38+
-- RLS policies for todos: users can only access todos in their own lists
39+
CREATE POLICY "Users can view todos in their lists" ON public.todos
40+
FOR SELECT USING (
41+
list_id IN (SELECT id FROM public.lists WHERE owner_id = auth.uid())
42+
);
43+
44+
CREATE POLICY "Users can insert todos in their lists" ON public.todos
45+
FOR INSERT WITH CHECK (
46+
list_id IN (SELECT id FROM public.lists WHERE owner_id = auth.uid())
47+
);
48+
49+
CREATE POLICY "Users can update todos in their lists" ON public.todos
50+
FOR UPDATE USING (
51+
list_id IN (SELECT id FROM public.lists WHERE owner_id = auth.uid())
52+
);
53+
54+
CREATE POLICY "Users can delete todos in their lists" ON public.todos
55+
FOR DELETE USING (
56+
list_id IN (SELECT id FROM public.lists WHERE owner_id = auth.uid())
57+
);
58+
59+
-- Create PowerSync role for replication access
60+
CREATE ROLE powersync_role REPLICATION LOGIN;
61+
GRANT SELECT ON public.lists TO powersync_role;
62+
GRANT SELECT ON public.todos TO powersync_role;
63+
64+
-- Create PowerSync publication
65+
-- Note: FOR ALL TABLES is simplest for dev. In production, specify tables explicitly.
66+
CREATE PUBLICATION powersync FOR TABLE public.lists, public.todos;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
sync_streams:
2+
- name: user_lists
3+
# Separate stream per todo list, scoped to the authenticated user
4+
parameters: select id as list_id from lists where owner_id = request.user_id()
5+
data:
6+
- select * from lists where id = bucket.list_id
7+
- select * from todos where list_id = bucket.list_id

0 commit comments

Comments
 (0)