You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The primary content of an entity adapter is a set of generated reducer functions for adding, updating, and removing entity instances from an entity state object:
205
205
206
-
-`addOne`: accepts a single entity, and adds it.
207
-
-`addMany`: accepts an array of entities or an object in the shape of `Record<EntityId, T>`, and adds them.
208
-
-`setAll`: accepts an array of entities or an object in the shape of `Record<EntityId, T>`, and replaces the existing entity contents with the values in the array.
206
+
-`addOne`: accepts a single entity, and adds it if it's not already present.
207
+
-`addMany`: accepts an array of entities or an object in the shape of `Record<EntityId, T>`, and adds them if not already present.
208
+
-`setOne`: accepts a single entity and adds or replaces it
209
+
-`setMany`: accepts an array of entities or an an object in the shape of `Record<EntityId, T>`, and adds or replaces them.
210
+
-`setAll`: accepts an array of entities or an object in the shape of `Record<EntityId, T>`, and replaces all existing entities with the values in the array.
209
211
-`removeOne`: accepts a single entity ID value, and removes the entity with that ID if it exists.
210
212
-`removeMany`: accepts an array of entity ID values, and removes each entity with those IDs if they exist.
211
213
-`removeAll`: removes all entities from the entity state object.
@@ -214,6 +216,17 @@ The primary content of an entity adapter is a set of generated reducer functions
214
216
-`upsertOne`: accepts a single entity. If an entity with that ID exists, it will perform a shallow update and the specified fields will be merged into the existing entity, with any matching fields overwriting the existing values. If the entity does not exist, it will be added.
215
217
-`upsertMany`: accepts an array of entities or an object in the shape of `Record<EntityId, T>` that will be shallowly upserted.
216
218
219
+
:::info Should I add, set or upsert my entity?
220
+
221
+
All three options will insert _new_ entities into the list. However they differ in how they handle entities that already exist. If an entity **already exists**:
222
+
223
+
-`addOne` and `addMany` will do nothing with the new entity
224
+
-`setOne` and `setMany` will completely replace the old entity with the new one. This will also get rid of any properties on the entity that are not present in the new version of said entity.
225
+
-`upsertOne` and `upsertMany` will do a shallow copy to merge the old and new entities overwritng existing values, adding any that were not there and not touching properties not provided in the new entity.
0 commit comments