โกSupabase Integration
Adding User Data to a Supabase Table
Supabase is an open-source Firebase alternative that provides all the backend services you need to build a scalable app in Databutton. This guide outlines the key steps to integrate Supabase with your application, including creating a Supabase project, setting up a table, and creating a backend in Databutton.
Key Steps
Create a Supabase Project
Set Up a Table in Supabase
Create a Backend in Databutton
Getting Started with Supabase Projects
Creating a Supabase Account
To get started with Supabase, you need to have a Supabase account. You can sign up using your email or use a third-party provider like GitHub to create your Supabase account.
Create a New Project
Once logged in, click on "New Project."
Choose a name for your project and select a region closest to your location.
Click "Create new project" to continue.
Setting up Your Table in Supabase
To work with tables, you need to first create a table in Supabase.
Navigate to the "Database" tab on the left sidebar.
And then create a "New table"
You need to name your table. In this demo example the table is named as ;
user_feedback
Defining Columns and Their Types
In this demo example, we define five columns:
id
: Primary key of typeuuid
with default valueuuid_generate_v4()
.user_id
: Typetext
, this will store the user IDfeedback_text
: Typetext
, to store feedback content.rating
: Typeint
, to store a numerical rating.movie_url
: Typetext
, to store the URL related to the feedback
Refer to the screenshot below to see how this looks in practice. You can add multiple columns and enable Row Level Security (RLS) as well, which we will discuss shortly.
Click "Save" to create your table!
Enable Row Level Security (RLS)
Row Level Security (RLS) allows you to control access to rows in a table based on specific conditions.
Enable the RLS checkbox to activate this feature.
Creating RLS Policies:
Go to the "Policies" tab for the selected table.
Click on "New Policy" to create a new policy
Creating an Unrestricted API Access Policy
During development or when dealing with public data, you might want to allow unrestricted access to your table. This can be done by creating a policy that grants all users permission to perform any operation on the table.
Policy Name: Allow API Access
Table:
public.user_feedback
Policy Behavior: Permissive (as clause)
Policy Command: ALL (SELECT, INSERT, UPDATE, DELETE)
Target Roles: Defaults to all (public) roles if none selected
Condition: Set the condition to
true
to allow unrestricted access
Security Implications: The true
condition means the policy will allow any operation by any user on the table. This is ideal for testing or open-access applications, but for production, you should replace this policy with more secure, user-specific access controls.
Building the Backend (API) with Databutton
Now we come to the main part where Databutton works like magic! Databutton simplifies building a backend that connects to your Supabase database, allowing you to easily perform CRUD operations on your data.
Databutton can guide you through this process.
Define Your Requirements
You can start by outlining your requirements to Databutton. Here's an example prompt you might use. Example prompt :
Hey! I would like to create a backend which has access to my supabase table <name of the table> and use Python SDK of supabase to fetch data, add data , delete data and update data. How shall I proceed?
Obtain API Credentials
To interact with your Supabase database, you need the API Secret Key and Web URL for authentication.
Navigate to the "Home" Tab:
Go to the "Home" tab in the Supabase dashboard.
Scroll down to find Project API details.
RLS API Key (service_role secret):
Ensure you provide Databutton with the RLS API key (
service_role
secret), as RLS is enabled for your table.
Note: Ensure to provide Databutton with the RLS API key (service_role secret) since RLS is enabled.
Store Credentials Securely
Once, you have both of them, ask Databutton to store your credentials securely for easy access in your backend code.
Example Prompt :
Hey help me to store the URL and API key
Develop the Backend API
The backend covers all necessary functionalities for a CRUD app, with each function treated as an individual endpoint. The core functionalities include:
Retrieve all feedback records from the Supabase table.
Adding data : Insert a new feedback record into the Supabase table
Update data : Update an existing feedback record based on the user ID
Deleting data : Delete a feedback record based on the user ID.
Pro Tip: Implement one function at a time rather than adding all functionalities at once. For example, start by implementing the "Retrieve" table feature first.
API Code Implementation
Databutton handles the installation of the supabase
Python package.
Test Your API Endpoints
Once each endpoint is implemented, ask Databutton to test it !
Next following steps would be to integrate the this backend to the UI component!
Last updated