How can I achieve something like this ```sql selec...
# javascript
a
How can I achieve something like this
Copy code
sql
select id from servers where count(select * from server_bots where servers.id = server_bots.id) < 25;
Using
@supabase/supabase-js
What i want to achieve is that i want to obtain an id for a server which has less than 25 bots registered in it. To do this with SQL i can use
count(...)
which will count the entries that has the same server id. I'm aware i can retrieve the entries in
server_bots
that has the same server id then i count them using if statement in JavaScript; and i repeat till i get a server id that points to a server that has less than 25 bots, but this practice seems to be bad as it will be expensive when the number of servers increase. i want to know what is better way to do this Using
@supabase/supabase-js
, thank you!.
s
I would create a Postgres view for this and just query it as you would a normal table with the supabase-js library
Copy code
sql
CREATE VIEW public.servers_with_less_than_25_bots AS select id from servers where count(select * from server_bots where servers.id = server_bots.id) < 25;
Then you can call it with the js library
Copy code
js
await supabase.from('servers_with_less_than_25_bots').select('id')