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

[Feature request] Improve syntax for using typescript "from" function #172

Closed
Nick-Mazuk opened this issue Mar 8, 2021 · 3 comments
Closed

Comments

@Nick-Mazuk
Copy link

Feature request

First off, it's great that I can use TypeScript to statically type check everything. I think the syntax could be improved, though.

Right now, you need to add the definitions to every database operation:

supabase.from<definitions['table']>('table').select('*')

This works, but isn't optimal for a few reasons:

  • Need to declare the type for each from call
  • Need to declare the table name multiple times (prone to mistyping the table name)
  • The input to from is not checked (e.g., you could time from('tables') for the above snippet and it would not fail linting)

But what if the definitions were added to the supabase client instead?

// lib file to create the client
import { createClient } from '@supabase/supabase-js'

export const supabase = createClient<definitions>(supabaseUrl, supabaseKey)

// file where you use the client
supabase.from('table').select('*')

Then, you could be confident in your static type checking:

  • No need to duplicate the table name in the from function
  • It's guaranteed that all database calls are type checked
  • The from input could be type-checked (i.e., it's just the keyof definitions)
  • Cleaner code syntax all around
  • Added benefit that in VS Code, you get autocomplete on the table names, making the code more self-documenting
@kiwicopple
Copy link
Member

This is actually a lot cleaner - but I might need a TS expert to investigate how difficult it is to implement. I'll transfer this one to our postgrest-js repo for now 👍

@kiwicopple kiwicopple transferred this issue from supabase/supabase Apr 14, 2021
@baba43
Copy link

baba43 commented Aug 5, 2022

This is still open, right?
Because I thought about exacly the same.

@soedirgo
Copy link
Member

This is resolved in #279. It's still a prerelease right now, but will be released as v2 in a few weeks.

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

No branches or pull requests

4 participants