Skip to content
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

Fix for filtering by compound id #1806

Merged
merged 2 commits into from
Oct 25, 2024

Conversation

thomassnielsen
Copy link
Contributor

In some cases Prisma doesn’t expect these to be nested under the compound id key. I don't have a full overview of where this is (just the example I hit myself), so I added it just where I can confirm it's needed.

In some cases Prisma doesn’t expect these to be nested under the compound id key.
Copy link
Contributor

coderabbitai bot commented Oct 24, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The pull request modifies the makePrismaIdFilter method in the RequestHandler class, adding a new parameter nested with a default value of true. This allows for flexible handling of ID fields in Prisma queries. The method's logic is updated to support both nested and flat representations of ID fields based on the nested parameter. Additionally, the makeFilterValue method is adjusted to utilize the updated makePrismaIdFilter method for relation filters.

Changes

File Path Change Summary
packages/server/src/api/rest/index.ts Updated makePrismaIdFilter method signature to include nested parameter. Adjusted logic for ID field handling. Updated makeFilterValue to call makePrismaIdFilter with nested set to false for relation filters.

Possibly related PRs

  • URI decode resourceId  #1791: This PR modifies the makePrismaIdFilter method in the same file as the main PR, adding URI decoding for the resourceId, which is directly related to the changes made in the main PR regarding the handling of ID fields.

Suggested reviewers

  • ymc9

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.

Copy link
Contributor

@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

🧹 Outside diff range and nitpick comments (1)
packages/server/src/api/rest/index.ts (1)

Line range hint 1239-1261: Optimize accumulator performance and improve documentation.

The method implementation has two areas for improvement:

  1. The spread operator in the reduce function can lead to O(n²) time complexity.

  2. The new nested parameter would benefit from documentation explaining its purpose.

  3. Optimize the reduce implementation:

-            return idFields.reduce(
-                (acc, curr, idx) => ({
-                    ...acc,
-                    [curr.name]: this.coerce(curr.type, decodedId.split(this.idDivider)[idx]),
-                }),
-                {}
-            );
+            const result = {};
+            idFields.forEach((curr, idx) => {
+                result[curr.name] = this.coerce(curr.type, decodedId.split(this.idDivider)[idx]);
+            });
+            return result;
  1. Add JSDoc documentation:
+    /**
+     * Creates a Prisma filter for ID fields
+     * @param idFields - Array of ID field information
+     * @param resourceId - The resource ID value
+     * @param nested - When true, creates a nested filter structure for compound IDs.
+     *                 When false, creates a flat structure with individual ID fields.
+     * @returns The Prisma filter object
+     */
     private makePrismaIdFilter(idFields: FieldInfo[], resourceId: string, nested: boolean = true) {
🧰 Tools
🪛 Biome

[error] 1257-1257: Avoid the use of spread (...) syntax on accumulators.

Spread syntax should be avoided on accumulators (like those in .reduce) because it causes a time complexity of O(n^2).
Consider methods such as .splice or .push instead.

(lint/performance/noAccumulatingSpread)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between fac631b and ba8bf68.

📒 Files selected for processing (1)
  • packages/server/src/api/rest/index.ts (3 hunks)
🧰 Additional context used
🪛 Biome
packages/server/src/api/rest/index.ts

[error] 1257-1257: Avoid the use of spread (...) syntax on accumulators.

Spread syntax should be avoided on accumulators (like those in .reduce) because it causes a time complexity of O(n^2).
Consider methods such as .splice or .push instead.

(lint/performance/noAccumulatingSpread)

🔇 Additional comments (1)
packages/server/src/api/rest/index.ts (1)

1609-1609: LGTM: Correct usage of flat ID structure for relation filters.

The change to use nested: false is appropriate here as Prisma's is filter operator expects a flat structure for ID fields.

Let's verify the usage of relation filters:

✅ Verification successful

Let me gather more context about the makePrismaIdFilter implementation and its usage to ensure the verification is thorough.


Let me try a different pattern to find the makePrismaIdFilter implementation.


Based on the implementation and usage context found, I can now provide the final verification response:

Verified: Correct implementation and usage of flat ID structure for relation filters

The code correctly uses nested: false in the relation filter context because:

  • The makePrismaIdFilter implementation shows it handles both nested and flat structures through the nested parameter
  • In the relation filter case (is: this.makePrismaIdFilter(info.idFields, value, false)), flat structure is correctly used
  • This aligns with Prisma's expectations for the is operator in relation filters, which requires a flat structure
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for relation filter usage patterns
# Test: Search for filter patterns with 'is' operator. Expect: Only flat ID structures.
rg -A 5 "is: this\.makePrismaIdFilter"

Length of output: 508


Script:

#!/bin/bash
# Check the implementation of makePrismaIdFilter
ast-grep --pattern 'makePrismaIdFilter($_) {
  $$$
}'

Length of output: 55


Script:

#!/bin/bash
# Search for makePrismaIdFilter implementation with context
rg -A 10 "makePrismaIdFilter" packages/server/src/api/rest/index.ts

Length of output: 4510

@thomassnielsen thomassnielsen requested a review from ymc9 October 25, 2024 06:02
Copy link
Contributor

@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: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between ba8bf68 and 5490bad.

📒 Files selected for processing (1)
  • packages/server/src/api/rest/index.ts (3 hunks)
🧰 Additional context used
🪛 Biome
packages/server/src/api/rest/index.ts

[error] 1257-1257: Avoid the use of spread (...) syntax on accumulators.

Spread syntax should be avoided on accumulators (like those in .reduce) because it causes a time complexity of O(n^2).
Consider methods such as .splice or .push instead.

(lint/performance/noAccumulatingSpread)

🔇 Additional comments (2)
packages/server/src/api/rest/index.ts (2)

1239-1243: LGTM! Good addition of the nested parameter.

The new nested parameter provides flexibility in ID filter generation while maintaining backward compatibility.


1605-1609: LGTM! Correct handling of ID filters in non-unique contexts.

The changes correctly implement flat ID filters for relation filtering, aligning with Prisma's expectations that nested ID filters should only be used in unique where clauses.

packages/server/src/api/rest/index.ts Show resolved Hide resolved
@ymc9 ymc9 merged commit 35ea74f into zenstackhq:dev Oct 25, 2024
13 checks passed
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