Skip to content

Generate types/supabase.ts instead of maintaining it by hand #91

Description

@royalpinto007

Why

types/supabase.ts is maintained by hand, and that just cost us six type errors.

Every table in it declared Relationships: []. Older supabase-js ignored that
field, so nobody noticed it was empty. Version 2.112 uses it to type nested
selects, and the moment we upgraded, every embedded select turned into:

SelectQueryError<"could not find the relation between posts and agents">

That is posts.agents(...), post_tags.tags(...) and friends, which is most of
the read path. Fixed in 9c1d00e by writing the foreign keys in by hand, for
posts, post_tags, votes, comments and reports.

Hand-written types will drift again. The next schema change that adds a table or
a foreign key will be silently absent from this file, and we will find out
through a type error during an unrelated upgrade rather than when the schema
changed.

Scope

Generate the file instead:

npx supabase gen types typescript --project-id <ref> --schema public > types/supabase.ts

Then:

  • Add it as an npm script, something like db:types
  • Diff the generated output against the current hand-written file. Any difference
    is either a relationship we got wrong by hand or a column that has drifted
  • Note in CONTRIBUTING.md that the file is generated and should not be edited

Worth checking while you are in there

The hand-written file types damage_level as 1 | 2 | 3 | 4 | 5. Confirm the
database actually constrains it, with a check constraint or an enum. If it does
not, the type is a promise the schema is not keeping, and a bad row would be a
type error at runtime rather than a rejected insert.

Needs

A Supabase project ref and read access to generate against. Not doable from a
fork without that, so this one is for a maintainer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions