Skip to content

Commit

Permalink
findAll
Browse files Browse the repository at this point in the history
  • Loading branch information
gitKrystan committed Apr 4, 2024
1 parent 584eb0d commit 57fb11d
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 6 deletions.
3 changes: 3 additions & 0 deletions packages/legacy-compat/src/builders.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export { findAllBuilder as findAll } from './builders/find-all';
export type { FindAllBuilderOptions, FindAllRequestInput } from './builders/find-all';

export { findRecordBuilder as findRecord } from './builders/find-record';
export type { FindRecordBuilderOptions, FindRecordRequestInput } from './builders/find-record';

Expand Down
53 changes: 53 additions & 0 deletions packages/legacy-compat/src/builders/find-all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { assert } from '@ember/debug';

import type { StoreRequestInput } from '@ember-data/store';
import type { FindAllOptions } from '@ember-data/store/-types/q/store';
import type { TypeFromInstance } from '@warp-drive/core-types/record';
import { SkipCache } from '@warp-drive/core-types/request';

import { normalizeModelName } from './utils';

export type FindAllRequestInput = StoreRequestInput & {
op: 'findAll';
data: {
type: string;
options: FindAllBuilderOptions;
};
};

export type FindAllBuilderOptions = FindAllOptions;

/**
This function builds a request config for the given type.
When passed to `store.request`, this config will result in the same behavior as a `store.findAll` request.
Additionally, it takes the same options as `store.findAll`.
@since x.x.x
@method query
@public
@param {String} type the name of the resource
@param {object} query a query to be used by the adapter
@param {FindAllBuilderOptions} options optional, may include `adapterOptions` hash which will be passed to adapter.query
@return {FindAllRequestInput} request config
*/
export function findAllBuilder<T>(type: TypeFromInstance<T>, options?: FindAllBuilderOptions): FindAllRequestInput;
export function findAllBuilder(type: string, options?: FindAllBuilderOptions): FindAllRequestInput;
export function findAllBuilder<T>(
type: TypeFromInstance<T> | string,
options: FindAllBuilderOptions = {}
): FindAllRequestInput {
assert(`You need to pass a model name to the findAll builder`, type);
assert(
`Model name passed to the findAll builder must be a dasherized string instead of ${type}`,
typeof type === 'string'
);

return {
op: 'findAll',
data: {
type: normalizeModelName(type),
options: options || {},
},
cacheOptions: { [SkipCache as symbol]: true },
};
}
2 changes: 1 addition & 1 deletion packages/legacy-compat/src/builders/find-record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export type FindRecordBuilderOptions = Omit<FindRecordOptions, 'preload'>;
@public
@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
@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
@param {Object} [options] - if the first param is a string this will be the optional options for the request. See examples for available options.
@param {FindRecordBuilderOptions} [options] - if the first param is a string this will be the optional options for the request. See examples for available options.
@return {FindRecordRequestInput} request config
*/
export function findRecordBuilder<T>(
Expand Down
14 changes: 9 additions & 5 deletions packages/legacy-compat/src/builders/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,23 @@ export type QueryBuilderOptions = QueryOptions;
@public
@param {String} type the name of the resource
@param {object} query a query to be used by the adapter
@param {Object} options optional, may include `adapterOptions` hash which will be passed to adapter.query
@param {QueryBuilderOptions} options optional, may include `adapterOptions` hash which will be passed to adapter.query
@return {QueryRequestInput} request config
*/
export function queryBuilder<T>(
type: TypeFromInstance<T>,
query: Record<string, unknown>,
options?: QueryOptions
options?: QueryBuilderOptions
): QueryRequestInput;
export function queryBuilder(
type: string,
query: Record<string, unknown>,
options?: QueryBuilderOptions
): QueryRequestInput;
export function queryBuilder(type: string, query: Record<string, unknown>, options?: QueryOptions): QueryRequestInput;
export function queryBuilder(
type: string,
query: Record<string, unknown>,
options: QueryOptions = {}
options: QueryBuilderOptions = {}
): QueryRequestInput {
assert(`You need to pass a model name to the query builder`, type);
assert(`You need to pass a query hash to the query builder`, query);
Expand Down Expand Up @@ -85,7 +89,7 @@ export type QueryRecordRequestInput = StoreRequestInput & {
export function queryRecordBuilder(
modelName: string,
query: Record<string, unknown>,
options?: QueryOptions
options?: QueryBuilderOptions
): QueryRecordRequestInput {
assert(`You need to pass a model name to the queryRecord builder`, modelName);
assert(`You need to pass a query hash to the queryRecord builder`, query);
Expand Down
85 changes: 85 additions & 0 deletions tests/main/tests/integration/legacy-compat/find-all-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { module, test } from 'qunit';

import { setupTest } from 'ember-qunit';

import type { CompatStore } from '@ember-data/legacy-compat';
import type { QueryBuilderOptions } from '@ember-data/legacy-compat/builders';
import { findAll } from '@ember-data/legacy-compat/builders';
import Model, { attr } from '@ember-data/model';

module('Integration - legacy-compat/builders/findAll', function (hooks) {
setupTest(hooks);

test('basic payload', async function (assert) {
class Post extends Model {
@attr declare name: string;
}
this.owner.register('model:post', Post);
this.owner.register(
'adapter:application',
class Adapter {
findAll() {
assert.step('adapter-findAll');
return Promise.resolve({
data: [
{
id: '1',
type: 'post',
attributes: {
name: 'Krystan rules, you drool',
},
},
],
});
}
static create() {
return new this();
}
}
);

const store = this.owner.lookup('service:store') as CompatStore;
const { content: results } = await store.request<Post[]>(findAll('post'));

assert.strictEqual(results.length, 1, 'post was found');
assert.strictEqual(results[0].id, '1', 'post has correct id');
assert.strictEqual(results[0].name, 'Krystan rules, you drool', 'post has correct name');
assert.verifySteps(['adapter-findAll'], 'adapter-findAll was called');
});

test('findAll', function (assert) {
const result = findAll('post');
assert.deepEqual(
result,
{
op: 'findAll',
data: {
type: 'post',
options: {},
},
cacheOptions: {},
},
`findAll works`
);
});

test('findAll with options', function (assert) {
const options: Required<QueryBuilderOptions> = {
whatever: true,
adapterOptions: {},
};
const result = findAll('post', options);
assert.deepEqual(
result,
{
op: 'findAll',
data: {
type: 'post',
options,
},
cacheOptions: {},
},
`findAll works with options`
);
});
});

0 comments on commit 57fb11d

Please sign in to comment.