@procedures/store 0.8.0 → 0.9.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 (47) hide show
  1. package/CHANGELOG.md +62 -0
  2. package/README.md +88 -0
  3. package/dist/.tsbuildinfo +1 -1
  4. package/dist/indexeddb/sync-state-store.d.ts +40 -0
  5. package/dist/indexeddb/sync-state-store.d.ts.map +1 -0
  6. package/dist/indexeddb/sync-state-store.js +94 -0
  7. package/dist/indexeddb/sync-state-store.js.map +1 -0
  8. package/dist/indexeddb.d.ts +7 -2
  9. package/dist/indexeddb.d.ts.map +1 -1
  10. package/dist/indexeddb.js +6 -2
  11. package/dist/indexeddb.js.map +1 -1
  12. package/dist/storage/api.d.ts +84 -11
  13. package/dist/storage/api.d.ts.map +1 -1
  14. package/dist/storage/api.js +148 -25
  15. package/dist/storage/api.js.map +1 -1
  16. package/dist/storage/feed.d.ts +133 -0
  17. package/dist/storage/feed.d.ts.map +1 -0
  18. package/dist/storage/feed.js +159 -0
  19. package/dist/storage/feed.js.map +1 -0
  20. package/dist/storage/hybrid.d.ts +367 -20
  21. package/dist/storage/hybrid.d.ts.map +1 -1
  22. package/dist/storage/hybrid.js +1295 -206
  23. package/dist/storage/hybrid.js.map +1 -1
  24. package/dist/storage/index.d.ts +6 -2
  25. package/dist/storage/index.d.ts.map +1 -1
  26. package/dist/storage/index.js +6 -1
  27. package/dist/storage/index.js.map +1 -1
  28. package/dist/storage/sync-state.d.ts +56 -0
  29. package/dist/storage/sync-state.d.ts.map +1 -0
  30. package/dist/storage/sync-state.js +58 -0
  31. package/dist/storage/sync-state.js.map +1 -0
  32. package/dist/storage/synced-registry.d.ts +22 -4
  33. package/dist/storage/synced-registry.d.ts.map +1 -1
  34. package/dist/storage/synced-registry.js +63 -31
  35. package/dist/storage/synced-registry.js.map +1 -1
  36. package/dist/storage/types.d.ts +22 -1
  37. package/dist/storage/types.d.ts.map +1 -1
  38. package/package.json +4 -3
  39. package/src/indexeddb/sync-state-store.ts +101 -0
  40. package/src/indexeddb.ts +7 -2
  41. package/src/storage/api.ts +210 -29
  42. package/src/storage/feed.ts +251 -0
  43. package/src/storage/hybrid.ts +1404 -211
  44. package/src/storage/index.ts +17 -0
  45. package/src/storage/sync-state.ts +89 -0
  46. package/src/storage/synced-registry.ts +76 -31
  47. package/src/storage/types.ts +25 -1
@@ -0,0 +1,40 @@
1
+ /**
2
+ * A sync state store in IndexedDB, for the `HybridStorage` of a browser (roadmap CLI-21).
3
+ *
4
+ * The state is in the object store `state` of one database: one record for the cursor, and one
5
+ * record for the base version of each id. A change writes only the records that it names, in one
6
+ * transaction, so a reader never sees a part of it. Give each storage its own database.
7
+ */
8
+ import type { SyncState, SyncStateChange, SyncStateStore } from "../storage/sync-state.js";
9
+ import { type IndexedDbOptions } from "./idb.js";
10
+ /**
11
+ * A sync state store in IndexedDB.
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * import { HybridStorage } from "@procedures/store";
16
+ * import { IndexedDbQueueStore, IndexedDbSyncStateStore } from "@procedures/store/indexeddb";
17
+ *
18
+ * const users = new HybridStorage(remote, {
19
+ * local: durableLocalCopy,
20
+ * queueStore: new IndexedDbQueueStore("users-queue"),
21
+ * syncState: new IndexedDbSyncStateStore("users-sync"),
22
+ * });
23
+ * ```
24
+ */
25
+ export declare class IndexedDbSyncStateStore implements SyncStateStore {
26
+ private readonly connection;
27
+ /**
28
+ * @param name - The name of the database: one database for each storage. The store makes it at the first operation.
29
+ * @param options - The IndexedDB factory. The default is the `indexedDB` of the global scope.
30
+ */
31
+ constructor(name: string, options?: IndexedDbOptions);
32
+ /** The name of the database. */
33
+ get name(): string;
34
+ load(): Promise<SyncState | undefined>;
35
+ write(change: SyncStateChange): Promise<void>;
36
+ clear(): Promise<void>;
37
+ /** This method closes the database. The state stays in it. */
38
+ close(): Promise<void>;
39
+ }
40
+ //# sourceMappingURL=sync-state-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sync-state-store.d.ts","sourceRoot":"","sources":["../../src/indexeddb/sync-state-store.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC3F,OAAO,EAAsB,KAAK,gBAAgB,EAAE,MAAM,UAAU,CAAC;AA2BrE;;;;;;;;;;;;;;GAcG;AACH,qBAAa,uBAAwB,YAAW,cAAc;IAC5D,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAqB;IAEhD;;;OAGG;IACH,YAAY,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,gBAAqB,EAGvD;IAED,gCAAgC;IAChC,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,IAAI,IAAI,OAAO,CAAC,SAAS,GAAG,SAAS,CAAC,CAKrC;IAEK,KAAK,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC,CAYlD;IAEK,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAK3B;IAED,8DAA8D;IAC9D,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAErB;CACF"}
@@ -0,0 +1,94 @@
1
+ /**
2
+ * A sync state store in IndexedDB, for the `HybridStorage` of a browser (roadmap CLI-21).
3
+ *
4
+ * The state is in the object store `state` of one database: one record for the cursor, and one
5
+ * record for the base version of each id. A change writes only the records that it names, in one
6
+ * transaction, so a reader never sees a part of it. Give each storage its own database.
7
+ */
8
+ import { IdbStoreConnection } from "./idb.js";
9
+ /** The key of the record of the cursor. The keys of the bases start with `BASE`. */
10
+ const CURSOR_KEY = "cursor";
11
+ const BASE = "base:";
12
+ /** The state of the records, or an error for a record of another form. */
13
+ function stateOf(records, name) {
14
+ if (records.length === 0)
15
+ return undefined;
16
+ let cursor;
17
+ const bases = [];
18
+ records.forEach((value, index) => {
19
+ const record = value;
20
+ if (record?.kind === "cursor" && (record.cursor === undefined || typeof record.cursor === "string")) {
21
+ cursor = record.cursor;
22
+ }
23
+ else if (record?.kind === "base" && typeof record.id === "string" && (record.version === null || typeof record.version === "string")) {
24
+ bases.push([record.id, record.version]);
25
+ }
26
+ else {
27
+ throw new Error(`The sync state database ${name} has a record that is not a sync state (record ${index + 1})`);
28
+ }
29
+ });
30
+ return { ...(cursor !== undefined && { cursor }), bases };
31
+ }
32
+ /**
33
+ * A sync state store in IndexedDB.
34
+ *
35
+ * @example
36
+ * ```typescript
37
+ * import { HybridStorage } from "@procedures/store";
38
+ * import { IndexedDbQueueStore, IndexedDbSyncStateStore } from "@procedures/store/indexeddb";
39
+ *
40
+ * const users = new HybridStorage(remote, {
41
+ * local: durableLocalCopy,
42
+ * queueStore: new IndexedDbQueueStore("users-queue"),
43
+ * syncState: new IndexedDbSyncStateStore("users-sync"),
44
+ * });
45
+ * ```
46
+ */
47
+ export class IndexedDbSyncStateStore {
48
+ connection;
49
+ /**
50
+ * @param name - The name of the database: one database for each storage. The store makes it at the first operation.
51
+ * @param options - The IndexedDB factory. The default is the `indexedDB` of the global scope.
52
+ */
53
+ constructor(name, options = {}) {
54
+ if (typeof name !== "string" || name === "")
55
+ throw new TypeError("IndexedDbSyncStateStore needs the name of a database");
56
+ this.connection = new IdbStoreConnection(name, "state", {}, options.indexedDB);
57
+ }
58
+ /** The name of the database. */
59
+ get name() {
60
+ return this.connection.database;
61
+ }
62
+ load() {
63
+ return this.connection.run("readonly", (store) => {
64
+ const request = store.getAll();
65
+ return () => stateOf(request.result, this.name);
66
+ });
67
+ }
68
+ async write(change) {
69
+ await this.connection.run("readwrite", (store) => {
70
+ if (change.reset)
71
+ store.clear();
72
+ const cursor = change.cursor === undefined ? { kind: "cursor" } : { kind: "cursor", cursor: change.cursor };
73
+ store.put(cursor, CURSOR_KEY);
74
+ for (const [id, version] of change.bases) {
75
+ const record = { kind: "base", id, version };
76
+ store.put(record, BASE + id);
77
+ }
78
+ for (const id of change.forgotten)
79
+ store.delete(BASE + id);
80
+ return () => undefined;
81
+ });
82
+ }
83
+ async clear() {
84
+ await this.connection.run("readwrite", (store) => {
85
+ store.clear();
86
+ return () => undefined;
87
+ });
88
+ }
89
+ /** This method closes the database. The state stays in it. */
90
+ close() {
91
+ return this.connection.close();
92
+ }
93
+ }
94
+ //# sourceMappingURL=sync-state-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sync-state-store.js","sourceRoot":"","sources":["../../src/indexeddb/sync-state-store.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAAE,kBAAkB,EAAyB,MAAM,UAAU,CAAC;AAErE,oFAAoF;AACpF,MAAM,UAAU,GAAG,QAAQ,CAAC;AAC5B,MAAM,IAAI,GAAG,OAAO,CAAC;AAKrB,0EAA0E;AAC1E,SAAS,OAAO,CAAC,OAA2B,EAAE,IAAY;IACxD,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,SAAS,CAAC;IAC3C,IAAI,MAA0B,CAAC;IAC/B,MAAM,KAAK,GAAmC,EAAE,CAAC;IACjD,OAAO,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE;QAC/B,MAAM,MAAM,GAAG,KAA0F,CAAC;QAC1G,IAAI,MAAM,EAAE,IAAI,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,MAAM,KAAK,SAAS,IAAI,OAAO,MAAM,CAAC,MAAM,KAAK,QAAQ,CAAC,EAAE,CAAC;YACpG,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;QACzB,CAAC;aAAM,IAAI,MAAM,EAAE,IAAI,KAAK,MAAM,IAAI,OAAO,MAAM,CAAC,EAAE,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,OAAO,KAAK,IAAI,IAAI,OAAO,MAAM,CAAC,OAAO,KAAK,QAAQ,CAAC,EAAE,CAAC;YACvI,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC;QAC1C,CAAC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AAS,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC;YACzH,KAAK,CAAC,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;YAC9B,KAAK,MAAM,CAAC,EAAE,EAAE,OAAO,CAAC,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;gBACzC,MAAM,MAAM,GAAgB,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,OAAO,EAAE,CAAC;gBAC1D,KAAK,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,GAAG,EAAE,CAAC,CAAC;YAC/B,CAAC;YACD,KAAK,MAAM,EAAE,IAAI,MAAM,CAAC,SAAS;gBAAE,KAAK,CAAC,MAAM,CAAC,IAAI,GAAG,EAAE,CAAC,CAAC;YAC3D,OAAO,GAAG,EAAE,CAAC,SAAS,CAAC;QACzB,CAAC,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,KAAK;QACT,MAAM,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,KAAK,EAAE,EAAE;YAC/C,KAAK,CAAC,KAAK,EAAE,CAAC;YACd,OAAO,GAAG,EAAE,CAAC,SAAS,CAAC;QACzB,CAAC,CAAC,CAAC;IACL,CAAC;IAED,8DAA8D;IAC9D,KAAK;QACH,OAAO,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;IACjC,CAAC;CACF"}
@@ -3,17 +3,22 @@
3
3
  *
4
4
  * - `IndexedDbProgramStore` keeps programs in IndexedDB.
5
5
  * - `IndexedDbQueueStore` keeps the queue of a `HybridStorage` in IndexedDB.
6
- * - `MemoryProgramStore` and `MemoryQueueStore` are the fallbacks for a browser without
7
- * IndexedDB. `hasIndexedDb()` tells which one to use.
6
+ * - `IndexedDbSyncStateStore` keeps the sync state of a `HybridStorage` in IndexedDB: the cursor
7
+ * and the base versions (roadmap CLI-21).
8
+ * - `MemoryProgramStore`, `MemoryQueueStore` and `MemorySyncStateStore` are the fallbacks for a
9
+ * browser without IndexedDB. `hasIndexedDb()` tells which one to use.
8
10
  *
9
11
  * The package root does not import this entry, and this entry does not import the client or zod:
10
12
  * a page that loads it gets only these stores.
11
13
  */
12
14
  export { IndexedDbProgramStore, DEFAULT_PROGRAM_DATABASE } from "./indexeddb/program-store.js";
13
15
  export { IndexedDbQueueStore } from "./indexeddb/queue-store.js";
16
+ export { IndexedDbSyncStateStore } from "./indexeddb/sync-state-store.js";
14
17
  export { hasIndexedDb, type IdbFactoryLike, type IndexedDbOptions } from "./indexeddb/idb.js";
