@ember-data/store 4.5.0-beta.0 → 4.5.0

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 (64) hide show
  1. package/addon/-private/{system/backburner.js → backburner.js} +0 -0
  2. package/addon/-private/{system/coerce-id.ts → coerce-id.ts} +0 -0
  3. package/addon/-private/{system/store/common.js → common.js} +0 -0
  4. package/addon/-private/{system/core-store.ts → core-store.ts} +467 -1253
  5. package/addon/-private/{system/errors-utils.js → errors-utils.js} +7 -6
  6. package/addon/-private/{system/fetch-manager.ts → fetch-manager.ts} +72 -42
  7. package/addon/-private/finders.js +107 -0
  8. package/addon/-private/identifer-debug-consts.ts +3 -0
  9. package/addon/-private/{identifiers/cache.ts → identifier-cache.ts} +26 -14
  10. package/addon/-private/{system/identity-map.ts → identity-map.ts} +2 -1
  11. package/addon/-private/index.ts +17 -17
  12. package/addon/-private/instance-cache.ts +387 -0
  13. package/addon/-private/{system/store/internal-model-factory.ts → internal-model-factory.ts} +25 -19
  14. package/addon/-private/{system/internal-model-map.ts → internal-model-map.ts} +9 -5
  15. package/addon/-private/model/internal-model.ts +602 -0
  16. package/addon/-private/{system/references/record.ts → model/record-reference.ts} +23 -36
  17. package/addon/-private/{system/model → model}/shim-model-class.ts +19 -14
  18. package/addon/-private/{system/normalize-model-name.ts → normalize-model-name.ts} +0 -0
  19. package/addon/-private/{system/promise-proxies.ts → promise-proxies.ts} +12 -5
  20. package/addon/-private/{system/promise-proxy-base.js → promise-proxy-base.js} +0 -0
  21. package/addon/-private/{system/record-array-manager.ts → record-array-manager.ts} +19 -18
  22. package/addon/-private/{system/record-arrays → record-arrays}/adapter-populated-record-array.ts +11 -10
  23. package/addon/-private/{system/record-arrays → record-arrays}/record-array.ts +37 -19
  24. package/addon/-private/record-data-for.ts +39 -0
  25. package/addon/-private/{system/store/record-data-store-wrapper.ts → record-data-store-wrapper.ts} +21 -26
  26. package/addon/-private/{system/record-notification-manager.ts → record-notification-manager.ts} +8 -3
  27. package/addon/-private/{system/request-cache.ts → request-cache.ts} +5 -6
  28. package/addon/-private/{system/schema-definition-service.ts → schema-definition-service.ts} +30 -14
  29. package/addon/-private/{system/store/serializer-response.ts → serializer-response.ts} +7 -6
  30. package/addon/-private/{system/snapshot-record-array.ts → snapshot-record-array.ts} +27 -8
  31. package/addon/-private/{system/snapshot.ts → snapshot.ts} +54 -39
  32. package/addon/-private/utils/construct-resource.ts +7 -3
  33. package/addon/-private/utils/promise-record.ts +9 -18
  34. package/addon/-private/{system/weak-cache.ts → weak-cache.ts} +2 -2
  35. package/addon/index.ts +1 -0
  36. package/package.json +21 -20
  37. package/addon/-private/identifiers/is-stable-identifier.ts +0 -18
  38. package/addon/-private/identifiers/utils/uuid-v4.ts +0 -80
  39. package/addon/-private/system/ds-model-store.ts +0 -136
  40. package/addon/-private/system/model/internal-model.ts +0 -1303
  41. package/addon/-private/system/model/states.js +0 -736
  42. package/addon/-private/system/record-arrays.ts +0 -8
  43. package/addon/-private/system/record-data-for.ts +0 -54
  44. package/addon/-private/system/references/belongs-to.ts +0 -406
  45. package/addon/-private/system/references/has-many.ts +0 -487
  46. package/addon/-private/system/references/reference.ts +0 -205
  47. package/addon/-private/system/references.js +0 -9
  48. package/addon/-private/system/store/finders.js +0 -412
  49. package/addon/-private/ts-interfaces/ds-model.ts +0 -50
  50. package/addon/-private/ts-interfaces/ember-data-json-api.ts +0 -145
  51. package/addon/-private/ts-interfaces/fetch-manager.ts +0 -44
  52. package/addon/-private/ts-interfaces/identifier.ts +0 -246
  53. package/addon/-private/ts-interfaces/minimum-adapter-interface.ts +0 -584
  54. package/addon/-private/ts-interfaces/minimum-serializer-interface.ts +0 -257
  55. package/addon/-private/ts-interfaces/promise-proxies.ts +0 -3
  56. package/addon/-private/ts-interfaces/record-data-json-api.ts +0 -29
  57. package/addon/-private/ts-interfaces/record-data-record-wrapper.ts +0 -46
  58. package/addon/-private/ts-interfaces/record-data-schemas.ts +0 -45
  59. package/addon/-private/ts-interfaces/record-data-store-wrapper.ts +0 -56
  60. package/addon/-private/ts-interfaces/record-data.ts +0 -72
  61. package/addon/-private/ts-interfaces/record-instance.ts +0 -18
  62. package/addon/-private/ts-interfaces/schema-definition-service.ts +0 -12
  63. package/addon/-private/ts-interfaces/store.ts +0 -10
  64. package/addon/-private/ts-interfaces/utils.ts +0 -6
