fix: map vector type to number[] in TypeScript type generation#1048
fix: map vector type to number[] in TypeScript type generation#1048nancysangani wants to merge 1 commit intosupabase:masterfrom
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review infoConfiguration used: Central YAML (base), Organization UI (inherited) Review profile: CHILL Plan: Pro Cache: Disabled due to Reviews > Disable Cache setting Disabled knowledge base sources:
📒 Files selected for processing (2)
📝 WalkthroughSummary by CodeRabbit
WalkthroughThis PR updates the TypeScript type generation to correctly map PostgreSQL vector types to TypeScript number arrays. Previously, the vector type was mapped to string, but the Supabase client requires number[] for RPC function parameters. The changes modify the pgTypeToTsType function to handle the vector type as a distinct case returning number[], and add comprehensive unit tests to verify mappings for both scalar vectors and array vectors. Assessment against linked issues
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
Fixes TypeScript type generation for Postgres vector so generated types align with Supabase client expectations when calling RPC functions.
Changes:
- Update
pgTypeToTsTypeto mapvectortonumber[](and_vectorto(number[])[]via existing array handling). - Add unit tests covering
vector/_vectormappings and a few basic scalar mappings.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/server/templates/typescript.ts |
Changes the TypeScript type mapping for vector from string to number[]. |
test/lib/typegen-typescript.test.ts |
Adds unit tests asserting the new vector / _vector mappings. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -0,0 +1,27 @@ | |||
| import { expect, test, describe } from 'vitest' | |||
| import { pgTypeToTsType } from '../../src/server/templates/typescript.js' | |||
There was a problem hiding this comment.
The test imports pgTypeToTsType from ../../src/server/templates/typescript.js, but there is no typescript.js file under src/server/templates (only typescript.ts). This will fail module resolution when running tests. Import from ../../src/server/templates/typescript (consistent with test/types.test.ts) or reference the .ts file as appropriate for the test runner config.
| import { pgTypeToTsType } from '../../src/server/templates/typescript.js' | |
| import { pgTypeToTsType } from '../../src/server/templates/typescript' |
| ) { | ||
| return 'string' | ||
| } else if (pgType === 'vector') { | ||
| return 'number[]' |
There was a problem hiding this comment.
Mapping vector to number[] here affects all type generation that uses pgTypeToTsType, including table/view column types (e.g. via generateColumnTsDefinition(... pgTypeToTsType(schema, column.format, ...) )), not just RPC function args. Given the reported runtime behavior difference between vector columns (often requiring a string representation) and RPC parameters (accepting number[]), consider scoping this mapping to RPC args only (e.g. add a mode/flag or a separate mapper for function params) or otherwise documenting/handling the column case to avoid a regression in generated table types.
| return 'number[]' | |
| return 'string | number[]' |
|
/cc @mandarini |
avallete
left a comment
There was a problem hiding this comment.
Thank's for contributing.
Only issue I have with this PR is the global scope of it, and lack of e2e tests about how this will work with postgrest-js end to end. I think the next steps we need is:
We need to first verify if this change would:
- Fix the issue on
postgrest-jsside - Have any noticeable side effect
To do so, we need to add more tests to reproduce the initial issue here:
And add some e2e tests:
https://github.com/supabase/supabase-js/blob/master/packages/core/postgrest-js/test/rpc.test.ts
Also add some tests with the behavior for similar types for insert/update/select on views/table.
Demonstrating that re-generating the types with this change doesn't BC something else and fix the original issue.
We currently don't have a good easy way to do so, that would be the first thing to do.
I think we need to change/add a scripts in postgrest-js:
https://github.com/supabase/supabase-js/blob/master/scripts/generate-postgrest-types.js
So we can easily point it to a local postgres-meta repo and regenerate the types from this version.
Right now the only way I see to do so, is to do:
cd supabase-js/packages/core/postgrest-js/test
npx supabase start db # start the test database
cd postgres-meta
PG_META_DB_URL="postgresql://postgres:postgres@127.0.0.1:54322/postgres" npm run dev # start pg-meta dev server and link it to the postgrest-js test db
curl --location 'http://localhost:1337/generators/typescript?included_schemas=public,personal&detect_one_to_one_relationships=true' -o supabase-js/packages/core/postgrest-js/test/types.generated.ts # regenerate the type tests for postgrest-js
cd supabase-js/packages/core/postgrest-js && npm run test:run # run the tests
With this new context we can safely proceed to deploy this more globally.
Thanks for the feedback. I’ll add tests to reproduce the issue, extend e2e coverage, and verify there are no breaking changes with regenerated types. I’ll also look into improving the type generation workflow. |
What kind of change does this PR introduce?
Bug fix
What is the current behavior?
vectortype in RPC function parameters generates asstringin TypeScript. The Supabase client requiresnumber[]for vector parameters —JSON.stringify()does not work as a workaround for RPC functions and causes queries to fail or return zero results.Fixes #1029
What is the new behavior?
vectortype now correctly generates asnumber[].Before:
After:
Additional context
vectorwas grouped with string types inpgTypeToTsType. Moved it to its own condition returningnumber[]. A unit test forpgTypeToTsTypeis added intest/lib/typegen-typescript.test.tscoveringvector → number[]and_vector → (number[])[].