@ember-data/model 4.5.0-beta.0 → 4.6.1
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.
- package/addon/-private/attr.js +14 -5
- package/addon/-private/belongs-to.js +9 -5
- package/addon/-private/{system/diff-array.ts → diff-array.ts} +0 -0
- package/addon/-private/errors.ts +7 -69
- package/addon/-private/has-many.js +7 -6
- package/addon/-private/index.ts +6 -5
- package/addon/-private/legacy-data-fetch.js +370 -0
- package/addon/-private/legacy-data-utils.ts +92 -0
- package/addon/-private/legacy-relationships-support.ts +709 -0
- package/addon/-private/{system/many-array.ts → many-array.ts} +38 -33
- package/addon/-private/{system/model-for-mixin.ts → model-for-mixin.ts} +3 -3
- package/addon/-private/model.js +104 -50
- package/addon/-private/notify-changes.ts +21 -13
- package/addon/-private/{system/promise-belongs-to.ts → promise-belongs-to.ts} +9 -8
- package/addon/-private/{system/promise-many-array.ts → promise-many-array.ts} +3 -3
- package/addon/-private/record-state.ts +31 -21
- package/addon/-private/references/belongs-to.ts +585 -0
- package/addon/-private/references/has-many.ts +621 -0
- package/addon/-private/{system/relationships/relationship-meta.ts → relationship-meta.ts} +5 -5
- package/blueprints/model-test/index.js +2 -0
- package/blueprints/model-test/mocha-rfc-232-files/__root__/__path__/__test__.js +1 -1
- package/blueprints/model-test/qunit-files/__root__/__path__/__test__.js +1 -1
- package/index.js +4 -0
- package/package.json +23 -22
|
@@ -0,0 +1,621 @@
|
|
|
1
|
+
import { dependentKeyCompat } from '@ember/object/compat';
|
|
2
|
+
import { DEBUG } from '@glimmer/env';
|
|
3
|
+
import { cached, tracked } from '@glimmer/tracking';
|
|
4
|
+
|
|
5
|
+
import type { Object as JSONObject, Value as JSONValue } from 'json-typescript';
|
|
6
|
+
import { resolve } from 'rsvp';
|
|
7
|
+
|
|
8
|
+
import { ManyArray } from 'ember-data/-private';
|
|
9
|
+
|
|
10
|
+
import type { ManyRelationship } from '@ember-data/record-data/-private';
|
|
11
|
+
import type Store from '@ember-data/store';
|
|
12
|
+
import { recordIdentifierFor } from '@ember-data/store';
|
|
13
|
+
import { assertPolymorphicType } from '@ember-data/store/-debug';
|
|
14
|
+
import type { NotificationType } from '@ember-data/store/-private/record-notification-manager';
|
|
15
|
+
import type { DebugWeakCache } from '@ember-data/store/-private/weak-cache';
|
|
16
|
+
import type {
|
|
17
|
+
CollectionResourceDocument,
|
|
18
|
+
CollectionResourceRelationship,
|
|
19
|
+
ExistingResourceObject,
|
|
20
|
+
LinkObject,
|
|
21
|
+
PaginationLinks,
|
|
22
|
+
SingleResourceDocument,
|
|
23
|
+
} from '@ember-data/types/q/ember-data-json-api';
|
|
24
|
+
import type { StableRecordIdentifier } from '@ember-data/types/q/identifier';
|
|
25
|
+
import type { RecordInstance } from '@ember-data/types/q/record-instance';
|
|
26
|
+
import type { FindOptions } from '@ember-data/types/q/store';
|
|
27
|
+
import type { Dict } from '@ember-data/types/q/utils';
|
|
28
|
+
|
|
29
|
+
import type { LegacySupport } from '../legacy-relationships-support';
|
|
30
|
+
import { LEGACY_SUPPORT } from '../model';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
@module @ember-data/model
|
|
34
|
+
*/
|
|
35
|
+
interface ResourceIdentifier {
|
|
36
|
+
links?: {
|
|
37
|
+
related?: string | LinkObject;
|
|
38
|
+
};
|
|
39
|
+
meta?: JSONObject;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function isResourceIdentiferWithRelatedLinks(
|
|
43
|
+
value: CollectionResourceRelationship | ResourceIdentifier | null
|
|
44
|
+
): value is ResourceIdentifier & { links: { related: string | LinkObject | null } } {
|
|
45
|
+
return Boolean(value && value.links && value.links.related);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
A `HasManyReference` is a low-level API that allows users and addon
|
|
49
|
+
authors to perform meta-operations on a has-many relationship.
|
|
50
|
+
|
|
51
|
+
@class HasManyReference
|
|
52
|
+
@public
|
|
53
|
+
@extends Reference
|
|
54
|
+
*/
|
|
55
|
+
export default class HasManyReference {
|
|
56
|
+
declare key: string;
|
|
57
|
+
declare hasManyRelationship: ManyRelationship;
|
|
58
|
+
declare type: string;
|
|
59
|
+
declare store: Store;
|
|
60
|
+
|
|
61
|
+
// unsubscribe tokens given to us by the notification manager
|
|
62
|
+
#token!: Object;
|
|
63
|
+
#identifier: StableRecordIdentifier;
|
|
64
|
+
#relatedTokenMap!: Map<StableRecordIdentifier, Object>;
|
|
65
|
+
|
|
66
|
+
@tracked _ref = 0;
|
|
67
|
+
|
|
68
|
+
constructor(
|
|
69
|
+
store: Store,
|
|
70
|
+
parentIdentifier: StableRecordIdentifier,
|
|
71
|
+
hasManyRelationship: ManyRelationship,
|
|
72
|
+
key: string
|
|
73
|
+
) {
|
|
74
|
+
this.key = key;
|
|
75
|
+
this.hasManyRelationship = hasManyRelationship;
|
|
76
|
+
this.type = hasManyRelationship.definition.type;
|
|
77
|
+
|
|
78
|
+
this.store = store;
|
|
79
|
+
this.#identifier = parentIdentifier;
|
|
80
|
+
this.#token = store._notificationManager.subscribe(
|
|
81
|
+
parentIdentifier,
|
|
82
|
+
(_: StableRecordIdentifier, bucket: NotificationType, notifiedKey?: string) => {
|
|
83
|
+
if ((bucket === 'relationships' || bucket === 'property') && notifiedKey === key) {
|
|
84
|
+
this._ref++;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
);
|
|
88
|
+
this.#relatedTokenMap = new Map();
|
|
89
|
+
// TODO inverse
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
destroy() {
|
|
93
|
+
this.store._notificationManager.unsubscribe(this.#token);
|
|
94
|
+
this.#relatedTokenMap.forEach((token) => {
|
|
95
|
+
this.store._notificationManager.unsubscribe(token);
|
|
96
|
+
});
|
|
97
|
+
this.#relatedTokenMap.clear();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
@cached
|
|
101
|
+
@dependentKeyCompat
|
|
102
|
+
get _relatedIdentifiers(): StableRecordIdentifier[] {
|
|
103
|
+
this._ref; // consume the tracked prop
|
|
104
|
+
|
|
105
|
+
let resource = this._resource();
|
|
106
|
+
|
|
107
|
+
this.#relatedTokenMap.forEach((token) => {
|
|
108
|
+
this.store._notificationManager.unsubscribe(token);
|
|
109
|
+
});
|
|
110
|
+
this.#relatedTokenMap.clear();
|
|
111
|
+
|
|
112
|
+
if (resource && resource.data) {
|
|
113
|
+
return resource.data.map((resourceIdentifier) => {
|
|
114
|
+
const identifier = this.store.identifierCache.getOrCreateRecordIdentifier(resourceIdentifier);
|
|
115
|
+
const token = this.store._notificationManager.subscribe(
|
|
116
|
+
identifier,
|
|
117
|
+
(_: StableRecordIdentifier, bucket: NotificationType, notifiedKey?: string) => {
|
|
118
|
+
if (bucket === 'identity' || ((bucket === 'attributes' || bucket === 'property') && notifiedKey === 'id')) {
|
|
119
|
+
this._ref++;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
this.#relatedTokenMap.set(identifier, token);
|
|
125
|
+
|
|
126
|
+
return identifier;
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return [];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
_resource() {
|
|
134
|
+
return this.store._instanceCache.recordDataFor(this.#identifier, false).getHasMany(this.key);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
This returns a string that represents how the reference will be
|
|
139
|
+
looked up when it is loaded. If the relationship has a link it will
|
|
140
|
+
use the "link" otherwise it defaults to "id".
|
|
141
|
+
|
|
142
|
+
Example
|
|
143
|
+
|
|
144
|
+
```app/models/post.js
|
|
145
|
+
import Model, { hasMany } from '@ember-data/model';
|
|
146
|
+
|
|
147
|
+
export default class PostModel extends Model {
|
|
148
|
+
@hasMany({ async: true }) comments;
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
```javascript
|
|
153
|
+
let post = store.push({
|
|
154
|
+
data: {
|
|
155
|
+
type: 'post',
|
|
156
|
+
id: 1,
|
|
157
|
+
relationships: {
|
|
158
|
+
comments: {
|
|
159
|
+
data: [{ type: 'comment', id: 1 }]
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
let commentsRef = post.hasMany('comments');
|
|
166
|
+
|
|
167
|
+
// get the identifier of the reference
|
|
168
|
+
if (commentsRef.remoteType() === "ids") {
|
|
169
|
+
let ids = commentsRef.ids();
|
|
170
|
+
} else if (commentsRef.remoteType() === "link") {
|
|
171
|
+
let link = commentsRef.link();
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
@method remoteType
|
|
176
|
+
@public
|
|
177
|
+
@return {String} The name of the remote type. This should either be `link` or `ids`
|
|
178
|
+
*/
|
|
179
|
+
remoteType(): 'link' | 'ids' {
|
|
180
|
+
let value = this._resource();
|
|
181
|
+
if (value && value.links && value.links.related) {
|
|
182
|
+
return 'link';
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return 'ids';
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
`ids()` returns an array of the record IDs in this relationship.
|
|
190
|
+
|
|
191
|
+
Example
|
|
192
|
+
|
|
193
|
+
```app/models/post.js
|
|
194
|
+
import Model, { hasMany } from '@ember-data/model';
|
|
195
|
+
|
|
196
|
+
export default class PostModel extends Model {
|
|
197
|
+
@hasMany({ async: true }) comments;
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
```javascript
|
|
202
|
+
let post = store.push({
|
|
203
|
+
data: {
|
|
204
|
+
type: 'post',
|
|
205
|
+
id: 1,
|
|
206
|
+
relationships: {
|
|
207
|
+
comments: {
|
|
208
|
+
data: [{ type: 'comment', id: 1 }]
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
let commentsRef = post.hasMany('comments');
|
|
215
|
+
|
|
216
|
+
commentsRef.ids(); // ['1']
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
@method ids
|
|
220
|
+
@public
|
|
221
|
+
@return {Array} The ids in this has-many relationship
|
|
222
|
+
*/
|
|
223
|
+
ids(): Array<string | null> {
|
|
224
|
+
return this._relatedIdentifiers.map((identifier) => identifier.id);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
The link Ember Data will use to fetch or reload this belongs-to
|
|
229
|
+
relationship. By default it uses only the "related" resource linkage.
|
|
230
|
+
|
|
231
|
+
Example
|
|
232
|
+
|
|
233
|
+
```javascript
|
|
234
|
+
// models/blog.js
|
|
235
|
+
import Model, { belongsTo } from '@ember-data/model';
|
|
236
|
+
export default Model.extend({
|
|
237
|
+
user: belongsTo({ async: true })
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
let blog = store.push({
|
|
241
|
+
data: {
|
|
242
|
+
type: 'blog',
|
|
243
|
+
id: 1,
|
|
244
|
+
relationships: {
|
|
245
|
+
user: {
|
|
246
|
+
links: {
|
|
247
|
+
related: '/articles/1/author'
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
let userRef = blog.belongsTo('user');
|
|
254
|
+
|
|
255
|
+
// get the identifier of the reference
|
|
256
|
+
if (userRef.remoteType() === "link") {
|
|
257
|
+
let link = userRef.link();
|
|
258
|
+
}
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
@method link
|
|
262
|
+
@public
|
|
263
|
+
@return {String} The link Ember Data will use to fetch or reload this belongs-to relationship.
|
|
264
|
+
*/
|
|
265
|
+
link(): string | null {
|
|
266
|
+
let resource = this._resource();
|
|
267
|
+
|
|
268
|
+
if (isResourceIdentiferWithRelatedLinks(resource)) {
|
|
269
|
+
if (resource.links) {
|
|
270
|
+
let related = resource.links.related;
|
|
271
|
+
return !related || typeof related === 'string' ? related : related.href;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* any links that have been received for this relationship
|
|
279
|
+
*
|
|
280
|
+
* @method links
|
|
281
|
+
* @public
|
|
282
|
+
* @returns
|
|
283
|
+
*/
|
|
284
|
+
links(): PaginationLinks | null {
|
|
285
|
+
let resource = this._resource();
|
|
286
|
+
|
|
287
|
+
return resource && resource.links ? resource.links : null;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
The meta data for the has-many relationship.
|
|
292
|
+
|
|
293
|
+
Example
|
|
294
|
+
|
|
295
|
+
```javascript
|
|
296
|
+
// models/blog.js
|
|
297
|
+
import Model, { hasMany } from '@ember-data/model';
|
|
298
|
+
export default Model.extend({
|
|
299
|
+
users: hasMany({ async: true })
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
let blog = store.push({
|
|
303
|
+
data: {
|
|
304
|
+
type: 'blog',
|
|
305
|
+
id: 1,
|
|
306
|
+
relationships: {
|
|
307
|
+
users: {
|
|
308
|
+
links: {
|
|
309
|
+
related: {
|
|
310
|
+
href: '/articles/1/authors'
|
|
311
|
+
},
|
|
312
|
+
},
|
|
313
|
+
meta: {
|
|
314
|
+
lastUpdated: 1458014400000
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
let usersRef = blog.hasMany('user');
|
|
322
|
+
|
|
323
|
+
usersRef.meta() // { lastUpdated: 1458014400000 }
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
@method meta
|
|
327
|
+
@public
|
|
328
|
+
@return {Object} The meta information for the belongs-to relationship.
|
|
329
|
+
*/
|
|
330
|
+
meta() {
|
|
331
|
+
let meta: Dict<JSONValue> | null = null;
|
|
332
|
+
let resource = this._resource();
|
|
333
|
+
if (resource && resource.meta && typeof resource.meta === 'object') {
|
|
334
|
+
meta = resource.meta;
|
|
335
|
+
}
|
|
336
|
+
return meta;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
`push` can be used to update the data in the relationship and Ember
|
|
341
|
+
Data will treat the new data as the canonical value of this
|
|
342
|
+
relationship on the backend.
|
|
343
|
+
|
|
344
|
+
Example
|
|
345
|
+
|
|
346
|
+
```app/models/post.js
|
|
347
|
+
import Model, { hasMany } from '@ember-data/model';
|
|
348
|
+
|
|
349
|
+
export default class PostModel extends Model {
|
|
350
|
+
@hasMany({ async: true }) comments;
|
|
351
|
+
}
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
```
|
|
355
|
+
let post = store.push({
|
|
356
|
+
data: {
|
|
357
|
+
type: 'post',
|
|
358
|
+
id: 1,
|
|
359
|
+
relationships: {
|
|
360
|
+
comments: {
|
|
361
|
+
data: [{ type: 'comment', id: 1 }]
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
let commentsRef = post.hasMany('comments');
|
|
368
|
+
|
|
369
|
+
commentsRef.ids(); // ['1']
|
|
370
|
+
|
|
371
|
+
commentsRef.push([
|
|
372
|
+
[{ type: 'comment', id: 2 }],
|
|
373
|
+
[{ type: 'comment', id: 3 }],
|
|
374
|
+
])
|
|
375
|
+
|
|
376
|
+
commentsRef.ids(); // ['2', '3']
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
@method push
|
|
380
|
+
@public
|
|
381
|
+
@param {Array|Promise} objectOrPromise a promise that resolves to a JSONAPI document object describing the new value of this relationship.
|
|
382
|
+
@return {ManyArray}
|
|
383
|
+
*/
|
|
384
|
+
async push(
|
|
385
|
+
objectOrPromise: ExistingResourceObject[] | CollectionResourceDocument | { data: SingleResourceDocument[] }
|
|
386
|
+
): Promise<ManyArray> {
|
|
387
|
+
const payload = await resolve(objectOrPromise);
|
|
388
|
+
let array: Array<ExistingResourceObject | SingleResourceDocument>;
|
|
389
|
+
|
|
390
|
+
if (!Array.isArray(payload) && typeof payload === 'object' && Array.isArray(payload.data)) {
|
|
391
|
+
array = payload.data;
|
|
392
|
+
} else {
|
|
393
|
+
array = payload as ExistingResourceObject[];
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
const { store } = this;
|
|
397
|
+
|
|
398
|
+
let identifiers = array.map((obj) => {
|
|
399
|
+
let record: RecordInstance;
|
|
400
|
+
if ('data' in obj) {
|
|
401
|
+
// TODO deprecate pushing non-valid JSON:API here
|
|
402
|
+
record = store.push(obj);
|
|
403
|
+
} else {
|
|
404
|
+
record = store.push({ data: obj });
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
if (DEBUG) {
|
|
408
|
+
let relationshipMeta = this.hasManyRelationship.definition;
|
|
409
|
+
let identifier = this.hasManyRelationship.identifier;
|
|
410
|
+
|
|
411
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
412
|
+
assertPolymorphicType(identifier, relationshipMeta, recordIdentifierFor(record), store);
|
|
413
|
+
}
|
|
414
|
+
return recordIdentifierFor(record);
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
const { graph, identifier } = this.hasManyRelationship;
|
|
418
|
+
store._backburner.join(() => {
|
|
419
|
+
graph.push({
|
|
420
|
+
op: 'replaceRelatedRecords',
|
|
421
|
+
record: identifier,
|
|
422
|
+
field: this.key,
|
|
423
|
+
value: identifiers,
|
|
424
|
+
});
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
return this.load();
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
_isLoaded() {
|
|
431
|
+
let hasRelationshipDataProperty = this.hasManyRelationship.state.hasReceivedData;
|
|
432
|
+
if (!hasRelationshipDataProperty) {
|
|
433
|
+
return false;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
let members = this.hasManyRelationship.currentState;
|
|
437
|
+
|
|
438
|
+
//TODO @runspired determine isLoaded via a better means
|
|
439
|
+
return members.every((identifier) => {
|
|
440
|
+
let internalModel = this.store._instanceCache._internalModelForResource(identifier);
|
|
441
|
+
return internalModel.isLoaded === true;
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
`value()` synchronously returns the current value of the has-many
|
|
447
|
+
relationship. Unlike `record.get('relationshipName')`, calling
|
|
448
|
+
`value()` on a reference does not trigger a fetch if the async
|
|
449
|
+
relationship is not yet loaded. If the relationship is not loaded
|
|
450
|
+
it will always return `null`.
|
|
451
|
+
|
|
452
|
+
Example
|
|
453
|
+
|
|
454
|
+
```app/models/post.js
|
|
455
|
+
import Model, { hasMany } from '@ember-data/model';
|
|
456
|
+
|
|
457
|
+
export default class PostModel extends Model {
|
|
458
|
+
@hasMany({ async: true }) comments;
|
|
459
|
+
}
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
```javascript
|
|
463
|
+
let post = store.push({
|
|
464
|
+
data: {
|
|
465
|
+
type: 'post',
|
|
466
|
+
id: 1,
|
|
467
|
+
relationships: {
|
|
468
|
+
comments: {
|
|
469
|
+
data: [{ type: 'comment', id: 1 }]
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
let commentsRef = post.hasMany('comments');
|
|
476
|
+
|
|
477
|
+
post.get('comments').then(function(comments) {
|
|
478
|
+
commentsRef.value() === comments
|
|
479
|
+
})
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
@method value
|
|
483
|
+
@public
|
|
484
|
+
@return {ManyArray}
|
|
485
|
+
*/
|
|
486
|
+
value() {
|
|
487
|
+
const support: LegacySupport = (
|
|
488
|
+
LEGACY_SUPPORT as DebugWeakCache<StableRecordIdentifier, LegacySupport>
|
|
489
|
+
).getWithError(this.#identifier);
|
|
490
|
+
|
|
491
|
+
return this._isLoaded() ? support.getManyArray(this.key) : null;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
Loads the relationship if it is not already loaded. If the
|
|
496
|
+
relationship is already loaded this method does not trigger a new
|
|
497
|
+
load. This causes a request to the specified
|
|
498
|
+
relationship link or reloads all items currently in the relationship.
|
|
499
|
+
|
|
500
|
+
Example
|
|
501
|
+
|
|
502
|
+
```app/models/post.js
|
|
503
|
+
import Model, { hasMany } from '@ember-data/model';
|
|
504
|
+
|
|
505
|
+
export default class PostModel extends Model {
|
|
506
|
+
@hasMany({ async: true }) comments;
|
|
507
|
+
}
|
|
508
|
+
```
|
|
509
|
+
|
|
510
|
+
```javascript
|
|
511
|
+
let post = store.push({
|
|
512
|
+
data: {
|
|
513
|
+
type: 'post',
|
|
514
|
+
id: 1,
|
|
515
|
+
relationships: {
|
|
516
|
+
comments: {
|
|
517
|
+
data: [{ type: 'comment', id: 1 }]
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
});
|
|
522
|
+
|
|
523
|
+
let commentsRef = post.hasMany('comments');
|
|
524
|
+
|
|
525
|
+
commentsRef.load().then(function(comments) {
|
|
526
|
+
//...
|
|
527
|
+
});
|
|
528
|
+
```
|
|
529
|
+
|
|
530
|
+
You may also pass in an options object whose properties will be
|
|
531
|
+
fed forward. This enables you to pass `adapterOptions` into the
|
|
532
|
+
request given to the adapter via the reference.
|
|
533
|
+
|
|
534
|
+
Example
|
|
535
|
+
|
|
536
|
+
```javascript
|
|
537
|
+
commentsRef.load({ adapterOptions: { isPrivate: true } })
|
|
538
|
+
.then(function(comments) {
|
|
539
|
+
//...
|
|
540
|
+
});
|
|
541
|
+
```
|
|
542
|
+
|
|
543
|
+
```app/adapters/comment.js
|
|
544
|
+
export default ApplicationAdapter.extend({
|
|
545
|
+
findMany(store, type, id, snapshots) {
|
|
546
|
+
// In the adapter you will have access to adapterOptions.
|
|
547
|
+
let adapterOptions = snapshots[0].adapterOptions;
|
|
548
|
+
}
|
|
549
|
+
});
|
|
550
|
+
```
|
|
551
|
+
|
|
552
|
+
@method load
|
|
553
|
+
@public
|
|
554
|
+
@param {Object} options the options to pass in.
|
|
555
|
+
@return {Promise} a promise that resolves with the ManyArray in
|
|
556
|
+
this has-many relationship.
|
|
557
|
+
*/
|
|
558
|
+
async load(options?: FindOptions): Promise<ManyArray> {
|
|
559
|
+
const support: LegacySupport = (
|
|
560
|
+
LEGACY_SUPPORT as DebugWeakCache<StableRecordIdentifier, LegacySupport>
|
|
561
|
+
).getWithError(this.#identifier);
|
|
562
|
+
return support.getHasMany(this.key, options) as Promise<ManyArray> | ManyArray; // this cast is necessary because typescript does not work properly with custom thenables;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
Reloads this has-many relationship. This causes a request to the specified
|
|
567
|
+
relationship link or reloads all items currently in the relationship.
|
|
568
|
+
|
|
569
|
+
Example
|
|
570
|
+
|
|
571
|
+
```app/models/post.js
|
|
572
|
+
import Model, { hasMany } from '@ember-data/model';
|
|
573
|
+
|
|
574
|
+
export default class PostModel extends Model {
|
|
575
|
+
@hasMany({ async: true }) comments;
|
|
576
|
+
}
|
|
577
|
+
```
|
|
578
|
+
|
|
579
|
+
```javascript
|
|
580
|
+
let post = store.push({
|
|
581
|
+
data: {
|
|
582
|
+
type: 'post',
|
|
583
|
+
id: 1,
|
|
584
|
+
relationships: {
|
|
585
|
+
comments: {
|
|
586
|
+
data: [{ type: 'comment', id: 1 }]
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
});
|
|
591
|
+
|
|
592
|
+
let commentsRef = post.hasMany('comments');
|
|
593
|
+
|
|
594
|
+
commentsRef.reload().then(function(comments) {
|
|
595
|
+
//...
|
|
596
|
+
});
|
|
597
|
+
```
|
|
598
|
+
|
|
599
|
+
You may also pass in an options object whose properties will be
|
|
600
|
+
fed forward. This enables you to pass `adapterOptions` into the
|
|
601
|
+
request given to the adapter via the reference. A full example
|
|
602
|
+
can be found in the `load` method.
|
|
603
|
+
|
|
604
|
+
Example
|
|
605
|
+
|
|
606
|
+
```javascript
|
|
607
|
+
commentsRef.reload({ adapterOptions: { isPrivate: true } })
|
|
608
|
+
```
|
|
609
|
+
|
|
610
|
+
@method reload
|
|
611
|
+
@public
|
|
612
|
+
@param {Object} options the options to pass in.
|
|
613
|
+
@return {Promise} a promise that resolves with the ManyArray in this has-many relationship.
|
|
614
|
+
*/
|
|
615
|
+
reload(options?: FindOptions) {
|
|
616
|
+
const support: LegacySupport = (
|
|
617
|
+
LEGACY_SUPPORT as DebugWeakCache<StableRecordIdentifier, LegacySupport>
|
|
618
|
+
).getWithError(this.#identifier);
|
|
619
|
+
return support.reloadHasMany(this.key, options);
|
|
620
|
+
}
|
|
621
|
+
}
|
|
@@ -2,9 +2,9 @@ import { DEBUG } from '@glimmer/env';
|
|
|
2
2
|
|
|
3
3
|
import { singularize } from 'ember-inflector';
|
|
4
4
|
|
|
5
|
+
import type Store from '@ember-data/store';
|
|
5
6
|
import { normalizeModelName } from '@ember-data/store/-private';
|
|
6
|
-
import type
|
|
7
|
-
import type { RelationshipSchema } from '@ember-data/store/-private/ts-interfaces/record-data-schemas';
|
|
7
|
+
import type { RelationshipSchema } from '@ember-data/types/q/record-data-schemas';
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
@module @ember-data/store
|
|
@@ -67,21 +67,21 @@ export class RelationshipDefinition implements RelationshipSchema {
|
|
|
67
67
|
return this.meta.name;
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
_inverseKey(store:
|
|
70
|
+
_inverseKey(store: Store, modelClass): string {
|
|
71
71
|
if (this.__hasCalculatedInverse === false) {
|
|
72
72
|
this._calculateInverse(store, modelClass);
|
|
73
73
|
}
|
|
74
74
|
return this.__inverseKey;
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
-
_inverseIsAsync(store:
|
|
77
|
+
_inverseIsAsync(store: Store, modelClass): boolean {
|
|
78
78
|
if (this.__hasCalculatedInverse === false) {
|
|
79
79
|
this._calculateInverse(store, modelClass);
|
|
80
80
|
}
|
|
81
81
|
return this.__inverseIsAsync;
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
_calculateInverse(store:
|
|
84
|
+
_calculateInverse(store: Store, modelClass): void {
|
|
85
85
|
this.__hasCalculatedInverse = true;
|
|
86
86
|
let inverseKey, inverseIsAsync;
|
|
87
87
|
let inverse: any = null;
|
|
@@ -2,6 +2,7 @@ const path = require('path');
|
|
|
2
2
|
|
|
3
3
|
const testInfo = require('ember-cli-test-info');
|
|
4
4
|
const useTestFrameworkDetector = require('@ember-data/private-build-infra/src/utilities/test-framework-detector');
|
|
5
|
+
const modulePrefixForProject = require('@ember-data/private-build-infra/src/utilities/module-prefix-for-project');
|
|
5
6
|
|
|
6
7
|
const ModelBlueprint = require('../model');
|
|
7
8
|
|
|
@@ -25,6 +26,7 @@ module.exports = useTestFrameworkDetector({
|
|
|
25
26
|
const result = ModelBlueprint.locals.apply(this, arguments);
|
|
26
27
|
|
|
27
28
|
result.friendlyTestDescription = testInfo.description(options.entity.name, 'Unit', 'Model');
|
|
29
|
+
result.modulePrefix = modulePrefixForProject(options.project);
|
|
28
30
|
|
|
29
31
|
return result;
|
|
30
32
|
},
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { expect } from 'chai';
|
|
2
2
|
import { describe, it } from 'mocha';
|
|
3
|
-
import { setupTest } from '<%=
|
|
3
|
+
import { setupTest } from '<%= modulePrefix %>/tests/helpers';
|
|
4
4
|
|
|
5
5
|
describe('<%= friendlyTestDescription %>', function() {
|
|
6
6
|
setupTest();
|
package/index.js
CHANGED
|
@@ -15,6 +15,9 @@ module.exports = Object.assign({}, addonBaseConfig, {
|
|
|
15
15
|
'@ember-data/canary-features',
|
|
16
16
|
'@ember-data/store',
|
|
17
17
|
'@ember-data/store/-private',
|
|
18
|
+
'@ember-data/store/-debug',
|
|
19
|
+
'@embroider/macros',
|
|
20
|
+
'@embroider/macros/es-compat',
|
|
18
21
|
|
|
19
22
|
'@ember/application',
|
|
20
23
|
'@ember/array',
|
|
@@ -29,6 +32,7 @@ module.exports = Object.assign({}, addonBaseConfig, {
|
|
|
29
32
|
'@ember/polyfills',
|
|
30
33
|
'@ember/runloop',
|
|
31
34
|
'@ember/utils',
|
|
35
|
+
'@ember/service',
|
|
32
36
|
|
|
33
37
|
'@glimmer/tracking/primitives/cache',
|
|
34
38
|
'@glimmer/tracking',
|