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