Skip to content

[Pg-core]: add typed references to PostgreSQL columns #4388

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tharropoulos
Copy link

TLDR

Add proper type tracking for PostgreSQL column references with optional/required distinction.

Change Summary

Added Features:

  1. Typed References in PostgreSQL:
    • Added proper type information to references between columns
    • Implemented typed references property on columns to track reference relationship
    • Distinction between optional and required references based on notNull status

Code Changes:

  1. In column-builder.ts:

    • Refactored BuildColumn type with conditional types to track reference relationships
    • Added reference type parameter to PgColumn generic interface
    • Added differentiation between optional and required references
  2. In pg-core/columns/common.ts:

    • Made ReferenceConfig generic to preserve type of referenced column
    • Updated references() method to return enhanced type with reference information
    • Added references property to PgColumn class to store reference target
    • Set reference during foreign key setup in column creation

Tests:

  1. In type-tests/pg/references.ts:
    • Added type tests to validate optional and required reference behavior
    • Verified correct type inference for both reference scenarios

Context

  • This change improves the type safety of column references in PostgreSQL, allowing TypeScript to track the relationship between columns and providing better autocomplete and type checking for referenced columns (Getting references from a column in types #4387)

- Add proper type information to references between columns in PostgreSQL
- Track reference relationship with conditional types in `BuildColumn`
- Preserve type information from referenced column in new `references` property
- Distinguish between optional and required references with proper typing
- Add type tests to validate typed reference behavior
@tharropoulos tharropoulos changed the title [Pg]: add typed references to PostgreSQL columns [Pg-core]: add typed references to PostgreSQL columns Apr 8, 2025
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.

1 participant