zenstack icon indicating copy to clipboard operation
zenstack copied to clipboard

RESTApiHandler support compound ids (@@id)

Open thomassnielsen opened this issue 1 year ago • 3 comments

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

thomassnielsen avatar Sep 30 '24 13:09 thomassnielsen

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

  1. The test uses the idDivider constant to construct the compound ID, which is a good practice for consistency.
  2. The response structure is correctly verified, including the compound ID in the data array.

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:

  1. "get all" test: Verifies that the API correctly returns all PostLike entries with compound IDs.
  2. "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 idDivider for 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:

  1. Creating a PostLike entry through the main endpoint.
  2. 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:

  1. Sets up the necessary data (User, Post, and PostLike).
  2. Constructs the compound ID using idDivider.
  3. Sends a PUT request to update the PostLike entry.
  4. 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:

  1. Sets up the necessary data (User, Post, and PostLike).
  2. Sends a PATCH request to update the likes relationship.
  3. Uses the correct compound ID format in the request body.
  4. 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:

  1. Sets up the necessary data (User, Post, and PostLike).
  2. Constructs the compound ID using idDivider.
  3. Sends a DELETE request to remove the PostLike entry.
  4. Checks for the correct response status (204 No Content).
  5. 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 idDivider constant 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:

  1. Comprehensive coverage of different operations involving compound IDs.
  2. Consistent use of idDivider for ID construction.
  3. Proper setup of test data before each operation.
  4. 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?

❤️ Share
🪧 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 @coderabbitai in 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 @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 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 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 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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file 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.

coderabbitai[bot] avatar Sep 30 '24 13:09 coderabbitai[bot]

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?

thomassnielsen avatar Oct 01 '24 06:10 thomassnielsen

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 😄.

ymc9 avatar Oct 01 '24 17:10 ymc9

Marking this ready for review now and moving on to looking at the openapi plugin.

thomassnielsen avatar Oct 04 '24 07:10 thomassnielsen

Testing with an updated openapi plugin it looks like there's something still missing here, so converting it back to a draft.

thomassnielsen avatar Oct 04 '24 09:10 thomassnielsen

Never mind, forgot to build before publishing the latest to verdaccio 🤦

thomassnielsen avatar Oct 04 '24 09:10 thomassnielsen