@ember-data/model 4.10.0-alpha.2 → 4.10.0-alpha.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.
@@ -749,7 +749,7 @@ function anyUnloaded(store: Store, relationship: ManyRelationship) {
749
749
  return unloaded || false;
750
750
  }
751
751
 
752
- function areAllInverseRecordsLoaded(store: Store, resource: JsonApiRelationship): boolean {
752
+ export function areAllInverseRecordsLoaded(store: Store, resource: JsonApiRelationship): boolean {
753
753
  const instanceCache = store._instanceCache;
754
754
  const identifiers = resource.data;
755
755
 
@@ -2,6 +2,7 @@ import { assert } from '@ember/debug';
2
2
  import { computed } from '@ember/object';
3
3
  import type PromiseProxyMixin from '@ember/object/promise-proxy-mixin';
4
4
  import type ObjectProxy from '@ember/object/proxy';
5
+ import { cached } from '@glimmer/tracking';
5
6
 
6
7
  import type Store from '@ember-data/store';
7
8
  import type { RecordInstance } from '@ember-data/types/q/record-instance';
@@ -9,6 +10,7 @@ import type { Dict } from '@ember-data/types/q/utils';
9
10
 
10
11
  import { LegacySupport } from './legacy-relationships-support';
11
12
  import { PromiseObject } from './promise-proxy-base';
13
+ import type BelongsToReference from './references/belongs-to';
12
14
 
13
15
  export interface BelongsToProxyMeta {
14
16
  key: string;
@@ -45,6 +47,15 @@ const Extended: PromiseObjectType<RecordInstance> = PromiseObject as unknown as
45
47
  */
46
48
  class PromiseBelongsTo extends Extended<RecordInstance> {
47
49
  declare _belongsToState: BelongsToProxyMeta;
50
+
51
+ @cached
52
+ get id() {
53
+ const { key, legacySupport } = this._belongsToState;
54
+ const ref = legacySupport.referenceFor('belongsTo', key) as BelongsToReference;
55
+
56
+ return ref.id();
57
+ }
58
+
48
59
  // we don't proxy meta because we would need to proxy it to the relationship state container
49
60
  // however, meta on relationships does not trigger change notifications.
50
61
  // if you need relationship meta, you should do `record.belongsTo(relationshipName).meta()`
@@ -23,7 +23,7 @@ import type { RecordInstance } from '@ember-data/types/q/record-instance';
23
23
  import type { Dict } from '@ember-data/types/q/utils';
24
24
 
25
25
  import { assertPolymorphicType } from '../debug/assert-polymorphic-type';
26
- import type { LegacySupport } from '../legacy-relationships-support';
26
+ import { areAllInverseRecordsLoaded, LegacySupport } from '../legacy-relationships-support';
27
27
  import { LEGACY_SUPPORT } from '../model';
28
28
 
29
29
  /**
@@ -112,7 +112,6 @@ export default class BelongsToReference {
112
112
  @cached
113
113
  @dependentKeyCompat
114
114
  get identifier(): StableRecordIdentifier | null {
115
- this._ref; // consume the tracked prop
116
115
  if (this.___relatedToken) {
117
116
  this.store._notificationManager.unsubscribe(this.___relatedToken);
118
117
  this.___relatedToken = null;
@@ -293,6 +292,7 @@ export default class BelongsToReference {
293
292
  }
294
293
 
295
294
  _resource() {
295
+ this._ref; // subscribe
296
296
  return this.store._instanceCache
297
297
  .getRecordData(this.___identifier)
298
298
  .getRelationship(this.___identifier, this.key) as SingleResourceRelationship;
@@ -562,7 +562,11 @@ export default class BelongsToReference {
562
562
  const support: LegacySupport = (LEGACY_SUPPORT as Map<StableRecordIdentifier, LegacySupport>).get(
563
563
  this.___identifier
564
564
  )!;
565
- return support.getBelongsTo(this.key, options);
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);
566
570
  }
567
571
 
568
572
  /**
@@ -28,7 +28,7 @@ import type { FindOptions } from '@ember-data/types/q/store';
28
28
  import type { Dict } from '@ember-data/types/q/utils';
29
29
 
30
30
  import { assertPolymorphicType } from '../debug/assert-polymorphic-type';
31
- import type { LegacySupport } from '../legacy-relationships-support';
31
+ import { areAllInverseRecordsLoaded, LegacySupport } from '../legacy-relationships-support';
32
32
  import { LEGACY_SUPPORT } from '../model';
33
33
 
34
34
  /**
@@ -607,7 +607,11 @@ export default class HasManyReference {
607
607
  const support: LegacySupport = (LEGACY_SUPPORT as Map<StableRecordIdentifier, LegacySupport>).get(
608
608
  this.___identifier
609
609
  )!;
610
- return support.getHasMany(this.key, options) as Promise<ManyArray> | ManyArray; // this cast is necessary because typescript does not work properly with custom thenables;
610
+ const fetchSyncRel =
611
+ !this.hasManyRelationship.definition.isAsync && !areAllInverseRecordsLoaded(this.store, this._resource());
612
+ return fetchSyncRel
613
+ ? (support.reloadHasMany(this.key, options) as Promise<ManyArray>)
614
+ : (support.getHasMany(this.key, options) as Promise<ManyArray> | ManyArray); // this cast is necessary because typescript does not work properly with custom thenables;
611
615
  }
612
616
 
613
617
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ember-data/model",
3
- "version": "4.10.0-alpha.2",
3
+ "version": "4.10.0-alpha.4",
4
4
  "description": "A presentation layer for apps built with @ember-data/store",
5
5
  "keywords": [
6
6
  "ember-addon"
@@ -14,9 +14,9 @@
14
14
  "author": "",
15
15
  "directories": {},
16
16
  "peerDependencies": {
17
- "@ember-data/record-data": "4.10.0-alpha.2",
18
- "@ember-data/store": "4.10.0-alpha.2",
19
- "@ember-data/tracking": "4.10.0-alpha.2",
17
+ "@ember-data/record-data": "4.10.0-alpha.4",
18
+ "@ember-data/store": "4.10.0-alpha.4",
19
+ "@ember-data/tracking": "4.10.0-alpha.4",
20
20
  "@ember/string": "^3.0.0",
21
21
  "ember-inflector": "^4.0.2"
22
22
  },
@@ -34,8 +34,8 @@
34
34
  }
35
35
  },
36
36
  "dependencies": {
37
- "@ember-data/canary-features": "4.10.0-alpha.2",
38
- "@ember-data/private-build-infra": "4.10.0-alpha.2",
37
+ "@ember-data/canary-features": "4.10.0-alpha.4",
38
+ "@ember-data/private-build-infra": "4.10.0-alpha.4",
39
39
  "@ember/edition-utils": "^1.2.0",
40
40
  "@embroider/macros": "^1.10.0",
41
41
  "ember-auto-import": "^2.5.0",
@@ -59,6 +59,6 @@
59
59
  "volta": {
60
60
  "extends": "../../package.json"
61
61
  },
62
- "packageManager": "pnpm@7.15.0",
62
+ "packageManager": "pnpm@7.18.0",
63
63
  "scripts": {}
64
64
  }