@ember-data/store 5.4.0-alpha.12 → 5.4.0-alpha.121

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