DEV Community

Cover image for 🐝 How a Simple Supabase RPC Function Cleaned Up My Frontend Logic
Egor
Egor

Posted on

🐝 How a Simple Supabase RPC Function Cleaned Up My Frontend Logic

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;
Enter fullscreen mode Exit fullscreen mode

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,
});
Enter fullscreen mode Exit fullscreen mode

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)