Generate Typescript definitions
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) andinsert/update(which don't allow relationships) - We should configure this so that we can generate types for other languages too
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 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.
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
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.
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 🙂
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?
@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.
Alright. Do you plan to add typesafety for it? And what about selects where you join tables?
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)
Closing as it's now supported.