All files / actions entity.ts

78.13% Statements 25/32
50% Branches 2/4
12.5% Functions 1/8
100% Lines 25/25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142    2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x                                                                                                                                                   2x                 2x             2x             2x             2x           2x             2x          
import { ErrorAction } from './action.types';
 
export enum EntityActions {
  beginInsert = 'entity.beginInsert',
  commitInsert = 'entity.commitInsert',
  insertResult = 'entity.insertResult',
  update = 'entity.update',
  updating = 'entity.updating',
  updated = 'entity.updated',
  delete = 'entity.delete',
  deleting = 'entity.deleting',
  deleted = 'entity.deleted',
  fetch = 'entity.fetch',
  fetching = 'entity.fetching',
  fetched = 'entity.fetched',
  query = 'entity.query',
  querying = 'entity.querying',
  queried = 'entity.queried',
  selectAll = 'entity.selectAll',
  select = 'entity.select'
}
 
//tslint:disable:no-reserved-keywords
export interface BaseEntity<TN> extends ErrorAction {
  target: TN;
  asyncKey?: Symbol;
}
 
export interface BeginInsert<T, TN> extends BaseEntity<TN> {
  type: EntityActions.beginInsert;
  entity: T;
}
 
export interface CommitInsert<T, TN> extends BaseEntity<TN> {
  type: EntityActions.commitInsert;
  entity: T;
}
 
export interface InsertResult<T, TN> extends BaseEntity<TN> {
  type: EntityActions.insertResult;
  entity: T;
}
 
export interface UpdateEntity<T, TN> extends BaseEntity<TN> {
  type: EntityActions.update;
  entity: T;
}
 
export interface DeleteEntity<T, TN> extends BaseEntity<TN> {
  type: EntityActions.delete;
  entity: T;
}
 
export interface FetchEntity<TN> extends BaseEntity<TN> {
  type: EntityActions.fetch;
  id: string;
}
 
export interface FetchResult<T, TN> extends BaseEntity<TN> {
  type: EntityActions.fetched;
  id: string;
  entity: T;
}
 
export interface QueryEntities<TN> extends BaseEntity<TN> {
  type: EntityActions.query;
  query: string;
  continuationToken?: string;
  skip?: number;
  take?: number;
}
 
export interface QueryResult<T, TN> extends BaseEntity<TN> {
  type: EntityActions.queried;
  items: T[];
  hasMore: boolean;
  prevToken?: string;
  continuationToken?: string;
}
 
export interface SelectEntity<T, TN> extends BaseEntity<TN> {
  type: EntityActions.select;
  entity: T;
  select: boolean;
}
 
export interface SelectAll<T, TN> extends BaseEntity<TN> {
  type: EntityActions.selectAll;
  select: boolean;
}
 
export type EntityAction<T, TN> = QueryEntities<TN> | QueryResult<T, TN> | FetchEntity<TN> | FetchResult<T, TN> | SelectEntity<T, TN> | SelectAll<T, TN>;
 
export const beginQuery = <TN>(target: TN, query: string, asyncKey: Symbol, skip?: number, take?: number): QueryEntities<TN> => ({
  type: EntityActions.query,
  target,
  query,
  asyncKey,
  skip,
  take
});
 
export const beginUpdate = <T, TN>(target: TN, entity: T, asyncKey: Symbol): UpdateEntity<T, TN> => ({
  type: EntityActions.update,
  target,
  entity,
  asyncKey
});
 
export const beginDelete = <T, TN>(target: TN, entity: T, asyncKey: Symbol): DeleteEntity<T, TN> => ({
  type: EntityActions.delete,
  target,
  entity,
  asyncKey
});
 
export const queryResult = <T, TN>(target: TN, items: T[]): QueryResult<T, TN> => ({
  type: EntityActions.queried,
  items,
  target,
  hasMore: items && items.length > 0
});
 
export const beginInsert = <T, TN>(target: TN, entity: T): BeginInsert<T, TN> => ({
  type: EntityActions.beginInsert,
  target,
  entity
});
 
export const selectEntity = <T, TN>(target: TN, entity: T, select: boolean): SelectEntity<T, TN> => ({
  type: EntityActions.select,
  target,
  entity,
  select
});
 
export const selectAll = <T, TN>(target: TN, select: boolean): SelectAll<T, TN> => ({
  type: EntityActions.selectAll,
  target,
  select
});