RESTApiHandler support compound ids (@@id)
https://github.com/zenstackhq/zenstack/issues/1748
Adds support for the Prisma @@id compound ids to the RESTApiHandler.
TODO
- [x] Add tests for common operations
- [x] Create
- [x] Read
- [x] Update
- [x] Delete
- [x] Support compound ids in related models
- [x] Create
- [x] Read
- [x] Update
- [x] ~~Delete~~ Doesn't make sense, since relationships that are a part of a compound id are always required
📝 Walkthrough
📝 Walkthrough
Walkthrough
The changes in this pull request primarily involve updates to the RequestHandler class in the packages/server/src/api/rest/index.ts file, introducing a new constant idDivider and modifying the structure of ID fields in the RelationshipInfo and ModelInfo types to support arrays of FieldInfo objects. Additionally, several methods have been updated to handle composite IDs, enhancing the request handling capabilities. Correspondingly, the packages/server/tests/api/rest.test.ts file has been updated to include tests for a new PostLike model, ensuring that the API correctly manages likes on posts using compound IDs.
Changes
| File | Change Summary |
|---|---|
packages/server/src/api/rest/index.ts |
- Added constant idDivider. - Updated idField to idFields: FieldInfo[] in RelationshipInfo and ModelInfo. - Updated method signatures for makeIdFilter, makeIdSelect, makeIdKey, and makeCompoundId to accommodate FieldInfo[]. |
packages/server/tests/api/rest.test.ts |
- Introduced PostLike model with relationships to User and Post. - Added tests for creating, retrieving, and deleting likes using compound IDs. - Included tests for existing CRUD operations to ensure no disruption of functionality. - Imported idDivider for managing compound IDs in tests. |
Sequence Diagram(s)
sequenceDiagram
participant Client
participant API
participant Database
Client->>API: Request to create a PostLike
API->>Database: Validate and process request with makeIdFilter
Database-->>API: Confirmation of PostLike creation
API-->>Client: Response with created PostLike
Client->>API: Request to fetch PostLikes
API->>Database: Query PostLikes with makeIdSelect
Database-->>API: Return PostLikes data
API-->>Client: Response with PostLikes data
sequenceDiagram
participant Client
participant API
participant Database
Client->>API: Request to update a PostLike
API->>Database: Validate and process request with makeCompoundId
Database-->>API: Confirmation of PostLike update
API-->>Client: Response with updated PostLike
Client->>API: Request to delete a PostLike
API->>Database: Validate and process request with makeIdKey
Database-->>API: Confirmation of PostLike deletion
API-->>Client: Response confirming deletion
📜 Recent review details
Configuration used: CodeRabbit UI Review profile: CHILL
📥 Commits
Files that changed from the base of the PR and between 7079a9c34dba084364561af0b82df12a753cc437 and dfb97b9b99bd8460ec1f2e4c4d5266c54fb5b11d.
📒 Files selected for processing (1)
- packages/server/tests/api/rest.test.ts (11 hunks)
🧰 Additional context used
🔇 Additional comments (7)
packages/server/tests/api/rest.test.ts (7)
306-333: New test case for fetching a related resource with a compound ID looks good.This test case correctly verifies the functionality of fetching a related resource (likes) for a post, where the likes have a compound ID. The test creates necessary data, makes the API call, and checks the response structure and content.
A few observations:
- The test uses the
idDividerconstant to construct the compound ID, which is a good practice for consistency.- The response structure is correctly verified, including the compound ID in the
dataarray.
1328-1376: New 'compound id' describe block with tests for GET operations looks good.This block adds two important test cases for compound ID support:
- "get all" test: Verifies that the API correctly returns all PostLike entries with compound IDs.
- "get single" test: Checks that a single PostLike entry can be retrieved using its compound ID.
Both tests create necessary data, make API calls, and verify the response structure and content. The use of
idDividerfor constructing compound IDs is consistent.
1641-1683: New test cases for creating relations with compound IDs look good.These test cases verify the creation of PostLike entries with compound IDs. They cover two scenarios:
- Creating a PostLike entry through the main endpoint.
- Creating a PostLike entry with a single compound ID.
Both tests properly set up the necessary data (User and Post) before attempting to create the PostLike entry. The compound ID is correctly constructed using the
idDivider.
1822-1841: Test case for updating an item with a compound ID is implemented correctly.This test verifies the update operation for a PostLike entry using its compound ID. It correctly:
- Sets up the necessary data (User, Post, and PostLike).
- Constructs the compound ID using
idDivider.- Sends a PUT request to update the PostLike entry.
- Checks for a successful response status.
1926-1942: Test case for updating a collection of relations with compound IDs looks good.This test case verifies the functionality of updating a collection of relations (likes) for a post, where the likes have compound IDs. It correctly:
- Sets up the necessary data (User, Post, and PostLike).
- Sends a PATCH request to update the likes relationship.
- Uses the correct compound ID format in the request body.
- Checks for a successful response status.
2022-2035: Test case for deleting an item with a compound ID is implemented correctly.This test verifies the delete operation for a PostLike entry using its compound ID. It properly:
- Sets up the necessary data (User, Post, and PostLike).
- Constructs the compound ID using
idDivider.- Sends a DELETE request to remove the PostLike entry.
- Checks for the correct response status (204 No Content).
- Verifies that the response body is undefined.
Line range hint
1-2492: Overall, the implementation of compound ID support is robust and well-tested.The new test cases added for compound ID support cover all CRUD operations (Create, Read, Update, Delete) and related data fetching. They consistently use the
idDividerconstant for ID construction, which promotes maintainability. The tests verify both the response structures and the correct handling of compound IDs in various scenarios.Some key strengths of the implementation:
- Comprehensive coverage of different operations involving compound IDs.
- Consistent use of
idDividerfor ID construction.- Proper setup of test data before each operation.
- Verification of response structures and statuses.
No significant issues were found in the implementation of compound ID support. The test suite provides a solid foundation for ensuring the reliability of the API's handling of entities with compound IDs.
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?
🪧 Tips
Chat
There are 3 ways to chat with CodeRabbit:
‼️ IMPORTANT Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.
- Files and specific lines of code (under the "Files changed" tab): Tag
@coderabbitaiin a new review comment at the desired location with your query. Examples:@coderabbitai generate unit testing code for this file.@coderabbitai modularize this function.
- PR comments: Tag
@coderabbitaiin 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 generate unit testing code.@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.
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 pauseto pause the reviews on a PR.@coderabbitai resumeto resume the paused reviews.@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository.@coderabbitai full reviewto do a full review from scratch and review all the files again.@coderabbitai summaryto regenerate the summary of the PR.@coderabbitai resolveresolve all the CodeRabbit review comments.@coderabbitai configurationto show the current CodeRabbit configuration for the repository.@coderabbitai helpto get help.
Other keywords and placeholders
- Add
@coderabbitai ignoreanywhere in the PR description to prevent this PR from being reviewed. - Add
@coderabbitai summaryto generate the high-level summary at a specific location in the PR description. - Add
@coderabbitaianywhere in the PR title to generate the title automatically.
CodeRabbit Configuration File (.coderabbit.yaml)
- You can programmatically configure CodeRabbit by adding a
.coderabbit.yamlfile to the root of your repository. - Please see the configuration documentation for more information.
- If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation:
# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
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.
I'm not sure how much time you have to continue working on this. Another option is we merge it first, and I can follow up with a separate PR to continue polishing it.
I have some time today and tomorrow so I'll try to fix at least what's been discussed in this PR and add some more tests. When's the next release cutoff point?
I'm not sure how much time you have to continue working on this. Another option is we merge it first, and I can follow up with a separate PR to continue polishing it.
I have some time today and tomorrow so I'll try to fix at least what's been discussed in this PR and add some more tests. When's the next release cutoff point?
Thank you! Current goal of v2.7 is 10/15. Please take your time 😄.
Marking this ready for review now and moving on to looking at the openapi plugin.
Testing with an updated openapi plugin it looks like there's something still missing here, so converting it back to a draft.
Never mind, forgot to build before publishing the latest to verdaccio 🤦