postgrest-js icon indicating copy to clipboard operation
postgrest-js copied to clipboard

Update Type Implementation (postgrest-js side)

Open beeequeue opened this issue 4 years ago • 3 comments
trafficstars

What kind of change does this PR introduce?

A breaking type update, which allows easier and stricter type checking. This is a combined update for this package and supabase-js.

supabase/supabase-js#125

What is the current behavior?

  • You have to pass the object you're working on into .from<User>('users') every time you use it.
  • .select() does not have any intellisense help.

What is the new behavior?

You get intellisense suggestions in .select().


You now pass the schema into the PostgrestClient, and let the types handle the rest from there.

The schema type is based on what openapi-typescript generates, as recommended in the supabase guide (which also has to be updated as the tool has renamed).

type User = {
  username: string
  data: object | null
  age_range: string | null
  status: 'ONLINE' | 'OFFLINE'
  catchphrase: 'string' | null
}

type Schema = {
  users: User
}

const supabase = new PostgrestClient<Schema>(REST_URL)

// Incorrect table, column names will result in type errors
typedClient.from('users').select('username').eq('username', 'supabot')

// Error
typedClient.from('uesrs').select('username').eq('username', 'wooo')
typedClient.from('users').select('username').eq('not_in_table', 'wooo')

Additional context

Not passing in a type still works as before, with any names working and returning any.

I tried very hard to not make this a breaking change, by still allowing .from<Object>(), but it's sadly impossible without some crazy workarounds. 😢

This package will have to be released before supabase-js so #123 can be updated to use the new version.

beeequeue avatar Feb 01 '21 19:02 beeequeue

Went digging looking for how to type select() calls and came across this PR 🙂 Sorry to be 'that guy' - any idea of the state of this?

jonlambert avatar Jul 20 '21 18:07 jonlambert

We're working on this as part of the Supabase v2 release (https://github.com/supabase/supabase-js/issues/170), but that will be after Launch Week (next week). We'll do some project planning in August and should have a better idea of dates then @jonlambert

kiwicopple avatar Jul 21 '21 15:07 kiwicopple

Brilliant, thanks @kiwicopple. I'll keep an eye out for that release.

jonlambert avatar Jul 23 '21 10:07 jonlambert

Guess I can close these now.

beeequeue avatar Sep 06 '22 15:09 beeequeue