@@ -1,15 +1,19 @@
1
- import { dependentKeyCompat } from '@ember/object/compat';
2
- import { cached, tracked } from '@glimmer/tracking';
1
+ import { assert } from '@ember/debug';
2
+ import { tracked } from '@glimmer/tracking';
3
3
 
4
+ /**
5
+ @module @ember-data/store
6
+ */
4
7
  import RSVP, { resolve } from 'rsvp';
5
8
 
6
- import type { SingleResourceDocument } from '../../ts-interfaces/ember-data-json-api';
7
- import type { StableRecordIdentifier } from '../../ts-interfaces/identifier';
8
- import type { RecordInstance } from '../../ts-interfaces/record-instance';
9
- import type CoreStore from '../core-store';
10
- import { NotificationType, unsubscribe } from '../record-notification-manager';
11
- import { internalModelFactoryFor } from '../store/internal-model-factory';
12
- import Reference from './reference';
9
+ import type { SingleResourceDocument } from '@ember-data/types/q/ember-data-json-api';
10
+ import type { StableRecordIdentifier } from '@ember-data/types/q/identifier';
11
+ import type { RecordInstance } from '@ember-data/types/q/record-instance';
12
+
13
+ import type Store from '../core-store';
14
+ import type { NotificationType } from '../record-notification-manager';
15
+ import { unsubscribe } from '../record-notification-manager';
16
+
13
17
  /**
14
18
  @module @ember-data/store
15
19
  */
@@ -22,15 +26,14 @@ import Reference from './reference';
22
26
  @public
23
27
  @extends Reference
24
28
  */
