Skip to content

Commit

Permalink
Merge pull request salesforce#9 from datorama/fix/set
Browse files Browse the repository at this point in the history
fix(crud): set now can accept the complete state object
  • Loading branch information
NetanelBasal authored Jul 9, 2018
2 parents d0331fe + a2c93c6 commit f2fc223
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
10 changes: 0 additions & 10 deletions akita/__tests__/crud.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,6 @@ describe('CRUD', () => {
expect(store.entities[201].title).toEqual('201');
});

it('should throw if entities are invalid', () => {
const state = {
entities: null,
ids: [200, 201]
};
expect(function() {
crud._set(store, state.entities, null, ID_KEY);
}).toThrow(new AkitaInvalidEntityState() as any);
});

it('should set ids when passing entities', () => {
let store = {
entities: {},
Expand Down
4 changes: 2 additions & 2 deletions akita/src/api/entity-store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { _crud } from '../internal/crud';
import { ActiveState, EntityState, HashMap, ID, Newable } from './types';
import { ActiveState, Entities, EntityState, HashMap, ID, Newable } from './types';
import { coerceArray, entityExists, isFunction, toBoolean } from '../internal/utils';
import { Store } from './store';
import { assertActive, assertEntityExists } from '../internal/error';
Expand Down Expand Up @@ -56,7 +56,7 @@ export class EntityStore<S extends EntityState<E>, E> extends Store<S> {
* this.store.set([{id: 1}, {id: 2}], Product);
*
*/
set(entities: E[] | HashMap<E>, entityClass?: Newable<E>) {
set(entities: E[] | HashMap<E> | Entities<E>, entityClass?: Newable<E>) {
applyTransaction(() => {
this.setState(state => _crud._set(state, entities, entityClass, this.idKey));
this.setDirty();
Expand Down
5 changes: 5 additions & 0 deletions akita/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export interface EntityState<T = any> {
[key: string]: any;
}

export interface Entities<E> {
entities?: HashMap<E>;
ids?: ID[];
}

/**
* Interface for stores that needs an active indication
*/
Expand Down
25 changes: 16 additions & 9 deletions akita/src/internal/crud.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import { EntityState, HashMap, ID, Newable } from '../api/types';
import { Entities, EntityState, HashMap, ID, Newable } from '../api/types';
import { entityExists, isPlainObject } from './utils';
import { assertEntityExists, assertEntityState } from './error';

export class CRUD {
_set<S, E>(state: S, entities: E[] | HashMap<E>, entityClass: Newable<E>, idKey): S {
const isArray = Array.isArray(entities);
let normalized = entities;
_set<S, E>(state: S, entities: E[] | HashMap<E> | Entities<E>, entityClass: Newable<E>, idKey): S {
let ids, normalized;

if (isArray) {
normalized = this.keyBy(entities as E[], entityClass, idKey) as E[];
if ((entities as Entities<E>).ids && (entities as Entities<E>).entities) {
ids = (entities as Entities<E>).ids;
normalized = (entities as Entities<E>).entities;
} else {
assertEntityState(entities);
}
const isArray = Array.isArray(entities);
normalized = entities;

const ids = isArray ? (entities as E[]).map(entity => entity[idKey]) : Object.keys(normalized as HashMap<E>).map(id => entities[id][idKey]);
if (isArray) {
normalized = this.keyBy(entities as E[], entityClass, idKey) as E[];
} else {
assertEntityState(entities);
}

ids = isArray ? (entities as E[]).map(entity => entity[idKey]) : Object.keys(normalized as HashMap<E>).map(id => entities[id][idKey]);
}

return {
...(state as any),
Expand Down

0 comments on commit f2fc223

Please sign in to comment.