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)