Skip to content

Commit 487e0aa

Browse files
authored
Merge pull request #1460 from jjspace/setOne-setMany-docs
Docs: add `setOne` and `setMany`, describe `add` vs `set` vs `upsert`
2 parents 8a5d4bc + e00e1ce commit 487e0aa

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

docs/api/createEntityAdapter.mdx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,11 @@ export interface EntityAdapter<T> extends EntityStateAdapter<T> {
203203

204204
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:
205205

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.
209211
- `removeOne`: accepts a single entity ID value, and removes the entity with that ID if it exists.
210212
- `removeMany`: accepts an array of entity ID values, and removes each entity with those IDs if they exist.
211213
- `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
214216
- `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.
215217
- `upsertMany`: accepts an array of entities or an object in the shape of `Record<EntityId, T>` that will be shallowly upserted.
216218

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.
226+
227+
:::
228+
229+
217230
Each method has a signature that looks like:
218231

219232
```ts no-transpile

0 commit comments

Comments
 (0)