25
- export default class RecordReference extends Reference {
29
+ export default class RecordReference {
26
30
  // unsubscribe token given to us by the notification manager
27
31
  #token!: Object;
28
- #identifier;
32
+ #identifier: StableRecordIdentifier;
29
33
 
30
34
  @tracked _ref = 0;
31
35
 
32
- constructor(public store: CoreStore, identifier: StableRecordIdentifier) {
33
- super(store, identifier);
36
+ constructor(public store: Store, identifier: StableRecordIdentifier) {
34
37
  this.#identifier = identifier;
35
38
  this.#token = store._notificationManager.subscribe(
36
39
  identifier,
@@ -46,22 +49,10 @@ export default class RecordReference extends Reference {
46
49
  unsubscribe(this.#token);
47
50
  }
48
51
 
49
- public get type(): string {
52
+ get type(): string {
50
53
  return this.identifier().type;
51
54
  }
52
55
 
53
- @cached
54
- @dependentKeyCompat
55
- private get _id(): string | null {
56
- this._ref; // consume the tracked prop
57
- let identifier = this.identifier();
58
- if (identifier) {
59
- return identifier.id;
60
- }
61
-
62
- return null;
63
- }
64
-
65
56
  /**
66
57
  The `id` of the record that this reference refers to.
67
58
 
@@ -81,7 +72,8 @@ export default class RecordReference extends Reference {
81
72
  @return {String} The id of the record.
82
73
  */
83
74
  id() {
84
- return this._id;
75
+ this._ref; // consume the tracked prop
76
+ return this.#identifier.id;
85
77
  }
86
78
 
87
79
  /**
@@ -167,6 +159,7 @@ export default class RecordReference extends Reference {
167
159
  @return a promise for the value (record or relationship)
168
160
  */
169
161
  push(objectOrPromise: SingleResourceDocument | Promise<SingleResourceDocument>): RSVP.Promise<RecordInstance> {
162
+ // TODO @deprecate pushing unresolved payloads
170
163
  return resolve(objectOrPromise).then((data) => {
171
164
  return this.store.push(data);
172
165
  });
@@ -190,13 +183,7 @@ export default class RecordReference extends Reference {
190
183
  @return {Model} the record for this RecordReference
191
184
  */
192
185
  value(): RecordInstance | null {
193
- if (this.id() !== null) {
194
- let internalModel = internalModelFactoryFor(this.store).peek(this.#identifier);
195
- if (internalModel && internalModel.currentState.isLoaded) {
196
- return internalModel.getRecord();
197
- }
198
- }
199
- return null;
186
+ return this.store.peekRecord(this.#identifier);
200
187
  }
201
188
 
202
189
  /**
@@ -221,7 +208,7 @@ export default class RecordReference extends Reference {
221
208
  if (id !== null) {
222
209
  return this.store.findRecord(this.type, id);
223
210
  }
224
- throw new Error(`Unable to fetch record of type ${this.type} without an id`);
211
+ assert(`Unable to fetch record of type ${this.type} without an id`);
225
212
  }
226
213
 
227
214
  /**
@@ -246,6 +233,6 @@ export default class RecordReference extends Reference {
246
233
  if (id !== null) {
247
234
  return this.store.findRecord(this.type, id, { reload: true });
248
235
  }
249
- throw new Error(`Unable to fetch record of type ${this.type} without an id`);
236
+ assert(`Unable to fetch record of type ${this.type} without an id`);
250
237
  }
251
238
  }
@@ -1,16 +1,17 @@
1
1
  import { DEBUG } from '@glimmer/env';
2
2
 
3
- import { ModelSchema } from '../../ts-interfaces/ds-model';
4
- import type { AttributeSchema, RelationshipSchema } from '../../ts-interfaces/record-data-schemas';
5
- import type { Dict } from '../../ts-interfaces/utils';
6
- import type CoreStore from '../core-store';
3
+ import type { ModelSchema } from '@ember-data/types/q/ds-model';
4
+ import type { AttributeSchema, RelationshipSchema } from '@ember-data/types/q/record-data-schemas';
5
+ import type { Dict } from '@ember-data/types/q/utils';
6
+
7
+ import type Store from '../core-store';
7
8
  import WeakCache from '../weak-cache';
8
9
 
9
- const AvailableShims = new WeakCache<CoreStore, Dict<ShimModelClass>>(DEBUG ? 'schema-shims' : '');
10
+ const AvailableShims = new WeakCache<Store, Dict<ShimModelClass>>(DEBUG ? 'schema-shims' : '');
10
11
  AvailableShims._generator = () => {
11
12
  return Object.create(null) as Dict<ShimModelClass>;
12
13
  };
13
- export function getShimClass(store: CoreStore, modelName: string): ShimModelClass {
14
+ export function getShimClass(store: Store, modelName: string): ShimModelClass {
14
15
  let shims = AvailableShims.lookup(store);
15
16
  let shim = shims[modelName];
16
17
  if (shim === undefined) {
@@ -33,11 +34,11 @@ function mapFromHash<T>(hash: Dict<T>): Map<string, T> {
33
34
  // Mimics the static apis of DSModel
34
35
  export default class ShimModelClass implements ModelSchema {
35
36
  // TODO Maybe expose the class here?
36
- constructor(private __store: CoreStore, public modelName: string) {}
37
+ constructor(private __store: Store, public modelName: string) {}
37
38
 
38
39
  get fields(): Map<string, 'attribute' | 'belongsTo' | 'hasMany'> {
39
- let attrs = this.__store._attributesDefinitionFor({ type: this.modelName });
40
- let relationships = this.__store._relationshipsDefinitionFor({ type: this.modelName });
40
+ let attrs = this.__store.getSchemaDefinitionService().attributesDefinitionFor({ type: this.modelName });
41
+ let relationships = this.__store.getSchemaDefinitionService().relationshipsDefinitionFor({ type: this.modelName });
41
42
  let fields = new Map<string, 'attribute' | 'belongsTo' | 'hasMany'>();
42
43
  Object.keys(attrs).forEach((key) => fields.set(key, 'attribute'));
43
44
  Object.keys(relationships).forEach((key) => fields.set(key, relationships[key]!.kind));
@@ -45,17 +46,17 @@ export default class ShimModelClass implements ModelSchema {
45
46
  }
46
47
 
47
48
  get attributes(): Map<string, AttributeSchema> {
48
- let attrs = this.__store._attributesDefinitionFor({ type: this.modelName });
49
+ let attrs = this.__store.getSchemaDefinitionService().attributesDefinitionFor({ type: this.modelName });
49
50
  return mapFromHash(attrs);
50
51
  }
51
52
 
52
53
  get relationshipsByName(): Map<string, RelationshipSchema> {
53
- let relationships = this.__store._relationshipsDefinitionFor({ type: this.modelName });
54
+ let relationships = this.__store.getSchemaDefinitionService().relationshipsDefinitionFor({ type: this.modelName });
54
55
  return mapFromHash(relationships);
55
56
  }
56
57
 
57
58
  eachAttribute<T>(callback: (this: T | undefined, key: string, attribute: AttributeSchema) => void, binding?: T) {
58
- let attrDefs = this.__store._attributesDefinitionFor({ type: this.modelName });
59
+ let attrDefs = this.__store.getSchemaDefinitionService().attributesDefinitionFor({ type: this.modelName });
59
60
  Object.keys(attrDefs).forEach((key) => {
60
61
  callback.call(binding, key, attrDefs[key] as AttributeSchema);
61
62
  });
@@ -65,7 +66,9 @@ export default class ShimModelClass implements ModelSchema {
65
66
  callback: (this: T | undefined, key: string, relationship: RelationshipSchema) => void,
66
67
  binding?: T
67
68
  ) {
68
- let relationshipDefs = this.__store._relationshipsDefinitionFor({ type: this.modelName });
69
+ let relationshipDefs = this.__store
70
+ .getSchemaDefinitionService()
71
+ .relationshipsDefinitionFor({ type: this.modelName });
69
72
  Object.keys(relationshipDefs).forEach((key) => {
70
73
  callback.call(binding, key, relationshipDefs[key] as RelationshipSchema);
71
74
  });
@@ -75,7 +78,9 @@ export default class ShimModelClass implements ModelSchema {
75
78
  callback: (this: T | undefined, key: string, relationship: RelationshipSchema) => void,
76
79
  binding?: T
77
80
  ) {
78
- let relationshipDefs = this.__store._relationshipsDefinitionFor({ type: this.modelName });
81
+ let relationshipDefs = this.__store
82
+ .getSchemaDefinitionService()
83
+ .relationshipsDefinitionFor({ type: this.modelName });
79
84
  Object.keys(relationshipDefs).forEach((key) => {
80
85
  if (relationshipDefs[key]!.type) {
81
86
  callback.call(binding, key, relationshipDefs[key] as RelationshipSchema);
@@ -4,7 +4,8 @@ import { reads } from '@ember/object/computed';
4
4
 
5
5
  import { resolve } from 'rsvp';
6
6
 
7
- import type { Dict } from '../ts-interfaces/utils';
7
+ import type { Dict } from '@ember-data/types/q/utils';
8
+
8
9
  import { PromiseArrayProxy, PromiseObjectProxy } from './promise-proxy-base';
9
10
 
10
11
  /**
@@ -105,12 +106,13 @@ export function promiseArray<I, T extends EmberArrayLike<I>>(promise: Promise<T>
105
106
  // constructor is accessed in some internals but not including it in the copyright for the deprecation
106
107
  const ALLOWABLE_METHODS = ['constructor', 'then', 'catch', 'finally'];
107
108
 
108
- export function deprecatedPromiseObject<T>(promise: PromiseObjectProxy<T>): PromiseObjectProxy<T> {
109
+ export function deprecatedPromiseObject<T>(promise: Promise<T>): PromiseObjectProxy<T> {
110
+ const promiseObjectProxy: PromiseObjectProxy<T> = promiseObject(promise);
109
111
  const handler = {
110
112
  get(target: object, prop: string, receiver?: object): unknown {
111
113
  if (!ALLOWABLE_METHODS.includes(prop)) {
112
114
  deprecate(
113
- `Accessing ${prop} is deprecated. Only available methods to access on a promise returned from model.save() are .then, .catch and .finally`,
115
+ `Accessing ${prop} is deprecated. The return type is being changed fomr PromiseObjectProxy to a Promise. The only available methods to access on this promise are .then, .catch and .finally`,
114
116
  false,
115
117
  {
116
118
  id: 'ember-data:model-save-promise',
@@ -124,9 +126,14 @@ export function deprecatedPromiseObject<T>(promise: PromiseObjectProxy<T>): Prom
124
126
  );
125
127
  }
126
128
 
127
- return (Reflect.get(target, prop, receiver) as Function).bind(target);
129
+ const value: unknown = Reflect.get(target, prop, receiver);
130
+ if (value && typeof value === 'function' && typeof value.bind === 'function') {
131
+ return value.bind(target);
132
+ }
133
+
134
+ return value;
128
135
  },
129
136
  };
130
137
 
131
- return new Proxy(promise, handler) as PromiseObjectProxy<T>;
138
+ return new Proxy(promiseObjectProxy, handler) as PromiseObjectProxy<T>;
132
139
  }
@@ -9,12 +9,14 @@ import { _backburner as emberBackburner } from '@ember/runloop';
9
9
  import { DEBUG } from '@glimmer/env';
10
10
 
11
11
  // import isStableIdentifier from '../identifiers/is-stable-identifier';
12
- import type { CollectionResourceDocument, Meta } from '../ts-interfaces/ember-data-json-api';
13
- import type { StableRecordIdentifier } from '../ts-interfaces/identifier';
14
- import type { Dict } from '../ts-interfaces/utils';
15
- import type CoreStore from './core-store';
16
- import { AdapterPopulatedRecordArray, RecordArray } from './record-arrays';
17
- import { internalModelFactoryFor } from './store/internal-model-factory';
12
+ import type { CollectionResourceDocument, Meta } from '@ember-data/types/q/ember-data-json-api';
13
+ import type { StableRecordIdentifier } from '@ember-data/types/q/identifier';
14
+ import type { Dict } from '@ember-data/types/q/utils';
15
+
16
+ import type Store from './core-store';
17
+ import { internalModelFactoryFor } from './internal-model-factory';
18
+ import AdapterPopulatedRecordArray from './record-arrays/adapter-populated-record-array';
19
+ import RecordArray from './record-arrays/record-array';
18
20
  import WeakCache from './weak-cache';
19
21
 
20
22
  const RecordArraysCache = new WeakCache<StableRecordIdentifier, Set<RecordArray>>(DEBUG ? 'record-arrays' : '');
@@ -36,7 +38,7 @@ function getIdentifier(identifier: StableRecordIdentifier): StableRecordIdentifi
36
38
  return identifier;
37
39
  }
38
40
 
39
- function shouldIncludeInRecordArrays(store: CoreStore, identifier: StableRecordIdentifier): boolean {
41
+ function shouldIncludeInRecordArrays(store: Store, identifier: StableRecordIdentifier): boolean {
40
42
  const cache = internalModelFactoryFor(store);
41
43
  const internalModel = cache.peek(identifier);
42
44
 
@@ -51,14 +53,14 @@ function shouldIncludeInRecordArrays(store: CoreStore, identifier: StableRecordI
51
53
  @internal
52
54
  */
53
55
  class RecordArrayManager {
54
- declare store: CoreStore;
56
+ declare store: Store;
55
57
  declare isDestroying: boolean;
56
58
  declare isDestroyed: boolean;
57
59
  declare _liveRecordArrays: Dict<RecordArray>;
58
60
  declare _pendingIdentifiers: Dict<StableRecordIdentifier[]>;
59
61
  declare _adapterPopulatedRecordArrays: RecordArray[];
60
62
 
61
- constructor(options: { store: CoreStore }) {
63
+ constructor(options: { store: Store }) {
62
64
  this.store = options.store;
63
65
  this.isDestroying = false;
64
66
  this.isDestroyed = false;
@@ -164,6 +166,9 @@ class RecordArrayManager {
164
166
  let recordArray = this._liveRecordArrays[modelName];
165
167
  if (recordArray) {
166
168
  set(recordArray, 'isUpdating', false);
169
+ // TODO potentially we should sync here, currently
170
+ // this occurs as a side-effect of individual records updating
171
+ // this._syncLiveRecordArray(recordArray, modelName);
167
172
  }
168
173
  }
169
174
 
@@ -275,8 +280,8 @@ class RecordArrayManager {
275
280
  // TODO this assign kills the root reference but a deep-copy would be required
276
281
  // for both meta and links to actually not be by-ref. We whould likely change
277
282
  // this to a dev-only deep-freeze.
278
- meta: Object.assign({} as Meta, payload!.meta),
279
- links: Object.assign({}, payload!.links),
283
+ meta: Object.assign({} as Meta, payload?.meta),
284
+ links: Object.assign({}, payload?.links),
280
285
  });
281
286
 
282
287
  this._associateWithRecordArray(identifiers, array);
@@ -389,11 +394,7 @@ function removeFromArray(array: RecordArray[], item: RecordArray): boolean {
389
394
  return false;
390
395
  }
391
396
 
392
- function updateLiveRecordArray(
393
- store: CoreStore,
394
- recordArray: RecordArray,
395
- identifiers: StableRecordIdentifier[]
396
- ): void {
397
+ function updateLiveRecordArray(store: Store, recordArray: RecordArray, identifiers: StableRecordIdentifier[]): void {
397
398
  let identifiersToAdd: StableRecordIdentifier[] = [];
398
399
  let identifiersToRemove: StableRecordIdentifier[] = [];
399
400
 
@@ -423,13 +424,13 @@ function updateLiveRecordArray(
423
424
  }
424
425
  }
425
426
 
426
- function removeFromAdapterPopulatedRecordArrays(store: CoreStore, identifiers: StableRecordIdentifier[]): void {
427
+ function removeFromAdapterPopulatedRecordArrays(store: Store, identifiers: StableRecordIdentifier[]): void {
427
428
  for (let i = 0; i < identifiers.length; i++) {
428
429
  removeFromAll(store, identifiers[i]);
429
430
  }
430
431
  }
431
432
 
432
- function removeFromAll(store: CoreStore, identifier: StableRecordIdentifier): void {
433
+ function removeFromAll(store: Store, identifier: StableRecordIdentifier): void {
433
434
  identifier = getIdentifier(identifier);
434
435
  const recordArrays = recordArraysForIdentifier(identifier);
435
436
 
@@ -1,21 +1,22 @@
1
1
  import type NativeArray from '@ember/array/-private/native-array';
2
2
  import { assert } from '@ember/debug';
3
3
 
4
- import type { PromiseArray, RecordArrayManager } from 'ember-data/-private';
5
-
6
- import type { CollectionResourceDocument, Links, Meta, PaginationLinks } from '../../ts-interfaces/ember-data-json-api';
7
- import type { StableRecordIdentifier } from '../../ts-interfaces/identifier';
8
- import type { RecordInstance } from '../../ts-interfaces/record-instance';
9
- import type { FindOptions } from '../../ts-interfaces/store';
10
- import type { Dict } from '../../ts-interfaces/utils';
11
- import type CoreStore from '../core-store';
4
+ import type { CollectionResourceDocument, Links, Meta, PaginationLinks } from '@ember-data/types/q/ember-data-json-api';
5
+ import type { StableRecordIdentifier } from '@ember-data/types/q/identifier';
6
+ import type { RecordInstance } from '@ember-data/types/q/record-instance';
7
+ import type { FindOptions } from '@ember-data/types/q/store';
8
+ import type { Dict } from '@ember-data/types/q/utils';
9
+
10
+ import type Store from '../core-store';
11
+ import type { PromiseArray } from '../promise-proxies';
12
12
  import { promiseArray } from '../promise-proxies';
13
+ import type RecordArrayManager from '../record-array-manager';
13
14
  import SnapshotRecordArray from '../snapshot-record-array';
14
15
  import RecordArray from './record-array';
15
16
 
16
17
  export interface AdapterPopulatedRecordArrayCreateArgs {
17
18
  modelName: string;
18
- store: CoreStore;
19
+ store: Store;
19
20
  manager: RecordArrayManager;
20
21
  content: NativeArray<StableRecordIdentifier>;
21
22
  isLoaded: boolean;
@@ -89,7 +90,7 @@ export default class AdapterPopulatedRecordArray extends RecordArray {
89
90
  const { store, query } = this;
90
91
 
91
92
  // TODO save options from initial request?
92
- return promiseArray(store._query(this.modelName, query, this, {}));
93
+ return promiseArray(store.query(this.modelName, query, { _recordArray: this }));
93
94
  }
94
95
 
95
96
  _setObjects(identifiers: StableRecordIdentifier[], payload: CollectionResourceDocument) {
@@ -3,30 +3,31 @@
3
3
  */
4
4
  import type NativeArray from '@ember/array/-private/native-array';
5
5
  import ArrayProxy from '@ember/array/proxy';
6
- import { assert } from '@ember/debug';
7
- import { computed, get, set } from '@ember/object';
6
+ import { assert, deprecate } from '@ember/debug';
7
+ import { get, set } from '@ember/object';
8
8
  import { tracked } from '@glimmer/tracking';
9
9
 
10
10
  import { Promise } from 'rsvp';
11
11
 
12
12
  import type { RecordArrayManager, Snapshot } from 'ember-data/-private';
13
13
 
14
- import type { StableRecordIdentifier } from '../../ts-interfaces/identifier';
15
- import type { RecordInstance } from '../../ts-interfaces/record-instance';
16
- import type { FindOptions } from '../../ts-interfaces/store';
17
- import type CoreStore from '../core-store';
14
+ import { DEPRECATE_SNAPSHOT_MODEL_CLASS_ACCESS } from '@ember-data/private-build-infra/deprecations';
15
+ import type { StableRecordIdentifier } from '@ember-data/types/q/identifier';
16
+ import type { RecordInstance } from '@ember-data/types/q/record-instance';
17
+ import type { FindOptions } from '@ember-data/types/q/store';
18
+
19
+ import type Store from '../core-store';
18
20
  import type { PromiseArray } from '../promise-proxies';
19
21
  import { promiseArray } from '../promise-proxies';
20
22
  import SnapshotRecordArray from '../snapshot-record-array';
21
- import { internalModelFactoryFor } from '../store/internal-model-factory';
22
23
 
23
- function recordForIdentifier(store: CoreStore, identifier: StableRecordIdentifier): RecordInstance {
24
- return internalModelFactoryFor(store).lookup(identifier).getRecord();
24
+ function recordForIdentifier(store: Store, identifier: StableRecordIdentifier): RecordInstance {
25
+ return store._instanceCache.getRecord(identifier);
25
26
  }
26
27
 
27
28
  export interface RecordArrayCreateArgs {
28
29
  modelName: string;
29
- store: CoreStore;
30
+ store: Store;
30
31
  manager: RecordArrayManager;
31
32
  content: NativeArray<StableRecordIdentifier>;
32
33
  isLoaded: boolean;
@@ -81,7 +82,7 @@ export default class RecordArray extends ArrayProxy<StableRecordIdentifier, Reco
81
82
  @private
82
83
  @type Store
83
84
  */
84
- declare store: CoreStore;
85
+ declare store: Store;
85
86
  declare _updatingPromise: PromiseArray<RecordInstance, RecordArray> | null;
86
87
  declare manager: RecordArrayManager;
87
88
 
@@ -121,15 +122,9 @@ export default class RecordArray extends ArrayProxy<StableRecordIdentifier, Reco
121
122
 
122
123
  @property type
123
124
  @public
125
+ @deprecated
124
126
  @type {subclass of Model}
125
127
  */
126
- @computed('modelName')
127
- get type() {
128
- if (!this.modelName) {
129
- return null;
130
- }
131
- return this.store.modelFor(this.modelName);
132
- }
133
128
 
134
129
  /**
135
130
  Retrieves an object from the content by index.
@@ -295,6 +290,29 @@ export default class RecordArray extends ArrayProxy<StableRecordIdentifier, Reco
295
290
  @internal
296
291
  */
297
292
  _takeSnapshot(): Snapshot[] {
298
- return this.content.map((identifier) => internalModelFactoryFor(this.store).lookup(identifier).createSnapshot());
293
+ return this.content.map((identifier) => this.store._instanceCache.createSnapshot(identifier));
299
294
  }
300
295
  }
296
+
297
+ if (DEPRECATE_SNAPSHOT_MODEL_CLASS_ACCESS) {
298
+ Object.defineProperty(RecordArray.prototype, 'type', {
299
+ get() {
300
+ deprecate(
301
+ `Using RecordArray.type to access the ModelClass for a record is deprecated. Use store.modelFor(<modelName>) instead.`,
302
+ false,
303
+ {
304
+ id: 'ember-data:deprecate-snapshot-model-class-access',
305
+ until: '5.0',
306
+ for: 'ember-data',
307
+ since: { available: '4.5.0', enabled: '4.5.0' },
308
+ }
309
+ );
310
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
311
+ if (!this.modelName) {
312
+ return null;
313
+ }
314
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
315
+ return this.store.modelFor(this.modelName);
316
+ },
317
+ });
318
+ }
@@ -0,0 +1,39 @@
1
+ import { assert } from '@ember/debug';
2
+ import { DEBUG } from '@glimmer/env';
3
+
4
+ import type { StableRecordIdentifier } from '@ember-data/types/q/identifier';
5
+ import type { RecordData } from '@ember-data/types/q/record-data';
6
+ import type { RecordInstance } from '@ember-data/types/q/record-instance';
7
+
8
+ import WeakCache from './weak-cache';
9
+
10
+ /*
11
+ * Returns the RecordData instance associated with a given
12
+ * Model or Identifier
13
+ */
14
+
15
+ const RecordDataForIdentifierCache = new WeakCache<StableRecordIdentifier | RecordInstance, RecordData>(
16
+ DEBUG ? 'recordData' : ''
17
+ );
18
+
19
+ export function setRecordDataFor(identifier: StableRecordIdentifier | RecordInstance, recordData: RecordData): void {
20
+ assert(
21
+ `Illegal set of identifier`,
22
+ !RecordDataForIdentifierCache.has(identifier) || RecordDataForIdentifierCache.get(identifier) === recordData
23
+ );
24
+ RecordDataForIdentifierCache.set(identifier, recordData);
25
+ }
26
+
27
+ export function removeRecordDataFor(identifier: StableRecordIdentifier): void {
28
+ RecordDataForIdentifierCache.delete(identifier);
29
+ }
30
+
31
+ export default function recordDataFor(instance: StableRecordIdentifier): RecordData | null;
32
+ export default function recordDataFor(instance: RecordInstance): RecordData;
33
+ export default function recordDataFor(instance: StableRecordIdentifier | RecordInstance): RecordData | null {
34
+ if (RecordDataForIdentifierCache.has(instance as StableRecordIdentifier)) {
35
+ return RecordDataForIdentifierCache.get(instance as StableRecordIdentifier) as RecordData;
36
+ }
37
+
38
+ return null;
39
+ }
@@ -1,21 +1,20 @@
1
1
  import { importSync } from '@embroider/macros';
2
2
 
3
- import type { RelationshipDefinition } from '@ember-data/model/-private/system/relationships/relationship-meta';
3
+ import type { RelationshipDefinition } from '@ember-data/model/-private/relationship-meta';
4
4
  import { HAS_RECORD_DATA_PACKAGE } from '@ember-data/private-build-infra';
5
-
6
- import type { IdentifierCache } from '../../identifiers/cache';
7
- import type { StableRecordIdentifier } from '../../ts-interfaces/identifier';
8
- import type { RecordData } from '../../ts-interfaces/record-data';
5
+ import type { StableRecordIdentifier } from '@ember-data/types/q/identifier';
6
+ import type { RecordData } from '@ember-data/types/q/record-data';
9
7
  import type {
10
8
  AttributesSchema,
11
9
  RelationshipSchema,
12
10
  RelationshipsSchema,
13
- } from '../../ts-interfaces/record-data-schemas';
14
- import type { RecordDataStoreWrapper as StoreWrapper } from '../../ts-interfaces/record-data-store-wrapper';
15
- import { RecordInstance } from '../../ts-interfaces/record-instance';
16
- import constructResource from '../../utils/construct-resource';
17
- import type CoreStore from '../core-store';
11
+ } from '@ember-data/types/q/record-data-schemas';
12
+ import type { RecordDataStoreWrapper as StoreWrapper } from '@ember-data/types/q/record-data-store-wrapper';
13
+
14
+ import type Store from './core-store';
15
+ import type { IdentifierCache } from './identifier-cache';
18
16
  import { internalModelFactoryFor } from './internal-model-factory';
17
+ import constructResource from './utils/construct-resource';
19
18
 
20
19
  /**
21
20
  @module @ember-data/store
@@ -39,9 +38,9 @@ if (HAS_RECORD_DATA_PACKAGE) {
39
38
  export default class RecordDataStoreWrapper implements StoreWrapper {
40
39
  declare _willNotify: boolean;
41
40
  declare _pendingNotifies: Map<StableRecordIdentifier, Map<string, string>>;
42
- declare _store: CoreStore;
41
+ declare _store: Store;
43
42
 
44
- constructor(_store: CoreStore) {
43
+ constructor(_store: Store) {
45
44
  this._store = _store;
46
45
  this._willNotify = false;
47
46
  this._pendingNotifies = new Map();
@@ -108,11 +107,11 @@ export default class RecordDataStoreWrapper implements StoreWrapper {
108
107
  }
109
108
 
110
109
  attributesDefinitionFor(type: string): AttributesSchema {
111
- return this._store._attributesDefinitionFor({ type });
110
+ return this._store.getSchemaDefinitionService().attributesDefinitionFor({ type });
112
111
  }
113
112
 
114
113
  relationshipsDefinitionFor(type: string): RelationshipsSchema {
115
- return this._store._relationshipsDefinitionFor({ type });
114
+ return this._store.getSchemaDefinitionService().relationshipsDefinitionFor({ type });
116
115
  }
117
116
 
118
117
  inverseForRelationship(type: string, key: string): string | null {
@@ -152,15 +151,15 @@ export default class RecordDataStoreWrapper implements StoreWrapper {
152
151
  }
153
152
  }
154
153
 
155
- notifyPropertyChange(type: string, id: string | null, lid: string, key: string): void;
156
- notifyPropertyChange(type: string, id: string, lid: string | null | undefined, key: string): void;
157
- notifyPropertyChange(type: string, id: string | null, lid: string | null | undefined, key: string): void {
154
+ notifyPropertyChange(type: string, id: string | null, lid: string, key?: string): void;
155
+ notifyPropertyChange(type: string, id: string, lid: string | null | undefined, key?: string): void;
156
+ notifyPropertyChange(type: string, id: string | null, lid: string | null | undefined, key?: string): void {
158
157
  const resource = constructResource(type, id, lid);
159
158
  const identifier = this.identifierCache.getOrCreateRecordIdentifier(resource);
160
159
  let internalModel = internalModelFactoryFor(this._store).peek(identifier);
161
160
 
162
161
  if (internalModel) {
163
- internalModel.notifyPropertyChange(key);
162
+ internalModel.notifyAttributes(key ? [key] : []);
164
163
  }
165
164
  }
166
165
 
@@ -207,11 +206,11 @@ export default class RecordDataStoreWrapper implements StoreWrapper {
207
206
  identifier = this.identifierCache.getOrCreateRecordIdentifier(resource);
208
207
  }
209
208
 
210
- return this._store.recordDataFor(identifier, isCreate);
209
+ return this._store._instanceCache.recordDataFor(identifier, isCreate);
211
210
  }
212
211
 
213
212
  setRecordId(type: string, id: string, lid: string) {
214
- this._store.setRecordId(type, id, lid);
213
+ this._store._instanceCache.setRecordId(type, id, lid);
215
214
  }
216
215
 
217
216
  isRecordInUse(type: string, id: string | null, lid: string): boolean;
@@ -219,14 +218,10 @@ export default class RecordDataStoreWrapper implements StoreWrapper {
219
218
  isRecordInUse(type: string, id: string | null, lid?: string | null): boolean {
220
219
  const resource = constructResource(type, id, lid);
221
220
  const identifier = this.identifierCache.getOrCreateRecordIdentifier(resource);
222
- const internalModel = internalModelFactoryFor(this._store).peek(identifier);
223
221
 
224
- if (!internalModel) {
225
- return false;
226
- }
222
+ const record = this._store._instanceCache.peek({ identifier, bucket: 'record' });
227
223
 
228
- const record = internalModel._record as RecordInstance;
229
- return record && !(record.isDestroyed || record.isDestroying);
224
+ return record ? !(record.isDestroyed || record.isDestroying) : false;
230
225
  }
231
226
 
232
227
  disconnectRecord(type: string, id: string | null, lid: string): void;