@ember-data-types/store 5.6.0-alpha.5 → 5.6.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/package.json +1 -1
  2. package/unstable-preview-types/-private.d.ts +3 -29
  3. package/unstable-preview-types/configure.d.ts +14 -14
  4. package/unstable-preview-types/index.d.ts +4 -225
  5. package/unstable-preview-types/types.d.ts +3 -6
  6. package/unstable-preview-types/-private/cache-handler/handler.d.ts +0 -59
  7. package/unstable-preview-types/-private/cache-handler/handler.d.ts.map +0 -1
  8. package/unstable-preview-types/-private/cache-handler/types.d.ts +0 -101
  9. package/unstable-preview-types/-private/cache-handler/types.d.ts.map +0 -1
  10. package/unstable-preview-types/-private/cache-handler/utils.d.ts +0 -34
  11. package/unstable-preview-types/-private/cache-handler/utils.d.ts.map +0 -1
  12. package/unstable-preview-types/-private/caches/cache-utils.d.ts +0 -11
  13. package/unstable-preview-types/-private/caches/cache-utils.d.ts.map +0 -1
  14. package/unstable-preview-types/-private/caches/identifier-cache.d.ts +0 -171
  15. package/unstable-preview-types/-private/caches/identifier-cache.d.ts.map +0 -1
  16. package/unstable-preview-types/-private/caches/instance-cache.d.ts +0 -63
  17. package/unstable-preview-types/-private/caches/instance-cache.d.ts.map +0 -1
  18. package/unstable-preview-types/-private/caches/resource-utils.d.ts +0 -12
  19. package/unstable-preview-types/-private/caches/resource-utils.d.ts.map +0 -1
  20. package/unstable-preview-types/-private/debug/utils.d.ts +0 -9
  21. package/unstable-preview-types/-private/debug/utils.d.ts.map +0 -1
  22. package/unstable-preview-types/-private/document.d.ts +0 -149
  23. package/unstable-preview-types/-private/document.d.ts.map +0 -1
  24. package/unstable-preview-types/-private/legacy-model-support/record-reference.d.ts +0 -166
  25. package/unstable-preview-types/-private/legacy-model-support/record-reference.d.ts.map +0 -1
  26. package/unstable-preview-types/-private/legacy-model-support/shim-model-class.d.ts +0 -19
  27. package/unstable-preview-types/-private/legacy-model-support/shim-model-class.d.ts.map +0 -1
  28. package/unstable-preview-types/-private/managers/cache-capabilities-manager.d.ts +0 -28
  29. package/unstable-preview-types/-private/managers/cache-capabilities-manager.d.ts.map +0 -1
  30. package/unstable-preview-types/-private/managers/cache-manager.d.ts +0 -430
  31. package/unstable-preview-types/-private/managers/cache-manager.d.ts.map +0 -1
  32. package/unstable-preview-types/-private/managers/notification-manager.d.ts +0 -95
  33. package/unstable-preview-types/-private/managers/notification-manager.d.ts.map +0 -1
  34. package/unstable-preview-types/-private/managers/record-array-manager.d.ts +0 -100
  35. package/unstable-preview-types/-private/managers/record-array-manager.d.ts.map +0 -1
  36. package/unstable-preview-types/-private/network/request-cache.d.ts +0 -106
  37. package/unstable-preview-types/-private/network/request-cache.d.ts.map +0 -1
  38. package/unstable-preview-types/-private/new-core-tmp/promise-state.d.ts +0 -257
  39. package/unstable-preview-types/-private/new-core-tmp/promise-state.d.ts.map +0 -1
  40. package/unstable-preview-types/-private/new-core-tmp/reactivity/configure.d.ts +0 -91
  41. package/unstable-preview-types/-private/new-core-tmp/reactivity/configure.d.ts.map +0 -1
  42. package/unstable-preview-types/-private/new-core-tmp/reactivity/internal.d.ts +0 -172
  43. package/unstable-preview-types/-private/new-core-tmp/reactivity/internal.d.ts.map +0 -1
  44. package/unstable-preview-types/-private/new-core-tmp/reactivity/signal.d.ts +0 -32
  45. package/unstable-preview-types/-private/new-core-tmp/reactivity/signal.d.ts.map +0 -1
  46. package/unstable-preview-types/-private/new-core-tmp/request-state.d.ts +0 -256
  47. package/unstable-preview-types/-private/new-core-tmp/request-state.d.ts.map +0 -1
  48. package/unstable-preview-types/-private/record-arrays/identifier-array.d.ts +0 -143
  49. package/unstable-preview-types/-private/record-arrays/identifier-array.d.ts.map +0 -1
  50. package/unstable-preview-types/-private/record-arrays/many-array.d.ts +0 -202
  51. package/unstable-preview-types/-private/record-arrays/many-array.d.ts.map +0 -1
  52. package/unstable-preview-types/-private/record-arrays/native-proxy-type-fix.d.ts +0 -118
  53. package/unstable-preview-types/-private/record-arrays/native-proxy-type-fix.d.ts.map +0 -1
  54. package/unstable-preview-types/-private/store-service.d.ts +0 -1597
  55. package/unstable-preview-types/-private/store-service.d.ts.map +0 -1
  56. package/unstable-preview-types/-private/store-service.type-test.d.ts +0 -4
  57. package/unstable-preview-types/-private/store-service.type-test.d.ts.map +0 -1
  58. package/unstable-preview-types/-private/utils/coerce-id.d.ts +0 -7
  59. package/unstable-preview-types/-private/utils/coerce-id.d.ts.map +0 -1
  60. package/unstable-preview-types/-private/utils/construct-resource.d.ts +0 -9
  61. package/unstable-preview-types/-private/utils/construct-resource.d.ts.map +0 -1
  62. package/unstable-preview-types/-private/utils/is-non-empty-string.d.ts +0 -4
  63. package/unstable-preview-types/-private/utils/is-non-empty-string.d.ts.map +0 -1
  64. package/unstable-preview-types/-private/utils/normalize-model-name.d.ts +0 -4
  65. package/unstable-preview-types/-private/utils/normalize-model-name.d.ts.map +0 -1
  66. package/unstable-preview-types/-private/utils/uuid-polyfill.d.ts +0 -4
  67. package/unstable-preview-types/-private/utils/uuid-polyfill.d.ts.map +0 -1
  68. package/unstable-preview-types/-private.d.ts.map +0 -1
  69. package/unstable-preview-types/-types/q/cache-capabilities-manager.d.ts +0 -102
  70. package/unstable-preview-types/-types/q/cache-capabilities-manager.d.ts.map +0 -1
  71. package/unstable-preview-types/-types/q/ds-model.d.ts +0 -24
  72. package/unstable-preview-types/-types/q/ds-model.d.ts.map +0 -1
  73. package/unstable-preview-types/-types/q/identifier.d.ts +0 -175
  74. package/unstable-preview-types/-types/q/identifier.d.ts.map +0 -1
  75. package/unstable-preview-types/-types/q/promise-proxies.d.ts +0 -4
  76. package/unstable-preview-types/-types/q/promise-proxies.d.ts.map +0 -1
  77. package/unstable-preview-types/-types/q/record-data-json-api.d.ts +0 -33
  78. package/unstable-preview-types/-types/q/record-data-json-api.d.ts.map +0 -1
  79. package/unstable-preview-types/-types/q/record-instance.d.ts +0 -24
  80. package/unstable-preview-types/-types/q/record-instance.d.ts.map +0 -1
  81. package/unstable-preview-types/-types/q/schema-service.d.ts +0 -334
  82. package/unstable-preview-types/-types/q/schema-service.d.ts.map +0 -1
  83. package/unstable-preview-types/-types/q/store.d.ts +0 -37
  84. package/unstable-preview-types/-types/q/store.d.ts.map +0 -1
  85. package/unstable-preview-types/configure.d.ts.map +0 -1
  86. package/unstable-preview-types/index.d.ts.map +0 -1
  87. package/unstable-preview-types/types.d.ts.map +0 -1
