The second installment of my series, “Build a Credit Payment System in Streamlit for Gen AI Apps,” is live! This time, I cover connecting your app to a database to securely store user data and manage credits.
I’d love any feedback or suggestions as I continue learning!
The only interrogation I have about st-supabase-connection is how to best use it with RLS. My current solution involves signing in with credentials for a single user I added myself from the dashboard, which seems a little bit hacky but since I don’t actually use it for authentication (I use Google) I couldn’t think of a better solution. But now, because I have to sign in with a password, I had to create a get_db_connection function that connects and authenticates so that I could cache the entire thing (as opposed to just the connection and having to rerun the auth every time). I was wondering if that seemed like a bit of an anti-pattern or if you think that’s fine?
The function for reference:
@st.cache_resource(show_spinner=False)
def get_db_connection() -> SupabaseConnection:
"""
Establish and cache a connection to the Supabase database.
Returns:
SupabaseConnection: Authenticated connection to Supabase
"""
conn = st.connection("supabase", type=SupabaseConnection)
conn.auth.sign_in_with_password(
{
"email": st.secrets["connections"]["supabase"]["EMAIL_ADDRESS"],
"password": st.secrets["connections"]["supabase"]["PASSWORD"],
}
)
return conn
Currently, the only convenience st_supabase_connection offers over Supabase’s native Python client is caching the connection, but for auth, I think it’ll definitely make more sense to cache the entire authenticated connection
So your implementation should actually become a part of st_supabase_connection itself!