forked from johnpapa/angular-ngrx-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity-definition.ts
56 lines (48 loc) · 1.96 KB
/
entity-definition.ts
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
import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
import { EntitySelectors, EntitySelectorsFactory } from '../selectors/entity-selectors';
import { Comparer, Dictionary, IdSelector, Update } from '../utils/ngrx-entity-models';
import { EntityDispatcherDefaultOptions } from '../dispatchers/entity-dispatcher-default-options';
import { defaultSelectId } from '../utils/utilities';
import { EntityCollection } from '../reducers/entity-collection';
import { EntityFilterFn } from './entity-filters';
import { EntityMetadata } from './entity-metadata';
export interface EntityDefinition<T = any> {
entityName: string;
entityAdapter: EntityAdapter<T>;
entityDispatcherOptions?: Partial<EntityDispatcherDefaultOptions>;
initialState: EntityCollection<T>;
metadata: EntityMetadata<T>;
noChangeTracking: boolean;
selectId: IdSelector<T>;
sortComparer: false | Comparer<T>;
}
export function createEntityDefinition<T, S extends object>(metadata: EntityMetadata<T, S>): EntityDefinition<T> {
let entityName = metadata.entityName;
if (!entityName) {
throw new Error('Missing required entityName');
}
metadata.entityName = entityName = entityName.trim();
const selectId = metadata.selectId || defaultSelectId;
const sortComparer = (metadata.sortComparer = metadata.sortComparer || false);
const entityAdapter = createEntityAdapter<T>({ selectId, sortComparer });
const entityDispatcherOptions: Partial<EntityDispatcherDefaultOptions> = metadata.entityDispatcherOptions || {};
const initialState: EntityCollection<T> = entityAdapter.getInitialState({
entityName,
filter: '',
loaded: false,
loading: false,
changeState: {},
...(metadata.additionalCollectionState || {})
});
const noChangeTracking = metadata.noChangeTracking === true; // false by default
return {
entityName,
entityAdapter,
entityDispatcherOptions,
initialState,
metadata,
noChangeTracking,
selectId,
sortComparer
};
}