zenstack icon indicating copy to clipboard operation
zenstack copied to clipboard

.count use same flow as findMany

Open PodkopovP opened this issue 6 months ago • 3 comments

The current .count implementation (and probably other aggregate functions) create a different .where clause to findMany - this means that doing a .count and .where with the same inputs, could (after zenstacks RLS implementation) cause be looking at different results - i.e. you might end up with 4 results from .findMany, but .count says there should be 10 results.

PodkopovP avatar Jun 24 '25 14:06 PodkopovP

📝 Walkthrough

Walkthrough

The changes extend the FindOperations type to include the 'count' operation and update the doFind method to handle 'count' by modifying its arguments before invoking the Prisma client. The count method is refactored to delegate to doFind, aligning its logic with other find operations for consistent policy enforcement.

Changes

File Change Summary
packages/runtime/src/enhancements/node/policy/handler.ts Extended FindOperations type to include 'count'; updated doFind to handle 'count'; refactored count method to delegate to doFind.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant PolicyProxyHandler
    participant PrismaClient

    Caller->>PolicyProxyHandler: count(args)
    PolicyProxyHandler->>PolicyProxyHandler: doFind('count', args, [])
    PolicyProxyHandler->>PrismaClient: count(modified args)
    PrismaClient-->>PolicyProxyHandler: result
    PolicyProxyHandler-->>Caller: result
✨ Finishing Touches
  • [ ] 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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 Jun 24 '25 14:06 coderabbitai[bot]

The two issues I found so far:

  • Including relations in select/include can get funny and can cause a mismatch between .findMany / count - .count doesnt support including relations, so the current implementation doesn't support injecting guards based on relations. The solution I have here is to pass .count through the same funnel .findMany goes through, but override select to true before the final prisma call is made - this lets zenstack inject relevant relational queries to the where clause
  • .count currently injects using .injectAuthGuardAsWhere, which always wraps the where clause in 'AND' - this isn't the same process as .findMany goes through (haven't done too much investigation to see what .findMany does, but it seems to more smartly combine the original & new args)

PodkopovP avatar Jun 24 '25 14:06 PodkopovP

Relevant discord discussion from a similar issue.

https://discord.com/channels/1035538056146595961/1338578707086049372

I was never able to create a simple proof of concept replication. It was only happening in our large real world project with highly complex rules across tables, including using check() across tables.

JoeRoddy avatar Jul 08 '25 17:07 JoeRoddy