@warp-drive/legacy 5.8.0-beta.0 → 5.8.0-beta.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/README.md +16 -26
- package/declarations/adapter/error.d.ts +7 -7
- package/declarations/adapter/json-api.d.ts +6 -8
- package/declarations/adapter/rest.d.ts +26 -112
- package/declarations/adapter.d.ts +6 -8
- package/declarations/compat/-private.d.ts +1 -1
- package/declarations/compat/builders/find-all.d.ts +6 -6
- package/declarations/compat/builders/find-record.d.ts +8 -8
- package/declarations/compat/builders/query.d.ts +12 -12
- package/declarations/compat/extensions.d.ts +1 -1
- package/declarations/compat/legacy-network-handler/minimum-adapter-interface.d.ts +7 -9
- package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts +20 -30
- package/declarations/compat/utils.d.ts +17 -17
- package/declarations/compat.d.ts +35 -11
- package/declarations/index.d.ts +70 -0
- package/declarations/model/-private/attr.d.ts +5 -6
- package/declarations/model/-private/belongs-to.d.ts +4 -5
- package/declarations/model/-private/has-many.d.ts +4 -5
- package/declarations/model/-private/hooks.d.ts +1 -1
- package/declarations/model/-private/legacy-relationships-support.d.ts +2 -2
- package/declarations/model/-private/model.d.ts +18 -59
- package/declarations/model/-private/promise-many-array.d.ts +0 -18
- package/declarations/model/-private/record-state.d.ts +1 -1
- package/declarations/model/-private/references/belongs-to.d.ts +19 -29
- package/declarations/model/-private/references/has-many.d.ts +14 -16
- package/declarations/model/migration-support.d.ts +46 -21
- package/declarations/model-fragments/extensions/fragment-array.d.ts +16 -0
- package/declarations/model-fragments/extensions/fragment.d.ts +15 -0
- package/declarations/model-fragments/hooks/model-for.d.ts +20 -0
- package/declarations/model-fragments/index.d.ts +5 -0
- package/declarations/model-fragments/instance-initializers/fragment-extensions.d.ts +9 -0
- package/declarations/model-fragments/utilities/with-array-defaults.d.ts +15 -0
- package/declarations/model-fragments/utilities/with-fragment-array-defaults.d.ts +20 -0
- package/declarations/model-fragments/utilities/with-fragment-defaults.d.ts +19 -0
- package/declarations/model-fragments/utilities/with-legacy.d.ts +3 -0
- package/declarations/model-fragments.d.ts +9 -0
- package/declarations/model.d.ts +2 -2
- package/declarations/serializer/-private/embedded-records-mixin.d.ts +1 -6
- package/declarations/serializer/-private/transforms/boolean.d.ts +2 -2
- package/declarations/serializer/-private/transforms/date.d.ts +2 -2
- package/declarations/serializer/-private/transforms/number.d.ts +1 -1
- package/declarations/serializer/-private/transforms/string.d.ts +1 -1
- package/declarations/serializer/json-api.d.ts +11 -12
- package/declarations/serializer/json.d.ts +9 -11
- package/declarations/serializer/rest.d.ts +4 -6
- package/declarations/serializer.d.ts +9 -12
- package/dist/{-private-8UmnAf9J.js → -private-BG3bMiKp.js} +3 -2
- package/dist/adapter/-private.js +1 -1
- package/dist/adapter/error.js +14 -15
- package/dist/adapter/json-api.js +4 -1
- package/dist/adapter/rest.js +38 -125
- package/dist/adapter.js +6 -8
- package/dist/compat/-private.js +1 -1
- package/dist/compat/builders.js +26 -26
- package/dist/compat/utils.js +17 -18
- package/dist/compat.js +61 -43
- package/dist/{errors-8kD2mSe_.js → errors-Cz5KrzBk.js} +115 -122
- package/dist/hooks-D6diaM34.js +74 -0
- package/dist/index.js +195 -0
- package/dist/{json-Et4mt_LM.js → json-ChdEfB0X.js} +18 -35
- package/dist/model/-private.js +1 -1
- package/dist/model/migration-support.js +59 -27
- package/dist/model-for-CqXsIKws.js +221 -0
- package/dist/model-fragments.js +76 -0
- package/dist/model.js +18 -90
- package/dist/{schema-provider-DQu4Rjco.js → schema-provider-DJCV_6AF.js} +50 -95
- package/dist/{serialize-into-hash-CS0MIv4F.js → serialize-into-hash-DPZYoF-i.js} +1 -1
- package/dist/serializer/json-api.js +18 -45
- package/dist/serializer/json.js +1 -1
- package/dist/serializer/rest.js +14 -21
- package/dist/serializer/transform.js +15 -6
- package/dist/serializer.js +9 -13
- package/dist/store.js +5 -1
- package/dist/unpkg/dev/-private-DtjBbEgy.js +1206 -0
- package/dist/unpkg/dev/adapter/-private.js +1 -0
- package/dist/unpkg/dev/adapter/error.js +335 -0
- package/dist/unpkg/dev/adapter/json-api.js +271 -0
- package/dist/unpkg/dev/adapter/rest.js +1171 -0
- package/dist/unpkg/dev/adapter.js +1252 -0
- package/dist/unpkg/dev/compat/-private.js +1 -0
- package/dist/unpkg/dev/compat/builders.js +275 -0
- package/dist/unpkg/dev/compat/extensions.js +242 -0
- package/dist/unpkg/dev/compat/utils.js +223 -0
- package/dist/unpkg/dev/compat.js +1147 -0
- package/dist/unpkg/dev/errors-DmGGJr3T.js +2562 -0
- package/dist/unpkg/dev/hooks-CkYiE6Ud.js +73 -0
- package/dist/unpkg/dev/index.js +197 -0
- package/dist/unpkg/dev/json-Cu1LNgmQ.js +1256 -0
- package/dist/unpkg/dev/model/-private.js +1 -0
- package/dist/unpkg/dev/model/migration-support.js +553 -0
- package/dist/unpkg/dev/model-for-CqXsIKws.js +221 -0
- package/dist/unpkg/dev/model-fragments.js +76 -0
- package/dist/unpkg/dev/model.js +678 -0
- package/dist/unpkg/dev/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
- package/dist/unpkg/dev/schema-provider-DDVYxmUV.js +2186 -0
- package/dist/unpkg/dev/serialize-into-hash-B2xDbuo5.js +259 -0
- package/dist/unpkg/dev/serializer/json-api.js +649 -0
- package/dist/unpkg/dev/serializer/json.js +4 -0
- package/dist/unpkg/dev/serializer/rest.js +1242 -0
- package/dist/unpkg/dev/serializer/transform.js +278 -0
- package/dist/unpkg/dev/serializer.js +248 -0
- package/dist/unpkg/dev/store.js +637 -0
- package/dist/unpkg/dev/util-DvanW33H.js +20 -0
- package/dist/unpkg/dev/utils-BhvS1iTS.js +8 -0
- package/dist/unpkg/dev-deprecated/-private-DtjBbEgy.js +1206 -0
- package/dist/unpkg/dev-deprecated/adapter/-private.js +1 -0
- package/dist/unpkg/dev-deprecated/adapter/error.js +335 -0
- package/dist/unpkg/dev-deprecated/adapter/json-api.js +271 -0
- package/dist/unpkg/dev-deprecated/adapter/rest.js +1171 -0
- package/dist/unpkg/dev-deprecated/adapter.js +1252 -0
- package/dist/unpkg/dev-deprecated/compat/-private.js +1 -0
- package/dist/unpkg/dev-deprecated/compat/builders.js +275 -0
- package/dist/unpkg/dev-deprecated/compat/extensions.js +242 -0
- package/dist/unpkg/dev-deprecated/compat/utils.js +223 -0
- package/dist/unpkg/dev-deprecated/compat.js +1147 -0
- package/dist/unpkg/dev-deprecated/errors-Spt6ubMd.js +2565 -0
- package/dist/unpkg/dev-deprecated/hooks-DOXegvhL.js +73 -0
- package/dist/unpkg/dev-deprecated/index.js +196 -0
- package/dist/unpkg/dev-deprecated/json-Cu1LNgmQ.js +1256 -0
- package/dist/unpkg/dev-deprecated/model/-private.js +1 -0
- package/dist/unpkg/dev-deprecated/model/migration-support.js +570 -0
- package/dist/unpkg/dev-deprecated/model-for-CqXsIKws.js +221 -0
- package/dist/unpkg/dev-deprecated/model-fragments.js +76 -0
- package/dist/unpkg/dev-deprecated/model.js +682 -0
- package/dist/unpkg/dev-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
- package/dist/unpkg/dev-deprecated/schema-provider-BP6_8N-V.js +2211 -0
- package/dist/unpkg/dev-deprecated/serialize-into-hash-B2xDbuo5.js +259 -0
- package/dist/unpkg/dev-deprecated/serializer/json-api.js +649 -0
- package/dist/unpkg/dev-deprecated/serializer/json.js +4 -0
- package/dist/unpkg/dev-deprecated/serializer/rest.js +1242 -0
- package/dist/unpkg/dev-deprecated/serializer/transform.js +278 -0
- package/dist/unpkg/dev-deprecated/serializer.js +248 -0
- package/dist/unpkg/dev-deprecated/store.js +637 -0
- package/dist/unpkg/dev-deprecated/util-CWr5WQOT.js +24 -0
- package/dist/unpkg/dev-deprecated/utils-C9PJehtL.js +12 -0
- package/dist/unpkg/prod/-private-BdyZaGEh.js +971 -0
- package/dist/unpkg/prod/adapter/-private.js +1 -0
- package/dist/unpkg/prod/adapter/error.js +330 -0
- package/dist/unpkg/prod/adapter/json-api.js +266 -0
- package/dist/unpkg/prod/adapter/rest.js +1134 -0
- package/dist/unpkg/prod/adapter.js +1219 -0
- package/dist/unpkg/prod/compat/-private.js +1 -0
- package/dist/unpkg/prod/compat/builders.js +210 -0
- package/dist/unpkg/prod/compat/extensions.js +232 -0
- package/dist/unpkg/prod/compat/utils.js +218 -0
- package/dist/unpkg/prod/compat.js +727 -0
- package/dist/unpkg/prod/errors-BGVFCBmi.js +2314 -0
- package/dist/unpkg/prod/hooks-BztVA_x0.js +41 -0
- package/dist/unpkg/prod/index.js +151 -0
- package/dist/unpkg/prod/json-BWrZ5546.js +1243 -0
- package/dist/unpkg/prod/model/-private.js +1 -0
- package/dist/unpkg/prod/model/migration-support.js +546 -0
- package/dist/unpkg/prod/model-for-CqXsIKws.js +221 -0
- package/dist/unpkg/prod/model-fragments.js +76 -0
- package/dist/unpkg/prod/model.js +593 -0
- package/dist/unpkg/prod/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
- package/dist/unpkg/prod/schema-provider-DJtD_8jZ.js +1861 -0
- package/dist/unpkg/prod/serialize-into-hash-DGlzQteF.js +215 -0
- package/dist/unpkg/prod/serializer/json-api.js +592 -0
- package/dist/unpkg/prod/serializer/json.js +4 -0
- package/dist/unpkg/prod/serializer/rest.js +1210 -0
- package/dist/unpkg/prod/serializer/transform.js +278 -0
- package/dist/unpkg/prod/serializer.js +248 -0
- package/dist/unpkg/prod/store.js +505 -0
- package/dist/unpkg/prod/util-DvanW33H.js +20 -0
- package/dist/unpkg/prod/utils-BhvS1iTS.js +8 -0
- package/dist/unpkg/prod-deprecated/-private-BdyZaGEh.js +971 -0
- package/dist/unpkg/prod-deprecated/adapter/-private.js +1 -0
- package/dist/unpkg/prod-deprecated/adapter/error.js +330 -0
- package/dist/unpkg/prod-deprecated/adapter/json-api.js +266 -0
- package/dist/unpkg/prod-deprecated/adapter/rest.js +1134 -0
- package/dist/unpkg/prod-deprecated/adapter.js +1219 -0
- package/dist/unpkg/prod-deprecated/compat/-private.js +1 -0
- package/dist/unpkg/prod-deprecated/compat/builders.js +210 -0
- package/dist/unpkg/prod-deprecated/compat/extensions.js +232 -0
- package/dist/unpkg/prod-deprecated/compat/utils.js +218 -0
- package/dist/unpkg/prod-deprecated/compat.js +727 -0
- package/dist/unpkg/prod-deprecated/errors-CdDaK81x.js +2317 -0
- package/dist/unpkg/prod-deprecated/hooks-yId87yyG.js +41 -0
- package/dist/unpkg/prod-deprecated/index.js +150 -0
- package/dist/unpkg/prod-deprecated/json-BWrZ5546.js +1243 -0
- package/dist/unpkg/prod-deprecated/model/-private.js +1 -0
- package/dist/unpkg/prod-deprecated/model/migration-support.js +563 -0
- package/dist/unpkg/prod-deprecated/model-for-CqXsIKws.js +221 -0
- package/dist/unpkg/prod-deprecated/model-fragments.js +76 -0
- package/dist/unpkg/prod-deprecated/model.js +596 -0
- package/dist/unpkg/prod-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
- package/dist/unpkg/prod-deprecated/schema-provider-CjX55uSY.js +1904 -0
- package/dist/unpkg/prod-deprecated/serialize-into-hash-DGlzQteF.js +215 -0
- package/dist/unpkg/prod-deprecated/serializer/json-api.js +592 -0
- package/dist/unpkg/prod-deprecated/serializer/json.js +4 -0
- package/dist/unpkg/prod-deprecated/serializer/rest.js +1210 -0
- package/dist/unpkg/prod-deprecated/serializer/transform.js +278 -0
- package/dist/unpkg/prod-deprecated/serializer.js +248 -0
- package/dist/unpkg/prod-deprecated/store.js +505 -0
- package/dist/unpkg/prod-deprecated/util-B6cn-i93.js +23 -0
- package/dist/unpkg/prod-deprecated/utils-BUWwQwCh.js +11 -0
- package/logos/README.md +2 -2
- package/logos/logo-yellow-slab.svg +1 -0
- package/logos/word-mark-black.svg +1 -0
- package/logos/word-mark-white.svg +1 -0
- package/package.json +15 -7
- package/logos/NCC-1701-a-blue.svg +0 -4
- package/logos/NCC-1701-a-gold.svg +0 -4
- package/logos/NCC-1701-a-gold_100.svg +0 -1
- package/logos/NCC-1701-a-gold_base-64.txt +0 -1
- package/logos/NCC-1701-a.svg +0 -4
- package/logos/docs-badge.svg +0 -2
- package/logos/ember-data-logo-dark.svg +0 -12
- package/logos/ember-data-logo-light.svg +0 -12
- package/logos/social1.png +0 -0
- package/logos/social2.png +0 -0
- package/logos/warp-drive-logo-dark.svg +0 -4
- package/logos/warp-drive-logo-gold.svg +0 -4
|
@@ -0,0 +1,1206 @@
|
|
|
1
|
+
import { Context } from '@warp-drive/core/reactive/-private';
|
|
2
|
+
import { createDeferred } from '@warp-drive/core/request';
|
|
3
|
+
import { waitFor } from '@warp-drive/core/signals/-leaked';
|
|
4
|
+
import { assertPrivateStore, coerceId } from '@warp-drive/core/store/-private';
|
|
5
|
+
import { getOrSetGlobal } from '@warp-drive/core/types/-private';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
SnapshotRecordArray is not directly instantiable.
|
|
9
|
+
Instances are provided to consuming application's
|
|
10
|
+
adapters for certain `findAll` requests.
|
|
11
|
+
|
|
12
|
+
@hideconstructor
|
|
13
|
+
@public
|
|
14
|
+
*/
|
|
15
|
+
class SnapshotRecordArray {
|
|
16
|
+
/**
|
|
17
|
+
* An array of snapshots
|
|
18
|
+
*
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/** @internal */
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The ResourceType of the underlying records for the {@link Snapshot | Snapshots} in the array
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/** @internal */
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A hash of adapter options passed into the store method for this request.
|
|
32
|
+
Example
|
|
33
|
+
```js [app/adapters/post.js]
|
|
34
|
+
import MyCustomAdapter from './custom-adapter';
|
|
35
|
+
export default class PostAdapter extends MyCustomAdapter {
|
|
36
|
+
findAll(store, type, sinceToken, snapshotRecordArray) {
|
|
37
|
+
if (snapshotRecordArray.adapterOptions.subscribe) {
|
|
38
|
+
// ...
|
|
39
|
+
}
|
|
40
|
+
// ...
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The relationships to include for this request.
|
|
48
|
+
Example
|
|
49
|
+
```js [app/adapters/application.js]
|
|
50
|
+
import Adapter from '@ember-data/adapter';
|
|
51
|
+
export default class ApplicationAdapter extends Adapter {
|
|
52
|
+
findAll(store, type, snapshotRecordArray) {
|
|
53
|
+
let url = `/${type.modelName}?include=${encodeURIComponent(snapshotRecordArray.include)}`;
|
|
54
|
+
return fetch(url).then((response) => response.json())
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
SnapshotRecordArray is not directly instantiable.
|
|
62
|
+
Instances are provided to consuming application's
|
|
63
|
+
adapters and serializers for certain requests.
|
|
64
|
+
@private
|
|
65
|
+
@constructor
|
|
66
|
+
@param {Store} store
|
|
67
|
+
@param {String} type
|
|
68
|
+
@param options
|
|
69
|
+
*/
|
|
70
|
+
constructor(store, type, options = {}) {
|
|
71
|
+
this.__store = store;
|
|
72
|
+
this._snapshots = null;
|
|
73
|
+
this.modelName = type;
|
|
74
|
+
this.adapterOptions = options.adapterOptions;
|
|
75
|
+
this.include = options.include;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
An array of records
|
|
80
|
+
@internal
|
|
81
|
+
*/
|
|
82
|
+
get _recordArray() {
|
|
83
|
+
return this.__store.peekAll(this.modelName);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
Number of records in the array
|
|
88
|
+
Example
|
|
89
|
+
```js [app/adapters/post.js]
|
|
90
|
+
import JSONAPIAdapter from '@ember-data/adapter/json-api';
|
|
91
|
+
export default class PostAdapter extends JSONAPIAdapter {
|
|
92
|
+
shouldReloadAll(store, snapshotRecordArray) {
|
|
93
|
+
return !snapshotRecordArray.length;
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
*/
|
|
98
|
+
get length() {
|
|
99
|
+
return this._recordArray.length;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
Get snapshots of the underlying record array
|
|
104
|
+
Example
|
|
105
|
+
```js [app/adapters/post.js]
|
|
106
|
+
import JSONAPIAdapter from '@ember-data/adapter/json-api';
|
|
107
|
+
export default class PostAdapter extends JSONAPIAdapter {
|
|
108
|
+
shouldReloadAll(store, snapshotArray) {
|
|
109
|
+
let snapshots = snapshotArray.snapshots();
|
|
110
|
+
return snapshots.any(function(ticketSnapshot) {
|
|
111
|
+
let timeDiff = moment().diff(ticketSnapshot.attr('lastAccessedAt'), 'minutes');
|
|
112
|
+
if (timeDiff > 20) {
|
|
113
|
+
return true;
|
|
114
|
+
} else {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
@public
|
|
122
|
+
@return Array of snapshots
|
|
123
|
+
*/
|
|
124
|
+
snapshots() {
|
|
125
|
+
if (this._snapshots !== null) {
|
|
126
|
+
return this._snapshots;
|
|
127
|
+
}
|
|
128
|
+
upgradeStore(this.__store);
|
|
129
|
+
const {
|
|
130
|
+
_fetchManager
|
|
131
|
+
} = this.__store;
|
|
132
|
+
const LiveArrayContext = this._recordArray[Context];
|
|
133
|
+
this._snapshots = LiveArrayContext.source.map(identifier => _fetchManager.createSnapshot(identifier));
|
|
134
|
+
return this._snapshots;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
function assertIdentifierHasId(identifier) {
|
|
138
|
+
(test => {
|
|
139
|
+
if (!test) {
|
|
140
|
+
throw new Error(`Attempted to schedule a fetch for a record without an id.`);
|
|
141
|
+
}
|
|
142
|
+
})(identifier && identifier.id !== null);
|
|
143
|
+
}
|
|
144
|
+
function iterateData(data, fn) {
|
|
145
|
+
if (Array.isArray(data)) {
|
|
146
|
+
return data.map(fn);
|
|
147
|
+
} else {
|
|
148
|
+
return fn(data, 0);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
function payloadIsNotBlank(adapterPayload) {
|
|
152
|
+
if (Array.isArray(adapterPayload)) {
|
|
153
|
+
return true;
|
|
154
|
+
} else {
|
|
155
|
+
return Object.keys(adapterPayload || {}).length !== 0;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
This is a helper method that validates a JSON API top-level document
|
|
161
|
+
|
|
162
|
+
The format of a document is described here:
|
|
163
|
+
http://jsonapi.org/format/#document-top-level
|
|
164
|
+
|
|
165
|
+
@internal
|
|
166
|
+
*/
|
|
167
|
+
function validateDocumentStructure(doc) {
|
|
168
|
+
{
|
|
169
|
+
const errors = [];
|
|
170
|
+
if (!doc || typeof doc !== 'object') {
|
|
171
|
+
errors.push('Top level of a JSON API document must be an object');
|
|
172
|
+
} else {
|
|
173
|
+
if (!('data' in doc) && !('errors' in doc) && !('meta' in doc)) {
|
|
174
|
+
errors.push('One or more of the following keys must be present: "data", "errors", "meta".');
|
|
175
|
+
} else {
|
|
176
|
+
if ('data' in doc && 'errors' in doc) {
|
|
177
|
+
errors.push('Top level keys "errors" and "data" cannot both be present in a JSON API document');
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if ('data' in doc) {
|
|
181
|
+
if (!(doc.data === null || Array.isArray(doc.data) || typeof doc.data === 'object')) {
|
|
182
|
+
errors.push('data must be null, an object, or an array');
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if ('meta' in doc) {
|
|
186
|
+
if (typeof doc.meta !== 'object') {
|
|
187
|
+
errors.push('meta must be an object');
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if ('errors' in doc) {
|
|
191
|
+
if (!Array.isArray(doc.errors)) {
|
|
192
|
+
errors.push('errors must be an array');
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if ('links' in doc) {
|
|
196
|
+
if (typeof doc.links !== 'object') {
|
|
197
|
+
errors.push('links must be an object');
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
if ('jsonapi' in doc) {
|
|
201
|
+
if (typeof doc.jsonapi !== 'object') {
|
|
202
|
+
errors.push('jsonapi must be an object');
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if ('included' in doc) {
|
|
206
|
+
if (typeof doc.included !== 'object') {
|
|
207
|
+
errors.push('included must be an array');
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
(test => {
|
|
212
|
+
if (!test) {
|
|
213
|
+
throw new Error(`Response must be normalized to a valid JSON API document:\n\t* ${errors.join('\n\t* ')}`);
|
|
214
|
+
}
|
|
215
|
+
})(errors.length === 0);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
function normalizeResponseHelper(serializer, store, modelClass, payload, id, requestType) {
|
|
219
|
+
const normalizedResponse = serializer ? serializer.normalizeResponse(store, modelClass, payload, id, requestType) : payload;
|
|
220
|
+
validateDocumentStructure(normalizedResponse);
|
|
221
|
+
return normalizedResponse;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
Snapshot is not directly instantiable.
|
|
226
|
+
Instances are provided to a consuming application's
|
|
227
|
+
adapters and serializers for certain requests.
|
|
228
|
+
|
|
229
|
+
Snapshots are only available when using `@ember-data/legacy-compat`
|
|
230
|
+
for legacy compatibility with adapters and serializers.
|
|
231
|
+
|
|
232
|
+
For serialization of records in modern paradigms, request data from
|
|
233
|
+
the cache or off the record directly.
|
|
234
|
+
|
|
235
|
+
@hideconstructor
|
|
236
|
+
@public
|
|
237
|
+
*/
|
|
238
|
+
class Snapshot {
|
|
239
|
+
/** @internal */
|
|
240
|
+
|
|
241
|
+
/** @internal */
|
|
242
|
+
|
|
243
|
+
/** @internal */
|
|
244
|
+
|
|
245
|
+
/** @internal */
|
|
246
|
+
|
|
247
|
+
/** @internal */
|
|
248
|
+
|
|
249
|
+
/** @internal */
|
|
250
|
+
|
|
251
|
+
/** @internal */
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
The unique ResourceKey associated with this Snapshot.
|
|
255
|
+
@public
|
|
256
|
+
*/
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
The ResourceType of the underlying record for this Snapshot, as a string.
|
|
260
|
+
@public
|
|
261
|
+
*/
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
The id of the snapshot's underlying record
|
|
265
|
+
Example
|
|
266
|
+
```js
|
|
267
|
+
// store.push('post', { id: 1, author: 'Tomster', title: 'Ember.js rocks' });
|
|
268
|
+
postSnapshot.id; // => '1'
|
|
269
|
+
```
|
|
270
|
+
@public
|
|
271
|
+
*/
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
If `include` was passed to the options for the request, the value
|
|
275
|
+
would be available here.
|
|
276
|
+
@public
|
|
277
|
+
*/
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
The adapterOptions passed to the request which generated this Snapshot, if any
|
|
281
|
+
@public
|
|
282
|
+
*/
|
|
283
|
+
|
|
284
|
+
constructor(options, identifier, store) {
|
|
285
|
+
this._store = store;
|
|
286
|
+
this.__attributes = null;
|
|
287
|
+
this._belongsToRelationships = Object.create(null);
|
|
288
|
+
this._belongsToIds = Object.create(null);
|
|
289
|
+
this._hasManyRelationships = Object.create(null);
|
|
290
|
+
this._hasManyIds = Object.create(null);
|
|
291
|
+
assertPrivateStore(store);
|
|
292
|
+
const hasRecord = !!store._instanceCache.peek(identifier);
|
|
293
|
+
this.modelName = identifier.type;
|
|
294
|
+
this.identifier = identifier;
|
|
295
|
+
|
|
296
|
+
/*
|
|
297
|
+
If the we do not yet have a record, then we are
|
|
298
|
+
likely a snapshot being provided to a find request, so we
|
|
299
|
+
populate __attributes lazily. Else, to preserve the "moment
|
|
300
|
+
in time" in which a snapshot is created, we greedily grab
|
|
301
|
+
the values.
|
|
302
|
+
*/
|
|
303
|
+
if (hasRecord) {
|
|
304
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
305
|
+
this._attributes;
|
|
306
|
+
}
|
|
307
|
+
this.id = identifier.id;
|
|
308
|
+
this.adapterOptions = options.adapterOptions;
|
|
309
|
+
this.include = options.include;
|
|
310
|
+
this.modelName = identifier.type;
|
|
311
|
+
if (hasRecord) {
|
|
312
|
+
const cache = this._store.cache;
|
|
313
|
+
this._changedAttributes = cache.changedAttrs(identifier);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
The underlying record for this snapshot. Can be used to access methods and
|
|
319
|
+
properties defined on the record.
|
|
320
|
+
```js
|
|
321
|
+
const someValue = snapshot.record.someProp;
|
|
322
|
+
```
|
|
323
|
+
@property record
|
|
324
|
+
@public
|
|
325
|
+
*/
|
|
326
|
+
get record() {
|
|
327
|
+
const record = this._store.peekRecord(this.identifier);
|
|
328
|
+
(test => {
|
|
329
|
+
if (!test) {
|
|
330
|
+
throw new Error(`Record ${this.identifier.type} ${this.identifier.id} (${this.identifier.lid}) is not yet loaded and thus cannot be accessed from the Snapshot during serialization`);
|
|
331
|
+
}
|
|
332
|
+
})(record !== null);
|
|
333
|
+
return record;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** @internal */
|
|
337
|
+
get _attributes() {
|
|
338
|
+
if (this.__attributes !== null) {
|
|
339
|
+
return this.__attributes;
|
|
340
|
+
}
|
|
341
|
+
const attributes = this.__attributes = Object.create(null);
|
|
342
|
+
const {
|
|
343
|
+
identifier
|
|
344
|
+
} = this;
|
|
345
|
+
const cache = this._store.cache;
|
|
346
|
+
this.eachAttribute((key, meta) => {
|
|
347
|
+
attributes[key] = cache.getAttr(identifier, key);
|
|
348
|
+
});
|
|
349
|
+
return attributes;
|
|
350
|
+
}
|
|
351
|
+
get isNew() {
|
|
352
|
+
const cache = this._store.cache;
|
|
353
|
+
return cache?.isNew(this.identifier) || false;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
Returns the value of an attribute.
|
|
358
|
+
Example
|
|
359
|
+
```javascript
|
|
360
|
+
// store.push('post', { id: 1, author: 'Tomster', title: 'Ember.js rocks' });
|
|
361
|
+
postSnapshot.attr('author'); // => 'Tomster'
|
|
362
|
+
postSnapshot.attr('title'); // => 'Ember.js rocks'
|
|
363
|
+
```
|
|
364
|
+
Note: Values are loaded eagerly and cached when the snapshot is created.
|
|
365
|
+
@return The attribute value or undefined
|
|
366
|
+
@public
|
|
367
|
+
*/
|
|
368
|
+
attr(keyName) {
|
|
369
|
+
if (keyName in this._attributes) {
|
|
370
|
+
return this._attributes[keyName];
|
|
371
|
+
}
|
|
372
|
+
(test => {
|
|
373
|
+
{
|
|
374
|
+
throw new Error(`Model '${this.identifier.lid}' has no attribute named '${keyName}' defined.`);
|
|
375
|
+
}
|
|
376
|
+
})();
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
Returns all attributes and their corresponding values.
|
|
381
|
+
::: warning ⚠️ WARNING
|
|
382
|
+
Attributes are SHALLOW copied from the cache.
|
|
383
|
+
Because they are NOT deep copied from the cache, mutating
|
|
384
|
+
any object or array fields will cause unintended side-effects
|
|
385
|
+
and bugs.
|
|
386
|
+
:::
|
|
387
|
+
Example
|
|
388
|
+
```js
|
|
389
|
+
// store.push('post', { id: 1, author: 'Tomster', title: 'Ember.js rocks' });
|
|
390
|
+
postSnapshot.attributes(); // => { author: 'Tomster', title: 'Ember.js rocks' }
|
|
391
|
+
```
|
|
392
|
+
@return All attributes of the current snapshot
|
|
393
|
+
@public
|
|
394
|
+
*/
|
|
395
|
+
attributes() {
|
|
396
|
+
return {
|
|
397
|
+
...this._attributes
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
Returns all changed attributes and their old and new values.
|
|
403
|
+
Example
|
|
404
|
+
```js
|
|
405
|
+
// store.push('post', { id: 1, author: 'Tomster', title: 'Ember.js rocks' });
|
|
406
|
+
postModel.set('title', 'Ember.js rocks!');
|
|
407
|
+
postSnapshot.changedAttributes(); // => { title: ['Ember.js rocks', 'Ember.js rocks!'] }
|
|
408
|
+
```
|
|
409
|
+
@return All changed attributes of the current snapshot
|
|
410
|
+
@public
|
|
411
|
+
*/
|
|
412
|
+
changedAttributes() {
|
|
413
|
+
const changedAttributes = Object.create(null);
|
|
414
|
+
if (!this._changedAttributes) {
|
|
415
|
+
return changedAttributes;
|
|
416
|
+
}
|
|
417
|
+
const changedAttributeKeys = Object.keys(this._changedAttributes);
|
|
418
|
+
for (let i = 0, length = changedAttributeKeys.length; i < length; i++) {
|
|
419
|
+
const key = changedAttributeKeys[i];
|
|
420
|
+
changedAttributes[key] = this._changedAttributes[key].slice();
|
|
421
|
+
}
|
|
422
|
+
return changedAttributes;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
Returns the current value of a belongsTo relationship.
|
|
427
|
+
`belongsTo` takes an optional hash of options as a second parameter,
|
|
428
|
+
currently supported options are:
|
|
429
|
+
- `id`: set to `true` if you only want the ID of the related record to be
|
|
430
|
+
returned.
|
|
431
|
+
Example
|
|
432
|
+
```js
|
|
433
|
+
// store.push('post', { id: 1, title: 'Hello World' });
|
|
434
|
+
// store.createRecord('comment', { body: 'Lorem ipsum', post: post });
|
|
435
|
+
commentSnapshot.belongsTo('post'); // => Snapshot
|
|
436
|
+
commentSnapshot.belongsTo('post', { id: true }); // => '1'
|
|
437
|
+
// store.push('comment', { id: 1, body: 'Lorem ipsum' });
|
|
438
|
+
commentSnapshot.belongsTo('post'); // => undefined
|
|
439
|
+
```
|
|
440
|
+
Calling `belongsTo` will return a new Snapshot as long as there's any known
|
|
441
|
+
data for the relationship available, such as an ID. If the relationship is
|
|
442
|
+
known but unset, `belongsTo` will return `null`. If the contents of the
|
|
443
|
+
relationship is unknown `belongsTo` will return `undefined`.
|
|
444
|
+
Note: Relationships are loaded lazily and cached upon first access.
|
|
445
|
+
@public
|
|
446
|
+
@return A snapshot or ID of a known relationship or null if the
|
|
447
|
+
relationship is known but unset. undefined will be returned if the
|
|
448
|
+
contents of the relationship are unknown.
|
|
449
|
+
*/
|
|
450
|
+
belongsTo(keyName, options) {
|
|
451
|
+
const returnModeIsId = !!(options && options.id);
|
|
452
|
+
let result;
|
|
453
|
+
const store = this._store;
|
|
454
|
+
if (returnModeIsId === true && keyName in this._belongsToIds) {
|
|
455
|
+
return this._belongsToIds[keyName];
|
|
456
|
+
}
|
|
457
|
+
if (returnModeIsId === false && keyName in this._belongsToRelationships) {
|
|
458
|
+
return this._belongsToRelationships[keyName];
|
|
459
|
+
}
|
|
460
|
+
const relationshipMeta = store.schema.fields({
|
|
461
|
+
type: this.modelName
|
|
462
|
+
}).get(keyName);
|
|
463
|
+
(test => {
|
|
464
|
+
if (!test) {
|
|
465
|
+
throw new Error(`Model '${this.identifier.lid}' has no belongsTo relationship named '${keyName}' defined.`);
|
|
466
|
+
}
|
|
467
|
+
})(relationshipMeta && relationshipMeta.kind === 'belongsTo');
|
|
468
|
+
(test => {
|
|
469
|
+
if (!test) {
|
|
470
|
+
throw new Error(`snapshot.belongsTo only supported when using a cache that supports the graph`);
|
|
471
|
+
}
|
|
472
|
+
})(this._store._graph);
|
|
473
|
+
const {
|
|
474
|
+
identifier
|
|
475
|
+
} = this;
|
|
476
|
+
{
|
|
477
|
+
const relationship = this._store.schema.fields(identifier)?.get(keyName);
|
|
478
|
+
(test => {
|
|
479
|
+
if (!test) {
|
|
480
|
+
throw new Error(`You looked up the ${keyName} belongsTo relationship for { type: ${identifier.type}, id: ${identifier.id || ''}, lid: ${identifier.lid} but no such relationship was found.`);
|
|
481
|
+
}
|
|
482
|
+
})(relationship);
|
|
483
|
+
(test => {
|
|
484
|
+
if (!test) {
|
|
485
|
+
throw new Error(`You looked up the ${keyName} belongsTo relationship for { type: ${identifier.type}, id: ${identifier.id || ''}, lid: ${identifier.lid} but that relationship is a hasMany.`);
|
|
486
|
+
}
|
|
487
|
+
})(relationship.kind === 'belongsTo');
|
|
488
|
+
}
|
|
489
|
+
const value = this._store.cache.getRelationship(identifier, keyName);
|
|
490
|
+
const data = value && value.data;
|
|
491
|
+
const inverseIdentifier = data ? store.cacheKeyManager.getOrCreateRecordIdentifier(data) : null;
|
|
492
|
+
if (value && value.data !== undefined) {
|
|
493
|
+
const cache = store.cache;
|
|
494
|
+
if (inverseIdentifier && !cache.isDeleted(inverseIdentifier)) {
|
|
495
|
+
if (returnModeIsId) {
|
|
496
|
+
result = inverseIdentifier.id;
|
|
497
|
+
} else {
|
|
498
|
+
result = store._fetchManager.createSnapshot(inverseIdentifier);
|
|
499
|
+
}
|
|
500
|
+
} else {
|
|
501
|
+
result = null;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
if (returnModeIsId) {
|
|
505
|
+
this._belongsToIds[keyName] = result;
|
|
506
|
+
} else {
|
|
507
|
+
this._belongsToRelationships[keyName] = result;
|
|
508
|
+
}
|
|
509
|
+
return result;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
Returns the current value of a hasMany relationship.
|
|
514
|
+
`hasMany` takes an optional hash of options as a second parameter,
|
|
515
|
+
currently supported options are:
|
|
516
|
+
- `ids`: set to `true` if you only want the IDs of the related records to be
|
|
517
|
+
returned.
|
|
518
|
+
Example
|
|
519
|
+
```javascript
|
|
520
|
+
// store.push('post', { id: 1, title: 'Hello World', comments: [2, 3] });
|
|
521
|
+
postSnapshot.hasMany('comments'); // => [Snapshot, Snapshot]
|
|
522
|
+
postSnapshot.hasMany('comments', { ids: true }); // => ['2', '3']
|
|
523
|
+
// store.push('post', { id: 1, title: 'Hello World' });
|
|
524
|
+
postSnapshot.hasMany('comments'); // => undefined
|
|
525
|
+
```
|
|
526
|
+
Note: Relationships are loaded lazily and cached upon first access.
|
|
527
|
+
@public
|
|
528
|
+
@return An array of snapshots or IDs of a known
|
|
529
|
+
relationship or an empty array if the relationship is known but unset.
|
|
530
|
+
undefined will be returned if the contents of the relationship is unknown.
|
|
531
|
+
*/
|
|
532
|
+
hasMany(keyName, options) {
|
|
533
|
+
const returnModeIsIds = !!(options && options.ids);
|
|
534
|
+
let results;
|
|
535
|
+
const cachedIds = this._hasManyIds[keyName];
|
|
536
|
+
const cachedSnapshots = this._hasManyRelationships[keyName];
|
|
537
|
+
if (returnModeIsIds === true && keyName in this._hasManyIds) {
|
|
538
|
+
return cachedIds;
|
|
539
|
+
}
|
|
540
|
+
if (returnModeIsIds === false && keyName in this._hasManyRelationships) {
|
|
541
|
+
return cachedSnapshots;
|
|
542
|
+
}
|
|
543
|
+
const store = this._store;
|
|
544
|
+
const relationshipMeta = store.schema.fields({
|
|
545
|
+
type: this.modelName
|
|
546
|
+
}).get(keyName);
|
|
547
|
+
(test => {
|
|
548
|
+
if (!test) {
|
|
549
|
+
throw new Error(`Model '${this.identifier.lid}' has no hasMany relationship named '${keyName}' defined.`);
|
|
550
|
+
}
|
|
551
|
+
})(relationshipMeta && relationshipMeta.kind === 'hasMany');
|
|
552
|
+
|
|
553
|
+
// TODO @runspired it seems this code branch would not work with CUSTOM_MODEL_CLASSes
|
|
554
|
+
// this check is not a regression in behavior because relationships don't currently
|
|
555
|
+
// function without access to intimate API contracts between RecordData and Model.
|
|
556
|
+
// This is a requirement we should fix as soon as the relationship layer does not require
|
|
557
|
+
// this intimate API usage.
|
|
558
|
+
(test => {
|
|
559
|
+
if (!test) {
|
|
560
|
+
throw new Error(`snapshot.hasMany only supported when using a cache that supports the graph`);
|
|
561
|
+
}
|
|
562
|
+
})(this._store._graph);
|
|
563
|
+
const {
|
|
564
|
+
identifier
|
|
565
|
+
} = this;
|
|
566
|
+
{
|
|
567
|
+
const relationship = this._store.schema.fields(identifier)?.get(keyName);
|
|
568
|
+
(test => {
|
|
569
|
+
if (!test) {
|
|
570
|
+
throw new Error(`You looked up the ${keyName} hasMany relationship for { type: ${identifier.type}, id: ${identifier.id || ''}, lid: ${identifier.lid} but no such relationship was found.`);
|
|
571
|
+
}
|
|
572
|
+
})(relationship);
|
|
573
|
+
(test => {
|
|
574
|
+
if (!test) {
|
|
575
|
+
throw new Error(`You looked up the ${keyName} hasMany relationship for { type: ${identifier.type}, id: ${identifier.id || ''}, lid: ${identifier.lid} but that relationship is a belongsTo.`);
|
|
576
|
+
}
|
|
577
|
+
})(relationship.kind === 'hasMany');
|
|
578
|
+
}
|
|
579
|
+
const value = this._store.cache.getRelationship(identifier, keyName);
|
|
580
|
+
if (value.data) {
|
|
581
|
+
results = [];
|
|
582
|
+
value.data.forEach(member => {
|
|
583
|
+
const inverseIdentifier = store.cacheKeyManager.getOrCreateRecordIdentifier(member);
|
|
584
|
+
const cache = store.cache;
|
|
585
|
+
if (!cache.isDeleted(inverseIdentifier)) {
|
|
586
|
+
if (returnModeIsIds) {
|
|
587
|
+
results.push(inverseIdentifier.id);
|
|
588
|
+
} else {
|
|
589
|
+
results.push(store._fetchManager.createSnapshot(inverseIdentifier));
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
// we assign even if `undefined` so that we don't reprocess the relationship
|
|
596
|
+
// on next access. This works with the `keyName in` checks above.
|
|
597
|
+
if (returnModeIsIds) {
|
|
598
|
+
this._hasManyIds[keyName] = results;
|
|
599
|
+
} else {
|
|
600
|
+
this._hasManyRelationships[keyName] = results;
|
|
601
|
+
}
|
|
602
|
+
return results;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/**
|
|
606
|
+
Iterates through all the attributes of the model, calling the passed
|
|
607
|
+
function on each attribute.
|
|
608
|
+
Example
|
|
609
|
+
```javascript
|
|
610
|
+
snapshot.eachAttribute(function(name, meta) {
|
|
611
|
+
// ...
|
|
612
|
+
});
|
|
613
|
+
```
|
|
614
|
+
@param callback the callback to execute
|
|
615
|
+
@param binding the optional value to which the callback's `this` should be bound
|
|
616
|
+
@public
|
|
617
|
+
*/
|
|
618
|
+
eachAttribute(callback, binding) {
|
|
619
|
+
// if the store has a modelFor implementation, we use it to iterate attributes. This allows
|
|
620
|
+
// a custom "ModelSchema" class for legacy serializers to adapt to new fields if desired.
|
|
621
|
+
if (typeof this._store.modelFor === 'function') {
|
|
622
|
+
const modelSchema = this._store.modelFor(this.identifier.type);
|
|
623
|
+
modelSchema.eachAttribute(callback, binding);
|
|
624
|
+
} else {
|
|
625
|
+
const fields = this._store.schema.fields(this.identifier);
|
|
626
|
+
fields.forEach((field, key) => {
|
|
627
|
+
if (field.kind === 'attribute') {
|
|
628
|
+
callback.call(binding, key, field);
|
|
629
|
+
}
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
Iterates through all the relationships of the model, calling the passed
|
|
636
|
+
function on each relationship.
|
|
637
|
+
Example
|
|
638
|
+
```javascript
|
|
639
|
+
snapshot.eachRelationship(function(name, relationship) {
|
|
640
|
+
// ...
|
|
641
|
+
});
|
|
642
|
+
```
|
|
643
|
+
@param callback the callback to execute
|
|
644
|
+
@param binding the optional value to which the callback's `this` should be bound
|
|
645
|
+
@public
|
|
646
|
+
*/
|
|
647
|
+
eachRelationship(callback, binding) {
|
|
648
|
+
// if the store has a modelFor implementation, we use it to iterate relationships. This allows
|
|
649
|
+
// a custom "ModelSchema" class for legacy serializers to adapt to new fields if desired.
|
|
650
|
+
if (typeof this._store.modelFor === 'function') {
|
|
651
|
+
const modelSchema = this._store.modelFor(this.identifier.type);
|
|
652
|
+
modelSchema.eachRelationship(callback, binding);
|
|
653
|
+
} else {
|
|
654
|
+
const fields = this._store.schema.fields(this.identifier);
|
|
655
|
+
fields.forEach((field, key) => {
|
|
656
|
+
if (field.kind === 'belongsTo' || field.kind === 'hasMany') {
|
|
657
|
+
callback.call(binding, key, field);
|
|
658
|
+
}
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
/**
|
|
664
|
+
Serializes the snapshot using the serializer for the model.
|
|
665
|
+
Example
|
|
666
|
+
```js [app/adapters/application.js]
|
|
667
|
+
import Adapter from '@ember-data/adapter';
|
|
668
|
+
export default Adapter.extend({
|
|
669
|
+
createRecord(store, type, snapshot) {
|
|
670
|
+
let data = snapshot.serialize({ includeId: true });
|
|
671
|
+
let url = `/${type.modelName}`;
|
|
672
|
+
return fetch(url, {
|
|
673
|
+
method: 'POST',
|
|
674
|
+
body: data,
|
|
675
|
+
}).then((response) => response.json())
|
|
676
|
+
}
|
|
677
|
+
});
|
|
678
|
+
```
|
|
679
|
+
@return an object whose values are primitive JSON values only
|
|
680
|
+
@public
|
|
681
|
+
*/
|
|
682
|
+
serialize(options) {
|
|
683
|
+
upgradeStore(this._store);
|
|
684
|
+
const serializer = this._store.serializerFor(this.modelName);
|
|
685
|
+
(test => {
|
|
686
|
+
if (!test) {
|
|
687
|
+
throw new Error(`Cannot serialize record, no serializer found`);
|
|
688
|
+
}
|
|
689
|
+
})(serializer);
|
|
690
|
+
return serializer.serialize(this, options);
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
const SaveOp = getOrSetGlobal('SaveOp', Symbol('SaveOp'));
|
|
694
|
+
/**
|
|
695
|
+
* @private
|
|
696
|
+
*/
|
|
697
|
+
class FetchManager {
|
|
698
|
+
/**
|
|
699
|
+
* @internal
|
|
700
|
+
*/
|
|
701
|
+
|
|
702
|
+
/** @internal */
|
|
703
|
+
|
|
704
|
+
// fetches pending in the runloop, waiting to be coalesced
|
|
705
|
+
/**
|
|
706
|
+
* @internal
|
|
707
|
+
*/
|
|
708
|
+
|
|
709
|
+
/** @internal */
|
|
710
|
+
|
|
711
|
+
constructor(store) {
|
|
712
|
+
assertPrivateStore(store);
|
|
713
|
+
this._store = store;
|
|
714
|
+
// used to keep track of all the find requests that need to be coalesced
|
|
715
|
+
this._pendingFetch = new Map();
|
|
716
|
+
this.requestCache = store.getRequestStateService();
|
|
717
|
+
this.isDestroyed = false;
|
|
718
|
+
}
|
|
719
|
+
createSnapshot(identifier, options = {}) {
|
|
720
|
+
return new Snapshot(options, identifier, this._store);
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
/**
|
|
724
|
+
This method is called by `record.save`, and gets passed a
|
|
725
|
+
resolver for the promise that `record.save` returns.
|
|
726
|
+
It schedules saving to happen at the end of the run loop.
|
|
727
|
+
@private
|
|
728
|
+
*/
|
|
729
|
+
scheduleSave(identifier, options) {
|
|
730
|
+
const resolver = createDeferred();
|
|
731
|
+
const query = {
|
|
732
|
+
op: 'saveRecord',
|
|
733
|
+
recordIdentifier: identifier,
|
|
734
|
+
options
|
|
735
|
+
};
|
|
736
|
+
const queryRequest = {
|
|
737
|
+
data: [query]
|
|
738
|
+
};
|
|
739
|
+
const snapshot = this.createSnapshot(identifier, options);
|
|
740
|
+
const pendingSaveItem = {
|
|
741
|
+
snapshot: snapshot,
|
|
742
|
+
resolver: resolver,
|
|
743
|
+
identifier,
|
|
744
|
+
options,
|
|
745
|
+
queryRequest
|
|
746
|
+
};
|
|
747
|
+
const monitored = this.requestCache._enqueue(resolver.promise, pendingSaveItem.queryRequest);
|
|
748
|
+
_flushPendingSave(this._store, pendingSaveItem);
|
|
749
|
+
return monitored;
|
|
750
|
+
}
|
|
751
|
+
scheduleFetch(identifier, options, request) {
|
|
752
|
+
const query = {
|
|
753
|
+
op: 'findRecord',
|
|
754
|
+
recordIdentifier: identifier,
|
|
755
|
+
options
|
|
756
|
+
};
|
|
757
|
+
const queryRequest = {
|
|
758
|
+
data: [query]
|
|
759
|
+
};
|
|
760
|
+
const pendingFetch = this.getPendingFetch(identifier, options);
|
|
761
|
+
if (pendingFetch) {
|
|
762
|
+
return pendingFetch;
|
|
763
|
+
}
|
|
764
|
+
const modelName = identifier.type;
|
|
765
|
+
const resolver = createDeferred();
|
|
766
|
+
const pendingFetchItem = {
|
|
767
|
+
identifier,
|
|
768
|
+
resolver,
|
|
769
|
+
options,
|
|
770
|
+
queryRequest
|
|
771
|
+
};
|
|
772
|
+
const resolverPromise = resolver.promise;
|
|
773
|
+
const store = this._store;
|
|
774
|
+
const isInitialLoad = !store._instanceCache.recordIsLoaded(identifier); // we don't use isLoading directly because we are the request
|
|
775
|
+
|
|
776
|
+
const monitored = this.requestCache._enqueue(resolverPromise, pendingFetchItem.queryRequest);
|
|
777
|
+
let promise = monitored.then(payload => {
|
|
778
|
+
// ensure that regardless of id returned we assign to the correct record
|
|
779
|
+
if (payload.data && !Array.isArray(payload.data)) {
|
|
780
|
+
payload.data.lid = identifier.lid;
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
// additional data received in the payload
|
|
784
|
+
// may result in the merging of identifiers (and thus records)
|
|
785
|
+
const potentiallyNewIm = store._push(payload, options.reload);
|
|
786
|
+
if (potentiallyNewIm && !Array.isArray(potentiallyNewIm)) {
|
|
787
|
+
return potentiallyNewIm;
|
|
788
|
+
}
|
|
789
|
+
return identifier;
|
|
790
|
+
}, error => {
|
|
791
|
+
(test => {
|
|
792
|
+
if (!test) {
|
|
793
|
+
throw new Error(`Async Leak Detected: Expected the store to not be destroyed`);
|
|
794
|
+
}
|
|
795
|
+
})(!store.isDestroyed);
|
|
796
|
+
const cache = store.cache;
|
|
797
|
+
if (!cache || cache.isEmpty(identifier) || isInitialLoad) {
|
|
798
|
+
let isReleasable = true;
|
|
799
|
+
if (store._graph) {
|
|
800
|
+
const graph = store._graph;
|
|
801
|
+
if (!cache) {
|
|
802
|
+
isReleasable = graph.isReleasable(identifier);
|
|
803
|
+
if (!isReleasable) {
|
|
804
|
+
graph.unload(identifier, true);
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
if (cache || isReleasable) {
|
|
809
|
+
store._enableAsyncFlush = true;
|
|
810
|
+
store._instanceCache.unloadRecord(identifier);
|
|
811
|
+
store._enableAsyncFlush = null;
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
throw error;
|
|
815
|
+
});
|
|
816
|
+
if (this._pendingFetch.size === 0) {
|
|
817
|
+
void new Promise(resolve => setTimeout(resolve, 0)).then(() => {
|
|
818
|
+
this.flushAllPendingFetches();
|
|
819
|
+
});
|
|
820
|
+
}
|
|
821
|
+
const fetchesByType = this._pendingFetch;
|
|
822
|
+
let fetchesById = fetchesByType.get(modelName);
|
|
823
|
+
if (!fetchesById) {
|
|
824
|
+
fetchesById = new Map();
|
|
825
|
+
fetchesByType.set(modelName, fetchesById);
|
|
826
|
+
}
|
|
827
|
+
let requestsForIdentifier = fetchesById.get(identifier);
|
|
828
|
+
if (!requestsForIdentifier) {
|
|
829
|
+
requestsForIdentifier = [];
|
|
830
|
+
fetchesById.set(identifier, requestsForIdentifier);
|
|
831
|
+
}
|
|
832
|
+
requestsForIdentifier.push(pendingFetchItem);
|
|
833
|
+
{
|
|
834
|
+
if (!request.disableTestWaiter) {
|
|
835
|
+
promise = waitFor(promise);
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
pendingFetchItem.promise = promise;
|
|
839
|
+
return promise;
|
|
840
|
+
}
|
|
841
|
+
getPendingFetch(identifier, options) {
|
|
842
|
+
const pendingFetches = this._pendingFetch.get(identifier.type)?.get(identifier);
|
|
843
|
+
|
|
844
|
+
// We already have a pending fetch for this
|
|
845
|
+
if (pendingFetches) {
|
|
846
|
+
const matchingPendingFetch = pendingFetches.find(fetch => isSameRequest(options, fetch.options));
|
|
847
|
+
if (matchingPendingFetch) {
|
|
848
|
+
return matchingPendingFetch.promise;
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
flushAllPendingFetches() {
|
|
853
|
+
if (this.isDestroyed) {
|
|
854
|
+
return;
|
|
855
|
+
}
|
|
856
|
+
const store = this._store;
|
|
857
|
+
this._pendingFetch.forEach((fetchItem, type) => _flushPendingFetchForType(store, fetchItem, type));
|
|
858
|
+
this._pendingFetch.clear();
|
|
859
|
+
}
|
|
860
|
+
fetchDataIfNeededForIdentifier(identifier, options = {}, request) {
|
|
861
|
+
// pre-loading will change the isEmpty value
|
|
862
|
+
const isEmpty = _isEmpty(this._store._instanceCache, identifier);
|
|
863
|
+
const isLoading = _isLoading(this._store._instanceCache, identifier);
|
|
864
|
+
let promise;
|
|
865
|
+
if (isEmpty) {
|
|
866
|
+
assertIdentifierHasId(identifier);
|
|
867
|
+
{
|
|
868
|
+
promise = this.scheduleFetch(identifier, Object.assign({}, options, {
|
|
869
|
+
reload: true
|
|
870
|
+
}), request);
|
|
871
|
+
}
|
|
872
|
+
} else if (isLoading) {
|
|
873
|
+
promise = this.getPendingFetch(identifier, options);
|
|
874
|
+
(test => {
|
|
875
|
+
if (!test) {
|
|
876
|
+
throw new Error(`Expected to find a pending request for a record in the loading state, but found none`);
|
|
877
|
+
}
|
|
878
|
+
})(promise);
|
|
879
|
+
} else {
|
|
880
|
+
promise = Promise.resolve(identifier);
|
|
881
|
+
}
|
|
882
|
+
return promise;
|
|
883
|
+
}
|
|
884
|
+
destroy() {
|
|
885
|
+
this.isDestroyed = true;
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
/**
|
|
890
|
+
* This type exists for internal use only for
|
|
891
|
+
* where intimate contracts still exist either for
|
|
892
|
+
* the Test Suite or for Legacy code.
|
|
893
|
+
*
|
|
894
|
+
* @private
|
|
895
|
+
*/
|
|
896
|
+
|
|
897
|
+
function _isEmpty(instanceCache, identifier) {
|
|
898
|
+
const cache = instanceCache.cache;
|
|
899
|
+
if (!cache) {
|
|
900
|
+
return true;
|
|
901
|
+
}
|
|
902
|
+
const isNew = cache.isNew(identifier);
|
|
903
|
+
const isDeleted = cache.isDeleted(identifier);
|
|
904
|
+
const isEmpty = cache.isEmpty(identifier);
|
|
905
|
+
return (!isNew || isDeleted) && isEmpty;
|
|
906
|
+
}
|
|
907
|
+
function _isLoading(cache, identifier) {
|
|
908
|
+
const req = cache.store.getRequestStateService();
|
|
909
|
+
// const fulfilled = req.getLastRequestForRecord(identifier);
|
|
910
|
+
const isLoaded = cache.recordIsLoaded(identifier);
|
|
911
|
+
return !isLoaded &&
|
|
912
|
+
// fulfilled === null &&
|
|
913
|
+
req.getPendingRequestsForRecord(identifier).some(r => r.type === 'query');
|
|
914
|
+
}
|
|
915
|
+
function includesSatisfies(current, existing) {
|
|
916
|
+
// if we have no includes we are good
|
|
917
|
+
if (!current?.length) {
|
|
918
|
+
return true;
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
// if we are here we have includes,
|
|
922
|
+
// and if existing has no includes then we will need a new request
|
|
923
|
+
if (!existing?.length) {
|
|
924
|
+
return false;
|
|
925
|
+
}
|
|
926
|
+
const arrCurrent = (Array.isArray(current) ? current : current.split(',')).sort();
|
|
927
|
+
const arrExisting = (Array.isArray(existing) ? existing : existing.split(',')).sort();
|
|
928
|
+
|
|
929
|
+
// includes are identical
|
|
930
|
+
if (arrCurrent.join(',') === arrExisting.join(',')) {
|
|
931
|
+
return true;
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
// if all of current includes are in existing includes then we are good
|
|
935
|
+
// so if we find one that is not in existing then we need a new request
|
|
936
|
+
for (let i = 0; i < arrCurrent.length; i++) {
|
|
937
|
+
if (!arrExisting.includes(arrCurrent[i])) {
|
|
938
|
+
return false;
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
return true;
|
|
942
|
+
}
|
|
943
|
+
function optionsSatisfies(current, existing) {
|
|
944
|
+
return !current || current === existing || Object.keys(current).length === 0;
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
// this function helps resolve whether we have a pending request that we should use instead
|
|
948
|
+
function isSameRequest(options = {}, existingOptions = {}) {
|
|
949
|
+
return optionsSatisfies(options.adapterOptions, existingOptions.adapterOptions) && includesSatisfies(options.include, existingOptions.include);
|
|
950
|
+
}
|
|
951
|
+
function _findMany(store, adapter, modelName, snapshots) {
|
|
952
|
+
const modelClass = store.modelFor(modelName); // `adapter.findMany` gets the modelClass still
|
|
953
|
+
const promise = Promise.resolve().then(() => {
|
|
954
|
+
const ids = snapshots.map(s => s.id);
|
|
955
|
+
(test => {
|
|
956
|
+
if (!test) {
|
|
957
|
+
throw new Error(`Cannot fetch a record without an id`);
|
|
958
|
+
}
|
|
959
|
+
})(ids.every(v => v !== null));
|
|
960
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
961
|
+
(test => {
|
|
962
|
+
if (!test) {
|
|
963
|
+
throw new Error(`Expected this adapter to implement findMany for coalescing`);
|
|
964
|
+
}
|
|
965
|
+
})(adapter.findMany);
|
|
966
|
+
const ret = adapter.findMany(store, modelClass, ids, snapshots);
|
|
967
|
+
(test => {
|
|
968
|
+
if (!test) {
|
|
969
|
+
throw new Error('adapter.findMany returned undefined, this was very likely a mistake');
|
|
970
|
+
}
|
|
971
|
+
})(ret !== undefined);
|
|
972
|
+
return ret;
|
|
973
|
+
});
|
|
974
|
+
return promise.then(adapterPayload => {
|
|
975
|
+
(test => {
|
|
976
|
+
if (!test) {
|
|
977
|
+
throw new Error(`You made a 'findMany' request for '${modelName}' records with ids '[${snapshots.map(s => s.id).join(',')}]', but the adapter's response did not have any data`);
|
|
978
|
+
}
|
|
979
|
+
})(!!payloadIsNotBlank(adapterPayload));
|
|
980
|
+
const serializer = store.serializerFor(modelName);
|
|
981
|
+
const payload = normalizeResponseHelper(serializer, store, modelClass, adapterPayload, null, 'findMany');
|
|
982
|
+
return payload;
|
|
983
|
+
});
|
|
984
|
+
}
|
|
985
|
+
function rejectFetchedItems(fetchMap, snapshots, error) {
|
|
986
|
+
for (let i = 0, l = snapshots.length; i < l; i++) {
|
|
987
|
+
const snapshot = snapshots[i];
|
|
988
|
+
const pair = fetchMap.get(snapshot);
|
|
989
|
+
if (pair) {
|
|
990
|
+
pair.resolver.reject(error || new Error(`Expected: '<${snapshot.modelName}:${snapshot.id}>' to be present in the adapter provided payload, but it was not found.`));
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
function handleFoundRecords(store, fetchMap, snapshots, coalescedPayload) {
|
|
995
|
+
/*
|
|
996
|
+
It is possible that the same ID is included multiple times
|
|
997
|
+
via multiple snapshots. This happens when more than one
|
|
998
|
+
options hash was supplied, each of which must be uniquely
|
|
999
|
+
accounted for.
|
|
1000
|
+
However, since we can't map from response to a specific
|
|
1001
|
+
options object, we resolve all snapshots by id with
|
|
1002
|
+
the first response we see.
|
|
1003
|
+
*/
|
|
1004
|
+
const snapshotsById = new Map();
|
|
1005
|
+
for (let i = 0; i < snapshots.length; i++) {
|
|
1006
|
+
const id = snapshots[i].id;
|
|
1007
|
+
let snapshotGroup = snapshotsById.get(id);
|
|
1008
|
+
if (!snapshotGroup) {
|
|
1009
|
+
snapshotGroup = [];
|
|
1010
|
+
snapshotsById.set(id, snapshotGroup);
|
|
1011
|
+
}
|
|
1012
|
+
snapshotGroup.push(snapshots[i]);
|
|
1013
|
+
}
|
|
1014
|
+
const included = Array.isArray(coalescedPayload.included) ? coalescedPayload.included : [];
|
|
1015
|
+
|
|
1016
|
+
// resolve found records
|
|
1017
|
+
const resources = coalescedPayload.data;
|
|
1018
|
+
for (let i = 0, l = resources.length; i < l; i++) {
|
|
1019
|
+
const resource = resources[i];
|
|
1020
|
+
const snapshotGroup = snapshotsById.get(resource.id);
|
|
1021
|
+
snapshotsById.delete(resource.id);
|
|
1022
|
+
if (!snapshotGroup) {
|
|
1023
|
+
// TODO consider whether this should be a deprecation/assertion
|
|
1024
|
+
included.push(resource);
|
|
1025
|
+
} else {
|
|
1026
|
+
snapshotGroup.forEach(snapshot => {
|
|
1027
|
+
const pair = fetchMap.get(snapshot);
|
|
1028
|
+
const resolver = pair.resolver;
|
|
1029
|
+
resolver.resolve({
|
|
1030
|
+
data: resource
|
|
1031
|
+
});
|
|
1032
|
+
});
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
if (included.length > 0) {
|
|
1036
|
+
store._push({
|
|
1037
|
+
data: null,
|
|
1038
|
+
included
|
|
1039
|
+
}, true);
|
|
1040
|
+
}
|
|
1041
|
+
if (snapshotsById.size === 0) {
|
|
1042
|
+
return;
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
// reject missing records
|
|
1046
|
+
const rejected = [];
|
|
1047
|
+
snapshotsById.forEach(snapshotArray => {
|
|
1048
|
+
rejected.push(...snapshotArray);
|
|
1049
|
+
});
|
|
1050
|
+
console.warn('Ember Data expected to find records with the following ids in the adapter response from findMany but they were missing: [ "' + [...snapshotsById.values()].map(r => r[0].id).join('", "') + '" ]', {
|
|
1051
|
+
id: 'ds.store.missing-records-from-adapter'
|
|
1052
|
+
});
|
|
1053
|
+
rejectFetchedItems(fetchMap, rejected);
|
|
1054
|
+
}
|
|
1055
|
+
function _fetchRecord(store, adapter, fetchItem) {
|
|
1056
|
+
const identifier = fetchItem.identifier;
|
|
1057
|
+
const modelName = identifier.type;
|
|
1058
|
+
(test => {
|
|
1059
|
+
if (!test) {
|
|
1060
|
+
throw new Error(`You tried to find a record but you have no adapter (for ${modelName})`);
|
|
1061
|
+
}
|
|
1062
|
+
})(adapter);
|
|
1063
|
+
(test => {
|
|
1064
|
+
if (!test) {
|
|
1065
|
+
throw new Error(`You tried to find a record but your adapter (for ${modelName}) does not implement 'findRecord'`);
|
|
1066
|
+
}
|
|
1067
|
+
})(typeof adapter.findRecord === 'function');
|
|
1068
|
+
const snapshot = store._fetchManager.createSnapshot(identifier, fetchItem.options);
|
|
1069
|
+
const klass = store.modelFor(identifier.type);
|
|
1070
|
+
const id = identifier.id;
|
|
1071
|
+
let promise = Promise.resolve().then(() => {
|
|
1072
|
+
return adapter.findRecord(store, klass, identifier.id, snapshot);
|
|
1073
|
+
});
|
|
1074
|
+
promise = promise.then(adapterPayload => {
|
|
1075
|
+
(test => {
|
|
1076
|
+
if (!test) {
|
|
1077
|
+
throw new Error(`Async Leak Detected: Expected the store to not be destroyed`);
|
|
1078
|
+
}
|
|
1079
|
+
})(!(store.isDestroyed || store.isDestroying));
|
|
1080
|
+
(test => {
|
|
1081
|
+
if (!test) {
|
|
1082
|
+
throw new Error(`You made a 'findRecord' request for a '${modelName}' with id '${id}', but the adapter's response did not have any data`);
|
|
1083
|
+
}
|
|
1084
|
+
})(!!payloadIsNotBlank(adapterPayload));
|
|
1085
|
+
const serializer = store.serializerFor(modelName);
|
|
1086
|
+
const payload = normalizeResponseHelper(serializer, store, klass, adapterPayload, id, 'findRecord');
|
|
1087
|
+
(test => {
|
|
1088
|
+
if (!test) {
|
|
1089
|
+
throw new Error(`Ember Data expected the primary data returned from a 'findRecord' response to be an object but instead it found an array.`);
|
|
1090
|
+
}
|
|
1091
|
+
})(!Array.isArray(payload.data));
|
|
1092
|
+
(test => {
|
|
1093
|
+
if (!test) {
|
|
1094
|
+
throw new Error(`The 'findRecord' request for ${modelName}:${id} resolved indicating success but contained no primary data. To indicate a 404 not found you should either reject the promise returned by the adapter's findRecord method or throw a NotFoundError.`);
|
|
1095
|
+
}
|
|
1096
|
+
})('data' in payload && payload.data !== null && typeof payload.data === 'object');
|
|
1097
|
+
console.warn(`You requested a record of type '${modelName}' with id '${id}' but the adapter returned a payload with primary data having an id of '${payload.data.id}'. Use 'store.findRecord()' when the requested id is the same as the one returned by the adapter. In other cases use 'store.queryRecord()' instead.`, coerceId(payload.data.id) === coerceId(id), {
|
|
1098
|
+
id: 'ds.store.findRecord.id-mismatch'
|
|
1099
|
+
});
|
|
1100
|
+
return payload;
|
|
1101
|
+
});
|
|
1102
|
+
fetchItem.resolver.resolve(promise);
|
|
1103
|
+
}
|
|
1104
|
+
function _processCoalescedGroup(store, fetchMap, group, adapter, modelName) {
|
|
1105
|
+
if (group.length > 1) {
|
|
1106
|
+
_findMany(store, adapter, modelName, group).then(payloads => {
|
|
1107
|
+
handleFoundRecords(store, fetchMap, group, payloads);
|
|
1108
|
+
}).catch(error => {
|
|
1109
|
+
rejectFetchedItems(fetchMap, group, error);
|
|
1110
|
+
});
|
|
1111
|
+
} else if (group.length === 1) {
|
|
1112
|
+
_fetchRecord(store, adapter, fetchMap.get(group[0]));
|
|
1113
|
+
} else {
|
|
1114
|
+
(test => {
|
|
1115
|
+
{
|
|
1116
|
+
throw new Error("You cannot return an empty array from adapter's method groupRecordsForFindMany");
|
|
1117
|
+
}
|
|
1118
|
+
})();
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
function _flushPendingFetchForType(store, pendingFetchMap, modelName) {
|
|
1122
|
+
const adapter = store.adapterFor(modelName);
|
|
1123
|
+
const shouldCoalesce = !!adapter.findMany && adapter.coalesceFindRequests;
|
|
1124
|
+
if (shouldCoalesce) {
|
|
1125
|
+
const pendingFetchItems = [];
|
|
1126
|
+
pendingFetchMap.forEach((requestsForIdentifier, identifier) => {
|
|
1127
|
+
if (requestsForIdentifier.length > 1) {
|
|
1128
|
+
return;
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
// remove this entry from the map so it's not processed again
|
|
1132
|
+
pendingFetchMap.delete(identifier);
|
|
1133
|
+
pendingFetchItems.push(requestsForIdentifier[0]);
|
|
1134
|
+
});
|
|
1135
|
+
const totalItems = pendingFetchItems.length;
|
|
1136
|
+
if (totalItems > 1) {
|
|
1137
|
+
const snapshots = new Array(totalItems);
|
|
1138
|
+
const fetchMap = new Map();
|
|
1139
|
+
for (let i = 0; i < totalItems; i++) {
|
|
1140
|
+
const fetchItem = pendingFetchItems[i];
|
|
1141
|
+
snapshots[i] = store._fetchManager.createSnapshot(fetchItem.identifier, fetchItem.options);
|
|
1142
|
+
fetchMap.set(snapshots[i], fetchItem);
|
|
1143
|
+
}
|
|
1144
|
+
let groups;
|
|
1145
|
+
if (adapter.groupRecordsForFindMany) {
|
|
1146
|
+
groups = adapter.groupRecordsForFindMany(store, snapshots);
|
|
1147
|
+
} else {
|
|
1148
|
+
groups = [snapshots];
|
|
1149
|
+
}
|
|
1150
|
+
for (let i = 0, l = groups.length; i < l; i++) {
|
|
1151
|
+
_processCoalescedGroup(store, fetchMap, groups[i], adapter, modelName);
|
|
1152
|
+
}
|
|
1153
|
+
} else if (totalItems === 1) {
|
|
1154
|
+
_fetchRecord(store, adapter, pendingFetchItems[0]);
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
pendingFetchMap.forEach(pendingFetchItems => {
|
|
1158
|
+
pendingFetchItems.forEach(pendingFetchItem => {
|
|
1159
|
+
_fetchRecord(store, adapter, pendingFetchItem);
|
|
1160
|
+
});
|
|
1161
|
+
});
|
|
1162
|
+
}
|
|
1163
|
+
function _flushPendingSave(store, pending) {
|
|
1164
|
+
const {
|
|
1165
|
+
snapshot,
|
|
1166
|
+
resolver,
|
|
1167
|
+
identifier,
|
|
1168
|
+
options
|
|
1169
|
+
} = pending;
|
|
1170
|
+
const adapter = store.adapterFor(identifier.type);
|
|
1171
|
+
const operation = options[SaveOp];
|
|
1172
|
+
const modelName = snapshot.modelName;
|
|
1173
|
+
const modelClass = store.modelFor(modelName);
|
|
1174
|
+
(test => {
|
|
1175
|
+
if (!test) {
|
|
1176
|
+
throw new Error(`You tried to update a record but you have no adapter (for ${modelName})`);
|
|
1177
|
+
}
|
|
1178
|
+
})(adapter);
|
|
1179
|
+
(test => {
|
|
1180
|
+
if (!test) {
|
|
1181
|
+
throw new Error(`You tried to update a record but your adapter (for ${modelName}) does not implement '${operation}'`);
|
|
1182
|
+
}
|
|
1183
|
+
})(typeof adapter[operation] === 'function');
|
|
1184
|
+
let promise = Promise.resolve().then(() => adapter[operation](store, modelClass, snapshot));
|
|
1185
|
+
const serializer = store.serializerFor(modelName);
|
|
1186
|
+
(test => {
|
|
1187
|
+
if (!test) {
|
|
1188
|
+
throw new Error(`Your adapter's '${operation}' method must return a value, but it returned 'undefined'`);
|
|
1189
|
+
}
|
|
1190
|
+
})(promise !== undefined);
|
|
1191
|
+
promise = promise.then(adapterPayload => {
|
|
1192
|
+
if (adapterPayload) {
|
|
1193
|
+
return normalizeResponseHelper(serializer, store, modelClass, adapterPayload, snapshot.id, operation);
|
|
1194
|
+
}
|
|
1195
|
+
});
|
|
1196
|
+
resolver.resolve(promise);
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
/**
|
|
1200
|
+
* Utilities - often temporary - for maintaining backwards compatibility with
|
|
1201
|
+
* older parts of WarpDrive.
|
|
1202
|
+
*
|
|
1203
|
+
@module
|
|
1204
|
+
*/
|
|
1205
|
+
function upgradeStore(store) {}
|
|
1206
|
+
export { FetchManager as F, SaveOp as S, assertIdentifierHasId as a, SnapshotRecordArray as b, Snapshot as c, iterateData as i, normalizeResponseHelper as n, payloadIsNotBlank as p, upgradeStore as u };
|