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.86k stars 220 forks source link

Add note about typescript support for fetching #959

Open TimurKr opened 3 months ago

TimurKr commented 3 months ago

Improve documentation

In the documentation for fetching, under Query the same referenced table multiple times, it states an approach, which doesn't infer correct types on the resulting object.

Link

https://supabase.com/docs/reference/javascript/select

Describe the problem

The documentation states this approach

const { data, error } = await supabase
  .from('messages')
  .select(`
    content,
    from:sender_id(name),
    to:receiver_id(name)
  `)

This works and fetches the correct data, but with typescript, the types are incorrect; from and to are both typed as {}[].

Describe the improvement

I believe the documentation should mention another possible approach, which is also mentioned in the PostgREST documentation, which returns the correct data and infers the types correctly:

const { data, error } = await supabase
  .from('messages')
  .select(`
      content,
      from:users!messages_from_fkey(name),
      to:users!messages_from_fkey(name)
  `)

Here the users is the name of the foreign table from which we are querying and messages_from_fkey and messages_from_fkey are the names of the foreign key constraints.