@warp-drive-mirror/json-api 5.6.0-alpha.14 → 5.6.0-alpha.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,569 +1,586 @@
1
- import type { Graph } from '@warp-drive-mirror/core/graph/-private';
2
- import type { CacheCapabilitiesManager } from '@warp-drive-mirror/core/types';
3
- import type { Cache, ChangedAttributesHash, RelationshipDiff } from '@warp-drive-mirror/core/types/cache';
4
- import type { Change } from '@warp-drive-mirror/core/types/cache/change';
5
- import type { Operation } from '@warp-drive-mirror/core/types/cache/operations';
6
- import type { CollectionRelationship, ResourceRelationship } from '@warp-drive-mirror/core/types/cache/relationship';
7
- import type { LocalRelationshipOperation } from '@warp-drive-mirror/core/types/graph';
8
- import type { StableDocumentIdentifier, StableExistingRecordIdentifier, StableRecordIdentifier } from '@warp-drive-mirror/core/types/identifier';
9
- import type { Value } from '@warp-drive-mirror/core/types/json/raw';
10
- import type { StructuredDataDocument, StructuredDocument, StructuredErrorDocument } from '@warp-drive-mirror/core/types/request';
11
- import type { CollectionResourceDataDocument, ResourceDocument, ResourceErrorDocument, ResourceMetaDocument, SingleResourceDataDocument } from '@warp-drive-mirror/core/types/spec/document';
12
- import type { ApiError } from '@warp-drive-mirror/core/types/spec/error';
13
- import type { CollectionResourceDocument, ExistingResourceObject, ResourceObject, SingleResourceDocument } from '@warp-drive-mirror/core/types/spec/json-api-raw';
1
+ import type { Graph } from "@warp-drive-mirror/core/graph/-private";
2
+ import type { CacheCapabilitiesManager } from "@warp-drive-mirror/core/types";
3
+ import type { Cache, ChangedAttributesHash, RelationshipDiff } from "@warp-drive-mirror/core/types/cache";
4
+ import type { Change } from "@warp-drive-mirror/core/types/cache/change";
5
+ import type { Operation } from "@warp-drive-mirror/core/types/cache/operations";
6
+ import type { CollectionRelationship, ResourceRelationship } from "@warp-drive-mirror/core/types/cache/relationship";
7
+ import type { LocalRelationshipOperation } from "@warp-drive-mirror/core/types/graph";
8
+ import type { StableDocumentIdentifier, StableExistingRecordIdentifier, StableRecordIdentifier } from "@warp-drive-mirror/core/types/identifier";
9
+ import type { Value } from "@warp-drive-mirror/core/types/json/raw";
10
+ import type { StructuredDataDocument, StructuredDocument, StructuredErrorDocument } from "@warp-drive-mirror/core/types/request";
11
+ import type { CollectionResourceDataDocument, ResourceDocument, ResourceErrorDocument, ResourceMetaDocument, SingleResourceDataDocument } from "@warp-drive-mirror/core/types/spec/document";
12
+ import type { ApiError } from "@warp-drive-mirror/core/types/spec/error";
13
+ import type { CollectionResourceDocument, ExistingResourceObject, ResourceObject, SingleResourceDocument } from "@warp-drive-mirror/core/types/spec/json-api-raw";
14
14
  interface CachedResource {
15
- id: string | null;
16
- remoteAttrs: Record<string, Value | undefined> | null;
17
- localAttrs: Record<string, Value | undefined> | null;
18
- defaultAttrs: Record<string, Value | undefined> | null;
19
- inflightAttrs: Record<string, Value | undefined> | null;
20
- changes: Record<string, [Value | undefined, Value]> | null;
21
- errors: ApiError[] | null;
22
- isNew: boolean;
23
- isDeleted: boolean;
24
- isDeletionCommitted: boolean;
25
- /**
26
- * debugging only
27
- *
28
- * @internal
29
- */
30
- inflightRelationships?: Record<string, unknown> | null;
15
+ id: string | null;
16
+ remoteAttrs: Record<string, Value | undefined> | null;
17
+ localAttrs: Record<string, Value | undefined> | null;
18
+ defaultAttrs: Record<string, Value | undefined> | null;
19
+ inflightAttrs: Record<string, Value | undefined> | null;
20
+ changes: Record<string, [Value | undefined, Value]> | null;
21
+ errors: ApiError[] | null;
22
+ isNew: boolean;
23
+ isDeleted: boolean;
24
+ isDeletionCommitted: boolean;
25
+ /**
26
+ * debugging only
27
+ *
28
+ * @internal
29
+ */
30
+ inflightRelationships?: Record<string, unknown> | null;
31
31
  }
