@ember-data/store 4.2.0-alpha.8 → 4.2.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.
@@ -1,9 +1,11 @@
1
1
  import { getOwner, setOwner } from '@ember/application';
2
2
  import { assert, deprecate } from '@ember/debug';
3
3
  import EmberError from '@ember/error';
4
+ import { get } from '@ember/object';
4
5
  import { isPresent } from '@ember/utils';
5
6
  import { DEBUG } from '@glimmer/env';
6
7
 
8
+ import { CUSTOM_MODEL_CLASS } from '@ember-data/canary-features';
7
9
  import type DSModelClass from '@ember-data/model';
8
10
 
9
11
  import type { DSModel } from '../ts-interfaces/ds-model';
@@ -67,7 +69,7 @@ class Store extends CoreStore {
67
69
  // for factorFor factory/class split
68
70
  let klass = maybeFactory && maybeFactory.class ? maybeFactory.class : maybeFactory;
69
71
  if (!klass || !klass.isModel) {
70
- if (!this.getSchemaDefinitionService().doesTypeExist(modelName)) {
72
+ if (!CUSTOM_MODEL_CLASS || !this.getSchemaDefinitionService().doesTypeExist(modelName)) {
71
73
  throw new EmberError(`No model was found for '${modelName}' and no schema handles the type`);
72
74
  }
73
75
  return getShimClass(this, modelName);
@@ -101,34 +103,83 @@ class Store extends CoreStore {
101
103
  typeof modelName === 'string'
102
104
  );
103
105
 
104
- return this.getSchemaDefinitionService().doesTypeExist(modelName);
106
+ if (CUSTOM_MODEL_CLASS) {
107
+ return this.getSchemaDefinitionService().doesTypeExist(modelName);
108
+ } else {
109
+ assert(`You need to pass a model name to the store's hasModelFor method`, isPresent(modelName));
110
+ assert(
111
+ `Passing classes to store methods has been removed. Please pass a dasherized string instead of ${modelName}`,
112
+ typeof modelName === 'string'
113
+ );
114
+ let normalizedModelName = normalizeModelName(modelName);
115
+ let factory = getModelFactory(this, this._modelFactoryCache, normalizedModelName);
116
+
117
+ return factory !== null;
118
+ }
105
119
  }
106
120
 
107
121
  _relationshipMetaFor(modelName: string, id: string | null, key: string) {
108
- return this._relationshipsDefinitionFor(modelName)[key];
122
+ if (CUSTOM_MODEL_CLASS) {
123
+ return this._relationshipsDefinitionFor(modelName)[key];
124
+ } else {
125
+ let modelClass = this.modelFor(modelName);
126
+ let relationshipsByName = get(modelClass, 'relationshipsByName');
127
+ return relationshipsByName.get(key);
128
+ }
109
129
  }
110
130
 
111
131
  _attributesDefinitionFor(modelName: string, identifier?: StableRecordIdentifier) {
112
- if (identifier) {
113
- return this.getSchemaDefinitionService().attributesDefinitionFor(identifier);
132
+ if (CUSTOM_MODEL_CLASS) {
133
+ if (identifier) {
134
+ return this.getSchemaDefinitionService().attributesDefinitionFor(identifier);
135
+ } else {
136
+ return this.getSchemaDefinitionService().attributesDefinitionFor(modelName);
137
+ }
114
138
  } else {
115
- return this.getSchemaDefinitionService().attributesDefinitionFor(modelName);
139
+ let attributes = this._attributesDefCache[modelName];
140
+
141
+ if (attributes === undefined) {
142
+ let modelClass = this.modelFor(modelName);
143
+ let attributeMap = get(modelClass, 'attributes');
144
+
145
+ attributes = Object.create(null);
146
+ attributeMap.forEach((meta, name) => (attributes[name] = meta));
147
+ this._attributesDefCache[modelName] = attributes;
148
+ }
149
+
150
+ return attributes;
116
151
  }
117
152
  }
118
153
 
119
154
  _relationshipsDefinitionFor(modelName: string, identifier?: StableRecordIdentifier): RelationshipsSchema {
120
- if (identifier) {
121
- return this.getSchemaDefinitionService().relationshipsDefinitionFor(identifier);
155
+ if (CUSTOM_MODEL_CLASS) {
156
+ if (identifier) {
157
+ return this.getSchemaDefinitionService().relationshipsDefinitionFor(identifier);
158
+ } else {
159
+ return this.getSchemaDefinitionService().relationshipsDefinitionFor(modelName);
160
+ }
122
161
  } else {
123
- return this.getSchemaDefinitionService().relationshipsDefinitionFor(modelName);
162
+ let relationships = this._relationshipsDefCache[modelName];
163
+
164
+ if (relationships === undefined) {
165
+ let modelClass = this.modelFor(modelName);
166
+ relationships = get(modelClass, 'relationshipsObject') || null;
167
+ this._relationshipsDefCache[modelName] = relationships;
168
+ }
169
+
170
+ return relationships;
124
171
  }
125
172
  }
126
173
 
127
174
  getSchemaDefinitionService(): SchemaDefinitionService {
128
- if (!this._schemaDefinitionService) {
129
- this._schemaDefinitionService = new DSModelSchemaDefinitionService(this);
175
+ if (CUSTOM_MODEL_CLASS) {
176
+ if (!this._schemaDefinitionService) {
177
+ this._schemaDefinitionService = new DSModelSchemaDefinitionService(this);
178
+ }
179
+ return this._schemaDefinitionService;
180
+ } else {
181
+ throw 'schema service is only available when custom model class feature flag is on';
130
182
  }
131
- return this._schemaDefinitionService;
132
183
  }
133
184
  }
134
185
 
@@ -501,10 +501,10 @@ export default class FetchManager {
501
501
  }
502
502
  }
503
503
 
504
- getPendingFetch(identifier: StableRecordIdentifier, options) {
505
- let pendingRequests = this.requestCache.getPendingRequestsForRecord(identifier).filter((req) => {
506
- return req.type === 'query' && isSameRequest(options, req.request.data[0].options);
507
- });
504
+ getPendingFetch(identifier: StableRecordIdentifier) {
505
+ let pendingRequests = this.requestCache
506
+ .getPendingRequestsForRecord(identifier)
507
+ .filter((req) => req.type === 'query');
508
508
 
509
509
  if (pendingRequests.length > 0) {
510
510
  return pendingRequests[0][RequestPromise];
@@ -532,8 +532,3 @@ function assertIsString(id: string | null): asserts id is string {
532
532
  }
533
533
  }
534
534
  }
535
-
536
- // this function helps resolve whether we have a pending request that we should use instead
537
- function isSameRequest(options: Dict<unknown> = {}, reqOptions: Dict<unknown> = {}) {
538
- return options.include === reqOptions.include;
539
- }