Building a Credit System in Streamlit: Database Connection

Hey everyone :wave:

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!

Check out the post here!

I wanted to throw a special shoutout to @SiddhantSadangi for creating st-supabase-connection, it was so easy to set up!

Cheers!

2 Likes

Thanks for the shoutout! :bowing_man:

Would love to hear any feedback on st-supabase-connection that you might have :hugs:

I’ll check the blog later, but meanwhile tagging @Jessica_Smith for the weekly and monthly roundups :slight_smile:

2 Likes

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
1 Like

Thanks for sharing!

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 :white_check_mark:

So your implementation should actually become a part of st_supabase_connection itself!

Would you like to submit a PR? :slight_smile:

Makes sense! Yeah I’d love to take a look, I’ll try to do that sometime this weekend

1 Like