@ember-data/store 4.10.0-alpha.2 → 4.10.0-alpha.21
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/addon/-private.js +1 -0
- package/addon/-private.js.map +1 -0
- package/addon/index-12e1fcb9.js +7660 -0
- package/addon/index-12e1fcb9.js.map +1 -0
- package/addon/index.js +1 -0
- package/addon/index.js.map +1 -0
- package/addon-main.js +90 -0
- package/package.json +44 -15
- package/addon/-private/caches/identifier-cache.ts +0 -686
- package/addon/-private/caches/instance-cache.ts +0 -695
- package/addon/-private/caches/record-data-for.ts +0 -34
- package/addon/-private/index.ts +0 -59
- package/addon/-private/legacy-model-support/record-reference.ts +0 -240
- package/addon/-private/legacy-model-support/schema-definition-service.ts +0 -148
- package/addon/-private/legacy-model-support/shim-model-class.ts +0 -97
- package/addon/-private/managers/record-array-manager.ts +0 -379
- package/addon/-private/managers/record-data-manager.ts +0 -845
- package/addon/-private/managers/record-data-store-wrapper.ts +0 -425
- package/addon/-private/managers/record-notification-manager.ts +0 -111
- package/addon/-private/network/fetch-manager.ts +0 -567
- package/addon/-private/network/finders.js +0 -104
- package/addon/-private/network/request-cache.ts +0 -132
- package/addon/-private/network/snapshot-record-array.ts +0 -209
- package/addon/-private/network/snapshot.ts +0 -563
- package/addon/-private/proxies/promise-proxies.ts +0 -228
- package/addon/-private/proxies/promise-proxy-base.js +0 -7
- package/addon/-private/record-arrays/identifier-array.ts +0 -929
- package/addon/-private/store-service.ts +0 -2896
- package/addon/-private/utils/coerce-id.ts +0 -41
- package/addon/-private/utils/common.js +0 -65
- package/addon/-private/utils/construct-resource.ts +0 -61
- package/addon/-private/utils/identifer-debug-consts.ts +0 -3
- package/addon/-private/utils/is-non-empty-string.ts +0 -3
- package/addon/-private/utils/normalize-model-name.ts +0 -21
- package/addon/-private/utils/promise-record.ts +0 -15
- package/addon/-private/utils/serializer-response.ts +0 -86
- package/addon/-private/utils/uuid-polyfill.ts +0 -73
- package/addon/index.ts +0 -14
- package/index.js +0 -49
|
@@ -1,2896 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
@module @ember-data/store
|
|
3
|
-
*/
|
|
4
|
-
import { getOwner, setOwner } from '@ember/application';
|
|
5
|
-
import { assert, deprecate } from '@ember/debug';
|
|
6
|
-
import { _backburner as emberBackburner } from '@ember/runloop';
|
|
7
|
-
import Service from '@ember/service';
|
|
8
|
-
import { registerWaiter, unregisterWaiter } from '@ember/test';
|
|
9
|
-
import { DEBUG } from '@glimmer/env';
|
|
10
|
-
|
|
11
|
-
import { importSync } from '@embroider/macros';
|
|
12
|
-
import { reject, resolve } from 'rsvp';
|
|
13
|
-
|
|
14
|
-
import type DSModelClass from '@ember-data/model';
|
|
15
|
-
import { HAS_MODEL_PACKAGE, HAS_RECORD_DATA_PACKAGE } from '@ember-data/private-build-infra';
|
|
16
|
-
import { LOG_PAYLOADS } from '@ember-data/private-build-infra/debugging';
|
|
17
|
-
import {
|
|
18
|
-
DEPRECATE_HAS_RECORD,
|
|
19
|
-
DEPRECATE_JSON_API_FALLBACK,
|
|
20
|
-
DEPRECATE_PROMISE_PROXIES,
|
|
21
|
-
DEPRECATE_STORE_FIND,
|
|
22
|
-
DEPRECATE_V1CACHE_STORE_APIS,
|
|
23
|
-
} from '@ember-data/private-build-infra/deprecations';
|
|
24
|
-
import type { RecordData as RecordDataClass } from '@ember-data/record-data/-private';
|
|
25
|
-
import type { DSModel } from '@ember-data/types/q/ds-model';
|
|
26
|
-
import type {
|
|
27
|
-
CollectionResourceDocument,
|
|
28
|
-
EmptyResourceDocument,
|
|
29
|
-
JsonApiDocument,
|
|
30
|
-
ResourceIdentifierObject,
|
|
31
|
-
SingleResourceDocument,
|
|
32
|
-
} from '@ember-data/types/q/ember-data-json-api';
|
|
33
|
-
import type { StableExistingRecordIdentifier, StableRecordIdentifier } from '@ember-data/types/q/identifier';
|
|
34
|
-
import type { MinimumAdapterInterface } from '@ember-data/types/q/minimum-adapter-interface';
|
|
35
|
-
import type { MinimumSerializerInterface } from '@ember-data/types/q/minimum-serializer-interface';
|
|
36
|
-
import type { RecordData, RecordDataV1 } from '@ember-data/types/q/record-data';
|
|
37
|
-
import { JsonApiValidationError } from '@ember-data/types/q/record-data-json-api';
|
|
38
|
-
import type { RecordDataStoreWrapper } from '@ember-data/types/q/record-data-store-wrapper';
|
|
39
|
-
import type { RecordInstance } from '@ember-data/types/q/record-instance';
|
|
40
|
-
import type { SchemaDefinitionService } from '@ember-data/types/q/schema-definition-service';
|
|
41
|
-
import type { FindOptions } from '@ember-data/types/q/store';
|
|
42
|
-
import type { Dict } from '@ember-data/types/q/utils';
|
|
43
|
-
|
|
44
|
-
import { IdentifierCache } from './caches/identifier-cache';
|
|
45
|
-
import {
|
|
46
|
-
InstanceCache,
|
|
47
|
-
peekRecordIdentifier,
|
|
48
|
-
recordDataIsFullyDeleted,
|
|
49
|
-
recordIdentifierFor,
|
|
50
|
-
setRecordIdentifier,
|
|
51
|
-
storeFor,
|
|
52
|
-
StoreMap,
|
|
53
|
-
} from './caches/instance-cache';
|
|
54
|
-
import recordDataFor, { setRecordDataFor } from './caches/record-data-for';
|
|
55
|
-
import RecordReference from './legacy-model-support/record-reference';
|
|
56
|
-
import { DSModelSchemaDefinitionService, getModelFactory } from './legacy-model-support/schema-definition-service';
|
|
57
|
-
import type ShimModelClass from './legacy-model-support/shim-model-class';
|
|
58
|
-
import { getShimClass } from './legacy-model-support/shim-model-class';
|
|
59
|
-
import RecordArrayManager from './managers/record-array-manager';
|
|
60
|
-
import type { NonSingletonRecordDataManager } from './managers/record-data-manager';
|
|
61
|
-
import NotificationManager from './managers/record-notification-manager';
|
|
62
|
-
import FetchManager, { SaveOp } from './network/fetch-manager';
|
|
63
|
-
import { _findAll, _query, _queryRecord } from './network/finders';
|
|
64
|
-
import type RequestCache from './network/request-cache';
|
|
65
|
-
import type Snapshot from './network/snapshot';
|
|
66
|
-
import SnapshotRecordArray from './network/snapshot-record-array';
|
|
67
|
-
import { PromiseArray, promiseArray, PromiseObject, promiseObject } from './proxies/promise-proxies';
|
|
68
|
-
import IdentifierArray, { Collection } from './record-arrays/identifier-array';
|
|
69
|
-
import coerceId, { ensureStringId } from './utils/coerce-id';
|
|
70
|
-
import constructResource from './utils/construct-resource';
|
|
71
|
-
import normalizeModelName from './utils/normalize-model-name';
|
|
72
|
-
import promiseRecord from './utils/promise-record';
|
|
73
|
-
|
|
74
|
-
export { storeFor };
|
|
75
|
-
|
|
76
|
-
// hello world
|
|
77
|
-
type RecordDataConstruct = typeof RecordDataClass;
|
|
78
|
-
let _RecordData: RecordDataConstruct | undefined;
|
|
79
|
-
|
|
80
|
-
type AsyncTrackingToken = Readonly<{ label: string; trace: Error | string }>;
|
|
81
|
-
|
|
82
|
-
function freeze<T>(obj: T): T {
|
|
83
|
-
if (typeof Object.freeze === 'function') {
|
|
84
|
-
return Object.freeze(obj);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return obj;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export interface CreateRecordProperties {
|
|
91
|
-
id?: string | null;
|
|
92
|
-
[key: string]: unknown;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
The store contains all of the data for records loaded from the server.
|
|
97
|
-
It is also responsible for creating instances of `Model` that wrap
|
|
98
|
-
the individual data for a record, so that they can be bound to in your
|
|
99
|
-
Handlebars templates.
|
|
100
|
-
|
|
101
|
-
Define your application's store like this:
|
|
102
|
-
|
|
103
|
-
```app/services/store.js
|
|
104
|
-
import Store from '@ember-data/store';
|
|
105
|
-
|
|
106
|
-
export default class MyStore extends Store {}
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
Most Ember.js applications will only have a single `Store` that is
|
|
110
|
-
automatically created by their `Ember.Application`.
|
|
111
|
-
|
|
112
|
-
You can retrieve models from the store in several ways. To retrieve a record
|
|
113
|
-
for a specific id, use `Store`'s `findRecord()` method:
|
|
114
|
-
|
|
115
|
-
```javascript
|
|
116
|
-
store.findRecord('person', 123).then(function (person) {
|
|
117
|
-
});
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
By default, the store will talk to your backend using a standard
|
|
121
|
-
REST mechanism. You can customize how the store talks to your
|
|
122
|
-
backend by specifying a custom adapter:
|
|
123
|
-
|
|
124
|
-
```app/adapters/application.js
|
|
125
|
-
import Adapter from '@ember-data/adapter';
|
|
126
|
-
|
|
127
|
-
export default class ApplicationAdapter extends Adapter {
|
|
128
|
-
}
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
You can learn more about writing a custom adapter by reading the `Adapter`
|
|
132
|
-
documentation.
|
|
133
|
-
|
|
134
|
-
### Store createRecord() vs. push() vs. pushPayload()
|
|
135
|
-
|
|
136
|
-
The store provides multiple ways to create new record objects. They have
|
|
137
|
-
some subtle differences in their use which are detailed below:
|
|
138
|
-
|
|
139
|
-
[createRecord](../methods/createRecord?anchor=createRecord) is used for creating new
|
|
140
|
-
records on the client side. This will return a new record in the
|
|
141
|
-
`created.uncommitted` state. In order to persist this record to the
|
|
142
|
-
backend, you will need to call `record.save()`.
|
|
143
|
-
|
|
144
|
-
[push](../methods/push?anchor=push) is used to notify Ember Data's store of new or
|
|
145
|
-
updated records that exist in the backend. This will return a record
|
|
146
|
-
in the `loaded.saved` state. The primary use-case for `store#push` is
|
|
147
|
-
to notify Ember Data about record updates (full or partial) that happen
|
|
148
|
-
outside of the normal adapter methods (for example
|
|
149
|
-
[SSE](http://dev.w3.org/html5/eventsource/) or [Web
|
|
150
|
-
Sockets](http://www.w3.org/TR/2009/WD-websockets-20091222/)).
|
|
151
|
-
|
|
152
|
-
[pushPayload](../methods/pushPayload?anchor=pushPayload) is a convenience wrapper for
|
|
153
|
-
`store#push` that will deserialize payloads if the
|
|
154
|
-
Serializer implements a `pushPayload` method.
|
|
155
|
-
|
|
156
|
-
Note: When creating a new record using any of the above methods
|
|
157
|
-
Ember Data will update `RecordArray`s such as those returned by
|
|
158
|
-
`store#peekAll()` or `store#findAll()`. This means any
|
|
159
|
-
data bindings or computed properties that depend on the RecordArray
|
|
160
|
-
will automatically be synced to include the new or updated record
|
|
161
|
-
values.
|
|
162
|
-
|
|
163
|
-
@main @ember-data/store
|
|
164
|
-
@class Store
|
|
165
|
-
@public
|
|
166
|
-
@extends Ember.Service
|
|
167
|
-
*/
|
|
168
|
-
|
|
169
|
-
class Store extends Service {
|
|
170
|
-
__private_singleton_recordData!: RecordData;
|
|
171
|
-
|
|
172
|
-
declare recordArrayManager: RecordArrayManager;
|
|
173
|
-
|
|
174
|
-
declare _notificationManager: NotificationManager;
|
|
175
|
-
declare identifierCache: IdentifierCache;
|
|
176
|
-
declare _adapterCache: Dict<MinimumAdapterInterface & { store: Store }>;
|
|
177
|
-
declare _serializerCache: Dict<MinimumSerializerInterface & { store: Store }>;
|
|
178
|
-
declare _modelFactoryCache: Dict<unknown>;
|
|
179
|
-
declare _fetchManager: FetchManager;
|
|
180
|
-
declare _schemaDefinitionService: SchemaDefinitionService;
|
|
181
|
-
declare _instanceCache: InstanceCache;
|
|
182
|
-
|
|
183
|
-
// DEBUG-only properties
|
|
184
|
-
declare _trackedAsyncRequests: AsyncTrackingToken[];
|
|
185
|
-
declare generateStackTracesForTrackedRequests: boolean;
|
|
186
|
-
declare _trackAsyncRequestStart: (str: string) => void;
|
|
187
|
-
declare _trackAsyncRequestEnd: (token: AsyncTrackingToken) => void;
|
|
188
|
-
declare __asyncWaiter: () => boolean;
|
|
189
|
-
declare DISABLE_WAITER?: boolean;
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
@method init
|
|
193
|
-
@private
|
|
194
|
-
*/
|
|
195
|
-
constructor() {
|
|
196
|
-
super(...arguments);
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Provides access to the IdentifierCache instance
|
|
200
|
-
* for this store.
|
|
201
|
-
*
|
|
202
|
-
* The IdentifierCache can be used to generate or
|
|
203
|
-
* retrieve a stable unique identifier for any resource.
|
|
204
|
-
*
|
|
205
|
-
* @property {IdentifierCache} identifierCache
|
|
206
|
-
* @public
|
|
207
|
-
*/
|
|
208
|
-
this.identifierCache = new IdentifierCache();
|
|
209
|
-
|
|
210
|
-
// private but maybe useful to be here, somewhat intimate
|
|
211
|
-
this.recordArrayManager = new RecordArrayManager({ store: this });
|
|
212
|
-
|
|
213
|
-
// private, TODO consider taking public as the instance is public to instantiateRecord anyway
|
|
214
|
-
this._notificationManager = new NotificationManager(this);
|
|
215
|
-
|
|
216
|
-
// private
|
|
217
|
-
this._fetchManager = new FetchManager(this);
|
|
218
|
-
this._instanceCache = new InstanceCache(this);
|
|
219
|
-
this._adapterCache = Object.create(null);
|
|
220
|
-
this._serializerCache = Object.create(null);
|
|
221
|
-
this._modelFactoryCache = Object.create(null);
|
|
222
|
-
|
|
223
|
-
if (DEBUG) {
|
|
224
|
-
if (this.generateStackTracesForTrackedRequests === undefined) {
|
|
225
|
-
this.generateStackTracesForTrackedRequests = false;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
this._trackedAsyncRequests = [];
|
|
229
|
-
this._trackAsyncRequestStart = (label) => {
|
|
230
|
-
let trace: string | Error =
|
|
231
|
-
'set `store.generateStackTracesForTrackedRequests = true;` to get a detailed trace for where this request originated';
|
|
232
|
-
|
|
233
|
-
if (this.generateStackTracesForTrackedRequests) {
|
|
234
|
-
try {
|
|
235
|
-
throw new Error(`EmberData TrackedRequest: ${label}`);
|
|
236
|
-
} catch (e) {
|
|
237
|
-
trace = e as Error;
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
let token = freeze({
|
|
242
|
-
label,
|
|
243
|
-
trace,
|
|
244
|
-
});
|
|
245
|
-
|
|
246
|
-
this._trackedAsyncRequests.push(token);
|
|
247
|
-
return token;
|
|
248
|
-
};
|
|
249
|
-
this._trackAsyncRequestEnd = (token) => {
|
|
250
|
-
let index = this._trackedAsyncRequests.indexOf(token);
|
|
251
|
-
|
|
252
|
-
if (index === -1) {
|
|
253
|
-
throw new Error(
|
|
254
|
-
`Attempted to end tracking for the following request but it was not being tracked:\n${token}`
|
|
255
|
-
);
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
this._trackedAsyncRequests.splice(index, 1);
|
|
259
|
-
};
|
|
260
|
-
|
|
261
|
-
this.__asyncWaiter = () => {
|
|
262
|
-
let tracked = this._trackedAsyncRequests;
|
|
263
|
-
return this.DISABLE_WAITER || tracked.length === 0;
|
|
264
|
-
};
|
|
265
|
-
|
|
266
|
-
registerWaiter(this.__asyncWaiter);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
declare _cbs: { coalesce?: () => void; sync?: () => void; notify?: () => void } | null;
|
|
271
|
-
_run(cb: () => void) {
|
|
272
|
-
assert(`EmberData should never encounter a nested run`, !this._cbs);
|
|
273
|
-
const _cbs: { coalesce?: () => void; sync?: () => void; notify?: () => void } = (this._cbs = {});
|
|
274
|
-
cb();
|
|
275
|
-
if (_cbs.coalesce) {
|
|
276
|
-
_cbs.coalesce();
|
|
277
|
-
}
|
|
278
|
-
if (_cbs.sync) {
|
|
279
|
-
_cbs.sync();
|
|
280
|
-
}
|
|
281
|
-
if (_cbs.notify) {
|
|
282
|
-
_cbs.notify();
|
|
283
|
-
}
|
|
284
|
-
this._cbs = null;
|
|
285
|
-
}
|
|
286
|
-
_join(cb: () => void): void {
|
|
287
|
-
if (this._cbs) {
|
|
288
|
-
cb();
|
|
289
|
-
} else {
|
|
290
|
-
this._run(cb);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
_schedule(name: 'coalesce' | 'sync' | 'notify', cb: () => void): void {
|
|
295
|
-
assert(`EmberData expects to schedule only when there is an active run`, !!this._cbs);
|
|
296
|
-
assert(`EmberData expects only one flush per queue name, cannot schedule ${name}`, !this._cbs[name]);
|
|
297
|
-
|
|
298
|
-
this._cbs[name] = cb;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
/**
|
|
302
|
-
* Retrieve the RequestStateService instance
|
|
303
|
-
* associated with this Store.
|
|
304
|
-
*
|
|
305
|
-
* This can be used to query the status of requests
|
|
306
|
-
* that have been initiated for a given identifier.
|
|
307
|
-
*
|
|
308
|
-
* @method getRequestStateService
|
|
309
|
-
* @returns {RequestStateService}
|
|
310
|
-
* @public
|
|
311
|
-
*/
|
|
312
|
-
getRequestStateService(): RequestCache {
|
|
313
|
-
return this._fetchManager.requestCache;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
/**
|
|
317
|
-
* A hook which an app or addon may implement. Called when
|
|
318
|
-
* the Store is attempting to create a Record Instance for
|
|
319
|
-
* a resource.
|
|
320
|
-
*
|
|
321
|
-
* This hook can be used to select or instantiate any desired
|
|
322
|
-
* mechanism of presentating cache data to the ui for access
|
|
323
|
-
* mutation, and interaction.
|
|
324
|
-
*
|
|
325
|
-
* @method instantiateRecord (hook)
|
|
326
|
-
* @param identifier
|
|
327
|
-
* @param createRecordArgs
|
|
328
|
-
* @param recordDataFor
|
|
329
|
-
* @param notificationManager
|
|
330
|
-
* @returns A record instance
|
|
331
|
-
* @public
|
|
332
|
-
*/
|
|
333
|
-
instantiateRecord(
|
|
334
|
-
identifier: StableRecordIdentifier,
|
|
335
|
-
createRecordArgs: { [key: string]: unknown },
|
|
336
|
-
recordDataFor: (identifier: StableRecordIdentifier) => RecordData,
|
|
337
|
-
notificationManager: NotificationManager
|
|
338
|
-
): DSModel | RecordInstance {
|
|
339
|
-
if (HAS_MODEL_PACKAGE) {
|
|
340
|
-
let modelName = identifier.type;
|
|
341
|
-
|
|
342
|
-
let recordData = this._instanceCache.getRecordData(identifier);
|
|
343
|
-
// TODO deprecate allowing unknown args setting
|
|
344
|
-
let createOptions: any = {
|
|
345
|
-
_createProps: createRecordArgs,
|
|
346
|
-
// TODO @deprecate consider deprecating accessing record properties during init which the below is necessary for
|
|
347
|
-
_secretInit: {
|
|
348
|
-
identifier,
|
|
349
|
-
recordData,
|
|
350
|
-
store: this,
|
|
351
|
-
cb: secretInit,
|
|
352
|
-
},
|
|
353
|
-
};
|
|
354
|
-
|
|
355
|
-
// ensure that `getOwner(this)` works inside a model instance
|
|
356
|
-
setOwner(createOptions, getOwner(this));
|
|
357
|
-
return getModelFactory(this, this._modelFactoryCache, modelName).class.create(createOptions);
|
|
358
|
-
}
|
|
359
|
-
assert(`You must implement the store's instantiateRecord hook for your custom model class.`);
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
/**
|
|
363
|
-
* A hook which an app or addon may implement. Called when
|
|
364
|
-
* the Store is destroying a Record Instance. This hook should
|
|
365
|
-
* be used to teardown any custom record instances instantiated
|
|
366
|
-
* with `instantiateRecord`.
|
|
367
|
-
*
|
|
368
|
-
* @method teardownRecord (hook)
|
|
369
|
-
* @public
|
|
370
|
-
* @param record
|
|
371
|
-
*/
|
|
372
|
-
teardownRecord(record: DSModel | RecordInstance): void {
|
|
373
|
-
if (HAS_MODEL_PACKAGE) {
|
|
374
|
-
assert(
|
|
375
|
-
`expected to receive an instance of DSModel. If using a custom model make sure you implement teardownRecord`,
|
|
376
|
-
'destroy' in record
|
|
377
|
-
);
|
|
378
|
-
(record as DSModel).destroy();
|
|
379
|
-
} else {
|
|
380
|
-
assert(`You must implement the store's teardownRecord hook for your custom models`);
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
/**
|
|
385
|
-
* Provides access to the SchemaDefinitionService instance
|
|
386
|
-
* for this Store instance.
|
|
387
|
-
*
|
|
388
|
-
* The SchemaDefinitionService can be used to query for
|
|
389
|
-
* information about the schema of a resource.
|
|
390
|
-
*
|
|
391
|
-
* @method getSchemaDefinitionService
|
|
392
|
-
* @public
|
|
393
|
-
*/
|
|
394
|
-
getSchemaDefinitionService(): SchemaDefinitionService {
|
|
395
|
-
if (HAS_MODEL_PACKAGE && !this._schemaDefinitionService) {
|
|
396
|
-
// it is potentially a mistake for the RFC to have not enabled chaining these services, though highlander rule is nice.
|
|
397
|
-
// what ember-m3 did via private API to allow both worlds to interop would be much much harder using this.
|
|
398
|
-
this._schemaDefinitionService = new DSModelSchemaDefinitionService(this);
|
|
399
|
-
}
|
|
400
|
-
assert(
|
|
401
|
-
`You must registerSchemaDefinitionService with the store to use custom model classes`,
|
|
402
|
-
this._schemaDefinitionService
|
|
403
|
-
);
|
|
404
|
-
return this._schemaDefinitionService;
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
/**
|
|
408
|
-
* Allows an app to register a custom SchemaDefinitionService
|
|
409
|
-
* for use when information about a resource's schema needs
|
|
410
|
-
* to be queried.
|
|
411
|
-
*
|
|
412
|
-
* This method can only be called more than once, but only one schema
|
|
413
|
-
* definition service may exist. Therefore if you wish to chain services
|
|
414
|
-
* you must lookup the existing service and close over it with the new
|
|
415
|
-
* service by calling `getSchemaDefinitionService` prior to registration.
|
|
416
|
-
*
|
|
417
|
-
* For Example:
|
|
418
|
-
*
|
|
419
|
-
* ```ts
|
|
420
|
-
* import Store from '@ember-data/store';
|
|
421
|
-
*
|
|
422
|
-
* class SchemaDelegator {
|
|
423
|
-
* constructor(schema) {
|
|
424
|
-
* this._schema = schema;
|
|
425
|
-
* }
|
|
426
|
-
*
|
|
427
|
-
* doesTypeExist(type: string): boolean {
|
|
428
|
-
* if (AbstractSchemas.has(type)) {
|
|
429
|
-
* return true;
|
|
430
|
-
* }
|
|
431
|
-
* return this._schema.doesTypeExist(type);
|
|
432
|
-
* }
|
|
433
|
-
*
|
|
434
|
-
* attributesDefinitionFor(identifier: RecordIdentifier | { type: string }): AttributesSchema {
|
|
435
|
-
* return this._schema.attributesDefinitionFor(identifier);
|
|
436
|
-
* }
|
|
437
|
-
*
|
|
438
|
-
* relationshipsDefinitionFor(identifier: RecordIdentifier | { type: string }): RelationshipsSchema {
|
|
439
|
-
* const schema = AbstractSchemas.get(identifier.type);
|
|
440
|
-
* return schema || this._schema.relationshipsDefinitionFor(identifier);
|
|
441
|
-
* }
|
|
442
|
-
* }
|
|
443
|
-
*
|
|
444
|
-
* export default class extends Store {
|
|
445
|
-
* constructor(...args) {
|
|
446
|
-
* super(...args);
|
|
447
|
-
*
|
|
448
|
-
* const schema = this.getSchemaDefinitionService();
|
|
449
|
-
* this.registerSchemaDefinitionService(new SchemaDelegator(schema));
|
|
450
|
-
* }
|
|
451
|
-
* }
|
|
452
|
-
* ```
|
|
453
|
-
*
|
|
454
|
-
* @method registerSchemaDefinitionService
|
|
455
|
-
* @param {SchemaDefinitionService} schema
|
|
456
|
-
* @public
|
|
457
|
-
*/
|
|
458
|
-
registerSchemaDefinitionService(schema: SchemaDefinitionService) {
|
|
459
|
-
this._schemaDefinitionService = schema;
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
/**
|
|
463
|
-
Returns the schema for a particular `modelName`.
|
|
464
|
-
|
|
465
|
-
When used with Model from @ember-data/model the return is the model class,
|
|
466
|
-
but this is not guaranteed.
|
|
467
|
-
|
|
468
|
-
If looking to query attribute or relationship information it is
|
|
469
|
-
recommended to use `getSchemaDefinitionService` instead. This method
|
|
470
|
-
should be considered legacy and exists primarily to continue to support
|
|
471
|
-
Adapter/Serializer APIs which expect it's return value in their method
|
|
472
|
-
signatures.
|
|
473
|
-
|
|
474
|
-
The class of a model might be useful if you want to get a list of all the
|
|
475
|
-
relationship names of the model, see
|
|
476
|
-
[`relationshipNames`](/ember-data/release/classes/Model?anchor=relationshipNames)
|
|
477
|
-
for example.
|
|
478
|
-
|
|
479
|
-
@method modelFor
|
|
480
|
-
@public
|
|
481
|
-
@param {String} modelName
|
|
482
|
-
@return {subclass of Model | ShimModelClass}
|
|
483
|
-
*/
|
|
484
|
-
// TODO @deprecate in favor of schema APIs, requires adapter/serializer overhaul or replacement
|
|
485
|
-
modelFor(modelName: string): ShimModelClass | DSModelClass {
|
|
486
|
-
if (DEBUG) {
|
|
487
|
-
assertDestroyedStoreOnly(this, 'modelFor');
|
|
488
|
-
}
|
|
489
|
-
assert(`You need to pass a model name to the store's modelFor method`, modelName);
|
|
490
|
-
assert(
|
|
491
|
-
`Passing classes to store methods has been removed. Please pass a dasherized string instead of ${modelName}`,
|
|
492
|
-
typeof modelName === 'string'
|
|
493
|
-
);
|
|
494
|
-
if (HAS_MODEL_PACKAGE) {
|
|
495
|
-
let normalizedModelName = normalizeModelName(modelName);
|
|
496
|
-
let maybeFactory = getModelFactory(this, this._modelFactoryCache, normalizedModelName);
|
|
497
|
-
|
|
498
|
-
// for factorFor factory/class split
|
|
499
|
-
let klass = maybeFactory && maybeFactory.class ? maybeFactory.class : maybeFactory;
|
|
500
|
-
if (!klass || !klass.isModel) {
|
|
501
|
-
assert(
|
|
502
|
-
`No model was found for '${modelName}' and no schema handles the type`,
|
|
503
|
-
this.getSchemaDefinitionService().doesTypeExist(modelName)
|
|
504
|
-
);
|
|
505
|
-
|
|
506
|
-
return getShimClass(this, modelName);
|
|
507
|
-
} else {
|
|
508
|
-
// TODO @deprecate ever returning the klass, always return the shim
|
|
509
|
-
return klass;
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
assert(
|
|
514
|
-
`No model was found for '${modelName}' and no schema handles the type`,
|
|
515
|
-
this.getSchemaDefinitionService().doesTypeExist(modelName)
|
|
516
|
-
);
|
|
517
|
-
return getShimClass(this, modelName);
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
/**
|
|
521
|
-
Create a new record in the current store. The properties passed
|
|
522
|
-
to this method are set on the newly created record.
|
|
523
|
-
|
|
524
|
-
To create a new instance of a `Post`:
|
|
525
|
-
|
|
526
|
-
```js
|
|
527
|
-
store.createRecord('post', {
|
|
528
|
-
title: 'Ember is awesome!'
|
|
529
|
-
});
|
|
530
|
-
```
|
|
531
|
-
|
|
532
|
-
To create a new instance of a `Post` that has a relationship with a `User` record:
|
|
533
|
-
|
|
534
|
-
```js
|
|
535
|
-
let user = this.store.peekRecord('user', 1);
|
|
536
|
-
store.createRecord('post', {
|
|
537
|
-
title: 'Ember is awesome!',
|
|
538
|
-
user: user
|
|
539
|
-
});
|
|
540
|
-
```
|
|
541
|
-
|
|
542
|
-
@method createRecord
|
|
543
|
-
@public
|
|
544
|
-
@param {String} modelName
|
|
545
|
-
@param {Object} inputProperties a hash of properties to set on the
|
|
546
|
-
newly created record.
|
|
547
|
-
@return {Model} record
|
|
548
|
-
*/
|
|
549
|
-
createRecord(modelName: string, inputProperties: CreateRecordProperties): RecordInstance {
|
|
550
|
-
if (DEBUG) {
|
|
551
|
-
assertDestroyingStore(this, 'createRecord');
|
|
552
|
-
}
|
|
553
|
-
assert(`You need to pass a model name to the store's createRecord method`, modelName);
|
|
554
|
-
assert(
|
|
555
|
-
`Passing classes to store methods has been removed. Please pass a dasherized string instead of ${modelName}`,
|
|
556
|
-
typeof modelName === 'string'
|
|
557
|
-
);
|
|
558
|
-
|
|
559
|
-
// This is wrapped in a `run.join` so that in test environments users do not need to manually wrap
|
|
560
|
-
// calls to `createRecord`. The run loop usage here is because we batch the joining and updating
|
|
561
|
-
// of record-arrays via ember's run loop, not our own.
|
|
562
|
-
//
|
|
563
|
-
// to remove this, we would need to move to a new `async` API.
|
|
564
|
-
let record!: RecordInstance;
|
|
565
|
-
emberBackburner.join(() => {
|
|
566
|
-
this._join(() => {
|
|
567
|
-
let normalizedModelName = normalizeModelName(modelName);
|
|
568
|
-
let properties = { ...inputProperties };
|
|
569
|
-
|
|
570
|
-
// If the passed properties do not include a primary key,
|
|
571
|
-
// give the adapter an opportunity to generate one. Typically,
|
|
572
|
-
// client-side ID generators will use something like uuid.js
|
|
573
|
-
// to avoid conflicts.
|
|
574
|
-
|
|
575
|
-
if (properties.id === null || properties.id === undefined) {
|
|
576
|
-
let adapter = this.adapterFor(modelName);
|
|
577
|
-
|
|
578
|
-
if (adapter && adapter.generateIdForRecord) {
|
|
579
|
-
properties.id = adapter.generateIdForRecord(this, modelName, properties);
|
|
580
|
-
} else {
|
|
581
|
-
properties.id = null;
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
// Coerce ID to a string
|
|
586
|
-
properties.id = coerceId(properties.id);
|
|
587
|
-
const resource = { type: normalizedModelName, id: properties.id };
|
|
588
|
-
|
|
589
|
-
if (resource.id) {
|
|
590
|
-
const identifier = this.identifierCache.peekRecordIdentifier(resource as ResourceIdentifierObject);
|
|
591
|
-
|
|
592
|
-
assert(
|
|
593
|
-
`The id ${properties.id} has already been used with another '${normalizedModelName}' record.`,
|
|
594
|
-
!identifier
|
|
595
|
-
);
|
|
596
|
-
}
|
|
597
|
-
|
|
598
|
-
const identifier = this.identifierCache.createIdentifierForNewRecord(resource);
|
|
599
|
-
const recordData = this._instanceCache.getRecordData(identifier);
|
|
600
|
-
|
|
601
|
-
const createOptions = normalizeProperties(
|
|
602
|
-
this,
|
|
603
|
-
identifier,
|
|
604
|
-
properties,
|
|
605
|
-
(recordData as NonSingletonRecordDataManager).managedVersion === '1'
|
|
606
|
-
);
|
|
607
|
-
const resultProps = recordData.clientDidCreate(identifier, createOptions);
|
|
608
|
-
this.recordArrayManager.identifierAdded(identifier);
|
|
609
|
-
|
|
610
|
-
record = this._instanceCache.getRecord(identifier, resultProps);
|
|
611
|
-
});
|
|
612
|
-
});
|
|
613
|
-
return record;
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
/**
|
|
617
|
-
For symmetry, a record can be deleted via the store.
|
|
618
|
-
|
|
619
|
-
Example
|
|
620
|
-
|
|
621
|
-
```javascript
|
|
622
|
-
let post = store.createRecord('post', {
|
|
623
|
-
title: 'Ember is awesome!'
|
|
624
|
-
});
|
|
625
|
-
|
|
626
|
-
store.deleteRecord(post);
|
|
627
|
-
```
|
|
628
|
-
|
|
629
|
-
@method deleteRecord
|
|
630
|
-
@public
|
|
631
|
-
@param {Model} record
|
|
632
|
-
*/
|
|
633
|
-
deleteRecord(record: RecordInstance): void {
|
|
634
|
-
if (DEBUG) {
|
|
635
|
-
assertDestroyingStore(this, 'deleteRecord');
|
|
636
|
-
}
|
|
637
|
-
|
|
638
|
-
const identifier = peekRecordIdentifier(record);
|
|
639
|
-
const recordData = identifier && this._instanceCache.peek({ identifier, bucket: 'recordData' });
|
|
640
|
-
assert(`expected a recordData instance to exist for the record`, recordData);
|
|
641
|
-
this._join(() => {
|
|
642
|
-
recordData.setIsDeleted(identifier, true);
|
|
643
|
-
|
|
644
|
-
if (recordData.isNew(identifier)) {
|
|
645
|
-
emberBackburner.join(() => {
|
|
646
|
-
this._instanceCache.unloadRecord(identifier);
|
|
647
|
-
});
|
|
648
|
-
}
|
|
649
|
-
});
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
/**
|
|
653
|
-
For symmetry, a record can be unloaded via the store.
|
|
654
|
-
This will cause the record to be destroyed and freed up for garbage collection.
|
|
655
|
-
|
|
656
|
-
Example
|
|
657
|
-
|
|
658
|
-
```javascript
|
|
659
|
-
store.findRecord('post', 1).then(function(post) {
|
|
660
|
-
store.unloadRecord(post);
|
|
661
|
-
});
|
|
662
|
-
```
|
|
663
|
-
|
|
664
|
-
@method unloadRecord
|
|
665
|
-
@public
|
|
666
|
-
@param {Model} record
|
|
667
|
-
*/
|
|
668
|
-
unloadRecord(record: RecordInstance): void {
|
|
669
|
-
if (DEBUG) {
|
|
670
|
-
assertDestroyingStore(this, 'unloadRecord');
|
|
671
|
-
}
|
|
672
|
-
const identifier = peekRecordIdentifier(record);
|
|
673
|
-
if (identifier) {
|
|
674
|
-
this._instanceCache.unloadRecord(identifier);
|
|
675
|
-
}
|
|
676
|
-
}
|
|
677
|
-
|
|
678
|
-
/**
|
|
679
|
-
@method find
|
|
680
|
-
@param {String} modelName
|
|
681
|
-
@param {String|Integer} id
|
|
682
|
-
@param {Object} options
|
|
683
|
-
@return {Promise} promise
|
|
684
|
-
@deprecated
|
|
685
|
-
@private
|
|
686
|
-
*/
|
|
687
|
-
find(modelName: string, id: string | number, options?): PromiseObject<RecordInstance> {
|
|
688
|
-
if (DEBUG) {
|
|
689
|
-
assertDestroyingStore(this, 'find');
|
|
690
|
-
}
|
|
691
|
-
// The default `model` hook in Route calls `find(modelName, id)`,
|
|
692
|
-
// that's why we have to keep this method around even though `findRecord` is
|
|
693
|
-
// the public way to get a record by modelName and id.
|
|
694
|
-
assert(
|
|
695
|
-
`Using store.find(type) has been removed. Use store.findAll(modelName) to retrieve all records for a given type.`,
|
|
696
|
-
arguments.length !== 1
|
|
697
|
-
);
|
|
698
|
-
assert(
|
|
699
|
-
`Calling store.find(modelName, id, { preload: preload }) is no longer supported. Use store.findRecord(modelName, id, { preload: preload }) instead.`,
|
|
700
|
-
!options
|
|
701
|
-
);
|
|
702
|
-
assert(`You need to pass the model name and id to the store's find method`, arguments.length === 2);
|
|
703
|
-
assert(
|
|
704
|
-
`You cannot pass '${id}' as id to the store's find method`,
|
|
705
|
-
typeof id === 'string' || typeof id === 'number'
|
|
706
|
-
);
|
|
707
|
-
assert(
|
|
708
|
-
`Calling store.find() with a query object is no longer supported. Use store.query() instead.`,
|
|
709
|
-
typeof id !== 'object'
|
|
710
|
-
);
|
|
711
|
-
assert(
|
|
712
|
-
`Passing classes to store methods has been removed. Please pass a dasherized string instead of ${modelName}`,
|
|
713
|
-
typeof modelName === 'string'
|
|
714
|
-
);
|
|
715
|
-
|
|
716
|
-
if (DEPRECATE_STORE_FIND) {
|
|
717
|
-
deprecate(
|
|
718
|
-
`Using store.find is deprecated, use store.findRecord instead. Likely this means you are relying on the implicit store fetching behavior of routes unknowingly.`,
|
|
719
|
-
false,
|
|
720
|
-
{
|
|
721
|
-
id: 'ember-data:deprecate-store-find',
|
|
722
|
-
since: { available: '4.5', enabled: '4.5' },
|
|
723
|
-
for: 'ember-data',
|
|
724
|
-
until: '5.0',
|
|
725
|
-
}
|
|
726
|
-
);
|
|
727
|
-
return this.findRecord(modelName, id);
|
|
728
|
-
}
|
|
729
|
-
assert(`store.find has been removed. Use store.findRecord instead.`);
|
|
730
|
-
}
|
|
731
|
-
|
|
732
|
-
/**
|
|
733
|
-
This method returns a record for a given identifier or type and id combination.
|
|
734
|
-
|
|
735
|
-
The `findRecord` method will always resolve its promise with the same
|
|
736
|
-
object for a given identifier or type and `id`.
|
|
737
|
-
|
|
738
|
-
The `findRecord` method will always return a **promise** that will be
|
|
739
|
-
resolved with the record.
|
|
740
|
-
|
|
741
|
-
**Example 1**
|
|
742
|
-
|
|
743
|
-
```app/routes/post.js
|
|
744
|
-
import Route from '@ember/routing/route';
|
|
745
|
-
|
|
746
|
-
export default class PostRoute extends Route {
|
|
747
|
-
model({ post_id }) {
|
|
748
|
-
return this.store.findRecord('post', post_id);
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
```
|
|
752
|
-
|
|
753
|
-
**Example 2**
|
|
754
|
-
|
|
755
|
-
`findRecord` can be called with a single identifier argument instead of the combination
|
|
756
|
-
of `type` (modelName) and `id` as separate arguments. You may recognize this combo as
|
|
757
|
-
the typical pairing from [JSON:API](https://jsonapi.org/format/#document-resource-object-identification)
|
|
758
|
-
|
|
759
|
-
```app/routes/post.js
|
|
760
|
-
import Route from '@ember/routing/route';
|
|
761
|
-
|
|
762
|
-
export default class PostRoute extends Route {
|
|
763
|
-
model({ post_id: id }) {
|
|
764
|
-
return this.store.findRecord({ type: 'post', id });
|
|
765
|
-
}
|
|
766
|
-
}
|
|
767
|
-
```
|
|
768
|
-
|
|
769
|
-
**Example 3**
|
|
770
|
-
|
|
771
|
-
If you have previously received an lid via an Identifier for this record, and the record
|
|
772
|
-
has already been assigned an id, you can find the record again using just the lid.
|
|
773
|
-
|
|
774
|
-
```app/routes/post.js
|
|
775
|
-
store.findRecord({ lid });
|
|
776
|
-
```
|
|
777
|
-
|
|
778
|
-
If the record is not yet available, the store will ask the adapter's `findRecord`
|
|
779
|
-
method to retrieve and supply the necessary data. If the record is already present
|
|
780
|
-
in the store, it depends on the reload behavior _when_ the returned promise
|
|
781
|
-
resolves.
|
|
782
|
-
|
|
783
|
-
### Preloading
|
|
784
|
-
|
|
785
|
-
You can optionally `preload` specific attributes and relationships that you know of
|
|
786
|
-
by passing them via the passed `options`.
|
|
787
|
-
|
|
788
|
-
For example, if your Ember route looks like `/posts/1/comments/2` and your API route
|
|
789
|
-
for the comment also looks like `/posts/1/comments/2` if you want to fetch the comment
|
|
790
|
-
without also fetching the post you can pass in the post to the `findRecord` call:
|
|
791
|
-
|
|
792
|
-
```app/routes/post-comments.js
|
|
793
|
-
import Route from '@ember/routing/route';
|
|
794
|
-
|
|
795
|
-
export default class PostRoute extends Route {
|
|
796
|
-
model({ post_id, comment_id: id }) {
|
|
797
|
-
return this.store.findRecord({ type: 'comment', id, { preload: { post: post_id }} });
|
|
798
|
-
}
|
|
799
|
-
}
|
|
800
|
-
```
|
|
801
|
-
|
|
802
|
-
In your adapter you can then access this id without triggering a network request via the
|
|
803
|
-
snapshot:
|
|
804
|
-
|
|
805
|
-
```app/adapters/application.js
|
|
806
|
-
import EmberObject from '@ember/object';
|
|
807
|
-
|
|
808
|
-
export default class Adapter extends EmberObject {
|
|
809
|
-
|
|
810
|
-
findRecord(store, schema, id, snapshot) {
|
|
811
|
-
let type = schema.modelName;
|
|
812
|
-
|
|
813
|
-
if (type === 'comment')
|
|
814
|
-
let postId = snapshot.belongsTo('post', { id: true });
|
|
815
|
-
|
|
816
|
-
return fetch(`./posts/${postId}/comments/${id}`)
|
|
817
|
-
.then(response => response.json())
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
|
-
}
|
|
821
|
-
```
|
|
822
|
-
|
|
823
|
-
This could also be achieved by supplying the post id to the adapter via the adapterOptions
|
|
824
|
-
property on the options hash.
|
|
825
|
-
|
|
826
|
-
```app/routes/post-comments.js
|
|
827
|
-
import Route from '@ember/routing/route';
|
|
828
|
-
|
|
829
|
-
export default class PostRoute extends Route {
|
|
830
|
-
model({ post_id, comment_id: id }) {
|
|
831
|
-
return this.store.findRecord({ type: 'comment', id, { adapterOptions: { post: post_id }} });
|
|
832
|
-
}
|
|
833
|
-
}
|
|
834
|
-
```
|
|
835
|
-
|
|
836
|
-
```app/adapters/application.js
|
|
837
|
-
import EmberObject from '@ember/object';
|
|
838
|
-
|
|
839
|
-
export default class Adapter extends EmberObject {
|
|
840
|
-
|
|
841
|
-
findRecord(store, schema, id, snapshot) {
|
|
842
|
-
let type = schema.modelName;
|
|
843
|
-
|
|
844
|
-
if (type === 'comment')
|
|
845
|
-
let postId = snapshot.adapterOptions.post;
|
|
846
|
-
|
|
847
|
-
return fetch(`./posts/${postId}/comments/${id}`)
|
|
848
|
-
.then(response => response.json())
|
|
849
|
-
}
|
|
850
|
-
}
|
|
851
|
-
}
|
|
852
|
-
```
|
|
853
|
-
|
|
854
|
-
If you have access to the post model you can also pass the model itself to preload:
|
|
855
|
-
|
|
856
|
-
```javascript
|
|
857
|
-
let post = await store.findRecord('post', 1);
|
|
858
|
-
let comment = await store.findRecord('comment', 2, { post: myPostModel });
|
|
859
|
-
```
|
|
860
|
-
|
|
861
|
-
### Reloading
|
|
862
|
-
|
|
863
|
-
The reload behavior is configured either via the passed `options` hash or
|
|
864
|
-
the result of the adapter's `shouldReloadRecord`.
|
|
865
|
-
|
|
866
|
-
If `{ reload: true }` is passed or `adapter.shouldReloadRecord` evaluates
|
|
867
|
-
to `true`, then the returned promise resolves once the adapter returns
|
|
868
|
-
data, regardless if the requested record is already in the store:
|
|
869
|
-
|
|
870
|
-
```js
|
|
871
|
-
store.push({
|
|
872
|
-
data: {
|
|
873
|
-
id: 1,
|
|
874
|
-
type: 'post',
|
|
875
|
-
revision: 1
|
|
876
|
-
}
|
|
877
|
-
});
|
|
878
|
-
|
|
879
|
-
// adapter#findRecord resolves with
|
|
880
|
-
// [
|
|
881
|
-
// {
|
|
882
|
-
// id: 1,
|
|
883
|
-
// type: 'post',
|
|
884
|
-
// revision: 2
|
|
885
|
-
// }
|
|
886
|
-
// ]
|
|
887
|
-
store.findRecord('post', 1, { reload: true }).then(function(post) {
|
|
888
|
-
post.revision; // 2
|
|
889
|
-
});
|
|
890
|
-
```
|
|
891
|
-
|
|
892
|
-
If no reload is indicated via the above mentioned ways, then the promise
|
|
893
|
-
immediately resolves with the cached version in the store.
|
|
894
|
-
|
|
895
|
-
### Background Reloading
|
|
896
|
-
|
|
897
|
-
Optionally, if `adapter.shouldBackgroundReloadRecord` evaluates to `true`,
|
|
898
|
-
then a background reload is started, which updates the records' data, once
|
|
899
|
-
it is available:
|
|
900
|
-
|
|
901
|
-
```js
|
|
902
|
-
// app/adapters/post.js
|
|
903
|
-
import ApplicationAdapter from "./application";
|
|
904
|
-
|
|
905
|
-
export default class PostAdapter extends ApplicationAdapter {
|
|
906
|
-
shouldReloadRecord(store, snapshot) {
|
|
907
|
-
return false;
|
|
908
|
-
},
|
|
909
|
-
|
|
910
|
-
shouldBackgroundReloadRecord(store, snapshot) {
|
|
911
|
-
return true;
|
|
912
|
-
}
|
|
913
|
-
});
|
|
914
|
-
|
|
915
|
-
// ...
|
|
916
|
-
|
|
917
|
-
store.push({
|
|
918
|
-
data: {
|
|
919
|
-
id: 1,
|
|
920
|
-
type: 'post',
|
|
921
|
-
revision: 1
|
|
922
|
-
}
|
|
923
|
-
});
|
|
924
|
-
|
|
925
|
-
let blogPost = store.findRecord('post', 1).then(function(post) {
|
|
926
|
-
post.revision; // 1
|
|
927
|
-
});
|
|
928
|
-
|
|
929
|
-
// later, once adapter#findRecord resolved with
|
|
930
|
-
// [
|
|
931
|
-
// {
|
|
932
|
-
// id: 1,
|
|
933
|
-
// type: 'post',
|
|
934
|
-
// revision: 2
|
|
935
|
-
// }
|
|
936
|
-
// ]
|
|
937
|
-
|
|
938
|
-
blogPost.revision; // 2
|
|
939
|
-
```
|
|
940
|
-
|
|
941
|
-
If you would like to force or prevent background reloading, you can set a
|
|
942
|
-
boolean value for `backgroundReload` in the options object for
|
|
943
|
-
`findRecord`.
|
|
944
|
-
|
|
945
|
-
```app/routes/post/edit.js
|
|
946
|
-
import Route from '@ember/routing/route';
|
|
947
|
-
|
|
948
|
-
export default class PostEditRoute extends Route {
|
|
949
|
-
model(params) {
|
|
950
|
-
return this.store.findRecord('post', params.post_id, { backgroundReload: false });
|
|
951
|
-
}
|
|
952
|
-
}
|
|
953
|
-
```
|
|
954
|
-
|
|
955
|
-
If you pass an object on the `adapterOptions` property of the options
|
|
956
|
-
argument it will be passed to your adapter via the snapshot
|
|
957
|
-
|
|
958
|
-
```app/routes/post/edit.js
|
|
959
|
-
import Route from '@ember/routing/route';
|
|
960
|
-
|
|
961
|
-
export default class PostEditRoute extends Route {
|
|
962
|
-
model(params) {
|
|
963
|
-
return this.store.findRecord('post', params.post_id, {
|
|
964
|
-
adapterOptions: { subscribe: false }
|
|
965
|
-
});
|
|
966
|
-
}
|
|
967
|
-
}
|
|
968
|
-
```
|
|
969
|
-
|
|
970
|
-
```app/adapters/post.js
|
|
971
|
-
import MyCustomAdapter from './custom-adapter';
|
|
972
|
-
|
|
973
|
-
export default class PostAdapter extends MyCustomAdapter {
|
|
974
|
-
findRecord(store, type, id, snapshot) {
|
|
975
|
-
if (snapshot.adapterOptions.subscribe) {
|
|
976
|
-
// ...
|
|
977
|
-
}
|
|
978
|
-
// ...
|
|
979
|
-
}
|
|
980
|
-
}
|
|
981
|
-
```
|
|
982
|
-
|
|
983
|
-
See [peekRecord](../methods/peekRecord?anchor=peekRecord) to get the cached version of a record.
|
|
984
|
-
|
|
985
|
-
### Retrieving Related Model Records
|
|
986
|
-
|
|
987
|
-
If you use an adapter such as Ember's default
|
|
988
|
-
[`JSONAPIAdapter`](/ember-data/release/classes/JSONAPIAdapter)
|
|
989
|
-
that supports the [JSON API specification](http://jsonapi.org/) and if your server
|
|
990
|
-
endpoint supports the use of an
|
|
991
|
-
['include' query parameter](http://jsonapi.org/format/#fetching-includes),
|
|
992
|
-
you can use `findRecord()` or `findAll()` to automatically retrieve additional records related to
|
|
993
|
-
the one you request by supplying an `include` parameter in the `options` object.
|
|
994
|
-
|
|
995
|
-
For example, given a `post` model that has a `hasMany` relationship with a `comment`
|
|
996
|
-
model, when we retrieve a specific post we can have the server also return that post's
|
|
997
|
-
comments in the same request:
|
|
998
|
-
|
|
999
|
-
```app/routes/post.js
|
|
1000
|
-
import Route from '@ember/routing/route';
|
|
1001
|
-
|
|
1002
|
-
export default class PostRoute extends Route {
|
|
1003
|
-
model(params) {
|
|
1004
|
-
return this.store.findRecord('post', params.post_id, { include: 'comments' });
|
|
1005
|
-
}
|
|
1006
|
-
}
|
|
1007
|
-
```
|
|
1008
|
-
|
|
1009
|
-
```app/adapters/application.js
|
|
1010
|
-
import EmberObject from '@ember/object';
|
|
1011
|
-
|
|
1012
|
-
export default class Adapter extends EmberObject {
|
|
1013
|
-
|
|
1014
|
-
findRecord(store, schema, id, snapshot) {
|
|
1015
|
-
let type = schema.modelName;
|
|
1016
|
-
|
|
1017
|
-
if (type === 'post')
|
|
1018
|
-
let includes = snapshot.adapterOptions.include;
|
|
1019
|
-
|
|
1020
|
-
return fetch(`./posts/${postId}?include=${includes}`)
|
|
1021
|
-
.then(response => response.json())
|
|
1022
|
-
}
|
|
1023
|
-
}
|
|
1024
|
-
}
|
|
1025
|
-
```
|
|
1026
|
-
|
|
1027
|
-
In this case, the post's comments would then be available in your template as
|
|
1028
|
-
`model.comments`.
|
|
1029
|
-
|
|
1030
|
-
Multiple relationships can be requested using an `include` parameter consisting of a
|
|
1031
|
-
comma-separated list (without white-space) while nested relationships can be specified
|
|
1032
|
-
using a dot-separated sequence of relationship names. So to request both the post's
|
|
1033
|
-
comments and the authors of those comments the request would look like this:
|
|
1034
|
-
|
|
1035
|
-
```app/routes/post.js
|
|
1036
|
-
import Route from '@ember/routing/route';
|
|
1037
|
-
|
|
1038
|
-
export default class PostRoute extends Route {
|
|
1039
|
-
model(params) {
|
|
1040
|
-
return this.store.findRecord('post', params.post_id, { include: 'comments,comments.author' });
|
|
1041
|
-
}
|
|
1042
|
-
}
|
|
1043
|
-
```
|
|
1044
|
-
|
|
1045
|
-
### Retrieving Specific Fields by Type
|
|
1046
|
-
|
|
1047
|
-
If your server endpoint supports the use of a ['fields' query parameter](https://jsonapi.org/format/#fetching-sparse-fieldsets),
|
|
1048
|
-
you can use pass those fields through to your server. At this point in time, this requires a few manual steps on your part.
|
|
1049
|
-
|
|
1050
|
-
1. Implement `buildQuery` in your adapter.
|
|
1051
|
-
|
|
1052
|
-
```app/adapters/application.js
|
|
1053
|
-
buildQuery(snapshot) {
|
|
1054
|
-
let query = super.buildQuery(...arguments);
|
|
1055
|
-
|
|
1056
|
-
let { fields } = snapshot.adapterOptions;
|
|
1057
|
-
|
|
1058
|
-
if (fields) {
|
|
1059
|
-
query.fields = fields;
|
|
1060
|
-
}
|
|
1061
|
-
|
|
1062
|
-
return query;
|
|
1063
|
-
}
|
|
1064
|
-
```
|
|
1065
|
-
|
|
1066
|
-
2. Then pass through the applicable fields to your `findRecord` request.
|
|
1067
|
-
|
|
1068
|
-
Given a `post` model with attributes body, title, publishDate and meta, you can retrieve a filtered list of attributes.
|
|
1069
|
-
|
|
1070
|
-
```app/routes/post.js
|
|
1071
|
-
import Route from '@ember/routing/route';
|
|
1072
|
-
export default Route.extend({
|
|
1073
|
-
model(params) {
|
|
1074
|
-
return this.store.findRecord('post', params.post_id, { adapterOptions: { fields: { post: 'body,title' } });
|
|
1075
|
-
}
|
|
1076
|
-
});
|
|
1077
|
-
```
|
|
1078
|
-
|
|
1079
|
-
Moreover, you can filter attributes on related models as well. If a `post` has a `belongsTo` relationship to a user,
|
|
1080
|
-
just include the relationship key and attributes.
|
|
1081
|
-
|
|
1082
|
-
```app/routes/post.js
|
|
1083
|
-
import Route from '@ember/routing/route';
|
|
1084
|
-
export default Route.extend({
|
|
1085
|
-
model(params) {
|
|
1086
|
-
return this.store.findRecord('post', params.post_id, { adapterOptions: { fields: { post: 'body,title', user: 'name,email' } });
|
|
1087
|
-
}
|
|
1088
|
-
});
|
|
1089
|
-
```
|
|
1090
|
-
|
|
1091
|
-
@since 1.13.0
|
|
1092
|
-
@method findRecord
|
|
1093
|
-
@public
|
|
1094
|
-
@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
|
|
1095
|
-
@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
|
|
1096
|
-
@param {Object} [options] - if the first param is a string this will be the optional options for the request. See examples for available options.
|
|
1097
|
-
@return {Promise} promise
|
|
1098
|
-
*/
|
|
1099
|
-
findRecord(resource: string, id: string | number, options?: FindOptions): PromiseObject<RecordInstance>;
|
|
1100
|
-
findRecord(resource: ResourceIdentifierObject, id?: FindOptions): PromiseObject<RecordInstance>;
|
|
1101
|
-
findRecord(
|
|
1102
|
-
resource: string | ResourceIdentifierObject,
|
|
1103
|
-
id?: string | number | FindOptions,
|
|
1104
|
-
options?: FindOptions
|
|
1105
|
-
): PromiseObject<RecordInstance> {
|
|
1106
|
-
if (DEBUG) {
|
|
1107
|
-
assertDestroyingStore(this, 'findRecord');
|
|
1108
|
-
}
|
|
1109
|
-
|
|
1110
|
-
assert(
|
|
1111
|
-
`You need to pass a modelName or resource identifier as the first argument to the store's findRecord method`,
|
|
1112
|
-
resource
|
|
1113
|
-
);
|
|
1114
|
-
if (isMaybeIdentifier(resource)) {
|
|
1115
|
-
options = id as FindOptions | undefined;
|
|
1116
|
-
} else {
|
|
1117
|
-
assert(
|
|
1118
|
-
`Passing classes to store methods has been removed. Please pass a dasherized string instead of ${resource}`,
|
|
1119
|
-
typeof resource === 'string'
|
|
1120
|
-
);
|
|
1121
|
-
const type = normalizeModelName(resource);
|
|
1122
|
-
const normalizedId = ensureStringId(id as string | number);
|
|
1123
|
-
resource = constructResource(type, normalizedId);
|
|
1124
|
-
}
|
|
1125
|
-
|
|
1126
|
-
const identifier = this.identifierCache.getOrCreateRecordIdentifier(resource);
|
|
1127
|
-
let promise;
|
|
1128
|
-
options = options || {};
|
|
1129
|
-
|
|
1130
|
-
// if not loaded start loading
|
|
1131
|
-
if (!this._instanceCache.recordIsLoaded(identifier)) {
|
|
1132
|
-
promise = this._instanceCache._fetchDataIfNeededForIdentifier(identifier, options);
|
|
1133
|
-
|
|
1134
|
-
// Refetch if the reload option is passed
|
|
1135
|
-
} else if (options.reload) {
|
|
1136
|
-
assertIdentifierHasId(identifier);
|
|
1137
|
-
promise = this._fetchManager.scheduleFetch(identifier, options);
|
|
1138
|
-
} else {
|
|
1139
|
-
let snapshot: Snapshot | null = null;
|
|
1140
|
-
let adapter = this.adapterFor(identifier.type);
|
|
1141
|
-
|
|
1142
|
-
// Refetch the record if the adapter thinks the record is stale
|
|
1143
|
-
if (
|
|
1144
|
-
typeof options.reload === 'undefined' &&
|
|
1145
|
-
adapter.shouldReloadRecord &&
|
|
1146
|
-
adapter.shouldReloadRecord(this, (snapshot = this._instanceCache.createSnapshot(identifier, options)))
|
|
1147
|
-
) {
|
|
1148
|
-
assertIdentifierHasId(identifier);
|
|
1149
|
-
promise = this._fetchManager.scheduleFetch(identifier, options);
|
|
1150
|
-
} else {
|
|
1151
|
-
// Trigger the background refetch if backgroundReload option is passed
|
|
1152
|
-
if (
|
|
1153
|
-
options.backgroundReload !== false &&
|
|
1154
|
-
(options.backgroundReload ||
|
|
1155
|
-
!adapter.shouldBackgroundReloadRecord ||
|
|
1156
|
-
adapter.shouldBackgroundReloadRecord(
|
|
1157
|
-
this,
|
|
1158
|
-
(snapshot = snapshot || this._instanceCache.createSnapshot(identifier, options))
|
|
1159
|
-
))
|
|
1160
|
-
) {
|
|
1161
|
-
assertIdentifierHasId(identifier);
|
|
1162
|
-
this._fetchManager.scheduleFetch(identifier, options);
|
|
1163
|
-
}
|
|
1164
|
-
|
|
1165
|
-
// Return the cached record
|
|
1166
|
-
promise = resolve(identifier);
|
|
1167
|
-
}
|
|
1168
|
-
}
|
|
1169
|
-
|
|
1170
|
-
if (DEPRECATE_PROMISE_PROXIES) {
|
|
1171
|
-
return promiseRecord(this, promise);
|
|
1172
|
-
}
|
|
1173
|
-
|
|
1174
|
-
return promise.then((identifier: StableRecordIdentifier) => this.peekRecord(identifier));
|
|
1175
|
-
}
|
|
1176
|
-
|
|
1177
|
-
/**
|
|
1178
|
-
Get the reference for the specified record.
|
|
1179
|
-
|
|
1180
|
-
Example
|
|
1181
|
-
|
|
1182
|
-
```javascript
|
|
1183
|
-
let userRef = store.getReference('user', 1);
|
|
1184
|
-
|
|
1185
|
-
// check if the user is loaded
|
|
1186
|
-
let isLoaded = userRef.value() !== null;
|
|
1187
|
-
|
|
1188
|
-
// get the record of the reference (null if not yet available)
|
|
1189
|
-
let user = userRef.value();
|
|
1190
|
-
|
|
1191
|
-
// get the identifier of the reference
|
|
1192
|
-
if (userRef.remoteType() === 'id') {
|
|
1193
|
-
let id = userRef.id();
|
|
1194
|
-
}
|
|
1195
|
-
|
|
1196
|
-
// load user (via store.find)
|
|
1197
|
-
userRef.load().then(...)
|
|
1198
|
-
|
|
1199
|
-
// or trigger a reload
|
|
1200
|
-
userRef.reload().then(...)
|
|
1201
|
-
|
|
1202
|
-
// provide data for reference
|
|
1203
|
-
userRef.push({ id: 1, username: '@user' }).then(function(user) {
|
|
1204
|
-
userRef.value() === user;
|
|
1205
|
-
});
|
|
1206
|
-
```
|
|
1207
|
-
|
|
1208
|
-
@method getReference
|
|
1209
|
-
@public
|
|
1210
|
-
@param {String|object} resource - modelName (string) or Identifier (object)
|
|
1211
|
-
@param {String|Integer} id
|
|
1212
|
-
@since 2.5.0
|
|
1213
|
-
@return {RecordReference}
|
|
1214
|
-
*/
|
|
1215
|
-
// TODO @deprecate getReference (and references generally)
|
|
1216
|
-
getReference(resource: string | ResourceIdentifierObject, id: string | number): RecordReference {
|
|
1217
|
-
if (DEBUG) {
|
|
1218
|
-
assertDestroyingStore(this, 'getReference');
|
|
1219
|
-
}
|
|
1220
|
-
|
|
1221
|
-
let resourceIdentifier;
|
|
1222
|
-
if (arguments.length === 1 && isMaybeIdentifier(resource)) {
|
|
1223
|
-
resourceIdentifier = resource;
|
|
1224
|
-
} else {
|
|
1225
|
-
const type = normalizeModelName(resource as string);
|
|
1226
|
-
const normalizedId = ensureStringId(id);
|
|
1227
|
-
resourceIdentifier = constructResource(type, normalizedId);
|
|
1228
|
-
}
|
|
1229
|
-
|
|
1230
|
-
assert(
|
|
1231
|
-
'getReference expected to receive either a resource identifier or type and id as arguments',
|
|
1232
|
-
isMaybeIdentifier(resourceIdentifier)
|
|
1233
|
-
);
|
|
1234
|
-
|
|
1235
|
-
let identifier: StableRecordIdentifier = this.identifierCache.getOrCreateRecordIdentifier(resourceIdentifier);
|
|
1236
|
-
|
|
1237
|
-
return this._instanceCache.getReference(identifier);
|
|
1238
|
-
}
|
|
1239
|
-
|
|
1240
|
-
/**
|
|
1241
|
-
Get a record by a given type and ID without triggering a fetch.
|
|
1242
|
-
|
|
1243
|
-
This method will synchronously return the record if it is available in the store,
|
|
1244
|
-
otherwise it will return `null`. A record is available if it has been fetched earlier, or
|
|
1245
|
-
pushed manually into the store.
|
|
1246
|
-
|
|
1247
|
-
See [findRecord](../methods/findRecord?anchor=findRecord) if you would like to request this record from the backend.
|
|
1248
|
-
|
|
1249
|
-
_Note: This is a synchronous method and does not return a promise._
|
|
1250
|
-
|
|
1251
|
-
**Example 1**
|
|
1252
|
-
|
|
1253
|
-
```js
|
|
1254
|
-
let post = store.peekRecord('post', 1);
|
|
1255
|
-
|
|
1256
|
-
post.id; // 1
|
|
1257
|
-
```
|
|
1258
|
-
|
|
1259
|
-
`peekRecord` can be called with a single identifier argument instead of the combination
|
|
1260
|
-
of `type` (modelName) and `id` as separate arguments. You may recognize this combo as
|
|
1261
|
-
the typical pairing from [JSON:API](https://jsonapi.org/format/#document-resource-object-identification)
|
|
1262
|
-
|
|
1263
|
-
**Example 2**
|
|
1264
|
-
|
|
1265
|
-
```js
|
|
1266
|
-
let post = store.peekRecord({ type: 'post', id });
|
|
1267
|
-
post.id; // 1
|
|
1268
|
-
```
|
|
1269
|
-
|
|
1270
|
-
If you have previously received an lid from an Identifier for this record, you can lookup the record again using
|
|
1271
|
-
just the lid.
|
|
1272
|
-
|
|
1273
|
-
**Example 3**
|
|
1274
|
-
|
|
1275
|
-
```js
|
|
1276
|
-
let post = store.peekRecord({ lid });
|
|
1277
|
-
post.id; // 1
|
|
1278
|
-
```
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
@since 1.13.0
|
|
1282
|
-
@method peekRecord
|
|
1283
|
-
@public
|
|
1284
|
-
@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
|
|
1285
|
-
@param {String|Integer} id - optional only if the first param is a ResourceIdentifier, else the string id of the record to be retrieved.
|
|
1286
|
-
@return {Model|null} record
|
|
1287
|
-
*/
|
|
1288
|
-
peekRecord(identifier: string, id: string | number): RecordInstance | null;
|
|
1289
|
-
peekRecord(identifier: ResourceIdentifierObject): RecordInstance | null;
|
|
1290
|
-
peekRecord(identifier: ResourceIdentifierObject | string, id?: string | number): RecordInstance | null {
|
|
1291
|
-
if (arguments.length === 1 && isMaybeIdentifier(identifier)) {
|
|
1292
|
-
const stableIdentifier = this.identifierCache.peekRecordIdentifier(identifier);
|
|
1293
|
-
const isLoaded = stableIdentifier && this._instanceCache.recordIsLoaded(stableIdentifier);
|
|
1294
|
-
// TODO come up with a better mechanism for determining if we have data and could peek.
|
|
1295
|
-
// this is basically an "are we not empty" query.
|
|
1296
|
-
return isLoaded ? this._instanceCache.getRecord(stableIdentifier) : null;
|
|
1297
|
-
}
|
|
1298
|
-
|
|
1299
|
-
if (DEBUG) {
|
|
1300
|
-
assertDestroyingStore(this, 'peekRecord');
|
|
1301
|
-
}
|
|
1302
|
-
|
|
1303
|
-
assert(`You need to pass a model name to the store's peekRecord method`, identifier);
|
|
1304
|
-
assert(
|
|
1305
|
-
`Passing classes to store methods has been removed. Please pass a dasherized string instead of ${identifier}`,
|
|
1306
|
-
typeof identifier === 'string'
|
|
1307
|
-
);
|
|
1308
|
-
|
|
1309
|
-
const type = normalizeModelName(identifier);
|
|
1310
|
-
const normalizedId = ensureStringId(id);
|
|
1311
|
-
const resource = { type, id: normalizedId };
|
|
1312
|
-
const stableIdentifier = this.identifierCache.peekRecordIdentifier(resource);
|
|
1313
|
-
const isLoaded = stableIdentifier && this._instanceCache.recordIsLoaded(stableIdentifier);
|
|
1314
|
-
|
|
1315
|
-
return isLoaded ? this._instanceCache.getRecord(stableIdentifier) : null;
|
|
1316
|
-
}
|
|
1317
|
-
|
|
1318
|
-
/**
|
|
1319
|
-
This method returns true if a record for a given modelName and id is already
|
|
1320
|
-
loaded in the store. Use this function to know beforehand if a findRecord()
|
|
1321
|
-
will result in a request or that it will be a cache hit.
|
|
1322
|
-
|
|
1323
|
-
Example
|
|
1324
|
-
|
|
1325
|
-
```javascript
|
|
1326
|
-
store.hasRecordForId('post', 1); // false
|
|
1327
|
-
store.findRecord('post', 1).then(function() {
|
|
1328
|
-
store.hasRecordForId('post', 1); // true
|
|
1329
|
-
});
|
|
1330
|
-
```
|
|
1331
|
-
|
|
1332
|
-
@method hasRecordForId
|
|
1333
|
-
@deprecated
|
|
1334
|
-
@public
|
|
1335
|
-
@param {String} modelName
|
|
1336
|
-
@param {(String|Integer)} id
|
|
1337
|
-
@return {Boolean}
|
|
1338
|
-
*/
|
|
1339
|
-
hasRecordForId(modelName: string, id: string | number): boolean {
|
|
1340
|
-
if (DEPRECATE_HAS_RECORD) {
|
|
1341
|
-
deprecate(`store.hasRecordForId has been deprecated in favor of store.peekRecord`, false, {
|
|
1342
|
-
id: 'ember-data:deprecate-has-record-for-id',
|
|
1343
|
-
since: { available: '4.5', enabled: '4.5' },
|
|
1344
|
-
until: '5.0',
|
|
1345
|
-
for: 'ember-data',
|
|
1346
|
-
});
|
|
1347
|
-
if (DEBUG) {
|
|
1348
|
-
assertDestroyingStore(this, 'hasRecordForId');
|
|
1349
|
-
}
|
|
1350
|
-
assert(`You need to pass a model name to the store's hasRecordForId method`, modelName);
|
|
1351
|
-
assert(
|
|
1352
|
-
`Passing classes to store methods has been removed. Please pass a dasherized string instead of ${modelName}`,
|
|
1353
|
-
typeof modelName === 'string'
|
|
1354
|
-
);
|
|
1355
|
-
|
|
1356
|
-
const type = normalizeModelName(modelName);
|
|
1357
|
-
const trueId = ensureStringId(id);
|
|
1358
|
-
const resource = { type, id: trueId };
|
|
1359
|
-
|
|
1360
|
-
const identifier = this.identifierCache.peekRecordIdentifier(resource);
|
|
1361
|
-
return Boolean(identifier && this._instanceCache.recordIsLoaded(identifier));
|
|
1362
|
-
}
|
|
1363
|
-
assert(`store.hasRecordForId has been removed`);
|
|
1364
|
-
}
|
|
1365
|
-
|
|
1366
|
-
/**
|
|
1367
|
-
This method delegates a query to the adapter. This is the one place where
|
|
1368
|
-
adapter-level semantics are exposed to the application.
|
|
1369
|
-
|
|
1370
|
-
Each time this method is called a new request is made through the adapter.
|
|
1371
|
-
|
|
1372
|
-
Exposing queries this way seems preferable to creating an abstract query
|
|
1373
|
-
language for all server-side queries, and then require all adapters to
|
|
1374
|
-
implement them.
|
|
1375
|
-
|
|
1376
|
-
---
|
|
1377
|
-
|
|
1378
|
-
If you do something like this:
|
|
1379
|
-
|
|
1380
|
-
```javascript
|
|
1381
|
-
store.query('person', { page: 1 });
|
|
1382
|
-
```
|
|
1383
|
-
|
|
1384
|
-
The request made to the server will look something like this:
|
|
1385
|
-
|
|
1386
|
-
```
|
|
1387
|
-
GET "/api/v1/person?page=1"
|
|
1388
|
-
```
|
|
1389
|
-
|
|
1390
|
-
---
|
|
1391
|
-
|
|
1392
|
-
If you do something like this:
|
|
1393
|
-
|
|
1394
|
-
```javascript
|
|
1395
|
-
store.query('person', { ids: [1, 2, 3] });
|
|
1396
|
-
```
|
|
1397
|
-
|
|
1398
|
-
The request made to the server will look something like this:
|
|
1399
|
-
|
|
1400
|
-
```
|
|
1401
|
-
GET "/api/v1/person?ids%5B%5D=1&ids%5B%5D=2&ids%5B%5D=3"
|
|
1402
|
-
decoded: "/api/v1/person?ids[]=1&ids[]=2&ids[]=3"
|
|
1403
|
-
```
|
|
1404
|
-
|
|
1405
|
-
This method returns a promise, which is resolved with a
|
|
1406
|
-
[`Collection`](/ember-data/release/classes/Collection)
|
|
1407
|
-
once the server returns.
|
|
1408
|
-
|
|
1409
|
-
@since 1.13.0
|
|
1410
|
-
@method query
|
|
1411
|
-
@public
|
|
1412
|
-
@param {String} modelName
|
|
1413
|
-
@param {any} query an opaque query to be used by the adapter
|
|
1414
|
-
@param {Object} options optional, may include `adapterOptions` hash which will be passed to adapter.query
|
|
1415
|
-
@return {Promise} promise
|
|
1416
|
-
*/
|
|
1417
|
-
query(modelName: string, query, options): PromiseArray<RecordInstance, Collection> | Promise<Collection> {
|
|
1418
|
-
if (DEBUG) {
|
|
1419
|
-
assertDestroyingStore(this, 'query');
|
|
1420
|
-
}
|
|
1421
|
-
assert(`You need to pass a model name to the store's query method`, modelName);
|
|
1422
|
-
assert(`You need to pass a query hash to the store's query method`, query);
|
|
1423
|
-
assert(
|
|
1424
|
-
`Passing classes to store methods has been removed. Please pass a dasherized string instead of ${modelName}`,
|
|
1425
|
-
typeof modelName === 'string'
|
|
1426
|
-
);
|
|
1427
|
-
|
|
1428
|
-
let adapterOptionsWrapper: { adapterOptions?: any } = {};
|
|
1429
|
-
|
|
1430
|
-
if (options && options.adapterOptions) {
|
|
1431
|
-
adapterOptionsWrapper.adapterOptions = options.adapterOptions;
|
|
1432
|
-
}
|
|
1433
|
-
let recordArray = options?._recordArray || null;
|
|
1434
|
-
|
|
1435
|
-
let normalizedModelName = normalizeModelName(modelName);
|
|
1436
|
-
let adapter = this.adapterFor(normalizedModelName);
|
|
1437
|
-
|
|
1438
|
-
assert(`You tried to load a query but you have no adapter (for ${modelName})`, adapter);
|
|
1439
|
-
assert(
|
|
1440
|
-
`You tried to load a query but your adapter does not implement 'query'`,
|
|
1441
|
-
typeof adapter.query === 'function'
|
|
1442
|
-
);
|
|
1443
|
-
|
|
1444
|
-
let queryPromise = _query(
|
|
1445
|
-
adapter,
|
|
1446
|
-
this,
|
|
1447
|
-
normalizedModelName,
|
|
1448
|
-
query,
|
|
1449
|
-
recordArray,
|
|
1450
|
-
adapterOptionsWrapper
|
|
1451
|
-
) as unknown as Promise<Collection>;
|
|
1452
|
-
|
|
1453
|
-
if (DEPRECATE_PROMISE_PROXIES) {
|
|
1454
|
-
return promiseArray(queryPromise);
|
|
1455
|
-
}
|
|
1456
|
-
return queryPromise;
|
|
1457
|
-
}
|
|
1458
|
-
|
|
1459
|
-
/**
|
|
1460
|
-
This method makes a request for one record, where the `id` is not known
|
|
1461
|
-
beforehand (if the `id` is known, use [`findRecord`](../methods/findRecord?anchor=findRecord)
|
|
1462
|
-
instead).
|
|
1463
|
-
|
|
1464
|
-
This method can be used when it is certain that the server will return a
|
|
1465
|
-
single object for the primary data.
|
|
1466
|
-
|
|
1467
|
-
Each time this method is called a new request is made through the adapter.
|
|
1468
|
-
|
|
1469
|
-
Let's assume our API provides an endpoint for the currently logged in user
|
|
1470
|
-
via:
|
|
1471
|
-
|
|
1472
|
-
```
|
|
1473
|
-
// GET /api/current_user
|
|
1474
|
-
{
|
|
1475
|
-
user: {
|
|
1476
|
-
id: 1234,
|
|
1477
|
-
username: 'admin'
|
|
1478
|
-
}
|
|
1479
|
-
}
|
|
1480
|
-
```
|
|
1481
|
-
|
|
1482
|
-
Since the specific `id` of the `user` is not known beforehand, we can use
|
|
1483
|
-
`queryRecord` to get the user:
|
|
1484
|
-
|
|
1485
|
-
```javascript
|
|
1486
|
-
store.queryRecord('user', {}).then(function(user) {
|
|
1487
|
-
let username = user.username;
|
|
1488
|
-
// do thing
|
|
1489
|
-
});
|
|
1490
|
-
```
|
|
1491
|
-
|
|
1492
|
-
The request is made through the adapters' `queryRecord`:
|
|
1493
|
-
|
|
1494
|
-
```app/adapters/user.js
|
|
1495
|
-
import Adapter from '@ember-data/adapter';
|
|
1496
|
-
import $ from 'jquery';
|
|
1497
|
-
|
|
1498
|
-
export default class UserAdapter extends Adapter {
|
|
1499
|
-
queryRecord(modelName, query) {
|
|
1500
|
-
return $.getJSON('/api/current_user');
|
|
1501
|
-
}
|
|
1502
|
-
}
|
|
1503
|
-
```
|
|
1504
|
-
|
|
1505
|
-
Note: the primary use case for `store.queryRecord` is when a single record
|
|
1506
|
-
is queried and the `id` is not known beforehand. In all other cases
|
|
1507
|
-
`store.query` and using the first item of the array is likely the preferred
|
|
1508
|
-
way:
|
|
1509
|
-
|
|
1510
|
-
```
|
|
1511
|
-
// GET /users?username=unique
|
|
1512
|
-
{
|
|
1513
|
-
data: [{
|
|
1514
|
-
id: 1234,
|
|
1515
|
-
type: 'user',
|
|
1516
|
-
attributes: {
|
|
1517
|
-
username: "unique"
|
|
1518
|
-
}
|
|
1519
|
-
}]
|
|
1520
|
-
}
|
|
1521
|
-
```
|
|
1522
|
-
|
|
1523
|
-
```javascript
|
|
1524
|
-
store.query('user', { username: 'unique' }).then(function(users) {
|
|
1525
|
-
return users.firstObject;
|
|
1526
|
-
}).then(function(user) {
|
|
1527
|
-
let id = user.id;
|
|
1528
|
-
});
|
|
1529
|
-
```
|
|
1530
|
-
|
|
1531
|
-
This method returns a promise, which resolves with the found record.
|
|
1532
|
-
|
|
1533
|
-
If the adapter returns no data for the primary data of the payload, then
|
|
1534
|
-
`queryRecord` resolves with `null`:
|
|
1535
|
-
|
|
1536
|
-
```
|
|
1537
|
-
// GET /users?username=unique
|
|
1538
|
-
{
|
|
1539
|
-
data: null
|
|
1540
|
-
}
|
|
1541
|
-
```
|
|
1542
|
-
|
|
1543
|
-
```javascript
|
|
1544
|
-
store.queryRecord('user', { username: 'unique' }).then(function(user) {
|
|
1545
|
-
// user is null
|
|
1546
|
-
});
|
|
1547
|
-
```
|
|
1548
|
-
|
|
1549
|
-
@since 1.13.0
|
|
1550
|
-
@method queryRecord
|
|
1551
|
-
@public
|
|
1552
|
-
@param {String} modelName
|
|
1553
|
-
@param {any} query an opaque query to be used by the adapter
|
|
1554
|
-
@param {Object} options optional, may include `adapterOptions` hash which will be passed to adapter.queryRecord
|
|
1555
|
-
@return {Promise} promise which resolves with the found record or `null`
|
|
1556
|
-
*/
|
|
1557
|
-
queryRecord(
|
|
1558
|
-
modelName: string,
|
|
1559
|
-
query,
|
|
1560
|
-
options?
|
|
1561
|
-
): PromiseObject<RecordInstance | null> | Promise<RecordInstance | null> {
|
|
1562
|
-
if (DEBUG) {
|
|
1563
|
-
assertDestroyingStore(this, 'queryRecord');
|
|
1564
|
-
}
|
|
1565
|
-
assert(`You need to pass a model name to the store's queryRecord method`, modelName);
|
|
1566
|
-
assert(`You need to pass a query hash to the store's queryRecord method`, query);
|
|
1567
|
-
assert(
|
|
1568
|
-
`Passing classes to store methods has been removed. Please pass a dasherized string instead of ${modelName}`,
|
|
1569
|
-
typeof modelName === 'string'
|
|
1570
|
-
);
|
|
1571
|
-
|
|
1572
|
-
let normalizedModelName = normalizeModelName(modelName);
|
|
1573
|
-
let adapter = this.adapterFor(normalizedModelName);
|
|
1574
|
-
let adapterOptionsWrapper: { adapterOptions?: any } = {};
|
|
1575
|
-
|
|
1576
|
-
if (options && options.adapterOptions) {
|
|
1577
|
-
adapterOptionsWrapper.adapterOptions = options.adapterOptions;
|
|
1578
|
-
}
|
|
1579
|
-
|
|
1580
|
-
assert(`You tried to make a query but you have no adapter (for ${normalizedModelName})`, adapter);
|
|
1581
|
-
assert(
|
|
1582
|
-
`You tried to make a query but your adapter does not implement 'queryRecord'`,
|
|
1583
|
-
typeof adapter.queryRecord === 'function'
|
|
1584
|
-
);
|
|
1585
|
-
|
|
1586
|
-
const promise: Promise<StableRecordIdentifier | null> = _queryRecord(
|
|
1587
|
-
adapter,
|
|
1588
|
-
this,
|
|
1589
|
-
normalizedModelName,
|
|
1590
|
-
query,
|
|
1591
|
-
adapterOptionsWrapper
|
|
1592
|
-
) as Promise<StableRecordIdentifier | null>;
|
|
1593
|
-
|
|
1594
|
-
if (DEPRECATE_PROMISE_PROXIES) {
|
|
1595
|
-
return promiseObject(promise.then((identifier) => identifier && this.peekRecord(identifier)));
|
|
1596
|
-
}
|
|
1597
|
-
return promise.then((identifier) => identifier && this.peekRecord(identifier));
|
|
1598
|
-
}
|
|
1599
|
-
|
|
1600
|
-
/**
|
|
1601
|
-
`findAll` asks the adapter's `findAll` method to find the records for the
|
|
1602
|
-
given type, and returns a promise which will resolve with all records of
|
|
1603
|
-
this type present in the store, even if the adapter only returns a subset
|
|
1604
|
-
of them.
|
|
1605
|
-
|
|
1606
|
-
```app/routes/authors.js
|
|
1607
|
-
import Route from '@ember/routing/route';
|
|
1608
|
-
|
|
1609
|
-
export default class AuthorsRoute extends Route {
|
|
1610
|
-
model(params) {
|
|
1611
|
-
return this.store.findAll('author');
|
|
1612
|
-
}
|
|
1613
|
-
}
|
|
1614
|
-
```
|
|
1615
|
-
|
|
1616
|
-
_When_ the returned promise resolves depends on the reload behavior,
|
|
1617
|
-
configured via the passed `options` hash and the result of the adapter's
|
|
1618
|
-
`shouldReloadAll` method.
|
|
1619
|
-
|
|
1620
|
-
### Reloading
|
|
1621
|
-
|
|
1622
|
-
If `{ reload: true }` is passed or `adapter.shouldReloadAll` evaluates to
|
|
1623
|
-
`true`, then the returned promise resolves once the adapter returns data,
|
|
1624
|
-
regardless if there are already records in the store:
|
|
1625
|
-
|
|
1626
|
-
```js
|
|
1627
|
-
store.push({
|
|
1628
|
-
data: {
|
|
1629
|
-
id: 'first',
|
|
1630
|
-
type: 'author'
|
|
1631
|
-
}
|
|
1632
|
-
});
|
|
1633
|
-
|
|
1634
|
-
// adapter#findAll resolves with
|
|
1635
|
-
// [
|
|
1636
|
-
// {
|
|
1637
|
-
// id: 'second',
|
|
1638
|
-
// type: 'author'
|
|
1639
|
-
// }
|
|
1640
|
-
// ]
|
|
1641
|
-
store.findAll('author', { reload: true }).then(function(authors) {
|
|
1642
|
-
authors.getEach('id'); // ['first', 'second']
|
|
1643
|
-
});
|
|
1644
|
-
```
|
|
1645
|
-
|
|
1646
|
-
If no reload is indicated via the above mentioned ways, then the promise
|
|
1647
|
-
immediately resolves with all the records currently loaded in the store.
|
|
1648
|
-
|
|
1649
|
-
### Background Reloading
|
|
1650
|
-
|
|
1651
|
-
Optionally, if `adapter.shouldBackgroundReloadAll` evaluates to `true`,
|
|
1652
|
-
then a background reload is started. Once this resolves, the array with
|
|
1653
|
-
which the promise resolves, is updated automatically so it contains all the
|
|
1654
|
-
records in the store:
|
|
1655
|
-
|
|
1656
|
-
```app/adapters/application.js
|
|
1657
|
-
import Adapter from '@ember-data/adapter';
|
|
1658
|
-
|
|
1659
|
-
export default class ApplicationAdapter extends Adapter {
|
|
1660
|
-
shouldReloadAll(store, snapshotsArray) {
|
|
1661
|
-
return false;
|
|
1662
|
-
},
|
|
1663
|
-
|
|
1664
|
-
shouldBackgroundReloadAll(store, snapshotsArray) {
|
|
1665
|
-
return true;
|
|
1666
|
-
}
|
|
1667
|
-
});
|
|
1668
|
-
|
|
1669
|
-
// ...
|
|
1670
|
-
|
|
1671
|
-
store.push({
|
|
1672
|
-
data: {
|
|
1673
|
-
id: 'first',
|
|
1674
|
-
type: 'author'
|
|
1675
|
-
}
|
|
1676
|
-
});
|
|
1677
|
-
|
|
1678
|
-
let allAuthors;
|
|
1679
|
-
store.findAll('author').then(function(authors) {
|
|
1680
|
-
authors.getEach('id'); // ['first']
|
|
1681
|
-
|
|
1682
|
-
allAuthors = authors;
|
|
1683
|
-
});
|
|
1684
|
-
|
|
1685
|
-
// later, once adapter#findAll resolved with
|
|
1686
|
-
// [
|
|
1687
|
-
// {
|
|
1688
|
-
// id: 'second',
|
|
1689
|
-
// type: 'author'
|
|
1690
|
-
// }
|
|
1691
|
-
// ]
|
|
1692
|
-
|
|
1693
|
-
allAuthors.getEach('id'); // ['first', 'second']
|
|
1694
|
-
```
|
|
1695
|
-
|
|
1696
|
-
If you would like to force or prevent background reloading, you can set a
|
|
1697
|
-
boolean value for `backgroundReload` in the options object for
|
|
1698
|
-
`findAll`.
|
|
1699
|
-
|
|
1700
|
-
```app/routes/post/edit.js
|
|
1701
|
-
import Route from '@ember/routing/route';
|
|
1702
|
-
|
|
1703
|
-
export default class PostEditRoute extends Route {
|
|
1704
|
-
model() {
|
|
1705
|
-
return this.store.findAll('post', { backgroundReload: false });
|
|
1706
|
-
}
|
|
1707
|
-
}
|
|
1708
|
-
```
|
|
1709
|
-
|
|
1710
|
-
If you pass an object on the `adapterOptions` property of the options
|
|
1711
|
-
argument it will be passed to you adapter via the `snapshotRecordArray`
|
|
1712
|
-
|
|
1713
|
-
```app/routes/posts.js
|
|
1714
|
-
import Route from '@ember/routing/route';
|
|
1715
|
-
|
|
1716
|
-
export default class PostsRoute extends Route {
|
|
1717
|
-
model(params) {
|
|
1718
|
-
return this.store.findAll('post', {
|
|
1719
|
-
adapterOptions: { subscribe: false }
|
|
1720
|
-
});
|
|
1721
|
-
}
|
|
1722
|
-
}
|
|
1723
|
-
```
|
|
1724
|
-
|
|
1725
|
-
```app/adapters/post.js
|
|
1726
|
-
import MyCustomAdapter from './custom-adapter';
|
|
1727
|
-
|
|
1728
|
-
export default class UserAdapter extends MyCustomAdapter {
|
|
1729
|
-
findAll(store, type, sinceToken, snapshotRecordArray) {
|
|
1730
|
-
if (snapshotRecordArray.adapterOptions.subscribe) {
|
|
1731
|
-
// ...
|
|
1732
|
-
}
|
|
1733
|
-
// ...
|
|
1734
|
-
}
|
|
1735
|
-
}
|
|
1736
|
-
```
|
|
1737
|
-
|
|
1738
|
-
See [peekAll](../methods/peekAll?anchor=peekAll) to get an array of current records in the
|
|
1739
|
-
store, without waiting until a reload is finished.
|
|
1740
|
-
|
|
1741
|
-
### Retrieving Related Model Records
|
|
1742
|
-
|
|
1743
|
-
If you use an adapter such as Ember's default
|
|
1744
|
-
[`JSONAPIAdapter`](/ember-data/release/classes/JSONAPIAdapter)
|
|
1745
|
-
that supports the [JSON API specification](http://jsonapi.org/) and if your server
|
|
1746
|
-
endpoint supports the use of an
|
|
1747
|
-
['include' query parameter](http://jsonapi.org/format/#fetching-includes),
|
|
1748
|
-
you can use `findAll()` to automatically retrieve additional records related to
|
|
1749
|
-
those requested by supplying an `include` parameter in the `options` object.
|
|
1750
|
-
|
|
1751
|
-
For example, given a `post` model that has a `hasMany` relationship with a `comment`
|
|
1752
|
-
model, when we retrieve all of the post records we can have the server also return
|
|
1753
|
-
all of the posts' comments in the same request:
|
|
1754
|
-
|
|
1755
|
-
```app/routes/posts.js
|
|
1756
|
-
import Route from '@ember/routing/route';
|
|
1757
|
-
|
|
1758
|
-
export default class PostsRoute extends Route {
|
|
1759
|
-
model() {
|
|
1760
|
-
return this.store.findAll('post', { include: 'comments' });
|
|
1761
|
-
}
|
|
1762
|
-
}
|
|
1763
|
-
```
|
|
1764
|
-
Multiple relationships can be requested using an `include` parameter consisting of a
|
|
1765
|
-
comma-separated list (without white-space) while nested relationships can be specified
|
|
1766
|
-
using a dot-separated sequence of relationship names. So to request both the posts'
|
|
1767
|
-
comments and the authors of those comments the request would look like this:
|
|
1768
|
-
|
|
1769
|
-
```app/routes/posts.js
|
|
1770
|
-
import Route from '@ember/routing/route';
|
|
1771
|
-
|
|
1772
|
-
export default class PostsRoute extends Route {
|
|
1773
|
-
model() {
|
|
1774
|
-
return this.store.findAll('post', { include: 'comments,comments.author' });
|
|
1775
|
-
}
|
|
1776
|
-
}
|
|
1777
|
-
```
|
|
1778
|
-
|
|
1779
|
-
See [query](../methods/query?anchor=query) to only get a subset of records from the server.
|
|
1780
|
-
|
|
1781
|
-
@since 1.13.0
|
|
1782
|
-
@method findAll
|
|
1783
|
-
@public
|
|
1784
|
-
@param {String} modelName
|
|
1785
|
-
@param {Object} options
|
|
1786
|
-
@return {Promise} promise
|
|
1787
|
-
*/
|
|
1788
|
-
findAll(
|
|
1789
|
-
modelName: string,
|
|
1790
|
-
options: { reload?: boolean; backgroundReload?: boolean } = {}
|
|
1791
|
-
): PromiseArray<RecordInstance, IdentifierArray> {
|
|
1792
|
-
if (DEBUG) {
|
|
1793
|
-
assertDestroyingStore(this, 'findAll');
|
|
1794
|
-
}
|
|
1795
|
-
assert(`You need to pass a model name to the store's findAll method`, modelName);
|
|
1796
|
-
assert(
|
|
1797
|
-
`Passing classes to store methods has been removed. Please pass a dasherized string instead of ${modelName}`,
|
|
1798
|
-
typeof modelName === 'string'
|
|
1799
|
-
);
|
|
1800
|
-
|
|
1801
|
-
let normalizedModelName = normalizeModelName(modelName);
|
|
1802
|
-
let array = this.peekAll(normalizedModelName);
|
|
1803
|
-
let fetch;
|
|
1804
|
-
|
|
1805
|
-
let adapter = this.adapterFor(normalizedModelName);
|
|
1806
|
-
|
|
1807
|
-
assert(`You tried to load all records but you have no adapter (for ${normalizedModelName})`, adapter);
|
|
1808
|
-
assert(
|
|
1809
|
-
`You tried to load all records but your adapter does not implement 'findAll'`,
|
|
1810
|
-
typeof adapter.findAll === 'function'
|
|
1811
|
-
);
|
|
1812
|
-
|
|
1813
|
-
if (options.reload) {
|
|
1814
|
-
array.isUpdating = true;
|
|
1815
|
-
fetch = _findAll(adapter, this, normalizedModelName, options);
|
|
1816
|
-
} else {
|
|
1817
|
-
let snapshotArray = new SnapshotRecordArray(this, array, options);
|
|
1818
|
-
|
|
1819
|
-
if (options.reload !== false) {
|
|
1820
|
-
if (
|
|
1821
|
-
(adapter.shouldReloadAll && adapter.shouldReloadAll(this, snapshotArray)) ||
|
|
1822
|
-
(!adapter.shouldReloadAll && snapshotArray.length === 0)
|
|
1823
|
-
) {
|
|
1824
|
-
array.isUpdating = true;
|
|
1825
|
-
fetch = _findAll(adapter, this, modelName, options, snapshotArray);
|
|
1826
|
-
}
|
|
1827
|
-
}
|
|
1828
|
-
|
|
1829
|
-
if (!fetch) {
|
|
1830
|
-
if (options.backgroundReload === false) {
|
|
1831
|
-
fetch = resolve(array);
|
|
1832
|
-
} else if (
|
|
1833
|
-
options.backgroundReload ||
|
|
1834
|
-
!adapter.shouldBackgroundReloadAll ||
|
|
1835
|
-
adapter.shouldBackgroundReloadAll(this, snapshotArray)
|
|
1836
|
-
) {
|
|
1837
|
-
array.isUpdating = true;
|
|
1838
|
-
_findAll(adapter, this, modelName, options, snapshotArray);
|
|
1839
|
-
}
|
|
1840
|
-
|
|
1841
|
-
fetch = resolve(array);
|
|
1842
|
-
}
|
|
1843
|
-
}
|
|
1844
|
-
|
|
1845
|
-
if (DEPRECATE_PROMISE_PROXIES) {
|
|
1846
|
-
return promiseArray(fetch);
|
|
1847
|
-
}
|
|
1848
|
-
return fetch;
|
|
1849
|
-
}
|
|
1850
|
-
|
|
1851
|
-
/**
|
|
1852
|
-
This method returns a filtered array that contains all of the
|
|
1853
|
-
known records for a given type in the store.
|
|
1854
|
-
|
|
1855
|
-
Note that because it's just a filter, the result will contain any
|
|
1856
|
-
locally created records of the type, however, it will not make a
|
|
1857
|
-
request to the backend to retrieve additional records. If you
|
|
1858
|
-
would like to request all the records from the backend please use
|
|
1859
|
-
[store.findAll](../methods/findAll?anchor=findAll).
|
|
1860
|
-
|
|
1861
|
-
Also note that multiple calls to `peekAll` for a given type will always
|
|
1862
|
-
return the same `RecordArray`.
|
|
1863
|
-
|
|
1864
|
-
Example
|
|
1865
|
-
|
|
1866
|
-
```javascript
|
|
1867
|
-
let localPosts = store.peekAll('post');
|
|
1868
|
-
```
|
|
1869
|
-
|
|
1870
|
-
@since 1.13.0
|
|
1871
|
-
@method peekAll
|
|
1872
|
-
@public
|
|
1873
|
-
@param {String} modelName
|
|
1874
|
-
@return {RecordArray}
|
|
1875
|
-
*/
|
|
1876
|
-
peekAll(modelName: string): IdentifierArray {
|
|
1877
|
-
if (DEBUG) {
|
|
1878
|
-
assertDestroyingStore(this, 'peekAll');
|
|
1879
|
-
}
|
|
1880
|
-
assert(`You need to pass a model name to the store's peekAll method`, modelName);
|
|
1881
|
-
assert(
|
|
1882
|
-
`Passing classes to store methods has been removed. Please pass a dasherized string instead of ${modelName}`,
|
|
1883
|
-
typeof modelName === 'string'
|
|
1884
|
-
);
|
|
1885
|
-
|
|
1886
|
-
let type = normalizeModelName(modelName);
|
|
1887
|
-
return this.recordArrayManager.liveArrayFor(type);
|
|
1888
|
-
}
|
|
1889
|
-
|
|
1890
|
-
/**
|
|
1891
|
-
This method unloads all records in the store.
|
|
1892
|
-
It schedules unloading to happen during the next run loop.
|
|
1893
|
-
|
|
1894
|
-
Optionally you can pass a type which unload all records for a given type.
|
|
1895
|
-
|
|
1896
|
-
```javascript
|
|
1897
|
-
store.unloadAll();
|
|
1898
|
-
store.unloadAll('post');
|
|
1899
|
-
```
|
|
1900
|
-
|
|
1901
|
-
@method unloadAll
|
|
1902
|
-
@public
|
|
1903
|
-
@param {String} modelName
|
|
1904
|
-
*/
|
|
1905
|
-
unloadAll(modelName?: string) {
|
|
1906
|
-
if (DEBUG) {
|
|
1907
|
-
assertDestroyedStoreOnly(this, 'unloadAll');
|
|
1908
|
-
}
|
|
1909
|
-
assert(
|
|
1910
|
-
`Passing classes to store methods has been removed. Please pass a dasherized string instead of ${modelName}`,
|
|
1911
|
-
!modelName || typeof modelName === 'string'
|
|
1912
|
-
);
|
|
1913
|
-
|
|
1914
|
-
this._join(() => {
|
|
1915
|
-
if (modelName === undefined) {
|
|
1916
|
-
// destroy the graph before unloadAll
|
|
1917
|
-
// since then we avoid churning relationships
|
|
1918
|
-
// during unload
|
|
1919
|
-
if (HAS_RECORD_DATA_PACKAGE) {
|
|
1920
|
-
const peekGraph = (
|
|
1921
|
-
importSync('@ember-data/record-data/-private') as typeof import('@ember-data/record-data/-private')
|
|
1922
|
-
).peekGraph;
|
|
1923
|
-
let graph = peekGraph(this);
|
|
1924
|
-
if (graph) {
|
|
1925
|
-
graph.identifiers.clear();
|
|
1926
|
-
}
|
|
1927
|
-
}
|
|
1928
|
-
this._notificationManager.destroy();
|
|
1929
|
-
|
|
1930
|
-
this.recordArrayManager.clear();
|
|
1931
|
-
this._instanceCache.clear();
|
|
1932
|
-
} else {
|
|
1933
|
-
let normalizedModelName = normalizeModelName(modelName);
|
|
1934
|
-
this._instanceCache.clear(normalizedModelName);
|
|
1935
|
-
}
|
|
1936
|
-
});
|
|
1937
|
-
}
|
|
1938
|
-
|
|
1939
|
-
/**
|
|
1940
|
-
Push some data for a given type into the store.
|
|
1941
|
-
|
|
1942
|
-
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:
|
|
1943
|
-
- record's `type` should always be in singular, dasherized form
|
|
1944
|
-
- members (properties) should be camelCased
|
|
1945
|
-
|
|
1946
|
-
[Your primary data should be wrapped inside `data` property](http://jsonapi.org/format/#document-top-level):
|
|
1947
|
-
|
|
1948
|
-
```js
|
|
1949
|
-
store.push({
|
|
1950
|
-
data: {
|
|
1951
|
-
// primary data for single record of type `Person`
|
|
1952
|
-
id: '1',
|
|
1953
|
-
type: 'person',
|
|
1954
|
-
attributes: {
|
|
1955
|
-
firstName: 'Daniel',
|
|
1956
|
-
lastName: 'Kmak'
|
|
1957
|
-
}
|
|
1958
|
-
}
|
|
1959
|
-
});
|
|
1960
|
-
```
|
|
1961
|
-
|
|
1962
|
-
[Demo.](http://ember-twiddle.com/fb99f18cd3b4d3e2a4c7)
|
|
1963
|
-
|
|
1964
|
-
`data` property can also hold an array (of records):
|
|
1965
|
-
|
|
1966
|
-
```js
|
|
1967
|
-
store.push({
|
|
1968
|
-
data: [
|
|
1969
|
-
// an array of records
|
|
1970
|
-
{
|
|
1971
|
-
id: '1',
|
|
1972
|
-
type: 'person',
|
|
1973
|
-
attributes: {
|
|
1974
|
-
firstName: 'Daniel',
|
|
1975
|
-
lastName: 'Kmak'
|
|
1976
|
-
}
|
|
1977
|
-
},
|
|
1978
|
-
{
|
|
1979
|
-
id: '2',
|
|
1980
|
-
type: 'person',
|
|
1981
|
-
attributes: {
|
|
1982
|
-
firstName: 'Tom',
|
|
1983
|
-
lastName: 'Dale'
|
|
1984
|
-
}
|
|
1985
|
-
}
|
|
1986
|
-
]
|
|
1987
|
-
});
|
|
1988
|
-
```
|
|
1989
|
-
|
|
1990
|
-
[Demo.](http://ember-twiddle.com/69cdbeaa3702159dc355)
|
|
1991
|
-
|
|
1992
|
-
There are some typical properties for `JSONAPI` payload:
|
|
1993
|
-
* `id` - mandatory, unique record's key
|
|
1994
|
-
* `type` - mandatory string which matches `model`'s dasherized name in singular form
|
|
1995
|
-
* `attributes` - object which holds data for record attributes - `attr`'s declared in model
|
|
1996
|
-
* `relationships` - object which must contain any of the following properties under each relationships' respective key (example path is `relationships.achievements.data`):
|
|
1997
|
-
- [`links`](http://jsonapi.org/format/#document-links)
|
|
1998
|
-
- [`data`](http://jsonapi.org/format/#document-resource-object-linkage) - place for primary data
|
|
1999
|
-
- [`meta`](http://jsonapi.org/format/#document-meta) - object which contains meta-information about relationship
|
|
2000
|
-
|
|
2001
|
-
For this model:
|
|
2002
|
-
|
|
2003
|
-
```app/models/person.js
|
|
2004
|
-
import Model, { attr, hasMany } from '@ember-data/model';
|
|
2005
|
-
|
|
2006
|
-
export default class PersonRoute extends Route {
|
|
2007
|
-
@attr('string') firstName;
|
|
2008
|
-
@attr('string') lastName;
|
|
2009
|
-
|
|
2010
|
-
@hasMany('person') children;
|
|
2011
|
-
}
|
|
2012
|
-
```
|
|
2013
|
-
|
|
2014
|
-
To represent the children as IDs:
|
|
2015
|
-
|
|
2016
|
-
```js
|
|
2017
|
-
{
|
|
2018
|
-
data: {
|
|
2019
|
-
id: '1',
|
|
2020
|
-
type: 'person',
|
|
2021
|
-
attributes: {
|
|
2022
|
-
firstName: 'Tom',
|
|
2023
|
-
lastName: 'Dale'
|
|
2024
|
-
},
|
|
2025
|
-
relationships: {
|
|
2026
|
-
children: {
|
|
2027
|
-
data: [
|
|
2028
|
-
{
|
|
2029
|
-
id: '2',
|
|
2030
|
-
type: 'person'
|
|
2031
|
-
},
|
|
2032
|
-
{
|
|
2033
|
-
id: '3',
|
|
2034
|
-
type: 'person'
|
|
2035
|
-
},
|
|
2036
|
-
{
|
|
2037
|
-
id: '4',
|
|
2038
|
-
type: 'person'
|
|
2039
|
-
}
|
|
2040
|
-
]
|
|
2041
|
-
}
|
|
2042
|
-
}
|
|
2043
|
-
}
|
|
2044
|
-
}
|
|
2045
|
-
```
|
|
2046
|
-
|
|
2047
|
-
[Demo.](http://ember-twiddle.com/343e1735e034091f5bde)
|
|
2048
|
-
|
|
2049
|
-
To represent the children relationship as a URL:
|
|
2050
|
-
|
|
2051
|
-
```js
|
|
2052
|
-
{
|
|
2053
|
-
data: {
|
|
2054
|
-
id: '1',
|
|
2055
|
-
type: 'person',
|
|
2056
|
-
attributes: {
|
|
2057
|
-
firstName: 'Tom',
|
|
2058
|
-
lastName: 'Dale'
|
|
2059
|
-
},
|
|
2060
|
-
relationships: {
|
|
2061
|
-
children: {
|
|
2062
|
-
links: {
|
|
2063
|
-
related: '/people/1/children'
|
|
2064
|
-
}
|
|
2065
|
-
}
|
|
2066
|
-
}
|
|
2067
|
-
}
|
|
2068
|
-
}
|
|
2069
|
-
```
|
|
2070
|
-
|
|
2071
|
-
If you're streaming data or implementing an adapter, make sure
|
|
2072
|
-
that you have converted the incoming data into this form. The
|
|
2073
|
-
store's [normalize](../methods/normalize?anchor=normalize) method is a convenience
|
|
2074
|
-
helper for converting a json payload into the form Ember Data
|
|
2075
|
-
expects.
|
|
2076
|
-
|
|
2077
|
-
```js
|
|
2078
|
-
store.push(store.normalize('person', data));
|
|
2079
|
-
```
|
|
2080
|
-
|
|
2081
|
-
This method can be used both to push in brand new
|
|
2082
|
-
records, as well as to update existing records.
|
|
2083
|
-
|
|
2084
|
-
@method push
|
|
2085
|
-
@public
|
|
2086
|
-
@param {Object} data
|
|
2087
|
-
@return the record(s) that was created or
|
|
2088
|
-
updated.
|
|
2089
|
-
*/
|
|
2090
|
-
push(data: EmptyResourceDocument): null;
|
|
2091
|
-
push(data: SingleResourceDocument): RecordInstance;
|
|
2092
|
-
push(data: CollectionResourceDocument): RecordInstance[];
|
|
2093
|
-
push(data: JsonApiDocument): RecordInstance | RecordInstance[] | null {
|
|
2094
|
-
if (DEBUG) {
|
|
2095
|
-
assertDestroyingStore(this, 'push');
|
|
2096
|
-
}
|
|
2097
|
-
let pushed = this._push(data);
|
|
2098
|
-
|
|
2099
|
-
if (Array.isArray(pushed)) {
|
|
2100
|
-
let records = pushed.map((identifier) => this._instanceCache.getRecord(identifier));
|
|
2101
|
-
return records;
|
|
2102
|
-
}
|
|
2103
|
-
|
|
2104
|
-
if (pushed === null) {
|
|
2105
|
-
return null;
|
|
2106
|
-
}
|
|
2107
|
-
|
|
2108
|
-
return this._instanceCache.getRecord(pushed);
|
|
2109
|
-
}
|
|
2110
|
-
|
|
2111
|
-
/**
|
|
2112
|
-
Push some data in the form of a json-api document into the store,
|
|
2113
|
-
without creating materialized records.
|
|
2114
|
-
|
|
2115
|
-
@method _push
|
|
2116
|
-
@private
|
|
2117
|
-
@param {Object} jsonApiDoc
|
|
2118
|
-
@return {StableRecordIdentifier|Array<StableRecordIdentifier>} identifiers for the primary records that had data loaded
|
|
2119
|
-
*/
|
|
2120
|
-
_push(jsonApiDoc): StableExistingRecordIdentifier | StableExistingRecordIdentifier[] | null {
|
|
2121
|
-
if (DEBUG) {
|
|
2122
|
-
assertDestroyingStore(this, '_push');
|
|
2123
|
-
}
|
|
2124
|
-
if (LOG_PAYLOADS) {
|
|
2125
|
-
try {
|
|
2126
|
-
let data = JSON.parse(JSON.stringify(jsonApiDoc));
|
|
2127
|
-
// eslint-disable-next-line no-console
|
|
2128
|
-
console.log('EmberData | Payload - push', data);
|
|
2129
|
-
} catch (e) {
|
|
2130
|
-
// eslint-disable-next-line no-console
|
|
2131
|
-
console.log('EmberData | Payload - push', jsonApiDoc);
|
|
2132
|
-
}
|
|
2133
|
-
}
|
|
2134
|
-
let ret;
|
|
2135
|
-
this._join(() => {
|
|
2136
|
-
let included = jsonApiDoc.included;
|
|
2137
|
-
let i, length;
|
|
2138
|
-
|
|
2139
|
-
if (included) {
|
|
2140
|
-
for (i = 0, length = included.length; i < length; i++) {
|
|
2141
|
-
this._instanceCache.loadData(included[i]);
|
|
2142
|
-
}
|
|
2143
|
-
}
|
|
2144
|
-
|
|
2145
|
-
if (Array.isArray(jsonApiDoc.data)) {
|
|
2146
|
-
length = jsonApiDoc.data.length;
|
|
2147
|
-
let identifiers = new Array(length);
|
|
2148
|
-
|
|
2149
|
-
for (i = 0; i < length; i++) {
|
|
2150
|
-
identifiers[i] = this._instanceCache.loadData(jsonApiDoc.data[i]);
|
|
2151
|
-
}
|
|
2152
|
-
ret = identifiers;
|
|
2153
|
-
return;
|
|
2154
|
-
}
|
|
2155
|
-
|
|
2156
|
-
if (jsonApiDoc.data === null) {
|
|
2157
|
-
ret = null;
|
|
2158
|
-
return;
|
|
2159
|
-
}
|
|
2160
|
-
|
|
2161
|
-
assert(
|
|
2162
|
-
`Expected an object in the 'data' property in a call to 'push' for ${
|
|
2163
|
-
jsonApiDoc.type
|
|
2164
|
-
}, but was ${typeof jsonApiDoc.data}`,
|
|
2165
|
-
typeof jsonApiDoc.data === 'object'
|
|
2166
|
-
);
|
|
2167
|
-
|
|
2168
|
-
ret = this._instanceCache.loadData(jsonApiDoc.data);
|
|
2169
|
-
return;
|
|
2170
|
-
});
|
|
2171
|
-
|
|
2172
|
-
return ret;
|
|
2173
|
-
}
|
|
2174
|
-
|
|
2175
|
-
/**
|
|
2176
|
-
Push some raw data into the store.
|
|
2177
|
-
|
|
2178
|
-
This method can be used both to push in brand new
|
|
2179
|
-
records, as well as to update existing records. You
|
|
2180
|
-
can push in more than one type of object at once.
|
|
2181
|
-
All objects should be in the format expected by the
|
|
2182
|
-
serializer.
|
|
2183
|
-
|
|
2184
|
-
```app/serializers/application.js
|
|
2185
|
-
import RESTSerializer from '@ember-data/serializer/rest';
|
|
2186
|
-
|
|
2187
|
-
export default class ApplicationSerializer extends RESTSerializer;
|
|
2188
|
-
```
|
|
2189
|
-
|
|
2190
|
-
```js
|
|
2191
|
-
let pushData = {
|
|
2192
|
-
posts: [
|
|
2193
|
-
{ id: 1, postTitle: "Great post", commentIds: [2] }
|
|
2194
|
-
],
|
|
2195
|
-
comments: [
|
|
2196
|
-
{ id: 2, commentBody: "Insightful comment" }
|
|
2197
|
-
]
|
|
2198
|
-
}
|
|
2199
|
-
|
|
2200
|
-
store.pushPayload(pushData);
|
|
2201
|
-
```
|
|
2202
|
-
|
|
2203
|
-
By default, the data will be deserialized using a default
|
|
2204
|
-
serializer (the application serializer if it exists).
|
|
2205
|
-
|
|
2206
|
-
Alternatively, `pushPayload` will accept a model type which
|
|
2207
|
-
will determine which serializer will process the payload.
|
|
2208
|
-
|
|
2209
|
-
```app/serializers/application.js
|
|
2210
|
-
import RESTSerializer from '@ember-data/serializer/rest';
|
|
2211
|
-
|
|
2212
|
-
export default class ApplicationSerializer extends RESTSerializer;
|
|
2213
|
-
```
|
|
2214
|
-
|
|
2215
|
-
```app/serializers/post.js
|
|
2216
|
-
import JSONSerializer from '@ember-data/serializer/json';
|
|
2217
|
-
|
|
2218
|
-
export default JSONSerializer;
|
|
2219
|
-
```
|
|
2220
|
-
|
|
2221
|
-
```js
|
|
2222
|
-
store.pushPayload(pushData); // Will use the application serializer
|
|
2223
|
-
store.pushPayload('post', pushData); // Will use the post serializer
|
|
2224
|
-
```
|
|
2225
|
-
|
|
2226
|
-
@method pushPayload
|
|
2227
|
-
@public
|
|
2228
|
-
@param {String} modelName Optionally, a model type used to determine which serializer will be used
|
|
2229
|
-
@param {Object} inputPayload
|
|
2230
|
-
*/
|
|
2231
|
-
// TODO @runspired @deprecate pushPayload in favor of looking up the serializer
|
|
2232
|
-
pushPayload(modelName, inputPayload) {
|
|
2233
|
-
if (DEBUG) {
|
|
2234
|
-
assertDestroyingStore(this, 'pushPayload');
|
|
2235
|
-
}
|
|
2236
|
-
let serializer;
|
|
2237
|
-
let payload;
|
|
2238
|
-
if (!inputPayload) {
|
|
2239
|
-
payload = modelName;
|
|
2240
|
-
serializer = this.serializerFor('application');
|
|
2241
|
-
assert(
|
|
2242
|
-
`You cannot use 'store#pushPayload' without a modelName unless your default serializer defines 'pushPayload'`,
|
|
2243
|
-
typeof serializer.pushPayload === 'function'
|
|
2244
|
-
);
|
|
2245
|
-
} else {
|
|
2246
|
-
payload = inputPayload;
|
|
2247
|
-
assert(
|
|
2248
|
-
`Passing classes to store methods has been removed. Please pass a dasherized string instead of ${modelName}`,
|
|
2249
|
-
typeof modelName === 'string'
|
|
2250
|
-
);
|
|
2251
|
-
let normalizedModelName = normalizeModelName(modelName);
|
|
2252
|
-
serializer = this.serializerFor(normalizedModelName);
|
|
2253
|
-
}
|
|
2254
|
-
assert(
|
|
2255
|
-
`You must define a pushPayload method in your serializer in order to call store.pushPayload`,
|
|
2256
|
-
serializer.pushPayload
|
|
2257
|
-
);
|
|
2258
|
-
serializer.pushPayload(this, payload);
|
|
2259
|
-
}
|
|
2260
|
-
|
|
2261
|
-
// TODO @runspired @deprecate records should implement their own serialization if desired
|
|
2262
|
-
serializeRecord(record: RecordInstance, options?: Dict<unknown>): unknown {
|
|
2263
|
-
// TODO we used to check if the record was destroyed here
|
|
2264
|
-
return this._instanceCache.createSnapshot(recordIdentifierFor(record)).serialize(options);
|
|
2265
|
-
}
|
|
2266
|
-
|
|
2267
|
-
/**
|
|
2268
|
-
* Trigger a save for a Record.
|
|
2269
|
-
*
|
|
2270
|
-
* @method saveRecord
|
|
2271
|
-
* @public
|
|
2272
|
-
* @param {RecordInstance} record
|
|
2273
|
-
* @param options
|
|
2274
|
-
* @returns {Promise<RecordInstance>}
|
|
2275
|
-
*/
|
|
2276
|
-
saveRecord(record: RecordInstance, options: Dict<unknown> = {}): Promise<RecordInstance> {
|
|
2277
|
-
assert(`Unable to initate save for a record in a disconnected state`, storeFor(record));
|
|
2278
|
-
let identifier = recordIdentifierFor(record);
|
|
2279
|
-
let recordData = identifier && this._instanceCache.peek({ identifier, bucket: 'recordData' });
|
|
2280
|
-
|
|
2281
|
-
if (!recordData) {
|
|
2282
|
-
// this commonly means we're disconnected
|
|
2283
|
-
// but just in case we reject here to prevent bad things.
|
|
2284
|
-
return reject(`Record Is Disconnected`);
|
|
2285
|
-
}
|
|
2286
|
-
// TODO we used to check if the record was destroyed here
|
|
2287
|
-
assert(
|
|
2288
|
-
`Cannot initiate a save request for an unloaded record: ${identifier}`,
|
|
2289
|
-
recordData && this._instanceCache.recordIsLoaded(identifier)
|
|
2290
|
-
);
|
|
2291
|
-
if (recordDataIsFullyDeleted(this._instanceCache, identifier)) {
|
|
2292
|
-
return resolve(record);
|
|
2293
|
-
}
|
|
2294
|
-
|
|
2295
|
-
recordData.willCommit(identifier);
|
|
2296
|
-
if (isDSModel(record)) {
|
|
2297
|
-
record.errors.clear();
|
|
2298
|
-
}
|
|
2299
|
-
|
|
2300
|
-
if (!options) {
|
|
2301
|
-
options = {};
|
|
2302
|
-
}
|
|
2303
|
-
let operation: 'createRecord' | 'deleteRecord' | 'updateRecord' = 'updateRecord';
|
|
2304
|
-
|
|
2305
|
-
if (recordData.isNew(identifier)) {
|
|
2306
|
-
operation = 'createRecord';
|
|
2307
|
-
} else if (recordData.isDeleted(identifier)) {
|
|
2308
|
-
operation = 'deleteRecord';
|
|
2309
|
-
}
|
|
2310
|
-
|
|
2311
|
-
const saveOptions = Object.assign({ [SaveOp]: operation }, options);
|
|
2312
|
-
let fetchManagerPromise = this._fetchManager.scheduleSave(identifier, saveOptions);
|
|
2313
|
-
return fetchManagerPromise.then(
|
|
2314
|
-
(payload) => {
|
|
2315
|
-
if (LOG_PAYLOADS) {
|
|
2316
|
-
try {
|
|
2317
|
-
let data = payload ? JSON.parse(JSON.stringify(payload)) : payload;
|
|
2318
|
-
// eslint-disable-next-line no-console
|
|
2319
|
-
console.log(`EmberData | Payload - ${operation}`, data);
|
|
2320
|
-
} catch (e) {
|
|
2321
|
-
// eslint-disable-next-line no-console
|
|
2322
|
-
console.log(`EmberData | Payload - ${operation}`, payload);
|
|
2323
|
-
}
|
|
2324
|
-
}
|
|
2325
|
-
/*
|
|
2326
|
-
// TODO @runspired re-evaluate the below claim now that
|
|
2327
|
-
// the save request pipeline is more streamlined.
|
|
2328
|
-
|
|
2329
|
-
Note to future spelunkers hoping to optimize.
|
|
2330
|
-
We rely on this `run` to create a run loop if needed
|
|
2331
|
-
that `store._push` and `store.saveRecord` will both share.
|
|
2332
|
-
|
|
2333
|
-
We use `join` because it is often the case that we
|
|
2334
|
-
have an outer run loop available still from the first
|
|
2335
|
-
call to `store._push`;
|
|
2336
|
-
*/
|
|
2337
|
-
this._join(() => {
|
|
2338
|
-
if (DEBUG) {
|
|
2339
|
-
assertDestroyingStore(this, 'saveRecord');
|
|
2340
|
-
}
|
|
2341
|
-
|
|
2342
|
-
let data = payload && payload.data;
|
|
2343
|
-
if (!data) {
|
|
2344
|
-
assert(
|
|
2345
|
-
`Your ${identifier.type} record was saved to the server, but the response does not have an id and no id has been set client side. Records must have ids. Please update the server response to provide an id in the response or generate the id on the client side either before saving the record or while normalizing the response.`,
|
|
2346
|
-
identifier.id
|
|
2347
|
-
);
|
|
2348
|
-
}
|
|
2349
|
-
|
|
2350
|
-
const cache = this.identifierCache;
|
|
2351
|
-
let actualIdentifier = identifier;
|
|
2352
|
-
if (operation !== 'deleteRecord' && data) {
|
|
2353
|
-
actualIdentifier = cache.updateRecordIdentifier(identifier, data);
|
|
2354
|
-
}
|
|
2355
|
-
|
|
2356
|
-
//We first make sure the primary data has been updated
|
|
2357
|
-
const recordData = this._instanceCache.getRecordData(actualIdentifier);
|
|
2358
|
-
recordData.didCommit(identifier, data);
|
|
2359
|
-
|
|
2360
|
-
if (operation === 'deleteRecord') {
|
|
2361
|
-
this.recordArrayManager.identifierRemoved(actualIdentifier);
|
|
2362
|
-
}
|
|
2363
|
-
|
|
2364
|
-
if (payload && payload.included) {
|
|
2365
|
-
this._push({ data: null, included: payload.included });
|
|
2366
|
-
}
|
|
2367
|
-
});
|
|
2368
|
-
return record;
|
|
2369
|
-
},
|
|
2370
|
-
(e) => {
|
|
2371
|
-
let err = e;
|
|
2372
|
-
if (!e) {
|
|
2373
|
-
err = new Error(`Unknown Error Occurred During Request`);
|
|
2374
|
-
} else if (typeof e === 'string') {
|
|
2375
|
-
err = new Error(e);
|
|
2376
|
-
}
|
|
2377
|
-
adapterDidInvalidate(this, identifier, err);
|
|
2378
|
-
throw err;
|
|
2379
|
-
}
|
|
2380
|
-
);
|
|
2381
|
-
}
|
|
2382
|
-
|
|
2383
|
-
/**
|
|
2384
|
-
* Instantiation hook allowing applications or addons to configure the store
|
|
2385
|
-
* to utilize a custom RecordData implementation.
|
|
2386
|
-
*
|
|
2387
|
-
* @method createRecordDataFor (hook)
|
|
2388
|
-
* @public
|
|
2389
|
-
* @param identifier
|
|
2390
|
-
* @param storeWrapper
|
|
2391
|
-
*/
|
|
2392
|
-
createRecordDataFor(
|
|
2393
|
-
identifier: StableRecordIdentifier,
|
|
2394
|
-
storeWrapper: RecordDataStoreWrapper
|
|
2395
|
-
): RecordData | RecordDataV1 {
|
|
2396
|
-
if (HAS_RECORD_DATA_PACKAGE) {
|
|
2397
|
-
// we can't greedily use require as this causes
|
|
2398
|
-
// a cycle we can't easily fix (or clearly pin point) at present.
|
|
2399
|
-
//
|
|
2400
|
-
// it can be reproduced in partner tests by running
|
|
2401
|
-
// node ./scripts/packages-for-commit.js && pnpm test-external:ember-observer
|
|
2402
|
-
if (_RecordData === undefined) {
|
|
2403
|
-
_RecordData = (
|
|
2404
|
-
importSync('@ember-data/record-data/-private') as typeof import('@ember-data/record-data/-private')
|
|
2405
|
-
).RecordData;
|
|
2406
|
-
}
|
|
2407
|
-
|
|
2408
|
-
if (DEPRECATE_V1CACHE_STORE_APIS) {
|
|
2409
|
-
if (arguments.length === 4) {
|
|
2410
|
-
deprecate(
|
|
2411
|
-
`Store.createRecordDataFor(<type>, <id>, <lid>, <storeWrapper>) has been deprecated in favor of Store.createRecordDataFor(<identifier>, <storeWrapper>)`,
|
|
2412
|
-
false,
|
|
2413
|
-
{
|
|
2414
|
-
id: 'ember-data:deprecate-v1cache-store-apis',
|
|
2415
|
-
for: 'ember-data',
|
|
2416
|
-
until: '5.0',
|
|
2417
|
-
since: { enabled: '4.7', available: '4.7' },
|
|
2418
|
-
}
|
|
2419
|
-
);
|
|
2420
|
-
identifier = this.identifierCache.getOrCreateRecordIdentifier({
|
|
2421
|
-
type: arguments[0],
|
|
2422
|
-
id: arguments[1],
|
|
2423
|
-
lid: arguments[2],
|
|
2424
|
-
});
|
|
2425
|
-
storeWrapper = arguments[3];
|
|
2426
|
-
}
|
|
2427
|
-
}
|
|
2428
|
-
|
|
2429
|
-
this.__private_singleton_recordData = this.__private_singleton_recordData || new _RecordData(storeWrapper);
|
|
2430
|
-
(
|
|
2431
|
-
this.__private_singleton_recordData as RecordData & { createCache(identifier: StableRecordIdentifier): void }
|
|
2432
|
-
).createCache(identifier);
|
|
2433
|
-
return this.__private_singleton_recordData;
|
|
2434
|
-
}
|
|
2435
|
-
|
|
2436
|
-
assert(`Expected store.createRecordDataFor to be implemented but it wasn't`);
|
|
2437
|
-
}
|
|
2438
|
-
|
|
2439
|
-
/**
|
|
2440
|
-
`normalize` converts a json payload into the normalized form that
|
|
2441
|
-
[push](../methods/push?anchor=push) expects.
|
|
2442
|
-
|
|
2443
|
-
Example
|
|
2444
|
-
|
|
2445
|
-
```js
|
|
2446
|
-
socket.on('message', function(message) {
|
|
2447
|
-
let modelName = message.model;
|
|
2448
|
-
let data = message.data;
|
|
2449
|
-
store.push(store.normalize(modelName, data));
|
|
2450
|
-
});
|
|
2451
|
-
```
|
|
2452
|
-
|
|
2453
|
-
@method normalize
|
|
2454
|
-
@public
|
|
2455
|
-
@param {String} modelName The name of the model type for this payload
|
|
2456
|
-
@param {Object} payload
|
|
2457
|
-
@return {Object} The normalized payload
|
|
2458
|
-
*/
|
|
2459
|
-
// TODO @runspired @deprecate users should call normalize on the associated serializer directly
|
|
2460
|
-
normalize(modelName: string, payload) {
|
|
2461
|
-
if (DEBUG) {
|
|
2462
|
-
assertDestroyingStore(this, 'normalize');
|
|
2463
|
-
}
|
|
2464
|
-
assert(`You need to pass a model name to the store's normalize method`, modelName);
|
|
2465
|
-
assert(
|
|
2466
|
-
`Passing classes to store methods has been removed. Please pass a dasherized string instead of ${typeof modelName}`,
|
|
2467
|
-
typeof modelName === 'string'
|
|
2468
|
-
);
|
|
2469
|
-
let normalizedModelName = normalizeModelName(modelName);
|
|
2470
|
-
let serializer = this.serializerFor(normalizedModelName);
|
|
2471
|
-
let model = this.modelFor(normalizedModelName);
|
|
2472
|
-
assert(
|
|
2473
|
-
`You must define a normalize method in your serializer in order to call store.normalize`,
|
|
2474
|
-
serializer?.normalize
|
|
2475
|
-
);
|
|
2476
|
-
return serializer.normalize(model, payload);
|
|
2477
|
-
}
|
|
2478
|
-
|
|
2479
|
-
/**
|
|
2480
|
-
Returns an instance of the adapter for a given type. For
|
|
2481
|
-
example, `adapterFor('person')` will return an instance of
|
|
2482
|
-
the adapter located at `app/adapters/person.js`
|
|
2483
|
-
|
|
2484
|
-
If no `person` adapter is found, this method will look
|
|
2485
|
-
for an `application` adapter (the default adapter for
|
|
2486
|
-
your entire application).
|
|
2487
|
-
|
|
2488
|
-
@method adapterFor
|
|
2489
|
-
@public
|
|
2490
|
-
@param {String} modelName
|
|
2491
|
-
@return Adapter
|
|
2492
|
-
*/
|
|
2493
|
-
adapterFor(modelName: string) {
|
|
2494
|
-
if (DEBUG) {
|
|
2495
|
-
assertDestroyingStore(this, 'adapterFor');
|
|
2496
|
-
}
|
|
2497
|
-
assert(`You need to pass a model name to the store's adapterFor method`, modelName);
|
|
2498
|
-
assert(
|
|
2499
|
-
`Passing classes to store.adapterFor has been removed. Please pass a dasherized string instead of ${modelName}`,
|
|
2500
|
-
typeof modelName === 'string'
|
|
2501
|
-
);
|
|
2502
|
-
let normalizedModelName = normalizeModelName(modelName);
|
|
2503
|
-
|
|
2504
|
-
let { _adapterCache } = this;
|
|
2505
|
-
let adapter = _adapterCache[normalizedModelName];
|
|
2506
|
-
if (adapter) {
|
|
2507
|
-
return adapter;
|
|
2508
|
-
}
|
|
2509
|
-
|
|
2510
|
-
let owner: any = getOwner(this);
|
|
2511
|
-
|
|
2512
|
-
// name specific adapter
|
|
2513
|
-
adapter = owner.lookup(`adapter:${normalizedModelName}`);
|
|
2514
|
-
if (adapter !== undefined) {
|
|
2515
|
-
_adapterCache[normalizedModelName] = adapter;
|
|
2516
|
-
return adapter;
|
|
2517
|
-
}
|
|
2518
|
-
|
|
2519
|
-
// no adapter found for the specific name, fallback and check for application adapter
|
|
2520
|
-
adapter = _adapterCache.application || owner.lookup('adapter:application');
|
|
2521
|
-
if (adapter !== undefined) {
|
|
2522
|
-
_adapterCache[normalizedModelName] = adapter;
|
|
2523
|
-
_adapterCache.application = adapter;
|
|
2524
|
-
return adapter;
|
|
2525
|
-
}
|
|
2526
|
-
|
|
2527
|
-
if (DEPRECATE_JSON_API_FALLBACK) {
|
|
2528
|
-
// final fallback, no model specific adapter, no application adapter, no
|
|
2529
|
-
// `adapter` property on store: use json-api adapter
|
|
2530
|
-
adapter = _adapterCache['-json-api'] || owner.lookup('adapter:-json-api');
|
|
2531
|
-
if (adapter !== undefined) {
|
|
2532
|
-
deprecate(
|
|
2533
|
-
`Your application is utilizing a deprecated hidden fallback adapter (-json-api). Please implement an application adapter to function as your fallback.`,
|
|
2534
|
-
false,
|
|
2535
|
-
{
|
|
2536
|
-
id: 'ember-data:deprecate-secret-adapter-fallback',
|
|
2537
|
-
for: 'ember-data',
|
|
2538
|
-
until: '5.0',
|
|
2539
|
-
since: { available: '4.5', enabled: '4.5' },
|
|
2540
|
-
}
|
|
2541
|
-
);
|
|
2542
|
-
_adapterCache[normalizedModelName] = adapter;
|
|
2543
|
-
_adapterCache['-json-api'] = adapter;
|
|
2544
|
-
|
|
2545
|
-
return adapter;
|
|
2546
|
-
}
|
|
2547
|
-
}
|
|
2548
|
-
|
|
2549
|
-
assert(`No adapter was found for '${modelName}' and no 'application' adapter was found as a fallback.`);
|
|
2550
|
-
}
|
|
2551
|
-
|
|
2552
|
-
/**
|
|
2553
|
-
Returns an instance of the serializer for a given type. For
|
|
2554
|
-
example, `serializerFor('person')` will return an instance of
|
|
2555
|
-
`App.PersonSerializer`.
|
|
2556
|
-
|
|
2557
|
-
If no `App.PersonSerializer` is found, this method will look
|
|
2558
|
-
for an `App.ApplicationSerializer` (the default serializer for
|
|
2559
|
-
your entire application).
|
|
2560
|
-
|
|
2561
|
-
If a serializer cannot be found on the adapter, it will fall back
|
|
2562
|
-
to an instance of `JSONSerializer`.
|
|
2563
|
-
|
|
2564
|
-
@method serializerFor
|
|
2565
|
-
@public
|
|
2566
|
-
@param {String} modelName the record to serialize
|
|
2567
|
-
@return {Serializer}
|
|
2568
|
-
*/
|
|
2569
|
-
serializerFor(modelName: string): MinimumSerializerInterface | null {
|
|
2570
|
-
if (DEBUG) {
|
|
2571
|
-
assertDestroyingStore(this, 'serializerFor');
|
|
2572
|
-
}
|
|
2573
|
-
assert(`You need to pass a model name to the store's serializerFor method`, modelName);
|
|
2574
|
-
assert(
|
|
2575
|
-
`Passing classes to store.serializerFor has been removed. Please pass a dasherized string instead of ${modelName}`,
|
|
2576
|
-
typeof modelName === 'string'
|
|
2577
|
-
);
|
|
2578
|
-
let normalizedModelName = normalizeModelName(modelName);
|
|
2579
|
-
|
|
2580
|
-
let { _serializerCache } = this;
|
|
2581
|
-
let serializer = _serializerCache[normalizedModelName];
|
|
2582
|
-
if (serializer) {
|
|
2583
|
-
return serializer;
|
|
2584
|
-
}
|
|
2585
|
-
|
|
2586
|
-
let owner: any = getOwner(this);
|
|
2587
|
-
|
|
2588
|
-
// by name
|
|
2589
|
-
serializer = owner.lookup(`serializer:${normalizedModelName}`);
|
|
2590
|
-
if (serializer !== undefined) {
|
|
2591
|
-
_serializerCache[normalizedModelName] = serializer;
|
|
2592
|
-
return serializer;
|
|
2593
|
-
}
|
|
2594
|
-
|
|
2595
|
-
// no serializer found for the specific model, fallback and check for application serializer
|
|
2596
|
-
serializer = _serializerCache.application || owner.lookup('serializer:application');
|
|
2597
|
-
if (serializer !== undefined) {
|
|
2598
|
-
_serializerCache[normalizedModelName] = serializer;
|
|
2599
|
-
_serializerCache.application = serializer;
|
|
2600
|
-
return serializer;
|
|
2601
|
-
}
|
|
2602
|
-
|
|
2603
|
-
return null;
|
|
2604
|
-
}
|
|
2605
|
-
|
|
2606
|
-
destroy() {
|
|
2607
|
-
// enqueue destruction of any adapters/serializers we have created
|
|
2608
|
-
for (let adapterName in this._adapterCache) {
|
|
2609
|
-
let adapter = this._adapterCache[adapterName]!;
|
|
2610
|
-
if (typeof adapter.destroy === 'function') {
|
|
2611
|
-
adapter.destroy();
|
|
2612
|
-
}
|
|
2613
|
-
}
|
|
2614
|
-
|
|
2615
|
-
for (let serializerName in this._serializerCache) {
|
|
2616
|
-
let serializer = this._serializerCache[serializerName]!;
|
|
2617
|
-
if (typeof serializer.destroy === 'function') {
|
|
2618
|
-
serializer.destroy();
|
|
2619
|
-
}
|
|
2620
|
-
}
|
|
2621
|
-
|
|
2622
|
-
if (HAS_RECORD_DATA_PACKAGE) {
|
|
2623
|
-
const peekGraph = (
|
|
2624
|
-
importSync('@ember-data/record-data/-private') as typeof import('@ember-data/record-data/-private')
|
|
2625
|
-
).peekGraph;
|
|
2626
|
-
let graph = peekGraph(this);
|
|
2627
|
-
if (graph) {
|
|
2628
|
-
graph.destroy();
|
|
2629
|
-
}
|
|
2630
|
-
}
|
|
2631
|
-
|
|
2632
|
-
return super.destroy();
|
|
2633
|
-
}
|
|
2634
|
-
|
|
2635
|
-
willDestroy() {
|
|
2636
|
-
super.willDestroy();
|
|
2637
|
-
this.recordArrayManager.destroy();
|
|
2638
|
-
this.identifierCache.destroy();
|
|
2639
|
-
|
|
2640
|
-
this.unloadAll();
|
|
2641
|
-
|
|
2642
|
-
if (DEBUG) {
|
|
2643
|
-
unregisterWaiter(this.__asyncWaiter);
|
|
2644
|
-
let tracked = this._trackedAsyncRequests;
|
|
2645
|
-
let isSettled = tracked.length === 0;
|
|
2646
|
-
|
|
2647
|
-
if (!isSettled) {
|
|
2648
|
-
throw new Error(
|
|
2649
|
-
'Async Request leaks detected. Add a breakpoint here and set `store.generateStackTracesForTrackedRequests = true;`to inspect traces for leak origins:\n\t - ' +
|
|
2650
|
-
tracked.map((o) => o.label).join('\n\t - ')
|
|
2651
|
-
);
|
|
2652
|
-
}
|
|
2653
|
-
}
|
|
2654
|
-
}
|
|
2655
|
-
}
|
|
2656
|
-
|
|
2657
|
-
export default Store;
|
|
2658
|
-
|
|
2659
|
-
let assertDestroyingStore: Function;
|
|
2660
|
-
let assertDestroyedStoreOnly: Function;
|
|
2661
|
-
|
|
2662
|
-
if (DEBUG) {
|
|
2663
|
-
assertDestroyingStore = function assertDestroyedStore(store, method) {
|
|
2664
|
-
assert(
|
|
2665
|
-
`Attempted to call store.${method}(), but the store instance has already been destroyed.`,
|
|
2666
|
-
!(store.isDestroying || store.isDestroyed)
|
|
2667
|
-
);
|
|
2668
|
-
};
|
|
2669
|
-
assertDestroyedStoreOnly = function assertDestroyedStoreOnly(store, method) {
|
|
2670
|
-
assert(
|
|
2671
|
-
`Attempted to call store.${method}(), but the store instance has already been destroyed.`,
|
|
2672
|
-
!store.isDestroyed
|
|
2673
|
-
);
|
|
2674
|
-
};
|
|
2675
|
-
}
|
|
2676
|
-
|
|
2677
|
-
function isMaybeIdentifier(
|
|
2678
|
-
maybeIdentifier: string | ResourceIdentifierObject
|
|
2679
|
-
): maybeIdentifier is ResourceIdentifierObject {
|
|
2680
|
-
return Boolean(
|
|
2681
|
-
maybeIdentifier !== null &&
|
|
2682
|
-
typeof maybeIdentifier === 'object' &&
|
|
2683
|
-
(('id' in maybeIdentifier && 'type' in maybeIdentifier && maybeIdentifier.id && maybeIdentifier.type) ||
|
|
2684
|
-
maybeIdentifier.lid)
|
|
2685
|
-
);
|
|
2686
|
-
}
|
|
2687
|
-
|
|
2688
|
-
export function assertIdentifierHasId(
|
|
2689
|
-
identifier: StableRecordIdentifier
|
|
2690
|
-
): asserts identifier is StableExistingRecordIdentifier {
|
|
2691
|
-
assert(`Attempted to schedule a fetch for a record without an id.`, identifier.id !== null);
|
|
2692
|
-
}
|
|
2693
|
-
|
|
2694
|
-
function isDSModel(record: RecordInstance | null): record is DSModel {
|
|
2695
|
-
return (
|
|
2696
|
-
HAS_MODEL_PACKAGE &&
|
|
2697
|
-
!!record &&
|
|
2698
|
-
'constructor' in record &&
|
|
2699
|
-
'isModel' in record.constructor &&
|
|
2700
|
-
record.constructor.isModel === true
|
|
2701
|
-
);
|
|
2702
|
-
}
|
|
2703
|
-
|
|
2704
|
-
type AdapterErrors = Error & { errors?: unknown[]; isAdapterError?: true; code?: string };
|
|
2705
|
-
type SerializerWithParseErrors = MinimumSerializerInterface & {
|
|
2706
|
-
extractErrors?(store: Store, modelClass: ShimModelClass, error: AdapterErrors, recordId: string | null): any;
|
|
2707
|
-
};
|
|
2708
|
-
|
|
2709
|
-
function adapterDidInvalidate(
|
|
2710
|
-
store: Store,
|
|
2711
|
-
identifier: StableRecordIdentifier,
|
|
2712
|
-
error: Error & { errors?: JsonApiValidationError[]; isAdapterError?: true; code?: string }
|
|
2713
|
-
) {
|
|
2714
|
-
if (error && error.isAdapterError === true && error.code === 'InvalidError') {
|
|
2715
|
-
let serializer = store.serializerFor(identifier.type) as SerializerWithParseErrors;
|
|
2716
|
-
|
|
2717
|
-
// TODO @deprecate extractErrors being called
|
|
2718
|
-
// TODO remove extractErrors from the default serializers.
|
|
2719
|
-
if (serializer && typeof serializer.extractErrors === 'function') {
|
|
2720
|
-
let errorsHash = serializer.extractErrors(store, store.modelFor(identifier.type), error, identifier.id);
|
|
2721
|
-
error.errors = errorsHashToArray(errorsHash);
|
|
2722
|
-
}
|
|
2723
|
-
}
|
|
2724
|
-
const recordData = store._instanceCache.getRecordData(identifier);
|
|
2725
|
-
|
|
2726
|
-
if (error.errors) {
|
|
2727
|
-
assert(
|
|
2728
|
-
`Expected the RecordData implementation for ${identifier} to have a getErrors(identifier) method for retreiving errors.`,
|
|
2729
|
-
typeof recordData.getErrors === 'function'
|
|
2730
|
-
);
|
|
2731
|
-
|
|
2732
|
-
let jsonApiErrors: JsonApiValidationError[] = error.errors;
|
|
2733
|
-
if (jsonApiErrors.length === 0) {
|
|
2734
|
-
jsonApiErrors = [{ title: 'Invalid Error', detail: '', source: { pointer: '/data' } }];
|
|
2735
|
-
}
|
|
2736
|
-
recordData.commitWasRejected(identifier, jsonApiErrors);
|
|
2737
|
-
} else {
|
|
2738
|
-
recordData.commitWasRejected(identifier);
|
|
2739
|
-
}
|
|
2740
|
-
}
|
|
2741
|
-
|
|
2742
|
-
function makeArray(value) {
|
|
2743
|
-
return Array.isArray(value) ? value : [value];
|
|
2744
|
-
}
|
|
2745
|
-
|
|
2746
|
-
const PRIMARY_ATTRIBUTE_KEY = 'base';
|
|
2747
|
-
|
|
2748
|
-
function errorsHashToArray(errors): JsonApiValidationError[] {
|
|
2749
|
-
const out: JsonApiValidationError[] = [];
|
|
2750
|
-
|
|
2751
|
-
if (errors) {
|
|
2752
|
-
Object.keys(errors).forEach((key) => {
|
|
2753
|
-
let messages = makeArray(errors[key]);
|
|
2754
|
-
for (let i = 0; i < messages.length; i++) {
|
|
2755
|
-
let title = 'Invalid Attribute';
|
|
2756
|
-
let pointer = `/data/attributes/${key}`;
|
|
2757
|
-
if (key === PRIMARY_ATTRIBUTE_KEY) {
|
|
2758
|
-
title = 'Invalid Document';
|
|
2759
|
-
pointer = `/data`;
|
|
2760
|
-
}
|
|
2761
|
-
out.push({
|
|
2762
|
-
title: title,
|
|
2763
|
-
detail: messages[i],
|
|
2764
|
-
source: {
|
|
2765
|
-
pointer: pointer,
|
|
2766
|
-
},
|
|
2767
|
-
});
|
|
2768
|
-
}
|
|
2769
|
-
});
|
|
2770
|
-
}
|
|
2771
|
-
|
|
2772
|
-
return out;
|
|
2773
|
-
}
|
|
2774
|
-
|
|
2775
|
-
function normalizeProperties(
|
|
2776
|
-
store: Store,
|
|
2777
|
-
identifier: StableRecordIdentifier,
|
|
2778
|
-
properties?: { [key: string]: unknown },
|
|
2779
|
-
isForV1: boolean = false
|
|
2780
|
-
): { [key: string]: unknown } | undefined {
|
|
2781
|
-
// assert here
|
|
2782
|
-
if (properties !== undefined) {
|
|
2783
|
-
if ('id' in properties) {
|
|
2784
|
-
assert(`expected id to be a string or null`, properties.id !== undefined);
|
|
2785
|
-
}
|
|
2786
|
-
assert(
|
|
2787
|
-
`You passed '${typeof properties}' as properties for record creation instead of an object.`,
|
|
2788
|
-
typeof properties === 'object' && properties !== null
|
|
2789
|
-
);
|
|
2790
|
-
|
|
2791
|
-
const { type } = identifier;
|
|
2792
|
-
|
|
2793
|
-
// convert relationship Records to RecordDatas before passing to RecordData
|
|
2794
|
-
let defs = store.getSchemaDefinitionService().relationshipsDefinitionFor({ type });
|
|
2795
|
-
|
|
2796
|
-
if (defs !== null) {
|
|
2797
|
-
let keys = Object.keys(properties);
|
|
2798
|
-
let relationshipValue;
|
|
2799
|
-
|
|
2800
|
-
for (let i = 0; i < keys.length; i++) {
|
|
2801
|
-
let prop = keys[i];
|
|
2802
|
-
let def = defs[prop];
|
|
2803
|
-
|
|
2804
|
-
if (def !== undefined) {
|
|
2805
|
-
if (def.kind === 'hasMany') {
|
|
2806
|
-
if (DEBUG) {
|
|
2807
|
-
assertRecordsPassedToHasMany(properties[prop] as RecordInstance[]);
|
|
2808
|
-
}
|
|
2809
|
-
relationshipValue = extractIdentifiersFromRecords(properties[prop] as RecordInstance[], isForV1);
|
|
2810
|
-
} else {
|
|
2811
|
-
relationshipValue = extractIdentifierFromRecord(properties[prop] as RecordInstance, isForV1);
|
|
2812
|
-
}
|
|
2813
|
-
|
|
2814
|
-
properties[prop] = relationshipValue;
|
|
2815
|
-
}
|
|
2816
|
-
}
|
|
2817
|
-
}
|
|
2818
|
-
}
|
|
2819
|
-
return properties;
|
|
2820
|
-
}
|
|
2821
|
-
|
|
2822
|
-
function assertRecordsPassedToHasMany(records: RecordInstance[]) {
|
|
2823
|
-
assert(`You must pass an array of records to set a hasMany relationship`, Array.isArray(records));
|
|
2824
|
-
assert(
|
|
2825
|
-
`All elements of a hasMany relationship must be instances of Model, you passed ${records
|
|
2826
|
-
.map((r) => `${typeof r}`)
|
|
2827
|
-
.join(', ')}`,
|
|
2828
|
-
(function () {
|
|
2829
|
-
return records.every((record) => {
|
|
2830
|
-
try {
|
|
2831
|
-
recordIdentifierFor(record);
|
|
2832
|
-
return true;
|
|
2833
|
-
} catch {
|
|
2834
|
-
return false;
|
|
2835
|
-
}
|
|
2836
|
-
});
|
|
2837
|
-
})()
|
|
2838
|
-
);
|
|
2839
|
-
}
|
|
2840
|
-
|
|
2841
|
-
function extractIdentifiersFromRecords(records: RecordInstance[], isForV1: boolean = false): StableRecordIdentifier[] {
|
|
2842
|
-
return records.map((record) => extractIdentifierFromRecord(record, isForV1)) as StableRecordIdentifier[];
|
|
2843
|
-
}
|
|
2844
|
-
|
|
2845
|
-
type PromiseProxyRecord = { then(): void; content: RecordInstance | null | undefined };
|
|
2846
|
-
|
|
2847
|
-
function extractIdentifierFromRecord(
|
|
2848
|
-
recordOrPromiseRecord: PromiseProxyRecord | RecordInstance | null,
|
|
2849
|
-
isForV1: boolean = false
|
|
2850
|
-
) {
|
|
2851
|
-
if (!recordOrPromiseRecord) {
|
|
2852
|
-
return null;
|
|
2853
|
-
}
|
|
2854
|
-
const extract = isForV1 ? recordDataFor : recordIdentifierFor;
|
|
2855
|
-
|
|
2856
|
-
if (DEPRECATE_PROMISE_PROXIES) {
|
|
2857
|
-
if (isPromiseRecord(recordOrPromiseRecord)) {
|
|
2858
|
-
let content = recordOrPromiseRecord.content;
|
|
2859
|
-
assert(
|
|
2860
|
-
'You passed in a promise that did not originate from an EmberData relationship. You can only pass promises that come from a belongsTo or hasMany relationship to the get call.',
|
|
2861
|
-
content !== undefined
|
|
2862
|
-
);
|
|
2863
|
-
deprecate(
|
|
2864
|
-
`You passed in a PromiseProxy to a Relationship API that now expects a resolved value. await the value before setting it.`,
|
|
2865
|
-
false,
|
|
2866
|
-
{
|
|
2867
|
-
id: 'ember-data:deprecate-promise-proxies',
|
|
2868
|
-
until: '5.0',
|
|
2869
|
-
since: {
|
|
2870
|
-
enabled: '4.7',
|
|
2871
|
-
available: '4.7',
|
|
2872
|
-
},
|
|
2873
|
-
for: 'ember-data',
|
|
2874
|
-
}
|
|
2875
|
-
);
|
|
2876
|
-
return content ? extract(content) : null;
|
|
2877
|
-
}
|
|
2878
|
-
}
|
|
2879
|
-
|
|
2880
|
-
return extract(recordOrPromiseRecord);
|
|
2881
|
-
}
|
|
2882
|
-
|
|
2883
|
-
function isPromiseRecord(record: PromiseProxyRecord | RecordInstance): record is PromiseProxyRecord {
|
|
2884
|
-
return !!record.then;
|
|
2885
|
-
}
|
|
2886
|
-
|
|
2887
|
-
function secretInit(
|
|
2888
|
-
record: RecordInstance,
|
|
2889
|
-
recordData: RecordData,
|
|
2890
|
-
identifier: StableRecordIdentifier,
|
|
2891
|
-
store: Store
|
|
2892
|
-
): void {
|
|
2893
|
-
setRecordIdentifier(record, identifier);
|
|
2894
|
-
StoreMap.set(record, store);
|
|
2895
|
-
setRecordDataFor(record, recordData);
|
|
2896
|
-
}
|