supabase/postgres-meta

Gen type throws an exception when table has no columns

Closed this issue · 2 comments

Describe the bug

I am encountering an error when attempting to generate Typescript types for multiple schemas in my self-hosted database.

Connecting to [REDACTED]
(node:1) ExperimentalWarning: Importing JSON modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
file:///usr/src/app/dist/server/templates/typescript.js:54
                ...columnsByTableId[table.id].map((column) => `${JSON.stringify(column.name)}: ${pgTypeToTsType(column.format, types, schemas)} ${column.is_nullable ? '| null' : ''}`),
                                              ^

TypeError: Cannot read properties of undefined (reading 'map')
    at file:///usr/src/app/dist/server/templates/typescript.js:54:47
    at Array.map (<anonymous>)
    at file:///usr/src/app/dist/server/templates/typescript.js:51:28
    at Array.map (<anonymous>)
    at apply (file:///usr/src/app/dist/server/templates/typescript.js:16:10)
    at file:///usr/src/app/dist/server/server.js:78:17
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
error running container: exit 1
Try rerunning the command with --debug to troubleshoot the error

To Reproduce

$ export DB_URL=[REDACTED]
$ supabase gen types typescript \
  --db-url $DB_URL >libs/database.types.ts \
  --schema public --schema assets

Expected behavior
Expected work fine without any issues.

Desktop (please complete the following information):

  • OS: Windows 11
  • Version of CLI: 1.86.2
  • Version of Node.js: v20.2.0

Additional context

It works when I pass a single schema name, but it fails when I pass multiple schema names.

Is there a solution or flag to automatically create types for every schema in the database without the need for manual addition in the command?

I'm mean something like this:

supabase gen types typescript --db-url $DB_URL >libs/database.types.ts --full-database

It will be very helpful for me and other developers.

Kinda late but for anyone that comes across this later.
This can be done with one schema tag and a string array of the schemas you would like to generate from.
Taking this example that throws an error from OP:

$ supabase gen types typescript \
  --db-url $DB_URL >libs/database.types.ts \
  --schema public --schema assets

This is how you would combine the two --schema tags into one

$ supabase gen types typescript \
  --db-url $DB_URL >libs/database.types.ts \
  --schema public,assests

Works like a charm!

I can reproduce this by creating a table without any columns, ie create table test(). Transferring to the postgres-meta repo since it is an upstream bug.