Skip to content

Commit 7acdc79

Browse files
brandonrobertsMikeRyanDev
authored andcommitted
feat(Schematics): Add upsert methods to entity blueprint (#809)
Closes #592
1 parent f31c898 commit 7acdc79

File tree

4 files changed

+70
-20
lines changed

4 files changed

+70
-20
lines changed

docs/entity/adapter.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ state if no changes were made.
8686
* `removeAll`: Clear entity collection
8787
* `updateOne`: Update one entity in the collection
8888
* `updateMany`: Update multiple entities in the collection
89+
* `upsertOne`: Add or Update one entity in the collection
90+
* `upsertMany`: Add or Update multiple entities in the collection
8991

9092
Usage:
9193

@@ -109,7 +111,9 @@ import { User } from './user.model';
109111
export enum UserActionTypes {
110112
LOAD_USERS = '[User] Load Users',
111113
ADD_USER = '[User] Add User',
114+
UPSERT_USER = '[User] Upsert User',
112115
ADD_USERS = '[User] Add Users',
116+
UPSERT_USERS = '[User] Upsert Users',
113117
UPDATE_USER = '[User] Update User',
114118
UPDATE_USERS = '[User] Update Users',
115119
DELETE_USER = '[User] Delete User',
@@ -129,12 +133,24 @@ export class AddUser implements Action {
129133
constructor(public payload: { user: User }) {}
130134
}
131135

136+
export class UpsertUser implements Action {
137+
readonly type = UserActionTypes.UPSERT_USER;
138+
139+
constructor(public payload: { user: User }) {}
140+
}
141+
132142
export class AddUsers implements Action {
133143
readonly type = UserActionTypes.ADD_USERS;
134144

135145
constructor(public payload: { users: User[] }) {}
136146
}
137147

148+
export class UpsertUsers implements Action {
149+
readonly type = UserActionTypes.UPSERT_USERS;
150+
151+
constructor(public payload: { users: User[] }) {}
152+
}
153+
138154
export class UpdateUser implements Action {
139155
readonly type = UserActionTypes.UPDATE_USER;
140156

@@ -166,7 +182,9 @@ export class ClearUsers implements Action {
166182
export type UserActions =
167183
LoadUsers
168184
| AddUser
185+
| UpsertUser
169186
| AddUsers
187+
| UpsertUsers
170188
| UpdateUser
171189
| UpdateUsers
172190
| DeleteUser
@@ -201,10 +219,18 @@ export function reducer(
201219
return adapter.addOne(action.payload.user, state);
202220
}
203221

222+
case UserActionTypes.UPSERT_USER: {
223+
return adapter.upsertOne(action.payload.user, state);
224+
}
225+
204226
case UserActionTypes.ADD_USERS: {
205227
return adapter.addMany(action.payload.users, state);
206228
}
207229

230+
case UserActionTypes.UPSERT_USERS: {
231+
return adapter.upsertMany(action.payload.users, state);
232+
}
233+
208234
case UserActionTypes.UPDATE_USER: {
209235
return adapter.updateOne(action.payload.user, state);
210236
}

example-app/app/books/reducers/books.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,31 @@ export function reducer(
4747
switch (action.type) {
4848
case BookActionTypes.SearchComplete:
4949
case CollectionActionTypes.LoadSuccess: {
50-
return {
51-
/**
52-
* The addMany function provided by the created adapter
53-
* adds many records to the entity dictionary
54-
* and returns a new state including those records. If
55-
* the collection is to be sorted, the adapter will
56-
* sort each record upon entry into the sorted array.
57-
*/
58-
...adapter.addMany(action.payload, state),
50+
/**
51+
* The addMany function provided by the created adapter
52+
* adds many records to the entity dictionary
53+
* and returns a new state including those records. If
54+
* the collection is to be sorted, the adapter will
55+
* sort each record upon entry into the sorted array.
56+
*/
57+
return adapter.addMany(action.payload, {
58+
...state,
5959
selectedBookId: state.selectedBookId,
60-
};
60+
});
6161
}
6262

6363
case BookActionTypes.Load: {
64-
return {
65-
/**
66-
* The addOne function provided by the created adapter
67-
* adds one record to the entity dictionary
68-
* and returns a new state including that records if it doesn't
69-
* exist already. If the collection is to be sorted, the adapter will
70-
* insert the new record into the sorted array.
71-
*/
72-
...adapter.addOne(action.payload, state),
64+
/**
65+
* The addOne function provided by the created adapter
66+
* adds one record to the entity dictionary
67+
* and returns a new state including that records if it doesn't
68+
* exist already. If the collection is to be sorted, the adapter will
69+
* insert the new record into the sorted array.
70+
*/
71+
return adapter.addOne(action.payload, {
72+
...state,
7373
selectedBookId: state.selectedBookId,
74-
};
74+
});
7575
}
7676

7777
case BookActionTypes.Select: {

modules/schematics/src/entity/files/__path__/__name@dasherize@if-flat__/__name@dasherize@group-actions__.actions.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import { <%= classify(name) %> } from '<%= featurePath(group, flat, "models", da
55
export enum <%= classify(name) %>ActionTypes {
66
Load<%= classify(name) %>s = '[<%= classify(name) %>] Load <%= classify(name) %>s',
77
Add<%= classify(name) %> = '[<%= classify(name) %>] Add <%= classify(name) %>',
8+
Upsert<%= classify(name) %> = '[<%= classify(name) %>] Upsert <%= classify(name) %>',
89
Add<%= classify(name) %>s = '[<%= classify(name) %>] Add <%= classify(name) %>s',
10+
Upsert<%= classify(name) %>s = '[<%= classify(name) %>] Upsert <%= classify(name) %>s',
911
Update<%= classify(name) %> = '[<%= classify(name) %>] Update <%= classify(name) %>',
1012
Update<%= classify(name) %>s = '[<%= classify(name) %>] Update <%= classify(name) %>s',
1113
Delete<%= classify(name) %> = '[<%= classify(name) %>] Delete <%= classify(name) %>',
@@ -25,12 +27,24 @@ export class Add<%= classify(name) %> implements Action {
2527
constructor(public payload: { <%= camelize(name) %>: <%= classify(name) %> }) {}
2628
}
2729

30+
export class Upsert<%= classify(name) %> implements Action {
31+
readonly type = <%= classify(name) %>ActionTypes.Upsert<%= classify(name) %>;
32+
33+
constructor(public payload: { <%= camelize(name) %>: <%= classify(name) %> }) {}
34+
}
35+
2836
export class Add<%= classify(name) %>s implements Action {
2937
readonly type = <%= classify(name) %>ActionTypes.Add<%= classify(name) %>s;
3038

3139
constructor(public payload: { <%= camelize(name) %>s: <%= classify(name) %>[] }) {}
3240
}
3341

42+
export class Upsert<%= classify(name) %>s implements Action {
43+
readonly type = <%= classify(name) %>ActionTypes.Upsert<%= classify(name) %>s;
44+
45+
constructor(public payload: { <%= camelize(name) %>s: <%= classify(name) %>[] }) {}
46+
}
47+
3448
export class Update<%= classify(name) %> implements Action {
3549
readonly type = <%= classify(name) %>ActionTypes.Update<%= classify(name) %>;
3650

@@ -62,7 +76,9 @@ export class Clear<%= classify(name) %>s implements Action {
6276
export type <%= classify(name) %>Actions =
6377
Load<%= classify(name) %>s
6478
| Add<%= classify(name) %>
79+
| Upsert<%= classify(name) %>
6580
| Add<%= classify(name) %>s
81+
| Upsert<%= classify(name) %>s
6682
| Update<%= classify(name) %>
6783
| Update<%= classify(name) %>s
6884
| Delete<%= classify(name) %>

modules/schematics/src/entity/files/__path__/__name@dasherize@if-flat__/__name@dasherize@group-reducers__.reducer.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,18 @@ export function reducer(
2121
return adapter.addOne(action.payload.<%= camelize(name) %>, state);
2222
}
2323

24+
case <%= classify(name) %>ActionTypes.Upsert<%= classify(name) %>: {
25+
return adapter.upsertOne(action.payload.<%= camelize(name) %>, state);
26+
}
27+
2428
case <%= classify(name) %>ActionTypes.Add<%= classify(name) %>s: {
2529
return adapter.addMany(action.payload.<%= camelize(name) %>s, state);
2630
}
2731

32+
case <%= classify(name) %>ActionTypes.Upsert<%= classify(name) %>s: {
33+
return adapter.upsertMany(action.payload.<%= camelize(name) %>s, state);
34+
}
35+
2836
case <%= classify(name) %>ActionTypes.Update<%= classify(name) %>: {
2937
return adapter.updateOne(action.payload.<%= camelize(name) %>, state);
3038
}

0 commit comments

Comments
 (0)