Skip to content

fix(postgres): preserve native contract query inference - #30342

Open
sam-goodwin wants to merge 1 commit into
prisma:mainfrom
sam-goodwin:fix/native-contract-type-inference
Open

sam-goodwin wants to merge 1 commit into
prisma:mainfrom
sam-goodwin:fix/native-contract-type-inference

Conversation

@sam-goodwin

@sam-goodwin sam-goodwin commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

Preserve query types when a TypeScript-authored contract is passed directly to postgres({ contract }), without emitted declarations. Addresses the query-inference failures in #30341.

  • Keep scalar/list channels, relation targets and cardinality, model namespaces, and naming strategies in the inferred contract.
  • Use empty maps that preserve target codec metadata when extensions are absent.
  • Derive aggregate types from the existing Postgres descriptors, with exact-codec precedence over trait fallbacks.
  • Include the registered text-array codec in the target's type map.

Summary by CodeRabbit

  • New Features

    • Added richer PostgreSQL contract typing for scalar and list fields, nullable values, relations, namespaces, naming options, and storage mappings.
    • Added type-safe aggregate operation support, including codec-specific results and validation of unsupported operations.
    • Added PostgreSQL text-array type support.
    • Contract results now preserve model namespaces and expose aggregate type information for direct consumers.
  • Documentation

    • Documented native query inference and aggregate metadata behavior for contract consumers.

Signed-off-by: Sam Goodwin <sam@alchemy.run>
@sam-goodwin
sam-goodwin requested a review from a team as a code owner September 18, 2026 07:06
@coderabbitai

coderabbitai Bot commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Advanced

Run ID: 07e3da84-b589-4bde-b2fa-35f749e0631a

📥 Commits

Reviewing files that changed from the base of the PR and between 1c434a7 and 9525fac.

📒 Files selected for processing (13)
  • packages/2-sql/2-authoring/contract-ts/README.md
  • packages/2-sql/2-authoring/contract-ts/src/aggregate-types.ts
  • packages/2-sql/2-authoring/contract-ts/src/composed-authoring-helpers.ts
  • packages/2-sql/2-authoring/contract-ts/src/contract-builder.ts
  • packages/2-sql/2-authoring/contract-ts/src/contract-dsl.ts
  • packages/2-sql/2-authoring/contract-ts/src/contract-types.ts
  • packages/3-extensions/postgres/package.json
  • packages/3-extensions/postgres/src/contract/define-contract.ts
  • packages/3-extensions/postgres/test/contract-builder/native-client.test-d.ts
  • packages/3-extensions/postgres/tsconfig.native-contract.json
  • packages/3-targets/3-targets/postgres/src/core/aggregates.ts
  • packages/3-targets/3-targets/postgres/src/core/codec-type-map.ts
  • packages/3-targets/3-targets/postgres/src/core/descriptor-meta.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The PR adds literal aggregate descriptor inference, namespace-aware contract type derivation, PostgreSQL naming and namespace options, narrowed contract return types, and native PostgreSQL compile-time tests.

Changes

Native contract inference

Layer / File(s) Summary
Aggregate descriptor publication
packages/2-sql/2-authoring/contract-ts/src/aggregate-types.ts, packages/3-targets/3-targets/postgres/src/core/aggregates.ts, packages/3-targets/3-targets/postgres/src/core/codec-type-map.ts, packages/3-targets/3-targets/postgres/src/core/descriptor-meta.ts, packages/2-sql/2-authoring/contract-ts/README.md
Aggregate operation types now resolve exact codec matches before trait matches. PostgreSQL descriptors preserve literal types, include the text-array codec, and are exposed through __aggregateDescriptors.
Namespace and aggregate type derivation
packages/2-sql/2-authoring/contract-ts/src/contract-dsl.ts, packages/2-sql/2-authoring/contract-ts/src/composed-authoring-helpers.ts, packages/2-sql/2-authoring/contract-ts/src/contract-types.ts, packages/2-sql/2-authoring/contract-ts/src/contract-builder.ts
Model builders retain namespace literals through staged methods. Relation, storage, field-channel, domain, extension, and aggregate result types are derived from the contract definition. defineContract implementation overloads return Contract<SqlStorage>.
PostgreSQL contract wiring and validation
packages/3-extensions/postgres/src/contract/define-contract.ts, packages/3-extensions/postgres/test/contract-builder/native-client.test-d.ts, packages/3-extensions/postgres/tsconfig.native-contract.json, packages/3-extensions/postgres/package.json
PostgreSQL contract overloads accept naming and namespaces. The native contract test suite checks inferred fields, relations, aggregates, prepared parameters, namespaces, naming mappings, and invalid entities. The package typecheck script runs the native test project.

Priority: ➖ Normal

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Bug fix

Suggested reviewers: wmadden-electric

Merge Risk: ⚪ Minimal · up to 9525f

No concrete regression in native contract inference or PostgreSQL aggregate typing is established by the available evidence.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 10 files. (3 skipped: … Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: preserving native contract query inference for PostgreSQL.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 10 files. (3 skipped: 3 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

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

@sam-goodwin

Copy link
Copy Markdown
Contributor Author

This was produced by AI, probably bad, LMK if you want changes or just re-do it. Problem is real

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