Skip to content

Commit

Permalink
feat: meta reducer and actions
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed May 30, 2020
1 parent 5543b4f commit 91cc581
Show file tree
Hide file tree
Showing 59 changed files with 4,667 additions and 1,312 deletions.
14 changes: 13 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,19 @@ jobs:
- ./e2e/angular9/node_modules
- run:
name: Spreading Build
command: npm run build && npm run s:a
command: npm run build && npm run s:a && npm run s:test
- run:
name: Angular 6 UT
command: npm run test:a6
- run:
name: Angular 7 UT
command: npm run test:a7
- run:
name: Angular 8 UT
command: npm run test:a8
- run:
name: Angular 9 UT
command: npm run test:a9
- run:
name: Angular 6 E2E
command: npm run e2e:a6
Expand Down
130 changes: 130 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,136 @@ In case of relationship functions there are two more keys
* `keyId` - a name of the keyId field.
* `keyValue` - a name of the keyValue field.

## NGRX Store integration

All selectors can be used to update the store with response data.

For that `ngrxEntityRelationshipReducer` should be added as a meta reducer to the root import:
```typescript
StoreModule.forRoot({/* ... */}, {
metaReducers: [
// ...
ngrxEntityRelationshipReducer, // <- add this
],
})
```

After that `reduceFlat` and `reduceGraph` can be used.

### ReduceFlat / reduceFlat action

This action helps to add to store data from a flat response.

Imagine a backend returns the next flat shape:
```json
{
"users": [
{
"id": "1",
"firstName": "John",
"lastName": "Smith",
"companyId": "1"
}
],
"companies": [
{
"id": "1",
"name": "Magic",
"adminId": "2",
"addressId": "1"
}
],
"addresses": [
{
"id": "1",
"street": "Main st.",
"city": "Town",
"country": "Land"
}
]
}
```

There's a selector that fetches this data from the store:
```typescript
export const selectUser = rootEntity(
selectUserState,
relatedEntity(
selectCompanyState,
'companyId',
'company',
relatedEntity(
selectAddressState,
'addressId',
'address',
),
),
);
```

Then the store can be updated by dispatching the `reduceFlat` action:
```typescript
this.store.dispatch(reduceFlat({
data: response,
selector: selectUser,
}));
// or
this.store.dispatch(new ReduceFlat(response, selectUser));
```

### ReduceGraph / reduceGraph action

This action helps to add to store data from a graph response.

Imagine a backend returns the next flat shape of a user:
```json
{
"id": "1",
"firstName": "John",
"lastName": "Smith",
"companyId": "1",
"company": {
"id": "1",
"name": "Magic",
"adminId": "2",
"addressId": "1",
"address": {
"id": "1",
"street": "Main st.",
"city": "Town",
"country": "Land"
}
}
}
```

There's a selector that fetches this data from the store:
```typescript
export const selectUser = rootEntity(
selectUserState,
relatedEntity(
selectCompanyState,
'companyId',
'company',
relatedEntity(
selectAddressState,
'addressId',
'address',
),
),
);
```

Then the store can be updated by dispatching the `reduceGraph` action:
```typescript
this.store.dispatch(reduceGraph({
data: response,
selector: selectUser,
}));
// or
this.store.dispatch(new ReduceGraph(response, selectUser));
```

## Additional examples

Of course, we can select as many relationships as we want until we have a field with a related id.
Expand Down
39 changes: 1 addition & 38 deletions e2e/angular6/.gitignore
Original file line number Diff line number Diff line change
@@ -1,39 +1,2 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.

# compiled output
/dist
/tmp
/out-tsc

# dependencies
/node_modules

# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# misc
/.sass-cache
/connect.lock
/coverage
/libpeerconnection.log
npm-debug.log
yarn-error.log
testem.log
/typings

# System Files
.DS_Store
Thumbs.db
/src/test
Loading

0 comments on commit 91cc581

Please sign in to comment.