Is there a way to build a query dinamically with t...
# javascript
n
Is there a way to build a query dinamically with the js client? For example, imagine we have a datatable and an input field to filter
Copy code
sql
let query = supabase.from('users').select('name')

if (name) query = query.filter('name', 'LIKE' `%${name}%`)
s
Something like this should work:
Copy code
js
if (name) {
  const query = supabase
    .from('users')
    .select('name')
    .filter('name', 'LIKE', `%${name}%`)
}
n
Yes, but thats not the point. The idea is to have a single query instance, and chain operators dinamically based on conditions. Another example where this would be useful
Copy code
js
let query = supabase.from('table').select()

conditions.foreach((val, key) => query = query.eq(key, val)))
g
I haven't used supabase querying yet but I use knex and knex is like this, have you tried
Copy code
js
const query = supabase.from('users');
const conditions = [ ['name', 'value here'] ];

for (const [key, value] of conditions) {
  query.filter(key, 'LIKE', `%${value}%`);
};

const res = await query.select('name');
n
@User so you have a supabase postgres database and use knex to query it?
g
no I was just saying supabase's query system is inspired by knex
and so try the code I gave
might apply to supabases system
n
@soedirgo TYSM. That’s quite smart and what I was looking for. Cheers !