I want to retrieve row count in Supabase.
I am guessing it would be something like this:
const { data, error } = await supabase
.from('cities')
.select('name', 'COUNT(*)')
Is this possible with Supabase?
I want to retrieve row count in Supabase.
I am guessing it would be something like this:
const { data, error } = await supabase
.from('cities')
.select('name', 'COUNT(*)')
Is this possible with Supabase?
For future visitors, we are working on this functionality with the PostgREST maintainer:
https://github.com/supabase/postgrest-js/issues/94
(I'm a maintainer)
This is now released:
const { data, count } = supabase
.from('countries')
.select('*', { count: 'exact', head: true })
If you want to return the results simultaneously, remove head
:
const { data, count } = supabase
.from('countries')
.select('*', { count: 'exact' })
As a workaround, you could write a standard Postgres stored procedure or function that returns the count then call that via the SB client.
It is currently not supported yet, but there is a WIP issue on Github that would bring this feature to Supabase.
The code below has not been implemented in Supabase yet, but it might look something like this:
const { data, error, count } = await supabase
.from('table')
.select('*')
.gt('id', 10)
.count()
Edit 7.19.2021
As kiwicopple has answered, this feature has been added to Supabase with a slightly different form from what I have above. View the accepted answer for more.