parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

feat: Add a new `beforeLiveQueryEvent` trigger

Open codeKonami opened this issue 1 year ago • 8 comments

Pull Request

Issue

Closes: https://github.com/parse-community/parse-server/issues/9431

Approach

I added a beforeLiveQueryEvent trigger which allows the user to modify the updated object before it is passed to the LiveQuery server (via PubSub). It is a cloned version of the object so it won't modify the one passed to the afterSave trigger.

I've added an option which I'm not really convinced. You can set the context of the request to req.context.preventLiveQuery === true to prevent the publish to be made by the Parse Server and so limiting some network and CPU consumption depending on some business logic.

Tasks

  • [x] Add tests
  • [ ] Add changes to documentation (guides, repository pages, code comments)
  • [ ] Add security check
  • [ ] Add new Parse Error codes to Parse JS SDK

Summary by CodeRabbit

  • New Features

    • Introduced support for registering "before live query event" triggers, allowing custom logic to run before live query events are published.
    • Added the ability to prevent specific live query events from being sent by setting a context flag in the trigger.
  • Bug Fixes

    • Improved context handling for triggers to ensure consistent behavior during live query events.
  • Tests

    • Added comprehensive tests to verify the behavior and effects of the new "before live query event" triggers.

codeKonami avatar Nov 19 '24 20:11 codeKonami

Thanks for opening this pull request!

@codeKonami please let us know when this is ready for review and change the PR state.

mtrezza avatar Apr 07 '25 19:04 mtrezza

@codeKonami please let us know when this is ready for review and change the PR state.

The code is ready, I'd be interested to have your opinion on the approach before going further on documentation and probably more unit tests.

codeKonami avatar Apr 15 '25 21:04 codeKonami

@coderabbitai review

mtrezza avatar May 03 '25 15:05 mtrezza

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

coderabbitai[bot] avatar May 03 '25 15:05 coderabbitai[bot]

📝 Walkthrough

"""

Walkthrough

The changes introduce a new Parse Server trigger called beforeLiveQueryEvent. This trigger allows developers to execute custom logic before a LiveQuery event is published. The implementation includes a new registration method in the Parse Cloud SDK, integration of the new trigger type into the trigger handling system, and updates to the LiveQuery publishing flow to support conditional event suppression based on the trigger's outcome. Comprehensive tests have been added to verify correct triggering, context handling, object mutation, and the ability to prevent LiveQuery events.

Changes

File(s) Change Summary
spec/ParseLiveQuery.spec.js Added new test cases for beforeLiveQueryEvent trigger, testing execution, context, mutation, and event prevention.
src/RestWrite.js Refactored runAfterSaveTrigger to async; integrated beforeLiveQueryEvent trigger before LiveQuery notification.
src/cloud-code/Parse.Cloud.js Added ParseCloud.beforeLiveQueryEvent method for registering the new trigger.
src/triggers.js Added beforeEvent trigger type; updated context handling in trigger execution to support the new trigger.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant ParseServer
    participant CloudCode
    participant LiveQueryServer

    Client->>ParseServer: Save or update object
    ParseServer->>CloudCode: Run beforeLiveQueryEvent trigger (if defined)
    alt beforeLiveQueryEvent sets preventLiveQuery
        CloudCode-->>ParseServer: preventLiveQuery = true
        ParseServer-->>LiveQueryServer: (No notification sent)
    else beforeLiveQueryEvent allows event
        CloudCode-->>ParseServer: allow event
        ParseServer->>LiveQueryServer: Notify LiveQuery event
    end
    ParseServer->>CloudCode: Run afterSave trigger (if defined)
    ParseServer-->>Client: Respond to save/update

Assessment against linked issues

Objective Addressed Explanation
Implement a beforeLiveQueryEvent trigger to allow conditional suppression of LiveQuery event publishing (#9431)
Provide a registration method for the new trigger in the Cloud Code SDK (#9431)
Integrate the new trigger into the event publishing flow and context handling (#9431)
Add tests verifying trigger execution, context mutation, and event suppression (#9431)
"""

[!TIP]

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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.

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 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 May 03 '25 15:05 coderabbitai[bot]

:tada: Snyk checks have passed. No issues have been found so far.

:white_check_mark: security/snyk check is complete. No issues have been found. (View Details)

parseplatformorg avatar May 03 '25 15:05 parseplatformorg

@codeKonami Apologies for the long wait time; when you have some time, please take a look at the review comments.

mtrezza avatar May 04 '25 20:05 mtrezza