Skip to content
This repository was archived by the owner on Nov 25, 2026. It is now read-only.

TML-2837: eliminate the defineContract cast pattern in the SQL extension wrappers via a shared bound builder - #731

Merged
wmadden-electric merged 5 commits into
mainfrom
tml-2837-sql-definecontract-cast-pattern
Jun 5, 2026
Merged

wmadden-electric merged 5 commits into
mainfrom
tml-2837-sql-definecontract-cast-pattern

Conversation

@wmadden-electric

@wmadden-electric wmadden-electric commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

What & why

Port of #730 (TML-2817, mongo) to the SQL family. The sqlite and postgres extension defineContract wrappers carried the same bare-cast pattern: they pre-bind family/target and delegate from a non-generic impl body to the overloaded, generic base defineContract, forcing construction / ContractInput / as unknown as factory / Parameters<…> casts.

Same fix: a shared, family/target-bound buildBoundContract in the SQL contract-builder, which the base defineContract and both extension wrappers delegate to. Both wrappers are now cast-free.

Result

  • packages/3-extensions/sqlite/src/contract/define-contract.ts: 4 → 0 bare casts.
  • packages/3-extensions/postgres/src/contract/define-contract.ts: 4 → 0 bare casts.
  • buildBoundContract (new, in the SQL contract-builder.ts): a single plainly-overloaded function — no overload-return cast, no new casts. The one irreducible helpers-composition cast already lives in createComposedAuthoringHelpers (composed-authoring-helpers.ts) — untouched, still in exactly one place.
  • Net −8 casts across the three files; the only as remaining in contract-builder.ts is a pre-existing, unrelated as Record<string, ModelLike> (byte-identical to main).

Result-type equivalence

The wrappers' result types were re-derived from Omit<ReturnType<baseDefineContract<…>>, 'target'|'targetFamily'> & { target; targetFamily } to ReturnType<typeof buildBoundContract<…>>. These are structurally identical — the old Omit & {…} was a no-op re-statement, since the base builder's result already emits the target-id literal and targetFamily: 'sql'. Verified empirically in review with a full-result toEqualTypeOf<wrapper, base> assertion across definition / factory / empty forms on both targets.

Verification

@prisma-next/sql-contract-ts, @prisma-next/sqlite, @prisma-next/postgres typecheck clean; 411 tests pass, including the type tests (define-contract.test-d.ts for both extensions — family/target rejection, definition- and factory-form result types, target/targetFamily literals). Pure type-level + structural refactor — no runtime behaviour change (factory-merge order and the base isContractInput guard preserved). Independently reviewed.

Refs: TML-2837

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added an exported API to build contracts with pre-bound family/target for direct use.
  • Refactor

    • Centralized contract-building so database-specific defineContract wrappers now delegate to the shared bound-builder, removing duplicated assembly/casting logic.
  • Documentation

    • Updated upgrade instructions to state no action required for extension authors.

@wmadden-electric
wmadden-electric requested a review from a team as a code owner June 5, 2026 04:55
@coderabbitai

coderabbitai Bot commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Review 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: Pro

Run ID: 0f9e533e-b765-4d90-b64b-beea5ec88fc0

📥 Commits

Reviewing files that changed from the base of the PR and between f5e2867 and 3d7326f.

📒 Files selected for processing (2)
  • packages/2-sql/2-authoring/contract-ts/src/contract-builder.ts
  • skills/extension-author/prisma-next-extension-upgrade/upgrades/0.12-to-0.13/instructions.md
✅ Files skipped from review due to trivial changes (1)
  • skills/extension-author/prisma-next-extension-upgrade/upgrades/0.12-to-0.13/instructions.md
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/2-sql/2-authoring/contract-ts/src/contract-builder.ts

📝 Walkthrough

Walkthrough

Adds a reusable buildBoundContract API that centralizes contract construction with pre-bound family/target and updates Postgres and SQLite defineContract wrappers to call it; re-exports buildBoundContract and updates upgrade docs to note no extension-author action is required.

Changes

Contract Construction Refactoring

