createEntityAdapter
#
#
OverviewA function that generates a set of prebuilt reducers and selectors for performing CRUD operations on a normalized state structure containing instances of a particular type of data object. These reducer functions may be passed as case reducers to createReducer
and createSlice
. They may also be used as "mutating" helper functions inside of createReducer
and createSlice
.
This API was ported from the @ngrx/entity
library created by the NgRx maintainers, but has been significantly modified for use with Redux Toolkit. We'd like to thank the NgRx team for originally creating this API and allowing us to port and adapt it for our needs.
Note: The term "Entity" is used to refer to a unique type of data object in an application. For example, in a blogging application, you might have
User
,Post
, andComment
data objects, with many instances of each being stored in the client and persisted on the server.User
is an "entity" - a unique type of data object that the application uses. Each unique instance of an entity is assumed to have a unique ID value in a specific field.As with all Redux logic, only plain JS objects and arrays should be passed in to the store - no class instances!
For purposes of this reference, we will use
Entity
to refer to the specific data type that is being managed by a copy of the reducer logic in a specific portion of the Redux state tree, andentity
to refer to a single instance of that type. Example: instate.users
,Entity
would refer to theUser
type, andstate.users.entities[123]
would be a singleentity
.
The methods generated by createEntityAdapter
will all manipulate an "entity state" structure that looks like:
createEntityAdapter
may be called multiple times in an application. If you are using it with plain JavaScript, you may be able to reuse a single adapter definition with multiple entity types if they're similar enough (such as all having an entity.id
field). For TypeScript usage, you will need to call createEntityAdapter
a separate time for each distinct Entity
type, so that the type definitions are inferred correctly.
Sample usage:
- TypeScript
- JavaScript
#
ParameterscreateEntityAdapter
accepts a single options object parameter, with two optional fields inside.
selectId
#
A function that accepts a single Entity
instance, and returns the value of whatever unique ID field is inside. If not provided, the default implementation is entity => entity.id
. If your Entity
type keeps its unique ID values in a field other than entity.id
, you must provide a selectId
function.
sortComparer
#
A callback function that accepts two Entity
instances, and should return a standard Array.sort()
numeric result (1, 0, -1) to indicate their relative order for sorting.
If provided, the state.ids
array will be kept in sorted order based on comparisons of the entity objects, so that mapping over the IDs array to retrieve entities by ID should result in a sorted array of entities.
If not provided, the state.ids
array will not be sorted, and no guarantees are made about the ordering. In other words, state.ids
can be expected to behave like a standard Javascript array.
#
Return ValueA "entity adapter" instance. An entity adapter is a plain JS object (not a class) containing the generated reducer functions, the original provided selectId
and sortComparer
callbacks, a method to generate an initial "entity state" value, and functions to generate a set of globalized and non-globalized memoized selector functions for this entity type.
The adapter instance will include the following methods (additional referenced TypeScript types included):
#
CRUD FunctionsThe 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:
addOne
: accepts a single entity, and adds it.addMany
: accepts an array of entities or an object in the shape ofRecord<EntityId, T>
, and adds them.setAll
: accepts an array of entities or an object in the shape ofRecord<EntityId, T>
, and replaces the existing entity contents with the values in the array.removeOne
: accepts a single entity ID value, and removes the entity with that ID if it exists.removeMany
: accepts an array of entity ID values, and removes each entity with those IDs if they exist.removeAll
: removes all entities from the entity state object.updateOne
: accepts an "update object" containing an entity ID and an object containing one or more new field values to update inside achanges
field, and performs a shallow update on the corresponding entity.updateMany
: accepts an array of update objects, and performs shallow updates on all corresponding entities.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.upsertMany
: accepts an array of entities or an object in the shape ofRecord<EntityId, T>
that will be shallowly upserted.
Each method has a signature that looks like:
In other words, they accept a state that looks like {ids: [], entities: {}}
, and calculate and return a new state.
These CRUD methods may be used in multiple ways:
- They may be passed as case reducers directly to
createReducer
andcreateSlice
. - They may be used as "mutating" helper methods when called manually, such as a separate hand-written call to
addOne()
inside of an existing case reducer, if thestate
argument is actually an ImmerDraft
value. - They may be used as immutable update methods when called manually, if the
state
argument is actually a plain JS object or array.
Note: These methods do not have corresponding Redux actions created - they are just standalone reducers / update logic. It is entirely up to you to decide where and how to use these methods! Most of the time, you will want to pass them to
createSlice
or use them inside another reducer.
Each method will check to see if the state
argument is an Immer Draft
or not. If it is a draft, the method will assume that it's safe to continue mutating that draft further. If it is not a draft, the method will pass the plain JS value to Immer's createNextState()
, and return the immutably updated result value.
The argument
may be either a plain value (such as a single Entity
object for addOne()
or an Entity[]
array for addMany()
, or a PayloadAction
action object with that same value as action.payload
. This enables using them as both helper functions and reducers.
Note on shallow updates:
updateOne
,updateMany
,upsertOne
, andupsertMany
only perform shallow updates in a mutable manner. This means that if your update/upsert consists of an object that includes nested properties, the value of the incoming change will overwrite the entire existing nested object. This may be unintended behavior for your application. As a general rule, these methods are best used with normalized data that do not have nested properties.
getInitialState
#
Returns a new entity state object like {ids: [], entities: {}}
.
It accepts an optional object as an argument. The fields in that object will be merged into the returned initial state value. For example, perhaps you want your slice to also track some loading state:
#
Selector FunctionsThe entity adapter will contain a getSelectors()
function that returns a set of selectors that know how to read the contents of an entity state object:
selectIds
: returns thestate.ids
array.selectEntities
: returns thestate.entities
lookup table.selectAll
: maps over thestate.ids
array, and returns an array of entities in the same order.selectTotal
: returns the total number of entities being stored in this state.selectById
: given the state and an entity ID, returns the entity with that ID orundefined
.
Each selector function will be created using the createSelector
function from Reselect, to enable memoizing calculation of the results.
Because selector functions are dependent on knowing where in the state tree this specific entity state object is kept, getSelectors()
can be called in two ways:
- If called without any arguments, it returns an "unglobalized" set of selector functions that assume their
state
argument is the actual entity state object to read from. - It may also be called with a selector function that accepts the entire Redux state tree and returns the correct entity state object.
For example, the entity state for a Book
type might be kept in the Redux state tree as state.books
. You can use getSelectors()
to read from that state in two ways:
#
Notes#
Applying Multiple UpdatesIf updateMany()
is called with multiple updates targeted to the same ID, they will be merged into a single update, with later updates overwriting the earlier ones.
For both updateOne()
and updateMany()
, changing the ID of one existing entity to match the ID of a second existing entity will cause the first to replace the second completely.
#
ExamplesExercising several of the CRUD methods and selectors: