Skip to content

Commit 904d99a

Browse files
committed
feat(core): factories for factory selectors for ngrx 12+ #694
1 parent 326d6b2 commit 904d99a

File tree

20 files changed

+270
-46
lines changed

20 files changed

+270
-46
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ dist
33
node_modules
44
test-reports
55
tmp
6+
7+
*.iml

.husky/commit-msg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
npx commitlint --edit $1

.husky/post-commit

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
git update-index --again

.husky/pre-commit

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
npx lint-staged
5+
npm run lint
6+
npm run test
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: toFactorySelector
3+
description: Information about toFactorySelector function and how to create a factory selector function
4+
sidebar_label: toFactorySelector
5+
---
6+
7+
`toFactorySelector` helps to create a factory selector function from a root selector.
8+
In this case, the produced selector can be passed directly to NGRX `store.select`.
9+
10+
This function is useful for NGRX v12 and younger.
11+
12+
```ts
13+
export class MyComponent {
14+
public readonly users$: Observable<User>;
15+
16+
private readonly selectUser =
17+
toFactorySelector(
18+
rootUser(
19+
relUserCompany(
20+
relCompanyAddress(),
21+
),
22+
),
23+
);
24+
25+
constructor(private store: Store) {
26+
this.users$ = this.store.select(
27+
// let's select current user
28+
this.selectUser(
29+
selectCurrentUserId,
30+
),
31+
);
32+
}
33+
}
34+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
title: toStaticSelector
3+
description: Information about toStaticSelector function and how to bind param to a root selector
4+
sidebar_label: toStaticSelector
5+
---
6+
7+
`toStaticSelector` helps to create a selector function from a root selector.
8+
Its behavior very similar to [`toFactorySelector`](./to-factory-selector.md),
9+
with the difference that the passed params are static and cannot be changed.
10+
11+
This function is useful for NGRX v12 and younger.
12+
13+
```ts
14+
export class MyComponent {
15+
public readonly users$: Observable<User>;
16+
17+
private readonly selectCurrentUser =
18+
toStaticSelector(
19+
rootUser(
20+
relUserCompany(
21+
relCompanyAddress(),
22+
),
23+
),
24+
selectCurrentUserId,
25+
);
26+
27+
constructor(private store: Store) {
28+
this.users$ = this.store.select(
29+
this.selectCurrentUser,
30+
);
31+
}
32+
}
33+
```

docs/articles/guide/ngrx-data.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,16 @@ export class MyComponent {
110110
userSelectors: UserSelectorService,
111111
) {
112112
this.user$ = store.select(
113-
userSelectors.selectUser,
114-
'1',
113+
toStaticSelector(
114+
userSelectors.selectUser,
115+
'1',
116+
),
115117
);
116118
this.users$ = store.select(
117-
userSelectors.selectUsers,
118-
['1', '2'],
119+
toStaticSelector(
120+
userSelectors.selectUsers,
121+
['1', '2'],
122+
),
119123
);
120124
}
121125
}

docs/articles/guide/quick.md

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ the only requirement is the `Dictionary` (a regular object).
5555
The next step is to define functions which select the state of an entity.
5656
In this library, they are called [**entity state selectors**](../api/core/entity-state-selector.md).
5757

58-
```ts
59-
// Redux
58+
```ts title="Redux"
6059
export const selectUserState = state => state.users;
6160
export const selectCompanyState = state => state.companies;
6261
// `stateKeys` function helps in case of different names of the properties.
@@ -67,8 +66,7 @@ export const selectAddressState = stateKeys(
6766
);
6867
```
6968

