forked from coderyansolomon/brainwave-final
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_notes_db.sql
More file actions
38 lines (32 loc) · 1006 Bytes
/
init_notes_db.sql
File metadata and controls
38 lines (32 loc) · 1006 Bytes
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
-- Table
create table if not exists notes (
id uuid primary key default gen_random_uuid(),
user_id uuid not null references auth.users (id) on delete cascade,
title text not null,
content text,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
-- RLS
alter table notes enable row level security;
-- Read own notes
create policy "read own notes" on notes
for select to authenticated
using (auth.uid() = user_id);
-- Insert/update/delete own notes
create policy "modify own notes" on notes
for all to authenticated
using (auth.uid() = user_id)
with check (auth.uid() = user_id);
-- Trigger to keep updated_at fresh (optional but nice)
create or replace function public.set_updated_at()
returns trigger as $$
begin
new.updated_at = now();
return new;
end;
$$ language plpgsql;
drop trigger if exists trg_set_updated_at on notes;
create trigger trg_set_updated_at
before update on notes
for each row execute function public.set_updated_at();