A few days ago, while building a beekeeping management platform, I hit a small but annoying wall π¨βπ».
We track honey batches uploaded by different beekeepers π―.
Each batch record has its senderβs email, while the sold honey records just reference a batch by ID β no email field there π.
When I started building the βSold Honeyβ page, I realized:
βHow do I show only the sales belonging to the logged-in user if sold_honey doesnβt even have an email?β
At first, I thought Iβd just pull everything from Supabase and filter client-side.
But that quickly felt messy and inefficient β especially with pagination π¦.
Thatβs when I remembered π₯: Supabase lets you write RPC (Postgres) functions.
Theyβre like serverless endpoints that live right inside your database.
β¨ The Fix
Instead of sending multiple queries or writing big joins in React (Next.js), I created a tiny SQL function get_sold_honey_by_email that joins both tables internally and filters by email.
create or replace function get_sold_honey_by_email(p_email text)
returns setof sold_honey as $$
select s.*
from sold_honey s
join honey_batches h on s.honey_batch_id = h.id
where h.sender_email = p_email
order by s.id desc;
$$ language sql;
Thatβs it β no sensitive fields, no extra logic in the frontend π.
βοΈ One Simple Call in Next.js
const { data, error } = await supabase.rpc("get_sold_honey_by_email", {
p_email: user.email,
});
Done. I get back only the userβs data, already filtered and ready to render.
No where clauses in React, no accidental overfetching, no extra stress.
π₯³π
π‘ What I Learned
- Supabase RPCs are perfect for moving business logic closer to your data.
- They keep your frontend clean and your backend predictable.
- Pagination, filtering, even validations β they can all live inside the database.
This small win made my data flow so much simpler that I started refactoring other pages the same way.
Sometimes the cleanest frontend is just a smart SQL function away π
Built with Supabase + Next.js on a real-world beekeeping data project π
If youβre exploring Supabase, try writing one RPC function today β it might surprise you how much cleaner your code feels.
Top comments (0)