Supabase Database Operations
An overview of how to interact with your Supabase database using our utility functions for fetching, inserting, updating, upserting, deleting data, and calling functions (RPC).
This boilerplate includes a set of modular, reusable utility functions to perform common database operations with Supabase. Whether you need to fetch data, insert new records, update or delete existing ones, or even call custom Postgres functions (RPC), these utilities make it easy.
Fetching Data
Use the fetchData function to run SELECT queries on your tables. For example, to get all rows from a table named "characters":
jsx
1const { data, error } = await fetchData("characters", "*", { name: "Harry" });
You can pass filters as an object to narrow down your results. Experiment with different filters to optimize your queries.
Inserting Data
Insert new records using the insertData function. It supports both single and multiple record insertions.
jsx
1await insertData("countries", { id: 1, name: "Mordor" });
Tip: Ensure your object keys match your table columns.
Updating & Upserting Data
Updating Data:
Use updateData with filters to change existing records.
jsx
1await updateData("instruments", { name: "piano" }, { id: 1 });
Upserting Data:
The upsertData function inserts a record if it doesn’t exist or updates it if it does.
jsx
1await upsertData("instruments", { id: 1, name: "piano" });
Deleting Data
Delete records safely with deleteData. Always include filters to avoid removing unintended rows.
jsx
1await deleteData("countries", { id: 1 });
Always include appropriate filters when performing DELETE operations.
Calling Functions (RPC)
Call custom Postgres functions using callFunction. For example, if you have a simple "hello_world" function:
jsx
1const { data, error } = await callFunction("hello_world"); 2console.log(data); // "Hello world"
Filters & Best Practices
Filters let you narrow down your queries. For example, to fetch only rows where "name" equals "Leia":
jsx
1const { data, error } = await fetchData("characters", "*", { name: "Leia" });
Tip: Chain filters as needed, but test your queries to ensure they’re efficient.
These utilities are designed to simplify your database interactions so you can focus on building great features without re-inventing common CRUD operations.