@ember-data/model 4.10.0-beta.2 → 4.10.0-beta.4

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 (40) hide show
  1. package/addon/{-private/diff-array.ts → -private.js} +41 -13
  2. package/addon/-private.js.map +1 -0
  3. package/addon/has-many-a275fe0d.js +6283 -0
  4. package/addon/has-many-a275fe0d.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 -16
  9. package/addon/-private/attr.js +0 -162
  10. package/addon/-private/belongs-to.js +0 -268
  11. package/addon/-private/debug/assert-polymorphic-type.js +0 -71
  12. package/addon/-private/deprecated-promise-proxy.ts +0 -76
  13. package/addon/-private/errors.ts +0 -425
  14. package/addon/-private/has-many.js +0 -280
  15. package/addon/-private/index.ts +0 -14
  16. package/addon/-private/legacy-data-fetch.js +0 -395
  17. package/addon/-private/legacy-data-utils.ts +0 -92
  18. package/addon/-private/legacy-relationships-support.ts +0 -774
  19. package/addon/-private/many-array.ts +0 -401
  20. package/addon/-private/model-for-mixin.ts +0 -38
  21. package/addon/-private/model.js +0 -2516
  22. package/addon/-private/notify-changes.ts +0 -72
  23. package/addon/-private/promise-belongs-to.ts +0 -84
  24. package/addon/-private/promise-many-array.ts +0 -425
  25. package/addon/-private/promise-proxy-base.js +0 -4
  26. package/addon/-private/record-state.ts +0 -468
  27. package/addon/-private/references/belongs-to.ts +0 -628
  28. package/addon/-private/references/has-many.ts +0 -673
  29. package/addon/-private/relationship-meta.ts +0 -98
  30. package/addon/-private/util.ts +0 -31
  31. package/addon/index.ts +0 -39
  32. package/blueprints/model/HELP.md +0 -26
  33. package/blueprints/model/files/__root__/__path__/__name__.js +0 -5
  34. package/blueprints/model/index.js +0 -158
  35. package/blueprints/model/native-files/__root__/__path__/__name__.js +0 -5
  36. package/blueprints/model-test/index.js +0 -33
  37. package/blueprints/model-test/mocha-files/__root__/__path__/__test__.js +0 -18
  38. package/blueprints/model-test/mocha-rfc-232-files/__root__/__path__/__test__.js +0 -15
  39. package/blueprints/model-test/qunit-files/__root__/__path__/__test__.js +0 -14
  40. package/index.js +0 -47
