Spaces:
Sleeping
Sleeping
Download supabase_client.py from ThinkCity/TC-Brain-26: direct link, hf CLI and curl.
- Browser
- Download file 1.71 kB
-
https://huggingface.co/spaces/ThinkCity/TC-Brain-26/resolve/main/supabase_client.py
- Command line
-
hf download hf://spaces/ThinkCity/TC-Brain-26/supabase_client.py
-
curl -L -o supabase_client.py https://huggingface.co/spaces/ThinkCity/TC-Brain-26/resolve/main/supabase_client.py
1.71 kB
| """ | |
| 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 | |
| def get_user_client() -> Client: | |
| """Anon/publishable client — respects Row Level Security.""" | |
| return create_client(_require("SUPABASE_URL"), _require("SUPABASE_ANON_KEY")) | |
| def get_admin_client() -> Client: | |
| """Service/secret client — bypasses RLS. Admin actions only.""" | |
| return create_client(_require("SUPABASE_URL"), _require("SUPABASE_SERVICE_KEY")) |