postgrest-js
postgrest-js copied to clipboard
Update Type Implementation (postgrest-js side)
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.
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?
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
Brilliant, thanks @kiwicopple. I'll keep an eye out for that release.
Guess I can close these now.