Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(types): add override types method #603

Conversation

avallete
Copy link
Member

@avallete avallete commented Feb 17, 2025

  • Add overrideTypes method allow both partial and complete override of the return type, do partial override by default

@kamilogorek mentioned that we often had to port a lot of properties definition in order to override a single field when using .returns method.

This new method introduce a way to perform "partial override" per default, and allow the user to have similar behavior than the current .returns method by providing the option { merge: false }.

This make this kind of type override where we only want to override func_call result easier to maintain.

Before:

const results = await getOrThrowTyped(
  readWriteNext
    .from('sometable')
    .select(
      'id, projects!projects_sometable_id_fkey(ref, name), func_call(id)'
    )
    .eq('slug', 'some-slug')
    .is('projects.metadata->base_project_id', null)
    .neq('projects.status', 'SOMEVALUE')
    .single()
    .returns<{
      id: number
      func_call?: { value: SomeEnum }
      projects: { ref: string; name: string }[]
    }>(),
  'Failed to retrieve infos'
)

After

const results = await getOrThrowTyped(
  readWriteNext
    .from('sometable')
    .select(
      'id, projects!projects_sometable_id_fkey(ref, name), func_call(id)'
    )
    .eq('slug', 'some-slug')
    .is('projects.metadata->base_project_id', null)
    .neq('projects.status', 'SOMEVALUE')
    .single()
    .overrideTypes<{
      // Here we can now override only the part that we are interested in
      func_call?: { value: SomeEnum }
    }>(),
  'Failed to retrieve infos'
)

// We can still opt out from the merging behavior with:
await getOrThrowTyped(
  readWriteNext
    .from('sometable')
    .select(
      'id, projects!projects_sometable_id_fkey(ref, name), func_call(id)'
    )
    .eq('slug', 'some-slug')
    .is('projects.metadata->base_project_id', null)
    .neq('projects.status', 'SOMEVALUE')
    .single()
    .overrideTypes<{
      id: number
      func_call?: { value: SomeEnum }
      projects: { ref: string; name: string }[]
    }, { merge: false }>(),
  'Failed to retrieve infos'
)

@kamilogorek
Copy link
Member

Concept looks good to me and I believe it'd fix most our type problems.
Just a general note that https://supabase.com/docs/reference/javascript/typescript-support and https://supabase.com/docs/reference/javascript/db-returns will need to be updated/expanded.

Allow both partial and complete override of the return type, do partial override by default
@avallete avallete force-pushed the avallete/feat-add-overrideTypes-method branch from 6e990b7 to 5f24f39 Compare February 19, 2025 02:38
@avallete
Copy link
Member Author

avallete commented Feb 19, 2025

Concept looks good to me and I believe it'd fix most our type problems. Just a general note that https://supabase.com/docs/reference/javascript/typescript-support and https://supabase.com/docs/reference/javascript/db-returns will need to be updated/expanded.

Started to write some draft for the docs here: supabase/supabase#33705 for when it land.

@avallete avallete requested a review from soedirgo February 19, 2025 09:29
@soedirgo
Copy link
Member

Is this meant to be in draft still?

@avallete avallete marked this pull request as ready for review February 20, 2025 03:31
@avallete
Copy link
Member Author

avallete commented Feb 20, 2025

Is this meant to be in draft still?

Nope 😓

Feel free to give a review. I'll be merging this one with the "return type casting" PR so we can deploy both together.

@avallete avallete merged commit bfeb5cc into avallete/fix-add-returns-to-builder Feb 21, 2025
2 checks passed
Copy link

🎉 This PR is included in version 1.19.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants