@hyper-fetch/db 0.0.0-stage → 10.2.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 (141) hide show
  1. package/README.md +180 -2
  2. package/__tests__/e2e/db.e2e.spec.ts +269 -0
  3. package/__tests__/features/connection/connection.spec.ts +23 -0
  4. package/__tests__/features/model/live-query.model.spec.ts +320 -0
  5. package/__tests__/features/operators/operators.spec.ts +113 -0
  6. package/__tests__/features/runtime/runtime.spec.ts +185 -0
  7. package/__tests__/features/serializer/serializer.spec.ts +73 -0
  8. package/__tests__/features/store/store.spec.ts +147 -0
  9. package/__tests__/features/types/db.types.spec.ts +337 -0
  10. package/__tests__/integration/audit.spec.ts +639 -0
  11. package/__tests__/integration/competitors.spec.ts +393 -0
  12. package/__tests__/integration/convergence.model.spec.ts +162 -0
  13. package/__tests__/integration/edge-cases.spec.ts +465 -0
  14. package/__tests__/integration/errors.spec.ts +178 -0
  15. package/__tests__/integration/infinite.model.spec.ts +142 -0
  16. package/__tests__/integration/infinite.spec.ts +290 -0
  17. package/__tests__/integration/inspect.spec.ts +103 -0
  18. package/__tests__/integration/joins-many.spec.ts +177 -0
  19. package/__tests__/integration/joins.spec.ts +179 -0
  20. package/__tests__/integration/key-loaders.spec.ts +196 -0
  21. package/__tests__/integration/links.spec.ts +105 -0
  22. package/__tests__/integration/mutations.spec.ts +337 -0
  23. package/__tests__/integration/normalize.spec.ts +80 -0
  24. package/__tests__/integration/offline.spec.ts +479 -0
  25. package/__tests__/integration/queries.spec.ts +513 -0
  26. package/__tests__/integration/races.spec.ts +127 -0
  27. package/__tests__/integration/server-operators.spec.ts +123 -0
  28. package/__tests__/integration/ssr.spec.ts +111 -0
  29. package/__tests__/integration/syncs.spec.ts +564 -0
  30. package/__tests__/integration/transactions.spec.ts +344 -0
  31. package/__tests__/integration/windows.spec.ts +192 -0
  32. package/__tests__/tsconfig.json +11 -0
  33. package/__tests__/type-budget/budget.fixture.ts +2476 -0
  34. package/__tests__/type-budget/tsconfig.json +4 -0
  35. package/__tests__/utils/fixtures.ts +225 -0
  36. package/__tests__/utils/random.ts +19 -0
  37. package/__tests__/vitest.setup.ts +11 -0
  38. package/dist/collection/collection.d.ts +111 -0
  39. package/dist/collection/collection.d.ts.map +1 -0
  40. package/dist/collection/collection.types.d.ts +316 -0
  41. package/dist/collection/collection.types.d.ts.map +1 -0
  42. package/dist/collection/resource.types.d.ts +77 -0
  43. package/dist/collection/resource.types.d.ts.map +1 -0
  44. package/dist/db/collection.runtime.d.ts +98 -0
  45. package/dist/db/collection.runtime.d.ts.map +1 -0
  46. package/dist/db/db.d.ts +154 -0
  47. package/dist/db/db.d.ts.map +1 -0
  48. package/dist/db/db.runtime.d.ts +114 -0
  49. package/dist/db/db.runtime.d.ts.map +1 -0
  50. package/dist/db/db.types.d.ts +162 -0
  51. package/dist/db/db.types.d.ts.map +1 -0
  52. package/dist/graphql/connection.d.ts +25 -0
  53. package/dist/graphql/connection.d.ts.map +1 -0
  54. package/dist/index.d.ts +17 -0
  55. package/dist/index.d.ts.map +1 -0
  56. package/dist/index.mjs +4220 -0
  57. package/dist/index.mjs.map +1 -0
  58. package/dist/mutation/mutation.d.ts +136 -0
  59. package/dist/mutation/mutation.d.ts.map +1 -0
  60. package/dist/mutation/mutation.errors.d.ts +6 -0
  61. package/dist/mutation/mutation.errors.d.ts.map +1 -0
  62. package/dist/mutation/mutation.utils.d.ts +35 -0
  63. package/dist/mutation/mutation.utils.d.ts.map +1 -0
  64. package/dist/operators/operators.d.ts +33 -0
  65. package/dist/operators/operators.d.ts.map +1 -0
  66. package/dist/operators/operators.types.d.ts +68 -0
  67. package/dist/operators/operators.types.d.ts.map +1 -0
  68. package/dist/operators/operators.utils.d.ts +19 -0
  69. package/dist/operators/operators.utils.d.ts.map +1 -0
  70. package/dist/persistence/persistence.d.ts +42 -0
  71. package/dist/persistence/persistence.d.ts.map +1 -0
  72. package/dist/persistence/serializer.d.ts +23 -0
  73. package/dist/persistence/serializer.d.ts.map +1 -0
  74. package/dist/persistence/snapshot.d.ts +29 -0
  75. package/dist/persistence/snapshot.d.ts.map +1 -0
  76. package/dist/persistence/tabs.d.ts +53 -0
  77. package/dist/persistence/tabs.d.ts.map +1 -0
  78. package/dist/query/infinite-query.d.ts +59 -0
  79. package/dist/query/infinite-query.d.ts.map +1 -0
  80. package/dist/query/live-query.d.ts +85 -0
  81. package/dist/query/live-query.d.ts.map +1 -0
  82. package/dist/query/planner.d.ts +57 -0
  83. package/dist/query/planner.d.ts.map +1 -0
  84. package/dist/query/query.d.ts +155 -0
  85. package/dist/query/query.d.ts.map +1 -0
  86. package/dist/query/query.types.d.ts +71 -0
  87. package/dist/query/query.types.d.ts.map +1 -0
  88. package/dist/schema/schema.types.d.ts +33 -0
  89. package/dist/schema/schema.types.d.ts.map +1 -0
  90. package/dist/store/store.d.ts +76 -0
  91. package/dist/store/store.d.ts.map +1 -0
  92. package/dist/store/store.types.d.ts +27 -0
  93. package/dist/store/store.types.d.ts.map +1 -0
  94. package/dist/subset/base-subset.d.ts +66 -0
  95. package/dist/subset/base-subset.d.ts.map +1 -0
  96. package/dist/subset/key-subset.d.ts +23 -0
  97. package/dist/subset/key-subset.d.ts.map +1 -0
  98. package/dist/subset/list-subset.d.ts +63 -0
  99. package/dist/subset/list-subset.d.ts.map +1 -0
  100. package/dist/sync/sync.d.ts +42 -0
  101. package/dist/sync/sync.d.ts.map +1 -0
  102. package/dist/utils/request.utils.d.ts +11 -0
  103. package/dist/utils/request.utils.d.ts.map +1 -0
  104. package/package.json +71 -4
  105. package/project.json +4 -0
  106. package/scripts/docs-typescript.mjs +24 -0
  107. package/scripts/type-budget.mjs +37 -0
  108. package/src/collection/collection.ts +330 -0
  109. package/src/collection/collection.types.ts +383 -0
  110. package/src/collection/resource.types.ts +114 -0
  111. package/src/db/collection.runtime.ts +572 -0
  112. package/src/db/db.runtime.ts +537 -0
  113. package/src/db/db.ts +451 -0
  114. package/src/db/db.types.ts +157 -0
  115. package/src/graphql/connection.ts +23 -0
  116. package/src/index.ts +16 -0
  117. package/src/mutation/mutation.errors.ts +7 -0
  118. package/src/mutation/mutation.ts +776 -0
  119. package/src/mutation/mutation.utils.ts +58 -0
  120. package/src/operators/operators.ts +90 -0
  121. package/src/operators/operators.types.ts +67 -0
  122. package/src/operators/operators.utils.ts +169 -0
  123. package/src/persistence/persistence.ts +168 -0
  124. package/src/persistence/serializer.ts +123 -0
  125. package/src/persistence/snapshot.ts +76 -0
  126. package/src/persistence/tabs.ts +86 -0
  127. package/src/query/infinite-query.ts +206 -0
  128. package/src/query/live-query.ts +552 -0
  129. package/src/query/planner.ts +199 -0
  130. package/src/query/query.ts +319 -0
  131. package/src/query/query.types.ts +90 -0
  132. package/src/schema/schema.types.ts +29 -0
  133. package/src/store/store.ts +300 -0
  134. package/src/store/store.types.ts +17 -0
  135. package/src/subset/base-subset.ts +169 -0
  136. package/src/subset/key-subset.ts +44 -0
  137. package/src/subset/list-subset.ts +328 -0
  138. package/src/sync/sync.ts +99 -0
  139. package/src/utils/request.utils.ts +67 -0
  140. package/tsconfig.json +12 -0
  141. package/vite.config.ts +71 -0
