Skip to content

docs: document the client in the computed field context - #634

Open
evgenovalov wants to merge 1 commit into
zenstackhq:mainfrom
evgenovalov:docs/computed-field-client-context
Open

docs: document the client in the computed field context#634
evgenovalov wants to merge 1 commit into
zenstackhq:mainfrom
evgenovalov:docs/computed-field-client-context

Conversation

@evgenovalov

@evgenovalov evgenovalov commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Documents the client property of the computed field context, added in v3.9.1 by zenstackhq/zenstack#2789.

Changes

docs/orm/computed-fields.md:

  • New Accessing the Client section (<AvailableSince version="v3.9.1" />) — what context.client is, the isMine example reading client.$auth, and how a $setAuth()-derived client evaluates the field against its own identity while the original stays anonymous. Links to Setting Auth User for the per-request client pattern.
  • client added to both callback signature blocks (ComputedFieldCallback and ParameterizedComputedFieldCallback).
  • Two callouts: the implementation returns its expression synchronously, so client is for reading per-client state rather than awaiting queries; and a note on why the example uses a sql<boolean> fragment (see below).

On the sql<boolean> fragment

The natural way to write isMine is eb('authorId', '=', id), but that doesn't typecheck against a Boolean @computed field. The generated stub returns boolean, so the callback must return OperandExpression<boolean>, while a Kysely comparison is SqlBool (boolean | 0 | 1):

Type 'ExpressionWrapper<ToKyselySchema<SchemaType>, "Post", SqlBool>' is not assignable
  to type 'OperandExpression<boolean>'.

So the example uses sql<boolean> and the tip lists the alternatives that also type correctly (eb.lit(), eb.case()). Happy to switch to whichever form you consider idiomatic — or drop the tip if SqlBool is something you'd rather accept in OperandExpression on the ORM side.

Verification

  • Documented implementation runs green against a real client on sqlite and postgresqlwhere: { isMine: true }, findUnique on both an authed and an anonymous client.
  • Type-checked against a generated schema with a Boolean @computed field: eb.lit() / sql<boolean> / eb.case() pass, and client.$auth?.id narrows to number | undefined.
  • npm run build passes (onBrokenLinks: 'throw'), so the anchor resolves.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Documentation
    • Documented access to per-client state in computed field callbacks.
    • Added an example of identity-dependent computed fields.
    • Clarified $setAuth() behavior, synchronous expression requirements, and boolean typing.
    • Updated parameterized computed field callback documentation to include client context.

Available since v3.9.1 (zenstackhq/zenstack#2789).

Adds an "Accessing the Client" section covering the `client` property of the
computed field context, and includes it in both callback signature blocks.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@vercel

vercel Bot commented Aug 14, 2026

Copy link
Copy Markdown

Someone is attempting to deploy a commit to the ZenStack Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Aug 14, 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: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 5e3ed05e-f19c-40fd-bb3a-c1898b45a5c3

📥 Commits

Reviewing files that changed from the base of the PR and between edd57a2 and ec69df1.

📒 Files selected for processing (1)
  • docs/orm/computed-fields.md

Walkthrough

Computed field callback contexts now include ClientContract<Schema>. The documentation covers $auth, $setAuth(), synchronous evaluation, boolean typing, and parameterized callbacks.

Changes

Client-aware computed fields

Layer / File(s) Summary
Computed field callback context
docs/orm/computed-fields.md
Callback signatures now include client: ClientContract<Schema>. The documentation describes $auth-based fields, $setAuth() behavior, synchronous expressions, boolean typing, and parameterized callback arguments.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Merge Risk: ⚪ Minimal · up to ec69d

This is a localized documentation-only change with no actionable merge-blocking risk remaining after normal checks and review.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 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 documentation change: adding the client to the computed field context.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

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