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

TML-2817: eliminate the defineContract cast pattern in @prisma-next/extension-mongo via a shared bound builder - #730

Merged
wmadden merged 2 commits into
mainfrom
tml-2817-cleanup-replace-bare-as-unknown-as-casts-in-prisma
Jun 5, 2026
Merged

wmadden merged 2 commits into
mainfrom
tml-2817-cleanup-replace-bare-as-unknown-as-casts-in-prisma

Conversation

@wmadden-electric

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

Copy link
Copy Markdown
Contributor

What & why

TML-2817 was filed as "swap two bare as unknown as casts for blindCast." On review, packages/3-extensions/mongo/src/contract/define-contract.ts actually carried three structural casts — a construction cast, a ContractScaffold cast, and an as unknown as factory cast — all symptoms of one thing: the wrapper pre-binds family/target and delegates from a non-generic implementation body to the overloaded, generic base defineContract, so the factory param collapses to a Parameters<…> union and needs casts to bridge it.

Rather than paper those over with blindCast (lipstick on a structural gap), this eliminates the pattern: a shared, family/target-bound buildBoundContract lives in the mongo contract-builder, and both the base defineContract and the extension wrapper delegate to it. The extension wrapper now carries zero casts.

Result

  • packages/3-extensions/mongo/src/contract/define-contract.ts: 3 → 0 bare casts.
  • buildBoundContract (new, in the base contract-builder.ts): one shared, plainly-overloaded function — no overload-return cast, and the helpers-composition step is de-duplicated here (previously duplicated in the base defineContract body).
  • Net −3 casts.

The one residual cast (irreducible, documented)

buildBoundContract keeps a single as unknown as ContractAuthoringHelpers<…> where it assembles the authoring helpers. This is genuinely irreducible: composeMongoEntityHelpers → createEntityHelpersFromNamespace returns Record<string, unknown> from a runtime walk over an opaque namespace, while ContractAuthoringHelpers is a mapped type reconstructed from the packs' static entityTypes declarations — there is no structural path from one to the other. The SQL sibling does the identical thing (packages/2-sql/2-authoring/contract-ts/src/composed-authoring-helpers.ts), so this is the established, accepted boundary cast — now in exactly one place rather than two.

Scope

Mongo only. The sqlite/postgres extension wrappers carry the identical cast pattern; porting buildBoundContract to the SQL base + those wrappers is a follow-up (same shape, verified here first).

Verification

@prisma-next/mongo-contract-ts and @prisma-next/mongo typecheck clean; all tests pass, including the negative-typing/result-type tests in define-contract.test-d.ts (family/target still rejected; definition-form and factory-form result types intact). Pure type-level + structural refactor — no runtime behaviour change (factory-merge order and the isContractScaffold guard preserved). Independently reviewed.

Refs: TML-2817

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Exposed a helper to compose pre-bound Mongo contracts for use when authoring contracts.
  • Refactor

    • Simplified contract-definition runtime paths by routing both factory and non-factory cases through the new helper.
    • Improved factory typings and removed redundant casting so return types flow more naturally.

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

coderabbitai Bot commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@wmadden-electric, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 25 minutes and 34 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, 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 include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 146466a6-aef6-4318-b2ae-43b1b37e9139

📥 Commits

Reviewing files that changed from the base of the PR and between 5e366dd and 9e61482.

📒 Files selected for processing (3)
  • packages/2-mongo-family/2-authoring/contract-ts/src/contract-builder.ts
  • packages/2-mongo-family/2-authoring/contract-ts/src/exports/contract-builder.ts
  • packages/3-extensions/mongo/src/contract/define-contract.ts
📝 Walkthrough

Walkthrough

Adds an exported buildBoundContract helper, updates defineContract to delegate to it for both factory and non-factory paths, tweaks a factory overload to allow optional roots, and re-exports buildBoundContract from the contract-builder exports.

Changes

Mongo contract builder refactor

