feat: provide the client in the computed field context - #2789
feat: provide the client in the computed field context#2789evgenovalov wants to merge 2 commits into
Conversation
Computed field implementations receive (eb, { modelAlias }, args) and
cannot read per-client state such as the auth context set via $setAuth.
Pass the executing client in the context, mirroring what custom function
implementations already get through ZModelFunctionContext.
Also replaces the scattered 'as unknown as ClientContract' casts with a
single ClientImpl.$contract accessor.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe ORM exposes a typed ChangesClient-aware computed fields
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
…t test A bare binary comparison embedded by the boolean where-filter renders as a chained '=' — a syntax error on postgres (sqlite tolerates it). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
CI fix in 771e216: the new e2e test failed on the postgres matrix only. The computed field was written as a bare binary comparison ( Side note for maintainers: this is reproducible on dev with any boolean computed field written as a bare comparison plus a boolean where filter — the dialect could parenthesize computed expressions when embedding them. Happy to file it as a separate issue. |
Motivation
Computed field implementations receive
(eb, { modelAlias }, args). There is no way to read per-client state — most notably the auth context set with$setAuth. A field like "is this row mine" currently needs a runtime plugin to inject the user id into the query. Custom function implementations already get the client throughZModelFunctionContext; this PR gives computed fields the same access.Example
Changes
ComputedFieldContext<Schema>type ({ modelAlias, client? });ComputedFieldsOptionstypes the context parameter with it. Generated schemas are untouched, so existing implementations keep compiling.BaseCrudDialect/getCrudDialecttake an optionalclient, threaded from the CRUD operations, the query executor, and the name mapper.fieldRefpasses it into the context.clientis optional in the context because a dialect can be constructed without one (e.g.ResultProcessor); every query issued through the client API has it.as unknown as ClientContract<SchemaDef>casts inclient-impl.ts/zenstack-query-executor.tswith a singleClientImpl.$contractaccessor — same object at runtime, one assertion at one documented boundary.$setAuth; the base client stays unaffected.No breaking changes: the new parameters are optional and the context object only gains a property.
🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
$contractaccessor for safer client interactions.Bug Fixes
Tests