Skip to content

Conversation

@Newbie012
Copy link
Collaborator

@Newbie012 Newbie012 commented Dec 22, 2025

Enhances type inference for arrays of enums in the TypeScript generation logic, ensuring that it correctly expands to the enum's members instead of defaulting to unknown[]. Additionally, adds testing guidelines for running tests in the project.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed type inference for arrays of enums—they now correctly expand to individual enum member types instead of being inferred as unknown[].

✏️ Tip: You can customize this high-level summary in your review settings.

Enhances type inference for arrays of enums in the TypeScript generation logic, ensuring that it correctly expands to the enum's members instead of defaulting to `unknown[]`. Additionally, adds testing guidelines for running tests in the project.
@changeset-bot
Copy link

changeset-bot bot commented Dec 22, 2025

🦋 Changeset detected

Latest commit: 1dd9be2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 17 packages
Name Type
@ts-safeql/eslint-plugin Patch
@ts-safeql/generate Patch
@ts-safeql-demos/basic-flat-config Patch
@ts-safeql-demos/basic-migrations-raw Patch
@ts-safeql-demos/basic-transform-type Patch
@ts-safeql-demos/basic Patch
@ts-safeql-demos/big-project Patch
@ts-safeql-demos/config-file-flat-config Patch
@ts-safeql-demos/from-config-file Patch
@ts-safeql-demos/multi-connections Patch
@ts-safeql-demos/playground Patch
@ts-safeql-demos/postgresjs-custom-types Patch
@ts-safeql-demos/postgresjs-demo Patch
@ts-safeql-demos/vercel-postgres Patch
@ts-safeql/shared Patch
@ts-safeql/sql-ast Patch
@ts-safeql/test-utils Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Dec 22, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Review Updated (UTC)
safeql Ignored Ignored Preview Dec 22, 2025 8:36am

@coderabbitai
Copy link

coderabbitai bot commented Dec 22, 2025

Warning

Rate limit exceeded

@Newbie012 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 2 minutes and 42 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 510cf76 and 1dd9be2.

📒 Files selected for processing (1)
  • packages/generate/src/generate.ts

Walkthrough

The changes implement a fix for inferring array enum types during code generation. A new typelem field is added to the PostgreSQL type metadata structure, enum resolution helpers are introduced across the generate module, and a test validates that array enum columns correctly expand to union types of enum members.

Changes

Cohort / File(s) Summary
Configuration & Testing Guidance
.changeset/fancy-chefs-cough.md, .cursor/rules/testing.mdc
Adds changeset documenting bugfix for array enum inference; introduces testing guidance file with Vitest workflow and examples.
Type System & Enum Resolution
packages/generate/src/generate.ts
Adds typelem field to PgTypeRow interface; updates SQL query to select typelem; introduces getEnumByOid helper; extends enum resolution to support array types by checking element type when direct OID lookup fails.
AST Description & Type Conversion
packages/generate/src/ast-describe.ts
Refactors enum handling with pgType constant fetched once per operation; introduces getEnumByOid helper for safe enum lookup; adds valueAsEnum logic that prefers exact enum by OID or falls back to array enum resolution; integrates enum-based descriptions into main type resolution flow.
Test Coverage
packages/generate/src/generate.test.ts
Adds test case "select array of enums" validating inference of my_enum[] columns expands to union of enum literal members.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

  • packages/generate/src/generate.ts: Review the new typelem field addition to PgTypeRow, verify SQL query change is complete, and validate the two-stage enum resolution logic (direct OID lookup → array element fallback)
  • packages/generate/src/ast-describe.ts: Examine the new helper functions getEnumByOid and valueAsEnum for correct null-safety and enum precedence; ensure non-enum paths remain unaffected
  • Integration point: Confirm that typelem field flows correctly from SQL retrieval through both files and that the fallback logic preserves existing behavior for non-enum types

Poem

🐰 An array of enums, once shrouded in mystery,
Now sparkles with clarity through our history!
Typelem whispers its secrets so true,
Resolving each member to a union anew.
No more unknown[], just beauty displayed—
Type safety's gambit, perfectly played! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: improve type inference for array of enums' directly and clearly summarizes the main change: fixing type inference for array of enums to expand to enum members instead of unknown[].

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/generate/src/generate.ts (1)

461-496: LGTM!

The enum resolution logic correctly implements the two-stage lookup:

  1. Direct enum lookup by OID
  2. Fallback to array element enum via typelem

The implementation is consistent with ast-describe.ts.

Consider extracting the getEnumByOid and valueAsEnum logic into a shared utility function to avoid duplication between generate.ts and ast-describe.ts. Both files implement nearly identical logic for enum resolution.

📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ac7a1b7 and 510cf76.

📒 Files selected for processing (5)
  • .changeset/fancy-chefs-cough.md
  • .cursor/rules/testing.mdc
  • packages/generate/src/ast-describe.ts
  • packages/generate/src/generate.test.ts
  • packages/generate/src/generate.ts
🔇 Additional comments (8)
.cursor/rules/testing.mdc (1)

1-29: LGTM!

The testing guidelines are clear and provide a useful reference for running Vitest tests in the monorepo structure. The step-by-step workflow and example command are helpful for developers.

.changeset/fancy-chefs-cough.md (1)

1-6: LGTM!

The changeset correctly identifies both affected packages and accurately describes the fix for array enum inference.

packages/generate/src/generate.test.ts (1)

2486-2510: LGTM!

The test case effectively validates the new array enum inference feature. The expected output structure correctly represents an array containing a union of the enum's string literal members, which aligns with the implementation changes in ast-describe.ts and generate.ts.

packages/generate/src/ast-describe.ts (3)

117-127: LGTM!

Good refactor to retrieve pgType early and use it for the type field. The optional chaining with fallback to "unknown" handles missing types gracefully.


140-180: LGTM!

The two-stage enum resolution logic is well-structured:

  1. Direct enum lookup by OID
  2. Fallback to array element enum when typelem is present and non-zero

The use of satisfies ASTDescribedColumnType ensures type safety for the array wrapper.


187-187: LGTM!

Consistent use of pgType?.name for the type field with proper fallback.

packages/generate/src/generate.ts (2)

627-631: LGTM!

The typelem field is correctly typed as number (PostgreSQL OID type). This field contains the element type OID for array types, which is essential for the array enum resolution.


635-638: LGTM!

The SQL query correctly fetches typelem from pg_type. This is a standard PostgreSQL catalog column that stores the OID of the element type for array types.

@Newbie012 Newbie012 merged commit 3b980f9 into main Dec 22, 2025
5 checks passed
@Newbie012 Newbie012 deleted the fix/array-enum-inference branch December 22, 2025 08:38
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