Add supabase setup

This commit is contained in:
2025-10-17 22:10:57 +02:00
parent 237052ce35
commit 2b6b515d47
16 changed files with 599 additions and 23 deletions

View File

@@ -0,0 +1,2 @@
id,user_id,name,color,category,completions,current_streak,longest_streak,sort_order,created_at,updated_at
3fa85f64-5717-4562-b3fc-2c963f66afa6,11111111-1111-1111-1111-111111111111,Sample Habit,#22c55e,Health,"[""2025-01-01"",""2025-01-02""]",2,5,0,2025-01-01T00:00:00Z,2025-01-02T00:00:00Z
1 id user_id name color category completions current_streak longest_streak sort_order created_at updated_at
2 3fa85f64-5717-4562-b3fc-2c963f66afa6 11111111-1111-1111-1111-111111111111 Sample Habit #22c55e Health ["2025-01-01","2025-01-02"] 2 5 0 2025-01-01T00:00:00Z 2025-01-02T00:00:00Z

57
supabase/habits_table.sql Normal file
View File

@@ -0,0 +1,57 @@
-- Create habits table with proper types, defaults, constraints, and RLS
-- Run this in Supabase SQL editor
-- Enable gen_random_uuid()
create extension if not exists pgcrypto;
create table if not exists public.habits (
id uuid primary key default gen_random_uuid(),
user_id uuid not null references auth.users(id) on delete cascade,
name text not null,
color text,
category text,
completions jsonb not null default '[]'::jsonb,
current_streak integer not null default 0,
longest_streak integer not null default 0,
sort_order integer not null default 0,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
-- Useful indexes
create index if not exists idx_habits_user_id on public.habits(user_id);
create index if not exists idx_habits_user_sort on public.habits(user_id, sort_order);
-- Automatically update updated_at
create or replace function public.set_updated_at()
returns trigger language plpgsql as $$
begin
new.updated_at = now();
return new;
end;
$$;
drop trigger if exists trg_habits_set_updated_at on public.habits;
create trigger trg_habits_set_updated_at
before update on public.habits
for each row execute function public.set_updated_at();
-- Row Level Security
alter table public.habits enable row level security;
-- Policies: each user can only access their own rows
drop policy if exists habits_select on public.habits;
create policy habits_select on public.habits
for select using (auth.uid() = user_id);
drop policy if exists habits_insert on public.habits;
create policy habits_insert on public.habits
for insert with check (auth.uid() = user_id);
drop policy if exists habits_update on public.habits;
create policy habits_update on public.habits
for update using (auth.uid() = user_id) with check (auth.uid() = user_id);
drop policy if exists habits_delete on public.habits;
create policy habits_delete on public.habits
for delete using (auth.uid() = user_id);