@ember-data/store 4.10.0-alpha.4 → 4.10.0-alpha.6

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