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

Generate Typescript definitions #103

Closed
kiwicopple opened this issue Apr 21, 2021 · 11 comments
Closed

Generate Typescript definitions #103

kiwicopple opened this issue Apr 21, 2021 · 11 comments
Labels
enhancement New feature or request

Comments

@kiwicopple
Copy link
Member

kiwicopple commented Apr 21, 2021

Feature request

Is your feature request related to a problem? Please describe.

Currently we recommend creating Types from the OpenAPI spec (https://supabase.io/docs/reference/javascript/generating-types)

This will cause a few problems because the types in Open API spec are somewhat limited.

Describe the solution you'd like

It would be great to generate these definitions directly from the database. If we build it into this library then we can use it on the Dashboard and our CLI.

Additional context

@soedirgo
Copy link
Member

Prior arts:

@thomas-coldwell
Copy link

Am I correct in assuming this will enable a better typed Data API similar to what Prisma offers where a dev can run a script using a CLI tool to generate a typed client and then use that typed client to get automatic return types for their query, for example:

const data = await typedSupabaseClient.posts.findMany({
  where: { author: "Bob" },
  include: { author: true }
});

// With the inferred return type of data thus being (Post & { author: Author })[]

I know that the API might have to be more explicit e.g. what fields to perform a join on, but I think the DX is quite nice. I'm currently using tRPC + Prisma + NextJS API Routes to achieve this kind of type safety, but would love to be able to do this directly with Supabase 😀

@soedirgo
Copy link
Member

soedirgo commented Sep 25, 2021

@thomas-coldwell this one's a bit different, the aim is to generate the types for your tables & columns, so you don't have to manually write something like this yourself:

// CREATE TABLE users (
//   id int8,
//   data text
// );
interface Users {
  id: number
  data: string
}

And then you use it like:

const { data }: { data: Users[] } = await supabase
  .from<Users>('users')
  .select()

We don't really do ORMs, so this is just for convenience when working with the REST API.

@HarryET
Copy link

HarryET commented Oct 30, 2021

Is anyone currently working on this? I could have a look.

It would be great to generate these definitions directly from the database. If we build it into this library then we can use it on the Dashboard and our CLI.

Would the lib would also generate files or just print it out as text e.g. generateTypes(/* args */) which returns a string of types

\cc @kiwicopple

@soedirgo
Copy link
Member

This issue has been stalled for a while - I suspect we'd need to sort out supabase/supabase-js#170 first (updating type implementation especially) before we do this.

@HarryET
Copy link

HarryET commented Oct 31, 2021

This issue has been stalled for a while - I suspect we'd need to sort out supabase/supabase-js#170 first (updating type implementation especially) before we do this.

Ah ok 👍🏼 I was just looking around for issues to help with 🙂

@NixBiks
Copy link

NixBiks commented Nov 30, 2021

Would this also support signatures for .rpc calls? I'm currently holding off on .rpc a bit because there is not really any typesafe way to get around it, right?

@soedirgo
Copy link
Member

soedirgo commented Dec 1, 2021

@mr-bjerre it's technically possible, but would need more work to make arguments type-safe from pg-meta itself. So yeah .rpc() isn't type-safe atm.

@NixBiks
Copy link

NixBiks commented Dec 1, 2021

Alright. Do you plan to add typesafety for it? And what about selects where you join tables?

@soedirgo
Copy link
Member

soedirgo commented Dec 1, 2021

The jury's out if we can make it type-safe, but yes ideally we'd have type-safety. Re: selects there has been some progress here: supabase/postgrest-js#217 (credits to @bnjmnt4n)

@sweatybridge
Copy link
Contributor

Closing as it's now supported.

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

No branches or pull requests

6 participants