Layer / File(s) Summary
Core buildBoundContract API and defineContract routing
packages/2-sql/2-authoring/contract-ts/src/contract-builder.ts, packages/2-sql/2-authoring/contract-ts/src/exports/contract-builder.ts
New buildBoundContract (two overloads) merges bound family/target into definitions, optionally runs a factory with composed helpers, conditionally injects factory-produced types/models, and delegates to buildContractFromDsl. defineContract now always delegates through buildBoundContract. buildBoundContract is re-exported.
PostgreSQL extension migration to buildBoundContract
packages/3-extensions/postgres/src/contract/define-contract.ts
Postgres defineContract now builds a bound definition (adds createNamespace) and calls buildBoundContract(sqlFamilyPack, postgresPack, bound, factory?). PostgresResult is now derived from ReturnType<typeof buildBoundContract<...>>.
SQLite extension migration to buildBoundContract
packages/3-extensions/sqlite/src/contract/define-contract.ts
SQLite defineContract now accepts a definition, adds createNamespace, and delegates to buildBoundContract(sqlFamilyPack, sqlitePack, bound, factory?). SqliteResult is now derived from ReturnType<typeof buildBoundContract<...>>; prior scaffold/defaults removed.
Upgrade documentation for 0.12 → 0.13
skills/extension-author/prisma-next-extension-upgrade/upgrades/0.12-to-0.13/instructions.md
Notes that no extension-author action is required and documents the internal cast-free defineContract refactor using buildBoundContract while preserving emitted contract shapes.

Sequence Diagram

sequenceDiagram
  participant PostgresExtension
  participant SqliteExtension
  participant ContractBuilder
  participant buildContractFromDsl
  PostgresExtension->>ContractBuilder: buildBoundContract(sqlFamilyPack, postgresPack, bound, factory?)
  SqliteExtension->>ContractBuilder: buildBoundContract(sqlFamilyPack, sqlitePack, bound, factory?)
  ContractBuilder->>buildContractFromDsl: buildContractFromDsl(mergedDefinition)
  buildContractFromDsl-->>ContractBuilder: SqlContractResult
  ContractBuilder-->>PostgresExtension: SqlContractResult
  ContractBuilder-->>SqliteExtension: SqlContractResult
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • prisma/prisma-next#730: Refactors extension-layer defineContract wrappers to delegate to shared buildBoundContract, similar migration pattern.
  • prisma/prisma-next#574: Overlaps at the contract-builder entrypoints and recent changes to SQL defineContract authoring pipeline.

Suggested reviewers

  • wmadden
  • aqrln

Poem

🐰 I stitched a bound builder neat and quick,
I pre-bound family and target — a clever trick,
Postgres and SQLite now hop the same way,
No casts, no scaffolds, just tidy relay,
Hooray—contracts hop forward, light and slick!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. 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 accurately and specifically describes the main change: eliminating type casts in SQL extension defineContract wrappers by introducing a shared buildBoundContract helper.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch tml-2837-sql-definecontract-cast-pattern

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.

@pkg-pr-new

pkg-pr-new Bot commented Jun 5, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@prisma-next/extension-author-tools

npm i https://pkg.pr.new/@prisma-next/extension-author-tools@731

@prisma-next/mongo-runtime

npm i https://pkg.pr.new/@prisma-next/mongo-runtime@731

@prisma-next/family-mongo

npm i https://pkg.pr.new/@prisma-next/family-mongo@731

@prisma-next/sql-runtime

npm i https://pkg.pr.new/@prisma-next/sql-runtime@731

@prisma-next/family-sql

npm i https://pkg.pr.new/@prisma-next/family-sql@731

@prisma-next/extension-arktype-json

npm i https://pkg.pr.new/@prisma-next/extension-arktype-json@731

@prisma-next/middleware-cache

npm i https://pkg.pr.new/@prisma-next/middleware-cache@731

@prisma-next/mongo

npm i https://pkg.pr.new/@prisma-next/mongo@731

@prisma-next/extension-paradedb

npm i https://pkg.pr.new/@prisma-next/extension-paradedb@731

@prisma-next/extension-pgvector

npm i https://pkg.pr.new/@prisma-next/extension-pgvector@731

