Skip to content

Conversation

@ymc9
Copy link
Member

@ymc9 ymc9 commented Nov 6, 2025

Summary by CodeRabbit

  • New Features

    • Optimistic update support added to TanStack Query clients (React, Svelte, Vue), surfacing an optional $optimistic flag on find query results.
  • Schema

    • Relations can now indicate/default when referenced foreign-key fields have defaults, improving default detection for relations.
  • Tests

    • Added typing tests and end-to-end tests validating optimistic fields and default-auth relation behavior.
  • Chores

    • Bumped package versions to 3.0.0-beta.19 across the monorepo.

ymc9 and others added 3 commits November 5, 2025 18:14
* fix(tanstack): query result $optimistic typing

* update
Co-authored-by: ymc9 <104139426+ymc9@users.noreply.github.com>
Copilot AI review requested due to automatic review settings November 6, 2025 05:50
@coderabbitai
Copy link

coderabbitai bot commented Nov 6, 2025

Walkthrough

This PR bumps many package versions from 3.0.0-beta.18 to 3.0.0-beta.19, adds optimistic-update typing support to the TanStack Query client, and enhances schema/relation metadata to mark relations whose foreign-key fields all have defaults.

Changes

Cohort / File(s) Summary
Version Bumps
package.json, packages/cli/package.json, packages/common-helpers/package.json, packages/config/*/package.json, packages/create-zenstack/package.json, packages/dialects/sql.js/package.json, packages/language/package.json, packages/orm/package.json, packages/plugins/policy/package.json, packages/schema/package.json, packages/sdk/package.json, packages/server/package.json, packages/testtools/package.json, packages/zod/package.json, samples/*/package.json, tests/*/package.json
Updated package versions from 3.0.0-beta.18 to 3.0.0-beta.19.
TanStack Query: optimistic typing
packages/clients/tanstack-query/src/utils/types.ts
Added WithOptimistic<T> and internal helper WithOptimisticFlag<T> to annotate data with an optional $optimistic?: boolean (applies to arrays and non-arrays).
TanStack Query: framework typings
packages/clients/tanstack-query/src/react.ts, packages/clients/tanstack-query/src/svelte.ts, packages/clients/tanstack-query/src/vue.ts
Updated ModelQueryResult / ModelSuspenseQueryResult types to use WithOptimistic<T> so returned query data may include $optimistic. Added WithOptimistic imports.
TanStack Query: typing tests
packages/clients/tanstack-query/test/react-typing-test.ts, packages/clients/tanstack-query/test/svelte-typing-test.ts, packages/clients/tanstack-query/test/vue-typing-test.ts
Added/adjusted type assertions to expect optional $optimistic on useFindFirst and useFindMany results and added ts-expect-error for certain useInfiniteFindMany optimistic accesses.
Schema: relation default metadata
packages/schema/src/schema.ts
Extended RelationInfo with optional hasDefault?: boolean; updated FieldHasDefault type logic to treat fields as having defaults when their relation has hasDefault: true.
TS schema generator: FK default detection
packages/sdk/src/ts-schema-generator.ts
Added logic to collect relation fields (fkFields) and, if all referenced FK fields have @default, emit hasDefault: true on the generated relation metadata.
Default-auth E2E schema & types
tests/e2e/orm/schemas/default-auth/schema.zmodel, tests/e2e/orm/schemas/default-auth/schema.ts, tests/e2e/orm/schemas/default-auth/input.ts, tests/e2e/orm/schemas/default-auth/models.ts
Added a new sample schema (User, Profile, Address), plus generated TypeScript model/result/args type aliases for tests.
Default-auth E2E test
tests/e2e/orm/client-api/default-auth.test.ts
New end-to-end test verifying default-auth behavior: authenticated context associates created profile records with the expected user ID.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Changes are a mix of many repetitive version bumps and several focused type/logic edits.
  • Review attention recommended for:
    • packages/clients/tanstack-query/src/utils/types.ts — correctness of recursive WithOptimistic<T> mapping for arrays and object types.
    • packages/clients/tanstack-query/*.(ts|svelte|vue) — consistency of updated query result types across frameworks and tests.
    • packages/schema/src/schema.ts & packages/sdk/src/ts-schema-generator.ts — ensure hasDefault is emitted and consumed only in intended cases and that FK detection correctly handles multiple FK fields.

Possibly related PRs

Poem

🐰 I nibbled at types through the night,

added $optimistic, set defaults just right,
bumped versions in a tidy line,
beta-18 to beta-19 — a small hop, fine! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main objective of the pull request: merging the dev branch to main with a version bump to v3.0.0-beta.19, which aligns with the extensive version updates across all package.json files throughout the changeset.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch dev

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 56acc1c and 2946115.

📒 Files selected for processing (1)
  • packages/clients/tanstack-query/src/utils/types.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: build-test (20.x, sqlite)
  • GitHub Check: build-test (20.x, postgresql)
  • GitHub Check: claude-review
🔇 Additional comments (1)
packages/clients/tanstack-query/src/utils/types.ts (1)

34-43: LGTM! Previous critical issue has been resolved.

The implementation correctly addresses the previous review's concern about scalar types collapsing to never. By using the conditional check T extends object, the type now:

  • Preserves primitives unchanged (e.g., numbernumber)
  • Augments objects with the $optimistic flag (e.g., {id: string}{id: string} & {$optimistic?: boolean})
  • Handles arrays appropriately by applying the flag only to object elements

This ensures consumers of scalar queries (like useCount returning number) will continue to work correctly.


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.

@claude
Copy link

claude bot commented Nov 6, 2025

Claude finished @ymc9's task —— View job


PR Review: v3.0.0-beta.19 Release

Reviewing this PR that merges dev to main for the v3.0.0-beta.19 release.

Tasks:

  • Analyze version bumps and package changes
  • Review TanStack query typing improvements
  • Examine new test schema additions
  • Check for potential bugs or issues
  • Assess security and performance considerations
  • Evaluate test coverage

Review Summary

This PR contains three main categories of changes for the v3.0.0-beta.19 release:

Version Bumps (35 files)

All package.json files have been consistently updated to version 3.0.0-beta.19. The version bump appears clean and follows the expected beta release pattern.

TanStack Query Typing Improvements

Location: packages/clients/tanstack-query/src/utils/types.ts:34-49, packages/clients/tanstack-query/src/react.ts:96,101

  • New Feature: Added WithOptimistic<T> type that adds an optional $optimistic?: boolean field to query results
  • Integration: Updated query result types to wrap returned data with WithOptimistic<T>
  • Purpose: Enables tracking of optimistic update states in TanStack Query integrations
  • Quality: Well-documented with TSDoc comments and properly handles both array and object types

Schema Generation Enhancement for Default Auth Fields

Location: packages/sdk/src/ts-schema-generator.ts:708-748, packages/schema/src/schema.ts:55,257-260

  • Fix: Relation fields with foreign keys that all have default values now get a hasDefault: true flag
  • Purpose: Enables the type system to recognize when relation fields can be omitted during creation
  • Implementation: Validates all FK fields have @default attributes before setting the flag
  • Type Integration: Updated FieldHasDefault type to check relation.hasDefault property

Comprehensive Test Coverage for Default Auth

Location: tests/e2e/orm/schemas/default-auth/ + tests/e2e/orm/client-api/default-auth.test.ts

  • Schema: Tests foreign key with @default(auth().id) in Profile model
  • Validation: Ensures Profile can be created without explicitly providing userId
  • Edge Cases: Tests both simple creation and creation with relation connections

Code Quality Assessment

Security: No security concerns identified

  • No exposure of sensitive data
  • Auth-related changes are for typing/testing, not auth implementation
  • Test schemas use appropriate default values

Performance: No performance impacts

  • Type-only changes with no runtime overhead
  • Schema generation improvements are compile-time only
  • New test suite is lightweight

Best Practices:

  • Consistent code formatting and patterns
  • Proper TypeScript typing throughout
  • Good test coverage for new functionality
  • Clear documentation with TSDoc comments

🔍 Minor Observations:

  1. Test Spacing: tests/e2e/orm/client-api/default-auth.test.ts:13 - Small typo in 'Seattle ' (extra space)
  2. Type Complexity: The TrimDelegateModelOperations type is quite complex but appears necessary for the delegate model feature

Recommendation: ✅ APPROVE

This is a solid beta release with:

  • Clean version management across all packages
  • Thoughtful typing improvements for optimistic updates
  • Important bug fix for default auth field handling
  • Comprehensive test coverage for new functionality
  • No breaking changes or security concerns

The changes are well-implemented, properly tested, and ready for release.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR bumps the version from 3.0.0-beta.18 to 3.0.0-beta.19 and adds support for detecting relations with foreign key fields that have default values, particularly for the use case where auth() is used as a default value. The key changes enable relations to be created without explicitly providing foreign key values when those fields have defaults.

  • Version bump across all packages and samples
  • Added logic to detect when all foreign key fields in a relation have default values and mark the relation with hasDefault: true
  • Updated the FieldHasDefault type to consider relations with default foreign keys
  • Added $optimistic property to TanStack Query result types for optimistic update support

Reviewed Changes

Copilot reviewed 35 out of 35 changed files in this pull request and generated no comments.

Show a summary per file
File Description
package.json, packages//package.json, tests//package.json, samples/*/package.json Version bump to 3.0.0-beta.19
packages/sdk/src/ts-schema-generator.ts Added logic to detect and mark relations where all FK fields have default values
packages/schema/src/schema.ts Extended RelationInfo type with hasDefault property and updated FieldHasDefault type logic
packages/clients/tanstack-query/src/utils/types.ts Added WithOptimistic type for marking items in optimistic update state
packages/clients/tanstack-query/src/{react,svelte,vue}.ts Updated query result types to include WithOptimistic wrapper
packages/clients/tanstack-query/test/*-typing-test.ts Added type tests for $optimistic property
tests/e2e/orm/schemas/default-auth/* New test schema demonstrating auth() as default value
tests/e2e/orm/client-api/default-auth.test.ts Test coverage for creating relations without providing defaulted FK fields

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bd75719 and 56acc1c.

📒 Files selected for processing (35)
  • package.json (1 hunks)
  • packages/cli/package.json (1 hunks)
  • packages/clients/tanstack-query/package.json (1 hunks)
  • packages/clients/tanstack-query/src/react.ts (2 hunks)
  • packages/clients/tanstack-query/src/svelte.ts (2 hunks)
  • packages/clients/tanstack-query/src/utils/types.ts (1 hunks)
  • packages/clients/tanstack-query/src/vue.ts (2 hunks)
  • packages/clients/tanstack-query/test/react-typing-test.ts (2 hunks)
  • packages/clients/tanstack-query/test/svelte-typing-test.ts (2 hunks)
  • packages/clients/tanstack-query/test/vue-typing-test.ts (2 hunks)
  • packages/common-helpers/package.json (1 hunks)
  • packages/config/eslint-config/package.json (1 hunks)
  • packages/config/typescript-config/package.json (1 hunks)
  • packages/config/vitest-config/package.json (1 hunks)
  • packages/create-zenstack/package.json (1 hunks)
  • packages/dialects/sql.js/package.json (1 hunks)
  • packages/language/package.json (1 hunks)
  • packages/orm/package.json (1 hunks)
  • packages/plugins/policy/package.json (1 hunks)
  • packages/schema/package.json (1 hunks)
  • packages/schema/src/schema.ts (2 hunks)
  • packages/sdk/package.json (1 hunks)
  • packages/sdk/src/ts-schema-generator.ts (2 hunks)
  • packages/server/package.json (1 hunks)
  • packages/testtools/package.json (1 hunks)
  • packages/zod/package.json (1 hunks)
  • samples/next.js/package.json (1 hunks)
  • samples/orm/package.json (1 hunks)
  • tests/e2e/orm/client-api/default-auth.test.ts (1 hunks)
  • tests/e2e/orm/schemas/default-auth/input.ts (1 hunks)
  • tests/e2e/orm/schemas/default-auth/models.ts (1 hunks)
  • tests/e2e/orm/schemas/default-auth/schema.ts (1 hunks)
  • tests/e2e/orm/schemas/default-auth/schema.zmodel (1 hunks)
  • tests/e2e/package.json (1 hunks)
  • tests/regression/package.json (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-10-21T16:09:31.218Z
Learnt from: ymc9
Repo: zenstackhq/zenstack-v3 PR: 319
File: packages/runtime/src/client/executor/zenstack-query-executor.ts:63-72
Timestamp: 2025-10-21T16:09:31.218Z
Learning: In ZenStack, TypeDefs can be inherited by models. When a TypeDef contains fields with `map` attributes, those mapped field names need to be processed by the QueryNameMapper since they become part of the inheriting model's schema. Therefore, when checking if a schema has mapped names (e.g., in `schemaHasMappedNames`), both `schema.models` and `schema.typeDefs` must be inspected for `@map` and `map` attributes.

Applied to files:

  • tests/e2e/orm/schemas/default-auth/schema.ts
🧬 Code graph analysis (6)
packages/clients/tanstack-query/src/svelte.ts (3)
packages/clients/tanstack-query/src/react.ts (1)
  • ModelQueryResult (96-96)
packages/clients/tanstack-query/src/vue.ts (1)
  • ModelQueryResult (89-89)
packages/clients/tanstack-query/src/utils/types.ts (1)
  • WithOptimistic (34-49)
packages/sdk/src/ts-schema-generator.ts (1)
packages/sdk/src/model-utils.ts (1)
  • hasAttribute (59-64)
packages/clients/tanstack-query/src/react.ts (4)
packages/clients/tanstack-query/src/svelte.ts (1)
  • ModelQueryResult (94-96)
packages/clients/tanstack-query/src/vue.ts (1)
  • ModelQueryResult (89-89)
packages/clients/tanstack-query/src/utils/types.ts (1)
  • WithOptimistic (34-49)
packages/clients/tanstack-query/src/utils/common.ts (1)
  • ExtraQueryOptions (101-106)
tests/e2e/orm/schemas/default-auth/schema.ts (2)
packages/schema/src/expression-utils.ts (1)
  • ExpressionUtils (19-123)
packages/schema/src/schema.ts (1)
  • SchemaDef (10-18)
packages/clients/tanstack-query/src/vue.ts (3)
packages/clients/tanstack-query/src/react.ts (1)
  • ModelQueryResult (96-96)
packages/clients/tanstack-query/src/svelte.ts (1)
  • ModelQueryResult (94-96)
packages/clients/tanstack-query/src/utils/types.ts (1)
  • WithOptimistic (34-49)
tests/e2e/orm/client-api/default-auth.test.ts (2)
packages/testtools/src/client.ts (1)
  • createTestClient (52-169)
tests/e2e/orm/schemas/default-auth/schema.ts (1)
  • schema (9-121)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: build-test (20.x, postgresql)
  • GitHub Check: build-test (20.x, sqlite)
  • GitHub Check: claude-review
🔇 Additional comments (27)
tests/e2e/package.json (1)

1-35: Version bump consistent with root package and monorepo release.

packages/config/typescript-config/package.json (1)

1-6: Version bump aligns with release series.

packages/plugins/policy/package.json (1)

1-54: Version bump consistent across the monorepo.

tests/regression/package.json (1)

1-22: Version bump aligns with monorepo release.

packages/dialects/sql.js/package.json (1)

1-46: Version bump correct and consistent with other packages.

samples/orm/package.json (1)

1-29: Version bump aligns with release.

package.json (1)

1-42: Root package version update is correct.

The root version bump to 3.0.0-beta.19 is the source of truth for workspace dependencies, making this change critical to the coordinated version update across the monorepo.

packages/orm/package.json (1)

1-101: Inconsistency detected: VSCode package version diverges from coordinated release.

Verification shows 21 of 22 packages correctly updated to 3.0.0-beta.19, but packages/ide/vscode/package.json remains at 3.0.11. Confirm whether this separate versioning is intentional or requires alignment with the coordinated release.

packages/server/package.json (1)

3-3: Version bump to beta.19 is consistent with PR release objectives.

The version metadata update aligns with the coordinated monorepo release.

samples/next.js/package.json (1)

3-3: Version bump to beta.19 maintained for consistency.

Sample package version aligns with the release version.

packages/zod/package.json (1)

3-3: Version bump to beta.19 is appropriate.

Version metadata updated consistently.

packages/testtools/package.json (1)

3-3: Version bump to beta.19 maintained consistently.

Version metadata aligns with release.

packages/config/vitest-config/package.json (1)

4-4: Version bump to beta.19 is consistent.

Version metadata updated as part of coordinated release.

packages/common-helpers/package.json (1)

3-3: Version bump to beta.19 aligns with release.

Version metadata consistently updated.

packages/config/eslint-config/package.json (1)

3-3: Version bump to beta.19 is consistent.

Version metadata updated appropriately.

packages/create-zenstack/package.json (1)

3-3: Version bump to beta.19 maintains consistency.

Version metadata updated as part of coordinated release.

packages/schema/src/schema.ts (2)

55-55: LGTM! Relation metadata enhancement.

The addition of the optional hasDefault property to RelationInfo correctly extends the schema to track whether foreign key fields have defaults, aligning with the runtime type generation changes.


257-259: LGTM! Default detection logic correctly extended.

The FieldHasDefault type now properly checks for relation.hasDefault alongside existing checks for primitive defaults and updatedAt fields. The logic flow is correct and maintains backward compatibility.

packages/clients/tanstack-query/src/react.ts (3)

60-60: LGTM! Import added for optimistic update typing.

The WithOptimistic type import enables the query result types to surface optimistic update state to consumers.


96-96: LGTM! Query result type enhanced with optimistic flag.

The ModelQueryResult<T> now wraps data with WithOptimistic<T>, exposing the $optimistic property to track optimistic update state. This aligns with parallel changes in Vue and Svelte clients.


101-103: LGTM! Suspense query result type enhanced with optimistic flag.

The ModelSuspenseQueryResult<T> now wraps data with WithOptimistic<T>, maintaining consistency with standard query results.

packages/clients/tanstack-query/test/svelte-typing-test.ts (2)

20-24: LGTM! Test coverage for optimistic update typing.

The tests correctly validate that $optimistic is accessible on useFindFirst and useFindMany results, confirming the WithOptimistic<T> wrapper is properly applied.


36-37: LGTM! Negative test case for infinite query.

The ts-expect-error correctly validates that $optimistic is not available at the item level in useInfiniteFindMany results, confirming the expected type behavior.

tests/e2e/orm/schemas/default-auth/schema.zmodel (1)

1-24: LGTM! Test schema for default tracking.

The schema correctly defines test models with intentional variation: Profile.userId has @default(auth().id) while Profile.addressId does not, enabling validation of the new hasDefault relation metadata tracking feature.

packages/clients/tanstack-query/test/vue-typing-test.ts (2)

19-22: LGTM! Test coverage for Vue optimistic update typing.

The tests correctly validate that $optimistic is accessible on Vue query results via data.value?.$optimistic, confirming the WithOptimistic<T> wrapper works properly with Vue's reactive references.


33-34: LGTM! Negative test case for Vue infinite query.

The ts-expect-error correctly validates that $optimistic is not available at the item level in infinite query results, maintaining consistency with React and Svelte test patterns.

tests/e2e/orm/schemas/default-auth/input.ts (1)

1-70: Auto-generated file looks correct.

This file is auto-generated by ZenStack CLI and provides the expected type aliases for ORM operations on the User, Profile, and Address models.

@claude
Copy link

claude bot commented Nov 6, 2025

Claude Code is working…

I'll analyze this and get back to you.

View job run

@ymc9 ymc9 added this pull request to the merge queue Nov 6, 2025
Merged via the queue into main with commit aa97bc2 Nov 6, 2025
7 of 8 checks passed
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.

2 participants