postgres-meta icon indicating copy to clipboard operation
postgres-meta copied to clipboard

Generate Typescript definitions

Open kiwicopple opened this issue 4 years ago • 10 comments

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

  • Issue: Array Columns Type Generation: https://github.com/supabase/supabase/pull/1215
  • We need different "types" for selects (which allow relationship) and insert/update (which don't allow relationships)
  • We should configure this so that we can generate types for other languages too

kiwicopple avatar Apr 21 '21 02:04 kiwicopple

Prior arts:

soedirgo avatar May 13 '21 08:05 soedirgo

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 😀

thomas-coldwell avatar Sep 24 '21 13:09 thomas-coldwell

@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.

soedirgo avatar Sep 25 '21 16:09 soedirgo

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

HarryET avatar Oct 30 '21 10:10 HarryET

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

soedirgo avatar Oct 31 '21 02:10 soedirgo

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

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

HarryET avatar Oct 31 '21 13:10 HarryET

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?

NixBiks avatar Nov 30 '21 11:11 NixBiks

@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.

soedirgo avatar Dec 01 '21 05:12 soedirgo

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

NixBiks avatar Dec 01 '21 06:12 NixBiks

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: https://github.com/supabase/postgrest-js/issues/217 (credits to @bnjmnt4n)

soedirgo avatar Dec 01 '21 07:12 soedirgo

Closing as it's now supported.

sweatybridge avatar Dec 23 '22 04:12 sweatybridge