-
-
Notifications
You must be signed in to change notification settings - Fork 109
fix(policy): properly handle array-form of upsert payload #1101
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 recent updates primarily target enhancements and bug fixes in nested data operations within a database management context, focusing on the reverse iteration of data for various actions and refining logic in policy handling for more efficient data manipulation. Furthermore, integration tests have been adjusted to better reflect these changes, removing unnecessary configurations and improving test reliability for database operations. Changes
Assessment against linked issues
These assessments validate that the majority of the objectives related to issue #1080 have been directly addressed through the implemented changes, with the exception of explicitly aligning the behavior of the enhanced Prisma client with the pure PrismaClient. The changes made in the codebase indicate a focus on improving the logic and functionality around nested relation operations, which is in line with the objectives of the linked issue. 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 as PR comments)
Additionally, you can add CodeRabbit Configration 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.
Review Status
Actionable comments generated: 0
Configuration used: CodeRabbit UI
Files selected for processing (4)
- packages/runtime/src/cross/nested-write-visitor.ts (11 hunks)
- packages/runtime/src/enhancements/policy/handler.ts (4 hunks)
- tests/integration/tests/regression/issue-1078.test.ts (3 hunks)
- tests/integration/tests/regression/issue-1080.test.ts (1 hunks)
Additional comments: 26
tests/integration/tests/regression/issue-1078.test.ts (4)
- 2-8: > 📝 NOTE
This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [5-15]
The schema definition for the
Counter
model within theloadSchema
call is well-defined and includes model-specific directives like@@validate
and@@allow
. This setup is crucial for testing the behavior of theCounter
model under various conditions, ensuring that the model's constraints and permissions are correctly enforced.
- 21-25: The use of
expect().toResolveTruthy()
for asserting the successful creation of acounter
instance is a good practice. It ensures that the promise resolves successfully, indicating that the operation completed without errors. This approach enhances the readability and maintainability of the test by clearly stating the expected outcome.- 28-33: Similarly, the use of
expect().toResolveTruthy()
for asserting the successful update of acounter
instance is consistent with best practices for writing clear and maintainable tests. It confirms that the update operation completes successfully, which is essential for verifying the correct behavior of theCounter
model under update scenarios.- 44-44: The schema definition for the
Post
model within theloadSchema
call is concise and includes model-specific directives like@allow
. This setup is important for testing the behavior of thePost
model, especially in terms of access control and permissions. It ensures that the model's constraints are correctly enforced and tested.tests/integration/tests/regression/issue-1080.test.ts (6)
- 1-23: The import of
loadSchema
from@zenstackhq/testtools
and the setup of theProject
andField
models within theloadSchema
call are correctly implemented. Including thelogPrismaQuery: true
option is useful for debugging purposes by logging the queries made by Prisma. However, it's important to ensure that such logging is disabled or removed in production environments to avoid potential performance impacts and to keep the logs clean.- 28-35: Creating a
project
instance with nestedFields
using thecreate
operation is a critical part of the test, ensuring that nested relations can be created successfully. This operation tests the ability to handle nested creation within the enhanced Prisma client, which is central to the issue being addressed.- 37-62: The test case for updating a
project
instance with nestedFields
using theupsert
operation is well-structured. It verifies that theupsert
operation can correctly handle multiple nested relations, addressing the core issue of the pull request. This test is essential for ensuring that the fix works as intended.- 64-82: The subsequent test case further validates the
upsert
operation on a single nested relation. It's important for ensuring that the fix not only works for multiple relations but also behaves correctly when applied to a single relation. This test complements the previous one by covering a different scenario.- 84-106: This test case introduces a mix of
upsert
andupdate
operations on nested relations, providing a more complex scenario to verify the fix. It's crucial for testing the fix's robustness and ensuring that it handles mixed operation types correctly.- 108-131: The final test case in this file tests the
upsert
operation with a non-existent ID, combined with anupdate
operation on an existing relation. This scenario is important for ensuring that the fix can handle edge cases, such as attempting to upsert a non-existent relation, while still correctly updating existing relations.packages/runtime/src/cross/nested-write-visitor.ts (10)
- 158-158: Replacing
enumerate
withenumerateReverse
in thecreate
action is a significant change that aims to ensure the correct order of processing nested relations. This change is likely to address the core issue of the pull request by ensuring that nested relations are processed in reverse order, which can be crucial for operations likeupsert
that depend on the order in which relations are processed.- 186-186: Similarly, the use of
enumerateReverse
in theconnectOrCreate
action aligns with the goal of processing nested relations in reverse order. This adjustment is consistent with the overall objective of the pull request and is likely to contribute to the correct handling of nested relations during upsert operations.- 201-201: The application of
enumerateReverse
to theconnect
action further emphasizes the importance of processing order for nested relations. This change ensures consistency across different actions, reinforcing the approach taken to address the issue at hand.- 213-213: The use of
enumerateReverse
for thedisconnect
action is another example of the consistent application of the reverse order processing strategy. This change is part of the broader effort to ensure that all nested relations are correctly processed, regardless of the specific action being performed.- 228-228: Applying
enumerateReverse
to theupdate
action is crucial for ensuring that updates to nested relations are processed in the correct order. This change is directly related to the core issue being addressed and is essential for the proper handling of nested relations during upsert operations.- 247-247: The use of
enumerateReverse
in theupdateMany
action is consistent with the changes made to other actions. It ensures that updates to multiple nested relations are processed in reverse order, which is important for maintaining the integrity of nested relations during complex operations.- 261-261: Finally, the application of
enumerateReverse
to theupsert
action is central to the pull request's objective. This change ensures that the upsert operation correctly processes all nested relations in reverse order, addressing the issue that prompted the pull request.- 281-281: The use of
enumerateReverse
for thedelete
action further extends the reverse order processing strategy to deletion operations. This consistency across actions is important for ensuring that all nested relations are processed correctly, regardless of the operation being performed.- 291-291: Applying
enumerateReverse
to thedeleteMany
action is in line with the changes made to other actions. It ensures that deletions of multiple nested relations are processed in reverse order, which can be crucial for maintaining the integrity of nested relations during complex deletion operations.- 339-350: The implementation of the
enumerateReverse
method is crucial for enabling the reverse order processing of nested relations. This method allows for iterating over data in reverse order, which is essential for the changes made to various actions in theNestedWriteVisitor
class. The method is well-implemented and directly supports the pull request's objective.packages/runtime/src/enhancements/policy/handler.ts (6)
- 1384-1394: The implementation of
mergeToParent
method is well-designed to handle merging values into a parent object, supporting both single and multiple relations under the same key. Ensure that this method is consistently used across all relevant parts of the class to manage nested relations effectively.- 1396-1408: The
removeFromParent
method is correctly implemented to handle the removal of values from a parent object, supporting both single values and arrays. It's essential for maintaining the integrity of nested relations. Verify its usage across the class to ensure it's applied correctly in all scenarios involving modification of nested relations.- 346-354: The usage of
mergeToParent
in theconnectOrCreate
method for handling the 'connect' and 'create' cases is appropriate and ensures that nested relations are correctly managed. This approach simplifies the logic and improves maintainability.- 358-358: The application of
removeFromParent
in theconnectOrCreate
method to remove theconnectOrCreate
clause after processing is a good practice. It ensures that the parent object remains clean and only contains the necessary data.- 888-888: Using
removeFromParent
in theupdate
method to remove the 'create' clause after processing is consistent with the intended purpose of managing nested relations effectively. This ensures that the update payload is correctly structured before proceeding with the operation.- 921-937: The use of
mergeToParent
andremoveFromParent
in theupsert
method to handle the conversion of an 'upsert' operation into an 'update' or 'create' operation is well thought out. It demonstrates a clear understanding of how to manage complex nested relations dynamically.
Fixes #1080
Summary by CodeRabbit
Refactor
Tests