|
| 1 | +-- Create tables |
| 2 | +create table |
| 3 | + public.lists ( |
| 4 | + id uuid not null default gen_random_uuid (), |
| 5 | + created_at timestamp with time zone not null default now(), |
| 6 | + name text not null, |
| 7 | + owner_id uuid not null, |
| 8 | + constraint lists_pkey primary key (id), |
| 9 | + constraint lists_owner_id_fkey foreign key (owner_id) references auth.users (id) on delete cascade |
| 10 | + ) tablespace pg_default; |
| 11 | + |
| 12 | +create table |
| 13 | + public.todos ( |
| 14 | + id uuid not null default gen_random_uuid (), |
| 15 | + created_at timestamp with time zone not null default now(), |
| 16 | + completed_at timestamp with time zone null, |
| 17 | + description text not null, |
| 18 | + completed boolean not null default false, |
| 19 | + created_by uuid null, |
| 20 | + completed_by uuid null, |
| 21 | + list_id uuid not null, |
| 22 | + photo_id uuid null, |
| 23 | + constraint todos_pkey primary key (id), |
| 24 | + constraint todos_created_by_fkey foreign key (created_by) references auth.users (id) on delete set null, |
| 25 | + constraint todos_completed_by_fkey foreign key (completed_by) references auth.users (id) on delete set null, |
| 26 | + constraint todos_list_id_fkey foreign key (list_id) references lists (id) on delete cascade |
| 27 | + ) tablespace pg_default; |
| 28 | + |
| 29 | +-- Create publication for powersync |
| 30 | +create publication powersync for table lists, todos; |
| 31 | + |
| 32 | +-- Set up Row Level Security (RLS) |
| 33 | +-- See https://supabase.com/docs/guides/auth/row-level-security for more details. |
| 34 | +alter table public.lists |
| 35 | + enable row level security; |
| 36 | + |
| 37 | +alter table public.todos |
| 38 | + enable row level security; |
| 39 | + |
| 40 | +create policy "owned lists" on public.lists for ALL using ( |
| 41 | + auth.uid() = owner_id |
| 42 | +); |
| 43 | + |
| 44 | +create policy "todos in owned lists" on public.todos for ALL using ( |
| 45 | + auth.uid() IN ( |
| 46 | + SELECT lists.owner_id FROM lists WHERE (lists.id = todos.list_id) |
| 47 | + ) |
| 48 | +); |
| 49 | + |
| 50 | +-- This trigger automatically creates some sample data when a user registers. |
| 51 | +-- See https://supabase.com/docs/guides/auth/managing-user-data#using-triggers for more details. |
| 52 | +create function public.handle_new_user_sample_data() |
| 53 | +returns trigger as $$ |
| 54 | +declare |
| 55 | + new_list_id uuid; |
| 56 | +begin |
| 57 | + insert into public.lists (name, owner_id) |
| 58 | + values ('Shopping list', new.id) |
| 59 | + returning id into new_list_id; |
| 60 | + |
| 61 | + insert into public.todos(description, list_id, created_by) |
| 62 | + values ('Bread', new_list_id, new.id); |
| 63 | + |
| 64 | + insert into public.todos(description, list_id, created_by) |
| 65 | + values ('Apples', new_list_id, new.id); |
| 66 | + |
| 67 | + return new; |
| 68 | +end; |
| 69 | +$$ language plpgsql security definer; |
| 70 | + |
| 71 | +create trigger new_user_sample_data after insert on auth.users for each row execute procedure public.handle_new_user_sample_data(); |
0 commit comments