feat: Add a new `beforeLiveQueryEvent` trigger
Pull Request
- Report security issues confidentially.
- Any contribution is under this license.
- Link this pull request to an issue.
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.
Thanks for opening this pull request!
@codeKonami please let us know when this is ready for review and change the PR state.
@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.
@coderabbitai review
✅ 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.
📝 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 Cacheat either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off theData Retentionsetting 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
@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.
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 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 generate sequence diagramto generate a sequence diagram of the changes in this 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.
: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)
@codeKonami Apologies for the long wait time; when you have some time, please take a look at the review comments.