EntityAdapter.upsert vs Custom Reducer for Nested State Merging
0 reputation · 12 Dec 2024, 15:26 UTC
State Preservation in Entity State Management
When managing complex entities with NgRx EntityAdapter, there is a requirement to update specific properties of an entity without losing existing nested collections or optional fields not present in the update payload.
The documented upsertOne and upsertMany methods replace the existing entity by ID. If the dispatched payload contains only a subset of the entity's properties, the remaining fields in the existing state are discarded rather than merged.
This creates a trade-off between using the built-in adapter methods for brevity and implementing a custom reducer to handle deep merging of entity properties to prevent partial state loss.
Technical Constraints
- Requirement to maintain immutable state updates.
- Need to preserve nested arrays or objects during partial updates.
- Assumption of NgRx 15+ environment.
Should the state merge logic be handled via a custom reducer wrapping the adapter, or is there a documented configuration for EntityAdapter to perform a shallow merge during upsert operations?