Stores anime in each user’s personal library with status, score, and progress tracking.
create table public.user_library ( id uuid default gen_random_uuid() primary key, user_id uuid references public.profiles(id) on delete cascade not null, anime_id_jikan integer not null, title text not null, image_url text, status public.library_status default 'watching' not null, score integer check (score >= 0 and score <= 10), episodes_watched integer default 0, created_at timestamp with time zone default timezone('utc'::text, now()) not null, updated_at timestamp with time zone default timezone('utc'::text, now()) not null, unique(user_id, anime_id_jikan));
The schema should include these indexes for optimal performance:
-- Index for user queries (most common)CREATE INDEX idx_user_library_user_id ON user_library(user_id);-- Index for anime lookupsCREATE INDEX idx_user_library_anime_id ON user_library(anime_id_jikan);-- Index for status filteringCREATE INDEX idx_user_library_status ON user_library(status);-- Index for ordering by update timeCREATE INDEX idx_user_library_updated_at ON user_library(updated_at DESC);
Automatically creates a profile when a new user signs up.
-- supabase/migrations/20260218_initial_schema.sql:26create or replace function public.handle_new_user()returns trigger as $$begin insert into public.profiles (id, full_name, username, avatar_url) values ( new.id, new.raw_user_meta_data->>'full_name', new.raw_user_meta_data->>'username', new.raw_user_meta_data->>'avatar_url' ); 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();
This trigger extracts user metadata from the signup form and populates the profile automatically. No manual profile creation needed!
Show 1. Always Use Transactions for Multiple Operations
BEGIN; UPDATE user_library SET episodes_watched = episodes_watched + 1 WHERE id = 'item-uuid'; UPDATE user_library SET status = 'completed' WHERE id = 'item-uuid' AND episodes_watched >= total_episodes;COMMIT;