70-
```ts
71-
// NGRX
69+
```ts title="NGRX"
7270
export const selectUserState =
7371
createFeatureSelector<fromUser.State>(
7472
'users',
@@ -165,8 +163,7 @@ In case of arrays, such as `company.staff`, there is [`childrenEntitiesSelector`
165163
Now, let's go to a component where we want to select a user with relationships,
166164
and create a **root selector** via the factories there:
167165

168-
```ts
169-
// Redux
166+
```ts title="Redux"
170167
const selectUser = rootUser(
171168
relUserCompany(
172169
relCompanyAddress(),
@@ -182,8 +179,32 @@ const mapStateToProps = state => {
182179
export default connect(mapStateToProps)(MyComponent);
183180
```
184181

185-
```ts
186-
// NGRX
182+
```ts title="NGRX 12 and younger"
183+
export class MyComponent {
184+
public readonly users$: Observable<User>;
185+
186+
private readonly selectUser =
187+
rootUser(
188+
relUserCompany(
189+
relCompanyAddress(),
190+
),
191+
);
192+
193+
constructor(private store: Store) {
194+
this.users$ = this.store.select(
195+
// toStaticSelector should be used
196+
// to create a selector for v12
197+
toStaticSelector(
198+
this.selectUser,
199+
// '1' is the id of user
200+
'1',
201+
),
202+
);
203+
}
204+
}
205+
```
206+
207+
```ts title="NGRX 11 and older"
187208
export class MyComponent {
188209
public readonly users$: Observable<User>;
189210

@@ -205,13 +226,20 @@ export class MyComponent {
205226

206227
Of course, instead of a hardcoded id like `1`, we can pass another **selector, that selects ids** from the state.
207228

208-
```ts
209-
// Redux
229+
```ts title="Redux"
210230
selectUser(state, selectCurrentUserId);
211231
```
212232

213-
```ts
214-
// NGRX
233+
```ts title="NGRX 12 and younger"
234+
this.store.select(
235+
toStaticSelector(
236+
this.selectUser,
237+
selectCurrentUserId,
238+
),
239+
);
240+
```
241+
242+
```ts title="NGRX 11 and older"
215243
this.store.select(this.selectUser, selectCurrentUserId);
216244
```
217245

@@ -234,13 +262,20 @@ const selectUsers = rootEntities(selectUser);
234262

235263
Now we can use `selectUsers` in our components, but instead of an id, it requires an array of them.
236264

237-
```ts
238-
// Redux
265+
```ts title="Redux"
239266
selectUsers(state, ['1', '2']);
240267
```
241268

242-
```ts
243-
// NGRX
269+
```ts title="NGRX 12 and younger"
270+
this.store.select(
271+
toStaticSelector(
272+
this.selectUsers,
273+
['1', '2'],
274+
),
275+
);
276+
```
277+
278+
```ts title="NGRX and older"
244279
this.store.select(this.selectUsers, ['1', '2']);
245280
```
246281

docs/articles/index.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ The current version of `ngrx-entity-relationship` has been tested and can be use
1919

2020
- Redux 4, React Redux 7, **try it live on [StackBlitz](https://stackblitz.com/edit/ngrx-entity-relationship-react?file=src/MyComponent.tsx)
2121
or [CodeSandbox](https://codesandbox.io/s/github/satanTime/ngrx-entity-relationship-react?file=/src/MyComponent.tsx)**
22-
23-
- NGRX 10, **try it live on [StackBlitz](https://stackblitz.com/github/satanTime/ngrx-entity-relationship-angular?file=src/app/app.component.ts)
22+
- NGRX 12, **try it live on [StackBlitz](https://stackblitz.com/github/satanTime/ngrx-entity-relationship-angular?file=src/app/app.component.ts)
2423
or [CodeSandbox](https://codesandbox.io/s/github/satanTime/ngrx-entity-relationship-angular?file=/src/app/app.component.ts)**
24+
- NGRX 11
25+
- NGRX 10
2526
- NGRX 9
2627
- NGRX 8
2728
- NGRX 7

docs/sidebars.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ module.exports = {
5151
'api/core/childentityselector-function',
5252
'api/core/childrenentities-function',
5353
'api/core/childrenentitiesselector-function',
54+
'api/core/to-factory-selector',
55+
'api/core/to-static-selector',
5456
],
5557
},
5658
{

0 commit comments

Comments
 (0)