@@ -0,0 +1,136 @@
1
+ import { RowKey } from '../collection/collection.types';
2
+ import { CollectionRuntime } from '../db/collection.runtime';
3
+ import { DbRuntime } from '../db/db.runtime';
4
+ import { DbOfflineOptions, MutationOperation, TransactionConfig, TransactionRecord } from '../db/db.types';
5
+ import { Row } from '../store/store.types';
6
+ import { PreparedWrite, Transaction } from './mutation.utils';
7
+ export type { PreparedWrite, Transaction } from './mutation.utils';
8
+ export { createTransaction, isTemporaryFailure } from './mutation.utils';
9
+ /**
10
+ * Sends writes. Each write shows at once as an optimistic layer. Writes to one row go out one at a time, and a failure
11
+ * drops the writes queued after it on that row. With `offline`, every write goes through a persisted outbox that is
12
+ * replayed in order across the whole db by one tab.
13
+ */
14
+ export declare class MutationEngine {
15
+ private readonly db;
16
+ private readonly transactions;
17
+ /** The outbox, in replay order */
18
+ outbox: TransactionRecord[];
19
+ /** The last write of each row, which the next write to the row waits for while it is pending */
20
+ private chains;
21
+ private inFlight;
22
+ private settleWaiters;
23
+ private resolvers;
24
+ /** Writes counted as in flight, so a write is counted once however its sending ends */
25
+ private begun;
26
+ /** Whether each settled write succeeded, so `dependsOn` can name a write that already settled */
27
+ private outcomes;
28
+ private outcomeWaiters;
29
+ /** When each write's request was sent, so a sync event that arrived while it was in flight confirms it */
30
+ private sentAt;
31
+ private pumping;
32
+ private retryTimer;
33
+ private cancelWait;
34
+ private readonly offline;
35
+ constructor(db: DbRuntime, offline: boolean | DbOfflineOptions | undefined, transactions?: Record<string, TransactionConfig>);
36
+ /**
37
+ * Applies several writes as one transaction. A named transaction sends them through its `mutationFn` as one request.
38
+ * Without a name, each write goes through its collection's `mutate`, in order, and a failure drops the writes after
39
+ * it and refetches the subsets holding the rows already written.
40
+ */
41
+ submitTransaction: ({ id, name, writes, }: {
42
+ id: string;
43
+ name: string | undefined;
44
+ writes: PreparedWrite[];
45
+ }) => Transaction;
46
+ private createRecord;
47
+ /** The result promise of a record, resolved once the record settles */
48
+ private track;
49
+ private submitSequence;
50
+ /** Sends the writes of an unnamed transaction one after another */
51
+ private runSequence;
52
+ private submitNamed;
53
+ private addTransactionLayers;
54
+ /** Runs a callback for each write of a named transaction, as a plain record with its layer id */
55
+ private eachWrite;
56
+ /** Applies a write optimistically and sends it */
57
+ submit: ({ collection, op, key, values, dependsOn, id, temporaryKey, }: {
58
+ collection: CollectionRuntime;
59
+ op: MutationOperation;
60
+ key: RowKey;
61
+ values: Row;
62
+ dependsOn: string[];
63
+ id: string;
64
+ temporaryKey?: boolean;
65
+ }) => Transaction;
66
+ /** A write rejected before it was sent, for example by validation */
67
+ reject: (error: unknown, id: string) => Transaction;
68
+ /** Whether writes go through the outbox */
69
+ get usesOutbox(): boolean;
70
+ /** Resolves once no write of the collection is in flight */
71
+ settled: (collection: string) => Promise<void>;
72
+ /** Rebuilds the optimistic layers of a restored outbox and replays it */
73
+ restore: (records: TransactionRecord[]) => void;
74
+ /**
75
+ * Writes another tab made: a follower sent them to this tab, the leader, or this tab became the leader and adopts the
76
+ * outbox the previous leader persisted. They are persisted together with this tab's own writes and replayed.
77
+ */
78
+ mergeOutbox: (records: TransactionRecord[]) => void;
79
+ /** A new leader asks for the writes followers still wait for */
80
+ resendToLeader: () => void;
81
+ /** Adds records this tab does not hold yet, with their optimistic layers, in the order they were made */
82
+ private addRecords;
83
+ /** The records this db can replay. The others are reported, never dropped silently. */
84
+ private replayable;
85
+ /** The replaying tab reports a write's outcome */
86
+ applyOutcome: (outcome: {
87
+ id: string;
88
+ success: boolean;
89
+ error?: unknown;
90
+ key?: RowKey;
91
+ }) => void;
92
+ /** Replays the outbox when this tab is the one that replays it, one write at a time and in order */
93
+ pump: () => Promise<void>;
94
+ /**
95
+ * Drops every pending write, for example on sign-out. Writes in flight resolve as failed now, and their responses,
96
+ * which carry the previous user's data, are never written.
97
+ */
98
+ clear: () => void;
99
+ /** Stops the retry timer, for `destroy` */
100
+ destroy: () => void;
101
+ /** The generation each write was made or restored in, so `clear` ends the writes of the previous user */
102
+ private generations;
103
+ /** Whether a write belongs to the current user, rather than to one `clear` ended */
104
+ private isActive;
105
+ /** Resolves with whether a write succeeded. A write this db never saw, such as one from another session, is done. */
106
+ private waitForOutcome;
107
+ private enqueue;
108
+ private persistOutbox;
109
+ private removeFromOutbox;
110
+ private runDirect;
111
+ /** The client a record's request goes through, so replay waits while it is offline */
112
+ private getClient;
113
+ private send;
114
+ private sendNamed;
115
+ /** A named transaction succeeded: its changes become the server state, or the subsets holding them reload */
116
+ private succeedNamed;
117
+ private removeTransactionLayers;
118
+ private succeed;
119
+ /** A write confirmed by another tab: its values become the server state */
120
+ private keep;
121
+ private writeKept;
122
+ private writeResponse;
123
+ /** Reloads the subsets that hold the row, after the loads already in flight */
124
+ private refetch;
125
+ /** Drops a rejected outbox write and every later write that was built on it */
126
+ private drop;
127
+ private fail;
128
+ private resolve;
129
+ /** The collections a write changes: one, or every collection a named transaction writes */
130
+ private collectionsOf;
131
+ /** Counts a sent write as in flight in every collection it changes, once per attempt */
132
+ private begin;
133
+ /** Ends a write's in-flight state once, even when its success handling ended it early */
134
+ private end;
135
+ }
136
+ //# sourceMappingURL=mutation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mutation.d.ts","sourceRoot":"","sources":["../../src/mutation/mutation.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gCAAgC,CAAC;AAC7D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,KAAK,EACV,gBAAgB,EAChB,iBAAiB,EAEjB,iBAAiB,EACjB,iBAAiB,EAElB,MAAM,gBAAgB,CAAC;AACxB,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,sBAAsB,CAAC;AAGhD,OAAO,KAAK,EAAe,aAAa,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAUhF,YAAY,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACnE,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAEzE;;;;GAIG;AACH,qBAAa,cAAc;IAqBvB,OAAO,CAAC,QAAQ,CAAC,EAAE;IAEnB,OAAO,CAAC,QAAQ,CAAC,YAAY;IAtB/B,kCAAkC;IAClC,MAAM,EAAE,iBAAiB,EAAE,CAAM;IACjC,gGAAgG;IAChG,OAAO,CAAC,MAAM,CAA4D;IAC1E,OAAO,CAAC,QAAQ,CAA6B;IAC7C,OAAO,CAAC,aAAa,CAAqC;IAC1D,OAAO,CAAC,SAAS,CAAuD;IACxE,uFAAuF;IACvF,OAAO,CAAC,KAAK,CAAqB;IAClC,iGAAiG;IACjG,OAAO,CAAC,QAAQ,CAA8B;IAC9C,OAAO,CAAC,cA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CAAC,IAAI,CAmBV;IAEF,OAAO,CAAC,IAAI,CAaV;IAEF,OAAO,CAAC,OAAO,CAOb;IAEF,2FAA2F;IAC3F,OAAO,CAAC,aAAa,CAEnB;IAEF,wFAAwF;IACxF,OAAO,CAAC,KAAK,CAKX;IAEF,yFAAyF;IACzF,OAAO,CAAC,GAAG,CAcT;CACH"}
@@ -0,0 +1,6 @@
1
+ /** Why a write was dropped: an earlier write it was built on failed */
2
+ export declare class DependencyError extends Error {
3
+ readonly transactionId: string;
4
+ constructor(transactionId: string);
5
+ }
6
+ //# sourceMappingURL=mutation.errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mutation.errors.d.ts","sourceRoot":"","sources":["../../src/mutation/mutation.errors.ts"],"names":[],"mappings":"AAAA,uEAAuE;AACvE,qBAAa,eAAgB,SAAQ,KAAK;IAC5B,QAAQ,CAAC,aAAa,EAAE,MAAM;gBAArB,aAAa,EAAE,MAAM;CAI3C"}
@@ -0,0 +1,35 @@
1
+ import { ClientInstance, ResponseType } from '@hyper-fetch/core';
2
+ import { RowKey } from '../collection/collection.types';
3
+ import { CollectionRuntime } from '../db/collection.runtime';
4
+ import { MutationOperation, MutationResult, TransactionRecord } from '../db/db.types';
5
+ import { Row } from '../store/store.types';
6
+ export type AnyResponse = ResponseType<unknown, unknown, any>;
7
+ /**
8
+ * A pending write: a promise of the write's result that never rejects, with the write's `id`. The `id`, or the
9
+ * transaction itself, can be passed to `dependsOn` of a later write, so the later write is dropped when this one is
10
+ * rejected.
11
+ */
12
+ export type Transaction = Promise<MutationResult> & {
13
+ readonly id: string;
14
+ };
15
+ export declare const createTransaction: (id: string, result: Promise<MutationResult>) => Transaction;
16
+ /** By default a 4xx response other than 408 and 429 is permanent, and everything else is retried */
17
+ export declare const isTemporaryFailure: (response: AnyResponse) => boolean;
18
+ /** A write of a transaction, validated and ready to apply */
19
+ export type PreparedWrite = {
20
+ collection: CollectionRuntime;
21
+ op: MutationOperation;
22
+ key: RowKey;
23
+ values: Row;
24
+ };
25
+ /** The rows a record writes, as `collection:key` */
26
+ export declare const rowsOf: (record: TransactionRecord) => string[];
27
+ /** One result for several writes: the first failure, or every write's data */
28
+ export declare const combineResults: (results: MutationResult[]) => MutationResult;
29
+ export declare const defaultRetryDelay: (attempt: number) => number;
30
+ /** Resolves once the client is online again. `cancel` stops listening, for `destroy`. */
31
+ export declare const waitForOnline: (client: ClientInstance) => {
32
+ promise: Promise<void>;
33
+ cancel: VoidFunction;
34
+ };
35
+ //# sourceMappingURL=mutation.utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mutation.utils.d.ts","sourceRoot":"","sources":["../../src/mutation/mutation.utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEtE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gCAAgC,CAAC;AAC7D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAC3F,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,sBAAsB,CAAC;AAEhD,MAAM,MAAM,WAAW,GAAG,YAAY,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,CAAC,CAAC;AAE9D;;;;GAIG;AACH,MAAM,MAAM,WAAW,GAAG,OAAO,CAAC,cAAc,CAAC,GAAG;IAAE,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAA;CAAE,CAAC;AAE5E,eAAO,MAAM,iBAAiB,GAAI,IAAI,MAAM,EAAE,QAAQ,OAAO,CAAC,cAAc,CAAC,KAAG,WAE/E,CAAC;AAEF,oGAAoG;AACpG,eAAO,MAAM,kBAAkB,GAAI,UAAU,WAAW,YAMvD,CAAC;AAEF,6DAA6D;AAC7D,MAAM,MAAM,aAAa,GAAG;IAAE,UAAU,EAAE,iBAAiB,CAAC;IAAC,EAAE,EAAE,iBAAiB,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,GAAG,CAAA;CAAE,CAAC;AAE/G,oDAAoD;AACpD,eAAO,MAAM,MAAM,GAAI,QAAQ,iBAAiB,aAG/C,CAAC;AAEF,8EAA8E;AAC9E,eAAO,MAAM,cAAc,GAAI,SAAS,cAAc,EAAE,KAAG,cAG1D,CAAC;AAEF,eAAO,MAAM,iBAAiB,GAAI,SAAS,MAAM,WAAgD,CAAC;AAElG,yFAAyF;AACzF,eAAO,MAAM,aAAa,GAAI,QAAQ,cAAc;;;CAUnD,CAAC"}
@@ -0,0 +1,33 @@
1
+ import { ComparableValue, Condition, ExtractConditionEq, ExtractConditionTokens, FieldRef, LocalToken, NoInferValue } from './operators.types';
2
+ /** Matches rows whose field equals the value. Dates are equal when they are the same instant. */
3
+ export declare const eq: <Name extends string, Value>(ref: FieldRef<Name, Value>, value: NoInferValue<Value>) => Condition<`${Name}:eq`, Name>;
4
+ /** Matches rows whose field is greater than the value */
5
+ export declare const gt: <Name extends string, Value extends ComparableValue | null | undefined>(ref: FieldRef<Name, Value>, value: NoInferValue<NonNullable<Value>>) => Condition<`${Name}:gt`, never>;
6
+ /** Matches rows whose field is greater than or equal to the value */
7
+ export declare const gte: <Name extends string, Value extends ComparableValue | null | undefined>(ref: FieldRef<Name, Value>, value: NoInferValue<NonNullable<Value>>) => Condition<`${Name}:gte`, never>;
8
+ /** Matches rows whose field is less than the value */
9
+ export declare const lt: <Name extends string, Value extends ComparableValue | null | undefined>(ref: FieldRef<Name, Value>, value: NoInferValue<NonNullable<Value>>) => Condition<`${Name}:lt`, never>;
10
+ /** Matches rows whose field is less than or equal to the value */
11
+ export declare const lte: <Name extends string, Value extends ComparableValue | null | undefined>(ref: FieldRef<Name, Value>, value: NoInferValue<NonNullable<Value>>) => Condition<`${Name}:lte`, never>;
12
+ /** Matches rows whose field equals one of the values */
13
+ export declare const inArray: <Name extends string, Value>(ref: FieldRef<Name, Value>, values: readonly NoInferValue<Value>[]) => Condition<`${Name}:inArray`, never>;
14
+ /**
15
+ * A server-only operator, such as full-text search, which has no meaning on the client. A loader maps it under its
16
+ * name like any operator, as in `title: { search: (text: string) => ({ queryParams: { q: text } }) }`. A query that
17
+ * uses one must send it, or it does not compile. Its subset never answers other queries, and new rows never join it:
18
+ * an insert marks it stale instead.
19
+ *
20
+ * @example
21
+ * db.post.where((post) => op("search", post.title, "tea"));
22
+ */
23
+ export declare const op: <Operator extends string, Name extends string, Value>(name: Operator, ref: FieldRef<Name, unknown>, value: Value) => Condition<`${Name}:${Operator}`, never>;
24
+ /**
25
+ * Matches rows that match every condition. Top-level `and` is where the db splits a query: the conditions a loader
26
+ * declares are sent to the backend and the rest run on the rows the db holds.
27
+ */
28
+ export declare const and: <Conditions extends readonly Condition<string, string>[]>(...conditions: Conditions) => Condition<ExtractConditionTokens<Conditions[number]>, ExtractConditionEq<Conditions[number]>>;
29
+ /** Matches rows that match at least one condition. It runs on the rows the db holds. */
30
+ export declare const or: (...conditions: readonly Condition<string, string>[]) => Condition<LocalToken, never>;
31
+ /** Matches rows that do not match the condition. It runs on the rows the db holds. */
32
+ export declare const not: (condition: Condition<string, string>) => Condition<LocalToken, never>;
33
+ //# sourceMappingURL=operators.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"operators.d.ts","sourceRoot":"","sources":["../../src/operators/operators.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EACf,SAAS,EACT,kBAAkB,EAClB,sBAAsB,EACtB,QAAQ,EACR,UAAU,EACV,YAAY,EACb,MAAM,mBAAmB,CAAC;AAM3B,iGAAiG;AACjG,eAAO,MAAM,EAAE,GAAI,IAAI,SAAS,MAAM,EAAE,KAAK,EAC3C,KAAK,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC,EAC1B,OAAO,YAAY,CAAC,KAAK,CAAC,KACzB,SAAS,CAAC,GAAG,IAAI,KAAK,EAA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IAAI,EAAE,KAAK,CAAC,EAC1B,QAAQ,SAAS,YAAY,CAAC,KAAK,CAAC,EAAE,KACrC,SAAS,CAAC,GAAG,IAAI,UAAU,EAAE,KAAK,CAAuC,CAAC;AAE7E;;;;;;;;GAQG;AACH,eAAO,MAAM,EAAE,GAAI,QAAQ,SAAS,MAAM,EAAE,IAAI,SAAS,MAAM,EAAE,KAAK,EACpE,MAAM,QAAQ,EACd,KAAK,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,EAC5B,OAAO,KAAK,KACX,SAAS,CAAC,GAAG,IAAI,IAAI,QAAQ,EAAE,EAAE,KAAK,CAGvC,CAAC;AAEH;;;GAGG;AACH,eAAO,MAAM,GAAG,GAAI,UAAU,SAAS,SAAS,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,EACzE,GAAG,YAAY,UAAU,KACxB,SAAS,CAAC,sBAAsB,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,EAAE,kBAAkB,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAG7F,CAAC;AAEH,wFAAwF;AACxF,eAAO,MAAM,EAAE,GAAI,GAAG,YAAY,SAAS,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,KAAG,SAAS,CAAC,UAAU,EAAE,KAAK,CAGlG,CAAC;AAEH,sFAAsF;AACtF,eAAO,MAAM,GAAG,GAAI,WAAW,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,KAAG,SAAS,CAAC,UAAU,EAAE,KAAK,CAGpF,CAAC"}
@@ -0,0 +1,68 @@
1
+ /** A comparison operator that loaders can map to their backend's notation */
2
+ export type ComparisonOperator = "eq" | "gt" | "gte" | "lt" | "lte" | "inArray";
3
+ /** Values that `gt`, `gte`, `lt`, `lte` and `orderBy` compare */
4
+ export type ComparableValue = string | number | bigint | Date;
5
+ /** A node of a filter expression. Operators build it, loaders translate it and the db evaluates it on rows. */
6
+ export type ExpressionNode = {
7
+ readonly op: ComparisonOperator;
8
+ readonly field: string;
9
+ readonly value: unknown;
10
+ } | {
11
+ readonly op: "and";
12
+ readonly items: readonly ExpressionNode[];
13
+ } | {
14
+ readonly op: "or";
15
+ readonly items: readonly ExpressionNode[];
16
+ } | {
17
+ readonly op: "not";
18
+ readonly item: ExpressionNode;
19
+ } | ServerNode;
20
+ /** A comparison node, which the db can evaluate on the rows it holds */
21
+ export type ComparisonNode = Extract<ExpressionNode, {
22
+ op: ComparisonOperator;
23
+ }>;
24
+ /** A server-only operator, such as full-text search: the loader must send it, and the client never evaluates it */
25
+ export type ServerNode = {
26
+ readonly op: "server";
27
+ readonly name: string;
28
+ readonly field: string;
29
+ readonly value: unknown;
30
+ };
31
+ /** A condition a loader can send to its backend */
32
+ export type SendableNode = ComparisonNode | ServerNode;
33
+ /**
34
+ * A reference to one row field, received by query callbacks such as `where((post) => eq(post.teamId, 1))`.
35
+ * `Value` is the field's type, so operators accept only values of that type.
36
+ */
37
+ export type FieldRef<Name extends string = string, Value = unknown> = {
38
+ readonly kind: "field";
39
+ readonly field: Name;
40
+ /** Type-only marker, never set at runtime */
41
+ readonly __value?: Value;
42
+ };
43
+ /** The field references a query callback receives, one for every row field */
44
+ export type RowRefs<Row> = {
45
+ readonly [Key in keyof Row & string]-?: FieldRef<Key, Row[Key]>;
46
+ };
47
+ /** Marks conditions that never reach a backend, such as `or` and `not` */
48
+ export type LocalToken = "~local";
49
+ /**
50
+ * A filter condition. `Tokens` lists what it asks a backend for, as `field:operator`, and `Eq` the fields it sets with
51
+ * `eq` at the top level. The types use both to check that a loader can serve the query.
52
+ */
53
+ export type Condition<Tokens extends string = string, Eq extends string = string> = {
54
+ readonly kind: "condition";
55
+ readonly node: ExpressionNode;
56
+ /** Type-only marker, never set at runtime */
57
+ readonly __tokens?: Tokens;
58
+ /** Type-only marker, never set at runtime */
59
+ readonly __eq?: Eq;
60
+ };
61
+ export type ExtractConditionTokens<C> = C extends Condition<infer Tokens, any> ? Tokens : never;
62
+ export type ExtractConditionEq<C> = C extends Condition<any, infer Eq> ? Eq : never;
63
+ /**
64
+ * Keeps a parameter out of type inference, so the field decides the type and the value is checked against it.
65
+ * TypeScript's own `NoInfer` needs 5.4, and the API docs and older consumers compile with earlier versions.
66
+ */
67
+ export type NoInferValue<T> = [T][T extends unknown ? 0 : never];
68
+ //# sourceMappingURL=operators.types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"operators.types.d.ts","sourceRoot":"","sources":["../../src/operators/operators.types.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,MAAM,MAAM,kBAAkB,GAAG,IAAI,GAAG,IAAI,GAAG,KAAK,GAAG,IAAI,GAAG,KAAK,GAAG,SAAS,CAAC;AAEhF,iEAAiE;AACjE,MAAM,MAAM,eAAe,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;AAE9D,+GAA+G;AAC/G,MAAM,MAAM,cAAc,GACtB;IAAE,QAAQ,CAAC,EAAE,EAAE,kBAAkB,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAA;CAAE,GACpF;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,SAAS,cAAc,EAAE,CAAA;CAAE,GACjE;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,SAAS,cAAc,EAAE,CAAA;CAAE,GAChE;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAA;CAAE,GACrD,UAAU,CAAC;AAEf,wEAAwE;AACxE,MAAM,MAAM,cAAc,GAAG,OAAO,CAAC,cAAc,EAAE;IAAE,EAAE,EAAE,kBAAkB,CAAA;CAAE,CAAC,CAAC;AAEjF,mHAAmH;AACnH,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,CAAC,EAAE,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AAC;AAEhG,MAAM,MAAM,kBAAkB,CAAC,CAAC,IAAI,CAAC,SAAS,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC;AAEpF;;;GAGG;AACH,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,OAAO,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC"}
@@ -0,0 +1,19 @@
1
+ import { ExpressionNode } from './operators.types';
2
+ /** Two values are equal when they are the same value, or dates of the same instant */
3
+ export declare const isEqualValue: (left: unknown, right: unknown) => boolean;
4
+ /**
5
+ * Orders two values. `null` and `undefined` sort before every other value. Strings compare by code point, dates by
6
+ * instant, and values of different types by their type name, so the order is always total.
7
+ */
8
+ export declare const compareValues: (left: unknown, right: unknown) => number;
9
+ /** Evaluates a filter expression on one row */
10
+ export declare const evaluate: (node: ExpressionNode, row: Record<string, unknown>) => boolean;
11
+ /** Splits an expression at its top-level `and`, flattening nested `and` nodes */
12
+ export declare const splitAnd: (node: ExpressionNode | undefined) => ExpressionNode[];
13
+ /** The fields an expression reads */
14
+ export declare const getExpressionFields: (node: ExpressionNode) => string[];
15
+ /** A JSON value that identifies a value structurally. Dates keep their instant. */
16
+ export declare const toStableValue: (value: unknown) => unknown;
17
+ /** A string that is equal for structurally equal values */
18
+ export declare const stableStringify: (value: unknown) => string;
19
+ //# sourceMappingURL=operators.utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"operators.utils.d.ts","sourceRoot":"","sources":["../../src/operators/operators.utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAkB,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAsBxE,sFAAsF;AACtF,eAAO,MAAM,YAAY,GAAI,MAAM,OAAO,EAAE,OAAO,OAAO,KAAG,OAE5D,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,aAAa,GAAI,MAAM,OAAO,EAAE,OAAO,OAAO,KAAG,MAyB7D,CAAC;AA4BF,+CAA+C;AAC/C,eAAO,MAAM,QAAQ,GAAI,MAAM,cAAc,EAAE,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAG,OAmB7E,CAAC;AAEF,iFAAiF;AACjF,eAAO,MAAM,QAAQ,GAAI,MAAM,cAAc,GAAG,SAAS,KAAG,cAAc,EAQzE,CAAC;AAEF,qCAAqC;AACrC,eAAO,MAAM,mBAAmB,GAAI,MAAM,cAAc,KAAG,MAAM,EAahE,CAAC;AAEF,mFAAmF;AACnF,eAAO,MAAM,aAAa,GAAI,OAAO,OAAO,KAAG,OA+B9C,CAAC;AAEF,2DAA2D;AAC3D,eAAO,MAAM,eAAe,GAAI,OAAO,OAAO,KAAG,MAA8C,CAAC"}
@@ -0,0 +1,42 @@
1
+ import { CollectionRuntime } from '../db/collection.runtime';
2
+ import { DbRuntime } from '../db/db.runtime';
3
+ import { DbPersistenceOptions, TransactionRecord } from '../db/db.types';
4
+ import { Serializer } from './serializer';
5
+ /** The outbox's own format version. It changes only when the stored shape of a transaction changes. */
6
+ export declare const OUTBOX_FORMAT = 1;
7
+ /**
8
+ * Persists server rows, loaded subsets and the outbox under a scope, such as the user id. Rows of another db version
9
+ * are cleared, because the server holds a copy. The outbox is never cleared with them.
10
+ */
11
+ export declare class Persistence {
12
+ private readonly db;
13
+ readonly networkTimeout: number;
14
+ private readonly storage;
15
+ private readonly prefix;
16
+ private readonly version;
17
+ private readonly serializer;
18
+ /** Set when the stored outbox cannot be read, so it is never written over */
19
+ private outboxLocked;
20
+ private scheduled;
21
+ private timer;
22
+ constructor(db: DbRuntime, options: DbPersistenceOptions, serializer: Serializer);
23
+ /** Restores every collection and returns the persisted outbox */
24
+ restore: () => Promise<TransactionRecord[]>;
25
+ /**
26
+ * Reads the outbox. An outbox this version cannot read, corrupt or of another format, is reported and never written
27
+ * over, because it may hold the only copy of writes another version of the app can replay.
28
+ */
29
+ loadOutbox: () => Promise<TransactionRecord[]>;
30
+ saveOutbox: (entries: TransactionRecord[]) => Promise<void>;
31
+ /** Saves the collections whose rows changed, once the current tick is over */
32
+ scheduleSave: (collections: Iterable<CollectionRuntime>) => void;
33
+ saveCollection: (collection: CollectionRuntime) => Promise<void>;
34
+ /** Removes everything this scope persisted */
35
+ clear: () => Promise<void>;
36
+ destroy: () => void;
37
+ private restoreCollection;
38
+ private read;
39
+ private write;
40
+ private remove;
41
+ }
42
+ //# sourceMappingURL=persistence.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"persistence.d.ts","sourceRoot":"","sources":["../../src/persistence/persistence.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,KAAK,EAAE,oBAAoB,EAAa,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACzF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAI/C,uGAAuG;AACvG,eAAO,MAAM,aAAa,IAAI,CAAC;AAM/B;;;GAGG;AACH,qBAAa,WAAW;IAYpB,OAAO,CAAC,QAAQ,CAAC,EAAE;IAXrB,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAY;IACpC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAkB;IAC1C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAa;IACxC,6EAA6E;IAC7E,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,SAAS,CAAgC;IACjD,OAAO,CAAC,KAAK,CAA4C;gBAGtC,EAAE,EAAE,SAAS,EAC9B,OAAO,EAAE,oBAAoB,EAC7B,UAAU,EAAE,UAAU;IASxB,iEAAiE;IACjE,OAAO,QAAa,OAAO,CAAC,iBAAiB,EAAE,CAAC,CAG9C;IAEF;;;OAGG;IACH,UAAU,QAAa,OAAO,CAAC,iBAAiB,EAAE,CAAC,CA6BjD;IAEF,UAAU,GAAI,SAAS,iBAAiB,EAAE,mBAMxC;IAEF,8EAA8E;IAC9E,YAAY,GAAI,aAAa,QAAQ,CAAC,iBAAiB,CAAC,UActD;IAEF,cAAc,GAAI,YAAY,iBAAiB,mBAG7C;IAEF,8CAA8C;IAC9C,KAAK,sBAQH;IAEF,OAAO,aAGL;IAEF,OAAO,CAAC,iBAAiB,CAYvB;IAEF,OAAO,CAAC,IAAI,CAQV;IAEF,OAAO,CAAC,KAAK,CAMX;IAEF,OAAO,CAAC,MAAM,CAMZ;CACH"}
@@ -0,0 +1,23 @@
1
+ /** A value type the db stores as tagged JSON, beyond the ones it knows */
2
+ export type DbCodec = {
3
+ /** The tag stored with encoded values. It must stay the same across releases. */
4
+ name: string;
5
+ /** Whether a value is of this type */
6
+ test: (value: unknown) => boolean;
7
+ /** Turns a value into one the serializer can store */
8
+ encode: (value: unknown) => unknown;
9
+ /** Turns a stored value back */
10
+ decode: (value: unknown) => unknown;
11
+ };
12
+ /**
13
+ * Stores values as type-tagged JSON, so `Date`, `bigint`, `Map`, `Set`, `undefined`, `NaN` and the infinities come back
14
+ * as they were. Persistence, the outbox and server rendering use it.
15
+ */
16
+ export declare const createSerializer: (codecs?: readonly DbCodec[]) => {
17
+ encode: (value: unknown) => unknown;
18
+ decode: (value: unknown) => unknown;
19
+ stringify: (value: unknown) => string;
20
+ parse: (raw: string) => unknown;
21
+ };
22
+ export type Serializer = ReturnType<typeof createSerializer>;
23
+ //# sourceMappingURL=serializer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serializer.d.ts","sourceRoot":"","sources":["../../src/persistence/serializer.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,MAAM,MAAM,OAAO,GAAG;IACpB,iFAAiF;IACjF,IAAI,EAAE,MAAM,CAAC;IACb,sCAAsC;IACtC,IAAI,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC;IAClC,sDAAsD;IACtD,MAAM,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC;IACpC,gCAAgC;IAChC,MAAM,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC;CACrC,CAAC;AAgBF;;;GAGG;AACH,eAAO,MAAM,gBAAgB,GAAI,SAAQ,SAAS,OAAO,EAAO;oBACvC,OAAO,KAAG,OAAO;oBAsEjB,OAAO,KAAG,OAAO;uBAgBnB,OAAO;iBACb,MAAM;CAEtB,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAAC"}
@@ -0,0 +1,29 @@
1
+ import { RowKey } from '../collection/collection.types';
2
+ import { CollectionRuntime } from '../db/collection.runtime';
3
+ import { ListPlan } from '../query/planner';
4
+ import { QueryDefinition } from '../query/query.types';
5
+ import { Row } from '../store/store.types';
6
+ /** A loaded subset as persistence and server rendering keep it */
7
+ export type SubsetSnapshot = {
8
+ plan: ListPlan;
9
+ definition: Pick<QueryDefinition, "orderBy" | "limit">;
10
+ owned: RowKey[];
11
+ cursor: unknown;
12
+ hasMore: boolean;
13
+ /** How many pages a paginated subset had loaded, so a refetch reloads all of them */
14
+ pages: number;
15
+ /** When the subset loaded, or 0 when that is unknown */
16
+ loadedAt: number;
17
+ };
18
+ /** A collection's server rows and loaded subsets. Optimistic layers are never part of it. */
19
+ export type CollectionSnapshot = {
20
+ rows: [RowKey, Row][];
21
+ subsets: SubsetSnapshot[];
22
+ };
23
+ export declare const snapshotCollection: (collection: CollectionRuntime) => CollectionSnapshot;
24
+ /**
25
+ * Writes a snapshot's rows and subsets into a collection. The subsets answer queries at once but count as stale,
26
+ * because events sent since the snapshot was taken were missed, so they refetch when a query mounts.
27
+ */
28
+ export declare const restoreSnapshot: (collection: CollectionRuntime, snapshot: CollectionSnapshot) => void;
29
+ //# sourceMappingURL=snapshot.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"snapshot.d.ts","sourceRoot":"","sources":["../../src/persistence/snapshot.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gCAAgC,CAAC;AAC7D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,sBAAsB,CAAC;AAEhD,kEAAkE;AAClE,MAAM,MAAM,cAAc,GAAG;IAC3B,IAAI,EAAE,QAAQ,CAAC;IACf,UAAU,EAAE,IAAI,CAAC,eAAe,EAAE,SAAS,GAAG,OAAO,CAAC,CAAC;IACvD,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,MAAM,EAAE,OAAO,CAAC;IAChB,OAAO,EAAE,OAAO,CAAC;IACjB,qFAAqF;IACrF,KAAK,EAAE,MAAM,CAAC;IACd,wDAAwD;IACxD,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,6FAA6F;AAC7F,MAAM,MAAM,kBAAkB,GAAG;IAC/B,IAAI,EAAE,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CAAC;IACtB,OAAO,EAAE,cAAc,EAAE,CAAC;CAC3B,CAAC;AAEF,eAAO,MAAM,kBAAkB,GAAI,YAAY,iBAAiB,KAAG,kBAmBlE,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,eAAe,GAAI,YAAY,iBAAiB,EAAE,UAAU,kBAAkB,SAyB1F,CAAC"}
@@ -0,0 +1,53 @@
1
+ import { DbChannel, DbLockManager, TransactionRecord } from '../db/db.types';
2
+ export type TabsMessage =
3
+ /** A follower's writes, which only the leader persists and replays */
4
+ {
5
+ type: "enqueue";
6
+ records: TransactionRecord[];
7
+ }
8
+ /** A tab became the leader: followers send it the writes they still wait for */
9
+ | {
10
+ type: "leader";
11
+ }
12
+ /** A tab cleared the db, for example on sign-out */
13
+ | {
14
+ type: "clear";
15
+ }
16
+ /** A replayed write settled. `key` is the key the server gave an insert that left it to the server. */
17
+ | {
18
+ type: "outcome";
19
+ id: string;
20
+ success: boolean;
21
+ error?: unknown;
22
+ key?: string | number;
23
+ } | {
24
+ type: "rows";
25
+ collection: string;
26
+ rows: [string | number, Record<string, unknown>][];
27
+ };
28
+ /**
29
+ * Elects the one tab that replays the outbox and writes persisted rows, with Web Locks. Without Web Locks every tab
30
+ * treats itself as the leader. Tabs exchange outbox changes, outcomes and rows over a `BroadcastChannel`.
31
+ */
32
+ export declare class Tabs {
33
+ private readonly name;
34
+ private readonly handlers;
35
+ isLeader: boolean;
36
+ private closed;
37
+ private readonly locks;
38
+ private readonly channel;
39
+ private releaseLock;
40
+ private readonly listener;
41
+ constructor(name: string, options: {
42
+ locks?: DbLockManager;
43
+ channel?: DbChannel;
44
+ } | undefined, handlers: {
45
+ onLeader: () => void;
46
+ onMessage: (message: TabsMessage) => void;
47
+ });
48
+ start: () => void;
49
+ post: (message: TabsMessage) => void;
50
+ close: () => void;
51
+ private becomeLeader;
52
+ }
53
+ //# sourceMappingURL=tabs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tabs.d.ts","sourceRoot":"","sources":["../../src/persistence/tabs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAElF,MAAM,MAAM,WAAW;AACrB,sEAAsE;AACpE;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,OAAO,EAAE,iBAAiB,EAAE,CAAA;CAAE;AACnD,gFAAgF;GAC9E;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE;AACpB,oDAAoD;GAClD;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE;AACnB,uGAAuG;GACrG;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,OAAO,CAAC;IAAC,KAAK,CAAC,EAAE,OAAO,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,GACzF;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,EAAE,CAAA;CAAE,CAAC;AAc7F;;;GAGG;AACH,qBAAa,IAAI;IASb,OAAO,CAAC,QAAQ,CAAC,IAAI;IAErB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAV3B,QAAQ,UAAS;IACjB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA4B;IAClD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAwB;IAChD,OAAO,CAAC,WAAW,CAA2B;IAC9C,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAoF;gBAG1F,IAAI,EAAE,MAAM,EAC7B,OAAO,EAAE;QAAE,KAAK,CAAC,EAAE,aAAa,CAAC;QAAC,OAAO,CAAC,EAAE,SAAS,CAAA;KAAE,GAAG,SAAS,EAClD,QAAQ,EAAE;QAAE,QAAQ,EAAE,MAAM,IAAI,CAAC;QAAC,SAAS,EAAE,CAAC,OAAO,EAAE,WAAW,KAAK,IAAI,CAAA;KAAE;IAMhG,KAAK,aAgBH;IAEF,IAAI,GAAI,SAAS,WAAW,UAM1B;IAEF,KAAK,aAOH;IAEF,OAAO,CAAC,YAAY,CAGlB;CACH"}
@@ -0,0 +1,59 @@
1
+ import { DbRuntime } from '../db/db.runtime';
2
+ import { QueryDefinition, QueryState } from './query.types';
3
+ /** The state of an infinite query: the rows of its window, split into pages */
4
+ export type InfiniteQueryState<Item> = QueryState<Item[]> & {
5
+ /** `data` split into pages of `pageSize` */
6
+ pages: Item[][];
7
+ /** Whether more rows are loading to fill the window after `fetchNextPage` */
8
+ isFetchingNextPage: boolean;
9
+ };
10
+ export type InfiniteQueryOptions = {
11
+ /** How many rows each page holds. Pages are independent of the backend's page size. */
12
+ pageSize: number;
13
+ };
14
+ /**
15
+ * A window over an ordered live query: the first `pageSize` times the number of pages rows of its result. Widening it
16
+ * shows rows the db holds at once and loads more from the query's loader only when the window runs past them.
17
+ *
18
+ * The window belongs to this instance, so two components showing the same query share its live query and its subset,
19
+ * and each keeps its own number of pages.
20
+ */
21
+ export declare class InfiniteQuery<Item> {
22
+ private readonly runtime;
23
+ private readonly definition;
24
+ private readonly options;
25
+ /** Identifies the infinite query by its query's structure and page size */
26
+ readonly key: string;
27
+ private pageCount;
28
+ private live;
29
+ /** The live query of the previous window, shown while a wider `limit` loads */
30
+ private previous;
31
+ private unsubscribeLive;
32
+ private listeners;
33
+ private filling;
34
+ private widening;
35
+ private readonly paging;
36
+ private snapshot;
37
+ constructor(runtime: DbRuntime, definition: QueryDefinition, options: InfiniteQueryOptions);
38
+ subscribe: (listener: () => void) => () => void;
39
+ getSnapshot: () => InfiniteQueryState<Item>;
40
+ /**
41
+ * Widens the window by one page. Rows the db holds show at once, and the loader loads more when the window runs past
42
+ * them. Resolves once the window is filled or the backend has no more rows.
43
+ */
44
+ fetchNextPage: () => Promise<void>;
45
+ /** Loads the query again from its first page */
46
+ refetch: () => Promise<void>;
47
+ private getDefinition;
48
+ /** The state and window the snapshot shows: the previous window while a wider one loads its first response */
49
+ private getSource;
50
+ private isFetchingNextPage;
51
+ private onLiveChange;
52
+ /** Loads backend pages until the db holds one row past the window, or the backend has none */
53
+ private fill;
54
+ /** A loader with `limit` and no cursor is sent the window size plus one, as a new live query */
55
+ private widenLimit;
56
+ private dropPrevious;
57
+ private notify;
58
+ }
59
+ //# sourceMappingURL=infinite-query.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"infinite-query.d.ts","sourceRoot":"","sources":["../../src/query/infinite-query.ts"],"names":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