supabase / supabase-js

An isomorphic Javascript client for Supabase. Query your Supabase database, subscribe to realtime events, upload and download files, browse typescript examples, invoke postgres functions via rpc, invoke supabase edge functions, query pgvector.
https://supabase.com
MIT License
2.96k stars 229 forks source link

Storing part of a query in a variable and reusing it causes unexpected behaviors #767

Open dshukertjr opened 1 year ago

dshukertjr commented 1 year ago

Bug report

Describe the bug

Storing part of a query in a variable is a common pattern in Firebase development. Right now because our client library is not immutable, when you try to do this with Supabase client library, some of the information is kept in the original instance causing unexpected behaviors.

const restaurantsTable = supabase.from('restaurants')

const { data: restaurant } = await restaurantsTable.select().eq('id', id).single()
const { data: restaurants } = await restaurantsTable.select() // the query here will contain the `eq` and `single` statement from the previous line, therefore returning a single restaurant.

Expected behavior

Storing part of a query in a variable and running multiple queries using it should return the proper data.

const restaurantsTable = supabase.from('restaurants')

const { data: restaurant } = await restaurantsTable.select().eq('id', id).single()
const { data: restaurants } = await restaurantsTable.select() // `restaurants` contains the unfiltered query result.

Additional context

Maybe this is something to keep in mind when doing the next major release.

kurtisauc3 commented 5 months ago

+1 to this. Its seems like the chaining of methods is mutating the original query, so you can't re-use it.

brettmwright commented 3 months ago

Having same issue :(

rjbrooksjr commented 1 month ago

Same issue - It would be great when I have a join and end up generating a type for it, I could do those once somewhere where I can export the query and type so I'm not doing the same thing over and over again; however this makes that very awkward because only the type is reusable and not the query.