supabase / postgrest-js

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

feat: allow overriding result types #345

Closed soedirgo closed 1 year ago

soedirgo commented 1 year ago

What kind of change does this PR introduce?

feature

What is the current behavior?

Result type for .select() cannot be overridden. Since typings for nested relations is still a WIP, we need an escape hatch so people can set the types for specific fields.

What is the new behavior?

Support overriding the types for specific fields. E.g. from the tests:

// can override result type
{
  const { data, error } = await postgrest
    .from('users')
    .select('*, messages(*)')
    .returns<{ messages: { foo: 'bar' }[] }>()
  if (error) {
    throw new Error(error.message)
  }
  expectType<{ foo: 'bar' }[]>(data[0].messages)
}

Additionally, instead of always returning unknown for all nested relations, we instead return T | T[]. This is as specific as we can get despite the ambiguity.

github-actions[bot] commented 1 year ago

:tada: This PR is included in version 1.1.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: