From a0f45ff035726f106f3f34ddf9b5025c54fc63e0 Mon Sep 17 00:00:00 2001 From: Tim Deschryver Date: Wed, 14 Mar 2018 19:30:12 +0100 Subject: [PATCH] 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 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); ``` --- .../entity/spec/sorted_state_adapter.spec.ts | 26 +++---------------- .../spec/unsorted_state_adapter.spec.ts | 26 +++---------------- modules/entity/src/models.ts | 4 +-- modules/entity/src/sorted_state_adapter.ts | 26 +++++++++---------- modules/entity/src/unsorted_state_adapter.ts | 24 ++++++++--------- 5 files changed, 33 insertions(+), 73 deletions(-) diff --git a/modules/entity/spec/sorted_state_adapter.spec.ts b/modules/entity/spec/sorted_state_adapter.spec.ts index ce142c9a1b..be2e3c3a4e 100644 --- a/modules/entity/spec/sorted_state_adapter.spec.ts +++ b/modules/entity/spec/sorted_state_adapter.spec.ts @@ -293,14 +293,7 @@ describe('Sorted State Adapter', () => { }); it('should let you add one entity to the state with upsert()', () => { - const withOneEntity = adapter.upsertOne( - { - id: TheGreatGatsby.id, - changes: TheGreatGatsby, - }, - state - ); - + const withOneEntity = adapter.upsertOne(TheGreatGatsby, state); expect(withOneEntity).toEqual({ ids: [TheGreatGatsby.id], entities: { @@ -314,13 +307,9 @@ describe('Sorted State Adapter', () => { const changes = { title: 'A New Hope' }; const withUpdates = adapter.upsertOne( - { - id: TheGreatGatsby.id, - changes, - }, + { ...TheGreatGatsby, ...changes }, withOne ); - expect(withUpdates).toEqual({ ids: [TheGreatGatsby.id], entities: { @@ -334,14 +323,10 @@ describe('Sorted State Adapter', () => { it('should let you upsert many entities in the state', () => { const firstChange = { title: 'Zack' }; - const secondChange = { title: 'Aaron' }; const withMany = adapter.addAll([TheGreatGatsby], state); const withUpserts = adapter.upsertMany( - [ - { id: TheGreatGatsby.id, changes: firstChange }, - { id: AClockworkOrange.id, changes: secondChange }, - ], + [{ ...TheGreatGatsby, ...firstChange }, AClockworkOrange], withMany ); @@ -352,10 +337,7 @@ describe('Sorted State Adapter', () => { ...TheGreatGatsby, ...firstChange, }, - [AClockworkOrange.id]: { - ...AClockworkOrange, - ...secondChange, - }, + [AClockworkOrange.id]: AClockworkOrange, }, }); }); diff --git a/modules/entity/spec/unsorted_state_adapter.spec.ts b/modules/entity/spec/unsorted_state_adapter.spec.ts index 0edb6bd6ef..f8e70e4c12 100644 --- a/modules/entity/spec/unsorted_state_adapter.spec.ts +++ b/modules/entity/spec/unsorted_state_adapter.spec.ts @@ -233,14 +233,7 @@ describe('Unsorted State Adapter', () => { }); it('should let you add one entity to the state with upsert()', () => { - const withOneEntity = adapter.upsertOne( - { - id: TheGreatGatsby.id, - changes: TheGreatGatsby, - }, - state - ); - + const withOneEntity = adapter.upsertOne(TheGreatGatsby, state); expect(withOneEntity).toEqual({ ids: [TheGreatGatsby.id], entities: { @@ -254,13 +247,9 @@ describe('Unsorted State Adapter', () => { const changes = { title: 'A New Hope' }; const withUpdates = adapter.upsertOne( - { - id: TheGreatGatsby.id, - changes, - }, + { ...TheGreatGatsby, ...changes }, withOne ); - expect(withUpdates).toEqual({ ids: [TheGreatGatsby.id], entities: { @@ -274,14 +263,10 @@ describe('Unsorted State Adapter', () => { it('should let you upsert many entities in the state', () => { const firstChange = { title: 'First Change' }; - const secondChange = { title: 'Second Change' }; const withMany = adapter.addAll([TheGreatGatsby], state); const withUpserts = adapter.upsertMany( - [ - { id: TheGreatGatsby.id, changes: firstChange }, - { id: AClockworkOrange.id, changes: secondChange }, - ], + [{ ...TheGreatGatsby, ...firstChange }, AClockworkOrange], withMany ); @@ -292,10 +277,7 @@ describe('Unsorted State Adapter', () => { ...TheGreatGatsby, ...firstChange, }, - [AClockworkOrange.id]: { - ...AClockworkOrange, - ...secondChange, - }, + [AClockworkOrange.id]: AClockworkOrange, }, }); }); diff --git a/modules/entity/src/models.ts b/modules/entity/src/models.ts index 4e60955388..1ec8beb542 100644 --- a/modules/entity/src/models.ts +++ b/modules/entity/src/models.ts @@ -64,8 +64,8 @@ export interface EntityStateAdapter { updateOne>(update: Update, state: S): S; updateMany>(updates: Update[], state: S): S; - upsertOne>(update: Update, state: S): S; - upsertMany>(updates: Update[], state: S): S; + upsertOne>(entity: T, state: S): S; + upsertMany>(entities: T[], state: S): S; } export type EntitySelectors = { diff --git a/modules/entity/src/sorted_state_adapter.ts b/modules/entity/src/sorted_state_adapter.ts index ed2b297da6..9d59ae0585 100644 --- a/modules/entity/src/sorted_state_adapter.ts +++ b/modules/entity/src/sorted_state_adapter.ts @@ -106,24 +106,22 @@ export function createSortedStateAdapter(selectId: any, sort: any): any { } } - function upsertOneMutably(update: Update, state: R): DidMutate; - function upsertOneMutably(update: any, state: any): DidMutate { - return upsertManyMutably([update], state); + function upsertOneMutably(entity: T, state: R): DidMutate; + function upsertOneMutably(entity: any, state: any): DidMutate { + return upsertManyMutably([entity], state); } - function upsertManyMutably(updates: Update[], state: R): DidMutate; - function upsertManyMutably(updates: any[], state: any): DidMutate { - const added: T[] = []; - const updated: Update[] = []; + function upsertManyMutably(entities: T[], state: R): DidMutate; + function upsertManyMutably(entities: any[], state: any): DidMutate { + const added: any[] = []; + const updated: any[] = []; - for (const update of updates) { - if (update.id in state.entities) { - updated.push(update); + for (const entity of entities) { + const id = selectId(entity); + if (id in state.entities) { + updated.push({ id, changes: entity }); } else { - added.push({ - ...update.changes, - id: update.id, - }); + added.push(entity); } } diff --git a/modules/entity/src/unsorted_state_adapter.ts b/modules/entity/src/unsorted_state_adapter.ts index 3bd62f63ce..a9fa47e237 100644 --- a/modules/entity/src/unsorted_state_adapter.ts +++ b/modules/entity/src/unsorted_state_adapter.ts @@ -122,24 +122,22 @@ export function createUnsortedStateAdapter(selectId: IdSelector): any { return DidMutate.None; } - function upsertOneMutably(update: Update, state: R): DidMutate; - function upsertOneMutably(update: any, state: any): DidMutate { - return upsertManyMutably([update], state); + function upsertOneMutably(entity: T, state: R): DidMutate; + function upsertOneMutably(entity: any, state: any): DidMutate { + return upsertManyMutably([entity], state); } - function upsertManyMutably(updates: Update[], state: R): DidMutate; - function upsertManyMutably(updates: any[], state: any): DidMutate { - const added: T[] = []; + function upsertManyMutably(entities: T[], state: R): DidMutate; + function upsertManyMutably(entities: any[], state: any): DidMutate { + const added: any[] = []; const updated: any[] = []; - for (const update of updates) { - if (update.id in state.entities) { - updated.push(update); + for (const entity of entities) { + const id = selectId(entity); + if (id in state.entities) { + updated.push({ id, changes: entity }); } else { - added.push({ - ...update.changes, - id: update.id, - }); + added.push(entity); } }