@@ -1,628 +0,0 @@
1
- import { deprecate } from '@ember/debug';
2
- import { dependentKeyCompat } from '@ember/object/compat';
3
- import { DEBUG } from '@glimmer/env';
4
- import { cached, tracked } from '@glimmer/tracking';
5
-
6
- import type { Object as JSONObject, Value as JSONValue } from 'json-typescript';
7
- import { resolve } from 'rsvp';
8
-
9
- import { DEPRECATE_PROMISE_PROXIES } from '@ember-data/private-build-infra/deprecations';
10
- import type { Graph } from '@ember-data/record-data/-private/graph/graph';
11
- import type BelongsToRelationship from '@ember-data/record-data/-private/relationships/state/belongs-to';
12
- import type Store from '@ember-data/store';
13
- import { recordIdentifierFor } from '@ember-data/store/-private';
14
- import type { NotificationType } from '@ember-data/store/-private/managers/record-notification-manager';
15
- import type {
16
- LinkObject,
17
- Links,
18
- SingleResourceDocument,
19
- SingleResourceRelationship,
20
- } from '@ember-data/types/q/ember-data-json-api';
21
- import type { StableRecordIdentifier } from '@ember-data/types/q/identifier';
22
- import type { RecordInstance } from '@ember-data/types/q/record-instance';
23
- import type { Dict } from '@ember-data/types/q/utils';
24
-
25
- import { assertPolymorphicType } from '../debug/assert-polymorphic-type';
26
- import { areAllInverseRecordsLoaded, LegacySupport } from '../legacy-relationships-support';
27
- import { LEGACY_SUPPORT } from '../model';
28
-
29
- /**
30
- @module @ember-data/model
31
- */
32
-
33
- interface ResourceIdentifier {
34
- links?: {
35
- related?: string | LinkObject;
36
- };
37
- meta?: JSONObject;
38
- }
39
-
40
- function isResourceIdentiferWithRelatedLinks(
41
- value: SingleResourceRelationship | ResourceIdentifier | null
42
- ): value is ResourceIdentifier & { links: { related: string | LinkObject | null } } {
43
- return Boolean(value && value.links && value.links.related);
44
- }
45
-
46
- /**
47
- A `BelongsToReference` is a low-level API that allows users and
48
- addon authors to perform meta-operations on a belongs-to
49
- relationship.
50
-
51
- @class BelongsToReference
52
- @public
53
- */
54
- export default class BelongsToReference {
55
- declare key: string;
56
- declare belongsToRelationship: BelongsToRelationship;
57
- declare type: string;
58
- ___identifier: StableRecordIdentifier;
59
- declare store: Store;
60
- declare graph: Graph;
61
-
62
- // unsubscribe tokens given to us by the notification manager
63
- ___token!: object;
64
- ___relatedToken: object | null = null;
65
-
66
- @tracked _ref = 0;
67
-
68
- constructor(
69
- store: Store,
70
- graph: Graph,
71
- parentIdentifier: StableRecordIdentifier,
72
- belongsToRelationship: BelongsToRelationship,
73
- key: string
74
- ) {
75
- this.graph = graph;
76
- this.key = key;
77
- this.belongsToRelationship = belongsToRelationship;
78
- this.type = belongsToRelationship.definition.type;
79
- this.store = store;
80
- this.___identifier = parentIdentifier;
81
-
82
- this.___token = store._notificationManager.subscribe(
83
- parentIdentifier,
84
- (_: StableRecordIdentifier, bucket: NotificationType, notifiedKey?: string) => {
85
- if (bucket === 'relationships' && notifiedKey === key) {
86
- this._ref++;
87
- }
88
- }
89
- );
90
-
91
- // TODO inverse
92
- }
93
-
94
- destroy() {
95
- // TODO @feature we need the notification manager often enough
96
- // we should potentially just expose it fully public
97
- this.store._notificationManager.unsubscribe(this.___token);
98
- this.___token = null as unknown as object;
99
- if (this.___relatedToken) {
100
- this.store._notificationManager.unsubscribe(this.___relatedToken);
101
- this.___relatedToken = null;
102
- }
103
- }
104
-
105
- /**
106
- * The identifier of the record that this reference refers to.
107
- * `null` if no related record is known.
108
- *
109
- * @property {StableRecordIdentifier | null} identifier
110
- * @public
111
- */
112
- @cached
113
- @dependentKeyCompat
114
- get identifier(): StableRecordIdentifier | null {
115
- if (this.___relatedToken) {
116
- this.store._notificationManager.unsubscribe(this.___relatedToken);
117
- this.___relatedToken = null;
118
- }
119
-
120
- let resource = this._resource();
121
- if (resource && resource.data) {
122
- const identifier = this.store.identifierCache.getOrCreateRecordIdentifier(resource.data);
123
- this.___relatedToken = this.store._notificationManager.subscribe(
124
- identifier,
125
- (_: StableRecordIdentifier, bucket: NotificationType, notifiedKey?: string) => {
126
- if (bucket === 'identity' || (bucket === 'attributes' && notifiedKey === 'id')) {
127
- this._ref++;
128
- }
129
- }
130
- );
131
-
132
- return identifier;
133
- }
134
-
135
- return null;
136
- }
137
-
138
- /**
139
- The `id` of the record that this reference refers to. Together, the
140
- `type()` and `id()` methods form a composite key for the identity
141
- map. This can be used to access the id of an async relationship
142
- without triggering a fetch that would normally happen if you
143
- attempted to use `record.relationship.id`.
144
-
145
- Example
146
-
147
- ```javascript
148
- // models/blog.js
149
- import Model, { belongsTo } from '@ember-data/model';
150
-
151
- export default class BlogModel extends Model {
152
- @belongsTo('user', { async: true, inverse: null }) user;
153
- }
154
-
155
- let blog = store.push({
156
- data: {
157
- type: 'blog',
158
- id: 1,
159
- relationships: {
160
- user: {
161
- data: { type: 'user', id: 1 }
162
- }
163
- }
164
- }
165
- });
166
- let userRef = blog.belongsTo('user');
167
-
168
- // get the identifier of the reference
169
- if (userRef.remoteType() === "id") {
170
- let id = userRef.id();
171
- }
172
- ```
173
-
174
- @method id
175
- @public
176
- @return {String} The id of the record in this belongsTo relationship.
177
- */
178
- id(): string | null {
179
- return this.identifier?.id || null;
180
- }
181
-
182
- /**
183
- The link Ember Data will use to fetch or reload this belongs-to
184
- relationship. By default it uses only the "related" resource linkage.
185
-
186
- Example
187
-
188
- ```javascript
189
- // models/blog.js
190
- import Model, { belongsTo } from '@ember-data/model';
191
- export default Model.extend({
192
- user: belongsTo('user', { async: true, inverse: null })
193
- });
194
-
195
- let blog = store.push({
196
- data: {
197
- type: 'blog',
198
- id: 1,
199
- relationships: {
200
- user: {
201
- links: {
202
- related: '/articles/1/author'
203
- }
204
- }
205
- }
206
- }
207
- });
208
- let userRef = blog.belongsTo('user');
209
-
210
- // get the identifier of the reference
211
- if (userRef.remoteType() === "link") {
212
- let link = userRef.link();
213
- }
214
- ```
215
-
216
- @method link
217
- @public
218
- @return {String} The link Ember Data will use to fetch or reload this belongs-to relationship.
219
- */
220
- link(): string | null {
221
- let resource = this._resource();
222
-
223
- if (isResourceIdentiferWithRelatedLinks(resource)) {
224
- if (resource.links) {
225
- let related = resource.links.related;
226
- return !related || typeof related === 'string' ? related : related.href;
227
- }
228
- }
229
- return null;
230
- }
231
-
232
- /**
233
- * any links that have been received for this relationship
234
- *
235
- * @method links
236
- * @public
237
- * @returns
238
- */
239
- links(): Links | null {
240
- let resource = this._resource();
241
-
242
- return resource && resource.links ? resource.links : null;
243
- }
244
-
245
- /**
246
- The meta data for the belongs-to relationship.
247
-
248
- Example
249
-
250
- ```javascript
251
- // models/blog.js
252
- import Model, { belongsTo } from '@ember-data/model';
253
- export default Model.extend({
254
- user: belongsTo('user', { async: true, inverse: null })
255
- });
256
-
257
- let blog = store.push({
258
- data: {
259
- type: 'blog',
260
- id: 1,
261
- relationships: {
262
- user: {
263
- links: {
264
- related: {
265
- href: '/articles/1/author'
266
- },
267
- },
268
- meta: {
269
- lastUpdated: 1458014400000
270
- }
271
- }
272
- }
273
- }
274
- });
275
-
276
- let userRef = blog.belongsTo('user');
277
-
278
- userRef.meta() // { lastUpdated: 1458014400000 }
279
- ```
280
-
281
- @method meta
282
- @public
283
- @return {Object} The meta information for the belongs-to relationship.
284
- */
285
- meta() {
286
- let meta: Dict<JSONValue> | null = null;
287
- let resource = this._resource();
288
- if (resource && resource.meta && typeof resource.meta === 'object') {
289
- meta = resource.meta;
290
- }
291
- return meta;
292
- }
293
-
294
- _resource() {
295
- this._ref; // subscribe
296
- return this.store._instanceCache
297
- .getRecordData(this.___identifier)
298
- .getRelationship(this.___identifier, this.key) as SingleResourceRelationship;
299
- }
300
-
301
- /**
302
- This returns a string that represents how the reference will be
303
- looked up when it is loaded. If the relationship has a link it will
304
- use the "link" otherwise it defaults to "id".
305
-
306
- Example
307
-
308
- ```app/models/post.js
309
- import Model, { hasMany } from '@ember-data/model';
310
-
311
- export default class PostModel extends Model {
312
- @hasMany('comment', { async: true, inverse: null }) comments;
313
- }
314
- ```
315
-
316
- ```javascript
317
- let post = store.push({
318
- data: {
319
- type: 'post',
320
- id: 1,
321
- relationships: {
322
- comments: {
323
- data: [{ type: 'comment', id: 1 }]
324
- }
325
- }
326
- }
327
- });
328
-
329
- let commentsRef = post.hasMany('comments');
330
-
331
- // get the identifier of the reference
332
- if (commentsRef.remoteType() === "ids") {
333
- let ids = commentsRef.ids();
334
- } else if (commentsRef.remoteType() === "link") {
335
- let link = commentsRef.link();
336
- }
337
- ```
338
-
339
- @method remoteType
340
- @public
341
- @return {String} The name of the remote type. This should either be `link` or `id`
342
- */
343
- remoteType(): 'link' | 'id' {
344
- let value = this._resource();
345
- if (isResourceIdentiferWithRelatedLinks(value)) {
346
- return 'link';
347
- }
348
- return 'id';
349
- }
350
-
351
- /**
352
- `push` can be used to update the data in the relationship and Ember
353
- Data will treat the new data as the canonical value of this
354
- relationship on the backend.
355
-
356
- Example
357
-
358
- ```app/models/blog.js
359
- import Model, { belongsTo } from '@ember-data/model';
360
-
361
- export default class BlogModel extends Model {
362
- @belongsTo('user', { async: true, inverse: null }) user;
363
- }
364
-
365
- let blog = store.push({
366
- data: {
367
- type: 'blog',
368
- id: 1,
369
- relationships: {
370
- user: {
371
- data: { type: 'user', id: 1 }
372
- }
373
- }
374
- }
375
- });
376
- let userRef = blog.belongsTo('user');
377
-
378
- // provide data for reference
379
- userRef.push({
380
- data: {
381
- type: 'user',
382
- id: 1,
383
- attributes: {
384
- username: "@user"
385
- }
386
- }
387
- }).then(function(user) {
388
- userRef.value() === user;
389
- });
390
- ```
391
-
392
- @method push
393
- @public
394
- @param {Object|Promise} objectOrPromise a promise that resolves to a JSONAPI document object describing the new value of this relationship.
395
- @return {Promise<record>} A promise that resolves with the new value in this belongs-to relationship.
396
- */
397
- async push(data: SingleResourceDocument | Promise<SingleResourceDocument>): Promise<RecordInstance> {
398
- let jsonApiDoc: SingleResourceDocument = data as SingleResourceDocument;
399
- if (DEPRECATE_PROMISE_PROXIES) {
400
- if ((data as { then: unknown }).then) {
401
- jsonApiDoc = await resolve(data);
402
- if (jsonApiDoc !== data) {
403
- deprecate(
404
- `You passed in a Promise to a Reference API that now expects a resolved value. await the value before setting it.`,
405
- false,
406
- {
407
- id: 'ember-data:deprecate-promise-proxies',
408
- until: '5.0',
409
- since: {
410
- enabled: '4.7',
411
- available: '4.7',
412
- },
413
- for: 'ember-data',
414
- }
415
- );
416
- }
417
- }
418
- }
419
- let record = this.store.push(jsonApiDoc);
420
-
421
- if (DEBUG) {
422
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
423
- assertPolymorphicType(
424
- this.belongsToRelationship.identifier,
425
- this.belongsToRelationship.definition,
426
- recordIdentifierFor(record),
427
- this.store
428
- );
429
- }
430
-
431
- const { identifier } = this.belongsToRelationship;
432
- this.store._join(() => {
433
- this.graph.push({
434
- op: 'replaceRelatedRecord',
435
- record: identifier,
436
- field: this.key,
437
- value: recordIdentifierFor(record),
438
- });
439
- });
440
-
441
- return record;
442
- }
443
-
444
- /**
445
- `value()` synchronously returns the current value of the belongs-to
446
- relationship. Unlike `record.relationshipName`, calling
447
- `value()` on a reference does not trigger a fetch if the async
448
- relationship is not yet loaded. If the relationship is not loaded
449
- it will always return `null`.
450
-
451
- Example
452
-
453
- ```javascript
454
- // models/blog.js
455
- import Model, { belongsTo } from '@ember-data/model';
456
-
457
- export default class BlogModel extends Model {
458
- @belongsTo('user', { async: true, inverse: null }) user;
459
- }
460
-
461
- let blog = store.push({
462
- data: {
463
- type: 'blog',
464
- id: 1,
465
- relationships: {
466
- user: {
467
- data: { type: 'user', id: 1 }
468
- }
469
- }
470
- }
471
- });
472
- let userRef = blog.belongsTo('user');
473
-
474
- userRef.value(); // null
475
-
476
- // provide data for reference
477
- userRef.push({
478
- data: {
479
- type: 'user',
480
- id: 1,
481
- attributes: {
482
- username: "@user"
483
- }
484
- }
485
- }).then(function(user) {
486
- userRef.value(); // user
487
- });
488
- ```
489
-
490
- @method value
491
- @public
492
- @return {Model} the record in this relationship
493
- */
494
- value(): RecordInstance | null {
495
- let resource = this._resource();
496
- return resource && resource.data ? this.store.peekRecord(resource.data) : null;
497
- }
498
-
499
- /**
500
- Loads a record in a belongs-to relationship if it is not already
501
- loaded. If the relationship is already loaded this method does not
502
- trigger a new load.
503
-
504
- Example
505
-
506
- ```javascript
507
- // models/blog.js
508
- import Model, { belongsTo } from '@ember-data/model';
509
-
510
- export default class BlogModel extends Model {
511
- @belongsTo('user', { async: true, inverse: null }) user;
512
- }
513
-
514
- let blog = store.push({
515
- data: {
516
- type: 'blog',
517
- id: 1,
518
- relationships: {
519
- user: {
520
- data: { type: 'user', id: 1 }
521
- }
522
- }
523
- }
524
- });
525
- let userRef = blog.belongsTo('user');
526
-
527
- userRef.value(); // null
528
-
529
- userRef.load().then(function(user) {
530
- userRef.value() === user
531
- });
532
- ```
533
-
534
- You may also pass in an options object whose properties will be
535
- fed forward. This enables you to pass `adapterOptions` into the
536
- request given to the adapter via the reference.
537
-
538
- Example
539
-
540
- ```javascript
541
- userRef.load({ adapterOptions: { isPrivate: true } }).then(function(user) {
542
- userRef.value() === user;
543
- });
544
- ```
545
- ```app/adapters/user.js
546
- import Adapter from '@ember-data/adapter';
547
-
548
- export default class UserAdapter extends Adapter {
549
- findRecord(store, type, id, snapshot) {
550
- // In the adapter you will have access to adapterOptions.
551
- let adapterOptions = snapshot.adapterOptions;
552
- }
553
- });
554
- ```
555
-
556
- @method load
557
- @public
558
- @param {Object} options the options to pass in.
559
- @return {Promise} a promise that resolves with the record in this belongs-to relationship.
560
- */
561
- load(options?: Dict<unknown>) {
562
- const support: LegacySupport = (LEGACY_SUPPORT as Map<StableRecordIdentifier, LegacySupport>).get(
563
- this.___identifier
564
- )!;
565
- const fetchSyncRel =
566
- !this.belongsToRelationship.definition.isAsync && !areAllInverseRecordsLoaded(this.store, this._resource());
567
- return fetchSyncRel
568
- ? support.reloadBelongsTo(this.key, options).then(() => this.value())
569
- : support.getBelongsTo(this.key, options);
570
- }
571
-
572
- /**
573
- Triggers a reload of the value in this relationship. If the
574
- remoteType is `"link"` Ember Data will use the relationship link to
575
- reload the relationship. Otherwise it will reload the record by its
576
- id.
577
-
578
- Example
579
-
580
- ```javascript
581
- // models/blog.js
582
- import Model, { belongsTo } from '@ember-data/model';
583
-
584
- export default class BlogModel extends Model {
585
- @belongsTo('user', { async: true, inverse: null }) user;
586
- }
587
-
588
- let blog = store.push({
589
- data: {
590
- type: 'blog',
591
- id: 1,
592
- relationships: {
593
- user: {
594
- data: { type: 'user', id: 1 }
595
- }
596
- }
597
- }
598
- });
599
- let userRef = blog.belongsTo('user');
600
-
601
- userRef.reload().then(function(user) {
602
- userRef.value() === user
603
- });
604
- ```
605
-
606
- You may also pass in an options object whose properties will be
607
- fed forward. This enables you to pass `adapterOptions` into the
608
- request given to the adapter via the reference. A full example
609
- can be found in the `load` method.
610
-
611
- Example
612
-
613
- ```javascript
614
- userRef.reload({ adapterOptions: { isPrivate: true } })
615
- ```
616
-
617
- @method reload
618
- @public
619
- @param {Object} options the options to pass in.
620
- @return {Promise} a promise that resolves with the record in this belongs-to relationship after the reload has completed.
621
- */
622
- reload(options?: Dict<unknown>) {
623
- const support: LegacySupport = (LEGACY_SUPPORT as Map<StableRecordIdentifier, LegacySupport>).get(
624
- this.___identifier
625
- )!;
626
- return support.reloadBelongsTo(this.key, options).then(() => this.value());
627
- }
628
- }