Skip to content

Commit 06b97bf

Browse files
authored
feat(data): add loadWithQuery method (#3717)
Closes #3088
1 parent e267d21 commit 06b97bf

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

modules/data/spec/dispatchers/entity-dispatcher.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,17 @@ export function commandDispatchTest(
262262
expect(entityName).toBe('Hero');
263263
expect(mergeStrategy).toBeUndefined();
264264
});
265+
266+
it('#loadWithQuery() dispatches QUERY_MANY', () => {
267+
dispatcher.loadWithQuery('name=B');
268+
269+
const { entityOp, data, entityName, mergeStrategy } =
270+
dispatchedAction().payload;
271+
expect(entityOp).toBe(EntityOp.QUERY_MANY);
272+
expect(entityName).toBe('Hero');
273+
expect(data).toEqual('name=B');
274+
expect(mergeStrategy).toBeUndefined(); //?
275+
});
265276
});
266277

267278
/*** Cache-only operations ***/

modules/data/spec/entity-services/entity-collection-service.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,23 @@ describe('EntityCollectionService', () => {
152152
dataService.setErrorResponse('getAll', error);
153153
heroCollectionService.load().subscribe(expectErrorToBe(error, done));
154154
});
155+
156+
it('loadWithQuery observable should emit heroes on success', (done: any) => {
157+
const hero1 = { id: 1, name: 'A' } as Hero;
158+
const hero2 = { id: 2, name: 'B' } as Hero;
159+
const heroes = [hero1, hero2];
160+
dataService.setResponse('getWithQuery', heroes);
161+
heroCollectionService.loadWithQuery({name: 'foo'}).subscribe(expectDataToBe(heroes, done));
162+
});
163+
164+
it('loadWithQuery observable should emit expected error when data service fails', (done: any) => {
165+
const httpError = { error: new Error('Test Failure'), status: 501 };
166+
const error = makeDataServiceError('GET', httpError);
167+
dataService.setErrorResponse('getWithQuery', error);
168+
heroCollectionService
169+
.loadWithQuery({name: 'foo'})
170+
.subscribe(expectErrorToBe(error, done));
171+
});
155172
});
156173

157174
describe('cancel', () => {

modules/data/src/dispatchers/entity-commands.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,21 @@ export interface EntityServerCommands<T> {
9191
*/
9292
load(options?: EntityActionOptions): Observable<T[]>;
9393

94+
/**
95+
* Dispatch action to query remote storage for the entities that satisfy a query expressed
96+
* with either a query parameter map or an HTTP URL query string, and
97+
* completely replace the cached collection with the queried entities.
98+
* @param queryParams the query in a form understood by the server
99+
* @param [options] options that influence load behavior
100+
* @returns A terminating Observable of the entities in the collection
101+
* after server reports successful query or the query error.
102+
* @see getWithQuery
103+
*/
104+
loadWithQuery(queryParams: QueryParams | string,
105+
options?: EntityActionOptions
106+
): Observable<T[]>;
107+
108+
94109
/**
95110
* Dispatch action to save the updated entity (or partial entity) in remote storage.
96111
* The update entity may be partial (but must have its key)

modules/data/src/dispatchers/entity-dispatcher-base.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,26 @@ export class EntityDispatcherBase<T> implements EntityDispatcher<T> {
316316
);
317317
}
318318

319+
/**
320+
* Dispatch action to query remote storage for the entities that satisfy a query expressed
321+
* with either a query parameter map or an HTTP URL query string,
322+
* and completely replace the cached collection with the queried entities.
323+
* @param queryParams the query in a form understood by the server
324+
* @param [options] options that influence load behavior
325+
* @returns A terminating Observable of the queried entities
326+
* after server reports successful query or the query error.
327+
*/
328+
loadWithQuery(queryParams: QueryParams | string,
329+
options?: EntityActionOptions
330+
): Observable<T[]> {
331+
options = this.setQueryEntityActionOptions(options);
332+
const action = this.createEntityAction(EntityOp.QUERY_MANY, queryParams, options);
333+
this.dispatch(action);
334+
return this.getResponseData$<T[]>(options.correlationId).pipe(
335+
shareReplay(1)
336+
);
337+
}
338+
319339
/**
320340
* Dispatch action to save the updated entity (or partial entity) in remote storage.
321341
* The update entity may be partial (but must have its key)

modules/data/src/entity-services/entity-collection-service-base.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,21 @@ export class EntityCollectionServiceBase<
245245
return this.dispatcher.load(options);
246246
}
247247

248+
/**
249+
* Dispatch action to query remote storage for the entities that satisfy a query expressed
250+
* with either a query parameter map or an HTTP URL query string,
251+
* and completely replace the cached collection with the queried entities.
252+
* @param queryParams the query in a form understood by the server
253+
* @param [options] options that influence load behavior
254+
* @returns Observable of the queried entities
255+
* after server reports successful query or the query error.
256+
*/
257+
loadWithQuery(queryParams: QueryParams | string,
258+
options?: EntityActionOptions
259+
): Observable<T[]> {
260+
return this.dispatcher.loadWithQuery(queryParams, options);
261+
}
262+
248263
/**
249264
* Dispatch action to save the updated entity (or partial entity) in remote storage.
250265
* The update entity may be partial (but must have its key)

0 commit comments

Comments
 (0)