""" supabase_client.py — the database connection ==================================================================== WHAT THIS FILE IS: the single place that connects to Supabase (the database shared with the Lovable app). WHAT IT DOES: hands out two connections — • get_user_client() → normal access, obeys security rules. Use for anything a regular user does (searching, browsing). • get_admin_client() → powerful access that ignores security rules. Use ONLY for admin actions (adding items, checking who's an admin). HOW IT FITS: search.py, auth.py and pages_ui.py all call this file to talk to the database, rather than each connecting on their own. FOR WHOEVER TAKES OVER: the three connection values live as Space secrets (SUPABASE_URL, SUPABASE_ANON_KEY, SUPABASE_SERVICE_KEY), never in this file. Never expose the admin client to a non-admin user. ==================================================================== """ import os import streamlit as st from supabase import create_client, Client def _require(name: str) -> str: val = os.environ.get(name) if not val: st.error( f"Missing configuration: {name}. Set it in the Space's " "Settings → Variables and secrets." ) st.stop() return val @st.cache_resource def get_user_client() -> Client: """Anon/publishable client — respects Row Level Security.""" return create_client(_require("SUPABASE_URL"), _require("SUPABASE_ANON_KEY")) @st.cache_resource def get_admin_client() -> Client: """Service/secret client — bypasses RLS. Admin actions only.""" return create_client(_require("SUPABASE_URL"), _require("SUPABASE_SERVICE_KEY"))