flux-entity
Utility library for the flux entity pattern
Motivation
A common pattern when working with a flux-based system such as Redux is to use a normalized registry of objects, keyed by some unique value.
Installation
With npm
npm install --save flux-entity
With yarn
yarn add flux-entity
API
// Returns a function for creating entitiescreateEntityFactory // Inserts values into an arrayinsertIntoArrayarray, ...values // Removes values from an arrayremoveFromArrayarray, ...values // Removes all values from an entityclearEntityentity // Returns a copy of the entity with all valuescopyEntityentity // Inserts values into an entityinsertIntoEntityentity, ...values // Removes values from an entityremoveFromEntityentity, ...values // Removes values from an entity by their idremoveFromEntityByIdentity, ...ids
Examples
Say we have an object type Person
.
Create an entity factory for type Person
. By supplying Person
, we can get excellent type safety with Typescript.
Create an entity, keyed by id
. This will now mean that each Person
is indexed with their id
as the uniqueness qualifier.
We now have an empty entity.
Insert a value into the entity.
insertIntoEntityentity,
The resulting entity state now looks like this.