Layer / File(s) Summary
Add buildBoundContract helper
packages/2-mongo-family/2-authoring/contract-ts/src/contract-builder.ts
Introduces buildBoundContract with overloads and an implementation that optionally composes ContractAuthoringHelpers and delegates to buildContractFromDefinition.
Route defineContract to buildBoundContract
packages/3-extensions/mongo/src/contract/define-contract.ts, packages/2-mongo-family/2-authoring/contract-ts/src/contract-builder.ts
Swap import to buildBoundContract, rewrite defineContract runtime to call buildBoundContract in factory and non-factory branches, and adjust the factory-form Built overload so roots may be undefined.
Re-export buildBoundContract
packages/2-mongo-family/2-authoring/contract-ts/src/exports/contract-builder.ts
Add buildBoundContract to the named exports from ../contract-builder.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐰 I bound the contract, neat and fleet,
families and targets now meet,
factories funnel through one door,
exports tidy, types restore,
a little hop — refactor complete.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Title check ⚠️ Warning The PR title describes refactoring to eliminate bare casts using blindCast, but the actual changes show a refactoring to use a new shared buildBoundContract utility to eliminate casts entirely via architectural change, not via blindCast replacement. Update the PR title to reflect the actual implementation: that defineContract now delegates to buildBoundContract to eliminate bare casts, rather than replacing them with blindCast.
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
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 unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch tml-2817-cleanup-replace-bare-as-unknown-as-casts-in-prisma

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.

@github-actions

github-actions Bot commented Jun 5, 2026 •

Copy link
Copy Markdown

size-limit report 📦

Path Size
postgres / no-emit 145.06 KB (0%)
postgres / emit 116.91 KB (0%)
mongo / no-emit 76.02 KB (-0.08% 🔽)
mongo / emit 70.89 KB (0%)
cf-worker / no-emit 174.82 KB (0%)
cf-worker / emit 143.56 KB (0%)

@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@730

@prisma-next/mongo-runtime

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

@prisma-next/family-mongo

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

@prisma-next/sql-runtime

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

@prisma-next/family-sql

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

@prisma-next/extension-arktype-json

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

@prisma-next/middleware-cache

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

@prisma-next/mongo

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

@prisma-next/extension-paradedb

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

@prisma-next/extension-pgvector

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

@prisma-next/extension-postgis

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

@prisma-next/postgres

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

@prisma-next/sql-orm-client

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

@prisma-next/sqlite

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

@prisma-next/target-mongo

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

@prisma-next/adapter-mongo

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

@prisma-next/driver-mongo

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

@prisma-next/contract

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

@prisma-next/utils

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

@prisma-next/config

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

@prisma-next/errors

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

@prisma-next/framework-components

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

@prisma-next/operations

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

@prisma-next/ts-render

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

@prisma-next/contract-authoring

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

@prisma-next/ids

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

@prisma-next/psl-parser

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

@prisma-next/psl-printer

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

@prisma-next/cli

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

@prisma-next/cli-telemetry

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

@prisma-next/emitter

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

@prisma-next/migration-tools

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

prisma-next

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

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

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

@prisma-next/mongo-codec

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

@prisma-next/mongo-contract

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

@prisma-next/mongo-value

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

@prisma-next/mongo-contract-psl

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

@prisma-next/mongo-contract-ts

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

@prisma-next/mongo-emitter

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

@prisma-next/mongo-schema-ir

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

@prisma-next/mongo-query-ast

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

@prisma-next/mongo-orm

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

@prisma-next/mongo-query-builder

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

@prisma-next/mongo-lowering

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

@prisma-next/mongo-wire

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

@prisma-next/sql-contract

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

@prisma-next/sql-errors

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

@prisma-next/sql-operations

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

@prisma-next/sql-schema-ir

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

@prisma-next/sql-contract-psl

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

@prisma-next/sql-contract-ts

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

@prisma-next/sql-contract-emitter

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

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

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

@prisma-next/sql-relational-core

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

@prisma-next/sql-builder

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

@prisma-next/target-postgres

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

@prisma-next/target-sqlite

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

@prisma-next/adapter-postgres

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

@prisma-next/adapter-sqlite

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

@prisma-next/driver-postgres

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

@prisma-next/driver-sqlite

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

commit: 9e61482

@wmadden-electric
wmadden-electric force-pushed the tml-2817-cleanup-replace-bare-as-unknown-as-casts-in-prisma branch 2 times, most recently from 3c9b7b1 to 5e366dd Compare June 5, 2026 04:32
@wmadden-electric wmadden-electric changed the title TML-2817: replace bare as-unknown-as casts in @prisma-next/extension-mongo with blindCast TML-2817: eliminate the defineContract cast pattern in @prisma-next/extension-mongo via a shared bound builder Jun 5, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/2-mongo-family/2-authoring/contract-ts/src/contract-builder.ts`:
- Around line 1711-1724: The factory output is currently spread after full (in
buildContractFromDefinition({ ...full, ...factory(helpers) })) allowing callers
to override bound scaffold keys like
family/target/extensionPacks/defaultControlPolicy; instead call factory(helpers)
into a local variable and selectively spread only the optional scaffold pieces
that are safe to accept (models, valueObjects, roots) using the ifDefined(key,
value) helper so the bound mongo packs (family, target, extensionPacks,
defaultControlPolicy, etc.) remain unmodified; update the return to pass
{...full, ...ifDefined('models', factoryOut.models),
...ifDefined('valueObjects', factoryOut.valueObjects), ...ifDefined('roots',
factoryOut.roots)} into buildContractFromDefinition, referencing
composeMongoEntityHelpers, ContractAuthoringHelpers and
buildContractFromDefinition to locate the change.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 45ab4d6f-e670-42e6-8deb-7e219571e416

📥 Commits

Reviewing files that changed from the base of the PR and between de8219e and 3c9b7b1.

📒 Files selected for processing (3)
  • packages/2-mongo-family/2-authoring/contract-ts/src/contract-builder.ts
  • packages/2-mongo-family/2-authoring/contract-ts/src/exports/contract-builder.ts
  • packages/3-extensions/mongo/src/contract/define-contract.ts

wmadden added 2 commits June 5, 2026 08:58
…sion-mongo via shared bound contract builder

Introduce a single family/target-bound contract builder in the mongo
contract-builder that both defineContract and extension wrappers delegate to,
so the extension's define-contract wrapper carries zero casts. The shared
builder removes the duplicated helpers-composition cast and avoids the
overloaded-function-return cast entirely.

Signed-off-by: Will Madden <[email protected]>
… change

TML-2817 touched packages/3-extensions/mongo/src/contract/define-contract.ts
as part of a cast elimination refactor. No extension API or behaviour change;
changes: [] records the incidental diff.

Signed-off-by: Will Madden <[email protected]>
@wmadden-electric
wmadden-electric force-pushed the tml-2817-cleanup-replace-bare-as-unknown-as-casts-in-prisma branch from 5e366dd to 9e61482 Compare June 5, 2026 07:02
@wmadden
wmadden merged commit 1b0cdc9 into main Jun 5, 2026
20 checks passed
@wmadden
wmadden deleted the tml-2817-cleanup-replace-bare-as-unknown-as-casts-in-prisma branch June 5, 2026 07:47
wmadden-electric pushed a commit that referenced this pull request Jun 5, 2026
…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 added a commit that referenced this pull request Jun 5, 2026
…s in mongo buildBoundContract

Spread only the factory's declared outputs (models/valueObjects/roots) via
ifDefined instead of the whole factory return, so a factory cannot clobber the
pre-bound family/target/extensionPacks. Follow-up to the CodeRabbit finding on
#730 (TML-2817), which merged before it was addressed.

Signed-off-by: Will Madden <[email protected]>
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