Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typegen): Fix support for custom SQL schemas in TypeScript type generation. #717

Closed
wants to merge 6 commits into from

Conversation

yingw787
Copy link
Contributor

@yingw787 yingw787 commented Feb 15, 2024

What kind of change does this PR introduce?

[x] Patches PR #654 to support SQL schemas outside of "public". Cannot directly use keyword "schema" due to circular type constraint error in definition PublicTableNameOrOptions.
[x] Update type generation tests to reflect updated TypeScript types, with local run of npm run test confirming 96 tests passed.

Addresses GitHub issue #716.

What is the current behavior?

When running:

PG_META_GENERATE_TYPES_INCLUDED_SCHEMAS=openai PG_META_DB_URL=postgresql://postgres:postgres@127.0.0.1:54322/postgres npm run gen:types:typescript

Against a Supabase instance where database is at postgresql://postgres:postgres@127.0.0.1:54322/postgres and has schema openai among others, types Table, TablesInsert, TablesUpdate, and Enums result in type errors because schema public is not defined.

What is the new behavior?

Against a Supabase instance where database is at postgresql://postgres:postgres@127.0.0.1:54322/postgres and has schema openai among others, types Table, TablesInsert, TablesUpdate, and Enums do not result in type errors.

Additional context

N/A.

@yingw787 yingw787 requested review from a team as code owners February 15, 2024 16:34
@yingw787 yingw787 changed the title [WIP] fix(typegen): Fix support for custom SQL schemas in TypeScript type generation. fix(typegen): Fix support for custom SQL schemas in TypeScript type generation. Feb 15, 2024
Comment on lines 369 to 371
PublicTableNameOrOptions extends
| keyof (Database["public"]["Tables"] & Database["public"]["Views"])
| keyof (Database[keyof Database]["Tables"] &
Database[keyof Database]["Views"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I think the concept here is to constrain each table to a new type on its own, for eg. PublicTableNameOrOptions type corresponds to the public schema exclusively.

We might need to generate separate export type Tables blocks depending on the schema name. @soedirgo wdyt?

Copy link
Contributor Author

@yingw787 yingw787 Feb 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sounds good! I was looking at that approach but since I don't personally use the new Table types I didn't know what the context of including them was. Happy to make this change (mapping over each schema to create the PascalCase Table types, and updating the typegen tests) and push up later this evening.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sweatybridge @soedirgo just pushed up some changes in order to create schema-specific types for Tables, TablesInsert, TablesUpdate, and Enums. Would love feedback on the source and test cases (should I add an additional test case to cover schemas not named public, and having multiple schemas)?

Copy link
Contributor

@sweatybridge sweatybridge Feb 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some thought, I think there's a backwards compatibility issue with this change. So I proposed a new approach in #723 and added you as the co-author.

We can close this PR if you are happy with it.

sweatybridge
sweatybridge previously approved these changes Feb 17, 2024
Copy link
Contributor

@sweatybridge sweatybridge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. Will wait for @soedirgo's confirmation.

@yingw787
Copy link
Contributor Author

@sweatybridge mentioned in #723 I'm happy with those changes - I'll close this PR now. Thanks!

@yingw787 yingw787 closed this Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants