-
-
Notifications
You must be signed in to change notification settings - Fork 111
feat(runtime): inject enhanced client or tx context so it can be retrieved in extensions #2018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
📝 WalkthroughWalkthroughThe changes update the proxy implementation and introduce an integration test for enhanced proxy context behavior. In the proxy code, an extra parameter is now passed to the handler creation, and the Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant Proxy
participant Handler
Caller->>Proxy: Access property "$parent"
Proxy->>Handler: Invoke get trap with key "$parent"
Handler-->>Caller: Return dbOrTx (database/transaction context)
sequenceDiagram
participant Client
participant EnhancedModel
participant CounterModel
participant TransactionHandler
Client->>EnhancedModel: call createWithCounter(args)
EnhancedModel->>CounterModel: Increment counter for the Address model
alt Operation in Transaction
EnhancedModel->>TransactionHandler: Execute creation within transaction
else Operation outside Transaction
EnhancedModel->>EnhancedModel: Create Address directly
end
CounterModel-->>Client: Return updated counter value
Possibly related PRs
Suggested reviewers
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
⏰ Context from checks skipped due to timeout of 90000ms (6)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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 (2)
tests/integration/tests/enhancements/proxy/extension-context.test.ts (1)
45-48
: Clarify the conditional logic comment.The comment on line 46 states "not running in a transaction, so we need to create a new transaction", but the conditional check is looking for the presence of a
$transaction
method, which indicates that we're working with a client that can create transactions.- if (dbOrTx['$transaction']) { - // not running in a transaction, so we need to create a new transaction + if (dbOrTx['$transaction']) { + // we have a client that can create transactions, not already in a transactionpackages/runtime/src/enhancements/node/proxy.ts (1)
311-313
: Effective implementation of context retrieval via a special property.This code adds support for accessing the enhanced database or transaction context through the
$zenstack_parent
property, which is the key feature being added in this PR.Consider adding a JSDoc comment explaining the purpose and usage of the
$zenstack_parent
property, which would make the code more maintainable and easier to understand:+ /** + * Returns the enhanced database or transaction context that created this proxy. + * This allows extensions to access the enhanced client functionality. + */ get(target, propKey) { if (propKey === '$zenstack_parent') { return dbOrTx; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
packages/runtime/src/enhancements/node/proxy.ts
(2 hunks)tests/integration/tests/enhancements/proxy/extension-context.test.ts
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (6)
- GitHub Check: build-test (20.x)
- GitHub Check: build-test (20.x)
- GitHub Check: build-test (20.x)
- GitHub Check: Analyze (javascript-typescript)
- GitHub Check: dependency-review
- GitHub Check: OSSAR-Scan
🔇 Additional comments (6)
tests/integration/tests/enhancements/proxy/extension-context.test.ts (4)
1-84
: Excellent test implementation for the proxy extension context feature.This new test file effectively validates the functionality of retrieving enhanced client/transaction context in extensions. The test creates a custom method that increments a counter whenever a new Address is created, demonstrating how the enhanced context can be utilized.
31-32
: Good implementation of context retrieval using the new property.The line
const dbOrTx = this['$zenstack_parent'];
shows how to access the enhanced client or transaction context within an extension, which is the core functionality being added in this PR.
70-73
: Effective test coverage for both standalone and transaction contexts.This test section creates Address entries both individually and within transactions, ensuring that the enhanced context retrieval works correctly in both scenarios.
76-81
: Clear validation of the expected behavior.The test verifies that the Counter model correctly tracks the total number of Address entries created, confirming that the enhanced context is properly accessible within extensions.
packages/runtime/src/enhancements/node/proxy.ts (2)
292-292
: Well-implemented modification to pass database/transaction context to the handler proxy.The change updates the
createHandlerProxy
call to pass the original target (database or transaction) as a parameter, enabling access to the enhanced context within extensions.
306-306
: Appropriate parameter addition for context retrieval.Adding the
dbOrTx
parameter tocreateHandlerProxy
enables the proxy to maintain a reference to the original database or transaction object.
477fe0d
to
d20cbd4
Compare
…ieved in extensions
d20cbd4
to
f55a2fd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't know there's actually the $parent
field in client extensions that refer to the current prisma client instance. I saw it in Prisma code but seems not documented anywhere ...
Do you think we should simply proxy this field directly instead of using a separate $zenstack_parent
field? Wondering if most people would expect $parent
to be enhanced if it originated from an enhanced client. What do you think?
@ymc9 Yeah, actually that makes a lot of sense since the proxy will still allow access to any non-proxied properties; so this shouldn't break any existing usage of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! Looking great now!
Prisma already exposes a
$parent
property in the context, but this will point towards the unenhanced client or transaction object