-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
584eb0d
commit 57fb11d
Showing
5 changed files
with
151 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
tests/main/tests/integration/legacy-compat/find-all-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
); | ||
}); | ||
}); |