32
32
  /**
33
- * ### JSONAPICache
34
- *
35
- * ```ts
36
- * import { JSONAPICache } from '@warp-drive-mirror/json-api';
37
- * ```
38
- *
39
- A {@link Cache} implementation tuned for [{json:api}](https://jsonapi.org/)
33
+ * ### JSONAPICache
34
+ *
35
+ * ```ts
36
+ * import { JSONAPICache } from '@warp-drive-mirror/json-api';
37
+ * ```
38
+ *
39
+ A {@link Cache} implementation tuned for [{json:api}](https://jsonapi.org/)
40
40
 
41
- This format excels at simiplifying common complex problems around cache
42
- consistency and information density. Because most API responses can be quickly
43
- transformed into {json:api} format without losing any information, WarpDrive
44
- recommends that most apps use this Cache implementation.
41
+ This format excels at simiplifying common complex problems around cache
42
+ consistency and information density. Because most API responses can be quickly
43
+ transformed into {json:api} format without losing any information, WarpDrive
44
+ recommends that most apps use this Cache implementation.
45
45
 
46
- If a cache built to understand another format would do better for your app then
47
- it just needs to follow the same interface.
46
+ If a cache built to understand another format would do better for your app then
47
+ it just needs to follow the same interface.
48
48
 
49
- Do you really need a cache? Are sunsets beautiful? Caching is what powers features like
50
- immutability, mutation management, and allows ***Warp*Drive** to understand your relational
51
- data.
49
+ Do you really need a cache? Are sunsets beautiful? Caching is what powers features like
50
+ immutability, mutation management, and allows ***Warp*Drive** to understand your relational
51
+ data.
52
52
 
53
- Some caches are simple request/response maps. ***Warp*Drive**'s is not. The Cache deeply
54
- understands the structure of your data, ensuring your data remains consistent both within
55
- and across requests.
53
+ Some caches are simple request/response maps. ***Warp*Drive**'s is not. The Cache deeply
54
+ understands the structure of your data, ensuring your data remains consistent both within
55
+ and across requests.
56
56
 
57
- ### Installation
57
+ ### Installation
58
58
 
59
- ::: code-group
59
+ ::: code-group
60
60
 
61
- ```sh [pnpm]
62
- pnpm add -E @warp-drive-mirror/core@latest @warp-drive-mirror/json-api@latest
63
- ```
61
+ ```sh [pnpm]
62
+ pnpm add -E @warp-drive-mirror/core@latest @warp-drive-mirror/json-api@latest
63
+ ```
64
64
 
65
- ```sh [npm]
66
- npm add -E @warp-drive-mirror/core@latest @warp-drive-mirror/json-api@latest
67
- ```
65
+ ```sh [npm]
66
+ npm add -E @warp-drive-mirror/core@latest @warp-drive-mirror/json-api@latest
67
+ ```
68
68
 
69
- ```sh [yarn]
70
- yarn add -E @warp-drive-mirror/core@latest @warp-drive-mirror/json-api@latest
71
- ```
69
+ ```sh [yarn]
70
+ yarn add -E @warp-drive-mirror/core@latest @warp-drive-mirror/json-api@latest
71
+ ```
72
72
 
73
- ```sh [bun]
74
- bun add --exact @warp-drive-mirror/core@latest @warp-drive-mirror/json-api@latest
75
- ```
73
+ ```sh [bun]
74
+ bun add --exact @warp-drive-mirror/core@latest @warp-drive-mirror/json-api@latest
75
+ ```
76
76
 
77
- :::
77
+ :::
78
78
 
79
- ### Setup
79
+ ### Setup
80
80
 
81
- ```ts [services/store.ts]
82
- import { Fetch, RequestManager, Store } from '@warp-drive-mirror/core';
83
- import {
84
- registerDerivations,
85
- SchemaService,
86
- } from '@warp-drive-mirror/core/reactive';
87
- import { CacheHandler } from '@warp-drive-mirror/core/store';
88
- import type { CacheCapabilitiesManager } from '@warp-drive-mirror/core/types'; // [!code focus:2]
89
- import { JSONAPICache } from '@warp-drive-mirror/json-api';
81
+ ```ts [services/store.ts]
82
+ import { Fetch, RequestManager, Store } from '@warp-drive-mirror/core';
83
+ import {
84
+ registerDerivations,
85
+ SchemaService,
86
+ } from '@warp-drive-mirror/core/reactive';
87
+ import { CacheHandler } from '@warp-drive-mirror/core/store';
88
+ import type { CacheCapabilitiesManager } from '@warp-drive-mirror/core/types'; // [!code focus:2]
89
+ import { JSONAPICache } from '@warp-drive-mirror/json-api';
90
90
 
91
- export default class AppStore extends Store {
91
+ export default class AppStore extends Store {
92
92
 
93
- requestManager = new RequestManager()
94
- .use([Fetch])
95
- .useCache(CacheHandler);
93
+ requestManager = new RequestManager()
94
+ .use([Fetch])
95
+ .useCache(CacheHandler);
96
96
 
97
- createSchemaService() {
98
- const schema = new SchemaService();
99
- registerDerivations(schema);
100
- return schema;
101
- }
97
+ createSchemaService() {
98
+ const schema = new SchemaService();
99
+ registerDerivations(schema);
100
+ return schema;
101
+ }
102
102
 
103
- createCache(capabilities: CacheCapabilitiesManager) { // [!code focus:3]
104
- return new JSONAPICache(capabilities);
105
- }
106
- }
107
- ```
103
+ createCache(capabilities: CacheCapabilitiesManager) { // [!code focus:3]
104
+ return new JSONAPICache(capabilities);
105
+ }
106
+ }
107
+ ```
108
108
 
109
- * @categoryDescription Cache Management
110
- * APIs for primary cache management functionality
111
- * @categoryDescription Cache Forking
112
- * APIs that support Cache Forking
113
- * @categoryDescription SSR Support
114
- * APIs that support SSR functionality
115
- * @categoryDescription Resource Lifecycle
116
- * APIs that support management of resource data
117
- * @categoryDescription Resource Data
118
- * APIs that support granular field level management of resource data
119
- * @categoryDescription Resource State
120
- * APIs that support managing Resource states
109
+ * @categoryDescription Cache Management
110
+ * APIs for primary cache management functionality
111
+ * @categoryDescription Cache Forking
112
+ * APIs that support Cache Forking
113
+ * @categoryDescription SSR Support
114
+ * APIs that support SSR functionality
115
+ * @categoryDescription Resource Lifecycle
116
+ * APIs that support management of resource data
117
+ * @categoryDescription Resource Data
118
+ * APIs that support granular field level management of resource data
119
+ * @categoryDescription Resource State
120
+ * APIs that support managing Resource states
121
121
 
122
- @public
123
- */
122
+ @public
123
+ */
124
124
  export declare class JSONAPICache implements Cache {
125
- /**
126
- * The Cache Version that this implementation implements.
127
- *
128
- * @public
129
- */
130
- version: '2';
131
- /** @internal */
132
- _capabilities: CacheCapabilitiesManager;
133
- /** @internal */
134
- __cache: Map<StableRecordIdentifier, CachedResource>;
135
- /** @internal */
136
- __destroyedCache: Map<StableRecordIdentifier, CachedResource>;
137
- /** @internal */
138
- __documents: Map<string, StructuredDocument<ResourceDocument>>;
139
- /** @internal */
140
- __graph: Graph;
141
- constructor(capabilities: CacheCapabilitiesManager);
142
- /**
143
- * Cache the response to a request
144
- *
145
- * Implements `Cache.put`.
146
- *
147
- * Expects a StructuredDocument whose `content` member is a JsonApiDocument.
148
- *
149
- * ```js
150
- * cache.put({
151
- * request: { url: 'https://api.example.com/v1/user/1' },
152
- * content: {
153
- * data: {
154
- * type: 'user',
155
- * id: '1',
156
- * attributes: {
157
- * name: 'Chris'
158
- * }
159
- * }
160
- * }
161
- * })
162
- * ```
163
- *
164
- * > **Note**
165
- * > The nested `content` and `data` members are not a mistake. This is because
166
- * > there are two separate concepts involved here, the `StructuredDocument` which contains
167
- * > the context of a given Request that has been issued with the returned contents as its
168
- * > `content` property, and a `JSON:API Document` which is the json contents returned by
169
- * > this endpoint and which uses its `data` property to signify which resources are the
170
- * > primary resources associated with the request.
171
- *
172
- * StructuredDocument's with urls will be cached as full documents with
173
- * associated resource membership order and contents preserved but linked
174
- * into the cache.
175
- *
176
- * @category Cache Management
177
- * @public
178
- */
179
- put<T extends SingleResourceDocument>(doc: StructuredDataDocument<T>): SingleResourceDataDocument;
180
- put<T extends CollectionResourceDocument>(doc: StructuredDataDocument<T>): CollectionResourceDataDocument;
181
- put<T extends ResourceErrorDocument>(doc: StructuredErrorDocument<T>): ResourceErrorDocument;
182
- put<T extends ResourceMetaDocument>(doc: StructuredDataDocument<T>): ResourceMetaDocument;
183
- /** @internal */
184
- _putDocument<T extends ResourceErrorDocument>(doc: StructuredErrorDocument<T>, data: undefined, included: undefined): ResourceErrorDocument;
185
- _putDocument<T extends ResourceMetaDocument>(doc: StructuredDataDocument<T>, data: undefined, included: undefined): ResourceMetaDocument;
186
- _putDocument<T extends SingleResourceDocument>(doc: StructuredDataDocument<T>, data: StableExistingRecordIdentifier | null, included: StableExistingRecordIdentifier[] | undefined): SingleResourceDataDocument;
187
- _putDocument<T extends CollectionResourceDocument>(doc: StructuredDataDocument<T>, data: StableExistingRecordIdentifier[], included: StableExistingRecordIdentifier[] | undefined): CollectionResourceDataDocument;
188
- /**
189
- * Update the "remote" or "canonical" (persisted) state of the Cache
190
- * by merging new information into the existing state.
191
- *
192
- * @category Cache Management
193
- * @public
194
- * @param op the operation or list of operations to perform
195
- */
196
- patch(op: Operation | Operation[]): void;
197
- /**
198
- * Update the "local" or "current" (unpersisted) state of the Cache
199
- *
200
- * @category Cache Management
201
- * @public
202
- */
203
- mutate(mutation: LocalRelationshipOperation): void;
204
- /**
205
- * Peek resource data from the Cache.
206
- *
207
- * In development, if the return value
208
- * is JSON the return value
209
- * will be deep-cloned and deep-frozen
210
- * to prevent mutation thereby enforcing cache
211
- * Immutability.
212
- *
213
- * This form of peek is useful for implementations
214
- * that want to feed raw-data from cache to the UI
215
- * or which want to interact with a blob of data
216
- * directly from the presentation cache.
217
- *
218
- * An implementation might want to do this because
219
- * de-referencing records which read from their own
220
- * blob is generally safer because the record does
221
- * not require retainining connections to the Store
222
- * and Cache to present data on a per-field basis.
223
- *
224
- * This generally takes the place of `getAttr` as
225
- * an API and may even take the place of `getRelationship`
226
- * depending on implementation specifics, though this
227
- * latter usage is less recommended due to the advantages
228
- * of the Graph handling necessary entanglements and
229
- * notifications for relational data.
230
- *
231
- * @category Cache Management
232
- * @public
233
- */
234
- peek(identifier: StableRecordIdentifier): ResourceObject | null;
235
- peek(identifier: StableDocumentIdentifier): ResourceDocument | null;
236
- /**
237
- * Peek the remote resource data from the Cache.
238
- *
239
- * @category Cache Management
240
- * @public
241
- */
242
- peekRemoteState(identifier: StableRecordIdentifier): ResourceObject | null;
243
- peekRemoteState(identifier: StableDocumentIdentifier): ResourceDocument | null;
244
- /**
245
- * Peek the Cache for the existing request data associated with
246
- * a cacheable request.
247
- *
248
- * This is effectively the reverse of `put` for a request in
249
- * that it will return the the request, response, and content
250
- * whereas `peek` will return just the `content`.
251
- *
252
- * @category Cache Management
253
- * @public
254
- */
255
- peekRequest(identifier: StableDocumentIdentifier): StructuredDocument<ResourceDocument> | null;
256
- /**
257
- * Push resource data from a remote source into the cache for this identifier
258
- *
259
- * @category Cache Management
260
- * @public
261
- * @return if `calculateChanges` is true then calculated key changes should be returned
262
- */
263
- upsert(identifier: StableRecordIdentifier, data: ExistingResourceObject, calculateChanges?: boolean): void | string[];
264
- /**
265
- * Create a fork of the cache from the current state.
266
- *
267
- * Applications should typically not call this method themselves,
268
- * preferring instead to fork at the Store level, which will
269
- * utilize this method to fork the cache.
270
- *
271
- * @category Cache Forking
272
- * @internal
273
- */
274
- fork(): Promise<Cache>;
275
- /**
276
- * Merge a fork back into a parent Cache.
277
- *
278
- * Applications should typically not call this method themselves,
279
- * preferring instead to merge at the Store level, which will
280
- * utilize this method to merge the caches.
281
- *
282
- * @category Cache Forking
283
- * @internal
284
- */
285
- merge(_cache: Cache): Promise<void>;
286
- /**
287
- * Generate the list of changes applied to all
288
- * record in the store.
289
- *
290
- * Each individual resource or document that has
291
- * been mutated should be described as an individual
292
- * `Change` entry in the returned array.
293
- *
294
- * A `Change` is described by an object containing up to
295
- * three properties: (1) the `identifier` of the entity that
296
- * changed; (2) the `op` code of that change being one of
297
- * `upsert` or `remove`, and if the op is `upsert` a `patch`
298
- * containing the data to merge into the cache for the given
299
- * entity.
300
- *
301
- * This `patch` is opaque to the Store but should be understood
302
- * by the Cache and may expect to be utilized by an Adapter
303
- * when generating data during a `save` operation.
304
- *
305
- * It is generally recommended that the `patch` contain only
306
- * the updated state, ignoring fields that are unchanged
307
- *
308
- * ```ts
309
- * interface Change {
310
- * identifier: StableRecordIdentifier | StableDocumentIdentifier;
311
- * op: 'upsert' | 'remove';
312
- * patch?: unknown;
313
- * }
314
- * ```
315
- *
316
- * @category Cache Forking
317
- * @internal
318
- */
319
- diff(): Promise<Change[]>;
320
- /**
321
- * Serialize the entire contents of the Cache into a Stream
322
- * which may be fed back into a new instance of the same Cache
323
- * via `cache.hydrate`.
324
- *
325
- * @category SSR Support
326
- * @internal
327
- */
328
- dump(): Promise<ReadableStream<unknown>>;
329
- /**
330
- * hydrate a Cache from a Stream with content previously serialized
331
- * from another instance of the same Cache, resolving when hydration
332
- * is complete.
333
- *
334
- * This method should expect to be called both in the context of restoring
335
- * the Cache during application rehydration after SSR **AND** at unknown
336
- * times during the lifetime of an already booted application when it is
337
- * desired to bulk-load additional information into the cache. This latter
338
- * behavior supports optimizing pre/fetching of data for route transitions
339
- * via data-only SSR modes.
340
- *
341
- * @category SSR Support
342
- * @internal
343
- */
344
- hydrate(stream: ReadableStream<unknown>): Promise<void>;
345
- /**
346
- * [LIFECYCLE] Signal to the cache that a new record has been instantiated on the client
347
- *
348
- * It returns properties from options that should be set on the record during the create
349
- * process. This return value behavior is deprecated.
350
- *
351
- * @category Resource Lifecycle
352
- * @public
353
- */
354
- clientDidCreate(identifier: StableRecordIdentifier, options?: Record<string, Value>): Record<string, unknown>;
355
- /**
356
- * [LIFECYCLE] Signals to the cache that a resource
357
- * will be part of a save transaction.
358
- *
359
- * @category Resource Lifecycle
360
- * @public
361
- */
362
- willCommit(identifier: StableRecordIdentifier): void;
363
- /**
364
- * [LIFECYCLE] Signals to the cache that a resource
365
- * was successfully updated as part of a save transaction.
366
- *
367
- * @category Resource Lifecycle
368
- * @public
369
- */
370
- didCommit(committedIdentifier: StableRecordIdentifier, result: StructuredDataDocument<SingleResourceDocument>): SingleResourceDataDocument;
371
- /**
372
- * [LIFECYCLE] Signals to the cache that a resource
373
- * was update via a save transaction failed.
374
- *
375
- * @category Resource Lifecycle
376
- * @public
377
- */
378
- commitWasRejected(identifier: StableRecordIdentifier, errors?: ApiError[]): void;
379
- /**
380
- * [LIFECYCLE] Signals to the cache that all data for a resource
381
- * should be cleared.
382
- *
383
- * This method is a candidate to become a mutation
384
- *
385
- * @category Resource Lifecycle
386
- * @public
387
- */
388
- unloadRecord(identifier: StableRecordIdentifier): void;
389
- /**
390
- * Retrieve the data for an attribute from the cache
391
- * with local mutations applied.
392
- *
393
- * @category Resource Data
394
- * @public
395
- */
396
- getAttr(identifier: StableRecordIdentifier, attr: string | string[]): Value | undefined;
397
- /**
398
- * Retrieve the remote data for an attribute from the cache
399
- *
400
- * @category Resource Data
401
- * @public
402
- */
403
- getRemoteAttr(identifier: StableRecordIdentifier, attr: string | string[]): Value | undefined;
404
- /**
405
- * Mutate the data for an attribute in the cache
406
- *
407
- * This method is a candidate to become a mutation
408
- *
409
- * @category Resource Data
410
- * @public
411
- */
412
- setAttr(identifier: StableRecordIdentifier, attr: string | string[], value: Value): void;
413
- /**
414
- * Query the cache for the changed attributes of a resource.
415
- *
416
- * @category Resource Data
417
- * @public
418
- * @return `{ '<field>': ['<old>', '<new>'] }`
419
- */
420
- changedAttrs(identifier: StableRecordIdentifier): ChangedAttributesHash;
421
- /**
422
- * Query the cache for whether any mutated attributes exist
423
- *
424
- * @category Resource Data
425
- * @public
426
- */
427
- hasChangedAttrs(identifier: StableRecordIdentifier): boolean;
428
- /**
429
- * Tell the cache to discard any uncommitted mutations to attributes
430
- *
431
- * This method is a candidate to become a mutation
432
- *
433
- * @category Resource Data
434
- * @public
435
- * @return the names of fields that were restored
436
- */
437
- rollbackAttrs(identifier: StableRecordIdentifier): string[];
438
- /**
439
- * Query the cache for the changes to relationships of a resource.
440
- *
441
- * Returns a map of relationship names to RelationshipDiff objects.
442
- *
443
- * ```ts
444
- * type RelationshipDiff =
445
- | {
446
- kind: 'collection';
447
- remoteState: StableRecordIdentifier[];
448
- additions: Set<StableRecordIdentifier>;
449
- removals: Set<StableRecordIdentifier>;
450
- localState: StableRecordIdentifier[];
451
- reordered: boolean;
452
- }
453
- | {
454
- kind: 'resource';
455
- remoteState: StableRecordIdentifier | null;
456
- localState: StableRecordIdentifier | null;
457
- };
458
- ```
459
- *
460
- * @category Resource Data
461
- * @public
462
- */
463
- changedRelationships(identifier: StableRecordIdentifier): Map<string, RelationshipDiff>;
464
- /**
465
- * Query the cache for whether any mutated relationships exist
466
- *
467
- * @category Resource Data
468
- * @public
469
- */
470
- hasChangedRelationships(identifier: StableRecordIdentifier): boolean;
471
- /**
472
- * Tell the cache to discard any uncommitted mutations to relationships.
473
- *
474
- * This will also discard the change on any appropriate inverses.
475
- *
476
- * This method is a candidate to become a mutation
477
- *
478
- * @category Resource Data
479
- * @public
480
- * @return the names of relationships that were restored
481
- */
482
- rollbackRelationships(identifier: StableRecordIdentifier): string[];
483
- /**
484
- * Query the cache for the current state of a relationship property
485
- *
486
- * @category Resource Data
487
- * @public
488
- * @return resource relationship object
489
- */
490
- getRelationship(identifier: StableRecordIdentifier, field: string): ResourceRelationship | CollectionRelationship;
491
- /**
492
- * Query the cache for the remote state of a relationship property
493
- *
494
- * @category Resource Data
495
- * @public
496
- * @return resource relationship object
497
- */
498
- getRemoteRelationship(identifier: StableRecordIdentifier, field: string): ResourceRelationship | CollectionRelationship;
499
- /**
500
- * Update the cache state for the given resource to be marked
501
- * as locally deleted, or remove such a mark.
502
- *
503
- * This method is a candidate to become a mutation
504
- *
505
- * @category Resource State
506
- * @public
507
- */
508
- setIsDeleted(identifier: StableRecordIdentifier, isDeleted: boolean): void;
509
- /**
510
- * Query the cache for any validation errors applicable to the given resource.
511
- *
512
- * @category Resource State
513
- * @public
514
- */
515
- getErrors(identifier: StableRecordIdentifier): ApiError[];
516
- /**
517
- * Query the cache for whether a given resource has any available data
518
- *
519
- * @category Resource State
520
- * @public
521
- */
522
- isEmpty(identifier: StableRecordIdentifier): boolean;
523
- /**
524
- * Query the cache for whether a given resource was created locally and not
525
- * yet persisted.
526
- *
527
- * @category Resource State
528
- * @public
529
- */
530
- isNew(identifier: StableRecordIdentifier): boolean;
531
- /**
532
- * Query the cache for whether a given resource is marked as deleted (but not
533
- * necessarily persisted yet).
534
- *
535
- * @category Resource State
536
- * @public
537
- */
538
- isDeleted(identifier: StableRecordIdentifier): boolean;
539
- /**
540
- * Query the cache for whether a given resource has been deleted and that deletion
541
- * has also been persisted.
542
- *
543
- * @category Resource State
544
- * @public
545
- */
546
- isDeletionCommitted(identifier: StableRecordIdentifier): boolean;
547
- /**
548
- * Private method used to populate an entry for the identifier
549
- *
550
- * @internal
551
- */
552
- _createCache(identifier: StableRecordIdentifier): CachedResource;
553
- /**
554
- * Peek whether we have cached resource data matching the identifier
555
- * without asserting if the resource data is missing.
556
- *
557
- * @internal
558
- */
559
- __safePeek(identifier: StableRecordIdentifier, allowDestroyed: boolean): CachedResource | undefined;
560
- /**
561
- * Peek whether we have cached resource data matching the identifier
562
- * Asserts if the resource data is missing.
563
- *
564
- * @internal
565
- */
566
- __peek(identifier: StableRecordIdentifier, allowDestroyed: boolean): CachedResource;
125
+ /**
126
+ * The Cache Version that this implementation implements.
127
+ *
128
+ * @public
129
+ */
130
+ version: "2";
131
+ /** @internal */
132
+ _capabilities: CacheCapabilitiesManager;
133
+ /** @internal */
134
+ __cache: Map<StableRecordIdentifier, CachedResource>;
135
+ /** @internal */
136
+ __destroyedCache: Map<StableRecordIdentifier, CachedResource>;
137
+ /** @internal */
138
+ __documents: Map<string, StructuredDocument<ResourceDocument>>;
139
+ /** @internal */
140
+ __graph: Graph;
141
+ constructor(capabilities: CacheCapabilitiesManager);
142
+ ////////// ================ //////////
143
+ ////////// Cache Management //////////
144
+ ////////// ================ //////////
145
+ /**
146
+ * Cache the response to a request
147
+ *
148
+ * Implements `Cache.put`.
149
+ *
150
+ * Expects a StructuredDocument whose `content` member is a JsonApiDocument.
151
+ *
152
+ * ```js
153
+ * cache.put({
154
+ * request: { url: 'https://api.example.com/v1/user/1' },
155
+ * content: {
156
+ * data: {
157
+ * type: 'user',
158
+ * id: '1',
159
+ * attributes: {
160
+ * name: 'Chris'
161
+ * }
162
+ * }
163
+ * }
164
+ * })
165
+ * ```
166
+ *
167
+ * > **Note**
168
+ * > The nested `content` and `data` members are not a mistake. This is because
169
+ * > there are two separate concepts involved here, the `StructuredDocument` which contains
170
+ * > the context of a given Request that has been issued with the returned contents as its
171
+ * > `content` property, and a `JSON:API Document` which is the json contents returned by
172
+ * > this endpoint and which uses its `data` property to signify which resources are the
173
+ * > primary resources associated with the request.
174
+ *
175
+ * StructuredDocument's with urls will be cached as full documents with
176
+ * associated resource membership order and contents preserved but linked
177
+ * into the cache.
178
+ *
179
+ * @category Cache Management
180
+ * @public
181
+ */
182
+ put<T extends SingleResourceDocument>(doc: StructuredDataDocument<T>): SingleResourceDataDocument;
183
+ put<T extends CollectionResourceDocument>(doc: StructuredDataDocument<T>): CollectionResourceDataDocument;
184
+ put<T extends ResourceErrorDocument>(doc: StructuredErrorDocument<T>): ResourceErrorDocument;
185
+ put<T extends ResourceMetaDocument>(doc: StructuredDataDocument<T>): ResourceMetaDocument;
186
+ /** @internal */
187
+ _putDocument<T extends ResourceErrorDocument>(doc: StructuredErrorDocument<T>, data: undefined, included: undefined): ResourceErrorDocument;
188
+ _putDocument<T extends ResourceMetaDocument>(doc: StructuredDataDocument<T>, data: undefined, included: undefined): ResourceMetaDocument;
189
+ _putDocument<T extends SingleResourceDocument>(doc: StructuredDataDocument<T>, data: StableExistingRecordIdentifier | null, included: StableExistingRecordIdentifier[] | undefined): SingleResourceDataDocument;
190
+ _putDocument<T extends CollectionResourceDocument>(doc: StructuredDataDocument<T>, data: StableExistingRecordIdentifier[], included: StableExistingRecordIdentifier[] | undefined): CollectionResourceDataDocument;
191
+ /**
192
+ * Update the "remote" or "canonical" (persisted) state of the Cache
193
+ * by merging new information into the existing state.
194
+ *
195
+ * @category Cache Management
196
+ * @public
197
+ * @param op the operation or list of operations to perform
198
+ */
199
+ patch(op: Operation | Operation[]): void;
200
+ /**
201
+ * Update the "local" or "current" (unpersisted) state of the Cache
202
+ *
203
+ * @category Cache Management
204
+ * @public
205
+ */
206
+ mutate(mutation: LocalRelationshipOperation): void;
207
+ /**
208
+ * Peek resource data from the Cache.
209
+ *
210
+ * In development, if the return value
211
+ * is JSON the return value
212
+ * will be deep-cloned and deep-frozen
213
+ * to prevent mutation thereby enforcing cache
214
+ * Immutability.
215
+ *
216
+ * This form of peek is useful for implementations
217
+ * that want to feed raw-data from cache to the UI
218
+ * or which want to interact with a blob of data
219
+ * directly from the presentation cache.
220
+ *
221
+ * An implementation might want to do this because
222
+ * de-referencing records which read from their own
223
+ * blob is generally safer because the record does
224
+ * not require retainining connections to the Store
225
+ * and Cache to present data on a per-field basis.
226
+ *
227
+ * This generally takes the place of `getAttr` as
228
+ * an API and may even take the place of `getRelationship`
229
+ * depending on implementation specifics, though this
230
+ * latter usage is less recommended due to the advantages
231
+ * of the Graph handling necessary entanglements and
232
+ * notifications for relational data.
233
+ *
234
+ * @category Cache Management
235
+ * @public
236
+ */
237
+ peek(identifier: StableRecordIdentifier): ResourceObject | null;
238
+ peek(identifier: StableDocumentIdentifier): ResourceDocument | null;
239
+ /**
240
+ * Peek the remote resource data from the Cache.
241
+ *
242
+ * @category Cache Management
243
+ * @public
244
+ */
245
+ peekRemoteState(identifier: StableRecordIdentifier): ResourceObject | null;
246
+ peekRemoteState(identifier: StableDocumentIdentifier): ResourceDocument | null;
247
+ /**
248
+ * Peek the Cache for the existing request data associated with
249
+ * a cacheable request.
250
+ *
251
+ * This is effectively the reverse of `put` for a request in
252
+ * that it will return the the request, response, and content
253
+ * whereas `peek` will return just the `content`.
254
+ *
255
+ * @category Cache Management
256
+ * @public
257
+ */
258
+ peekRequest(identifier: StableDocumentIdentifier): StructuredDocument<ResourceDocument> | null;
259
+ /**
260
+ * Push resource data from a remote source into the cache for this identifier
261
+ *
262
+ * @category Cache Management
263
+ * @public
264
+ * @return if `calculateChanges` is true then calculated key changes should be returned
265
+ */
266
+ upsert(identifier: StableRecordIdentifier, data: ExistingResourceObject, calculateChanges?: boolean): void | string[];
267
+ ////////// ============= //////////
268
+ ////////// Cache Forking //////////
269
+ ////////// ============= //////////
270
+ /**
271
+ * Create a fork of the cache from the current state.
272
+ *
273
+ * Applications should typically not call this method themselves,
274
+ * preferring instead to fork at the Store level, which will
275
+ * utilize this method to fork the cache.
276
+ *
277
+ * @category Cache Forking
278
+ * @internal
279
+ */
280
+ fork(): Promise<Cache>;
281
+ /**
282
+ * Merge a fork back into a parent Cache.
283
+ *
284
+ * Applications should typically not call this method themselves,
285
+ * preferring instead to merge at the Store level, which will
286
+ * utilize this method to merge the caches.
287
+ *
288
+ * @category Cache Forking
289
+ * @internal
290
+ */
291
+ merge(_cache: Cache): Promise<void>;
292
+ /**
293
+ * Generate the list of changes applied to all
294
+ * record in the store.
295
+ *
296
+ * Each individual resource or document that has
297
+ * been mutated should be described as an individual
298
+ * `Change` entry in the returned array.
299
+ *
300
+ * A `Change` is described by an object containing up to
301
+ * three properties: (1) the `identifier` of the entity that
302
+ * changed; (2) the `op` code of that change being one of
303
+ * `upsert` or `remove`, and if the op is `upsert` a `patch`
304
+ * containing the data to merge into the cache for the given
305
+ * entity.
306
+ *
307
+ * This `patch` is opaque to the Store but should be understood
308
+ * by the Cache and may expect to be utilized by an Adapter
309
+ * when generating data during a `save` operation.
310
+ *
311
+ * It is generally recommended that the `patch` contain only
312
+ * the updated state, ignoring fields that are unchanged
313
+ *
314
+ * ```ts
315
+ * interface Change {
316
+ * identifier: StableRecordIdentifier | StableDocumentIdentifier;
317
+ * op: 'upsert' | 'remove';
318
+ * patch?: unknown;
319
+ * }
320
+ * ```
321
+ *
322
+ * @category Cache Forking
323
+ * @internal
324
+ */
325
+ diff(): Promise<Change[]>;
326
+ ////////// =========== //////////
327
+ ////////// SSR Support //////////
328
+ ////////// =========== //////////
329
+ /**
330
+ * Serialize the entire contents of the Cache into a Stream
331
+ * which may be fed back into a new instance of the same Cache
332
+ * via `cache.hydrate`.
333
+ *
334
+ * @category SSR Support
335
+ * @internal
336
+ */
337
+ dump(): Promise<ReadableStream<unknown>>;
338
+ /**
339
+ * hydrate a Cache from a Stream with content previously serialized
340
+ * from another instance of the same Cache, resolving when hydration
341
+ * is complete.
342
+ *
343
+ * This method should expect to be called both in the context of restoring
344
+ * the Cache during application rehydration after SSR **AND** at unknown
345
+ * times during the lifetime of an already booted application when it is
346
+ * desired to bulk-load additional information into the cache. This latter
347
+ * behavior supports optimizing pre/fetching of data for route transitions
348
+ * via data-only SSR modes.
349
+ *
350
+ * @category SSR Support
351
+ * @internal
352
+ */
353
+ hydrate(stream: ReadableStream<unknown>): Promise<void>;
354
+ ////////// ================== //////////
355
+ ////////// Resource Lifecycle //////////
356
+ ////////// ================== //////////
357
+ /**
358
+ * [LIFECYCLE] Signal to the cache that a new record has been instantiated on the client
359
+ *
360
+ * It returns properties from options that should be set on the record during the create
361
+ * process. This return value behavior is deprecated.
362
+ *
363
+ * @category Resource Lifecycle
364
+ * @public
365
+ */
366
+ clientDidCreate(identifier: StableRecordIdentifier, options?: Record<string, Value>): Record<string, unknown>;
367
+ /**
368
+ * [LIFECYCLE] Signals to the cache that a resource
369
+ * will be part of a save transaction.
370
+ *
371
+ * @category Resource Lifecycle
372
+ * @public
373
+ */
374
+ willCommit(identifier: StableRecordIdentifier): void;
375
+ /**
376
+ * [LIFECYCLE] Signals to the cache that a resource
377
+ * was successfully updated as part of a save transaction.
378
+ *
379
+ * @category Resource Lifecycle
380
+ * @public
381
+ */
382
+ didCommit(committedIdentifier: StableRecordIdentifier, result: StructuredDataDocument<SingleResourceDocument>): SingleResourceDataDocument;
383
+ /**
384
+ * [LIFECYCLE] Signals to the cache that a resource
385
+ * was update via a save transaction failed.
386
+ *
387
+ * @category Resource Lifecycle
388
+ * @public
389
+ */
390
+ commitWasRejected(identifier: StableRecordIdentifier, errors?: ApiError[]): void;
391
+ /**
392
+ * [LIFECYCLE] Signals to the cache that all data for a resource
393
+ * should be cleared.
394
+ *
395
+ * This method is a candidate to become a mutation
396
+ *
397
+ * @category Resource Lifecycle
398
+ * @public
399
+ */
400
+ unloadRecord(identifier: StableRecordIdentifier): void;
401
+ ////////// ============= //////////
402
+ ////////// Resource Data //////////
403
+ ////////// ============= //////////
404
+ /**
405
+ * Retrieve the data for an attribute from the cache
406
+ * with local mutations applied.
407
+ *
408
+ * @category Resource Data
409
+ * @public
410
+ */
411
+ getAttr(identifier: StableRecordIdentifier, attr: string | string[]): Value | undefined;
412
+ /**
413
+ * Retrieve the remote data for an attribute from the cache
414
+ *
415
+ * @category Resource Data
416
+ * @public
417
+ */
418
+ getRemoteAttr(identifier: StableRecordIdentifier, attr: string | string[]): Value | undefined;
419
+ /**
420
+ * Mutate the data for an attribute in the cache
421
+ *
422
+ * This method is a candidate to become a mutation
423
+ *
424
+ * @category Resource Data
425
+ * @public
426
+ */
427
+ setAttr(identifier: StableRecordIdentifier, attr: string | string[], value: Value): void;
428
+ /**
429
+ * Query the cache for the changed attributes of a resource.
430
+ *
431
+ * @category Resource Data
432
+ * @public
433
+ * @return `{ '<field>': ['<old>', '<new>'] }`
434
+ */
435
+ changedAttrs(identifier: StableRecordIdentifier): ChangedAttributesHash;
436
+ /**
437
+ * Query the cache for whether any mutated attributes exist
438
+ *
439
+ * @category Resource Data
440
+ * @public
441
+ */
442
+ hasChangedAttrs(identifier: StableRecordIdentifier): boolean;
443
+ /**
444
+ * Tell the cache to discard any uncommitted mutations to attributes
445
+ *
446
+ * This method is a candidate to become a mutation
447
+ *
448
+ * @category Resource Data
449
+ * @public
450
+ * @return the names of fields that were restored
451
+ */
452
+ rollbackAttrs(identifier: StableRecordIdentifier): string[];
453
+ /**
454
+ * Query the cache for the changes to relationships of a resource.
455
+ *
456
+ * Returns a map of relationship names to RelationshipDiff objects.
457
+ *
458
+ * ```ts
459
+ * type RelationshipDiff =
460
+ | {
461
+ kind: 'collection';
462
+ remoteState: StableRecordIdentifier[];
463
+ additions: Set<StableRecordIdentifier>;
464
+ removals: Set<StableRecordIdentifier>;
465
+ localState: StableRecordIdentifier[];
466
+ reordered: boolean;
467
+ }
468
+ | {
469
+ kind: 'resource';
470
+ remoteState: StableRecordIdentifier | null;
471
+ localState: StableRecordIdentifier | null;
472
+ };
473
+ ```
474
+ *
475
+ * @category Resource Data
476
+ * @public
477
+ */
478
+ changedRelationships(identifier: StableRecordIdentifier): Map<string, RelationshipDiff>;
479
+ /**
480
+ * Query the cache for whether any mutated relationships exist
481
+ *
482
+ * @category Resource Data
483
+ * @public
484
+ */
485
+ hasChangedRelationships(identifier: StableRecordIdentifier): boolean;
486
+ /**
487
+ * Tell the cache to discard any uncommitted mutations to relationships.
488
+ *
489
+ * This will also discard the change on any appropriate inverses.
490
+ *
491
+ * This method is a candidate to become a mutation
492
+ *
493
+ * @category Resource Data
494
+ * @public
495
+ * @return the names of relationships that were restored
496
+ */
497
+ rollbackRelationships(identifier: StableRecordIdentifier): string[];
498
+ /**
499
+ * Query the cache for the current state of a relationship property
500
+ *
501
+ * @category Resource Data
502
+ * @public
503
+ * @return resource relationship object
504
+ */
505
+ getRelationship(identifier: StableRecordIdentifier, field: string): ResourceRelationship | CollectionRelationship;
506
+ /**
507
+ * Query the cache for the remote state of a relationship property
508
+ *
509
+ * @category Resource Data
510
+ * @public
511
+ * @return resource relationship object
512
+ */
513
+ getRemoteRelationship(identifier: StableRecordIdentifier, field: string): ResourceRelationship | CollectionRelationship;
514
+ ////////// ============== //////////
515
+ ////////// Resource State //////////
516
+ ////////// ============== //////////
517
+ /**
518
+ * Update the cache state for the given resource to be marked
519
+ * as locally deleted, or remove such a mark.
520
+ *
521
+ * This method is a candidate to become a mutation
522
+ *
523
+ * @category Resource State
524
+ * @public
525
+ */
526
+ setIsDeleted(identifier: StableRecordIdentifier, isDeleted: boolean): void;
527
+ /**
528
+ * Query the cache for any validation errors applicable to the given resource.
529
+ *
530
+ * @category Resource State
531
+ * @public
532
+ */
533
+ getErrors(identifier: StableRecordIdentifier): ApiError[];
534
+ /**
535
+ * Query the cache for whether a given resource has any available data
536
+ *
537
+ * @category Resource State
538
+ * @public
539
+ */
540
+ isEmpty(identifier: StableRecordIdentifier): boolean;
541
+ /**
542
+ * Query the cache for whether a given resource was created locally and not
543
+ * yet persisted.
544
+ *
545
+ * @category Resource State
546
+ * @public
547
+ */
548
+ isNew(identifier: StableRecordIdentifier): boolean;
549
+ /**
550
+ * Query the cache for whether a given resource is marked as deleted (but not
551
+ * necessarily persisted yet).
552
+ *
553
+ * @category Resource State
554
+ * @public
555
+ */
556
+ isDeleted(identifier: StableRecordIdentifier): boolean;
557
+ /**
558
+ * Query the cache for whether a given resource has been deleted and that deletion
559
+ * has also been persisted.
560
+ *
561
+ * @category Resource State
562
+ * @public
563
+ */
564
+ isDeletionCommitted(identifier: StableRecordIdentifier): boolean;
565
+ /**
566
+ * Private method used to populate an entry for the identifier
567
+ *
568
+ * @internal
569
+ */
570
+ _createCache(identifier: StableRecordIdentifier): CachedResource;
571
+ /**
572
+ * Peek whether we have cached resource data matching the identifier
573
+ * without asserting if the resource data is missing.
574
+ *
575
+ * @internal
576
+ */
577
+ __safePeek(identifier: StableRecordIdentifier, allowDestroyed: boolean): CachedResource | undefined;
578
+ /**
579
+ * Peek whether we have cached resource data matching the identifier
580
+ * Asserts if the resource data is missing.
581
+ *
582
+ * @internal
583
+ */
584
+ __peek(identifier: StableRecordIdentifier, allowDestroyed: boolean): CachedResource;
567
585
  }
568
586
  export {};
569
- //# sourceMappingURL=cache.d.ts.map