Skip to content

Commit

Permalink
feat(Schematics): Add upsert methods to entity blueprint (#809)
Browse files Browse the repository at this point in the history
Closes #592
  • Loading branch information
brandonroberts authored and MikeRyanDev committed Feb 13, 2018
1 parent f31c898 commit 7acdc79
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 20 deletions.
26 changes: 26 additions & 0 deletions docs/entity/adapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ state if no changes were made.
* `removeAll`: Clear entity collection
* `updateOne`: Update one entity in the collection
* `updateMany`: Update multiple entities in the collection
* `upsertOne`: Add or Update one entity in the collection
* `upsertMany`: Add or Update multiple entities in the collection

Usage:

Expand All @@ -109,7 +111,9 @@ import { User } from './user.model';
export enum UserActionTypes {
LOAD_USERS = '[User] Load Users',
ADD_USER = '[User] Add User',
UPSERT_USER = '[User] Upsert User',
ADD_USERS = '[User] Add Users',
UPSERT_USERS = '[User] Upsert Users',
UPDATE_USER = '[User] Update User',
UPDATE_USERS = '[User] Update Users',
DELETE_USER = '[User] Delete User',
Expand All @@ -129,12 +133,24 @@ export class AddUser implements Action {
constructor(public payload: { user: User }) {}
}

export class UpsertUser implements Action {
readonly type = UserActionTypes.UPSERT_USER;

constructor(public payload: { user: User }) {}
}

export class AddUsers implements Action {
readonly type = UserActionTypes.ADD_USERS;

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

export class UpsertUsers implements Action {
readonly type = UserActionTypes.UPSERT_USERS;

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

export class UpdateUser implements Action {
readonly type = UserActionTypes.UPDATE_USER;

Expand Down Expand Up @@ -166,7 +182,9 @@ export class ClearUsers implements Action {
export type UserActions =
LoadUsers
| AddUser
| UpsertUser
| AddUsers
| UpsertUsers
| UpdateUser
| UpdateUsers
| DeleteUser
Expand Down Expand Up @@ -201,10 +219,18 @@ export function reducer(
return adapter.addOne(action.payload.user, state);
}

case UserActionTypes.UPSERT_USER: {
return adapter.upsertOne(action.payload.user, state);
}

case UserActionTypes.ADD_USERS: {
return adapter.addMany(action.payload.users, state);
}

case UserActionTypes.UPSERT_USERS: {
return adapter.upsertMany(action.payload.users, state);
}

case UserActionTypes.UPDATE_USER: {
return adapter.updateOne(action.payload.user, state);
}
Expand Down
40 changes: 20 additions & 20 deletions example-app/app/books/reducers/books.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,31 @@ export function reducer(
switch (action.type) {
case BookActionTypes.SearchComplete:
case CollectionActionTypes.LoadSuccess: {
return {
/**
* The addMany function provided by the created adapter
* adds many records to the entity dictionary
* and returns a new state including those records. If
* the collection is to be sorted, the adapter will
* sort each record upon entry into the sorted array.
*/
...adapter.addMany(action.payload, state),
/**
* The addMany function provided by the created adapter
* adds many records to the entity dictionary
* and returns a new state including those records. If
* the collection is to be sorted, the adapter will
* sort each record upon entry into the sorted array.
*/
return adapter.addMany(action.payload, {
...state,
selectedBookId: state.selectedBookId,
};
});
}

case BookActionTypes.Load: {
return {
/**
* The addOne function provided by the created adapter
* adds one record to the entity dictionary
* and returns a new state including that records if it doesn't
* exist already. If the collection is to be sorted, the adapter will
* insert the new record into the sorted array.
*/
...adapter.addOne(action.payload, state),
/**
* The addOne function provided by the created adapter
* adds one record to the entity dictionary
* and returns a new state including that records if it doesn't
* exist already. If the collection is to be sorted, the adapter will
* insert the new record into the sorted array.
*/
return adapter.addOne(action.payload, {
...state,
selectedBookId: state.selectedBookId,
};
});
}

case BookActionTypes.Select: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { <%= classify(name) %> } from '<%= featurePath(group, flat, "models", da
export enum <%= classify(name) %>ActionTypes {
Load<%= classify(name) %>s = '[<%= classify(name) %>] Load <%= classify(name) %>s',
Add<%= classify(name) %> = '[<%= classify(name) %>] Add <%= classify(name) %>',
Upsert<%= classify(name) %> = '[<%= classify(name) %>] Upsert <%= classify(name) %>',
Add<%= classify(name) %>s = '[<%= classify(name) %>] Add <%= classify(name) %>s',
Upsert<%= classify(name) %>s = '[<%= classify(name) %>] Upsert <%= classify(name) %>s',
Update<%= classify(name) %> = '[<%= classify(name) %>] Update <%= classify(name) %>',
Update<%= classify(name) %>s = '[<%= classify(name) %>] Update <%= classify(name) %>s',
Delete<%= classify(name) %> = '[<%= classify(name) %>] Delete <%= classify(name) %>',
Expand All @@ -25,12 +27,24 @@ export class Add<%= classify(name) %> implements Action {
constructor(public payload: { <%= camelize(name) %>: <%= classify(name) %> }) {}
}

export class Upsert<%= classify(name) %> implements Action {
readonly type = <%= classify(name) %>ActionTypes.Upsert<%= classify(name) %>;

constructor(public payload: { <%= camelize(name) %>: <%= classify(name) %> }) {}
}

export class Add<%= classify(name) %>s implements Action {
readonly type = <%= classify(name) %>ActionTypes.Add<%= classify(name) %>s;

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

export class Upsert<%= classify(name) %>s implements Action {
readonly type = <%= classify(name) %>ActionTypes.Upsert<%= classify(name) %>s;

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

export class Update<%= classify(name) %> implements Action {
readonly type = <%= classify(name) %>ActionTypes.Update<%= classify(name) %>;

Expand Down Expand Up @@ -62,7 +76,9 @@ export class Clear<%= classify(name) %>s implements Action {
export type <%= classify(name) %>Actions =
Load<%= classify(name) %>s
| Add<%= classify(name) %>
| Upsert<%= classify(name) %>
| Add<%= classify(name) %>s
| Upsert<%= classify(name) %>s
| Update<%= classify(name) %>
| Update<%= classify(name) %>s
| Delete<%= classify(name) %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@ export function reducer(
return adapter.addOne(action.payload.<%= camelize(name) %>, state);
}

case <%= classify(name) %>ActionTypes.Upsert<%= classify(name) %>: {
return adapter.upsertOne(action.payload.<%= camelize(name) %>, state);
}

case <%= classify(name) %>ActionTypes.Add<%= classify(name) %>s: {
return adapter.addMany(action.payload.<%= camelize(name) %>s, state);
}

case <%= classify(name) %>ActionTypes.Upsert<%= classify(name) %>s: {
return adapter.upsertMany(action.payload.<%= camelize(name) %>s, state);
}

case <%= classify(name) %>ActionTypes.Update<%= classify(name) %>: {
return adapter.updateOne(action.payload.<%= camelize(name) %>, state);
}
Expand Down

0 comments on commit 7acdc79

Please sign in to comment.