|
| 1 | +/** |
| 2 | + * @module @ember-data/legacy-compat/builders |
| 3 | + */ |
| 4 | +import { assert } from '@ember/debug'; |
| 5 | + |
| 6 | +import type { StoreRequestInput } from '@ember-data/store'; |
| 7 | +import type { QueryOptions } from '@ember-data/store/-types/q/store'; |
| 8 | +import type { TypedRecordInstance, TypeFromInstance } from '@warp-drive/core-types/record'; |
| 9 | +import { SkipCache } from '@warp-drive/core-types/request'; |
| 10 | +import type { RequestSignature } from '@warp-drive/core-types/symbols'; |
| 11 | + |
| 12 | +import { normalizeModelName } from './utils'; |
| 13 | + |
| 14 | +type QueryRequestInput<T extends string = string, RT = unknown[]> = StoreRequestInput & { |
| 15 | + op: 'query'; |
| 16 | + data: { |
| 17 | + type: T; |
| 18 | + query: Record<string, unknown>; |
| 19 | + options: QueryBuilderOptions; |
| 20 | + }; |
| 21 | + [RequestSignature]?: RT; |
| 22 | +}; |
| 23 | + |
| 24 | +type QueryBuilderOptions = QueryOptions; |
| 25 | + |
| 26 | +/** |
| 27 | + This function builds a request config for a given type and query object. |
| 28 | + When passed to `store.request`, this config will result in the same behavior as a `store.query` request. |
| 29 | + Additionally, it takes the same options as `store.query`. |
| 30 | +
|
| 31 | + All `@ember-data/legacy-compat` builders exist to enable you to migrate your codebase to using the correct syntax for `store.request` while temporarily preserving legacy behaviors. |
| 32 | + This is useful for quickly upgrading an entire app to a unified syntax while a longer incremental migration is made to shift off of adapters and serializers. |
| 33 | + To that end, these builders are deprecated and will be removed in a future version of Ember Data. |
| 34 | +
|
| 35 | + @method query |
| 36 | + @deprecated |
| 37 | + @public |
| 38 | + @static |
| 39 | + @for @ember-data/legacy-compat/builders |
| 40 | + @param {string} type the name of the resource |
| 41 | + @param {object} query a query to be used by the adapter |
| 42 | + @param {QueryBuilderOptions} [options] optional, may include `adapterOptions` hash which will be passed to adapter.query |
| 43 | + @return {QueryRequestInput} request config |
| 44 | +*/ |
| 45 | +export function queryBuilder<T extends TypedRecordInstance>( |
| 46 | + type: TypeFromInstance<T>, |
| 47 | + query: Record<string, unknown>, |
| 48 | + options?: QueryBuilderOptions |
| 49 | +): QueryRequestInput<TypeFromInstance<T>, T[]>; |
| 50 | +export function queryBuilder( |
| 51 | + type: string, |
| 52 | + query: Record<string, unknown>, |
| 53 | + options?: QueryBuilderOptions |
| 54 | +): QueryRequestInput; |
| 55 | +export function queryBuilder( |
| 56 | + type: string, |
| 57 | + query: Record<string, unknown>, |
| 58 | + options: QueryBuilderOptions = {} |
| 59 | +): QueryRequestInput { |
| 60 | + assert(`You need to pass a model name to the query builder`, type); |
| 61 | + assert(`You need to pass a query hash to the query builder`, query); |
| 62 | + assert( |
| 63 | + `Model name passed to the query builder must be a dasherized string instead of ${type}`, |
| 64 | + typeof type === 'string' |
| 65 | + ); |
| 66 | + |
| 67 | + return { |
| 68 | + op: 'query' as const, |
| 69 | + data: { |
| 70 | + type: normalizeModelName(type), |
| 71 | + query, |
| 72 | + options: options, |
| 73 | + }, |
| 74 | + cacheOptions: { [SkipCache as symbol]: true }, |
| 75 | + }; |
| 76 | +} |
| 77 | + |
| 78 | +type QueryRecordRequestInput<T extends string = string, RT = unknown> = StoreRequestInput & { |
| 79 | + op: 'queryRecord'; |
| 80 | + data: { |
| 81 | + type: T; |
| 82 | + query: Record<string, unknown>; |
| 83 | + options: QueryBuilderOptions; |
| 84 | + }; |
| 85 | + [RequestSignature]?: RT; |
| 86 | +}; |
| 87 | + |
| 88 | +/** |
| 89 | + This function builds a request config for a given type and query object. |
| 90 | + When passed to `store.request`, this config will result in the same behavior as a `store.queryRecord` request. |
| 91 | + Additionally, it takes the same options as `store.queryRecord`. |
| 92 | +
|
| 93 | + All `@ember-data/legacy-compat` builders exist to enable you to migrate your codebase to using the correct syntax for `store.request` while temporarily preserving legacy behaviors. |
| 94 | + This is useful for quickly upgrading an entire app to a unified syntax while a longer incremental migration is made to shift off of adapters and serializers. |
| 95 | + To that end, these builders are deprecated and will be removed in a future version of Ember Data. |
| 96 | +
|
| 97 | + @method queryRecord |
| 98 | + @deprecated |
| 99 | + @public |
| 100 | + @static |
| 101 | + @for @ember-data/legacy-compat/builders |
| 102 | + @param {string} type the name of the resource |
| 103 | + @param {object} query a query to be used by the adapter |
| 104 | + @param {QueryBuilderOptions} [options] optional, may include `adapterOptions` hash which will be passed to adapter.query |
| 105 | + @return {QueryRecordRequestInput} request config |
| 106 | +*/ |
| 107 | +export function queryRecordBuilder<T extends TypedRecordInstance>( |
| 108 | + type: TypeFromInstance<T>, |
| 109 | + query: Record<string, unknown>, |
| 110 | + options?: QueryBuilderOptions |
| 111 | +): QueryRecordRequestInput<TypeFromInstance<T>, T | null>; |
| 112 | +export function queryRecordBuilder( |
| 113 | + type: string, |
| 114 | + query: Record<string, unknown>, |
| 115 | + options?: QueryBuilderOptions |
| 116 | +): QueryRecordRequestInput; |
| 117 | +export function queryRecordBuilder( |
| 118 | + type: string, |
| 119 | + query: Record<string, unknown>, |
| 120 | + options?: QueryBuilderOptions |
| 121 | +): QueryRecordRequestInput { |
| 122 | + assert(`You need to pass a model name to the queryRecord builder`, type); |
| 123 | + assert(`You need to pass a query hash to the queryRecord builder`, query); |
| 124 | + assert( |
| 125 | + `Model name passed to the queryRecord builder must be a dasherized string instead of ${type}`, |
| 126 | + typeof type === 'string' |
| 127 | + ); |
| 128 | + |
| 129 | + return { |
| 130 | + op: 'queryRecord', |
| 131 | + data: { |
| 132 | + type: normalizeModelName(type), |
| 133 | + query, |
| 134 | + options: options || {}, |
| 135 | + }, |
| 136 | + cacheOptions: { [SkipCache as symbol]: true }, |
| 137 | + }; |
| 138 | +} |
0 commit comments