Skip to content

Commit 57fb11d

Browse files
committed
findAll
1 parent 584eb0d commit 57fb11d

File tree

5 files changed

+151
-6
lines changed

5 files changed

+151
-6
lines changed

packages/legacy-compat/src/builders.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
export { findAllBuilder as findAll } from './builders/find-all';
2+
export type { FindAllBuilderOptions, FindAllRequestInput } from './builders/find-all';
3+
14
export { findRecordBuilder as findRecord } from './builders/find-record';
25
export type { FindRecordBuilderOptions, FindRecordRequestInput } from './builders/find-record';
36

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { assert } from '@ember/debug';
2+
3+
import type { StoreRequestInput } from '@ember-data/store';
4+
import type { FindAllOptions } from '@ember-data/store/-types/q/store';
5+
import type { TypeFromInstance } from '@warp-drive/core-types/record';
6+
import { SkipCache } from '@warp-drive/core-types/request';
7+
8+
import { normalizeModelName } from './utils';
9+
10+
export type FindAllRequestInput = StoreRequestInput & {
11+
op: 'findAll';
12+
data: {
13+
type: string;
14+
options: FindAllBuilderOptions;
15+
};
16+
};
17+
18+
export type FindAllBuilderOptions = FindAllOptions;
19+
20+
/**
21+
This function builds a request config for the given type.
22+
When passed to `store.request`, this config will result in the same behavior as a `store.findAll` request.
23+
Additionally, it takes the same options as `store.findAll`.
24+
25+
@since x.x.x
26+
@method query
27+
@public
28+
@param {String} type the name of the resource
29+
@param {object} query a query to be used by the adapter
30+
@param {FindAllBuilderOptions} options optional, may include `adapterOptions` hash which will be passed to adapter.query
31+
@return {FindAllRequestInput} request config
32+
*/
33+
export function findAllBuilder<T>(type: TypeFromInstance<T>, options?: FindAllBuilderOptions): FindAllRequestInput;
34+
export function findAllBuilder(type: string, options?: FindAllBuilderOptions): FindAllRequestInput;
35+
export function findAllBuilder<T>(
36+
type: TypeFromInstance<T> | string,
37+
options: FindAllBuilderOptions = {}
38+
): FindAllRequestInput {
39+
assert(`You need to pass a model name to the findAll builder`, type);
40+
assert(
41+
`Model name passed to the findAll builder must be a dasherized string instead of ${type}`,
42+
typeof type === 'string'
43+
);
44+
45+
return {
46+
op: 'findAll',
47+
data: {
48+
type: normalizeModelName(type),
49+
options: options || {},
50+
},
51+
cacheOptions: { [SkipCache as symbol]: true },
52+
};
53+
}

