Skip to content

Commit 27f5059

Browse files
authored
feat(entity): deprecate addAll and rename it to setAll (#2348)
Closes #2330
1 parent 0dabfc4 commit 27f5059

File tree

9 files changed

+70
-29
lines changed

9 files changed

+70
-29
lines changed

docs/entity/adapter.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ state if no changes were made.
8585

8686
- `addOne`: Add one entity to the collection
8787
- `addMany`: Add multiple entities to the collection
88-
- `addAll`: Replace current collection with provided collection
88+
- ~~`addAll`~~: (Deprecated and renamed to `setAll`). ~~Replace current collection with provided collection~~
89+
- `setAll`: Replace current collection with provided collection
8990
- `removeOne`: Remove one entity from the collection
9091
- `removeMany`: Remove multiple entities from the collection
9192
- `removeAll`: Clear entity collection

modules/entity/spec/sorted_state_adapter.spec.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,27 @@ describe('Sorted State Adapter', () => {
6969
});
7070
});
7171

72-
it('should let you add all entities to the state', () => {
72+
it('should remove existing and add new ones on setAll', () => {
7373
const withOneEntity = adapter.addOne(TheGreatGatsby, state);
7474

75-
const withAll = adapter.addAll(
75+
const withAll = adapter.setAll(
76+
[AClockworkOrange, AnimalFarm],
77+
withOneEntity
78+
);
79+
80+
expect(withAll).toEqual({
81+
ids: [AClockworkOrange.id, AnimalFarm.id],
82+
entities: {
83+
[AClockworkOrange.id]: AClockworkOrange,
84+
[AnimalFarm.id]: AnimalFarm,
85+
},
86+
});
87+
});
88+
89+
it('should remove existing and add new ones on addAll (deprecated)', () => {
90+
const withOneEntity = adapter.addOne(TheGreatGatsby, state);
91+
92+
const withAll = adapter.setAll(
7693
[AClockworkOrange, AnimalFarm],
7794
withOneEntity
7895
);
@@ -98,7 +115,7 @@ describe('Sorted State Adapter', () => {
98115
});
99116

100117
it('should let you remove many entities by id from the state', () => {
101-
const withAll = adapter.addAll(
118+
const withAll = adapter.setAll(
102119
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
103120
state
104121
);
@@ -117,7 +134,7 @@ describe('Sorted State Adapter', () => {
117134
});
118135

119136
it('should let you remove many entities by a predicate from the state', () => {
120-
const withAll = adapter.addAll(
137+
const withAll = adapter.setAll(
121138
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
122139
state
123140
);
@@ -133,7 +150,7 @@ describe('Sorted State Adapter', () => {
133150
});
134151

135152
it('should let you remove all entities from the state', () => {
136-
const withAll = adapter.addAll(
153+
const withAll = adapter.setAll(
137154
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
138155
state
139156
);
@@ -182,7 +199,7 @@ describe('Sorted State Adapter', () => {
182199
});
183200

184201
it('should not change ids state if you attempt to update an entity that does not impact sorting', () => {
185-
const withAll = adapter.addAll(
202+
const withAll = adapter.setAll(
186203
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
187204
state
188205
);
@@ -223,7 +240,7 @@ describe('Sorted State Adapter', () => {
223240
});
224241

225242
it('should resort correctly if same id but sort key update', () => {
226-
const withAll = adapter.addAll(
243+
const withAll = adapter.setAll(
227244
[TheGreatGatsby, AnimalFarm, AClockworkOrange],
228245
state
229246
);
@@ -251,7 +268,7 @@ describe('Sorted State Adapter', () => {
251268
});
252269

253270
it('should resort correctly if the id and sort key update', () => {
254-
const withOne = adapter.addAll(
271+
const withOne = adapter.setAll(
255272
[TheGreatGatsby, AnimalFarm, AClockworkOrange],
256273
state
257274
);
@@ -281,7 +298,7 @@ describe('Sorted State Adapter', () => {
281298
it('should let you update many entities by id in the state', () => {
282299
const firstChange = { title: 'Zack' };
283300
const secondChange = { title: 'Aaron' };
284-
const withMany = adapter.addAll([TheGreatGatsby, AClockworkOrange], state);
301+
const withMany = adapter.setAll([TheGreatGatsby, AClockworkOrange], state);
285302

286303
const withUpdates = adapter.updateMany(
287304
[
@@ -310,7 +327,7 @@ describe('Sorted State Adapter', () => {
310327
const firstChange = { ...TheGreatGatsby, title: 'First change' };
311328
const secondChange = { ...AClockworkOrange, title: 'Second change' };
312329

313-
const withMany = adapter.addAll(
330+
const withMany = adapter.setAll(
314331
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
315332
state
316333
);
@@ -372,7 +389,7 @@ describe('Sorted State Adapter', () => {
372389

373390
it('should let you upsert many entities in the state', () => {
374391
const firstChange = { title: 'Zack' };
375-
const withMany = adapter.addAll([TheGreatGatsby], state);
392+
const withMany = adapter.setAll([TheGreatGatsby], state);
376393

377394
const withUpserts = adapter.upsertMany(
378395
[{ ...TheGreatGatsby, ...firstChange }, AClockworkOrange],

modules/entity/spec/state_selectors.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('Entity State Selectors', () => {
2424
});
2525

2626
state = {
27-
books: adapter.addAll(
27+
books: adapter.setAll(
2828
[AClockworkOrange, AnimalFarm, TheGreatGatsby],
2929
adapter.getInitialState()
3030
),
@@ -70,7 +70,7 @@ describe('Entity State Selectors', () => {
7070
selectId: (book: BookModel) => book.id,
7171
});
7272

73-
state = adapter.addAll(
73+
state = adapter.setAll(
7474
[AClockworkOrange, AnimalFarm, TheGreatGatsby],
7575
adapter.getInitialState()
7676
);

modules/entity/spec/unsorted_state_adapter.spec.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,24 @@ describe('Unsorted State Adapter', () => {
6868
});
6969
});
7070

71-
it('should let you add all entities to the state', () => {
71+
it('should remove existing and add new ones on setAll', () => {
72+
const withOneEntity = adapter.addOne(TheGreatGatsby, state);
73+
74+
const withAll = adapter.setAll(
75+
[AClockworkOrange, AnimalFarm],
76+
withOneEntity
77+
);
78+
79+
expect(withAll).toEqual({
80+
ids: [AClockworkOrange.id, AnimalFarm.id],
81+
entities: {
82+
[AClockworkOrange.id]: AClockworkOrange,
83+
[AnimalFarm.id]: AnimalFarm,
84+
},
85+
});
86+
});
87+
88+
it('should remove existing and add new ones on addAll (deprecated)', () => {
7289
const withOneEntity = adapter.addOne(TheGreatGatsby, state);
7390

7491
const withAll = adapter.addAll(
@@ -97,7 +114,7 @@ describe('Unsorted State Adapter', () => {
97114
});
98115

99116
it('should let you remove many entities by id from the state', () => {
100-
const withAll = adapter.addAll(
117+
const withAll = adapter.setAll(
101118
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
102119
state
103120
);
@@ -116,7 +133,7 @@ describe('Unsorted State Adapter', () => {
116133
});
117134

118135
it('should let you remove many entities by a predicate from the state', () => {
119-
const withAll = adapter.addAll(
136+
const withAll = adapter.setAll(
120137
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
121138
state
122139
);
@@ -132,7 +149,7 @@ describe('Unsorted State Adapter', () => {
132149
});
133150

134151
it('should let you remove all entities from the state', () => {
135-
const withAll = adapter.addAll(
152+
const withAll = adapter.setAll(
136153
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
137154
state
138155
);
@@ -221,7 +238,7 @@ describe('Unsorted State Adapter', () => {
221238
it('should let you update many entities by id in the state', () => {
222239
const firstChange = { title: 'First Change' };
223240
const secondChange = { title: 'Second Change' };
224-
const withMany = adapter.addAll([TheGreatGatsby, AClockworkOrange], state);
241+
const withMany = adapter.setAll([TheGreatGatsby, AClockworkOrange], state);
225242

226243
const withUpdates = adapter.updateMany(
227244
[
@@ -250,7 +267,7 @@ describe('Unsorted State Adapter', () => {
250267
const firstChange = { ...TheGreatGatsby, title: 'First change' };
251268
const secondChange = { ...AClockworkOrange, title: 'Second change' };
252269

253-
const withMany = adapter.addAll(
270+
const withMany = adapter.setAll(
254271
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
255272
state
256273
);
@@ -312,7 +329,7 @@ describe('Unsorted State Adapter', () => {
312329

313330
it('should let you upsert many entities in the state', () => {
314331
const firstChange = { title: 'First Change' };
315-
const withMany = adapter.addAll([TheGreatGatsby], state);
332+
const withMany = adapter.setAll([TheGreatGatsby], state);
316333

317334
const withUpserts = adapter.upsertMany(
318335
[{ ...TheGreatGatsby, ...firstChange }, AClockworkOrange],

modules/entity/src/models.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@ export interface EntityDefinition<T> {
4545
export interface EntityStateAdapter<T> {
4646
addOne<S extends EntityState<T>>(entity: T, state: S): S;
4747
addMany<S extends EntityState<T>>(entities: T[], state: S): S;
48+
49+
/** @deprecated addAll has been renamed. Use setAll instead. */
4850
addAll<S extends EntityState<T>>(entities: T[], state: S): S;
4951

52+
setAll<S extends EntityState<T>>(entities: T[], state: S): S;
53+
5054
removeOne<S extends EntityState<T>>(key: string, state: S): S;
5155
removeOne<S extends EntityState<T>>(key: number, state: S): S;
5256

modules/entity/src/sorted_state_adapter.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
4040
}
4141
}
4242

43-
function addAllMutably(models: T[], state: R): DidMutate;
44-
function addAllMutably(models: any[], state: any): DidMutate {
43+
function setAllMutably(models: T[], state: R): DidMutate;
44+
function setAllMutably(models: any[], state: any): DidMutate {
4545
state.entities = {};
4646
state.ids = [];
4747

@@ -199,7 +199,8 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
199199
addOne: createStateOperator(addOneMutably),
200200
updateOne: createStateOperator(updateOneMutably),
201201
upsertOne: createStateOperator(upsertOneMutably),
202-
addAll: createStateOperator(addAllMutably),
202+
addAll: createStateOperator(setAllMutably),
203+
setAll: createStateOperator(setAllMutably),
203204
addMany: createStateOperator(addManyMutably),
204205
updateMany: createStateOperator(updateManyMutably),
205206
upsertMany: createStateOperator(upsertManyMutably),

modules/entity/src/unsorted_state_adapter.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ export function createUnsortedStateAdapter<T>(selectId: IdSelector<T>): any {
4040
return didMutate ? DidMutate.Both : DidMutate.None;
4141
}
4242

43-
function addAllMutably(entities: T[], state: R): DidMutate;
44-
function addAllMutably(entities: any[], state: any): DidMutate {
43+
function setAllMutably(entities: T[], state: R): DidMutate;
44+
function setAllMutably(entities: any[], state: any): DidMutate {
4545
state.ids = [];
4646
state.entities = {};
4747

@@ -194,7 +194,8 @@ export function createUnsortedStateAdapter<T>(selectId: IdSelector<T>): any {
194194
removeAll,
195195
addOne: createStateOperator(addOneMutably),
196196
addMany: createStateOperator(addManyMutably),
197-
addAll: createStateOperator(addAllMutably),
197+
addAll: createStateOperator(setAllMutably),
198+
setAll: createStateOperator(setAllMutably),
198199
updateOne: createStateOperator(updateOneMutably),
199200
updateMany: createStateOperator(updateManyMutably),
200201
upsertOne: createStateOperator(upsertOneMutably),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const <%= camelize(name) %>Reducer = createReducer(
4242
(state, action) => adapter.removeMany(action.ids, state)
4343
),
4444
on(<%= classify(name) %>Actions.load<%= classify(name) %>s,
45-
(state, action) => adapter.addAll(action.<%= camelize(name) %>s, state)
45+
(state, action) => adapter.setAll(action.<%= camelize(name) %>s, state)
4646
),
4747
on(<%= classify(name) %>Actions.clear<%= classify(name) %>s,
4848
state => adapter.removeAll(state)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function reducer(
5252
}
5353

5454
case <%= classify(name) %>ActionTypes.Load<%= classify(name) %>s: {
55-
return adapter.addAll(action.payload.<%= camelize(name) %>s, state);
55+
return adapter.setAll(action.payload.<%= camelize(name) %>s, state);
5656
}
5757

5858
case <%= classify(name) %>ActionTypes.Clear<%= classify(name) %>s: {

0 commit comments

Comments
 (0)