Skip to content

Commit

Permalink
fix(Entity): Change EntityAdapter upsertOne/upsertMany to accept an e…
Browse files Browse the repository at this point in the history
…ntity

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);
  ```
  • Loading branch information
timdeschryver authored and MikeRyanDev committed Mar 30, 2018
1 parent d1286d2 commit a0f45ff
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 73 deletions.
26 changes: 4 additions & 22 deletions modules/entity/spec/sorted_state_adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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: {
Expand All @@ -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
);

Expand All @@ -352,10 +337,7 @@ describe('Sorted State Adapter', () => {
...TheGreatGatsby,
...firstChange,
},
[AClockworkOrange.id]: {
...AClockworkOrange,
...secondChange,
},
[AClockworkOrange.id]: AClockworkOrange,
},
});
});
Expand Down
26 changes: 4 additions & 22 deletions modules/entity/spec/unsorted_state_adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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: {
Expand All @@ -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
);

Expand All @@ -292,10 +277,7 @@ describe('Unsorted State Adapter', () => {
...TheGreatGatsby,
...firstChange,
},
[AClockworkOrange.id]: {
...AClockworkOrange,
...secondChange,
},
[AClockworkOrange.id]: AClockworkOrange,
},
});
});
Expand Down
4 changes: 2 additions & 2 deletions modules/entity/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export interface EntityStateAdapter<T> {
updateOne<S extends EntityState<T>>(update: Update<T>, state: S): S;
updateMany<S extends EntityState<T>>(updates: Update<T>[], state: S): S;

upsertOne<S extends EntityState<T>>(update: Update<T>, state: S): S;
upsertMany<S extends EntityState<T>>(updates: Update<T>[], state: S): S;
upsertOne<S extends EntityState<T>>(entity: T, state: S): S;
upsertMany<S extends EntityState<T>>(entities: T[], state: S): S;
}

export type EntitySelectors<T, V> = {
Expand Down
26 changes: 12 additions & 14 deletions modules/entity/src/sorted_state_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,22 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
}
}

function upsertOneMutably(update: Update<T>, 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<T>[], state: R): DidMutate;
function upsertManyMutably(updates: any[], state: any): DidMutate {
const added: T[] = [];
const updated: Update<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);
}
}

Expand Down
24 changes: 11 additions & 13 deletions modules/entity/src/unsorted_state_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,22 @@ export function createUnsortedStateAdapter<T>(selectId: IdSelector<T>): any {
return DidMutate.None;
}

function upsertOneMutably(update: Update<T>, 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<T>[], 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);
}
}

Expand Down

0 comments on commit a0f45ff

Please sign in to comment.