@@ -1,1597 +0,0 @@
1
- declare module '@ember-data/store/-private/store-service' {
2
- import type RequestManager from '@ember-data/request';
3
- import type { Future } from '@ember-data/request';
4
- import type { Cache } from '@warp-drive/core-types/cache';
5
- import type { Graph } from '@warp-drive/core-types/graph';
6
- import type { StableExistingRecordIdentifier, StableRecordIdentifier } from '@warp-drive/core-types/identifier';
7
- import type { TypedRecordInstance, TypeFromInstance } from '@warp-drive/core-types/record';
8
- import type { CollectionResourceDocument, EmptyResourceDocument, JsonApiDocument, ResourceIdentifierObject, SingleResourceDocument } from '@warp-drive/core-types/spec/json-api-raw';
9
- import type { Type } from '@warp-drive/core-types/symbols';
10
- import type { CacheCapabilitiesManager } from '@ember-data/store/-types/q/cache-capabilities-manager';
11
- import type { ModelSchema } from '@ember-data/store/-types/q/ds-model';
12
- import type { OpaqueRecordInstance } from '@ember-data/store/-types/q/record-instance';
13
- import type { SchemaService } from '@ember-data/store/-types/q/schema-service';
14
- import type { FindAllOptions, FindRecordOptions, LegacyResourceQuery, QueryOptions } from '@ember-data/store/-types/q/store';
15
- import type { StoreRequestInput } from '@ember-data/store/-private/cache-handler/handler';
16
- import type { CachePolicy } from '@ember-data/store/-private/cache-handler/types';
17
- import { IdentifierCache } from '@ember-data/store/-private/caches/identifier-cache';
18
- import { InstanceCache, storeFor } from '@ember-data/store/-private/caches/instance-cache';
19
- import type RecordReference from '@ember-data/store/-private/legacy-model-support/record-reference';
20
- import NotificationManager from '@ember-data/store/-private/managers/notification-manager';
21
- import { RecordArrayManager } from '@ember-data/store/-private/managers/record-array-manager';
22
- import { RequestStateService } from '@ember-data/store/-private/network/request-cache';
23
- import type { Collection, IdentifierArray } from '@ember-data/store/-private/record-arrays/identifier-array';
24
- export { storeFor };
25
- type EmberObjectKey = '_debugContainerKey' | '_super' | 'addObserver' | 'cacheFor' | 'concatenatedProperties' | 'decrementProperty' | 'destroy' | 'get' | 'getProperties' | 'incrementProperty' | 'init' | 'isDestroyed' | 'isDestroying' | 'mergedProperties' | 'notifyPropertyChange' | 'removeObserver' | 'reopen' | 'set' | 'setProperties' | 'toggleProperty' | 'toString' | 'willDestroy';
26
- type DSModelKeys = '___(unique) Symbol(Store)' | '___private_notifications' | '___recordState' | '_createSnapshot' | 'adapterError' | 'attr' | 'belongsTo' | 'changedAttributes' | 'currentState' | 'deleteRecord' | 'destroyRecord' | 'dirtyType' | 'eachAttribute' | 'eachRelationship' | 'errors' | 'hasDirtyAttributes' | 'hasMany' | 'inverseFor' | 'isDeleted' | 'isEmpty' | 'isError' | 'isLoaded' | 'isLoading' | 'isNew' | 'isReloading' | 'isSaving' | 'isValid' | 'relationshipFor' | 'reload' | 'rollbackAttributes' | 'save' | 'serialize' | 'store' | 'unloadRecord';
27
- type DownlevelArrays<T> = T extends Array<infer U> ? U[] : T;
28
- type AwaitedKeys<T> = {
29
- [K in keyof T & string]: DownlevelArrays<Awaited<T[K]>>;
30
- };
31
- type FilteredKeys<T> = AwaitedKeys<Omit<T, typeof Type | EmberObjectKey | DSModelKeys | 'constructor'>>;
32
- type MaybeHasId = {
33
- id?: string | null;
34
- };
35
- /**
36
- * Currently only records that extend object can be created via
37
- * store.createRecord. This is a limitation of the current API,
38
- * but can be worked around by creating a new identifier, running
39
- * the cache.clientDidCreate method, and then peeking the record
40
- * for the identifier.
41
- *
42
- * To assign primary key to a record during creation, only `id` will
43
- * work correctly for `store.createRecord`, other primary key may be
44
- * handled by updating the record after creation or using the flow
45
- * described above.
46
- *
47
- * TODO: These are limitations we want to (and can) address. If you
48
- * have need of lifting these limitations, please open an issue.
49
- *
50
- */
51
- export type CreateRecordProperties<T = MaybeHasId & Record<string, unknown>> = T extends TypedRecordInstance ? Partial<FilteredKeys<T>> : T extends MaybeHasId ? MaybeHasId & Partial<FilteredKeys<T>> : MaybeHasId & Record<string, unknown>;
52
- const BaseClass: {
53
- new (args?: unknown): {};
54
- };
55
- export interface Store {
56
- createCache(capabilities: CacheCapabilitiesManager): Cache;
57
- /**
58
- * This is the hook WarpDrive uses to create a record instance to give reactive access to
59
- * a resource in the cache.
60
- */
61
- instantiateRecord<T>(identifier: StableRecordIdentifier, createRecordArgs: {
62
- [key: string]: unknown;
63
- }): OpaqueRecordInstance;
64
- /**
65
- * This is the hook WarpDrive uses to remove a record instance that is no longer needed
66
- */
67
- teardownRecord(record: OpaqueRecordInstance): void;
68
- /**
69
- * This hook enables an app to supply a SchemaService
70
- * for use when information about a resource's schema needs
71
- * to be queried.
72
- *
73
- * This method will only be called once to instantiate the singleton
74
- * service, which can then be accessed via `store.schema`.
75
- *
76
- * For Example, to use the default SchemaService for SchemaRecord
77
- *
78
- * ```ts
79
- * import { SchemaService } from '@warp-drive/schema-record';
80
- *
81
- * class extends Store {
82
- * createSchemaService() {
83
- * return new SchemaService();
84
- * }
85
- * }
86
- * ```
87
- *
88
- * Or to use the SchemaService for @ember-data/model
89
- *
90
- * ```ts
91
- * import { buildSchema } from '@ember-data/model/hooks';
92
- *
93
- * class extends Store {
94
- * createSchemaService() {
95
- * return buildSchema(this);
96
- * }
97
- * }
98
- * ```
99
- *
100
- * If you wish to chain services, you must either
101
- * instantiate each schema source directly or super to retrieve
102
- * an existing service. For convenience, when migrating from
103
- * `@ember-data/model` to `@warp-drive/schema-record` a
104
- * SchemaService is provided that handles this transition
105
- * for you:
106
- *
107
- * ```ts
108
- * import { DelegatingSchemaService } from '@ember-data/model/migration-support';
109
- * import { SchemaService } from '@warp-drive/schema-record';
110
- *
111
- * class extends Store {
112
- * createSchemaService() {
113
- * const schema = new SchemaService();
114
- * return new DelegatingSchemaService(this, schema);
115
- * }
116
- * }
117
- * ```
118
- *
119
- * When using the DelegateSchemaService, the schema will first
120
- * be sourced from directly registered schemas, then will fallback
121
- * to sourcing a schema from available models if no schema is found.
122
- *
123
- * @return {SchemaService}
124
- * @public
125
- */
126
- createSchemaService(): SchemaService;
127
- /**
128
- * DEPRECATED - Use the property `store.schema` instead.
129
- *
130
- * Provides access to the SchemaDefinitionService instance
131
- * for this Store instance.
132
- *
133
- * The SchemaDefinitionService can be used to query for
134
- * information about the schema of a resource.
135
- *
136
- * @deprecated
137
- * @public
138
- */
139
- getSchemaDefinitionService(): SchemaService;
140
- /**
141
- * DEPRECATED - Use `createSchemaService` instead.
142
- *
143
- * Allows an app to register a custom SchemaService
144
- * for use when information about a resource's schema needs
145
- * to be queried.
146
- *
147
- * This method can only be called more than once, but only one schema
148
- * definition service may exist. Therefore if you wish to chain services
149
- * you must lookup the existing service and close over it with the new
150
- * service by accessing `store.schema` prior to registration.
151
- *
152
- * For Example:
153
- *
154
- * ```ts
155
- * import Store from '@ember-data/store';
156
- *
157
- * class SchemaDelegator {
158
- * constructor(schema) {
159
- * this._schema = schema;
160
- * }
161
- *
162
- * hasResource(resource: { type: string }): boolean {
163
- * if (AbstractSchemas.has(resource.type)) {
164
- * return true;
165
- * }
166
- * return this._schema.hasResource(resource);
167
- * }
168
- *
169
- * attributesDefinitionFor(identifier: RecordIdentifier | { type: string }): AttributesSchema {
170
- * return this._schema.attributesDefinitionFor(identifier);
171
- * }
172
- *
173
- * relationshipsDefinitionFor(identifier: RecordIdentifier | { type: string }): RelationshipsSchema {
174
- * const schema = AbstractSchemas.get(identifier.type);
175
- * return schema || this._schema.relationshipsDefinitionFor(identifier);
176
- * }
177
- * }
178
- *
179
- * export default class extends Store {
180
- * constructor(...args) {
181
- * super(...args);
182
- *
183
- * const schema = this.createSchemaService();
184
- * this.registerSchemaDefinitionService(new SchemaDelegator(schema));
185
- * }
186
- * }
187
- * ```
188
- *
189
- * @param {SchemaService} schema
190
- * @deprecated
191
- * @public
192
- */
193
- registerSchemaDefinitionService(schema: SchemaService): void;
194
- /**
195
- * DEPRECATED - Use `createSchemaService` instead.
196
- *
197
- * Allows an app to register a custom SchemaService
198
- * for use when information about a resource's schema needs
199
- * to be queried.
200
- *
201
- * This method can only be called more than once, but only one schema
202
- * definition service may exist. Therefore if you wish to chain services
203
- * you must lookup the existing service and close over it with the new
204
- * service by accessing `store.schema` prior to registration.
205
- *
206
- * For Example:
207
- *
208
- * ```ts
209
- * import Store from '@ember-data/store';
210
- *
211
- * class SchemaDelegator {
212
- * constructor(schema) {
213
- * this._schema = schema;
214
- * }
215
- *
216
- * hasResource(resource: { type: string }): boolean {
217
- * if (AbstractSchemas.has(resource.type)) {
218
- * return true;
219
- * }
220
- * return this._schema.hasResource(resource);
221
- * }
222
- *
223
- * attributesDefinitionFor(identifier: RecordIdentifier | { type: string }): AttributesSchema {
224
- * return this._schema.attributesDefinitionFor(identifier);
225
- * }
226
- *
227
- * relationshipsDefinitionFor(identifier: RecordIdentifier | { type: string }): RelationshipsSchema {
228
- * const schema = AbstractSchemas.get(identifier.type);
229
- * return schema || this._schema.relationshipsDefinitionFor(identifier);
230
- * }
231
- * }
232
- *
233
- * export default class extends Store {
234
- * constructor(...args) {
235
- * super(...args);
236
- *
237
- * const schema = this.schema;
238
- * this.registerSchema(new SchemaDelegator(schema));
239
- * }
240
- * }
241
- * ```
242
- *
243
- * @param {SchemaService} schema
244
- * @deprecated
245
- * @public
246
- */
247
- registerSchema(schema: SchemaService): void;
248
- }
249
- export class Store extends BaseClass {
250
- /** @internal */
251
- recordArrayManager: RecordArrayManager;
252
- /**
253
- * Provides access to the NotificationManager associated
254
- * with this Store instance.
255
- *
256
- * The NotificationManager can be used to subscribe to
257
- * changes to the cache.
258
- *
259
- * @property notifications
260
- * @type {NotificationManager}
261
- * @public
262
- */
263
- notifications: NotificationManager;
264
- /**
265
- * Provides access to the SchemaService instance
266
- * for this Store instance.
267
- *
268
- * The SchemaService can be used to query for
269
- * information about the schema of a resource.
270
- *
271
- * @property schema
272
- * @type {SchemaService}
273
- * @public
274
- */
275
- get schema(): ReturnType<this['createSchemaService']>;
276
- /** @internal */
277
- _schema: SchemaService;
278
- /**
279
- * Provides access to the IdentifierCache instance
280
- * for this store.
281
- *
282
- * The IdentifierCache can be used to generate or
283
- * retrieve a stable unique identifier for any resource.
284
- *
285
- * @property identifierCache
286
- * @type {IdentifierCache}
287
- * @public
288
- */
289
- identifierCache: IdentifierCache;
290
- /**
291
- * Provides access to the requestManager instance associated
292
- * with this Store instance.
293
- *
294
- * When using `ember-data` this property is automatically
295
- * set to an instance of `RequestManager`. When not using `ember-data`
296
- * you must configure this property yourself, either by declaring
297
- * it as a service or by initializing it.
298
- *
299
- * ```ts
300
- * import Store, { CacheHandler } from '@ember-data/store';
301
- * import RequestManager from '@ember-data/request';
302
- * import Fetch from '@ember-data/request/fetch';
303
- *
304
- * class extends Store {
305
- * requestManager = new RequestManager()
306
- * .use([Fetch])
307
- * .useCache(CacheHandler);
308
- * }
309
- * ```
310
- *
311
- * @public
312
- * @property requestManager
313
- * @type {RequestManager}
314
- */
315
- requestManager: RequestManager;
316
- /**
317
- * A Property which an App may set to provide a CachePolicy
318
- * to control when a cached request becomes stale.
319
- *
320
- * Note, when defined, these methods will only be invoked if a
321
- * cache key exists for the request, either because the request
322
- * contains `cacheOptions.key` or because the [IdentifierCache](/ember-data/release/classes/IdentifierCache)
323
- * was able to generate a key for the request using the configured
324
- * [generation method](/ember-data/release/functions/@ember-data%2Fstore/setIdentifierGenerationMethod).
325
- *
326
- * `isSoftExpired` will only be invoked if `isHardExpired` returns `false`.
327
- *
328
- * ```ts
329
- * store.lifetimes = {
330
- * // make the request and ignore the current cache state
331
- * isHardExpired(identifier: StableDocumentIdentifier): boolean {
332
- * return false;
333
- * }
334
- *
335
- * // make the request in the background if true, return cache state
336
- * isSoftExpired(identifier: StableDocumentIdentifier): boolean {
337
- * return false;
338
- * }
339
- * }
340
- * ```
341
- *
342
- * @public
343
- * @property lifetimes
344
- * @type {CachePolicy|undefined}
345
- */
346
- lifetimes?: CachePolicy;
347
- /** @internal */
348
- _graph?: Graph;
349
- /** @internal */
350
- _requestCache: RequestStateService;
351
- /** @internal */
352
- _instanceCache: InstanceCache;
353
- /** @internal */
354
- _cbs: {
355
- coalesce?: () => void;
356
- sync?: () => void;
357
- notify?: () => void;
358
- } | null;
359
- /** @internal */
360
- _forceShim: boolean;
361
- /**
362
- * Async flush buffers notifications until flushed
363
- * by finalization of a future configured by store.request
364
- *
365
- * This is useful for ensuring that notifications are delivered
366
- * prior to the promise resolving but without risk of promise
367
- * interleaving.
368
- *
369
- * @internal
370
- */
371
- _enableAsyncFlush: boolean | null;
372
- /** @internal */
373
- DISABLE_WAITER?: boolean;
374
- /** @internal */
375
- _isDestroying: boolean;
376
- /** @internal */
377
- _isDestroyed: boolean;
378
- /** @internal */
379
- get isDestroying(): boolean;
380
- set isDestroying(value: boolean);
381
- /** @internal */
382
- get isDestroyed(): boolean;
383
- set isDestroyed(value: boolean);
384
- /**
385
- @private
386
- */
387
- constructor(createArgs?: unknown);
388
- /** @internal */
389
- _run(cb: () => void): void;
390
- /**
391
- * Executes the callback, ensurng that any work that calls
392
- * store._schedule is executed after in the right order.
393
- *
394
- * When queues already exist, scheduled callbacks will
395
- * join the existing queue.
396
- *
397
- * @internal
398
- */
399
- _join(cb: () => void): void;
400
- /** @internal */
401
- _schedule(name: 'coalesce' | 'sync' | 'notify', cb: () => void): void;
402
- /**
403
- * Retrieve the RequestStateService instance
404
- * associated with this Store.
405
- *
406
- * This can be used to query the status of requests
407
- * that have been initiated for a given identifier.
408
- *
409
- * @return {RequestStateService}
410
- * @public
411
- */
412
- getRequestStateService(): RequestStateService;
413
- /** @internal */
414
- _getAllPending(): (Promise<unknown[]> & {
415
- length: number;
416
- }) | void;
417
- /**
418
- * Issue a request via the configured RequestManager,
419
- * inserting the response into the cache and handing
420
- * back a Future which resolves to a ResponseDocument
421
- *
422
- * ## Cache Keys
423
- *
424
- * Only GET requests with a url or requests with an explicit
425
- * cache key (`cacheOptions.key`) will have the request result
426
- * and document cached.
427
- *
428
- * The cache key used is `requestConfig.cacheOptions.key`
429
- * if present, falling back to `requestConfig.url`.
430
- *
431
- * Params are not serialized as part of the cache-key, so
432
- * either ensure they are already in the url or utilize
433
- * `requestConfig.cacheOptions.key`. For queries issued
434
- * via the `POST` method `requestConfig.cacheOptions.key`
435
- * MUST be supplied for the document to be cached.
436
- *
437
- * ## Requesting Without a Cache Key
438
- *
439
- * Resource data within the request is always updated in the cache,
440
- * regardless of whether a cache key is present for the request.
441
- *
442
- * ## Fulfilling From Cache
443
- *
444
- * When a cache-key is determined, the request may fulfill
445
- * from cache provided the cache is not stale.
446
- *
447
- * Cache staleness is determined by the configured CachePolicy
448
- * with priority given to the `cacheOptions.reload` and
449
- * `cacheOptions.backgroundReload` on the request if present.
450
- *
451
- * If the cache data has soft expired or the request asks for a background
452
- * reload, the request will fulfill from cache if possible and
453
- * make a non-blocking request in the background to update the cache.
454
- *
455
- * If the cache data has hard expired or the request asks for a reload,
456
- * the request will not fulfill from cache and will make a blocking
457
- * request to update the cache.
458
- *
459
- * ## The Response
460
- *
461
- * The primary difference between `requestManager.request` and `store.request`
462
- * is that `store.request` will attempt to hydrate the response content into
463
- * a response Document containing RecordInstances.
464
- *
465
- * @param {StoreRequestInput} requestConfig
466
- * @return {Future}
467
- * @public
468
- */
469
- request<RT, T = unknown>(requestConfig: StoreRequestInput<RT, T>): Future<RT>;
470
- /**
471
- * A hook which an app or addon may implement. Called when
472
- * the Store is attempting to create a Record Instance for
473
- * a resource.
474
- *
475
- * This hook can be used to select or instantiate any desired
476
- * mechanism of presenting cache data to the ui for access
477
- * mutation, and interaction.
478
- *
479
- * @param identifier
480
- * @param createRecordArgs
481
- * @param recordDataFor deprecated use this.cache
482
- * @param notificationManager deprecated use this.notifications
483
- * @return A record instance
484
- * @public
485
- */
486
- /**
487
- * A hook which an app or addon may implement. Called when
488
- * the Store is destroying a Record Instance. This hook should
489
- * be used to teardown any custom record instances instantiated
490
- * with `instantiateRecord`.
491
- *
492
- * @public
493
- * @param record
494
- */
495
- /**
496
- Returns the schema for a particular resource type (modelName).
497
-
498
- When used with Model from @ember-data/model the return is the model class,
499
- but this is not guaranteed.
500
-
501
- If looking to query attribute or relationship information it is
502
- recommended to use `getSchemaDefinitionService` instead. This method
503
- should be considered legacy and exists primarily to continue to support
504
- Adapter/Serializer APIs which expect it's return value in their method
505
- signatures.
506
-
507
- The class of a model might be useful if you want to get a list of all the
508
- relationship names of the model, see
509
- [`relationshipNames`](/ember-data/release/classes/Model?anchor=relationshipNames)
510
- for example.
511
-
512
- @public
513
- @deprecated
514
- @param {String} type
515
- @return {ModelSchema}
516
- */
517
- modelFor<T>(type: TypeFromInstance<T>): ModelSchema<T>;
518
- modelFor(type: string): ModelSchema;
519
- /**
520
- Create a new record in the current store. The properties passed
521
- to this method are set on the newly created record.
522
-
523
- To create a new instance of a `Post`:
524
-
525
- ```js
526
- store.createRecord('post', {
527
- title: 'Ember is awesome!'
528
- });
529
- ```
530
-
531
- To create a new instance of a `Post` that has a relationship with a `User` record:
532
-
533
- ```js
534
- let user = this.store.peekRecord('user', '1');
535
- store.createRecord('post', {
536
- title: 'Ember is awesome!',
537
- user: user
538
- });
539
- ```
540
-
541
- @public
542
- @param {String} type the name of the resource
543
- @param {Object} inputProperties a hash of properties to set on the
544
- newly created record.
545
- @return {Model} record
546
- */
547
- createRecord<T>(type: TypeFromInstance<T>, inputProperties: CreateRecordProperties<T>): T;
548
- createRecord(type: string, inputProperties: CreateRecordProperties): OpaqueRecordInstance;
549
- /**
550
- For symmetry, a record can be deleted via the store.
551
-
552
- Example
553
-
554
- ```javascript
555
- let post = store.createRecord('post', {
556
- title: 'Ember is awesome!'
557
- });
558
-
559
- store.deleteRecord(post);
560
- ```
561
-
562
- @public
563
- @param {unknown} record
564
- */
565
- deleteRecord<T>(record: T): void;
566
- /**
567
- For symmetry, a record can be unloaded via the store.
568
- This will cause the record to be destroyed and freed up for garbage collection.
569
-
570
- Example
571
-
572
- ```javascript
573
- const { content: { data: post } } = await store.request(findRecord({ type: 'post', id: '1' }));
574
- store.unloadRecord(post);
575
- ```
576
-
577
- @public
578
- @param {Model} record
579
- */
580
- unloadRecord<T>(record: T): void;
581
- /**
582
- This method returns a record for a given identifier or type and id combination.
583
-
584
- The `findRecord` method will always resolve its promise with the same
585
- object for a given identifier or type and `id`.
586
-
587
- The `findRecord` method will always return a **promise** that will be
588
- resolved with the record.
589
-
590
- **Example 1**
591
-
592
- ```js [app/routes/post.js]
593
- export default class PostRoute extends Route {
594
- model({ post_id }) {
595
- return this.store.findRecord('post', post_id);
596
- }
597
- }
598
- ```
599
-
600
- **Example 2**
601
-
602
- `findRecord` can be called with a single identifier argument instead of the combination
603
- of `type` (modelName) and `id` as separate arguments. You may recognize this combo as
604
- the typical pairing from [JSON:API](https://jsonapi.org/format/#document-resource-object-identification)
605
-
606
- ```js [app/routes/post.js]
607
- export default class PostRoute extends Route {
608
- model({ post_id: id }) {
609
- return this.store.findRecord({ type: 'post', id });
610
- }
611
- }
612
- ```
613
-
614
- **Example 3**
615
-
616
- If you have previously received an lid via an Identifier for this record, and the record
617
- has already been assigned an id, you can find the record again using just the lid.
618
-
619
- ```js [app/routes/post.js]
620
- store.findRecord({ lid });
621
- ```
622
-
623
- If the record is not yet available, the store will ask the adapter's `findRecord`
624
- method to retrieve and supply the necessary data. If the record is already present
625
- in the store, it depends on the reload behavior _when_ the returned promise
626
- resolves.
627
-
628
- ### Preloading
629
-
630
- You can optionally `preload` specific attributes and relationships that you know of
631
- by passing them via the passed `options`.
632
-
633
- For example, if your Ember route looks like `/posts/1/comments/2` and your API route
634
- for the comment also looks like `/posts/1/comments/2` if you want to fetch the comment
635
- without also fetching the post you can pass in the post to the `findRecord` call:
636
-
637
- ```js [app/routes/post-comments.js]
638
- export default class PostRoute extends Route {
639
- model({ post_id, comment_id: id }) {
640
- return this.store.findRecord({ type: 'comment', id, { preload: { post: post_id }} });
641
- }
642
- }
643
- ```
644
-
645
- In your adapter you can then access this id without triggering a network request via the
646
- snapshot:
647
-
648
- ```js [app/adapters/application.js]
649
- export default class Adapter {
650
-
651
- findRecord(store, schema, id, snapshot) {
652
- let type = schema.modelName;
653
-
654
- if (type === 'comment')
655
- let postId = snapshot.belongsTo('post', { id: true });
656
-
657
- return fetch(`./posts/${postId}/comments/${id}`)
658
- .then(response => response.json())
659
- }
660
- }
661
-
662
- static create() {
663
- return new this();
664
- }
665
- }
666
- ```
667
-
668
- This could also be achieved by supplying the post id to the adapter via the adapterOptions
669
- property on the options hash.
670
-
671
- ```js [app/routes/post-comments.js]
672
- export default class PostRoute extends Route {
673
- model({ post_id, comment_id: id }) {
674
- return this.store.findRecord({ type: 'comment', id, { adapterOptions: { post: post_id }} });
675
- }
676
- }
677
- ```
678
-
679
- ```js [app/adapters/application.js]
680
- export default class Adapter {
681
- findRecord(store, schema, id, snapshot) {
682
- let type = schema.modelName;
683
-
684
- if (type === 'comment')
685
- let postId = snapshot.adapterOptions.post;
686
-
687
- return fetch(`./posts/${postId}/comments/${id}`)
688
- .then(response => response.json())
689
- }
690
- }
691
-
692
- static create() {
693
- return new this();
694
- }
695
- }
696
- ```
697
-
698
- If you have access to the post model you can also pass the model itself to preload:
699
-
700
- ```javascript
701
- let post = await store.findRecord('post', '1');
702
- let comment = await store.findRecord('comment', '2', { post: myPostModel });
703
- ```
704
-
705
- ### Reloading
706
-
707
- The reload behavior is configured either via the passed `options` hash or
708
- the result of the adapter's `shouldReloadRecord`.
709
-
710
- If `{ reload: true }` is passed or `adapter.shouldReloadRecord` evaluates
711
- to `true`, then the returned promise resolves once the adapter returns
712
- data, regardless if the requested record is already in the store:
713
-
714
- ```js
715
- store.push({
716
- data: {
717
- id: 1,
718
- type: 'post',
719
- revision: 1
720
- }
721
- });
722
-
723
- // adapter#findRecord resolves with
724
- // [
725
- // {
726
- // id: 1,
727
- // type: 'post',
728
- // revision: 2
729
- // }
730
- // ]
731
- store.findRecord('post', '1', { reload: true }).then(function(post) {
732
- post.revision; // 2
733
- });
734
- ```
735
-
736
- If no reload is indicated via the above mentioned ways, then the promise
737
- immediately resolves with the cached version in the store.
738
-
739
- ### Background Reloading
740
-
741
- Optionally, if `adapter.shouldBackgroundReloadRecord` evaluates to `true`,
742
- then a background reload is started, which updates the records' data, once
743
- it is available:
744
-
745
- ```js
746
- // app/adapters/post.js
747
- import ApplicationAdapter from "./application";
748
-
749
- export default class PostAdapter extends ApplicationAdapter {
750
- shouldReloadRecord(store, snapshot) {
751
- return false;
752
- },
753
-
754
- shouldBackgroundReloadRecord(store, snapshot) {
755
- return true;
756
- }
757
- });
758
-
759
- // ...
760
-
761
- store.push({
762
- data: {
763
- id: 1,
764
- type: 'post',
765
- revision: 1
766
- }
767
- });
768
-
769
- let blogPost = store.findRecord('post', '1').then(function(post) {
770
- post.revision; // 1
771
- });
772
-
773
- // later, once adapter#findRecord resolved with
774
- // [
775
- // {
776
- // id: 1,
777
- // type: 'post',
778
- // revision: 2
779
- // }
780
- // ]
781
-
782
- blogPost.revision; // 2
783
- ```
784
-
785
- If you would like to force or prevent background reloading, you can set a
786
- boolean value for `backgroundReload` in the options object for
787
- `findRecord`.
788
-
789
- ```js [app/routes/post/edit.js]
790
- export default class PostEditRoute extends Route {
791
- model(params) {
792
- return this.store.findRecord('post', params.post_id, { backgroundReload: false });
793
- }
794
- }
795
- ```
796
-
797
- If you pass an object on the `adapterOptions` property of the options
798
- argument it will be passed to your adapter via the snapshot
799
-
800
- ```js [app/routes/post/edit.js]
801
- export default class PostEditRoute extends Route {
802
- model(params) {
803
- return this.store.findRecord('post', params.post_id, {
804
- adapterOptions: { subscribe: false }
805
- });
806
- }
807
- }
808
- ```
809
-
810
- ```js [app/adapters/post.js]
811
- import MyCustomAdapter from './custom-adapter';
812
-
813
- export default class PostAdapter extends MyCustomAdapter {
814
- findRecord(store, type, id, snapshot) {
815
- if (snapshot.adapterOptions.subscribe) {
816
- // ...
817
- }
818
- // ...
819
- }
820
- }
821
- ```
822
-
823
- See [peekRecord](../methods/peekRecord?anchor=peekRecord) to get the cached version of a record.
824
-
825
- ### Retrieving Related Model Records
826
-
827
- If you use an adapter such as Ember's default
828
- [`JSONAPIAdapter`](/ember-data/release/classes/JSONAPIAdapter)
829
- that supports the [JSON API specification](http://jsonapi.org/) and if your server
830
- endpoint supports the use of an
831
- ['include' query parameter](http://jsonapi.org/format/#fetching-includes),
832
- you can use `findRecord()` or `findAll()` to automatically retrieve additional records related to
833
- the one you request by supplying an `include` parameter in the `options` object.
834
-
835
- For example, given a `post` model that has a `hasMany` relationship with a `comment`
836
- model, when we retrieve a specific post we can have the server also return that post's
837
- comments in the same request:
838
-
839
- ```js [app/routes/post.js]
840
- export default class PostRoute extends Route {
841
- model(params) {
842
- return this.store.findRecord('post', params.post_id, { include: ['comments'] });
843
- }
844
- }
845
- ```
846
-
847
- ```js [app/adapters/application.js]
848
- export default class Adapter {
849
- findRecord(store, schema, id, snapshot) {
850
- let type = schema.modelName;
851
-
852
- if (type === 'post')
853
- let includes = snapshot.adapterOptions.include;
854
-
855
- return fetch(`./posts/${postId}?include=${includes}`)
856
- .then(response => response.json())
857
- }
858
- }
859
-
860
- static create() {
861
- return new this();
862
- }
863
- }
864
- ```
865
-
866
- In this case, the post's comments would then be available in your template as
867
- `model.comments`.
868
-
869
- Multiple relationships can be requested using an `include` parameter consisting of a
870
- list of relationship names, while nested relationships can be specified
871
- using a dot-separated sequence of relationship names. So to request both the post's
872
- comments and the authors of those comments the request would look like this:
873
-
874
- ```js [app/routes/post.js]
875
- export default class PostRoute extends Route {
876
- model(params) {
877
- return this.store.findRecord('post', params.post_id, { include: ['comments','comments.author'] });
878
- }
879
- }
880
- ```
881
-
882
- ### Retrieving Specific Fields by Type
883
-
884
- If your server endpoint supports the use of a ['fields' query parameter](https://jsonapi.org/format/#fetching-sparse-fieldsets),
885
- you can use pass those fields through to your server. At this point in time, this requires a few manual steps on your part.
886
-
887
- 1. Implement `buildQuery` in your adapter.
888
-
889
- ```js [app/adapters/application.js]
890
- buildQuery(snapshot) {
891
- let query = super.buildQuery(...arguments);
892
-
893
- let { fields } = snapshot.adapterOptions;
894
-
895
- if (fields) {
896
- query.fields = fields;
897
- }
898
-
899
- return query;
900
- }
901
- ```
902
-
903
- 2. Then pass through the applicable fields to your `findRecord` request.
904
-
905
- Given a `post` model with attributes body, title, publishDate and meta, you can retrieve a filtered list of attributes.
906
-
907
- ```js [app/routes/post.js]
908
- export default class extends Route {
909
- model(params) {
910
- return this.store.findRecord('post', params.post_id, { adapterOptions: { fields: { post: 'body,title' } });
911
- }
912
- }
913
- ```
914
-
915
- Moreover, you can filter attributes on related models as well. If a `post` has a `belongsTo` relationship to a user,
916
- just include the relationship key and attributes.
917
-
918
- ```js [app/routes/post.js]
919
- export default class extends Route {
920
- model(params) {
921
- return this.store.findRecord('post', params.post_id, { adapterOptions: { fields: { post: 'body,title', user: 'name,email' } });
922
- }
923
- }
924
- ```
925
-
926
- @since 1.13.0
927
- @public
928
- @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
929
- @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
930
- @param {Object} [options] - if the first param is a string this will be the optional options for the request. See examples for available options.
931
- @return {Promise} promise
932
- */
933
- findRecord<T>(type: TypeFromInstance<T>, id: string | number, options?: FindRecordOptions<T>): Promise<T>;
934
- findRecord(type: string, id: string | number, options?: FindRecordOptions): Promise<unknown>;
935
- findRecord<T>(resource: ResourceIdentifierObject<TypeFromInstance<T>>, options?: FindRecordOptions<T>): Promise<T>;
936
- findRecord(resource: ResourceIdentifierObject, options?: FindRecordOptions): Promise<unknown>;
937
- /**
938
- Get the reference for the specified record.
939
-
940
- Example
941
-
942
- ```javascript
943
- let userRef = store.getReference('user', '1');
944
-
945
- // check if the user is loaded
946
- let isLoaded = userRef.value() !== null;
947
-
948
- // get the record of the reference (null if not yet available)
949
- let user = userRef.value();
950
-
951
- // get the identifier of the reference
952
- if (userRef.remoteType() === 'id') {
953
- let id = userRef.id();
954
- }
955
-
956
- // load user (via store.find)
957
- userRef.load().then(...)
958
-
959
- // or trigger a reload
960
- userRef.reload().then(...)
961
-
962
- // provide data for reference
963
- userRef.push({ id: 1, username: '@user' }).then(function(user) {
964
- userRef.value() === user;
965
- });
966
- ```
967
-
968
- @public
969
- @param {String|object} resource - modelName (string) or Identifier (object)
970
- @param {String|Integer} id
971
- @since 2.5.0
972
- @return {RecordReference}
973
- */
974
- getReference(resource: string | ResourceIdentifierObject, id: string | number): RecordReference;
975
- /**
976
- Get a record by a given type and ID without triggering a fetch.
977
-
978
- This method will synchronously return the record if it is available in the store,
979
- otherwise it will return `null`. A record is available if it has been fetched earlier, or
980
- pushed manually into the store.
981
-
982
- See [findRecord](../methods/findRecord?anchor=findRecord) if you would like to request this record from the backend.
983
-
984
- _Note: This is a synchronous method and does not return a promise._
985
-
986
- **Example 1**
987
-
988
- ```js
989
- let post = store.peekRecord('post', '1');
990
-
991
- post.id; // '1'
992
- ```
993
-
994
- `peekRecord` can be called with a single identifier argument instead of the combination
995
- of `type` (modelName) and `id` as separate arguments. You may recognize this combo as
996
- the typical pairing from [JSON:API](https://jsonapi.org/format/#document-resource-object-identification)
997
-
998
- **Example 2**
999
-
1000
- ```js
1001
- let post = store.peekRecord({ type: 'post', id });
1002
- post.id; // '1'
1003
- ```
1004
-
1005
- If you have previously received an lid from an Identifier for this record, you can lookup the record again using
1006
- just the lid.
1007
-
1008
- **Example 3**
1009
-
1010
- ```js
1011
- let post = store.peekRecord({ lid });
1012
- post.id; // '1'
1013
- ```
1014
-
1015
-
1016
- @since 1.13.0
1017
- @public
1018
- @param {String|object} modelName - either a string representing the modelName 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
1019
- @param {String|Integer} id - optional only if the first param is a ResourceIdentifier, else the string id of the record to be retrieved.
1020
- @return {Model|null} record
1021
- */
1022
- peekRecord<T>(type: TypeFromInstance<T>, id: string | number): T | null;
1023
- peekRecord(type: string, id: string | number): unknown | null;
1024
- peekRecord<T>(identifier: ResourceIdentifierObject<TypeFromInstance<T>>): T | null;
1025
- peekRecord(identifier: ResourceIdentifierObject): unknown | null;
1026
- /**
1027
- This method delegates a query to the adapter. This is the one place where
1028
- adapter-level semantics are exposed to the application.
1029
-
1030
- Each time this method is called a new request is made through the adapter.
1031
-
1032
- Exposing queries this way seems preferable to creating an abstract query
1033
- language for all server-side queries, and then require all adapters to
1034
- implement them.
1035
-
1036
- ---
1037
-
1038
- If you do something like this:
1039
-
1040
- ```javascript
1041
- store.query('person', { page: 1 });
1042
- ```
1043
-
1044
- The request made to the server will look something like this:
1045
-
1046
- ```
1047
- GET "/api/v1/person?page=1"
1048
- ```
1049
-
1050
- ---
1051
-
1052
- If you do something like this:
1053
-
1054
- ```javascript
1055
- store.query('person', { ids: ['1', '2', '3'] });
1056
- ```
1057
-
1058
- The request made to the server will look something like this:
1059
-
1060
- ```
1061
- GET "/api/v1/person?ids%5B%5D=1&ids%5B%5D=2&ids%5B%5D=3"
1062
- decoded: "/api/v1/person?ids[]=1&ids[]=2&ids[]=3"
1063
- ```
1064
-
1065
- This method returns a promise, which is resolved with a
1066
- [`Collection`](/ember-data/release/classes/Collection)
1067
- once the server returns.
1068
-
1069
- @since 1.13.0
1070
- @public
1071
- @param {String} type the name of the resource
1072
- @param {Object} query a query to be used by the adapter
1073
- @param {Object} options optional, may include `adapterOptions` hash which will be passed to adapter.query
1074
- @return {Promise} promise
1075
- */
1076
- query<T>(type: TypeFromInstance<T>, query: LegacyResourceQuery<T>, options?: QueryOptions): Promise<Collection<T>>;
1077
- query(type: string, query: LegacyResourceQuery, options?: QueryOptions): Promise<Collection>;
1078
- /**
1079
- This method makes a request for one record, where the `id` is not known
1080
- beforehand (if the `id` is known, use [`findRecord`](../methods/findRecord?anchor=findRecord)
1081
- instead).
1082
-
1083
- This method can be used when it is certain that the server will return a
1084
- single object for the primary data.
1085
-
1086
- Each time this method is called a new request is made through the adapter.
1087
-
1088
- Let's assume our API provides an endpoint for the currently logged in user
1089
- via:
1090
-
1091
- ```
1092
- // GET /api/current_user
1093
- {
1094
- user: {
1095
- id: 1234,
1096
- username: 'admin'
1097
- }
1098
- }
1099
- ```
1100
-
1101
- Since the specific `id` of the `user` is not known beforehand, we can use
1102
- `queryRecord` to get the user:
1103
-
1104
- ```javascript
1105
- store.queryRecord('user', {}).then(function(user) {
1106
- let username = user.username;
1107
- // do thing
1108
- });
1109
- ```
1110
-
1111
- The request is made through the adapters' `queryRecord`:
1112
-
1113
- ```js [app/adapters/user.js]
1114
- import Adapter from '@ember-data/adapter';
1115
- import $ from 'jquery';
1116
-
1117
- export default class UserAdapter extends Adapter {
1118
- queryRecord(modelName, query) {
1119
- return $.getJSON('/api/current_user');
1120
- }
1121
- }
1122
- ```
1123
-
1124
- Note: the primary use case for `store.queryRecord` is when a single record
1125
- is queried and the `id` is not known beforehand. In all other cases
1126
- `store.query` and using the first item of the array is likely the preferred
1127
- way:
1128
-
1129
- ```
1130
- // GET /users?username=unique
1131
- {
1132
- data: [{
1133
- id: 1234,
1134
- type: 'user',
1135
- attributes: {
1136
- username: "unique"
1137
- }
1138
- }]
1139
- }
1140
- ```
1141
-
1142
- ```javascript
1143
- store.query('user', { username: 'unique' }).then(function(users) {
1144
- return users.firstObject;
1145
- }).then(function(user) {
1146
- let id = user.id;
1147
- });
1148
- ```
1149
-
1150
- This method returns a promise, which resolves with the found record.
1151
-
1152
- If the adapter returns no data for the primary data of the payload, then
1153
- `queryRecord` resolves with `null`:
1154
-
1155
- ```
1156
- // GET /users?username=unique
1157
- {
1158
- data: null
1159
- }
1160
- ```
1161
-
1162
- ```javascript
1163
- store.queryRecord('user', { username: 'unique' }).then(function(user) {
1164
- // user is null
1165
- });
1166
- ```
1167
-
1168
- @since 1.13.0
1169
- @public
1170
- @param {String} type
1171
- @param {Object} query an opaque query to be used by the adapter
1172
- @param {Object} options optional, may include `adapterOptions` hash which will be passed to adapter.queryRecord
1173
- @return {Promise} promise which resolves with the found record or `null`
1174
- */
1175
- queryRecord<T>(type: TypeFromInstance<T>, query: LegacyResourceQuery<T>, options?: QueryOptions): Promise<T | null>;
1176
- queryRecord(type: string, query: LegacyResourceQuery, options?: QueryOptions): Promise<unknown | null>;
1177
- /**
1178
- `findAll` asks the adapter's `findAll` method to find the records for the
1179
- given type, and returns a promise which will resolve with all records of
1180
- this type present in the store, even if the adapter only returns a subset
1181
- of them.
1182
-
1183
- ```js [app/routes/authors.js]
1184
- export default class AuthorsRoute extends Route {
1185
- model(params) {
1186
- return this.store.findAll('author');
1187
- }
1188
- }
1189
- ```
1190
-
1191
- _When_ the returned promise resolves depends on the reload behavior,
1192
- configured via the passed `options` hash and the result of the adapter's
1193
- `shouldReloadAll` method.
1194
-
1195
- ### Reloading
1196
-
1197
- If `{ reload: true }` is passed or `adapter.shouldReloadAll` evaluates to
1198
- `true`, then the returned promise resolves once the adapter returns data,
1199
- regardless if there are already records in the store:
1200
-
1201
- ```js
1202
- store.push({
1203
- data: {
1204
- id: 'first',
1205
- type: 'author'
1206
- }
1207
- });
1208
-
1209
- // adapter#findAll resolves with
1210
- // [
1211
- // {
1212
- // id: 'second',
1213
- // type: 'author'
1214
- // }
1215
- // ]
1216
- store.findAll('author', { reload: true }).then(function(authors) {
1217
- authors.getEach('id'); // ['first', 'second']
1218
- });
1219
- ```
1220
-
1221
- If no reload is indicated via the above mentioned ways, then the promise
1222
- immediately resolves with all the records currently loaded in the store.
1223
-
1224
- ### Background Reloading
1225
-
1226
- Optionally, if `adapter.shouldBackgroundReloadAll` evaluates to `true`,
1227
- then a background reload is started. Once this resolves, the array with
1228
- which the promise resolves, is updated automatically so it contains all the
1229
- records in the store:
1230
-
1231
- ```js [app/adapters/application.js]
1232
- import Adapter from '@ember-data/adapter';
1233
-
1234
- export default class ApplicationAdapter extends Adapter {
1235
- shouldReloadAll(store, snapshotsArray) {
1236
- return false;
1237
- },
1238
-
1239
- shouldBackgroundReloadAll(store, snapshotsArray) {
1240
- return true;
1241
- }
1242
- });
1243
-
1244
- // ...
1245
-
1246
- store.push({
1247
- data: {
1248
- id: 'first',
1249
- type: 'author'
1250
- }
1251
- });
1252
-
1253
- let allAuthors;
1254
- store.findAll('author').then(function(authors) {
1255
- authors.getEach('id'); // ['first']
1256
-
1257
- allAuthors = authors;
1258
- });
1259
-
1260
- // later, once adapter#findAll resolved with
1261
- // [
1262
- // {
1263
- // id: 'second',
1264
- // type: 'author'
1265
- // }
1266
- // ]
1267
-
1268
- allAuthors.getEach('id'); // ['first', 'second']
1269
- ```
1270
-
1271
- If you would like to force or prevent background reloading, you can set a
1272
- boolean value for `backgroundReload` in the options object for
1273
- `findAll`.
1274
-
1275
- ```js [app/routes/post/edit.js]
1276
- export default class PostEditRoute extends Route {
1277
- model() {
1278
- return this.store.findAll('post', { backgroundReload: false });
1279
- }
1280
- }
1281
- ```
1282
-
1283
- If you pass an object on the `adapterOptions` property of the options
1284
- argument it will be passed to you adapter via the `snapshotRecordArray`
1285
-
1286
- ```js [app/routes/posts.js]
1287
- export default class PostsRoute extends Route {
1288
- model(params) {
1289
- return this.store.findAll('post', {
1290
- adapterOptions: { subscribe: false }
1291
- });
1292
- }
1293
- }
1294
- ```
1295
-
1296
- ```js [app/adapters/post.js]
1297
- import MyCustomAdapter from './custom-adapter';
1298
-
1299
- export default class UserAdapter extends MyCustomAdapter {
1300
- findAll(store, type, sinceToken, snapshotRecordArray) {
1301
- if (snapshotRecordArray.adapterOptions.subscribe) {
1302
- // ...
1303
- }
1304
- // ...
1305
- }
1306
- }
1307
- ```
1308
-
1309
- See [peekAll](../methods/peekAll?anchor=peekAll) to get an array of current records in the
1310
- store, without waiting until a reload is finished.
1311
-
1312
- ### Retrieving Related Model Records
1313
-
1314
- If you use an adapter such as Ember's default
1315
- [`JSONAPIAdapter`](/ember-data/release/classes/JSONAPIAdapter)
1316
- that supports the [JSON API specification](http://jsonapi.org/) and if your server
1317
- endpoint supports the use of an
1318
- ['include' query parameter](http://jsonapi.org/format/#fetching-includes),
1319
- you can use `findAll()` to automatically retrieve additional records related to
1320
- those requested by supplying an `include` parameter in the `options` object.
1321
-
1322
- For example, given a `post` model that has a `hasMany` relationship with a `comment`
1323
- model, when we retrieve all of the post records we can have the server also return
1324
- all of the posts' comments in the same request:
1325
-
1326
- ```js [app/routes/posts.js]
1327
- export default class PostsRoute extends Route {
1328
- model() {
1329
- return this.store.findAll('post', { include: ['comments'] });
1330
- }
1331
- }
1332
- ```
1333
- Multiple relationships can be requested using an `include` parameter consisting of a
1334
- list or relationship names, while nested relationships can be specified
1335
- using a dot-separated sequence of relationship names. So to request both the posts'
1336
- comments and the authors of those comments the request would look like this:
1337
-
1338
- ```js [app/routes/posts.js]
1339
- export default class PostsRoute extends Route {
1340
- model() {
1341
- return this.store.findAll('post', { include: ['comments','comments.author'] });
1342
- }
1343
- }
1344
- ```
1345
-
1346
- See [query](../methods/query?anchor=query) to only get a subset of records from the server.
1347
-
1348
- @since 1.13.0
1349
- @public
1350
- @param {String} type the name of the resource
1351
- @param {Object} options
1352
- @return {Promise} promise
1353
- */
1354
- findAll<T>(type: TypeFromInstance<T>, options?: FindAllOptions<T>): Promise<IdentifierArray<T>>;
1355
- findAll(type: string, options?: FindAllOptions): Promise<IdentifierArray>;
1356
- /**
1357
- This method returns a filtered array that contains all of the
1358
- known records for a given type in the store.
1359
-
1360
- Note that because it's just a filter, the result will contain any
1361
- locally created records of the type, however, it will not make a
1362
- request to the backend to retrieve additional records. If you
1363
- would like to request all the records from the backend please use
1364
- [store.findAll](../methods/findAll?anchor=findAll).
1365
-
1366
- Also note that multiple calls to `peekAll` for a given type will always
1367
- return the same `RecordArray`.
1368
-
1369
- Example
1370
-
1371
- ```javascript
1372
- let localPosts = store.peekAll('post');
1373
- ```
1374
-
1375
- @since 1.13.0
1376
- @public
1377
- @param {String} type the name of the resource
1378
- @return {RecordArray}
1379
- */
1380
- peekAll<T>(type: TypeFromInstance<T>): IdentifierArray<T>;
1381
- peekAll(type: string): IdentifierArray;
1382
- /**
1383
- This method unloads all records in the store.
1384
- It schedules unloading to happen during the next run loop.
1385
-
1386
- Optionally you can pass a type which unload all records for a given type.
1387
-
1388
- ```javascript
1389
- store.unloadAll();
1390
- store.unloadAll('post');
1391
- ```
1392
-
1393
- @param {String} type the name of the resource
1394
- @public
1395
- */
1396
- unloadAll<T>(type: TypeFromInstance<T>): void;
1397
- unloadAll(type?: string): void;
1398
- /**
1399
- Push some data for a given type into the store.
1400
-
1401
- This method expects normalized [JSON API](http://jsonapi.org/) document. This means you have to follow [JSON API specification](http://jsonapi.org/format/) with few minor adjustments:
1402
- - record's `type` should always be in singular, dasherized form
1403
- - members (properties) should be camelCased
1404
-
1405
- [Your primary data should be wrapped inside `data` property](http://jsonapi.org/format/#document-top-level):
1406
-
1407
- ```js
1408
- store.push({
1409
- data: {
1410
- // primary data for single record of type `Person`
1411
- id: '1',
1412
- type: 'person',
1413
- attributes: {
1414
- firstName: 'Daniel',
1415
- lastName: 'Kmak'
1416
- }
1417
- }
1418
- });
1419
- ```
1420
-
1421
- [Demo.](http://ember-twiddle.com/fb99f18cd3b4d3e2a4c7)
1422
-
1423
- `data` property can also hold an array (of records):
1424
-
1425
- ```js
1426
- store.push({
1427
- data: [
1428
- // an array of records
1429
- {
1430
- id: '1',
1431
- type: 'person',
1432
- attributes: {
1433
- firstName: 'Daniel',
1434
- lastName: 'Kmak'
1435
- }
1436
- },
1437
- {
1438
- id: '2',
1439
- type: 'person',
1440
- attributes: {
1441
- firstName: 'Tom',
1442
- lastName: 'Dale'
1443
- }
1444
- }
1445
- ]
1446
- });
1447
- ```
1448
-
1449
- [Demo.](http://ember-twiddle.com/69cdbeaa3702159dc355)
1450
-
1451
- There are some typical properties for `JSONAPI` payload:
1452
- * `id` - mandatory, unique record's key
1453
- * `type` - mandatory string which matches `model`'s dasherized name in singular form
1454
- * `attributes` - object which holds data for record attributes - `attr`'s declared in model
1455
- * `relationships` - object which must contain any of the following properties under each relationships' respective key (example path is `relationships.achievements.data`):
1456
- - [`links`](http://jsonapi.org/format/#document-links)
1457
- - [`data`](http://jsonapi.org/format/#document-resource-object-linkage) - place for primary data
1458
- - [`meta`](http://jsonapi.org/format/#document-meta) - object which contains meta-information about relationship
1459
-
1460
- For this model:
1461
-
1462
- ```js [app/models/person.js]
1463
- import Model, { attr, hasMany } from '@ember-data/model';
1464
-
1465
- export default class PersonRoute extends Route {
1466
- @attr('string') firstName;
1467
- @attr('string') lastName;
1468
-
1469
- @hasMany('person') children;
1470
- }
1471
- ```
1472
-
1473
- To represent the children as IDs:
1474
-
1475
- ```js
1476
- {
1477
- data: {
1478
- id: '1',
1479
- type: 'person',
1480
- attributes: {
1481
- firstName: 'Tom',
1482
- lastName: 'Dale'
1483
- },
1484
- relationships: {
1485
- children: {
1486
- data: [
1487
- {
1488
- id: '2',
1489
- type: 'person'
1490
- },
1491
- {
1492
- id: '3',
1493
- type: 'person'
1494
- },
1495
- {
1496
- id: '4',
1497
- type: 'person'
1498
- }
1499
- ]
1500
- }
1501
- }
1502
- }
1503
- }
1504
- ```
1505
-
1506
- [Demo.](http://ember-twiddle.com/343e1735e034091f5bde)
1507
-
1508
- To represent the children relationship as a URL:
1509
-
1510
- ```js
1511
- {
1512
- data: {
1513
- id: '1',
1514
- type: 'person',
1515
- attributes: {
1516
- firstName: 'Tom',
1517
- lastName: 'Dale'
1518
- },
1519
- relationships: {
1520
- children: {
1521
- links: {
1522
- related: '/people/1/children'
1523
- }
1524
- }
1525
- }
1526
- }
1527
- }
1528
- ```
1529
-
1530
- If you're streaming data or implementing an adapter, make sure
1531
- that you have converted the incoming data into this form. The
1532
- store's [normalize](../methods/normalize?anchor=normalize) method is a convenience
1533
- helper for converting a json payload into the form Ember Data
1534
- expects.
1535
-
1536
- ```js
1537
- store.push(store.normalize('person', data));
1538
- ```
1539
-
1540
- This method can be used both to push in brand new
1541
- records, as well as to update existing records.
1542
-
1543
- @public
1544
- @param {Object} data
1545
- @return the record(s) that was created or
1546
- updated.
1547
- */
1548
- push(data: EmptyResourceDocument): null;
1549
- push<T>(data: SingleResourceDocument<TypeFromInstance<T>>): T;
1550
- push(data: SingleResourceDocument): OpaqueRecordInstance;
1551
- push<T>(data: CollectionResourceDocument<TypeFromInstance<T>>): T[];
1552
- push(data: CollectionResourceDocument): OpaqueRecordInstance[];
1553
- /**
1554
- Push some data in the form of a json-api document into the store,
1555
- without creating materialized records.
1556
-
1557
- @private
1558
- @param {Object} jsonApiDoc
1559
- @return {StableRecordIdentifier|Array<StableRecordIdentifier>|null} identifiers for the primary records that had data loaded
1560
- */
1561
- _push(jsonApiDoc: JsonApiDocument, asyncFlush?: boolean): StableExistingRecordIdentifier | StableExistingRecordIdentifier[] | null;
1562
- /**
1563
- * Trigger a save for a Record.
1564
- *
1565
- * Returns a promise resolving with the same record when the save is complete.
1566
- *
1567
- * @public
1568
- * @param {unknown} record
1569
- * @param options
1570
- * @return {Promise<record>}
1571
- */
1572
- saveRecord<T>(record: T, options?: Record<string, unknown>): Promise<T>;
1573
- /**
1574
- * Instantiation hook allowing applications or addons to configure the store
1575
- * to utilize a custom Cache implementation.
1576
- *
1577
- * This hook should not be called directly by consuming applications or libraries.
1578
- * Use `Store.cache` to access the Cache instance.
1579
- *
1580
- * @public
1581
- * @param storeWrapper
1582
- * @return {Cache}
1583
- */
1584
- /**
1585
- * Returns the cache instance associated to this Store, instantiates the Cache
1586
- * if necessary via `Store.createCache`
1587
- *
1588
- * @property cache
1589
- * @type {Cache}
1590
- * @public
1591
- */
1592
- get cache(): ReturnType<this['createCache']>;
1593
- destroy(): void;
1594
- static create(args?: Record<string, unknown>): Store;
1595
- }
1596
- }
1597
- //# sourceMappingURL=store-service.d.ts.map