somebody figured out a good and expandable query f...
# help
d
somebody figured out a good and expandable query function like this but for other queries like gt() etc. as well? const getCollection = (c, q, eq) => { await supabase .from(c) .select(q) .eq(eq) }
n
Hello @daviscup! This thread has been automatically created from your message in #843999948717555735 a ``few seconds ago``. Pinging @User so that they see this as well! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ... menu) and select Leave Thread to unsubscribe from future updates. Want to change the title? Use the
/title
command! We have solved your problem? Click the button below to archive it.
s
Not sure what you mean by this? btw you probably need a return somewhere in that function.
n
daviscup (2022-03-24)
d
Ah yeah that's just the first couple lines. So what I mean is that instead of using different queries in all kinds of component, I want to use the same composable query function and pass the specifications to the composable function. ``const getCollection = (c, q, filter) => { if( filter == 'eq') { await supabase .from(c) .select(q) .eq(...) } if( filter == 'gt') { > await supabase .from(c) .select(q) .gt(...) } if( filter == 'lt') { await supabase .from(c) .select(q) .lt(...)`` } ......... ......... } This would work but it's a lame way to do it, I guess. I realize that's more a javascript question than supabase though. Just wondered if somebody else had this kind of flexible composable made as a template
g
d
awesome!
a
You can do this in a dynamic way and avoid having to create a parameter for each filter,
Copy code
const getCollection = (c, q, filter) => {
  const query = supabase
                .from(c)
                .select(q)

  Object
    .entries(filter)
    .forEach(([key, [column, value]]) => { 
      query = query[key](column, value)
    })
}
Filter object is like,
Copy code
{
  eq: [ 'name', 'John' ],
  gte: [ 'age', 10 ]
}