Using Stored Procedures (RPC) in Supabase to Increment a “Like” Counter

Kelvin Zhao
Geek Culture
Published in
3 min readMay 27, 2021

--

In my recent side project, Lila, I wanted a simple way to increment a “Like” counter for the quotes on the site. As I am using Supabase — The Open Source Firebase Alternative for my backend database, I thought perhaps there’s an increment function within the APIs.

I really don’t want to read the database, update the count in my app then write it back to the database again. Feels like a long-winded way to do such a simple function. Plus, depending on the lag situation, the counter might go out of sync. But alas, there is no such API available.

Luckily there’s something called Stored Procedures (RPC) where you can add a small piece of logic or function to the database and then call it from your app. The problem is the documentation in Supabase requires you to have some basic SQL knowledge or similar experience to know how things work.

I went a super long way with trial and error and also asking a lot of stupid questions on Twitter and the Supabase Github discussions to finally get this to work. I thought I’ll just document how to get it to finally work here.

In the Suapabase dashboard, select the SQL tab on the left menu.

Create a ‘New query’ and type in the code below.

create function vote…

--

--