Skip to content

Conversation

@ymc9
Copy link
Member

@ymc9 ymc9 commented Jul 9, 2025

Summary by CodeRabbit

  • New Features

    • Added support for sequential transactions, allowing execution of multiple operations in order within a single transaction.
    • Introduced transaction isolation level options for greater control over transaction behavior.
    • Added a new CLI executable alias, enabling the CLI to be invoked with an alternative command.
  • Bug Fixes

    • Improved transaction API to prevent use of unsupported methods within transactions.
  • Tests

    • Expanded and reorganized transaction tests to cover both interactive (callback-based) and sequential (array-based) transaction modes.

Copilot AI review requested due to automatic review settings July 9, 2025 08:30
@coderabbitai
Copy link

coderabbitai bot commented Jul 9, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

This update refactors the transaction handling in the client runtime by introducing overloads for the $transaction method to support both interactive (callback-based) and sequential (array-based) transactions, adds transaction isolation level support, and replaces the internal promise creation utility. Related type definitions, constants, and comprehensive tests are also updated.

Changes

Files/Groups Change Summary
TODO.md Marked "Batch transaction" as complete and renamed to "Sequential transaction".
packages/cli/package.json Added a new CLI binary alias "zen" pointing to the same entry as "zenstack".
packages/runtime/src/client/client-impl.ts Refactored $transaction to support overloads for interactive and sequential transactions, added isolation level, replaced createDeferredPromise with createZenStackPromise, and updated related logic and imports.
packages/runtime/src/client/constants.ts Added constant TRANSACTION_UNSUPPORTED_METHODS listing unsupported transaction methods.
packages/runtime/src/client/contract.ts Added TransactionIsolationLevel enum, updated $transaction method overloads, and introduced types for unsupported methods.
packages/runtime/src/client/promise.ts Introduced ZenStackPromise type, refactored and renamed createDeferredPromise to createZenStackPromise with schema typing and optional transaction client parameter.
packages/runtime/src/utils/type-utils.ts Added utility type UnwrapTuplePromises to unwrap promises in tuple types.
packages/runtime/test/client-api/transaction.test.ts Reorganized and expanded tests to cover both interactive and sequential transaction APIs, including success, failure, nesting, and execution order scenarios.

Possibly related PRs

  • zenstackhq/zenstack-v3#69: Originally implemented raw query methods ($executeRaw, $queryRaw, etc.) in the client; this PR builds upon that by refactoring transaction handling and promise creation for those methods.

Poem

In fields of code where rabbits dwell,
Transactions now can run quite well—
Interactive or in a row,
Promises line up, ready to go!
With isolation levels in the mix,
Bugs and confusion, this update nicks.
🐇✨ Hooray for code that hops and ticks!


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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@ymc9
Copy link
Member Author

ymc9 commented Jul 9, 2025

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Jul 9, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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

Implements support for sequential transactions in the client API alongside existing interactive transactions.

  • Adds end-to-end tests for sequential transactions in transaction.test.ts
  • Introduces the UnwrapTuplePromises type and ZenStackPromise abstraction
  • Updates client contract and implementation to handle array-based (sequential) transactions, including overloads and runtime logic

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
packages/runtime/test/client-api/transaction.test.ts Added tests covering empty, successful, ordered, and rollback scenarios for sequential transactions
packages/runtime/src/utils/type-utils.ts Added UnwrapTuplePromises to map a tuple of promises to their unwrapped results
packages/runtime/src/client/promise.ts Renamed and extended the deferred promise utility to createZenStackPromise with a cb callback
packages/runtime/src/client/contract.ts Defined overloads for interactive vs. sequential $transaction and added TransactionIsolationLevel
packages/runtime/src/client/constants.ts Introduced TRANSACTION_UNSUPPORTED_METHODS constant
packages/runtime/src/client/client-impl.ts Implemented $transaction overload dispatch, interactiveTransaction, and sequentialTransaction
packages/cli/package.json Added "zen" alias for the CLI binary
TODO.md Marked sequential transaction feature as completed

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: 0

🧹 Nitpick comments (1)
TODO.md (1)

57-57: Fix indentation to maintain consistent markdown formatting.

The list item indentation should be 4 spaces instead of 8 to match the markdown structure.

-        - [x] Sequential transaction
+    - [x] Sequential transaction
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d6834d8 and 4069781.

📒 Files selected for processing (8)
  • TODO.md (1 hunks)
  • packages/cli/package.json (1 hunks)
  • packages/runtime/src/client/client-impl.ts (7 hunks)
  • packages/runtime/src/client/constants.ts (1 hunks)
  • packages/runtime/src/client/contract.ts (3 hunks)
  • packages/runtime/src/client/promise.ts (2 hunks)
  • packages/runtime/src/utils/type-utils.ts (1 hunks)
  • packages/runtime/test/client-api/transaction.test.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/runtime/src/client/promise.ts (2)
packages/sdk/src/schema/schema.ts (1)
  • SchemaDef (10-17)
packages/runtime/src/client/contract.ts (1)
  • ClientContract (49-158)
🪛 markdownlint-cli2 (0.17.2)
TODO.md

57-57: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)

⏰ 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). (1)
  • GitHub Check: build-test (20.x)
🔇 Additional comments (26)
packages/runtime/src/client/constants.ts (1)

11-14: Well-designed constant for transaction method restrictions.

The constant properly defines methods that should be unavailable in transaction contexts. The choices are logical - nested transactions, disconnection during transaction, and plugin modifications could all cause issues.

packages/runtime/src/utils/type-utils.ts (1)

72-74: Correct implementation of tuple promise unwrapping utility.

The type utility properly maps over a readonly tuple and unwraps each promise element using Awaited<T[K]>. This will be essential for typing the results of sequential transactions.

packages/cli/package.json (1)

21-21: Good UX improvement with shorter CLI alias.

Adding the "zen" alias provides a convenient shorter command while maintaining the existing "zenstack" command for backward compatibility.

packages/runtime/src/client/promise.ts (5)

1-3: Appropriate imports for enhanced promise typing.

The new imports support the schema-aware promise typing and transaction client integration.


4-13: Well-designed promise type with transaction support.

The ZenStackPromise type properly extends Promise<T> while adding:

  • Generic schema parameter for type safety
  • Private cb property for internal transaction client handling
  • Optional transaction client parameter in the callback

19-21: Correct function signature for schema-aware promise creation.

The function signature properly:

  • Adds generic Schema parameter
  • Updates callback to accept optional transaction client
  • Maintains backward compatibility through optional parameter

23-25: Proper transaction client parameter handling.

The callback correctly:

  • Accepts optional transaction client parameter
  • Passes it to the user-provided callback
  • Maintains promise memoization behavior

42-42: Correct exposure of callback function.

Exposing the cb property allows the transaction implementation to invoke the callback with the appropriate transaction client.

packages/runtime/test/client-api/transaction.test.ts (6)

19-20: Well-structured test organization.

The restructuring into separate describe blocks for interactive and sequential transactions improves test organization and readability.


29-29: Good correction to email value.

The email value has been corrected from '[email protected]' to '[email protected]' to properly represent the second user in the test.


72-72: Type casting approach for nested transactions.

The explicit type casting (tx as any) is used to access the $transaction method on the transaction client. This suggests the transaction client type doesn't include the $transaction method, which aligns with the contract changes.

Also applies to: 93-93


85-105: Comprehensive nested transaction failure test.

The addition of a nested failed transaction test provides good coverage for error handling in nested scenarios, ensuring proper rollback behavior.


108-157: Excellent coverage for sequential transactions.

The sequential transaction test suite provides comprehensive coverage including:

  • Empty array handling
  • Deferred execution verification
  • Successful transaction execution with mixed operations
  • Execution order preservation
  • Rollback behavior on errors

The tests effectively validate the new sequential transaction API behavior.


114-122: Excellent test for deferred execution.

This test cleverly verifies that promises are not executed immediately when passed to $transaction but only when the transaction is actually executed. This is crucial for the sequential transaction implementation.

packages/runtime/src/client/contract.ts (4)

4-4: Good addition of utility types.

The import of UnwrapTuplePromises utility type is necessary for the sequential transaction return type handling.


31-31: Well-designed type safety for transaction clients.

The import of TRANSACTION_UNSUPPORTED_METHODS constant and the derived TransactionUnsupportedMethods type provide proper type safety by excluding unsupported methods from transaction clients.

Also applies to: 33-33


35-44: Standard SQL transaction isolation levels.

The TransactionIsolationLevel enum includes all standard SQL isolation levels with proper string values. This provides good compatibility with database systems.


116-129: Well-designed transaction method overloads.

The two overloads provide clear separation between interactive (callback-based) and sequential (array-based) transaction modes:

  1. Interactive transaction properly excludes unsupported methods from the transaction client
  2. Sequential transaction uses tuple type to maintain result type correspondence
  3. Both overloads support optional isolation level configuration

The type system ensures proper usage and return types for each mode.

packages/runtime/src/client/client-impl.ts (8)

1-1: Good addition of invariant for input validation.

The import of invariant from @zenstackhq/common-helpers is appropriate for input validation in the transaction dispatch logic.


18-18: Proper import of transaction isolation level.

The import of TransactionIsolationLevel type from the contract module maintains proper type consistency.


36-36: Enhanced promise type for transaction support.

The import of createZenStackPromise and ZenStackPromise type supports the new transaction-aware promise handling needed for sequential transactions.


148-168: Well-implemented transaction dispatch logic.

The implementation provides:

  • Clear overloads for interactive and sequential transaction modes
  • Proper input validation using invariant
  • Clean dispatch logic based on input type
  • Good error messaging for invalid inputs

The logic correctly identifies function vs array inputs and dispatches to appropriate handlers.


170-189: Robust interactive transaction implementation.

The interactive transaction implementation properly:

  • Checks for existing transaction context to avoid nested transaction issues
  • Supports optional isolation level configuration
  • Creates proper transaction client with shared state
  • Maintains backward compatibility with existing transaction usage

The nested transaction handling is appropriate for the interactive mode.


191-215: Excellent sequential transaction implementation.

The sequential transaction implementation correctly:

  • Processes promises sequentially within the transaction context
  • Maintains execution order through the for loop
  • Passes the transaction client to each promise callback
  • Handles both nested and new transaction scenarios
  • Supports isolation level configuration

The sequential execution ensures proper transaction semantics.


287-290: Updated promise creation for transaction support.

The replacement of createDeferredPromise with createZenStackPromise in raw query methods enables proper integration with the new transaction system while maintaining existing functionality.

Also applies to: 294-298, 302-305, 309-313


362-362: Enhanced promise creation with transaction client support.

The updated createZenStackPromise call now accepts an optional transaction client parameter, enabling sequential transaction execution while maintaining backward compatibility.

@ymc9 ymc9 merged commit 5a746d9 into dev Jul 9, 2025
3 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Jul 9, 2025
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