@ember-data/store 4.6.1 → 4.7.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/-debug/index.js +35 -13
- package/addon/-private/{identifier-cache.ts → caches/identifier-cache.ts} +148 -73
- package/addon/-private/caches/instance-cache.ts +690 -0
- package/addon/-private/{record-data-for.ts → caches/record-data-for.ts} +2 -7
- package/addon/-private/index.ts +44 -24
- package/addon/-private/{model → legacy-model-support}/record-reference.ts +15 -13
- package/addon/-private/{schema-definition-service.ts → legacy-model-support/schema-definition-service.ts} +13 -9
- package/addon/-private/{model → legacy-model-support}/shim-model-class.ts +18 -11
- package/addon/-private/managers/record-array-manager.ts +377 -0
- package/addon/-private/managers/record-data-manager.ts +845 -0
- package/addon/-private/managers/record-data-store-wrapper.ts +421 -0
- package/addon/-private/managers/record-notification-manager.ts +109 -0
- package/addon/-private/network/fetch-manager.ts +567 -0
- package/addon/-private/{finders.js → network/finders.js} +14 -17
- package/addon/-private/{request-cache.ts → network/request-cache.ts} +21 -18
- package/addon/-private/{snapshot-record-array.ts → network/snapshot-record-array.ts} +14 -31
- package/addon/-private/{snapshot.ts → network/snapshot.ts} +40 -49
- package/addon/-private/{promise-proxies.ts → proxies/promise-proxies.ts} +76 -15
- package/addon/-private/{promise-proxy-base.js → proxies/promise-proxy-base.js} +0 -0
- package/addon/-private/record-arrays/identifier-array.ts +924 -0
- package/addon/-private/{core-store.ts → store-service.ts} +574 -215
- package/addon/-private/{coerce-id.ts → utils/coerce-id.ts} +1 -1
- package/addon/-private/{common.js → utils/common.js} +1 -2
- package/addon/-private/utils/construct-resource.ts +2 -2
- package/addon/-private/{identifer-debug-consts.ts → utils/identifer-debug-consts.ts} +0 -0
- package/addon/-private/utils/is-non-empty-string.ts +1 -1
- package/addon/-private/{normalize-model-name.ts → utils/normalize-model-name.ts} +1 -3
- package/addon/-private/utils/promise-record.ts +5 -6
- package/addon/-private/{serializer-response.ts → utils/serializer-response.ts} +2 -2
- package/addon/-private/utils/uuid-polyfill.ts +73 -0
- package/package.json +12 -8
- package/addon/-private/backburner.js +0 -25
- package/addon/-private/errors-utils.js +0 -146
- package/addon/-private/fetch-manager.ts +0 -597
- package/addon/-private/identity-map.ts +0 -54
- package/addon/-private/instance-cache.ts +0 -387
- package/addon/-private/internal-model-factory.ts +0 -359
- package/addon/-private/internal-model-map.ts +0 -121
- package/addon/-private/model/internal-model.ts +0 -602
- package/addon/-private/record-array-manager.ts +0 -444
- package/addon/-private/record-arrays/adapter-populated-record-array.ts +0 -130
- package/addon/-private/record-arrays/record-array.ts +0 -318
- package/addon/-private/record-data-store-wrapper.ts +0 -243
- package/addon/-private/record-notification-manager.ts +0 -73
- package/addon/-private/weak-cache.ts +0 -125
|
@@ -0,0 +1,567 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @ember-data/store
|
|
3
|
+
*/
|
|
4
|
+
import { assert, deprecate, warn } from '@ember/debug';
|
|
5
|
+
import { _backburner as emberBackburner } from '@ember/runloop';
|
|
6
|
+
import { DEBUG } from '@glimmer/env';
|
|
7
|
+
|
|
8
|
+
import { importSync } from '@embroider/macros';
|
|
9
|
+
import { default as RSVP, resolve } from 'rsvp';
|
|
10
|
+
|
|
11
|
+
import { HAS_RECORD_DATA_PACKAGE } from '@ember-data/private-build-infra';
|
|
12
|
+
import { DEPRECATE_RSVP_PROMISE } from '@ember-data/private-build-infra/deprecations';
|
|
13
|
+
import type { CollectionResourceDocument, SingleResourceDocument } from '@ember-data/types/q/ember-data-json-api';
|
|
14
|
+
import type { FindRecordQuery, Request, SaveRecordMutation } from '@ember-data/types/q/fetch-manager';
|
|
15
|
+
import type {
|
|
16
|
+
RecordIdentifier,
|
|
17
|
+
StableExistingRecordIdentifier,
|
|
18
|
+
StableRecordIdentifier,
|
|
19
|
+
} from '@ember-data/types/q/identifier';
|
|
20
|
+
import { MinimumAdapterInterface } from '@ember-data/types/q/minimum-adapter-interface';
|
|
21
|
+
import type { MinimumSerializerInterface } from '@ember-data/types/q/minimum-serializer-interface';
|
|
22
|
+
import type { FindOptions } from '@ember-data/types/q/store';
|
|
23
|
+
|
|
24
|
+
import ShimModelClass from '../legacy-model-support/shim-model-class';
|
|
25
|
+
import type Store from '../store-service';
|
|
26
|
+
import coerceId from '../utils/coerce-id';
|
|
27
|
+
import { _bind, _guard, _objectIsAlive, guardDestroyedStore } from '../utils/common';
|
|
28
|
+
import { normalizeResponseHelper } from '../utils/serializer-response';
|
|
29
|
+
import RequestCache from './request-cache';
|
|
30
|
+
import Snapshot from './snapshot';
|
|
31
|
+
|
|
32
|
+
function payloadIsNotBlank(adapterPayload): boolean {
|
|
33
|
+
if (Array.isArray(adapterPayload)) {
|
|
34
|
+
return true;
|
|
35
|
+
} else {
|
|
36
|
+
return Object.keys(adapterPayload || {}).length !== 0;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
type AdapterErrors = Error & { errors?: string[]; isAdapterError?: true };
|
|
41
|
+
type SerializerWithParseErrors = MinimumSerializerInterface & {
|
|
42
|
+
extractErrors?(store: Store, modelClass: ShimModelClass, error: AdapterErrors, recordId: string | null): any;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const SaveOp: unique symbol = Symbol('SaveOp');
|
|
46
|
+
|
|
47
|
+
export type FetchMutationOptions = FindOptions & { [SaveOp]: 'createRecord' | 'deleteRecord' | 'updateRecord' };
|
|
48
|
+
|
|
49
|
+
interface PendingFetchItem {
|
|
50
|
+
identifier: StableExistingRecordIdentifier;
|
|
51
|
+
queryRequest: Request;
|
|
52
|
+
resolver: RSVP.Deferred<any>;
|
|
53
|
+
options: FindOptions;
|
|
54
|
+
trace?: any;
|
|
55
|
+
promise: Promise<StableRecordIdentifier>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface PendingSaveItem {
|
|
59
|
+
resolver: RSVP.Deferred<any>;
|
|
60
|
+
snapshot: Snapshot;
|
|
61
|
+
identifier: RecordIdentifier;
|
|
62
|
+
options: FetchMutationOptions;
|
|
63
|
+
queryRequest: Request;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Manages the state of network requests initiated by the store
|
|
68
|
+
*
|
|
69
|
+
* @class FetchManager
|
|
70
|
+
* @private
|
|
71
|
+
*/
|
|
72
|
+
export default class FetchManager {
|
|
73
|
+
declare isDestroyed: boolean;
|
|
74
|
+
declare requestCache: RequestCache;
|
|
75
|
+
// saves which are pending in the runloop
|
|
76
|
+
declare _pendingSave: PendingSaveItem[];
|
|
77
|
+
// fetches pending in the runloop, waiting to be coalesced
|
|
78
|
+
declare _pendingFetch: Map<string, PendingFetchItem[]>;
|
|
79
|
+
declare _store: Store;
|
|
80
|
+
|
|
81
|
+
constructor(store: Store) {
|
|
82
|
+
this._store = store;
|
|
83
|
+
// used to keep track of all the find requests that need to be coalesced
|
|
84
|
+
this._pendingFetch = new Map();
|
|
85
|
+
this._pendingSave = [];
|
|
86
|
+
this.requestCache = new RequestCache();
|
|
87
|
+
this.isDestroyed = false;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
clearEntries(identifier: StableRecordIdentifier) {
|
|
91
|
+
this.requestCache._done.delete(identifier);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
This method is called by `record.save`, and gets passed a
|
|
96
|
+
resolver for the promise that `record.save` returns.
|
|
97
|
+
|
|
98
|
+
It schedules saving to happen at the end of the run loop.
|
|
99
|
+
|
|
100
|
+
@internal
|
|
101
|
+
*/
|
|
102
|
+
scheduleSave(identifier: RecordIdentifier, options: FetchMutationOptions): Promise<null | SingleResourceDocument> {
|
|
103
|
+
let promiseLabel = 'DS: Model#save ' + this;
|
|
104
|
+
let resolver = RSVP.defer<null | SingleResourceDocument>(promiseLabel);
|
|
105
|
+
let query: SaveRecordMutation = {
|
|
106
|
+
op: 'saveRecord',
|
|
107
|
+
recordIdentifier: identifier,
|
|
108
|
+
options,
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
let queryRequest: Request = {
|
|
112
|
+
data: [query],
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
let snapshot = new Snapshot(options, identifier, this._store);
|
|
116
|
+
let pendingSaveItem = {
|
|
117
|
+
snapshot: snapshot,
|
|
118
|
+
resolver: resolver,
|
|
119
|
+
identifier,
|
|
120
|
+
options,
|
|
121
|
+
queryRequest,
|
|
122
|
+
};
|
|
123
|
+
this._pendingSave.push(pendingSaveItem);
|
|
124
|
+
emberBackburner.scheduleOnce('actions', this, this._flushPendingSaves);
|
|
125
|
+
|
|
126
|
+
this.requestCache.enqueue(resolver.promise, pendingSaveItem.queryRequest);
|
|
127
|
+
|
|
128
|
+
return resolver.promise;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
This method is called at the end of the run loop, and
|
|
133
|
+
flushes any records passed into `scheduleSave`
|
|
134
|
+
|
|
135
|
+
@method flushPendingSave
|
|
136
|
+
@internal
|
|
137
|
+
*/
|
|
138
|
+
_flushPendingSaves() {
|
|
139
|
+
const store = this._store;
|
|
140
|
+
let pending = this._pendingSave.slice();
|
|
141
|
+
this._pendingSave = [];
|
|
142
|
+
for (let i = 0, j = pending.length; i < j; i++) {
|
|
143
|
+
let pendingItem = pending[i];
|
|
144
|
+
_flushPendingSave(store, pendingItem);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
scheduleFetch(identifier: StableExistingRecordIdentifier, options: FindOptions): Promise<StableRecordIdentifier> {
|
|
149
|
+
// TODO Probably the store should pass in the query object
|
|
150
|
+
let shouldTrace = DEBUG && this._store.generateStackTracesForTrackedRequests;
|
|
151
|
+
|
|
152
|
+
let query: FindRecordQuery = {
|
|
153
|
+
op: 'findRecord',
|
|
154
|
+
recordIdentifier: identifier,
|
|
155
|
+
options,
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
let queryRequest: Request = {
|
|
159
|
+
data: [query],
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
let pendingFetch = this.getPendingFetch(identifier, options);
|
|
163
|
+
if (pendingFetch) {
|
|
164
|
+
return pendingFetch;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
let id = identifier.id;
|
|
168
|
+
let modelName = identifier.type;
|
|
169
|
+
|
|
170
|
+
let resolver = RSVP.defer<SingleResourceDocument>(`Fetching ${modelName}' with id: ${id}`);
|
|
171
|
+
let pendingFetchItem: PendingFetchItem = {
|
|
172
|
+
identifier,
|
|
173
|
+
resolver,
|
|
174
|
+
options,
|
|
175
|
+
queryRequest,
|
|
176
|
+
} as PendingFetchItem;
|
|
177
|
+
|
|
178
|
+
if (DEBUG) {
|
|
179
|
+
if (shouldTrace) {
|
|
180
|
+
let trace;
|
|
181
|
+
|
|
182
|
+
try {
|
|
183
|
+
throw new Error(`Trace Origin for scheduled fetch for ${modelName}:${id}.`);
|
|
184
|
+
} catch (e) {
|
|
185
|
+
trace = e;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// enable folks to discover the origin of this findRecord call when
|
|
189
|
+
// debugging. Ideally we would have a tracked queue for requests with
|
|
190
|
+
// labels or local IDs that could be used to merge this trace with
|
|
191
|
+
// the trace made available when we detect an async leak
|
|
192
|
+
pendingFetchItem.trace = trace;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
let resolverPromise = resolver.promise;
|
|
197
|
+
const store = this._store;
|
|
198
|
+
const isLoading = !store._instanceCache.recordIsLoaded(identifier); // we don't use isLoading directly because we are the request
|
|
199
|
+
|
|
200
|
+
const promise = resolverPromise.then(
|
|
201
|
+
(payload) => {
|
|
202
|
+
// ensure that regardless of id returned we assign to the correct record
|
|
203
|
+
if (payload.data && !Array.isArray(payload.data)) {
|
|
204
|
+
payload.data.lid = identifier.lid;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// additional data received in the payload
|
|
208
|
+
// may result in the merging of identifiers (and thus records)
|
|
209
|
+
let potentiallyNewIm = store._push(payload);
|
|
210
|
+
if (potentiallyNewIm && !Array.isArray(potentiallyNewIm)) {
|
|
211
|
+
return potentiallyNewIm;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return identifier;
|
|
215
|
+
},
|
|
216
|
+
(error) => {
|
|
217
|
+
const recordData = store._instanceCache.peek({ identifier, bucket: 'recordData' });
|
|
218
|
+
if (!recordData || recordData.isEmpty(identifier) || isLoading) {
|
|
219
|
+
let isReleasable = true;
|
|
220
|
+
if (!recordData && HAS_RECORD_DATA_PACKAGE) {
|
|
221
|
+
const graphFor = (
|
|
222
|
+
importSync('@ember-data/record-data/-private') as typeof import('@ember-data/record-data/-private')
|
|
223
|
+
).graphFor;
|
|
224
|
+
const graph = graphFor(store);
|
|
225
|
+
isReleasable = graph.isReleasable(identifier);
|
|
226
|
+
if (!isReleasable) {
|
|
227
|
+
graph.unload(identifier, true);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
if (recordData || isReleasable) {
|
|
231
|
+
store._instanceCache.unloadRecord(identifier);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
throw error;
|
|
235
|
+
}
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
if (this._pendingFetch.size === 0) {
|
|
239
|
+
emberBackburner.schedule('actions', this, this.flushAllPendingFetches);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
let fetches = this._pendingFetch;
|
|
243
|
+
|
|
244
|
+
if (!fetches.has(modelName)) {
|
|
245
|
+
fetches.set(modelName, []);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
(fetches.get(modelName) as PendingFetchItem[]).push(pendingFetchItem);
|
|
249
|
+
|
|
250
|
+
pendingFetchItem.promise = promise;
|
|
251
|
+
this.requestCache.enqueue(resolverPromise, pendingFetchItem.queryRequest);
|
|
252
|
+
return promise;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
getPendingFetch(identifier: StableRecordIdentifier, options: FindOptions) {
|
|
256
|
+
let pendingFetches = this._pendingFetch.get(identifier.type);
|
|
257
|
+
|
|
258
|
+
// We already have a pending fetch for this
|
|
259
|
+
if (pendingFetches) {
|
|
260
|
+
let matchingPendingFetch = pendingFetches.find(
|
|
261
|
+
(fetch) => fetch.identifier === identifier && isSameRequest(options, fetch.options)
|
|
262
|
+
);
|
|
263
|
+
if (matchingPendingFetch) {
|
|
264
|
+
return matchingPendingFetch.promise;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
flushAllPendingFetches() {
|
|
270
|
+
if (this.isDestroyed) {
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const store = this._store;
|
|
275
|
+
this._pendingFetch.forEach((fetchItem, type) => _flushPendingFetchForType(store, fetchItem, type));
|
|
276
|
+
this._pendingFetch.clear();
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
destroy() {
|
|
280
|
+
this.isDestroyed = true;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// this function helps resolve whether we have a pending request that we should use instead
|
|
285
|
+
function isSameRequest(options: FindOptions = {}, existingOptions: FindOptions = {}) {
|
|
286
|
+
let includedMatches = !options.include || options.include === existingOptions.include;
|
|
287
|
+
let adapterOptionsMatches = options.adapterOptions === existingOptions.adapterOptions;
|
|
288
|
+
|
|
289
|
+
return includedMatches && adapterOptionsMatches;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function _findMany(
|
|
293
|
+
store: Store,
|
|
294
|
+
adapter: MinimumAdapterInterface,
|
|
295
|
+
modelName: string,
|
|
296
|
+
snapshots: Snapshot[]
|
|
297
|
+
): Promise<CollectionResourceDocument> {
|
|
298
|
+
let modelClass = store.modelFor(modelName); // `adapter.findMany` gets the modelClass still
|
|
299
|
+
const ids = snapshots.map((s) => s.id!);
|
|
300
|
+
assert(
|
|
301
|
+
`Cannot fetch a record without an id`,
|
|
302
|
+
ids.every((v) => v !== null)
|
|
303
|
+
);
|
|
304
|
+
assert(`Expected this adapter to implement findMany for coalescing`, adapter.findMany);
|
|
305
|
+
let promise = adapter.findMany(store, modelClass, ids, snapshots);
|
|
306
|
+
let label = `DS: Handle Adapter#findMany of '${modelName}'`;
|
|
307
|
+
|
|
308
|
+
if (promise === undefined) {
|
|
309
|
+
throw new Error('adapter.findMany returned undefined, this was very likely a mistake');
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
promise = guardDestroyedStore(promise, store, label);
|
|
313
|
+
|
|
314
|
+
return promise.then((adapterPayload) => {
|
|
315
|
+
assert(
|
|
316
|
+
`You made a 'findMany' request for '${modelName}' records with ids '[${ids}]', but the adapter's response did not have any data`,
|
|
317
|
+
!!payloadIsNotBlank(adapterPayload)
|
|
318
|
+
);
|
|
319
|
+
let serializer = store.serializerFor(modelName);
|
|
320
|
+
let payload = normalizeResponseHelper(serializer, store, modelClass, adapterPayload, null, 'findMany');
|
|
321
|
+
return payload as CollectionResourceDocument;
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function rejectFetchedItems(fetchMap: Map<Snapshot, PendingFetchItem>, snapshots: Snapshot[], error?) {
|
|
326
|
+
for (let i = 0, l = snapshots.length; i < l; i++) {
|
|
327
|
+
let snapshot = snapshots[i];
|
|
328
|
+
let pair = fetchMap.get(snapshot);
|
|
329
|
+
|
|
330
|
+
if (pair) {
|
|
331
|
+
pair.resolver.reject(
|
|
332
|
+
error ||
|
|
333
|
+
new Error(
|
|
334
|
+
`Expected: '<${snapshot.modelName}:${snapshot.id}>' to be present in the adapter provided payload, but it was not found.`
|
|
335
|
+
)
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function handleFoundRecords(
|
|
342
|
+
store: Store,
|
|
343
|
+
fetchMap: Map<Snapshot, PendingFetchItem>,
|
|
344
|
+
snapshots: Snapshot[],
|
|
345
|
+
coalescedPayload: CollectionResourceDocument
|
|
346
|
+
) {
|
|
347
|
+
/*
|
|
348
|
+
It is possible that the same ID is included multiple times
|
|
349
|
+
via multiple snapshots. This happens when more than one
|
|
350
|
+
options hash was supplied, each of which must be uniquely
|
|
351
|
+
accounted for.
|
|
352
|
+
|
|
353
|
+
However, since we can't map from response to a specific
|
|
354
|
+
options object, we resolve all snapshots by id with
|
|
355
|
+
the first response we see.
|
|
356
|
+
*/
|
|
357
|
+
let snapshotsById = new Map<string, Snapshot[]>();
|
|
358
|
+
for (let i = 0; i < snapshots.length; i++) {
|
|
359
|
+
let id = snapshots[i].id!;
|
|
360
|
+
let snapshotGroup = snapshotsById.get(id);
|
|
361
|
+
if (!snapshotGroup) {
|
|
362
|
+
snapshotGroup = [];
|
|
363
|
+
snapshotsById.set(id, snapshotGroup);
|
|
364
|
+
}
|
|
365
|
+
snapshotGroup.push(snapshots[i]);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const included = Array.isArray(coalescedPayload.included) ? coalescedPayload.included : [];
|
|
369
|
+
|
|
370
|
+
// resolve found records
|
|
371
|
+
let resources = coalescedPayload.data;
|
|
372
|
+
for (let i = 0, l = resources.length; i < l; i++) {
|
|
373
|
+
let resource = resources[i];
|
|
374
|
+
let snapshotGroup = snapshotsById.get(resource.id);
|
|
375
|
+
snapshotsById.delete(resource.id);
|
|
376
|
+
|
|
377
|
+
if (!snapshotGroup) {
|
|
378
|
+
// TODO consider whether this should be a deprecation/assertion
|
|
379
|
+
included.push(resource);
|
|
380
|
+
} else {
|
|
381
|
+
snapshotGroup.forEach((snapshot) => {
|
|
382
|
+
let pair = fetchMap.get(snapshot)!;
|
|
383
|
+
let resolver = pair.resolver;
|
|
384
|
+
resolver.resolve({ data: resource });
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
if (included.length > 0) {
|
|
390
|
+
store._push({ data: null, included });
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
if (snapshotsById.size === 0) {
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// reject missing records
|
|
398
|
+
let rejected: Snapshot[] = [];
|
|
399
|
+
snapshotsById.forEach((snapshots) => {
|
|
400
|
+
rejected.push(...snapshots);
|
|
401
|
+
});
|
|
402
|
+
warn(
|
|
403
|
+
'Ember Data expected to find records with the following ids in the adapter response from findMany but they were missing: [ "' +
|
|
404
|
+
[...snapshotsById.values()].map((r) => r[0].id).join('", "') +
|
|
405
|
+
'" ]',
|
|
406
|
+
{
|
|
407
|
+
id: 'ds.store.missing-records-from-adapter',
|
|
408
|
+
}
|
|
409
|
+
);
|
|
410
|
+
|
|
411
|
+
rejectFetchedItems(fetchMap, rejected);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function _fetchRecord(store: Store, fetchItem: PendingFetchItem) {
|
|
415
|
+
let identifier = fetchItem.identifier;
|
|
416
|
+
let modelName = identifier.type;
|
|
417
|
+
let adapter = store.adapterFor(modelName);
|
|
418
|
+
|
|
419
|
+
assert(`You tried to find a record but you have no adapter (for ${modelName})`, adapter);
|
|
420
|
+
assert(
|
|
421
|
+
`You tried to find a record but your adapter (for ${modelName}) does not implement 'findRecord'`,
|
|
422
|
+
typeof adapter.findRecord === 'function'
|
|
423
|
+
);
|
|
424
|
+
|
|
425
|
+
let snapshot = new Snapshot(fetchItem.options, identifier, store);
|
|
426
|
+
let klass = store.modelFor(identifier.type);
|
|
427
|
+
let id = identifier.id;
|
|
428
|
+
let label = `DS: Handle Adapter#findRecord of '${modelName}' with id: '${id}'`;
|
|
429
|
+
|
|
430
|
+
let promise = guardDestroyedStore(
|
|
431
|
+
resolve().then(() => {
|
|
432
|
+
return adapter.findRecord(store, klass, identifier.id, snapshot);
|
|
433
|
+
}),
|
|
434
|
+
store,
|
|
435
|
+
label
|
|
436
|
+
).then((adapterPayload) => {
|
|
437
|
+
assert(
|
|
438
|
+
`You made a 'findRecord' request for a '${modelName}' with id '${id}', but the adapter's response did not have any data`,
|
|
439
|
+
!!payloadIsNotBlank(adapterPayload)
|
|
440
|
+
);
|
|
441
|
+
let serializer = store.serializerFor(modelName);
|
|
442
|
+
let payload = normalizeResponseHelper(serializer, store, klass, adapterPayload, id, 'findRecord');
|
|
443
|
+
assert(
|
|
444
|
+
`Ember Data expected the primary data returned from a 'findRecord' response to be an object but instead it found an array.`,
|
|
445
|
+
!Array.isArray(payload.data)
|
|
446
|
+
);
|
|
447
|
+
assert(
|
|
448
|
+
`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.`,
|
|
449
|
+
'data' in payload && payload.data !== null && typeof payload.data === 'object'
|
|
450
|
+
);
|
|
451
|
+
|
|
452
|
+
warn(
|
|
453
|
+
`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.`,
|
|
454
|
+
coerceId(payload.data.id) === coerceId(id),
|
|
455
|
+
{
|
|
456
|
+
id: 'ds.store.findRecord.id-mismatch',
|
|
457
|
+
}
|
|
458
|
+
);
|
|
459
|
+
|
|
460
|
+
return payload;
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
fetchItem.resolver.resolve(promise);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
function _processCoalescedGroup(
|
|
467
|
+
store: Store,
|
|
468
|
+
fetchMap: Map<Snapshot, PendingFetchItem>,
|
|
469
|
+
group: Snapshot[],
|
|
470
|
+
adapter: MinimumAdapterInterface,
|
|
471
|
+
modelName: string
|
|
472
|
+
) {
|
|
473
|
+
if (group.length > 1) {
|
|
474
|
+
_findMany(store, adapter, modelName, group)
|
|
475
|
+
.then((payloads: CollectionResourceDocument) => {
|
|
476
|
+
handleFoundRecords(store, fetchMap, group, payloads);
|
|
477
|
+
})
|
|
478
|
+
.catch((error) => {
|
|
479
|
+
rejectFetchedItems(fetchMap, group, error);
|
|
480
|
+
});
|
|
481
|
+
} else if (group.length === 1) {
|
|
482
|
+
_fetchRecord(store, fetchMap.get(group[0])!);
|
|
483
|
+
} else {
|
|
484
|
+
assert("You cannot return an empty array from adapter's method groupRecordsForFindMany", false);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
function _flushPendingFetchForType(store: Store, pendingFetchItems: PendingFetchItem[], modelName: string) {
|
|
489
|
+
let adapter = store.adapterFor(modelName);
|
|
490
|
+
let shouldCoalesce = !!adapter.findMany && adapter.coalesceFindRequests;
|
|
491
|
+
let totalItems = pendingFetchItems.length;
|
|
492
|
+
|
|
493
|
+
if (shouldCoalesce) {
|
|
494
|
+
let snapshots = new Array<Snapshot>(totalItems);
|
|
495
|
+
let fetchMap = new Map();
|
|
496
|
+
for (let i = 0; i < totalItems; i++) {
|
|
497
|
+
let fetchItem = pendingFetchItems[i];
|
|
498
|
+
snapshots[i] = new Snapshot(fetchItem.options, fetchItem.identifier, store);
|
|
499
|
+
fetchMap.set(snapshots[i], fetchItem);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
let groups: Snapshot[][];
|
|
503
|
+
if (adapter.groupRecordsForFindMany) {
|
|
504
|
+
groups = adapter.groupRecordsForFindMany(store, snapshots);
|
|
505
|
+
} else {
|
|
506
|
+
groups = [snapshots];
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
for (let i = 0, l = groups.length; i < l; i++) {
|
|
510
|
+
_processCoalescedGroup(store, fetchMap, groups[i], adapter, modelName);
|
|
511
|
+
}
|
|
512
|
+
} else {
|
|
513
|
+
for (let i = 0; i < totalItems; i++) {
|
|
514
|
+
_fetchRecord(store, pendingFetchItems[i]);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
function _flushPendingSave(store: Store, pending: PendingSaveItem) {
|
|
520
|
+
const { snapshot, resolver, identifier, options } = pending;
|
|
521
|
+
const adapter = store.adapterFor(identifier.type);
|
|
522
|
+
const operation = options[SaveOp];
|
|
523
|
+
|
|
524
|
+
let modelName = snapshot.modelName;
|
|
525
|
+
let modelClass = store.modelFor(modelName);
|
|
526
|
+
const record = store._instanceCache.getRecord(identifier);
|
|
527
|
+
|
|
528
|
+
assert(`You tried to update a record but you have no adapter (for ${modelName})`, adapter);
|
|
529
|
+
assert(
|
|
530
|
+
`You tried to update a record but your adapter (for ${modelName}) does not implement '${operation}'`,
|
|
531
|
+
typeof adapter[operation] === 'function'
|
|
532
|
+
);
|
|
533
|
+
|
|
534
|
+
let promise = resolve().then(() => adapter[operation](store, modelClass, snapshot));
|
|
535
|
+
let serializer: SerializerWithParseErrors | null = store.serializerFor(modelName);
|
|
536
|
+
let label = `DS: Extract and notify about ${operation} completion of ${identifier}`;
|
|
537
|
+
|
|
538
|
+
assert(
|
|
539
|
+
`Your adapter's '${operation}' method must return a value, but it returned 'undefined'`,
|
|
540
|
+
promise !== undefined
|
|
541
|
+
);
|
|
542
|
+
|
|
543
|
+
promise = _guard(guardDestroyedStore(promise, store, label), _bind(_objectIsAlive, record)).then((adapterPayload) => {
|
|
544
|
+
if (!_objectIsAlive(record)) {
|
|
545
|
+
if (DEPRECATE_RSVP_PROMISE) {
|
|
546
|
+
deprecate(
|
|
547
|
+
`A Promise while saving ${modelName} did not resolve by the time your model was destroyed. This will error in a future release.`,
|
|
548
|
+
false,
|
|
549
|
+
{
|
|
550
|
+
id: 'ember-data:rsvp-unresolved-async',
|
|
551
|
+
until: '5.0',
|
|
552
|
+
for: '@ember-data/store',
|
|
553
|
+
since: {
|
|
554
|
+
available: '4.5',
|
|
555
|
+
enabled: '4.5',
|
|
556
|
+
},
|
|
557
|
+
}
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
if (adapterPayload) {
|
|
563
|
+
return normalizeResponseHelper(serializer, store, modelClass, adapterPayload, snapshot.id, operation);
|
|
564
|
+
}
|
|
565
|
+
});
|
|
566
|
+
resolver.resolve(promise);
|
|
567
|
+
}
|
|
@@ -2,8 +2,9 @@ import { assert } from '@ember/debug';
|
|
|
2
2
|
|
|
3
3
|
import { Promise } from 'rsvp';
|
|
4
4
|
|
|
5
|
-
import { guardDestroyedStore } from '
|
|
6
|
-
import { normalizeResponseHelper } from '
|
|
5
|
+
import { guardDestroyedStore } from '../utils/common';
|
|
6
|
+
import { normalizeResponseHelper } from '../utils/serializer-response';
|
|
7
|
+
import SnapshotRecordArray from './snapshot-record-array';
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
@module @ember-data/store
|
|
@@ -17,10 +18,10 @@ function payloadIsNotBlank(adapterPayload) {
|
|
|
17
18
|
}
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
export function _findAll(adapter, store, modelName, options) {
|
|
21
|
+
export function _findAll(adapter, store, modelName, options, snapshotArray) {
|
|
21
22
|
let modelClass = store.modelFor(modelName); // adapter.findAll depends on the class
|
|
22
23
|
let recordArray = store.peekAll(modelName);
|
|
23
|
-
|
|
24
|
+
snapshotArray = snapshotArray || new SnapshotRecordArray(store, recordArray, options);
|
|
24
25
|
let promise = Promise.resolve().then(() => adapter.findAll(store, modelClass, null, snapshotArray));
|
|
25
26
|
let label = 'DS: Handle Adapter#findAll of ' + modelClass;
|
|
26
27
|
|
|
@@ -36,8 +37,7 @@ export function _findAll(adapter, store, modelName, options) {
|
|
|
36
37
|
let payload = normalizeResponseHelper(serializer, store, modelClass, adapterPayload, null, 'findAll');
|
|
37
38
|
|
|
38
39
|
store._push(payload);
|
|
39
|
-
|
|
40
|
-
|
|
40
|
+
recordArray.isUpdating = false;
|
|
41
41
|
return recordArray;
|
|
42
42
|
},
|
|
43
43
|
null,
|
|
@@ -48,7 +48,13 @@ export function _findAll(adapter, store, modelName, options) {
|
|
|
48
48
|
export function _query(adapter, store, modelName, query, recordArray, options) {
|
|
49
49
|
let modelClass = store.modelFor(modelName); // adapter.query needs the class
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
// TODO @deprecate RecordArrays being passed to Adapters
|
|
52
|
+
recordArray =
|
|
53
|
+
recordArray ||
|
|
54
|
+
store.recordArrayManager.createArray({
|
|
55
|
+
type: modelName,
|
|
56
|
+
query,
|
|
57
|
+
});
|
|
52
58
|
let promise = Promise.resolve().then(() => adapter.query(store, modelClass, query, recordArray, options));
|
|
53
59
|
|
|
54
60
|
let label = `DS: Handle Adapter#query of ${modelName}`;
|
|
@@ -64,16 +70,7 @@ export function _query(adapter, store, modelName, query, recordArray, options) {
|
|
|
64
70
|
'The response to store.query is expected to be an array but it was a single record. Please wrap your response in an array or use `store.queryRecord` to query for a single record.',
|
|
65
71
|
Array.isArray(identifiers)
|
|
66
72
|
);
|
|
67
|
-
|
|
68
|
-
recordArray._setIdentifiers(identifiers, payload);
|
|
69
|
-
} else {
|
|
70
|
-
recordArray = store.recordArrayManager.createAdapterPopulatedRecordArray(
|
|
71
|
-
modelName,
|
|
72
|
-
query,
|
|
73
|
-
identifiers,
|
|
74
|
-
payload
|
|
75
|
-
);
|
|
76
|
-
}
|
|
73
|
+
store.recordArrayManager.populateManagedArray(recordArray, identifiers, payload);
|
|
77
74
|
|
|
78
75
|
return recordArray;
|
|
79
76
|
},
|