shelving 1.307.4 → 1.309.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.
Files changed (37) hide show
  1. package/api/index.d.ts +1 -0
  2. package/api/index.js +1 -0
  3. package/api/provider/HydratingAPIProvider.d.ts +28 -0
  4. package/api/provider/HydratingAPIProvider.js +82 -0
  5. package/db/cache/CollectionCache.d.ts +1 -7
  6. package/db/cache/CollectionCache.js +3 -9
  7. package/db/cache/DBCache.d.ts +1 -7
  8. package/db/cache/DBCache.js +2 -12
  9. package/db/collection/Collection.d.ts +24 -1
  10. package/db/collection/Collection.js +28 -2
  11. package/db/collection/CollectionItemSchema.d.ts +24 -0
  12. package/db/collection/CollectionItemSchema.js +23 -0
  13. package/db/collection/CollectionItemsSchema.d.ts +25 -0
  14. package/db/collection/CollectionItemsSchema.js +24 -0
  15. package/db/collection/CollectionQueryItemsSchema.d.ts +27 -0
  16. package/db/collection/CollectionQueryItemsSchema.js +25 -0
  17. package/db/collection/CollectionQuerySchema.d.ts +25 -0
  18. package/db/collection/CollectionQuerySchema.js +28 -0
  19. package/db/index.d.ts +4 -1
  20. package/db/index.js +4 -1
  21. package/db/provider/MemoryDBProvider.d.ts +42 -4
  22. package/db/provider/MemoryDBProvider.js +73 -6
  23. package/db/provider/RecordingDBProvider.d.ts +1 -1
  24. package/db/provider/RecordingDBProvider.js +1 -1
  25. package/db/provider/StorageDBProvider.d.ts +1 -1
  26. package/db/provider/StorageDBProvider.js +1 -1
  27. package/db/store/ItemStore.d.ts +6 -4
  28. package/db/store/ItemStore.js +55 -8
  29. package/db/store/QueryStore.d.ts +9 -99
  30. package/db/store/QueryStore.js +55 -260
  31. package/package.json +1 -1
  32. package/react/createDBContext.d.ts +1 -1
  33. package/react/createDBContext.js +1 -1
  34. package/util/query.d.ts +29 -6
  35. package/util/query.js +33 -6
  36. package/db/provider/CacheDBProvider.d.ts +0 -56
  37. package/db/provider/CacheDBProvider.js +0 -105
