Skip to content

Commit e8ef993

Browse files
author
michaelkrone
committed
Merge branch 'develop'
2 parents 7ab4fc6 + 6c26959 commit e8ef993

35 files changed

+4197
-284
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
* _2.2.0_
2+
* Support for adding and removing child data. Adding `AddChildData` and `RemoveChildData` actions and `addChildData` and `removeChildData` action creators.
3+
14
* _2.1.0_
2-
entitiesProjector takes an optional array of id strings ([@juanpmarin](https://github.com/juanpmarin)) Closes #18
5+
* entitiesProjector takes an optional array of id strings ([@juanpmarin](https://github.com/juanpmarin)) Closes #18
36

47
* _2.0.0_
58
* Serialization support for actions. _Details:_ The normalization of entities is now perfomed in the action constructor. Previously it was handled by the reducer. As ([@PachowStudios](https://github.com/PachowStudios)) pointed out in Issue #16, ngrx-normalizr actions were not serializable. This could raise issues with other redux/ngrx libraries. The normalizr `schema.Entity` is not part of the action payload anymore, hence the interfaces for describing the payload have changed and the action constructor does no longer take the payload itself as an argument. As long as you did not type any action parameters in your code or dispatched actions directly with a simle pojo by using the exported action type names, you should have no problem updating, since the arity/keys of the constructor API did not change - see Breaking Changes. Closes #16

README.md

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55

66
> Managing [normalized state](https://redux.js.org/docs/recipes/reducers/NormalizingStateShape.html) in [ngrx](https://github.com/ngrx/platform) applications, transparently.
77
8-
98
This package provides a set of actions, reducers and selectors for handling normalization and denormalization of state data **transparently**.
109
*ngrx-normalizr* uses [normalizr](https://github.com/paularmstrong/normalizr) for [normalizing](https://github.com/paularmstrong/normalizr/blob/master/docs/api.md#normalizedata-schema) and [denormalizing](https://github.com/paularmstrong/normalizr/blob/master/docs/api.md#denormalizeinput-schema-entities) data. All normalization and denormalization
1110
is defined by the use of [normalizr schemas](https://github.com/paularmstrong/normalizr/blob/master/docs/api.md#schema), since that's the way normalizr works. This enables selectors to use a transparent and powerful projection of state data.
1211

12+
> Releases will be published from the [`master`](https://github.com/michaelkrone/ngrx-normalizr/tree/master) branch. [Go there](https://github.com/michaelkrone/ngrx-normalizr/tree/master) for documentation that aligns with the npm repo version.
13+
1314
## Installation
1415
To install this package:
1516
```sh
@@ -81,7 +82,7 @@ Actions are used to set data in - and remove data from - the normalized store.
8182

8283
### Adding data
8384
To add data and automatically normalize it, *ngrx-normalizr* provides a `AddData` action. This action takes an object with `data` and `schema` as an argument. Entities are identified by their id attribute set in the passed schema.
84-
Existing entities will be overwritten by updated data, new entities will be added to the store.
85+
Existing entities will be overwritten by updated data, new entities will be added to the store. For adding related childs, an `AddChildData` action is provided.
8586

8687
###### Using `AddData` in an effect
8788
```javascript
@@ -98,6 +99,29 @@ loadEffect$ = this.actions$
9899
.catch(err => Observable.of(new LoadFail(err)));
99100
```
100101

102+
#### Adding child data
103+
Adding a related child data to a parent entity can be done with the `AddChildData` action. Note that for this to work, the relation has to be defined in the schema. The action takes a couple of arguments which need to be given in an object:
104+
105+
* `data`: Array of child entities to add
106+
* `childSchema`The `schema.Entity` of the child entity
107+
* `parentSchema`: The `schema.Entity` of the parent entity
108+
* `parentId`: The id of the entity to add child references to
109+
110+
###### Using `AddChildData` in an effect
111+
```javascript
112+
@Effect()
113+
addPetEffect$ = this.actions$
114+
.ofType(ADD_PET)
115+
.switchMap(action => this.http.post('https://example.com/api/pets'))
116+
.mergeMap((data: Pet[]) => [
117+
// dispatch to add data to the store
118+
new AddChildData<Pet>({ data, childSchema, parentSchema, parentId }),
119+
// dispatch to inform feature reducer
120+
new AddPetSuccess(data)
121+
])
122+
.catch(err => Observable.of(new LoadFail(err)));
123+
```
124+
101125
### Setting data
102126
The `SetData` action will overwrite all entities for a given schema with the normalized entities of the `data` property of the action constructor argument. This action can
103127
be used for resetting entity state data instead of adding and updating existing entities.
@@ -120,12 +144,36 @@ removeEffect$ = this.actions$
120144
])
121145
.catch(err => Observable.of(new RemoveFail(err)));
122146
```
147+
#### Removing child data
148+
Removing a child entity which is 1:1 related to a parent entity can be done with the `RemoveChildData` action. Note that for this to work, the relation has to be defined in the schema. The action takes a couple of arguments which need to be given in an object:
149+
150+
* `id`: Id of the child entity that should be removed
151+
* `childSchema`The `schema.Entity` of the child entity
152+
* `parentSchema`: The `schema.Entity` of the parent entity
153+
* `parentId`: The id of the entity to remove child references from
154+
155+
###### Using `AddChildData` in an effect
156+
```javascript
157+
@Effect()
158+
removePetEffect$ = this.actions$
159+
.ofType(REMOVE_PET)
160+
.switchMap(action => this.http.remove(`https://example.com/api/pets/${action.payload.id}`))
161+
.mergeMap((data: Pet) => [
162+
// dispatch to add data to the store
163+
new RemoveChildData({ id: data.id, childSchema, parentSchema, parentId }),
164+
// dispatch to inform feature reducer
165+
new RemovePetSuccess(data)
166+
])
167+
.catch(err => Observable.of(new LoadFail(err)));
168+
```
123169

124170
### Action creators
125171
For convenience, *ngrx-normalizr* provides an `actionCreators` function which will return an object with following schema bound action creators:
126172
* `setData` - `(data: T[]) => SetData<T>`
127173
* `addData` - `(data: T[]) => AddData<T>`
174+
* `addChildData<C>` - `(data: C[], childSchema: schema.Entity, parentId: string) => AddChildData`
128175
* `removeData` - `(id: string, removeChildren?: SchemaMap) => RemoveData`
176+
* `removeChildData` - `(id: string, childSchema: schema.Entity, parentId: string) => RemoveChildData`
129177

130178
Action creators could be exported along whith other feature actions:
131179
```javascript
@@ -173,7 +221,7 @@ const schemaSelectors = createSchemaSelectors<User>(userSchema);
173221
`createSchemaSelectors` will return schema bound selectors (instance of `SchemaSelectors`):
174222
* `getEntities` - ` MemoizedSelector<{}, T[]>` Returns all denormalized entities for the schema
175223
* `getNormalizedEntities` - `MemoizedSelector<any, EntityMap>` Returns all normalized (raw) state entities of every schema (the whole entities state)
176-
* `entitiesProjector` - `(entities: {}, ids?: Array<string>) => T[]` Projector function for denormalizing a the set of normalized entities to an denormalized entity array
224+
* `entitiesProjector` - `(entities: {}, ids?: Array<string>) => T[]` Projector function for denormalizing a the set of normalized entities to an denormalized entity array. If no `ids` are given, all entities will be denormalized.
177225
* `entityProjector` - `(entities: {}, id: string) => T` Projector function for denormalizing a single normalized entity with the given id
178226

179227
You might create several selectors with several schemas, i.e. a *listView* schema, which only denormalizes the data used in the list

docs/assets/js/search.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)