Skip to content

Commit 8883417

Browse files
authored
Serializable actions (#17)
* Closes #16 * feat: WIP serializable actions, bump normalizr version, updated docs * refactor: assert that removeChildren is null (was an empty object) for invalid key value pairs * test: ignore not existing else path for 100% coverage * docs: add 2.0.0 changelog * feat: reuse payload interface types for action config * docs(changelog): add breaking changes description * docs: update action constructor description
1 parent 1e72a16 commit 8883417

25 files changed

+1006
-220
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
* _2.0.0_
2+
* 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)
3+
4+
* **Breaking Changes**
5+
* Action constructor parameters changed from `NormalizeDataPayload` to `NormalizeActionConfig` and from `NormalizeRemovePayload` to `NormalizeRemoveActionConfig`
6+
* Action `payload` property types changed from `NormalizeDataPayload` to `NormalizeActionPayload` and from `NormalizeRemovePayload` to `NormalizeRemoveActionPayload`
7+
* (might break) Internal used interface `SchemaPayload` replaced by `NormalizeActionSchemaConfig`
8+
19
* _1.0.4_
210
* exporting `SchemaSelectors` interface ([@JSantha](https://github.com/JSantha))
311

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Build Status](https://travis-ci.org/michaelkrone/ngrx-normalizr.svg?branch=master)](https://travis-ci.org/michaelkrone/ngrx-normalizr)
44
![AOT compatible](https://img.shields.io/badge/aot-compatible-blue.svg)
55

6-
> Managing [normalized state](http://redux.js.org/docs/recipes/reducers/NormalizingStateShape.html) in [ngrx](https://github.com/ngrx/platform) applications, transparently.
6+
> Managing [normalized state](https://redux.js.org/docs/recipes/reducers/NormalizingStateShape.html) in [ngrx](https://github.com/ngrx/platform) applications, transparently.
77
88

99
This package provides a set of actions, reducers and selectors for handling normalization and denormalization of state data **transparently**.
@@ -80,7 +80,7 @@ export const userSchema = new schema.Entity('users', { pets: [petSchema] });
8080
Actions are used to set data in - and remove data from - the normalized store.
8181

8282
### Adding data
83-
To add data and automatically normalize it, *ngrx-normalizr* provides a `AddData` action. This action takes an object with `data` and `schema` as a payload. Entities are identified by their id attribute set in the passed schema.
83+
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.
8484
Existing entities will be overwritten by updated data, new entities will be added to the store.
8585

8686
###### Using `AddData` in an effect
@@ -99,19 +99,19 @@ loadEffect$ = this.actions$
9999
```
100100

101101
### Setting data
102-
The `SetData` action will overwrite all entities for a given schema with the normalized entities of the `data` property of the action payload. This action can
102+
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
103103
be used for resetting entity state data instead of adding and updating existing entities.
104104

105105
### Removing data
106106
To remove data, *ngrx-normalizr* provides a `RemoveData` action.
107-
This action takes an object with `id`, `schema` and an optional `removeChildren` property as payload. The schema entity with the given id will be removed. If `removeChildren` is a map of the schema key mapped to an object property, all referenced child entities will also be removed from the store. This is handy for 1:1 relations, since only removing the parent entity may leave unused child entities in the store.
107+
This action takes an object with `id`, `schema` and an optional `removeChildren` property as constructor argument. The schema entity with the given id will be removed. If `removeChildren` is a map of the schema key mapped to an object property, all referenced child entities will also be removed from the store. This is handy for 1:1 relations, since only removing the parent entity may leave unused child entities in the store.
108108

109109
###### Using `RemoveData` in an effect
110110
```javascript
111111
@Effect()
112112
removeEffect$ = this.actions$
113113
.ofType(REMOVE)
114-
.switchMap((action: Remove) => this.http.delete(`http://example.com/api/user/${action.payload}`))
114+
.switchMap((action: Remove) => this.http.delete(`https://example.com/api/user/${action.payload.id}`))
115115
.mergeMap(result => [
116116
// dispatch to remove data from the store
117117
new RemoveData({ id: result.id, schema, removeChildren: { pets: 'pets' } }),
@@ -146,7 +146,7 @@ import { removeUserData } from '../actions';
146146
@Effect()
147147
removeEffect$ = this.actions$
148148
.ofType(REMOVE)
149-
.switchMap((action: Remove) => this.http.delete(`http://example.com/api/user/${action.payload}`))
149+
.switchMap((action: Remove) => this.http.delete(`https://example.com/api/user/${action.payload.id}`))
150150
.mergeMap(result => [
151151
// dispatch to remove data from the store
152152
removeUserData(id: result.id, { pets: 'pets' }),

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.

docs/classes/_actions_normalize_.adddata.html

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ <h2>Constructors</h2>
125125
<a name="constructor" class="tsd-anchor"></a>
126126
<h3>constructor</h3>
127127
<ul class="tsd-signatures tsd-kind-constructor tsd-parent-kind-class tsd-is-external">
128-
<li class="tsd-signature tsd-kind-icon">new <wbr>Add<wbr>Data<span class="tsd-signature-symbol">(</span>payload<span class="tsd-signature-symbol">: </span><a href="../interfaces/_actions_normalize_.normalizedatapayload.html" class="tsd-signature-type">NormalizeDataPayload</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">T</span><span class="tsd-signature-symbol">&gt;</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><a href="_actions_normalize_.adddata.html" class="tsd-signature-type">AddData</a></li>
128+
<li class="tsd-signature tsd-kind-icon">new <wbr>Add<wbr>Data<span class="tsd-signature-symbol">(</span>config<span class="tsd-signature-symbol">: </span><a href="../interfaces/_actions_normalize_.normalizeactionconfig.html" class="tsd-signature-type">NormalizeActionConfig</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">T</span><span class="tsd-signature-symbol">&gt;</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><a href="_actions_normalize_.adddata.html" class="tsd-signature-type">AddData</a></li>
129129
</ul>
130130
<ul class="tsd-descriptions">
131131
<li class="tsd-description">
132132
<aside class="tsd-sources">
133133
<ul>
134-
<li>Defined in <a href="https://github.com/michaelkrone/ngrx-normalizr/blob/1eda8a0/src/actions/normalize.ts#L124">actions/normalize.ts:124</a></li>
134+
<li>Defined in <a href="https://github.com/michaelkrone/ngrx-normalizr/blob/ab53558/src/actions/normalize.ts#L178">actions/normalize.ts:178</a></li>
135135
</ul>
136136
</aside>
137137
<div class="tsd-comment tsd-typography">
@@ -142,10 +142,10 @@ <h3>constructor</h3>
142142
<h4 class="tsd-parameters-title">Parameters</h4>
143143
<ul class="tsd-parameters">
144144
<li>
145-
<h5>payload: <a href="../interfaces/_actions_normalize_.normalizedatapayload.html" class="tsd-signature-type">NormalizeDataPayload</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">T</span><span class="tsd-signature-symbol">&gt;</span></h5>
145+
<h5>config: <a href="../interfaces/_actions_normalize_.normalizeactionconfig.html" class="tsd-signature-type">NormalizeActionConfig</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">T</span><span class="tsd-signature-symbol">&gt;</span></h5>
146146
<div class="tsd-comment tsd-typography">
147147
<div class="lead">
148-
<p>The action payload used in the reducer</p>
148+
<p>The action config object</p>
149149
</div>
150150
</div>
151151
</li>
@@ -160,15 +160,16 @@ <h2>Properties</h2>
160160
<section class="tsd-panel tsd-member tsd-kind-property tsd-parent-kind-class tsd-is-external">
161161
<a name="payload" class="tsd-anchor"></a>
162162
<h3>payload</h3>
163-
<div class="tsd-signature tsd-kind-icon">payload<span class="tsd-signature-symbol">:</span> <a href="../interfaces/_actions_normalize_.normalizedatapayload.html" class="tsd-signature-type">NormalizeDataPayload</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">T</span><span class="tsd-signature-symbol">&gt;</span></div>
163+
<div class="tsd-signature tsd-kind-icon">payload<span class="tsd-signature-symbol">:</span> <a href="../interfaces/_actions_normalize_.normalizeactionpayload.html" class="tsd-signature-type">NormalizeActionPayload</a></div>
164164
<aside class="tsd-sources">
165165
<ul>
166-
<li>Defined in <a href="https://github.com/michaelkrone/ngrx-normalizr/blob/1eda8a0/src/actions/normalize.ts#L130">actions/normalize.ts:130</a></li>
166+
<li>Defined in <a href="https://github.com/michaelkrone/ngrx-normalizr/blob/ab53558/src/actions/normalize.ts#L178">actions/normalize.ts:178</a></li>
167167
</ul>
168168
</aside>
169169
<div class="tsd-comment tsd-typography">
170170
<div class="lead">
171-
<p>The action payload used in the reducer</p>
171+
<p>The payload will be an object of the normalized entity map as <code>entities</code>
172+
and the original sorted id&#39;s as an array in the <code>result</code> property.</p>
172173
</div>
173174
</div>
174175
</section>
@@ -178,7 +179,7 @@ <h3>type</h3>
178179
<div class="tsd-signature tsd-kind-icon">type<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol"> =&nbsp;NormalizeActionTypes.ADD_DATA</span></div>
179180
<aside class="tsd-sources">
180181
<ul>
181-
<li>Defined in <a href="https://github.com/michaelkrone/ngrx-normalizr/blob/1eda8a0/src/actions/normalize.ts#L124">actions/normalize.ts:124</a></li>
182+
<li>Defined in <a href="https://github.com/michaelkrone/ngrx-normalizr/blob/ab53558/src/actions/normalize.ts#L172">actions/normalize.ts:172</a></li>
182183
</ul>
183184
</aside>
184185
<div class="tsd-comment tsd-typography">
@@ -242,19 +243,25 @@ <h3>type</h3>
242243
<a href="_actions_normalize_.setdata.html" class="tsd-kind-icon">Set<wbr>Data</a>
243244
</li>
244245
<li class=" tsd-kind-interface tsd-parent-kind-external-module tsd-has-type-parameter tsd-is-external">
245-
<a href="../interfaces/_actions_normalize_.normalizeactioncreators.html" class="tsd-kind-icon">Normalize<wbr>Action<wbr>Creators</a>
246+
<a href="../interfaces/_actions_normalize_.normalizeactionconfig.html" class="tsd-kind-icon">Normalize<wbr>Action<wbr>Config</a>
246247
</li>
247248
<li class=" tsd-kind-interface tsd-parent-kind-external-module tsd-has-type-parameter tsd-is-external">
248-
<a href="../interfaces/_actions_normalize_.normalizedatapayload.html" class="tsd-kind-icon">Normalize<wbr>Data<wbr>Payload</a>
249+
<a href="../interfaces/_actions_normalize_.normalizeactioncreators.html" class="tsd-kind-icon">Normalize<wbr>Action<wbr>Creators</a>
249250
</li>
250251
<li class=" tsd-kind-interface tsd-parent-kind-external-module tsd-is-external">
251-
<a href="../interfaces/_actions_normalize_.normalizeremovepayload.html" class="tsd-kind-icon">Normalize<wbr>Remove<wbr>Payload</a>
252+
<a href="../interfaces/_actions_normalize_.normalizeactionpayload.html" class="tsd-kind-icon">Normalize<wbr>Action<wbr>Payload</a>
252253
</li>
253254
<li class=" tsd-kind-interface tsd-parent-kind-external-module tsd-is-external">
254-
<a href="../interfaces/_actions_normalize_.schemamap.html" class="tsd-kind-icon">Schema<wbr>Map</a>
255+
<a href="../interfaces/_actions_normalize_.normalizeactionschemaconfig.html" class="tsd-kind-icon">Normalize<wbr>Action<wbr>Schema<wbr>Config</a>
256+
</li>
257+
<li class=" tsd-kind-interface tsd-parent-kind-external-module tsd-is-external">
258+
<a href="../interfaces/_actions_normalize_.normalizeremoveactionconfig.html" class="tsd-kind-icon">Normalize<wbr>Remove<wbr>Action<wbr>Config</a>
259+
</li>
260+
<li class=" tsd-kind-interface tsd-parent-kind-external-module tsd-is-external">
261+
<a href="../interfaces/_actions_normalize_.normalizeremoveactionpayload.html" class="tsd-kind-icon">Normalize<wbr>Remove<wbr>Action<wbr>Payload</a>
255262
</li>
256263
<li class=" tsd-kind-interface tsd-parent-kind-external-module tsd-is-external">
257-
<a href="../interfaces/_actions_normalize_.schemapayload.html" class="tsd-kind-icon">Schema<wbr>Payload</a>
264+
<a href="../interfaces/_actions_normalize_.schemamap.html" class="tsd-kind-icon">Schema<wbr>Map</a>
258265
</li>
259266
<li class=" tsd-kind-variable tsd-parent-kind-external-module tsd-is-external tsd-is-not-exported">
260267
<a href="../modules/_actions_normalize_.html#action_namespace" class="tsd-kind-icon">ACTION_<wbr>NAMESPACE</a>

0 commit comments

Comments
 (0)