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)