@prisma-next/extension-postgis

npm i https://pkg.pr.new/@prisma-next/extension-postgis@731

@prisma-next/postgres

npm i https://pkg.pr.new/@prisma-next/postgres@731

@prisma-next/sql-orm-client

npm i https://pkg.pr.new/@prisma-next/sql-orm-client@731

@prisma-next/sqlite

npm i https://pkg.pr.new/@prisma-next/sqlite@731

@prisma-next/target-mongo

npm i https://pkg.pr.new/@prisma-next/target-mongo@731

@prisma-next/adapter-mongo

npm i https://pkg.pr.new/@prisma-next/adapter-mongo@731

@prisma-next/driver-mongo

npm i https://pkg.pr.new/@prisma-next/driver-mongo@731

@prisma-next/contract

npm i https://pkg.pr.new/@prisma-next/contract@731

@prisma-next/utils

npm i https://pkg.pr.new/@prisma-next/utils@731

@prisma-next/config

npm i https://pkg.pr.new/@prisma-next/config@731

@prisma-next/errors

npm i https://pkg.pr.new/@prisma-next/errors@731

@prisma-next/framework-components

npm i https://pkg.pr.new/@prisma-next/framework-components@731

@prisma-next/operations

npm i https://pkg.pr.new/@prisma-next/operations@731

@prisma-next/ts-render

npm i https://pkg.pr.new/@prisma-next/ts-render@731

@prisma-next/contract-authoring

npm i https://pkg.pr.new/@prisma-next/contract-authoring@731

@prisma-next/ids

npm i https://pkg.pr.new/@prisma-next/ids@731

@prisma-next/psl-parser

npm i https://pkg.pr.new/@prisma-next/psl-parser@731

@prisma-next/psl-printer

npm i https://pkg.pr.new/@prisma-next/psl-printer@731

@prisma-next/cli

npm i https://pkg.pr.new/@prisma-next/cli@731

@prisma-next/cli-telemetry

npm i https://pkg.pr.new/@prisma-next/cli-telemetry@731

@prisma-next/emitter

npm i https://pkg.pr.new/@prisma-next/emitter@731

@prisma-next/migration-tools

npm i https://pkg.pr.new/@prisma-next/migration-tools@731

prisma-next

npm i https://pkg.pr.new/prisma-next@731

@prisma-next/vite-plugin-contract-emit

npm i https://pkg.pr.new/@prisma-next/vite-plugin-contract-emit@731

@prisma-next/mongo-codec

npm i https://pkg.pr.new/@prisma-next/mongo-codec@731

@prisma-next/mongo-contract

npm i https://pkg.pr.new/@prisma-next/mongo-contract@731

@prisma-next/mongo-value

npm i https://pkg.pr.new/@prisma-next/mongo-value@731

@prisma-next/mongo-contract-psl

npm i https://pkg.pr.new/@prisma-next/mongo-contract-psl@731

@prisma-next/mongo-contract-ts

npm i https://pkg.pr.new/@prisma-next/mongo-contract-ts@731

@prisma-next/mongo-emitter

npm i https://pkg.pr.new/@prisma-next/mongo-emitter@731

@prisma-next/mongo-schema-ir

npm i https://pkg.pr.new/@prisma-next/mongo-schema-ir@731

@prisma-next/mongo-query-ast

npm i https://pkg.pr.new/@prisma-next/mongo-query-ast@731

@prisma-next/mongo-orm

npm i https://pkg.pr.new/@prisma-next/mongo-orm@731

@prisma-next/mongo-query-builder

npm i https://pkg.pr.new/@prisma-next/mongo-query-builder@731

@prisma-next/mongo-lowering

npm i https://pkg.pr.new/@prisma-next/mongo-lowering@731

@prisma-next/mongo-wire

npm i https://pkg.pr.new/@prisma-next/mongo-wire@731

@prisma-next/sql-contract

npm i https://pkg.pr.new/@prisma-next/sql-contract@731

@prisma-next/sql-errors

npm i https://pkg.pr.new/@prisma-next/sql-errors@731

@prisma-next/sql-operations

npm i https://pkg.pr.new/@prisma-next/sql-operations@731