@@ -1,105 +0,0 @@
1
- import { awaitDispose } from "../../util/dispose.js";
2
- import { MemoryDBProvider } from "./MemoryDBProvider.js";
3
- import { RecordingDBProvider } from "./RecordingDBProvider.js";
4
- import { ThroughDBProvider } from "./ThroughDBProvider.js";
5
- /**
6
- * Database provider that keeps a copy of asynchronous remote data in a local synchronous cache.
7
- *
8
- * - Wraps a `source` provider and mirrors every read and write into an in-memory `MemoryDBProvider`, so subsequent reads can be served synchronously and live subscriptions stay seeded.
9
- * - Reads fetch from `source`, then refresh the cache; writes hit `source`, then mirror the change into the cache.
10
- * - Fetch-first item writes: `updateItem()` and `deleteItem()` fetch the item first (caching it) and skip the source write when it doesn't exist. Query writes are inherited two-step, resolving through this provider's own `getQuery()` — so the matched items are cached, and each per-item write mirrors exactly. The fetch and the writes are separate steps, so wrap them in `transact()` when they must be atomic.
11
- * - Transactions run on `source` via `transact()` with a transaction-scoped mirror — only a committed transaction's writes reach the cache.
12
- * - Discover the cache from a wrapping layer with `getSource(CacheDBProvider, provider)` to seed stores from `.memory`.
13
- *
14
- * @see https://shelving.cc/db/CacheDBProvider
15
- */
16
- export class CacheDBProvider extends ThroughDBProvider {
17
- /**
18
- * The in-memory provider holding the local synchronous cache of `source` data.
19
- *
20
- * @see https://shelving.cc/db/CacheDBProvider/memory
21
- */
22
- memory;
23
- /**
24
- * @param cache In-memory provider to use as the cache (a fresh `MemoryDBProvider` by default).
25
- */
26
- constructor(source, cache = new MemoryDBProvider()) {
27
- super(source);
28
- this.memory = cache;
29
- }
30
- /** Read from `source`, then refresh the cache. */
31
- async getItem(collection, id) {
32
- const item = await super.getItem(collection, id);
33
- const table = this.memory.getTable(collection);
34
- item ? table.setItem(id, item) : table.deleteItem(id);
35
- return item;
36
- }
37
- /** Mirror each emission into the cache. */
38
- getItemSequence(collection, id) {
39
- return this.memory.getTable(collection).setItemSequence(id, super.getItemSequence(collection, id));
40
- }
41
- /** Mirror the added item into the cache. */
42
- async addItem(collection, data) {
43
- const id = await super.addItem(collection, data);
44
- this.memory.getTable(collection).setItem(id, data);
45
- return id;
46
- }
47
- /** Mirror the set item into the cache. */
48
- async setItem(collection, id, data) {
49
- await super.setItem(collection, id, data);
50
- this.memory.getTable(collection).setItem(id, data);
51
- }
52
- /** Fetch the item first (caching it), then update it only if it exists. */
53
- async updateItem(collection, id, updates) {
54
- const item = await this.getItem(collection, id);
55
- if (!item)
56
- return;
57
- await super.updateItem(collection, id, updates);
58
- this.memory.getTable(collection).updateItem(id, updates);
59
- }
60
- /** Fetch the item first, then delete it only if it exists. */
61
- async deleteItem(collection, id) {
62
- const item = await this.getItem(collection, id);
63
- if (!item)
64
- return;
65
- await super.deleteItem(collection, id);
66
- this.memory.getTable(collection).deleteItem(id);
67
- }
68
- /** Read from `source`, then refresh the cache. */
69
- async getQuery(collection, query) {
70
- const items = await super.getQuery(collection, query);
71
- this.memory.getTable(collection).setItems(items);
72
- return items;
73
- }
74
- /** Mirror each emission into the cache. */
75
- getQuerySequence(collection, query) {
76
- return this.memory.getTable(collection).setItemsSequence(super.getQuerySequence(collection, query));
77
- }
78
- // Override so transaction copies get their own transaction-scoped mirror — uncommitted writes must never touch the real cache.
79
- cloneWith(source) {
80
- const clone = super.cloneWith(source);
81
- Object.defineProperty(clone, "memory", { value: new MemoryDBProvider(), enumerable: true });
82
- return clone;
83
- }
84
- /**
85
- * Runs the transaction on `source`, recording the callback's operations, then commits the recorded writes into the cache once the source commits.
86
- * - The callback's provider is this cache over the source's transaction (with its own transaction-scoped mirror), so fetch-first writes and query resolution behave exactly as they do outside a transaction — and the fetch-then-write steps are atomic because both run in the source transaction.
87
- * - Uncommitted data never touches the cache: a thrown callback commits nothing, and if the backend retries the callback only the committed attempt's writes are mirrored.
88
- * - Update writes commit to the cache as deltas, so they refresh cached items and skip uncached ones — an item only read inside the transaction stays uncached until its next read.
89
- */
90
- async transact(callback) {
91
- let transaction;
92
- const result = await this.source.transact(provider => {
93
- transaction = new RecordingDBProvider(this.cloneWith(provider));
94
- return callback(transaction);
95
- });
96
- if (transaction)
97
- await transaction.replayWrites(this.memory); // Commit the recorded writes into the cache.
98
- return result;
99
- }
100
- // Implement `AsyncDisposable`
101
- async [Symbol.asyncDispose]() {
102
- await awaitDispose(this.memory, // Dispose the cache memory provider.
103
- super[Symbol.asyncDispose]());
104
- }
105
- }