Skip to content

Commit a0f45ff

Browse files
timdeschryverMikeRyanDev
authored andcommitted
fix(Entity): Change EntityAdapter upsertOne/upsertMany to accept an entity
BREAKING CHANGE: The signature of the upsertOne/upsertMany functions in the EntityAdapter has been changed to accept a fully qualified entity instead of an update object that implements the Update<T> interface. Before: ``` entityAdapter.upsertOne({ id: 'Entity ID', changes: { id: 'Entity ID', name: 'Entity Name' }, }, state); ``` After: ``` entityAdapter.upsertOne({ id: 'Entity ID', name: 'Entity Name', }, state); ```
1 parent d1286d2 commit a0f45ff

File tree

5 files changed

+33
-73
lines changed

5 files changed

+33
-73
lines changed

modules/entity/spec/sorted_state_adapter.spec.ts

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,7 @@ describe('Sorted State Adapter', () => {
293293
});
294294

295295
it('should let you add one entity to the state with upsert()', () => {
296-
const withOneEntity = adapter.upsertOne(
297-
{
298-
id: TheGreatGatsby.id,
299-
changes: TheGreatGatsby,
300-
},
301-
state
302-
);
303-
296+
const withOneEntity = adapter.upsertOne(TheGreatGatsby, state);
304297
expect(withOneEntity).toEqual({
305298
ids: [TheGreatGatsby.id],
306299
entities: {
@@ -314,13 +307,9 @@ describe('Sorted State Adapter', () => {
314307
const changes = { title: 'A New Hope' };
315308

316309
const withUpdates = adapter.upsertOne(
317-
{
318-
id: TheGreatGatsby.id,
319-
changes,
320-
},
310+
{ ...TheGreatGatsby, ...changes },
321311
withOne
322312
);
323-
324313
expect(withUpdates).toEqual({
325314
ids: [TheGreatGatsby.id],
326315
entities: {
@@ -334,14 +323,10 @@ describe('Sorted State Adapter', () => {
334323

335324
it('should let you upsert many entities in the state', () => {
336325
const firstChange = { title: 'Zack' };
337-
const secondChange = { title: 'Aaron' };
338326
const withMany = adapter.addAll([TheGreatGatsby], state);
339327

340328
const withUpserts = adapter.upsertMany(
341-
[
342-
{ id: TheGreatGatsby.id, changes: firstChange },
343-
{ id: AClockworkOrange.id, changes: secondChange },
344-
],
329+
[{ ...TheGreatGatsby, ...firstChange }, AClockworkOrange],
345330
withMany
346331
);
347332

@@ -352,10 +337,7 @@ describe('Sorted State Adapter', () => {
352337
...TheGreatGatsby,
353338
...firstChange,
354339
},
355-
[AClockworkOrange.id]: {
356-
...AClockworkOrange,
357-
...secondChange,
358-
},
340+
[AClockworkOrange.id]: AClockworkOrange,
359341
},
360342
});
361343
});

modules/entity/spec/unsorted_state_adapter.spec.ts

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,7 @@ describe('Unsorted State Adapter', () => {
233233
});
234234

235235
it('should let you add one entity to the state with upsert()', () => {
236-
const withOneEntity = adapter.upsertOne(
237-
{
238-
id: TheGreatGatsby.id,
239-
changes: TheGreatGatsby,
240-
},
241-
state
242-
);
243-
236+
const withOneEntity = adapter.upsertOne(TheGreatGatsby, state);
244237
expect(withOneEntity).toEqual({
245238
ids: [TheGreatGatsby.id],
246239
entities: {
@@ -254,13 +247,9 @@ describe('Unsorted State Adapter', () => {
254247
const changes = { title: 'A New Hope' };
255248

256249
const withUpdates = adapter.upsertOne(
257-
{
258-
id: TheGreatGatsby.id,
259-
changes,
260-
},
250+
{ ...TheGreatGatsby, ...changes },
261251
withOne
262252
);
263-
264253
expect(withUpdates).toEqual({
265254
ids: [TheGreatGatsby.id],
266255
entities: {
@@ -274,14 +263,10 @@ describe('Unsorted State Adapter', () => {
274263

275264
it('should let you upsert many entities in the state', () => {
276265
const firstChange = { title: 'First Change' };
277-
const secondChange = { title: 'Second Change' };
278266
const withMany = adapter.addAll([TheGreatGatsby], state);
279267

280268
const withUpserts = adapter.upsertMany(
281-
[
282-
{ id: TheGreatGatsby.id, changes: firstChange },
283-
{ id: AClockworkOrange.id, changes: secondChange },
284-
],
269+
[{ ...TheGreatGatsby, ...firstChange }, AClockworkOrange],
285270
withMany
286271
);
287272

@@ -292,10 +277,7 @@ describe('Unsorted State Adapter', () => {
292277
...TheGreatGatsby,
293278
...firstChange,
294279
},
295-
[AClockworkOrange.id]: {
296-
...AClockworkOrange,
297-
...secondChange,
298-
},
280+
[AClockworkOrange.id]: AClockworkOrange,
299281
},
300282
});
301283
});

modules/entity/src/models.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ export interface EntityStateAdapter<T> {
6464
updateOne<S extends EntityState<T>>(update: Update<T>, state: S): S;
6565
updateMany<S extends EntityState<T>>(updates: Update<T>[], state: S): S;
6666

67-
upsertOne<S extends EntityState<T>>(update: Update<T>, state: S): S;
68-
upsertMany<S extends EntityState<T>>(updates: Update<T>[], state: S): S;
67+
upsertOne<S extends EntityState<T>>(entity: T, state: S): S;
68+
upsertMany<S extends EntityState<T>>(entities: T[], state: S): S;
6969
}
7070

7171
export type EntitySelectors<T, V> = {

modules/entity/src/sorted_state_adapter.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,24 +106,22 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
106106
}
107107
}
108108

109-
function upsertOneMutably(update: Update<T>, state: R): DidMutate;
110-
function upsertOneMutably(update: any, state: any): DidMutate {
111-
return upsertManyMutably([update], state);
109+
function upsertOneMutably(entity: T, state: R): DidMutate;
110+
function upsertOneMutably(entity: any, state: any): DidMutate {
111+
return upsertManyMutably([entity], state);
112112
}
113113

114-
function upsertManyMutably(updates: Update<T>[], state: R): DidMutate;
115-
function upsertManyMutably(updates: any[], state: any): DidMutate {
116-
const added: T[] = [];
117-
const updated: Update<T>[] = [];
114+
function upsertManyMutably(entities: T[], state: R): DidMutate;
115+
function upsertManyMutably(entities: any[], state: any): DidMutate {
116+
const added: any[] = [];
117+
const updated: any[] = [];
118118

119-
for (const update of updates) {
120-
if (update.id in state.entities) {
121-
updated.push(update);
119+
for (const entity of entities) {
120+
const id = selectId(entity);
121+
if (id in state.entities) {
122+
updated.push({ id, changes: entity });
122123
} else {
123-
added.push({
124-
...update.changes,
125-
id: update.id,
126-
});
124+
added.push(entity);
127125
}
128126
}
129127

modules/entity/src/unsorted_state_adapter.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,24 +122,22 @@ export function createUnsortedStateAdapter<T>(selectId: IdSelector<T>): any {
122122
return DidMutate.None;
123123
}
124124

125-
function upsertOneMutably(update: Update<T>, state: R): DidMutate;
126-
function upsertOneMutably(update: any, state: any): DidMutate {
127-
return upsertManyMutably([update], state);
125+
function upsertOneMutably(entity: T, state: R): DidMutate;
126+
function upsertOneMutably(entity: any, state: any): DidMutate {
127+
return upsertManyMutably([entity], state);
128128
}
129129

130-
function upsertManyMutably(updates: Update<T>[], state: R): DidMutate;
131-
function upsertManyMutably(updates: any[], state: any): DidMutate {
132-
const added: T[] = [];
130+
function upsertManyMutably(entities: T[], state: R): DidMutate;
131+
function upsertManyMutably(entities: any[], state: any): DidMutate {
132+
const added: any[] = [];
133133
const updated: any[] = [];
134134

135-
for (const update of updates) {
136-
if (update.id in state.entities) {
137-
updated.push(update);
135+
for (const entity of entities) {
136+
const id = selectId(entity);
137+
if (id in state.entities) {
138+
updated.push({ id, changes: entity });
138139
} else {
139-
added.push({
140-
...update.changes,
141-
id: update.id,
142-
});
140+
added.push(entity);
143141
}
144142
}
145143

0 commit comments

Comments
 (0)