@prisma-next/sql-schema-ir

npm i https://pkg.pr.new/@prisma-next/sql-schema-ir@731

@prisma-next/sql-contract-psl

npm i https://pkg.pr.new/@prisma-next/sql-contract-psl@731

@prisma-next/sql-contract-ts

npm i https://pkg.pr.new/@prisma-next/sql-contract-ts@731

@prisma-next/sql-contract-emitter

npm i https://pkg.pr.new/@prisma-next/sql-contract-emitter@731

@prisma-next/sql-lane-query-builder

npm i https://pkg.pr.new/@prisma-next/sql-lane-query-builder@731

@prisma-next/sql-relational-core

npm i https://pkg.pr.new/@prisma-next/sql-relational-core@731

@prisma-next/sql-builder

npm i https://pkg.pr.new/@prisma-next/sql-builder@731

@prisma-next/target-postgres

npm i https://pkg.pr.new/@prisma-next/target-postgres@731

@prisma-next/target-sqlite

npm i https://pkg.pr.new/@prisma-next/target-sqlite@731

@prisma-next/adapter-postgres

npm i https://pkg.pr.new/@prisma-next/adapter-postgres@731

@prisma-next/adapter-sqlite

npm i https://pkg.pr.new/@prisma-next/adapter-sqlite@731

@prisma-next/driver-postgres

npm i https://pkg.pr.new/@prisma-next/driver-postgres@731

@prisma-next/driver-sqlite

npm i https://pkg.pr.new/@prisma-next/driver-sqlite@731

commit: d6707c5

@github-actions

github-actions Bot commented Jun 5, 2026 •

Copy link
Copy Markdown

size-limit report 📦

Path Size
postgres / no-emit 144.8 KB (-0.08% 🔽)
postgres / emit 116.86 KB (0%)
mongo / no-emit 76.02 KB (0%)
mongo / emit 70.89 KB (0%)
cf-worker / no-emit 174.61 KB (-0.04% 🔽)
cf-worker / emit 143.48 KB (0%)

wmadden and others added 2 commits June 5, 2026 09:38
…ion wrappers via a shared bound builder

Port of the mongo fix (TML-2817): add a family/target-bound buildBoundContract
to the SQL contract-builder that the base defineContract and the sqlite +
postgres extension wrappers all delegate to, so both wrappers carry zero casts.

Signed-off-by: Will Madden <[email protected]>
The check:upgrade-coverage gate requires that every PR touching
packages/3-extensions/ also modifies instructions.md in the in-flight
upgrade directory and declares changes: [] (no consumer action) or a
non-empty entry. This PR's changes to the postgres and sqlite
defineContract wrappers are a pure internal refactor with no
consumer-visible API change, so changes: [] is correct.

Signed-off-by: Will Madden <[email protected]>
Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@wmadden-electric
wmadden-electric force-pushed the tml-2837-sql-definecontract-cast-pattern branch from 5c0dd04 to f5e2867 Compare June 5, 2026 07:38
…s in SQL buildBoundContract

Spread only the factory's declared outputs (types/models) via ifDefined instead
of the whole factory return, so a factory cannot clobber the pre-bound
family/target/extensionPacks. Addresses the CodeRabbit finding from the sibling
mongo PR (#730); same pattern existed here.

Signed-off-by: Will Madden <[email protected]>
@wmadden-electric

Copy link
Copy Markdown
Contributor Author

Note: while addressing the CodeRabbit finding from the sibling mongo PR (#730), I applied the same hardening here in 55e17b178 — buildBoundContract now spreads only the factory's declared outputs (types/models) via ifDefined, so factory output cannot clobber the pre-bound family/target/extensionPacks. (CodeRabbit did not flag it inline here, but the pattern was identical.) Tracked in TML-2839.

@wmadden-electric
wmadden-electric enabled auto-merge (squash) June 5, 2026 08:00
@wmadden-electric
wmadden-electric merged commit fa181e3 into main Jun 5, 2026
20 checks passed
@wmadden-electric
wmadden-electric deleted the tml-2837-sql-definecontract-cast-pattern branch June 5, 2026 08:23
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants