supabase / postgrest-js

Isomorphic JavaScript client for PostgREST.
https://supabase.com
MIT License
962 stars 128 forks source link

[PSA] createClient().from(tableName) should not be reused! #510

Closed galaxyblur closed 6 months ago

galaxyblur commented 6 months ago

I'm not sure if the team wants to document this, but I spent a good half day trying to figure out what I was doing wrong. I'm leaving some keywords here in case someone else runs into this issue. (simultaneous, async, await, synchronous, subsequent, multiple)

const myClient = createClient<Database>(supabaseUrl, supabaseKey);
const myTable = myClient.from('my_table_name');
const result1 = await myTable.select('*');

// later...
const result2 = await myTable.select('*, other_table');
// ^ don't do this!

I was expecting from() to provide a table reference that I could setup & reuse for multiple queries. Digging around in the code, it creates some internal properties that get overwritten or extended by subsequent calls to select(), eq(), et al. The result is that the select "columns", and filters, will pollute the other calls to the API.