Are conditional .eq checks possible?

Is it possible to conditionally check for something in a table? For intance: I want to run
return supabase
.from('table1')
.select(`*`)
.eq('account', account)
return supabase
.from('table1')
.select(`*`)
.eq('account', account)
on one function call, but then:
return supabase
.from('table1')
.select(`*`)
.eq('account', account)
.eq('2ndThing', 2ndThing)
return supabase
.from('table1')
.select(`*`)
.eq('account', account)
.eq('2ndThing', 2ndThing)
Can I toggle on/off that last eq or do I need to define a separate function?
2 Replies
mrgandalfandhi
You can build up the query step by step:
let query = supabase
.from('table1')
.select(`*`)
.eq('account', account)
if (yourCondition) query = query.eq('2ndThing', 2ndThing);
return await query;
let query = supabase
.from('table1')
.select(`*`)
.eq('account', account)
if (yourCondition) query = query.eq('2ndThing', 2ndThing);
return await query;
The query won't be executed until you await it. Docs: https://supabase.com/docs/reference/javascript/using-filters#conditional-chaining
Using Filters | Supabase
Filters can be used on select(), update(), and delete() queries.
Kruger
KrugerOP3y ago
awesome thank you!

Did you find this page helpful?