packages/legacy-compat/src/builders/find-record.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export type FindRecordBuilderOptions = Omit<FindRecordOptions, 'preload'>;
6161
@public
6262
@param {String|object} type - either a string representing the name of the resource or a ResourceIdentifier object containing both the type (a string) and the id (a string) for the record or an lid (a string) of an existing record
6363
@param {(String|Integer|Object)} id - optional object with options for the request only if the first param is a ResourceIdentifier, else the string id of the record to be retrieved
64-
@param {Object} [options] - if the first param is a string this will be the optional options for the request. See examples for available options.
64+
@param {FindRecordBuilderOptions} [options] - if the first param is a string this will be the optional options for the request. See examples for available options.
6565
@return {FindRecordRequestInput} request config
6666
*/
6767
export function findRecordBuilder<T>(

packages/legacy-compat/src/builders/query.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,23 @@ export type QueryBuilderOptions = QueryOptions;
2828
@public
2929
@param {String} type the name of the resource
3030
@param {object} query a query to be used by the adapter
31-
@param {Object} options optional, may include `adapterOptions` hash which will be passed to adapter.query
31+
@param {QueryBuilderOptions} options optional, may include `adapterOptions` hash which will be passed to adapter.query
3232
@return {QueryRequestInput} request config
3333
*/
3434
export function queryBuilder<T>(
3535
type: TypeFromInstance<T>,
3636
query: Record<string, unknown>,
37-
options?: QueryOptions
37+
options?: QueryBuilderOptions
38+
): QueryRequestInput;
39+
export function queryBuilder(
40+
type: string,
41+
query: Record<string, unknown>,
42+
options?: QueryBuilderOptions
3843
): QueryRequestInput;
39-
export function queryBuilder(type: string, query: Record<string, unknown>, options?: QueryOptions): QueryRequestInput;
4044
export function queryBuilder(
4145
type: string,
4246
query: Record<string, unknown>,
43-
options: QueryOptions = {}
47+
options: QueryBuilderOptions = {}
4448
): QueryRequestInput {
4549
assert(`You need to pass a model name to the query builder`, type);
4650
assert(`You need to pass a query hash to the query builder`, query);
@@ -85,7 +89,7 @@ export type QueryRecordRequestInput = StoreRequestInput & {
8589
export function queryRecordBuilder(
8690
modelName: string,
8791
query: Record<string, unknown>,
88-
options?: QueryOptions
92+
options?: QueryBuilderOptions
8993
): QueryRecordRequestInput {
9094
assert(`You need to pass a model name to the queryRecord builder`, modelName);
9195
assert(`You need to pass a query hash to the queryRecord builder`, query);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { module, test } from 'qunit';
2+
3+
import { setupTest } from 'ember-qunit';
4+
5+
import type { CompatStore } from '@ember-data/legacy-compat';
6+
import type { QueryBuilderOptions } from '@ember-data/legacy-compat/builders';
7+
import { findAll } from '@ember-data/legacy-compat/builders';
8+
import Model, { attr } from '@ember-data/model';
9+
10+
module('Integration - legacy-compat/builders/findAll', function (hooks) {
11+
setupTest(hooks);
12+
13+
test('basic payload', async function (assert) {
14+
class Post extends Model {
15+
@attr declare name: string;
16+
}
17+
this.owner.register('model:post', Post);
18+
this.owner.register(
19+
'adapter:application',
20+
class Adapter {
21+
findAll() {
22+
assert.step('adapter-findAll');
23+
return Promise.resolve({
24+
data: [
25+
{
26+
id: '1',
27+
type: 'post',
28+
attributes: {
29+
name: 'Krystan rules, you drool',
30+
},
31+
},
32+
],
33+
});
34+
}
35+
static create() {
36+
return new this();
37+
}
38+
}
39+
);
40+
41+
const store = this.owner.lookup('service:store') as CompatStore;
42+
const { content: results } = await store.request<Post[]>(findAll('post'));
43+
44+
assert.strictEqual(results.length, 1, 'post was found');
45+
assert.strictEqual(results[0].id, '1', 'post has correct id');
46+
assert.strictEqual(results[0].name, 'Krystan rules, you drool', 'post has correct name');
47+
assert.verifySteps(['adapter-findAll'], 'adapter-findAll was called');
48+
});
49+
50+
test('findAll', function (assert) {
51+
const result = findAll('post');
52+
assert.deepEqual(
53+
result,
54+
{
55+
op: 'findAll',
56+
data: {
57+
type: 'post',
58+
options: {},
59+
},
60+
cacheOptions: {},
61+
},
62+
`findAll works`
63+
);
64+
});
65+
66+
test('findAll with options', function (assert) {
67+
const options: Required<QueryBuilderOptions> = {
68+
whatever: true,
69+
adapterOptions: {},
70+
};
71+
const result = findAll('post', options);
72+
assert.deepEqual(
73+
result,
74+
{
75+
op: 'findAll',
76+
data: {
77+
type: 'post',
78+
options,
79+
},
80+
cacheOptions: {},
81+
},
82+
`findAll works with options`
83+
);
84+
});
85+
});

0 commit comments

Comments
 (0)