forked from johnpapa/angular-ngrx-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity-commands.ts
209 lines (182 loc) · 8.41 KB
/
entity-commands.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { Observable } from 'rxjs';
import { EntityActionOptions } from '../actions/entity-action';
import { MergeStrategy } from '../actions/merge-strategy';
import { QueryParams } from '../dataservices/interfaces';
/** Commands that update the remote server. */
export interface EntityServerCommands<T> {
/**
* Dispatch action to save a new entity to remote storage.
* @param entity entity to add, which may omit its key if pessimistic and the server creates the key;
* must have a key if optimistic save.
* @returns A terminating Observable of the entity
* after server reports successful save or the save error.
*/
add(entity: T, options?: EntityActionOptions): Observable<T>;
/**
* Dispatch action to cancel the persistence operation (query or save) with the given correlationId.
* @param correlationId The correlation id for the corresponding EntityAction
* @param [reason] explains why canceled and by whom.
* @param [options] options such as the tag
*/
cancel(correlationId: any, reason?: string, options?: EntityActionOptions): void;
/**
* Dispatch action to delete entity from remote storage by key.
* @param key The entity to delete
* @param [options] options that influence save and merge behavior
* @returns A terminating Observable of the deleted key
* after server reports successful save or the save error.
*/
delete(entity: T, options?: EntityActionOptions): Observable<number | string>;
/**
* Dispatch action to delete entity from remote storage by key.
* @param key The primary key of the entity to remove
* @param [options] options that influence save and merge behavior
* @returns Observable of the deleted key
* after server reports successful save or the save error.
*/
delete(key: number | string, options?: EntityActionOptions): Observable<number | string>;
/**
* Dispatch action to query remote storage for all entities and
* merge the queried entities into the cached collection.
* @param [options] options that influence merge behavior
* @returns A terminating Observable of the collection
* after server reports successful query or the query error.
* @see load()
*/
getAll(options?: EntityActionOptions): Observable<T[]>;
/**
* Dispatch action to query remote storage for the entity with this primary key.
* If the server returns an entity,
* merge it into the cached collection.
* @param key The primary key of the entity to get.
* @param [options] options that influence merge behavior
* @returns A terminating Observable of the queried entities that are in the collection
* after server reports success or the query error.
*/
getByKey(key: any, options?: EntityActionOptions): Observable<T>;
/**
* Dispatch action to query remote storage for the entities that satisfy a query expressed
* with either a query parameter map or an HTTP URL query string,
* and merge the results into the cached collection.
* @params queryParams the query in a form understood by the server
* @param [options] options that influence merge behavior
* @returns A terminating Observable of the queried entities
* after server reports successful query or the query error.
*/
getWithQuery(queryParams: QueryParams | string, options?: EntityActionOptions): Observable<T[]>;
/**
* Dispatch action to query remote storage for all entities and
* completely replace the cached collection with the queried entities.
* @param [options] options that influence load behavior
* @returns A terminating Observable of the entities in the collection
* after server reports successful query or the query error.
* @see getAll
*/
load(options?: EntityActionOptions): Observable<T[]>;
/**
* Dispatch action to save the updated entity (or partial entity) in remote storage.
* The update entity may be partial (but must have its key)
* in which case it patches the existing entity.
* @param entity update entity, which might be a partial of T but must at least have its key.
* @param [options] options that influence save and merge behavior
* @returns A terminating Observable of the updated entity
* after server reports successful save or the save error.
*/
update(entity: Partial<T>, options?: EntityActionOptions): Observable<T>;
/**
* Dispatch action to save a new or update an existing entity to remote storage.
* Only dispatch this action if your server supports upsert.
* @param entity entity to upsert, which may omit its key if pessimistic and the server creates the key;
* must have a key if optimistic save.
* @returns A terminating Observable of the entity
* after server reports successful save or the save error.
*/
upsert(entity: T, options?: EntityActionOptions): Observable<T>;
}
/*** A collection's cache-only commands, which do not update remote storage ***/
export interface EntityCacheCommands<T> {
/**
* Replace all entities in the cached collection.
* Does not save to remote storage.
*/
addAllToCache(entities: T[], options?: EntityActionOptions): void;
/**
* Add a new entity directly to the cache.
* Does not save to remote storage.
* Ignored if an entity with the same primary key is already in cache.
*/
addOneToCache(entity: T, options?: EntityActionOptions): void;
/**
* Add multiple new entities directly to the cache.
* Does not save to remote storage.
* Entities with primary keys already in cache are ignored.
*/
addManyToCache(entities: T[], options?: EntityActionOptions): void;
/** Clear the cached entity collection */
clearCache(options?: EntityActionOptions): void;
/**
* Remove an entity directly from the cache.
* Does not delete that entity from remote storage.
* @param entity The entity to remove
*/
removeOneFromCache(entity: T, options?: EntityActionOptions): void;
/**
* Remove an entity directly from the cache.
* Does not delete that entity from remote storage.
* @param key The primary key of the entity to remove
*/
removeOneFromCache(key: number | string, options?: EntityActionOptions): void;
/**
* Remove multiple entities directly from the cache.
* Does not delete these entities from remote storage.
* @param entity The entities to remove
*/
removeManyFromCache(entities: T[], options?: EntityActionOptions): void;
/**
* Remove multiple entities directly from the cache.
* Does not delete these entities from remote storage.
* @param keys The primary keys of the entities to remove
*/
removeManyFromCache(keys: (number | string)[], options?: EntityActionOptions): void;
/**
* Update a cached entity directly.
* Does not update that entity in remote storage.
* Ignored if an entity with matching primary key is not in cache.
* The update entity may be partial (but must have its key)
* in which case it patches the existing entity.
*/
updateOneInCache(entity: Partial<T>, options?: EntityActionOptions): void;
/**
* Update multiple cached entities directly.
* Does not update these entities in remote storage.
* Entities whose primary keys are not in cache are ignored.
* Update entities may be partial but must at least have their keys.
* such partial entities patch their cached counterparts.
*/
updateManyInCache(entities: Partial<T>[], options?: EntityActionOptions): void;
/**
* Insert or update a cached entity directly.
* Does not save to remote storage.
* Upsert entity might be a partial of T but must at least have its key.
* Pass the Update<T> structure as the payload
*/
upsertOneInCache(entity: Partial<T>, options?: EntityActionOptions): void;
/**
* Insert or update multiple cached entities directly.
* Does not save to remote storage.
* Upsert entities might be partial but must at least have their keys.
* Pass an array of the Update<T> structure as the payload
*/
upsertManyInCache(entities: Partial<T>[], options?: EntityActionOptions): void;
/**
* Set the pattern that the collection's filter applies
* when using the `filteredEntities` selector.
*/
setFilter(pattern: any, options?: EntityActionOptions): void;
/** Set the loaded flag */
setLoaded(isLoaded: boolean, options?: EntityActionOptions): void;
/** Set the loading flag */
setLoading(isLoading: boolean, options?: EntityActionOptions): void;
}
/** Commands that dispatch entity actions for a collection */
export interface EntityCommands<T> extends EntityServerCommands<T>, EntityCacheCommands<T> {}