supabase / postgrest-js

Isomorphic JavaScript client for PostgREST.
https://supabase.com
MIT License
1.04k stars 133 forks source link

NO Typing/ autocompletion hint working When filtering on Foreign keys - V2. #351

Open roker15 opened 1 year ago

roker15 commented 1 year ago

Here is Example link from supabase docs.

const { data, error } = await supabase
  .from('cities')
  .select('name, countries(*)')
  .eq('countries.name', 'Estonia')

In above example countries.name shows error in types. And no autocompletion available aftercountries.

matt-winfield commented 1 year ago

I also have this issue. For example:

const result = await supabaseClient
    .from('Orders')
    .select(`
        order_id,
        Customers (
            name,
            customer_id
        )`)
    .eq('Customers.customer_id, 10);

result has the return type

PostgrestResponse<{
    order_status: number;
    id: number;
    created_at: string | null;
    customer_id: number;
} & {
    Customers: unknown;
}>

when I would expect the return type to be:

PostgrestResponse<{
    order_status: number;
    id: number;
    created_at: string | null;
    customer_id: number;
} & {
    Customers: {
        name: string,
        customer_id: number
    }
}>

Where the type of Customers properties should be determined from the Database schema.

This bug means that result.Customers.name is not possible due to "Customers" being unknown type, instead it must be done something like:

type Customer = Database['public']['Tables']['Customers']['Row'];
const customerName = (result.Customers as Customer).name;

Which is not ideal, as this approach will allow properties from the db missed from the query (e.g. if the table has a "email" field in customers table, but we forgot to include it in the query, (result.Customers as Customer).email would compile with TypeScript but throw an error at runtime).

pcardosolei commented 1 year ago

I can second this.

Even forcing on the from the type it does not work as <"table", Type> it works on the first one but always fails on relations.