@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.
- package/addon/-private/{system/backburner.js → backburner.js} +0 -0
- package/addon/-private/{system/coerce-id.ts → coerce-id.ts} +0 -0
- package/addon/-private/{system/store/common.js → common.js} +0 -0
- package/addon/-private/{system/core-store.ts → core-store.ts} +467 -1253
- package/addon/-private/{system/errors-utils.js → errors-utils.js} +7 -6
- package/addon/-private/{system/fetch-manager.ts → fetch-manager.ts} +72 -42
- package/addon/-private/finders.js +107 -0
- package/addon/-private/identifer-debug-consts.ts +3 -0
- package/addon/-private/{identifiers/cache.ts → identifier-cache.ts} +26 -14
- package/addon/-private/{system/identity-map.ts → identity-map.ts} +2 -1
- package/addon/-private/index.ts +17 -17
- package/addon/-private/instance-cache.ts +387 -0
- package/addon/-private/{system/store/internal-model-factory.ts → internal-model-factory.ts} +25 -19
- package/addon/-private/{system/internal-model-map.ts → internal-model-map.ts} +9 -5
- package/addon/-private/model/internal-model.ts +602 -0
- package/addon/-private/{system/references/record.ts → model/record-reference.ts} +23 -36
- package/addon/-private/{system/model → model}/shim-model-class.ts +19 -14
- package/addon/-private/{system/normalize-model-name.ts → normalize-model-name.ts} +0 -0
- package/addon/-private/{system/promise-proxies.ts → promise-proxies.ts} +12 -5
- package/addon/-private/{system/promise-proxy-base.js → promise-proxy-base.js} +0 -0
- package/addon/-private/{system/record-array-manager.ts → record-array-manager.ts} +19 -18
- package/addon/-private/{system/record-arrays → record-arrays}/adapter-populated-record-array.ts +11 -10
- package/addon/-private/{system/record-arrays → record-arrays}/record-array.ts +37 -19
- package/addon/-private/record-data-for.ts +39 -0
- package/addon/-private/{system/store/record-data-store-wrapper.ts → record-data-store-wrapper.ts} +21 -26
- package/addon/-private/{system/record-notification-manager.ts → record-notification-manager.ts} +8 -3
- package/addon/-private/{system/request-cache.ts → request-cache.ts} +5 -6
- package/addon/-private/{system/schema-definition-service.ts → schema-definition-service.ts} +30 -14
- package/addon/-private/{system/store/serializer-response.ts → serializer-response.ts} +7 -6
- package/addon/-private/{system/snapshot-record-array.ts → snapshot-record-array.ts} +27 -8
- package/addon/-private/{system/snapshot.ts → snapshot.ts} +54 -39
- package/addon/-private/utils/construct-resource.ts +7 -3
- package/addon/-private/utils/promise-record.ts +9 -18
- package/addon/-private/{system/weak-cache.ts → weak-cache.ts} +2 -2
- package/addon/index.ts +1 -0
- package/package.json +21 -20
- package/addon/-private/identifiers/is-stable-identifier.ts +0 -18
- package/addon/-private/identifiers/utils/uuid-v4.ts +0 -80
- package/addon/-private/system/ds-model-store.ts +0 -136
- package/addon/-private/system/model/internal-model.ts +0 -1303
- package/addon/-private/system/model/states.js +0 -736
- package/addon/-private/system/record-arrays.ts +0 -8
- package/addon/-private/system/record-data-for.ts +0 -54
- package/addon/-private/system/references/belongs-to.ts +0 -406
- package/addon/-private/system/references/has-many.ts +0 -487
- package/addon/-private/system/references/reference.ts +0 -205
- package/addon/-private/system/references.js +0 -9
- package/addon/-private/system/store/finders.js +0 -412
- package/addon/-private/ts-interfaces/ds-model.ts +0 -50
- package/addon/-private/ts-interfaces/ember-data-json-api.ts +0 -145
- package/addon/-private/ts-interfaces/fetch-manager.ts +0 -44
- package/addon/-private/ts-interfaces/identifier.ts +0 -246
- package/addon/-private/ts-interfaces/minimum-adapter-interface.ts +0 -584
- package/addon/-private/ts-interfaces/minimum-serializer-interface.ts +0 -257
- package/addon/-private/ts-interfaces/promise-proxies.ts +0 -3
- package/addon/-private/ts-interfaces/record-data-json-api.ts +0 -29
- package/addon/-private/ts-interfaces/record-data-record-wrapper.ts +0 -46
- package/addon/-private/ts-interfaces/record-data-schemas.ts +0 -45
- package/addon/-private/ts-interfaces/record-data-store-wrapper.ts +0 -56
- package/addon/-private/ts-interfaces/record-data.ts +0 -72
- package/addon/-private/ts-interfaces/record-instance.ts +0 -18
- package/addon/-private/ts-interfaces/schema-definition-service.ts +0 -12
- package/addon/-private/ts-interfaces/store.ts +0 -10
- package/addon/-private/ts-interfaces/utils.ts +0 -6
|
@@ -1,487 +0,0 @@
|
|
|
1
|
-
import { dependentKeyCompat } from '@ember/object/compat';
|
|
2
|
-
import { DEBUG } from '@glimmer/env';
|
|
3
|
-
import { cached, tracked } from '@glimmer/tracking';
|
|
4
|
-
|
|
5
|
-
import { resolve } from 'rsvp';
|
|
6
|
-
|
|
7
|
-
import { ManyArray } from 'ember-data/-private';
|
|
8
|
-
|
|
9
|
-
import type { ManyRelationship } from '@ember-data/record-data/-private';
|
|
10
|
-
import { assertPolymorphicType } from '@ember-data/store/-debug';
|
|
11
|
-
|
|
12
|
-
import {
|
|
13
|
-
CollectionResourceDocument,
|
|
14
|
-
ExistingResourceObject,
|
|
15
|
-
SingleResourceDocument,
|
|
16
|
-
} from '../../ts-interfaces/ember-data-json-api';
|
|
17
|
-
import { StableRecordIdentifier } from '../../ts-interfaces/identifier';
|
|
18
|
-
import CoreStore from '../core-store';
|
|
19
|
-
import { NotificationType, unsubscribe } from '../record-notification-manager';
|
|
20
|
-
import { internalModelFactoryFor, recordIdentifierFor } from '../store/internal-model-factory';
|
|
21
|
-
import RecordReference from './record';
|
|
22
|
-
import Reference from './reference';
|
|
23
|
-
/**
|
|
24
|
-
@module @ember-data/store
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
A `HasManyReference` is a low-level API that allows users and addon
|
|
29
|
-
authors to perform meta-operations on a has-many relationship.
|
|
30
|
-
|
|
31
|
-
@class HasManyReference
|
|
32
|
-
@public
|
|
33
|
-
@extends Reference
|
|
34
|
-
*/
|
|
35
|
-
export default class HasManyReference extends Reference {
|
|
36
|
-
declare key: string;
|
|
37
|
-
declare hasManyRelationship: ManyRelationship;
|
|
38
|
-
declare type: string;
|
|
39
|
-
declare parent: RecordReference;
|
|
40
|
-
|
|
41
|
-
// unsubscribe tokens given to us by the notification manager
|
|
42
|
-
#token!: Object;
|
|
43
|
-
#identifier: StableRecordIdentifier;
|
|
44
|
-
#relatedTokenMap!: Map<StableRecordIdentifier, Object>;
|
|
45
|
-
|
|
46
|
-
@tracked _ref = 0;
|
|
47
|
-
|
|
48
|
-
constructor(
|
|
49
|
-
store: CoreStore,
|
|
50
|
-
parentIdentifier: StableRecordIdentifier,
|
|
51
|
-
hasManyRelationship: ManyRelationship,
|
|
52
|
-
key: string
|
|
53
|
-
) {
|
|
54
|
-
super(store, parentIdentifier);
|
|
55
|
-
this.key = key;
|
|
56
|
-
this.hasManyRelationship = hasManyRelationship;
|
|
57
|
-
this.type = hasManyRelationship.definition.type;
|
|
58
|
-
|
|
59
|
-
this.parent = internalModelFactoryFor(store).peek(parentIdentifier)!.recordReference;
|
|
60
|
-
this.#identifier = parentIdentifier;
|
|
61
|
-
this.#token = store._notificationManager.subscribe(
|
|
62
|
-
parentIdentifier,
|
|
63
|
-
(_: StableRecordIdentifier, bucket: NotificationType, notifiedKey?: string) => {
|
|
64
|
-
if ((bucket === 'relationships' || bucket === 'property') && notifiedKey === key) {
|
|
65
|
-
this._ref++;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
);
|
|
69
|
-
this.#relatedTokenMap = new Map();
|
|
70
|
-
// TODO inverse
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
destroy() {
|
|
74
|
-
unsubscribe(this.#token);
|
|
75
|
-
this.#relatedTokenMap.forEach((token) => {
|
|
76
|
-
unsubscribe(token);
|
|
77
|
-
});
|
|
78
|
-
this.#relatedTokenMap.clear();
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
@cached
|
|
82
|
-
@dependentKeyCompat
|
|
83
|
-
get _relatedIdentifiers(): StableRecordIdentifier[] {
|
|
84
|
-
this._ref; // consume the tracked prop
|
|
85
|
-
|
|
86
|
-
let resource = this._resource();
|
|
87
|
-
|
|
88
|
-
this.#relatedTokenMap.forEach((token) => {
|
|
89
|
-
unsubscribe(token);
|
|
90
|
-
});
|
|
91
|
-
this.#relatedTokenMap.clear();
|
|
92
|
-
|
|
93
|
-
if (resource && resource.data) {
|
|
94
|
-
return resource.data.map((resourceIdentifier) => {
|
|
95
|
-
const identifier = this.store.identifierCache.getOrCreateRecordIdentifier(resourceIdentifier);
|
|
96
|
-
const token = this.store._notificationManager.subscribe(
|
|
97
|
-
identifier,
|
|
98
|
-
(_: StableRecordIdentifier, bucket: NotificationType, notifiedKey?: string) => {
|
|
99
|
-
if (bucket === 'identity' || ((bucket === 'attributes' || bucket === 'property') && notifiedKey === 'id')) {
|
|
100
|
-
this._ref++;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
);
|
|
104
|
-
|
|
105
|
-
this.#relatedTokenMap.set(identifier, token);
|
|
106
|
-
|
|
107
|
-
return identifier;
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return [];
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
_resource() {
|
|
115
|
-
return this.recordData.getHasMany(this.key);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
This returns a string that represents how the reference will be
|
|
120
|
-
looked up when it is loaded. If the relationship has a link it will
|
|
121
|
-
use the "link" otherwise it defaults to "id".
|
|
122
|
-
|
|
123
|
-
Example
|
|
124
|
-
|
|
125
|
-
```app/models/post.js
|
|
126
|
-
import Model, { hasMany } from '@ember-data/model';
|
|
127
|
-
|
|
128
|
-
export default class PostModel extends Model {
|
|
129
|
-
@hasMany({ async: true }) comments;
|
|
130
|
-
}
|
|
131
|
-
```
|
|
132
|
-
|
|
133
|
-
```javascript
|
|
134
|
-
let post = store.push({
|
|
135
|
-
data: {
|
|
136
|
-
type: 'post',
|
|
137
|
-
id: 1,
|
|
138
|
-
relationships: {
|
|
139
|
-
comments: {
|
|
140
|
-
data: [{ type: 'comment', id: 1 }]
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
let commentsRef = post.hasMany('comments');
|
|
147
|
-
|
|
148
|
-
// get the identifier of the reference
|
|
149
|
-
if (commentsRef.remoteType() === "ids") {
|
|
150
|
-
let ids = commentsRef.ids();
|
|
151
|
-
} else if (commentsRef.remoteType() === "link") {
|
|
152
|
-
let link = commentsRef.link();
|
|
153
|
-
}
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
@method remoteType
|
|
157
|
-
@public
|
|
158
|
-
@return {String} The name of the remote type. This should either be `link` or `ids`
|
|
159
|
-
*/
|
|
160
|
-
remoteType(): 'link' | 'ids' {
|
|
161
|
-
let value = this._resource();
|
|
162
|
-
if (value && value.links && value.links.related) {
|
|
163
|
-
return 'link';
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
return 'ids';
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
`ids()` returns an array of the record IDs in this relationship.
|
|
171
|
-
|
|
172
|
-
Example
|
|
173
|
-
|
|
174
|
-
```app/models/post.js
|
|
175
|
-
import Model, { hasMany } from '@ember-data/model';
|
|
176
|
-
|
|
177
|
-
export default class PostModel extends Model {
|
|
178
|
-
@hasMany({ async: true }) comments;
|
|
179
|
-
}
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
```javascript
|
|
183
|
-
let post = store.push({
|
|
184
|
-
data: {
|
|
185
|
-
type: 'post',
|
|
186
|
-
id: 1,
|
|
187
|
-
relationships: {
|
|
188
|
-
comments: {
|
|
189
|
-
data: [{ type: 'comment', id: 1 }]
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
let commentsRef = post.hasMany('comments');
|
|
196
|
-
|
|
197
|
-
commentsRef.ids(); // ['1']
|
|
198
|
-
```
|
|
199
|
-
|
|
200
|
-
@method ids
|
|
201
|
-
@public
|
|
202
|
-
@return {Array} The ids in this has-many relationship
|
|
203
|
-
*/
|
|
204
|
-
ids(): Array<string | null> {
|
|
205
|
-
return this._relatedIdentifiers.map((identifier) => identifier.id);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
`push` can be used to update the data in the relationship and Ember
|
|
210
|
-
Data will treat the new data as the canonical value of this
|
|
211
|
-
relationship on the backend.
|
|
212
|
-
|
|
213
|
-
Example
|
|
214
|
-
|
|
215
|
-
```app/models/post.js
|
|
216
|
-
import Model, { hasMany } from '@ember-data/model';
|
|
217
|
-
|
|
218
|
-
export default class PostModel extends Model {
|
|
219
|
-
@hasMany({ async: true }) comments;
|
|
220
|
-
}
|
|
221
|
-
```
|
|
222
|
-
|
|
223
|
-
```
|
|
224
|
-
let post = store.push({
|
|
225
|
-
data: {
|
|
226
|
-
type: 'post',
|
|
227
|
-
id: 1,
|
|
228
|
-
relationships: {
|
|
229
|
-
comments: {
|
|
230
|
-
data: [{ type: 'comment', id: 1 }]
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
let commentsRef = post.hasMany('comments');
|
|
237
|
-
|
|
238
|
-
commentsRef.ids(); // ['1']
|
|
239
|
-
|
|
240
|
-
commentsRef.push([
|
|
241
|
-
[{ type: 'comment', id: 2 }],
|
|
242
|
-
[{ type: 'comment', id: 3 }],
|
|
243
|
-
])
|
|
244
|
-
|
|
245
|
-
commentsRef.ids(); // ['2', '3']
|
|
246
|
-
```
|
|
247
|
-
|
|
248
|
-
@method push
|
|
249
|
-
@public
|
|
250
|
-
@param {Array|Promise} objectOrPromise a promise that resolves to a JSONAPI document object describing the new value of this relationship.
|
|
251
|
-
@return {ManyArray}
|
|
252
|
-
*/
|
|
253
|
-
async push(
|
|
254
|
-
objectOrPromise: ExistingResourceObject[] | CollectionResourceDocument | { data: SingleResourceDocument[] }
|
|
255
|
-
): Promise<ManyArray> {
|
|
256
|
-
const payload = await resolve(objectOrPromise);
|
|
257
|
-
let array: Array<ExistingResourceObject | SingleResourceDocument>;
|
|
258
|
-
|
|
259
|
-
if (!Array.isArray(payload) && typeof payload === 'object' && Array.isArray(payload.data)) {
|
|
260
|
-
array = payload.data;
|
|
261
|
-
} else {
|
|
262
|
-
array = payload as ExistingResourceObject[];
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
const internalModel = internalModelFactoryFor(this.store).peek(this.#identifier)!;
|
|
266
|
-
const { store } = this;
|
|
267
|
-
|
|
268
|
-
let identifiers = array.map((obj) => {
|
|
269
|
-
let record;
|
|
270
|
-
if ('data' in obj) {
|
|
271
|
-
// TODO deprecate pushing non-valid JSON:API here
|
|
272
|
-
record = store.push(obj);
|
|
273
|
-
} else {
|
|
274
|
-
record = store.push({ data: obj });
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
if (DEBUG) {
|
|
278
|
-
let relationshipMeta = this.hasManyRelationship.definition;
|
|
279
|
-
let identifier = this.hasManyRelationship.identifier;
|
|
280
|
-
assertPolymorphicType(identifier, relationshipMeta, recordIdentifierFor(record), store);
|
|
281
|
-
}
|
|
282
|
-
return recordIdentifierFor(record);
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
const { graph, identifier } = this.hasManyRelationship;
|
|
286
|
-
store._backburner.join(() => {
|
|
287
|
-
graph.push({
|
|
288
|
-
op: 'replaceRelatedRecords',
|
|
289
|
-
record: identifier,
|
|
290
|
-
field: this.key,
|
|
291
|
-
value: identifiers,
|
|
292
|
-
});
|
|
293
|
-
});
|
|
294
|
-
|
|
295
|
-
// TODO IGOR it seems wrong that we were returning the many array here
|
|
296
|
-
return internalModel.getHasMany(this.key) as Promise<ManyArray> | ManyArray; // this cast is necessary because typescript does not work properly with custom thenables
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
_isLoaded() {
|
|
300
|
-
let hasRelationshipDataProperty = this.hasManyRelationship.state.hasReceivedData;
|
|
301
|
-
if (!hasRelationshipDataProperty) {
|
|
302
|
-
return false;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
let members = this.hasManyRelationship.currentState;
|
|
306
|
-
|
|
307
|
-
//TODO Igor cleanup
|
|
308
|
-
return members.every((identifier) => {
|
|
309
|
-
let internalModel = this.store._internalModelForResource(identifier);
|
|
310
|
-
return internalModel.currentState.isLoaded === true;
|
|
311
|
-
});
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
/**
|
|
315
|
-
`value()` synchronously returns the current value of the has-many
|
|
316
|
-
relationship. Unlike `record.get('relationshipName')`, calling
|
|
317
|
-
`value()` on a reference does not trigger a fetch if the async
|
|
318
|
-
relationship is not yet loaded. If the relationship is not loaded
|
|
319
|
-
it will always return `null`.
|
|
320
|
-
|
|
321
|
-
Example
|
|
322
|
-
|
|
323
|
-
```app/models/post.js
|
|
324
|
-
import Model, { hasMany } from '@ember-data/model';
|
|
325
|
-
|
|
326
|
-
export default class PostModel extends Model {
|
|
327
|
-
@hasMany({ async: true }) comments;
|
|
328
|
-
}
|
|
329
|
-
```
|
|
330
|
-
|
|
331
|
-
```javascript
|
|
332
|
-
let post = store.push({
|
|
333
|
-
data: {
|
|
334
|
-
type: 'post',
|
|
335
|
-
id: 1,
|
|
336
|
-
relationships: {
|
|
337
|
-
comments: {
|
|
338
|
-
data: [{ type: 'comment', id: 1 }]
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
});
|
|
343
|
-
|
|
344
|
-
let commentsRef = post.hasMany('comments');
|
|
345
|
-
|
|
346
|
-
post.get('comments').then(function(comments) {
|
|
347
|
-
commentsRef.value() === comments
|
|
348
|
-
})
|
|
349
|
-
```
|
|
350
|
-
|
|
351
|
-
@method value
|
|
352
|
-
@public
|
|
353
|
-
@return {ManyArray}
|
|
354
|
-
*/
|
|
355
|
-
value() {
|
|
356
|
-
const internalModel = internalModelFactoryFor(this.store).peek(this.#identifier)!;
|
|
357
|
-
if (this._isLoaded()) {
|
|
358
|
-
return internalModel.getManyArray(this.key);
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
return null;
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
/**
|
|
365
|
-
Loads the relationship if it is not already loaded. If the
|
|
366
|
-
relationship is already loaded this method does not trigger a new
|
|
367
|
-
load. This causes a request to the specified
|
|
368
|
-
relationship link or reloads all items currently in the relationship.
|
|
369
|
-
|
|
370
|
-
Example
|
|
371
|
-
|
|
372
|
-
```app/models/post.js
|
|
373
|
-
import Model, { hasMany } from '@ember-data/model';
|
|
374
|
-
|
|
375
|
-
export default class PostModel extends Model {
|
|
376
|
-
@hasMany({ async: true }) comments;
|
|
377
|
-
}
|
|
378
|
-
```
|
|
379
|
-
|
|
380
|
-
```javascript
|
|
381
|
-
let post = store.push({
|
|
382
|
-
data: {
|
|
383
|
-
type: 'post',
|
|
384
|
-
id: 1,
|
|
385
|
-
relationships: {
|
|
386
|
-
comments: {
|
|
387
|
-
data: [{ type: 'comment', id: 1 }]
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
});
|
|
392
|
-
|
|
393
|
-
let commentsRef = post.hasMany('comments');
|
|
394
|
-
|
|
395
|
-
commentsRef.load().then(function(comments) {
|
|
396
|
-
//...
|
|
397
|
-
});
|
|
398
|
-
```
|
|
399
|
-
|
|
400
|
-
You may also pass in an options object whose properties will be
|
|
401
|
-
fed forward. This enables you to pass `adapterOptions` into the
|
|
402
|
-
request given to the adapter via the reference.
|
|
403
|
-
|
|
404
|
-
Example
|
|
405
|
-
|
|
406
|
-
```javascript
|
|
407
|
-
commentsRef.load({ adapterOptions: { isPrivate: true } })
|
|
408
|
-
.then(function(comments) {
|
|
409
|
-
//...
|
|
410
|
-
});
|
|
411
|
-
```
|
|
412
|
-
|
|
413
|
-
```app/adapters/comment.js
|
|
414
|
-
export default ApplicationAdapter.extend({
|
|
415
|
-
findMany(store, type, id, snapshots) {
|
|
416
|
-
// In the adapter you will have access to adapterOptions.
|
|
417
|
-
let adapterOptions = snapshots[0].adapterOptions;
|
|
418
|
-
}
|
|
419
|
-
});
|
|
420
|
-
```
|
|
421
|
-
|
|
422
|
-
@method load
|
|
423
|
-
@public
|
|
424
|
-
@param {Object} options the options to pass in.
|
|
425
|
-
@return {Promise} a promise that resolves with the ManyArray in
|
|
426
|
-
this has-many relationship.
|
|
427
|
-
*/
|
|
428
|
-
load(options) {
|
|
429
|
-
const internalModel = internalModelFactoryFor(this.store).peek(this.#identifier)!;
|
|
430
|
-
return internalModel.getHasMany(this.key, options);
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
/**
|
|
434
|
-
Reloads this has-many relationship. This causes a request to the specified
|
|
435
|
-
relationship link or reloads all items currently in the relationship.
|
|
436
|
-
|
|
437
|
-
Example
|
|
438
|
-
|
|
439
|
-
```app/models/post.js
|
|
440
|
-
import Model, { hasMany } from '@ember-data/model';
|
|
441
|
-
|
|
442
|
-
export default class PostModel extends Model {
|
|
443
|
-
@hasMany({ async: true }) comments;
|
|
444
|
-
}
|
|
445
|
-
```
|
|
446
|
-
|
|
447
|
-
```javascript
|
|
448
|
-
let post = store.push({
|
|
449
|
-
data: {
|
|
450
|
-
type: 'post',
|
|
451
|
-
id: 1,
|
|
452
|
-
relationships: {
|
|
453
|
-
comments: {
|
|
454
|
-
data: [{ type: 'comment', id: 1 }]
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
});
|
|
459
|
-
|
|
460
|
-
let commentsRef = post.hasMany('comments');
|
|
461
|
-
|
|
462
|
-
commentsRef.reload().then(function(comments) {
|
|
463
|
-
//...
|
|
464
|
-
});
|
|
465
|
-
```
|
|
466
|
-
|
|
467
|
-
You may also pass in an options object whose properties will be
|
|
468
|
-
fed forward. This enables you to pass `adapterOptions` into the
|
|
469
|
-
request given to the adapter via the reference. A full example
|
|
470
|
-
can be found in the `load` method.
|
|
471
|
-
|
|
472
|
-
Example
|
|
473
|
-
|
|
474
|
-
```javascript
|
|
475
|
-
commentsRef.reload({ adapterOptions: { isPrivate: true } })
|
|
476
|
-
```
|
|
477
|
-
|
|
478
|
-
@method reload
|
|
479
|
-
@public
|
|
480
|
-
@param {Object} options the options to pass in.
|
|
481
|
-
@return {Promise} a promise that resolves with the ManyArray in this has-many relationship.
|
|
482
|
-
*/
|
|
483
|
-
reload(options) {
|
|
484
|
-
const internalModel = internalModelFactoryFor(this.store).peek(this.#identifier)!;
|
|
485
|
-
return internalModel.reloadHasMany(this.key, options);
|
|
486
|
-
}
|
|
487
|
-
}
|
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
import type { Object as JSONObject, Value as JSONValue } from 'json-typescript';
|
|
2
|
-
|
|
3
|
-
import type { LinkObject, PaginationLinks } from '../../ts-interfaces/ember-data-json-api';
|
|
4
|
-
import type { StableRecordIdentifier } from '../../ts-interfaces/identifier';
|
|
5
|
-
import type { JsonApiRelationship } from '../../ts-interfaces/record-data-json-api';
|
|
6
|
-
import type { Dict } from '../../ts-interfaces/utils';
|
|
7
|
-
import type CoreStore from '../core-store';
|
|
8
|
-
/**
|
|
9
|
-
@module @ember-data/store
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
interface ResourceIdentifier {
|
|
13
|
-
links?: {
|
|
14
|
-
related?: string;
|
|
15
|
-
};
|
|
16
|
-
meta?: JSONObject;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function isResourceIdentiferWithRelatedLinks(
|
|
20
|
-
value: any
|
|
21
|
-
): value is ResourceIdentifier & { links: { related: string | LinkObject | null } } {
|
|
22
|
-
return value && value.links && value.links.related;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
This is the baseClass for the different References
|
|
27
|
-
like RecordReference/HasManyReference/BelongsToReference
|
|
28
|
-
|
|
29
|
-
@class Reference
|
|
30
|
-
@public
|
|
31
|
-
*/
|
|
32
|
-
interface Reference {
|
|
33
|
-
links(): PaginationLinks | null;
|
|
34
|
-
}
|
|
35
|
-
abstract class Reference {
|
|
36
|
-
#identifier: StableRecordIdentifier;
|
|
37
|
-
|
|
38
|
-
constructor(public store: CoreStore, identifier: StableRecordIdentifier) {
|
|
39
|
-
this.#identifier = identifier;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
get recordData() {
|
|
43
|
-
return this.store.recordDataFor(this.#identifier, false);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
public _resource(): ResourceIdentifier | JsonApiRelationship | void {}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
This returns a string that represents how the reference will be
|
|
50
|
-
looked up when it is loaded. If the relationship has a link it will
|
|
51
|
-
use the "link" otherwise it defaults to "id".
|
|
52
|
-
|
|
53
|
-
Example
|
|
54
|
-
|
|
55
|
-
```app/models/post.js
|
|
56
|
-
import Model, { hasMany } from '@ember-data/model';
|
|
57
|
-
|
|
58
|
-
export default Model.extend({
|
|
59
|
-
comments: hasMany({ async: true })
|
|
60
|
-
});
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
```javascript
|
|
64
|
-
let post = store.push({
|
|
65
|
-
data: {
|
|
66
|
-
type: 'post',
|
|
67
|
-
id: 1,
|
|
68
|
-
relationships: {
|
|
69
|
-
comments: {
|
|
70
|
-
data: [{ type: 'comment', id: 1 }]
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
let commentsRef = post.hasMany('comments');
|
|
77
|
-
|
|
78
|
-
// get the identifier of the reference
|
|
79
|
-
if (commentsRef.remoteType() === "ids") {
|
|
80
|
-
let ids = commentsRef.ids();
|
|
81
|
-
} else if (commentsRef.remoteType() === "link") {
|
|
82
|
-
let link = commentsRef.link();
|
|
83
|
-
}
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
@method remoteType
|
|
87
|
-
@public
|
|
88
|
-
@return {String} The name of the remote type. This should either be "link" or "ids"
|
|
89
|
-
*/
|
|
90
|
-
remoteType(): 'link' | 'id' | 'ids' | 'identity' {
|
|
91
|
-
let value = this._resource();
|
|
92
|
-
if (isResourceIdentiferWithRelatedLinks(value)) {
|
|
93
|
-
return 'link';
|
|
94
|
-
}
|
|
95
|
-
return 'id';
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
The link Ember Data will use to fetch or reload this belongs-to
|
|
100
|
-
relationship. By default it uses only the "related" resource linkage.
|
|
101
|
-
|
|
102
|
-
Example
|
|
103
|
-
|
|
104
|
-
```javascript
|
|
105
|
-
// models/blog.js
|
|
106
|
-
import Model, { belongsTo } from '@ember-data/model';
|
|
107
|
-
export default Model.extend({
|
|
108
|
-
user: belongsTo({ async: true })
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
let blog = store.push({
|
|
112
|
-
data: {
|
|
113
|
-
type: 'blog',
|
|
114
|
-
id: 1,
|
|
115
|
-
relationships: {
|
|
116
|
-
user: {
|
|
117
|
-
links: {
|
|
118
|
-
related: '/articles/1/author'
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
let userRef = blog.belongsTo('user');
|
|
125
|
-
|
|
126
|
-
// get the identifier of the reference
|
|
127
|
-
if (userRef.remoteType() === "link") {
|
|
128
|
-
let link = userRef.link();
|
|
129
|
-
}
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
@method link
|
|
133
|
-
@public
|
|
134
|
-
@return {String} The link Ember Data will use to fetch or reload this belongs-to relationship.
|
|
135
|
-
*/
|
|
136
|
-
link(): string | null {
|
|
137
|
-
let link;
|
|
138
|
-
let resource = this._resource();
|
|
139
|
-
|
|
140
|
-
if (isResourceIdentiferWithRelatedLinks(resource)) {
|
|
141
|
-
if (resource.links) {
|
|
142
|
-
link = resource.links.related;
|
|
143
|
-
link = !link || typeof link === 'string' ? link : link.href;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
return link || null;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
links(): PaginationLinks | null {
|
|
150
|
-
let resource = this._resource();
|
|
151
|
-
|
|
152
|
-
return resource && resource.links ? resource.links : null;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
The meta data for the belongs-to relationship.
|
|
157
|
-
|
|
158
|
-
Example
|
|
159
|
-
|
|
160
|
-
```javascript
|
|
161
|
-
// models/blog.js
|
|
162
|
-
import Model, { belongsTo } from '@ember-data/model';
|
|
163
|
-
export default Model.extend({
|
|
164
|
-
user: belongsTo({ async: true })
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
let blog = store.push({
|
|
168
|
-
data: {
|
|
169
|
-
type: 'blog',
|
|
170
|
-
id: 1,
|
|
171
|
-
relationships: {
|
|
172
|
-
user: {
|
|
173
|
-
links: {
|
|
174
|
-
related: {
|
|
175
|
-
href: '/articles/1/author'
|
|
176
|
-
},
|
|
177
|
-
},
|
|
178
|
-
meta: {
|
|
179
|
-
lastUpdated: 1458014400000
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
let userRef = blog.belongsTo('user');
|
|
187
|
-
|
|
188
|
-
userRef.meta() // { lastUpdated: 1458014400000 }
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
@method meta
|
|
192
|
-
@public
|
|
193
|
-
@return {Object} The meta information for the belongs-to relationship.
|
|
194
|
-
*/
|
|
195
|
-
meta() {
|
|
196
|
-
let meta: Dict<JSONValue> | null = null;
|
|
197
|
-
let resource = this._resource();
|
|
198
|
-
if (resource && resource.meta && typeof resource.meta === 'object') {
|
|
199
|
-
meta = resource.meta;
|
|
200
|
-
}
|
|
201
|
-
return meta;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
export default Reference;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
@module @ember-data/store
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import BelongsToReference from './references/belongs-to';
|
|
6
|
-
import HasManyReference from './references/has-many';
|
|
7
|
-
import RecordReference from './references/record';
|
|
8
|
-
|
|
9
|
-
export { RecordReference, BelongsToReference, HasManyReference };
|