feat: add $push and $remove atomic operations to localAPI update for hasMany relationship fields #13997
+1,107
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What?
This PR adds atomic update operations (
$push
and$remove
) to Local API update functions for relationship fields configured withhasMany: true
. The operations work for both single-document updates (updateById) and multi-document updates. It is related to #13891, which introduced atomic operations at the database adapter layer.New API:
Why?
Right now, updating relationship arrays generally means replacing the whole array. That forces clients to first fetch the existing document, merge changes locally, and then send everything back. Atomic operations let clients express intent directly (e.g., “add this category” or “remove that relation”) without shuttling full arrays back and forth.
How?
LocalAPI
update
got a newoperations
property where atomic operations forhasMany: true
relationship fields can be submitted. Atomic operations are resolved inupdateById
/update
internally before collection- and field-level hooks are executed so existing hooks and validations are not affected by this change.Implementation Notes:
$push
and$remove
for relationship fields only. Similar operations such as$inc
might be added later.data
andoperations
Considerations
This change proposes introducing a dedicated
operations
property to carry atomic updates such as$remove
,$push
, and$inc
. Keeping these mutation operators separate fromdata
preserves the existing semantics ofdata
as pure payload and avoids breaking changes - especially hook implementations - that currently assumedata
contains only field data. From a typing perspective, operations can be described with a focused, well-scoped type that models update operators without altering the shape ofdata
.An obvious alternative would be to allow these operators directly within
data
. While superficially simpler, that approach risks breaking existing hooks that are not prepared to receive operator syntax mixed with collection data. It would also force a significant expansion in type complexity: we’d need recursive types that permit operators at every nesting level, which would likely cascade into type errors for downstream implementations and reduce overall readability. For these reasons, the proposal favors a new operations field, keeping backward compatibility intact and making both validation and type definitions more tractable.