How would you record views per listing in Supabase?

Hi again fellow Webbers,

I’ve got a very basic Supabase table with three fields, id , listing_id and viewed at (which is a timestamp). I’m looking to count every time a listing (think of it as a product on a store) is clicked on as a view.

What would be best practice to update this table? I want to have RLS on this table, as I don’t want the general public able to have write access to this table.

I’ve seen some people do periodic updates to their database, so would one method be to store values on WeWeb storage, and then just update to Supabase once every x hours? Is there a way to use the native Supabase integration to do this? Cause listings are publicly accessible, and a user shouldn’t have to be logged in for me to want to count the listing click as a view.

You have two options

  1. Make an another table, that does not have RLS and connect (reference) it via id to the users table, then update that table and when you’re fetching the data you can use a JOIN statement to fetch the data, and the reference to the table. Or if you only need the number (count of views) you can fetch the table directly via the id (which they both share)
    See the attached schema

  1. Make your own PostgreSQL function in Supabase and use some statements (e.g. security definer) to bypass RLS (not reccomended)

Also a question, now that I’m thinking about it, why do you have id, and listing_id? Is there a need for two ids?

Oops I’m thinking I might not have explained myself well enough

I have a table called listings (similar to your posts table illustration) and another table called views.

As I’m tracking each view on each listing, each view needs it’s own unique identifier, and the listing_id is just a foreign key I’m referencing.

In one of the WeWeb videos, they mention it would be inefficient to do it in the way you’ve setup the “views” table in your illustration. Cause it would require a constant read and update request. They suggest it’s better to utilize a Supabase “view” as that’s more performant.

My question was more of just how would I insert the record in Supabase (to my views table) without a user having to be logged in (as it feels the native Supabase insert requires a user login to insert a record if RLS policies are enabled).

I thought removing RLS policies was one option, but then if a bad actor could figure out how to write to my table, they could just inflate the views on their listings.

I don’t think you can do this without any sort of identification. You want to basically protect a table while giving people access to it, but also without identifying them anyhow. I thought Views in Supabase were view only, I didn’t know you could write/edit data from within them. Do you have a reference to this? You might be confusing Views with a table that is just called views. Also, why would you need to identify each view? It would just number anonymous views and when they were created, which would bloat your DB without any added value.

Might be a good thing to explain what is the expected result/app function

Supabase Views are read only. I meant rather than doing a read and write request to a table, just have a workflow that does a write request only.

And then use the Supabase view to do a count of views for that listing. It’s explained better in this video Building an upvote system with Supabase and WeWeb - YouTube