-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsupabase.sql
More file actions
43 lines (40 loc) · 1.45 KB
/
supabase.sql
File metadata and controls
43 lines (40 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
CREATE TABLE IF NOT EXISTS "todos" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"task" text,
"is_complete" integer DEFAULT 0,
"modified_at" timestamp DEFAULT now(),
"user_id" uuid NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "users" (
"id" uuid PRIMARY KEY NOT NULL,
"email" text
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "todos" ADD CONSTRAINT "todos_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
alter table todos enable row level security;
create policy "Individuals can create todos." on todos for
insert with check (auth.uid() = user_id);
create policy "Individuals can view their own todos. " on todos for
select using ((select auth.uid()) = user_id);
create policy "Individuals can update their own todos." on todos for
update using ((select auth.uid()) = user_id);
create policy "Individuals can delete their own todos." on todos for
delete using ((select auth.uid()) = user_id);
create function public.handle_new_user()
returns trigger as $$
begin
insert into public.users (id, email)
values (new.id, new.email);
return new;
end;
$$ language plpgsql security definer;
create trigger on_auth_user_created
after insert on auth.users
for each row execute procedure public.handle_new_user();
drop publication if exists powersync;
create publication powersync for table public.todos;