rafaelwendel / phpsupabase

PHP Client to use Supabase
MIT License
187 stars 18 forks source link

How to count records? #25

Open gfh31fgh2 opened 1 year ago

gfh31fgh2 commented 1 year ago

How to count records with this api?

rafaelwendel commented 1 year ago

Hello @gfh31fgh2

There is no specific function in the library to do this, but it is possible to obtain it through the count function

$db = $service->initializeDatabase('products', 'id');

try{
    $listProducts = $db->fetchAll()->getResult(); //fetch all products
    echo "Total: " . count($listProducts) . " rows on product table";
}
catch(Exception $e){
    echo $e->getMessage();
}
gfh31fgh2 commented 1 year ago

Hello @gfh31fgh2

There is no specific function in the library to do this, but it is possible to obtain it through the count function

$db = $service->initializeDatabase('products', 'id');

try{
    $listProducts = $db->fetchAll()->getResult(); //fetch all products
    echo "Total: " . count($listProducts) . " rows on product table";
}
catch(Exception $e){
    echo $e->getMessage();
}

Thanks for answer! But for count > 100k, it isnt good function for this. Trying to use javascript on same instance for counting special requests like

const { data, count } = supabase
  .from('countries')
  .select('*', { count: 'exact', head: true })
reidsneo commented 1 year ago

Hello @gfh31fgh2 There is no specific function in the library to do this, but it is possible to obtain it through the count function

$db = $service->initializeDatabase('products', 'id');

try{
    $listProducts = $db->fetchAll()->getResult(); //fetch all products
    echo "Total: " . count($listProducts) . " rows on product table";
}
catch(Exception $e){
    echo $e->getMessage();
}

Thanks for answer! But for count > 100k, it isnt good function for this. Trying to use javascript on same instance for counting special requests like

const { data, count } = supabase
  .from('countries')
  .select('*', { count: 'exact', head: true })

Hi, I struggle same issue, but after read the discussion here, I found out that the count parameter and response was calculated in header of request & response.

What I do is :

  1. Modify the request section to able handle count parameter
  2. In executeQuery under QueryBuilder.php must be able to handle the HEAD request
  3. In executeHttpRequest under Service.php you must be able to map header response of "Content-Range" to body section
  4. Viola now count result can be accessible

I don't recommend to use GET request to do this because it will render the body section, but use HEAD instead more lighter