supabase / cli

Supabase CLI. Manage postgres migrations, run Supabase locally, deploy edge functions. Postgres backups. Generating types from your database schema.
https://supabase.com/docs/reference/cli/about
MIT License
1.07k stars 209 forks source link

Include database comments in generated types #412

Open profiluefter opened 2 years ago

profiluefter commented 2 years ago

Feature request

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

My database schema has comments on every table an some columns. It would be nice if those comments could be included in the generated TypeScript types so that they will be displayed by the IDE.

Describe the solution you'd like

The supabase-cli should include comments in the generated TypeScipt interface.

Describe alternatives you've considered

I'm currently not yet using supabase-js V2 so I'm using an OpenAPI TypeScript generator from the definition by PostgREST. That definition includes comments and the generator includes them in the types.

Additional context

N/A

nate-peters commented 1 year ago

This would be hugely helpful to me as well. My workflow with supabase v1 (in absence of the new gen types features) was a combination of OpenAPI Typescript, and then typescript-json-schema to generate a JSON schema we use as a type validator.

With the OpenAPI workflow, the generated types with the comments included would look something like this:

// CLI command:
npx openapi-typescript $SUPABASE_API_URL --version=2 --output types/database/index.ts

// Output - ./types/database/index.ts
export interface definitions {
  /** @description Custom table built in supabase */
  table_name: {
    /**
     * Format: uuid
     * @description Note:
     * This is a Primary Key.<pk/>
     * @default extensions.uuid_generate_v4()
     */
    id: string;
    /**
     * Format: timestamp with time zone
     * @default now()
     */
    created_at?: string;
    /**
     * Format: integer
     * @description Custom description added in the Supabase dashboard
     */
    secret_number: number;
  }
{..other tables..}
}

And then my generated JSON schema from typescript-json-schema would look like:

// CLI command:
typescript-json-schema ./types/database/index.ts definitions -o ./sample-schema.json

// Output -sample-schema.json
{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "properties": {
        "table_name": {
            "description": "Custom table built in supabase ",
            "properties": {
                "id": {
                    "default": "extensions.uuid_generate_v4()",
                    "description": "Note:\nThis is a Primary Key.<pk/>",
                    "type": "string"
                },
                "created_at": {
                    "default": "now()",
                    "description": "Format: timestamp with time zone",
                    "type": "string"
                },
                "secret_number": {
                    "description": "Custom description added in the Supabase dashboard",
                    "type": "number"
                }
            }
        }
    },
    "type": "object"
}

It would be great if the supabase gen types command had a flag like --include-descriptions that would include table and column descriptions in the same format as OpenAPI uses.