@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,34 +0,0 @@
1
- import { assert } from '@ember/debug';
2
-
3
- import type { StableRecordIdentifier } from '@ember-data/types/q/identifier';
4
- import type { RecordData } from '@ember-data/types/q/record-data';
5
- import type { RecordInstance } from '@ember-data/types/q/record-instance';
6
-
7
- /*
8
- * Returns the RecordData instance associated with a given
9
- * Model or Identifier
10
- */
11
-
12
- const RecordDataForIdentifierCache = new Map<StableRecordIdentifier | RecordInstance, RecordData>();
13
-
14
- export function setRecordDataFor(identifier: StableRecordIdentifier | RecordInstance, recordData: RecordData): void {
15
- assert(
16
- `Illegal set of identifier`,
17
- !RecordDataForIdentifierCache.has(identifier) || RecordDataForIdentifierCache.get(identifier) === recordData
18
- );
19
- RecordDataForIdentifierCache.set(identifier, recordData);
20
- }
21
-
22
- export function removeRecordDataFor(identifier: StableRecordIdentifier | RecordInstance): void {
23
- RecordDataForIdentifierCache.delete(identifier);
24
- }
25
-
26
- export default function recordDataFor(instance: StableRecordIdentifier): RecordData | null;
27
- export default function recordDataFor(instance: RecordInstance): RecordData;
28
- export default function recordDataFor(instance: StableRecordIdentifier | RecordInstance): RecordData | null {
29
- if (RecordDataForIdentifierCache.has(instance as StableRecordIdentifier)) {
30
- return RecordDataForIdentifierCache.get(instance as StableRecordIdentifier) as RecordData;
31
- }
32
-
33
- return null;
34
- }
@@ -1,59 +0,0 @@
1
- /**
2
- @module @ember-data/store
3
- */
4
-
5
- import { assert, deprecate } from '@ember/debug';
6
-
7
- import { DEPRECATE_HELPERS } from '@ember-data/private-build-infra/deprecations';
8
-
9
- import _normalize from './utils/normalize-model-name';
10
-
11
- export { default as Store, storeFor } from './store-service';
12
-
13
- export { recordIdentifierFor } from './caches/instance-cache';
14
-
15
- export { default as Snapshot } from './network/snapshot';
16
- export {
17
- setIdentifierGenerationMethod,
18
- setIdentifierUpdateMethod,
19
- setIdentifierForgetMethod,
20
- setIdentifierResetMethod,
21
- isStableIdentifier,
22
- } from './caches/identifier-cache';
23
-
24
- export function normalizeModelName(modelName: string) {
25
- if (DEPRECATE_HELPERS) {
26
- deprecate(
27
- `the helper function normalizeModelName is deprecated. You should use model names that are already normalized, or use string helpers of your own. This function is primarily an alias for dasherize from @ember/string.`,
28
- false,
29
- {
30
- id: 'ember-data:deprecate-normalize-modelname-helper',
31
- for: 'ember-data',
32
- until: '5.0',
33
- since: { available: '4.7', enabled: '4.7' },
34
- }
35
- );
36
- return _normalize(modelName);
37
- }
38
- assert(`normalizeModelName support has been removed`);
39
- }
40
-
41
- // TODO this should be a deprecated helper but we have so much usage of it
42
- // to also eliminate
43
- export { default as coerceId } from './utils/coerce-id';
44
-
45
- export {
46
- default as RecordArray,
47
- default as IdentifierArray,
48
- Collection as AdapterPopulatedRecordArray,
49
- SOURCE,
50
- MUTATE,
51
- IDENTIFIER_ARRAY_TAG,
52
- } from './record-arrays/identifier-array';
53
- export { default as RecordArrayManager, fastPush } from './managers/record-array-manager';
54
-
55
- // // Used by tests
56
- export { default as SnapshotRecordArray } from './network/snapshot-record-array';
57
-
58
- // leaked for private use / test use, should investigate removing
59
- export { default as recordDataFor, removeRecordDataFor } from './caches/record-data-for';
@@ -1,240 +0,0 @@
1
- import { assert } from '@ember/debug';
2
- import { tracked } from '@glimmer/tracking';
3
-
4
- /**
5
- @module @ember-data/store
6
- */
7
- import RSVP, { resolve } from 'rsvp';
8
-
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 { NotificationType } from '../managers/record-notification-manager';
14
- import { unsubscribe } from '../managers/record-notification-manager';
15
- import type Store from '../store-service';
16
-
17
- /**
18
- @module @ember-data/store
19
- */
20
-
21
- /**
22
- A `RecordReference` is a low-level API that allows users and
23
- addon authors to perform meta-operations on a record.
24
-
25
- @class RecordReference
26
- @public
27
- @extends Reference
28
- */
29
- export default class RecordReference {
30
- declare store: Store;
31
- // unsubscribe token given to us by the notification manager
32
- ___token!: Object;
33
- ___identifier: StableRecordIdentifier;
34
-
35
- @tracked _ref = 0;
36
-
37
- constructor(store: Store, identifier: StableRecordIdentifier) {
38
- this.store = store;
39
- this.___identifier = identifier;
40
- this.___token = store._notificationManager.subscribe(
41
- identifier,
42
- (_: StableRecordIdentifier, bucket: NotificationType, notifiedKey?: string) => {
43
- if (bucket === 'identity' || (bucket === 'attributes' && notifiedKey === 'id')) {
44
- this._ref++;
45
- }
46
- }
47
- );
48
- }
49
-
50
- destroy() {
51
- unsubscribe(this.___token);
52
- }
53
-
54
- get type(): string {
55
- return this.identifier().type;
56
- }
57
-
58
- /**
59
- The `id` of the record that this reference refers to.
60
-
61
- Together, the `type` and `id` properties form a composite key for
62
- the identity map.
63
-
64
- Example
65
-
66
- ```javascript
67
- let userRef = store.getReference('user', 1);
68
-
69
- userRef.id(); // '1'
70
- ```
71
-
72
- @method id
73
- @public
74
- @return {String} The id of the record.
75
- */
76
- id() {
77
- this._ref; // consume the tracked prop
78
- return this.___identifier.id;
79
- }
80
-
81
- /**
82
- The `identifier` of the record that this reference refers to.
83
-
84
- Together, the `type` and `id` properties form a composite key for
85
- the identity map.
86
-
87
- Example
88
-
89
- ```javascript
90
- let userRef = store.getReference('user', 1);
91
-
92
- userRef.identifier(); // '1'
93
- ```
94
-
95
- @method identifier
96
- @public
97
- @return {String} The identifier of the record.
98
- */
99
- identifier(): StableRecordIdentifier {
100
- return this.___identifier;
101
- }
102
-
103
- /**
104
- How the reference will be looked up when it is loaded. Currently
105
- this always returns `identity` to signify that a record will be
106
- loaded by its `type` and `id`.
107
-
108
- Example
109
-
110
- ```javascript
111
- const userRef = store.getReference('user', 1);
112
-
113
- userRef.remoteType(); // 'identity'
114
- ```
115
-
116
- @method remoteType
117
- @public
118
- @return {String} 'identity'
119
- */
120
- remoteType(): 'identity' {
121
- return 'identity';
122
- }
123
-
124
- /**
125
- This API allows you to provide a reference with new data. The
126
- simplest usage of this API is similar to `store.push`: you provide a
127
- normalized hash of data and the object represented by the reference
128
- will update.
129
-
130
- If you pass a promise to `push`, Ember Data will not ask the adapter
131
- for the data if another attempt to fetch it is made in the
132
- interim. When the promise resolves, the underlying object is updated
133
- with the new data, and the promise returned by *this function* is resolved
134
- with that object.
135
-
136
- For example, `recordReference.push(promise)` will be resolved with a
137
- record.
138
-
139
- Example
140
-
141
- ```javascript
142
- let userRef = store.getReference('user', 1);
143
-
144
- // provide data for reference
145
- userRef.push({
146
- data: {
147
- id: "1",
148
- type: "user",
149
- attributes: {
150
- username: "@user"
151
- }
152
- }
153
- }).then(function(user) {
154
- userRef.value() === user;
155
- });
156
- ```
157
-
158
- @method push
159
- @public
160
- @param objectOrPromise a JSON:API ResourceDocument or a promise resolving to one
161
- @return a promise for the value (record or relationship)
162
- */
163
- push(objectOrPromise: SingleResourceDocument | Promise<SingleResourceDocument>): RSVP.Promise<RecordInstance> {
164
- // TODO @deprecate pushing unresolved payloads
165
- return resolve(objectOrPromise).then((data) => {
166
- return this.store.push(data);
167
- });
168
- }
169
-
170
- /**
171
- If the entity referred to by the reference is already loaded, it is
172
- present as `reference.value`. Otherwise the value returned by this function
173
- is `null`.
174
-
175
- Example
176
-
177
- ```javascript
178
- let userRef = store.getReference('user', 1);
179
-
180
- userRef.value(); // user
181
- ```
182
-
183
- @method value
184
- @public
185
- @return {Model} the record for this RecordReference
186
- */
187
- value(): RecordInstance | null {
188
- return this.store.peekRecord(this.___identifier);
189
- }
190
-
191
- /**
192
- Triggers a fetch for the backing entity based on its `remoteType`
193
- (see `remoteType` definitions per reference type).
194
-
195
- Example
196
-
197
- ```javascript
198
- let userRef = store.getReference('user', 1);
199
-
200
- // load user (via store.find)
201
- userRef.load().then(...)
202
- ```
203
-
204
- @method load
205
- @public
206
- @return {Promise<record>} the record for this RecordReference
207
- */
208
- load() {
209
- const id = this.id();
210
- if (id !== null) {
211
- return this.store.findRecord(this.type, id);
212
- }
213
- assert(`Unable to fetch record of type ${this.type} without an id`);
214
- }
215
-
216
- /**
217
- Reloads the record if it is already loaded. If the record is not
218
- loaded it will load the record via `store.findRecord`
219
-
220
- Example
221
-
222
- ```javascript
223
- let userRef = store.getReference('user', 1);
224
-
225
- // or trigger a reload
226
- userRef.reload().then(...)
227
- ```
228
-
229
- @method reload
230
- @public
231
- @return {Promise<record>} the record for this RecordReference
232
- */
233
- reload() {
234
- const id = this.id();
235
- if (id !== null) {
236
- return this.store.findRecord(this.type, id, { reload: true });
237
- }
238
- assert(`Unable to fetch record of type ${this.type} without an id`);
239
- }
240
- }
@@ -1,148 +0,0 @@
1
- import { getOwner } from '@ember/application';
2
- import { deprecate } from '@ember/debug';
3
-
4
- import { importSync } from '@embroider/macros';
5
-
6
- import type Model from '@ember-data/model';
7
- import { HAS_MODEL_PACKAGE } from '@ember-data/private-build-infra';
8
- import { DEPRECATE_STRING_ARG_SCHEMAS } from '@ember-data/private-build-infra/deprecations';
9
- import type { RecordIdentifier } from '@ember-data/types/q/identifier';
10
- import type { AttributesSchema, RelationshipsSchema } from '@ember-data/types/q/record-data-schemas';
11
-
12
- import type Store from '../store-service';
13
- import normalizeModelName from '../utils/normalize-model-name';
14
-
15
- type ModelForMixin = (store: Store, normalizedModelName: string) => Model | null;
16
-
17
- let _modelForMixin: ModelForMixin;
18
- if (HAS_MODEL_PACKAGE) {
19
- let _found;
20
- _modelForMixin = function () {
21
- if (!_found) {
22
- _found = (importSync('@ember-data/model/-private') as typeof import('@ember-data/model/-private'))._modelForMixin;
23
- }
24
- return _found(...arguments);
25
- };
26
- }
27
-
28
- export class DSModelSchemaDefinitionService {
29
- declare store: Store;
30
- declare _relationshipsDefCache;
31
- declare _attributesDefCache;
32
-
33
- constructor(store: Store) {
34
- this.store = store;
35
- this._relationshipsDefCache = Object.create(null);
36
- this._attributesDefCache = Object.create(null);
37
- }
38
-
39
- // Following the existing RD implementation
40
- attributesDefinitionFor(identifier: RecordIdentifier | { type: string }): AttributesSchema {
41
- let modelName, attributes;
42
- if (DEPRECATE_STRING_ARG_SCHEMAS) {
43
- if (typeof identifier === 'string') {
44
- deprecate(
45
- `attributesDefinitionFor expects either a record identifier or an argument of shape { type: string }, received a string.`,
46
- false,
47
- {
48
- id: 'ember-data:deprecate-string-arg-schemas',
49
- for: 'ember-data',
50
- until: '5.0',
51
- since: { enabled: '4.5', available: '4.5' },
52
- }
53
- );
54
- modelName = identifier;
55
- } else {
56
- modelName = identifier.type;
57
- }
58
- } else {
59
- modelName = identifier.type;
60
- }
61
-
62
- attributes = this._attributesDefCache[modelName];
63
-
64
- if (attributes === undefined) {
65
- let modelClass = this.store.modelFor(modelName);
66
- let attributeMap = modelClass.attributes;
67
-
68
- attributes = Object.create(null);
69
- attributeMap.forEach((meta, name) => (attributes[name] = meta));
70
- this._attributesDefCache[modelName] = attributes;
71
- }
72
-
73
- return attributes;
74
- }
75
-
76
- // Following the existing RD implementation
77
- relationshipsDefinitionFor(identifier: RecordIdentifier | { type: string }): RelationshipsSchema {
78
- let modelName, relationships;
79
- if (DEPRECATE_STRING_ARG_SCHEMAS) {
80
- if (typeof identifier === 'string') {
81
- deprecate(
82
- `relationshipsDefinitionFor expects either a record identifier or an argument of shape { type: string }, received a string.`,
83
- false,
84
- {
85
- id: 'ember-data:deprecate-string-arg-schemas',
86
- for: 'ember-data',
87
- until: '5.0',
88
- since: { enabled: '4.5', available: '4.5' },
89
- }
90
- );
91
- modelName = identifier;
92
- } else {
93
- modelName = identifier.type;
94
- }
95
- } else {
96
- modelName = identifier.type;
97
- }
98
-
99
- relationships = this._relationshipsDefCache[modelName];
100
-
101
- if (relationships === undefined) {
102
- let modelClass = this.store.modelFor(modelName);
103
- relationships = modelClass.relationshipsObject || null;
104
- this._relationshipsDefCache[modelName] = relationships;
105
- }
106
-
107
- return relationships;
108
- }
109
-
110
- doesTypeExist(modelName: string): boolean {
111
- let normalizedModelName = normalizeModelName(modelName);
112
- let factory = getModelFactory(this.store, this.store._modelFactoryCache, normalizedModelName);
113
-
114
- return factory !== null;
115
- }
116
- }
117
-
118
- export function getModelFactory(store: Store, cache, normalizedModelName: string): Model | null {
119
- let factory = cache[normalizedModelName];
120
-
121
- if (!factory) {
122
- let owner: any = getOwner(store);
123
- factory = owner.factoryFor(`model:${normalizedModelName}`);
124
-
125
- if (!factory && HAS_MODEL_PACKAGE) {
126
- //Support looking up mixins as base types for polymorphic relationships
127
- factory = _modelForMixin(store, normalizedModelName);
128
- }
129
-
130
- if (!factory) {
131
- // we don't cache misses in case someone wants to register a missing model
132
- return null;
133
- }
134
-
135
- let klass = factory.class;
136
-
137
- if (klass.isModel) {
138
- let hasOwnModelNameSet = klass.modelName && Object.prototype.hasOwnProperty.call(klass, 'modelName');
139
- if (!hasOwnModelNameSet) {
140
- Object.defineProperty(klass, 'modelName', { value: normalizedModelName });
141
- }
142
- }
143
-
144
- cache[normalizedModelName] = factory;
145
- }
146
-
147
- return factory;
148
- }
@@ -1,92 +0,0 @@
1
- import type { ModelSchema } from '@ember-data/types/q/ds-model';
2
- import type { AttributeSchema, RelationshipSchema } from '@ember-data/types/q/record-data-schemas';
3
- import type { Dict } from '@ember-data/types/q/utils';
4
-
5
- import type Store from '../store-service';
6
-
7
- // if modelFor turns out to be a bottleneck we should replace with a Map
8
- // and clear it during store teardown.
9
- const AvailableShims = new WeakMap<Store, Dict<ShimModelClass>>();
10
-
11
- export function getShimClass(store: Store, modelName: string): ShimModelClass {
12
- let shims = AvailableShims.get(store);
13
-
14
- if (!shims) {
15
- shims = Object.create(null) as Dict<ShimModelClass>;
16
- AvailableShims.set(store, shims);
17
- }
18
-
19
- let shim = shims[modelName];
20
- if (shim === undefined) {
21
- shim = shims[modelName] = new ShimModelClass(store, modelName);
22
- }
23
-
24
- return shim;
25
- }
26
-
27
- function mapFromHash<T>(hash: Dict<T>): Map<string, T> {
28
- let map = new Map();
29
- for (let i in hash) {
30
- if (Object.prototype.hasOwnProperty.call(hash, i)) {
31
- map.set(i, hash[i]);
32
- }
33
- }
34
- return map;
35
- }
36
-
37
- // Mimics the static apis of DSModel
38
- export default class ShimModelClass implements ModelSchema {
39
- declare __store: Store;
40
- declare modelName: string;
41
- constructor(store: Store, modelName: string) {
42
- this.__store = store;
43
- this.modelName = modelName;
44
- }
45
-
46
- get fields(): Map<string, 'attribute' | 'belongsTo' | 'hasMany'> {
47
- let attrs = this.__store.getSchemaDefinitionService().attributesDefinitionFor({ type: this.modelName });
48
- let relationships = this.__store.getSchemaDefinitionService().relationshipsDefinitionFor({ type: this.modelName });
49
- let fields = new Map<string, 'attribute' | 'belongsTo' | 'hasMany'>();
50
- Object.keys(attrs).forEach((key) => fields.set(key, 'attribute'));
51
- Object.keys(relationships).forEach((key) => fields.set(key, relationships[key]!.kind));
52
- return fields;
53
- }
54
-
55
- get attributes(): Map<string, AttributeSchema> {
56
- let attrs = this.__store.getSchemaDefinitionService().attributesDefinitionFor({ type: this.modelName });
57
- return mapFromHash(attrs);
58
- }
59
-
60
- get relationshipsByName(): Map<string, RelationshipSchema> {
61
- let relationships = this.__store.getSchemaDefinitionService().relationshipsDefinitionFor({ type: this.modelName });
62
- return mapFromHash(relationships);
63
- }
64
-
65
- eachAttribute<T>(callback: (this: T | undefined, key: string, attribute: AttributeSchema) => void, binding?: T) {
66
- let attrDefs = this.__store.getSchemaDefinitionService().attributesDefinitionFor({ type: this.modelName });
67
- Object.keys(attrDefs).forEach((key) => {
68
- callback.call(binding, key, attrDefs[key] as AttributeSchema);
69
- });
70
- }
71
-
72
- eachRelationship<T>(
73
- callback: (this: T | undefined, key: string, relationship: RelationshipSchema) => void,
74
- binding?: T
75
- ) {
76
- let relationshipDefs = this.__store
77
- .getSchemaDefinitionService()
78
- .relationshipsDefinitionFor({ type: this.modelName });
79
- Object.keys(relationshipDefs).forEach((key) => {
80
- callback.call(binding, key, relationshipDefs[key] as RelationshipSchema);
81
- });
82
- }
83
-
84
- eachTransformedAttribute<T>(callback: (this: T | undefined, key: string, type: string) => void, binding?: T) {
85
- const attrDefs = this.__store.getSchemaDefinitionService().attributesDefinitionFor({ type: this.modelName });
86
- Object.keys(attrDefs).forEach((key) => {
87
- if (attrDefs[key]!.type) {
88
- callback.call(binding, key, attrDefs[key]!.type);
89
- }
90
- });
91
- }
92
- }