15
18
  export { MemoryProgramStore } from "./program-store/memory.js";
16
19
  export { MemoryQueueStore } from "./storage/queue.js";
20
+ export { MemorySyncStateStore } from "./storage/sync-state.js";
21
+ export type { SyncState, SyncStateChange, SyncStateStore } from "./storage/sync-state.js";
17
22
  export type { ProgramStore, StoredProgram } from "./program-store/types.js";
18
23
  export type { QueuedOperation, QueueStore } from "./storage/queue.js";
19
24
  //# sourceMappingURL=indexeddb.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"indexeddb.d.ts","sourceRoot":"","sources":["../src/indexeddb.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,qBAAqB,EAAE,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AAC/F,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,OAAO,EAAE,YAAY,EAAE,KAAK,cAAc,EAAE,KAAK,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAC9F,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAC5E,YAAY,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC"}
1
+ {"version":3,"file":"indexeddb.d.ts","sourceRoot":"","sources":["../src/indexeddb.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,qBAAqB,EAAE,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AAC/F,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAC1E,OAAO,EAAE,YAAY,EAAE,KAAK,cAAc,EAAE,KAAK,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAC9F,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAC/D,YAAY,EAAE,SAAS,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAC1F,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAC5E,YAAY,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC"}
package/dist/indexeddb.js CHANGED
@@ -3,15 +3,19 @@
3
3
  *
4
4
  * - `IndexedDbProgramStore` keeps programs in IndexedDB.
5
5
  * - `IndexedDbQueueStore` keeps the queue of a `HybridStorage` in IndexedDB.
6
- * - `MemoryProgramStore` and `MemoryQueueStore` are the fallbacks for a browser without
7
- * IndexedDB. `hasIndexedDb()` tells which one to use.
6
+ * - `IndexedDbSyncStateStore` keeps the sync state of a `HybridStorage` in IndexedDB: the cursor
7
+ * and the base versions (roadmap CLI-21).
8
+ * - `MemoryProgramStore`, `MemoryQueueStore` and `MemorySyncStateStore` are the fallbacks for a
9
+ * browser without IndexedDB. `hasIndexedDb()` tells which one to use.
8
10
  *
9
11
  * The package root does not import this entry, and this entry does not import the client or zod:
10
12
  * a page that loads it gets only these stores.
11
13
  */
12
14
  export { IndexedDbProgramStore, DEFAULT_PROGRAM_DATABASE } from "./indexeddb/program-store.js";
13
15
  export { IndexedDbQueueStore } from "./indexeddb/queue-store.js";
16
+ export { IndexedDbSyncStateStore } from "./indexeddb/sync-state-store.js";
14
17
  export { hasIndexedDb } from "./indexeddb/idb.js";
15
18
  export { MemoryProgramStore } from "./program-store/memory.js";
16
19
  export { MemoryQueueStore } from "./storage/queue.js";
20
+ export { MemorySyncStateStore } from "./storage/sync-state.js";
17
21
  //# sourceMappingURL=indexeddb.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"indexeddb.js","sourceRoot":"","sources":["../src/indexeddb.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,qBAAqB,EAAE,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AAC/F,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,OAAO,EAAE,YAAY,EAA8C,MAAM,oBAAoB,CAAC;AAC9F,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC"}
1
+ {"version":3,"file":"indexeddb.js","sourceRoot":"","sources":["../src/indexeddb.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,qBAAqB,EAAE,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AAC/F,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAC1E,OAAO,EAAE,YAAY,EAA8C,MAAM,oBAAoB,CAAC;AAC9F,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC"}
@@ -6,9 +6,10 @@
6
6
  *
7
7
  * This is the KEY integration between Collections and Universal Client!
8
8
  */
9
- import type { CollectionStorage, StorageChanges, StorageMetadata } from "@procedures/collections";
9
+ import type { CollectionStorage, StorageChanges, StorageMetadata, StorageWriteOptions } from "@procedures/collections";
10
+ import { type StoragePatch, type StorageQueryOptions, type StorageQueryPolicy, type StorageSelector } from "@procedures/client/query";
10
11
  import type { Client } from "@procedures/client";
11
- import { type Duration } from "@procedures/client";
12
+ import { type Clock, type Duration } from "@procedures/client";
12
13
  /**
13
14
  * API storage configuration options.
14
15
  */
@@ -31,7 +32,7 @@ export interface ApiStorageOptions {
31
32
  */
32
33
  version?: string;
33
34
  /**
34
- * Custom operation names (override defaults)
35
+ * Custom operation names (override defaults). `find` is the operation of `query()`.
35
36
  */
36
37
  operations?: {
37
38
  get?: string;
@@ -40,6 +41,7 @@ export interface ApiStorageOptions {
40
41
  ids?: string;
41
42
  changes?: string;
42
43
  find?: string;
44
+ patch?: string;
43
45
  has?: string;
44
46
  size?: string;
45
47
  set?: string;
@@ -56,15 +58,50 @@ export interface ApiStorageOptions {
56
58
  timeout?: Duration;
57
59
  /**
58
60
  * Retry a call that fails with a retryable error: true (3 attempts), false (1 attempt), or the
59
- * number of attempts.
61
+ * number of attempts. Each collection procedure is idempotent, so a retry is safe.
60
62
  * @default true
61
63
  */
62
64
  retry?: boolean | number;
63
65
  /**
64
- * The first delay between attempts. Each later delay is two times longer.
66
+ * The backoff of the first retry. Each later backoff is `retryFactor` times longer, at most
67
+ * `maxRetryDelay`. The wait is `backoffDelay` of `@mark1russell7/service/backoff`.
65
68
  * @default 100 milliseconds
66
69
  */
67
70
  retryDelay?: Duration;
71
+ /**
72
+ * The factor of the backoff from one retry to the next.
73
+ * @default 2
74
+ */
75
+ retryFactor?: number;
76
+ /**
77
+ * The longest backoff of a retry, before its random part.
78
+ * @default 30 seconds
79
+ */
80
+ maxRetryDelay?: Duration;
81
+ /**
82
+ * The random part of each wait, from 0 to 1. With 1 (full jitter), the wait is a random time from
83
+ * 0 to the backoff, so the clients that failed together do not retry together. 0 gives the
84
+ * backoff exactly. A server that asks for a wait (`retryAfterMs` of the error) gets at least it.
85
+ * @default 1
86
+ */
87
+ retryJitter?: number;
88
+ /**
89
+ * The clock of the timeout and of the waits between attempts. A test gives `fakeClock` of
90
+ * `@mark1russell7/service/testing`.
91
+ * @default the clock of the platform (`realClock` of `@procedures/client`)
92
+ */
93
+ clock?: Clock;
94
+ /**
95
+ * The random numbers of the jitter, from 0 up to 1. A test gives a fixed generator.
96
+ * @default Math.random
97
+ */
98
+ random?: () => number;
99
+ /**
100
+ * The query policy that `query()` applies before it sends a query, for a call without a policy.
101
+ * The server applies its own policy too. Without it, the storage examines only the form of a
102
+ * selector, and the server decides the rest.
103
+ */
104
+ queryPolicy?: StorageQueryPolicy;
68
105
  /**
69
106
  * AbortSignal for cancelling requests
70
107
  */
@@ -84,11 +121,16 @@ export interface ApiStorageOptions {
84
121
  * - set(id, value) → `collections.<collection>.set` with `{ id, value }`
85
122
  * - setBatch(items) → `collections.<collection>.setBatch` with `{ items: [{ id, value }] }`
86
123
  * - getIds() → `collections.<collection>.ids` with `{}`, or the entries of an older server
124
+ * - query(selector, options) → `collections.<collection>.find` with `{ selector, sort, skip, limit }`
125
+ * - patch(id, fields) → `collections.<collection>.patch` with `{ id, fields }`
87
126
  * - etc.
88
127
  *
89
128
  * Before, the class expected a `{ data }` envelope and called `<service>.get`: no core
90
129
  * procedure matched, and every read was undefined (deep dive CORE-9, DATA-9).
91
130
  *
131
+ * The retries wait with `backoffDelay` of `@mark1russell7/service/backoff` on the clock of the
132
+ * options, with full jitter by default. Before, the waits had no jitter, and they used the timers of the platform directly.
133
+ *
92
134
  * @example
93
135
  * ```typescript
94
136
  * // Create API storage
@@ -101,6 +143,7 @@ export interface ApiStorageOptions {
101
143
  * // Operations are automatically sent to server
102
144
  * await storage.set("123", { name: "John" });
103
145
  * const user = await storage.get("123"); // Fetched from server
146
+ * const admins = await storage.query({ role: "admin" }, { sort: { name: 1 }, limit: 50 }); // Runs on the server
104
147
  * ```
105
148
  */
106
149
  export declare class ApiStorage<T> implements CollectionStorage<T> {
@@ -109,6 +152,8 @@ export declare class ApiStorage<T> implements CollectionStorage<T> {
109
152
  private closed;
110
153
  /** True when the server does not know the procedure `ids`: `getIds()` reads the entries */
111
154
  private idsUnsupported;
155
+ /** True when the server does not know the procedure `find`: `query()` reads the entries */
156
+ private findUnsupported;
112
157
  constructor(client: Client, options: ApiStorageOptions);
113
158
  /**
114
159
  * The item of an id. For the core collection procedures, the call asks for `{ found, value }`,
@@ -131,14 +176,38 @@ export declare class ApiStorage<T> implements CollectionStorage<T> {
131
176
  * `getEntries()`.
132
177
  */
133
178
  getChangesSince(since?: string): Promise<StorageChanges<T>>;
179
+ /**
180
+ * The items for which the predicate gives true. A function cannot go over the network, so this
181
+ * method reads all items and filters here. `query()` sends a JSON selector to the server.
182
+ */
134
183
  find(predicate: (item: T) => boolean): Promise<T[]>;
184
+ /**
185
+ * The entries whose items match the selector, from the `find` procedure of the collection: the
186
+ * server runs the selector, the order, the skip and the limit (roadmap CLI-14), under its own
187
+ * query policy. An older server without the procedure answers `NOT_FOUND`. Then this call and
188
+ * each later call read the entries and run the query here, with the same result.
189
+ *
190
+ * @param policy - The policy that the storage applies before it sends the query. The default is
191
+ * the option `queryPolicy`. The server applies its own policy too.
192
+ */
193
+ query(selector: StorageSelector, options?: StorageQueryOptions, policy?: StorageQueryPolicy): Promise<Array<[string, T]>>;
135
194
  has(id: string): Promise<boolean>;
136
195
  size(): Promise<number>;
137
- set(id: string, value: T): Promise<void>;
138
- delete(id: string): Promise<boolean>;
139
- clear(): Promise<void>;
140
- setBatch(items: Array<[string, T]>): Promise<void>;
141
- deleteBatch(ids: string[]): Promise<number>;
196
+ /**
197
+ * This method sets an item on the server. The origin of the options goes to the procedure `set`,
198
+ * so the storage of the server and the change event of the write have it (roadmap CLI-15).
199
+ */
200
+ set(id: string, value: T, options?: StorageWriteOptions): Promise<void>;
201
+ delete(id: string, options?: StorageWriteOptions): Promise<boolean>;
202
+ clear(options?: StorageWriteOptions): Promise<void>;
203
+ /**
204
+ * This method changes an item on the server in one step: the `patch` procedure of the collection
205
+ * (roadmap CLI-14). The fields are a JSON Merge Patch, or the operations `$set` and `$unset`.
206
+ * It gives the new item, or undefined for a missing id.
207
+ */
208
+ patch(id: string, fields: StoragePatch, options?: StorageWriteOptions): Promise<T | undefined>;
209
+ setBatch(items: Array<[string, T]>, options?: StorageWriteOptions): Promise<void>;
210
+ deleteBatch(ids: string[], options?: StorageWriteOptions): Promise<number>;
142
211
  getBatch(ids: string[]): Promise<Map<string, T>>;
143
212
  /**
144
213
  * This method closes the storage. After it, each operation rejects, and a second `close()` does
@@ -147,8 +216,12 @@ export declare class ApiStorage<T> implements CollectionStorage<T> {
147
216
  */
148
217
  close(): Promise<void>;
149
218
  getMetadata(): Promise<StorageMetadata>;
219
+ /** The field `origin` of a write input, or no field: a custom service gets no origin. */
220
+ private originOf;
150
221
  /**
151
- * Make an RPC call via the universal client, with the timeout, the signal and the retries.
222
+ * Make an RPC call via the universal client, with the timeout, the signal and the retries. The
223
+ * wait before retry `n` is `backoffDelay(backoff, n)`, or the wait that the error asks for when
224
+ * it is longer.
152
225
  */
153
226
  private call;
154
227
  /** One attempt: the call ends at the timeout, or when the signal of the options aborts. */
@@ -1 +1 @@
1
- {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/storage/api.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAClG,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAGjD,OAAO,EAAc,KAAK,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAE/D;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC;;;;OAIG;IACH,UAAU,CAAC,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