@noy-db/hub 0.1.0-pre.10

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 (203) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +197 -0
  3. package/dist/aggregate/index.cjs +476 -0
  4. package/dist/aggregate/index.cjs.map +1 -0
  5. package/dist/aggregate/index.d.cts +38 -0
  6. package/dist/aggregate/index.d.ts +38 -0
  7. package/dist/aggregate/index.js +53 -0
  8. package/dist/aggregate/index.js.map +1 -0
  9. package/dist/blobs/index.cjs +1480 -0
  10. package/dist/blobs/index.cjs.map +1 -0
  11. package/dist/blobs/index.d.cts +45 -0
  12. package/dist/blobs/index.d.ts +45 -0
  13. package/dist/blobs/index.js +48 -0
  14. package/dist/blobs/index.js.map +1 -0
  15. package/dist/bundle/index.cjs +496 -0
  16. package/dist/bundle/index.cjs.map +1 -0
  17. package/dist/bundle/index.d.cts +7 -0
  18. package/dist/bundle/index.d.ts +7 -0
  19. package/dist/bundle/index.js +51 -0
  20. package/dist/bundle/index.js.map +1 -0
  21. package/dist/chunk-2QR2PQTT.js +217 -0
  22. package/dist/chunk-2QR2PQTT.js.map +1 -0
  23. package/dist/chunk-72UIIX3E.js +1109 -0
  24. package/dist/chunk-72UIIX3E.js.map +1 -0
  25. package/dist/chunk-A4NFZKRW.js +722 -0
  26. package/dist/chunk-A4NFZKRW.js.map +1 -0
  27. package/dist/chunk-AOYCZP2H.js +793 -0
  28. package/dist/chunk-AOYCZP2H.js.map +1 -0
  29. package/dist/chunk-CIMZBAZB.js +72 -0
  30. package/dist/chunk-CIMZBAZB.js.map +1 -0
  31. package/dist/chunk-E3AGCGJ4.js +160 -0
  32. package/dist/chunk-E3AGCGJ4.js.map +1 -0
  33. package/dist/chunk-EKX3YVCI.js +97 -0
  34. package/dist/chunk-EKX3YVCI.js.map +1 -0
  35. package/dist/chunk-EMIGCR7X.js +39 -0
  36. package/dist/chunk-EMIGCR7X.js.map +1 -0
  37. package/dist/chunk-EMMRIE3C.js +72 -0
  38. package/dist/chunk-EMMRIE3C.js.map +1 -0
  39. package/dist/chunk-EUNIORPU.js +680 -0
  40. package/dist/chunk-EUNIORPU.js.map +1 -0
  41. package/dist/chunk-FZU343FL.js +32 -0
  42. package/dist/chunk-FZU343FL.js.map +1 -0
  43. package/dist/chunk-GHGXG53C.js +795 -0
  44. package/dist/chunk-GHGXG53C.js.map +1 -0
  45. package/dist/chunk-GKA4BGJN.js +79 -0
  46. package/dist/chunk-GKA4BGJN.js.map +1 -0
  47. package/dist/chunk-HG2OWBLX.js +430 -0
  48. package/dist/chunk-HG2OWBLX.js.map +1 -0
  49. package/dist/chunk-IGAROPKM.js +34 -0
  50. package/dist/chunk-IGAROPKM.js.map +1 -0
  51. package/dist/chunk-J66GRPNH.js +111 -0
  52. package/dist/chunk-J66GRPNH.js.map +1 -0
  53. package/dist/chunk-LVMMDXFT.js +275 -0
  54. package/dist/chunk-LVMMDXFT.js.map +1 -0
  55. package/dist/chunk-M5INGEFC.js +84 -0
  56. package/dist/chunk-M5INGEFC.js.map +1 -0
  57. package/dist/chunk-NBYQNDXA.js +557 -0
  58. package/dist/chunk-NBYQNDXA.js.map +1 -0
  59. package/dist/chunk-NPC4LFV5.js +132 -0
  60. package/dist/chunk-NPC4LFV5.js.map +1 -0
  61. package/dist/chunk-NSWHB5VQ.js +1285 -0
  62. package/dist/chunk-NSWHB5VQ.js.map +1 -0
  63. package/dist/chunk-OLM4LA6K.js +392 -0
  64. package/dist/chunk-OLM4LA6K.js.map +1 -0
  65. package/dist/chunk-UAFBZWFB.js +155 -0
  66. package/dist/chunk-UAFBZWFB.js.map +1 -0
  67. package/dist/chunk-UF3BUNQZ.js +1 -0
  68. package/dist/chunk-UF3BUNQZ.js.map +1 -0
  69. package/dist/chunk-UMMAVAYW.js +17 -0
  70. package/dist/chunk-UMMAVAYW.js.map +1 -0
  71. package/dist/chunk-UPY7WLBH.js +381 -0
  72. package/dist/chunk-UPY7WLBH.js.map +1 -0
  73. package/dist/chunk-W63BWEJH.js +311 -0
  74. package/dist/chunk-W63BWEJH.js.map +1 -0
  75. package/dist/chunk-WIGI5OJK.js +90 -0
  76. package/dist/chunk-WIGI5OJK.js.map +1 -0
  77. package/dist/chunk-XNL2TKKR.js +490 -0
  78. package/dist/chunk-XNL2TKKR.js.map +1 -0
  79. package/dist/chunk-XWNUJPIS.js +367 -0
  80. package/dist/chunk-XWNUJPIS.js.map +1 -0
  81. package/dist/chunk-YWKJZZGV.js +715 -0
  82. package/dist/chunk-YWKJZZGV.js.map +1 -0
  83. package/dist/consent/index.cjs +204 -0
  84. package/dist/consent/index.cjs.map +1 -0
  85. package/dist/consent/index.d.cts +24 -0
  86. package/dist/consent/index.d.ts +24 -0
  87. package/dist/consent/index.js +23 -0
  88. package/dist/consent/index.js.map +1 -0
  89. package/dist/crdt/index.cjs +152 -0
  90. package/dist/crdt/index.cjs.map +1 -0
  91. package/dist/crdt/index.d.cts +30 -0
  92. package/dist/crdt/index.d.ts +30 -0
  93. package/dist/crdt/index.js +24 -0
  94. package/dist/crdt/index.js.map +1 -0
  95. package/dist/crypto-6PNIHP7W.js +44 -0
  96. package/dist/crypto-6PNIHP7W.js.map +1 -0
  97. package/dist/delegation-WVIVMF73.js +17 -0
  98. package/dist/delegation-WVIVMF73.js.map +1 -0
  99. package/dist/dev-unlock-D4xB0_gs.d.cts +263 -0
  100. package/dist/dev-unlock-Dz8GEbd3.d.ts +263 -0
  101. package/dist/hash--EflSV65.d.cts +63 -0
  102. package/dist/hash-CRdXYnv3.d.ts +63 -0
  103. package/dist/history/index.cjs +1215 -0
  104. package/dist/history/index.cjs.map +1 -0
  105. package/dist/history/index.d.cts +62 -0
  106. package/dist/history/index.d.ts +62 -0
  107. package/dist/history/index.js +79 -0
  108. package/dist/history/index.js.map +1 -0
  109. package/dist/i18n/index.cjs +840 -0
  110. package/dist/i18n/index.cjs.map +1 -0
  111. package/dist/i18n/index.d.cts +38 -0
  112. package/dist/i18n/index.d.ts +38 -0
  113. package/dist/i18n/index.js +68 -0
  114. package/dist/i18n/index.js.map +1 -0
  115. package/dist/index-CD1VnONm.d.cts +415 -0
  116. package/dist/index-CLRxPs-W.d.cts +1960 -0
  117. package/dist/index-CUi9wfss.d.ts +415 -0
  118. package/dist/index-DtV93TMP.d.ts +1960 -0
  119. package/dist/index.cjs +17387 -0
  120. package/dist/index.cjs.map +1 -0
  121. package/dist/index.d.cts +565 -0
  122. package/dist/index.d.ts +565 -0
  123. package/dist/index.js +7525 -0
  124. package/dist/index.js.map +1 -0
  125. package/dist/indexing/index.cjs +736 -0
  126. package/dist/indexing/index.cjs.map +1 -0
  127. package/dist/indexing/index.d.cts +36 -0
  128. package/dist/indexing/index.d.ts +36 -0
  129. package/dist/indexing/index.js +77 -0
  130. package/dist/indexing/index.js.map +1 -0
  131. package/dist/lazy-builder-BwEoBQZ9.d.ts +304 -0
  132. package/dist/lazy-builder-CZVLKh0Z.d.cts +304 -0
  133. package/dist/ledger-HBBH2NPZ.js +33 -0
  134. package/dist/ledger-HBBH2NPZ.js.map +1 -0
  135. package/dist/mime-magic-CBBSOkjm.d.cts +50 -0
  136. package/dist/mime-magic-CBBSOkjm.d.ts +50 -0
  137. package/dist/periods/index.cjs +1035 -0
  138. package/dist/periods/index.cjs.map +1 -0
  139. package/dist/periods/index.d.cts +21 -0
  140. package/dist/periods/index.d.ts +21 -0
  141. package/dist/periods/index.js +25 -0
  142. package/dist/periods/index.js.map +1 -0
  143. package/dist/predicate-SBHmi6D0.d.cts +161 -0
  144. package/dist/predicate-SBHmi6D0.d.ts +161 -0
  145. package/dist/public-envelope-TLQA6REO.js +31 -0
  146. package/dist/public-envelope-TLQA6REO.js.map +1 -0
  147. package/dist/query/index.cjs +1999 -0
  148. package/dist/query/index.cjs.map +1 -0
  149. package/dist/query/index.d.cts +3 -0
  150. package/dist/query/index.d.ts +3 -0
  151. package/dist/query/index.js +73 -0
  152. package/dist/query/index.js.map +1 -0
  153. package/dist/session/index.cjs +495 -0
  154. package/dist/session/index.cjs.map +1 -0
  155. package/dist/session/index.d.cts +45 -0
  156. package/dist/session/index.d.ts +45 -0
  157. package/dist/session/index.js +51 -0
  158. package/dist/session/index.js.map +1 -0
  159. package/dist/shadow/index.cjs +133 -0
  160. package/dist/shadow/index.cjs.map +1 -0
  161. package/dist/shadow/index.d.cts +16 -0
  162. package/dist/shadow/index.d.ts +16 -0
  163. package/dist/shadow/index.js +20 -0
  164. package/dist/shadow/index.js.map +1 -0
  165. package/dist/store/index.cjs +1083 -0
  166. package/dist/store/index.cjs.map +1 -0
  167. package/dist/store/index.d.cts +491 -0
  168. package/dist/store/index.d.ts +491 -0
  169. package/dist/store/index.js +37 -0
  170. package/dist/store/index.js.map +1 -0
  171. package/dist/strategy-BSxFXGzb.d.cts +110 -0
  172. package/dist/strategy-BSxFXGzb.d.ts +110 -0
  173. package/dist/strategy-D-SrOLCl.d.cts +548 -0
  174. package/dist/strategy-D-SrOLCl.d.ts +548 -0
  175. package/dist/sync/index.cjs +1062 -0
  176. package/dist/sync/index.cjs.map +1 -0
  177. package/dist/sync/index.d.cts +42 -0
  178. package/dist/sync/index.d.ts +42 -0
  179. package/dist/sync/index.js +28 -0
  180. package/dist/sync/index.js.map +1 -0
  181. package/dist/team/index.cjs +2606 -0
  182. package/dist/team/index.cjs.map +1 -0
  183. package/dist/team/index.d.cts +117 -0
  184. package/dist/team/index.d.ts +117 -0
  185. package/dist/team/index.js +106 -0
  186. package/dist/team/index.js.map +1 -0
  187. package/dist/tx/index.cjs +212 -0
  188. package/dist/tx/index.cjs.map +1 -0
  189. package/dist/tx/index.d.cts +20 -0
  190. package/dist/tx/index.d.ts +20 -0
  191. package/dist/tx/index.js +20 -0
  192. package/dist/tx/index.js.map +1 -0
  193. package/dist/types-DSFLtbKg.d.ts +9702 -0
  194. package/dist/types-zwwMOqkg.d.cts +9702 -0
  195. package/dist/ulid-COREQ2RQ.js +9 -0
  196. package/dist/ulid-COREQ2RQ.js.map +1 -0
  197. package/dist/util/index.cjs +230 -0
  198. package/dist/util/index.cjs.map +1 -0
  199. package/dist/util/index.d.cts +77 -0
  200. package/dist/util/index.d.ts +77 -0
  201. package/dist/util/index.js +190 -0
  202. package/dist/util/index.js.map +1 -0
  203. package/package.json +244 -0
@@ -0,0 +1,1960 @@
1
+ import { C as CollectionIndexes, a as Clause, O as Operator } from './predicate-SBHmi6D0.cjs';
2
+ import { A as AggregateStrategy, b as AggregateSpec, c as Aggregation, a as AggregateResult, g as GroupedQuery } from './strategy-D-SrOLCl.cjs';
3
+
4
+ /**
5
+ * All NOYDB error classes — a single import surface for `catch` blocks and
6
+ * `instanceof` checks.
7
+ *
8
+ * ## Class hierarchy
9
+ *
10
+ * ```
11
+ * Error
12
+ * └─ NoydbError (code: string)
13
+ * ├─ Crypto errors
14
+ * │ ├─ DecryptionError — AES-GCM tag failure
15
+ * │ ├─ TamperedError — ciphertext modified after write
16
+ * │ └─ InvalidKeyError — wrong passphrase / corrupt keyring
17
+ * ├─ Access errors
18
+ * │ ├─ NoAccessError — no DEK for this collection
19
+ * │ ├─ ReadOnlyError — ro permission, write attempted
20
+ * │ ├─ PermissionDeniedError — role too low for operation
21
+ * │ ├─ PrivilegeEscalationError — grant wider than grantor holds
22
+ * │ └─ StoreCapabilityError — optional store method missing
23
+ * ├─ Sync errors
24
+ * │ ├─ ConflictError — optimistic-lock version mismatch
25
+ * │ ├─ BundleVersionConflictError — bundle push rejected by remote
26
+ * │ └─ NetworkError — push/pull network failure
27
+ * ├─ Data errors
28
+ * │ ├─ NotFoundError — get(id) on missing record
29
+ * │ ├─ ValidationError — application-level guard failed
30
+ * │ └─ SchemaValidationError — Standard Schema v1 rejection
31
+ * ├─ Query errors
32
+ * │ ├─ JoinTooLargeError — join row ceiling exceeded
33
+ * │ ├─ DanglingReferenceError — strict ref() points at nothing
34
+ * │ ├─ GroupCardinalityError — groupBy bucket cap exceeded
35
+ * │ ├─ IndexRequiredError — lazy-mode query touches unindexed field
36
+ * │ └─ IndexWriteFailureError — index side-car put/delete failed post-main
37
+ * ├─ i18n / Dictionary errors
38
+ * │ ├─ ReservedCollectionNameError
39
+ * │ ├─ DictKeyMissingError
40
+ * │ ├─ DictKeyInUseError
41
+ * │ ├─ MissingTranslationError
42
+ * │ ├─ LocaleNotSpecifiedError
43
+ * │ └─ TranslatorNotConfiguredError
44
+ * ├─ Backup errors
45
+ * │ ├─ BackupLedgerError — hash-chain verification failed
46
+ * │ └─ BackupCorruptedError — envelope hash mismatch in dump
47
+ * ├─ Bundle errors
48
+ * │ └─ BundleIntegrityError — .noydb body sha256 mismatch
49
+ * └─ Session errors
50
+ * ├─ SessionExpiredError
51
+ * ├─ SessionNotFoundError
52
+ * └─ SessionPolicyError
53
+ * ```
54
+ *
55
+ * ## Catching all NOYDB errors
56
+ *
57
+ * ```ts
58
+ * import { NoydbError, InvalidKeyError, ConflictError } from '@noy-db/hub'
59
+ *
60
+ * try {
61
+ * await vault.unlock(passphrase)
62
+ * } catch (e) {
63
+ * if (e instanceof InvalidKeyError) { showBadPassphraseUI(); return }
64
+ * if (e instanceof NoydbError) { logToSentry(e.code, e); return }
65
+ * throw e // unexpected — re-throw
66
+ * }
67
+ * ```
68
+ *
69
+ * @module
70
+ */
71
+ /**
72
+ * Base class for all NOYDB errors.
73
+ *
74
+ * Every error thrown by `@noy-db/hub` extends this class, so consumers can
75
+ * catch all NOYDB errors in a single `catch (e) { if (e instanceof NoydbError) ... }`
76
+ * block. The `code` field is a machine-readable string (e.g. `'DECRYPTION_FAILED'`)
77
+ * suitable for `switch` statements and logging pipelines.
78
+ */
79
+ declare class NoydbError extends Error {
80
+ /** Machine-readable error code. Stable across library versions. */
81
+ readonly code: string;
82
+ constructor(code: string, message: string);
83
+ }
84
+ /**
85
+ * Thrown when AES-GCM decryption fails.
86
+ *
87
+ * The most common cause is a wrong passphrase or a corrupted ciphertext.
88
+ * A `DecryptionError` at the wrong passphrase level is caught internally
89
+ * and re-thrown as `InvalidKeyError` — so in practice this surfaces for
90
+ * per-record corruption rather than authentication failures.
91
+ */
92
+ declare class DecryptionError extends NoydbError {
93
+ constructor(message?: string);
94
+ }
95
+ /**
96
+ * Thrown when GCM tag verification fails, indicating the ciphertext was
97
+ * modified after encryption.
98
+ *
99
+ * AES-256-GCM is authenticated encryption — the tag over the ciphertext
100
+ * is checked on every decrypt. If any byte was flipped (accidental
101
+ * corruption or deliberate tampering), decryption throws this error.
102
+ * Treat it as a security alert: the stored bytes are not what NOYDB wrote.
103
+ */
104
+ declare class TamperedError extends NoydbError {
105
+ constructor(message?: string);
106
+ }
107
+ /**
108
+ * Thrown when key unwrapping fails, typically because the passphrase is wrong
109
+ * or the keyring file is corrupted.
110
+ *
111
+ * NOYDB uses AES-KW (RFC 3394) to wrap DEKs with the KEK. If AES-KW
112
+ * unwrapping fails, it means either the KEK was derived from the wrong
113
+ * passphrase (PBKDF2 with 600K iterations) or the keyring bytes are
114
+ * corrupted. This is the error shown to the user on a failed unlock attempt.
115
+ */
116
+ declare class InvalidKeyError extends NoydbError {
117
+ constructor(message?: string);
118
+ }
119
+ /**
120
+ * Thrown when a keyring's wrapped-DEK set unwraps partially — at least
121
+ * one DEK succeeds (proving the KEK is correct) but at least one fails.
122
+ * The passphrase is right; the failed entries are corrupted.
123
+ *
124
+ * This is distinct from {@link InvalidKeyError} so that
125
+ * `NoydbOptions.onInvalidKey: 'reset'` does NOT fire — resetting on
126
+ * partial corruption would destroy the still-valid DEKs and the data
127
+ * they protect, which is silent data loss in response to a feature
128
+ * designed for stale-credential recovery.
129
+ */
130
+ declare class KeyringCorruptError extends NoydbError {
131
+ readonly failedCollections: readonly string[];
132
+ readonly intactCount: number;
133
+ constructor(opts: {
134
+ failedCollections: readonly string[];
135
+ intactCount: number;
136
+ message?: string;
137
+ });
138
+ }
139
+ /**
140
+ * Thrown when the authenticated user does not have a DEK for the requested
141
+ * collection — i.e. the collection is not in their keyring at all.
142
+ *
143
+ * This is the "no key for this door" error. It is different from
144
+ * `ReadOnlyError` (user has a key but it only grants ro) and from
145
+ * `PermissionDeniedError` (user's role doesn't allow the operation).
146
+ */
147
+ declare class NoAccessError extends NoydbError {
148
+ constructor(message?: string);
149
+ }
150
+ /**
151
+ * Thrown when a user with read-only (`ro`) permission attempts a write
152
+ * operation (`put` or `delete`) on a collection.
153
+ *
154
+ * The user has a DEK for the collection (they can decrypt and read), but
155
+ * their keyring grants only `ro`. To fix: re-grant the user with `rw`
156
+ * permission, or do not attempt writes as a viewer/client role.
157
+ */
158
+ declare class ReadOnlyError extends NoydbError {
159
+ constructor(message?: string);
160
+ }
161
+ /**
162
+ * Thrown when a write is attempted against a historical view produced
163
+ * by `vault.at(timestamp)`. Time-machine views are read-only by
164
+ * contract — mutating the past would require either the shadow-vault
165
+ * mechanism or a ledger-history rewrite (which breaks
166
+ * the tamper-evidence guarantee).
167
+ *
168
+ * Distinct from {@link ReadOnlyError} (keyring-level) and
169
+ * {@link PermissionDeniedError} (role-level): this error is about the
170
+ * *view* being historical, independent of the caller's permissions.
171
+ */
172
+ declare class ReadOnlyAtInstantError extends NoydbError {
173
+ constructor(operation: string, timestamp: string);
174
+ }
175
+ /**
176
+ * Thrown when a write is attempted against a shadow-vault frame
177
+ * produced by `vault.frame()`. Frames are read-only by contract —
178
+ * the use case is screen-sharing / demos / compliance review where
179
+ * the operator wants to prevent accidental edits.
180
+ *
181
+ * Behavioural enforcement only — the underlying keyring still holds
182
+ * write-capable DEKs. See {@link VaultFrame} for the full caveat.
183
+ */
184
+ declare class ReadOnlyFrameError extends NoydbError {
185
+ constructor(operation: string);
186
+ }
187
+ /**
188
+ * Thrown when the authenticated user's role does not permit the requested
189
+ * operation — e.g. a `viewer` calling `grantAccess()`, or an `operator`
190
+ * calling `rotateKeys()`.
191
+ *
192
+ * This is a role-level check (what the user's role allows), distinct from
193
+ * `NoAccessError` (collection not in keyring) and `ReadOnlyError` (in
194
+ * keyring, but write not allowed).
195
+ */
196
+ declare class PermissionDeniedError extends NoydbError {
197
+ constructor(message?: string);
198
+ }
199
+ /**
200
+ * Thrown when an `@noy-db/as-*` export is attempted without the
201
+ * required capability bit on the invoking keyring.
202
+ *
203
+ * Two sub-cases discriminated by the `tier` field:
204
+ *
205
+ * - `tier: 'plaintext'` — a plaintext-tier export (`as-xlsx`,
206
+ * `as-csv`, `as-blob`, `as-zip`, …) was attempted but the
207
+ * keyring's `exportCapability.plaintext` does not include the
208
+ * requested `format` (nor the `'*'` wildcard). Default for every
209
+ * role is `plaintext: []` — the owner must positively grant.
210
+ * - `tier: 'bundle'` — an encrypted `as-noydb` bundle export was
211
+ * attempted but the keyring's `exportCapability.bundle` is
212
+ * `false`. Default for `owner`/`admin` is `true`; for
213
+ * `operator`/`viewer`/`client` it is `false`.
214
+ *
215
+ * Distinct from `PermissionDeniedError` (role-level check) and
216
+ * `NoAccessError` (collection not readable). Surfaces separately so
217
+ * UI layers can show a "request the export capability from your
218
+ * admin" flow rather than a generic permission error.
219
+ */
220
+ declare class ExportCapabilityError extends NoydbError {
221
+ readonly tier: 'plaintext' | 'bundle';
222
+ readonly format?: string;
223
+ readonly userId: string;
224
+ constructor(opts: {
225
+ tier: 'plaintext' | 'bundle';
226
+ userId: string;
227
+ format?: string;
228
+ message?: string;
229
+ });
230
+ }
231
+ /**
232
+ * Thrown when a keyring file's `expires_at` cutoff has passed.
233
+ * Surfaced by `loadKeyring` before any DEK unwrap is attempted —
234
+ * past the cutoff the slot refuses to open even with the right
235
+ * passphrase. Distinct from PBKDF2 / unwrap errors so consumer code
236
+ * can show a precise "this bundle slot has expired" message instead
237
+ * of the generic decryption-failure UX.
238
+ *
239
+ * Used predominantly on `BundleRecipient` slots produced by
240
+ * `writeNoydbBundle({ recipients: [...] })` to time-box audit access.
241
+ */
242
+ declare class KeyringExpiredError extends NoydbError {
243
+ readonly userId: string;
244
+ readonly expiresAt: string;
245
+ constructor(opts: {
246
+ userId: string;
247
+ expiresAt: string;
248
+ });
249
+ }
250
+ /**
251
+ * Thrown when an `@noy-db/as-*` import is attempted but the invoking
252
+ * keyring lacks the required import-capability bit.
253
+ *
254
+ * - `tier: 'plaintext'` — a plaintext-tier import (`as-csv`, `as-json`,
255
+ * `as-ndjson`, `as-zip`, …) was attempted but the keyring's
256
+ * `importCapability.plaintext` does not include the requested
257
+ * `format` (nor the `'*'` wildcard).
258
+ * - `tier: 'bundle'` — a `.noydb` bundle import was attempted but the
259
+ * keyring's `importCapability.bundle` is not `true`.
260
+ *
261
+ * Default for every role on every dimension is closed — owners and
262
+ * admins must positively grant the capability. Distinct from
263
+ * `PermissionDeniedError` and `NoAccessError` so UI layers can show a
264
+ * specific "request the import capability" flow.
265
+ */
266
+ declare class ImportCapabilityError extends NoydbError {
267
+ readonly tier: 'plaintext' | 'bundle';
268
+ readonly format?: string;
269
+ readonly userId: string;
270
+ constructor(opts: {
271
+ tier: 'plaintext' | 'bundle';
272
+ userId: string;
273
+ format?: string;
274
+ message?: string;
275
+ });
276
+ }
277
+ /**
278
+ * Thrown when a grant would give the grantee a permission the grantor
279
+ * does not themselves hold — the "admin cannot grant what admin cannot
280
+ * do" rule from the admin-delegation work.
281
+ *
282
+ * Distinct from `PermissionDeniedError` so callers can tell the two
283
+ * cases apart in logs and tests:
284
+ *
285
+ * - `PermissionDeniedError` — "you are not allowed to perform this
286
+ * operation at all" (wrong role).
287
+ * - `PrivilegeEscalationError` — "you are allowed to grant, but not
288
+ * with these specific permissions" (widening attempt).
289
+ *
290
+ * Under the admin model the grantee of an admin-grants-admin call
291
+ * inherits the caller's entire DEK set by construction, so this error
292
+ * is structurally unreachable in typical flows. The check and error
293
+ * class exist so that future per-collection admin scoping cannot
294
+ * accidentally bypass the subset rule — the guard is already wired in.
295
+ *
296
+ * `offendingCollection` carries the first collection name that failed
297
+ * the subset check, to make the violation actionable in error output.
298
+ */
299
+ /**
300
+ * Thrown when a caller invokes an API that requires an optional
301
+ * store capability the active store does not implement.
302
+ *
303
+ * Today the only call site is `Noydb.listAccessibleVaults()`,
304
+ * which depends on the optional `NoydbStore.listVaults()`
305
+ * method. The error message names the missing method and the calling
306
+ * API so consumers know exactly which combination is unsupported,
307
+ * and the `capability` field is machine-readable so library code can
308
+ * pattern-match in catch blocks (e.g. fall back to a candidate-list
309
+ * shape).
310
+ *
311
+ * The class lives in `errors.ts` rather than as a generic
312
+ * `ValidationError` because the diagnostic shape is different: a
313
+ * `ValidationError` says "the inputs you passed are wrong"; this
314
+ * error says "the inputs are fine, but the store you wired up
315
+ * doesn't support what you're asking for." Different fix, different
316
+ * documentation.
317
+ */
318
+ declare class StoreCapabilityError extends NoydbError {
319
+ /** The store method/capability that was missing. */
320
+ readonly capability: string;
321
+ constructor(capability: string, callerApi: string, storeName?: string);
322
+ }
323
+ declare class PrivilegeEscalationError extends NoydbError {
324
+ readonly offendingCollection: string;
325
+ constructor(offendingCollection: string, message?: string);
326
+ }
327
+ /**
328
+ * Thrown by `Collection.put` / `.delete` when the target record's
329
+ * envelope `_ts` falls within a closed accounting period.
330
+ *
331
+ * Distinct from `ReadOnlyError` (keyring-level), `ReadOnlyAtInstantError`
332
+ * (historical view), and `ReadOnlyFrameError` (shadow vault): this
333
+ * error is about the STORED RECORD being sealed by an operator call
334
+ * to `vault.closePeriod()`, independent of caller permissions or
335
+ * view type. The `periodName` and `endDate` fields name the sealing
336
+ * period so audit UIs can surface a "this record is locked in
337
+ * FY2026-Q1 (closed 2026-03-31)" message without parsing the error
338
+ * string.
339
+ *
340
+ * To apply a correction after close, book a compensating entry in a
341
+ * new period rather than unlocking the old one. Re-opening a closed
342
+ * period is deliberately unsupported.
343
+ */
344
+ declare class PeriodClosedError extends NoydbError {
345
+ readonly periodName: string;
346
+ readonly endDate: string;
347
+ readonly recordTs: string;
348
+ constructor(periodName: string, endDate: string, recordTs: string);
349
+ }
350
+ /**
351
+ * Thrown when a user tries to act at a tier they are not cleared for.
352
+ *
353
+ * This is the umbrella error for tier write refusals:
354
+ * - `put({ tier: N })` when the user's keyring lacks tier-N DEK.
355
+ * - `elevate(id, N)` when the caller cannot reach tier N.
356
+ *
357
+ * Distinct from `TierAccessDeniedError` which covers *read* refusals on
358
+ * the invisibility/ghost path.
359
+ */
360
+ declare class TierNotGrantedError extends NoydbError {
361
+ readonly tier: number;
362
+ readonly collection: string;
363
+ constructor(collection: string, tier: number);
364
+ }
365
+ /**
366
+ * Thrown when an elevated-handle operation runs after the elevation's
367
+ * TTL expired. Reads continue at the original tier; only writes
368
+ * through the scoped handle flip to throwing once expired.
369
+ */
370
+ declare class ElevationExpiredError extends NoydbError {
371
+ readonly tier: number;
372
+ readonly expiresAt: number;
373
+ constructor(opts: {
374
+ tier: number;
375
+ expiresAt: number;
376
+ });
377
+ }
378
+ /**
379
+ * Thrown by `vault.elevate(...)` when an elevation is already active
380
+ * on the vault. Adopters must `release()` the existing handle before
381
+ * starting a new elevation.
382
+ */
383
+ declare class AlreadyElevatedError extends NoydbError {
384
+ readonly activeTier: number;
385
+ constructor(activeTier: number);
386
+ }
387
+ /**
388
+ * Thrown when `demote()` is called by someone who is not the original
389
+ * elevator and not an owner.
390
+ */
391
+ declare class TierDemoteDeniedError extends NoydbError {
392
+ constructor(id: string, tier: number);
393
+ }
394
+ /**
395
+ * Thrown when `db.delegate()` is called against a user that has no
396
+ * keyring in the target vault — the delegation token cannot be
397
+ * constructed without the target user's KEK wrap.
398
+ */
399
+ declare class DelegationTargetMissingError extends NoydbError {
400
+ readonly toUser: string;
401
+ constructor(toUser: string);
402
+ }
403
+ /**
404
+ * Thrown when a `put()` detects an optimistic concurrency conflict.
405
+ *
406
+ * NOYDB uses version numbers (`_v`) for optimistic locking. If a `put()`
407
+ * is called with `expectedVersion: N` but the stored record is at version
408
+ * `M ≠ N`, the write is rejected and the caller must re-read, re-apply their
409
+ * change, and retry. The `version` field carries the actual stored version
410
+ * so callers can decide whether to retry or surface the conflict to the user.
411
+ */
412
+ declare class ConflictError extends NoydbError {
413
+ /** The actual stored version at the time of conflict. */
414
+ readonly version: number;
415
+ constructor(version: number, message?: string);
416
+ }
417
+ /**
418
+ * Thrown by `LedgerStore.append()` after exhausting its CAS retry
419
+ * budget under multi-writer contention. Two browser tabs, a
420
+ * web app + an offline mobile peer, or a server worker pool all
421
+ * producing ledger entries against the same vault can race on the
422
+ * "read head, write head+1" cycle; the optimistic-CAS retry loop
423
+ * resolves the race for `casAtomic: true` stores, but pathological
424
+ * contention (or a buggy peer) can still exhaust the budget. When
425
+ * that happens, the chain is intact — the failed writer simply
426
+ * couldn't claim a slot. Caller's choice whether to retry, queue,
427
+ * or surface the failure to the user.
428
+ */
429
+ declare class LedgerContentionError extends NoydbError {
430
+ readonly attempts: number;
431
+ constructor(attempts: number);
432
+ }
433
+ /**
434
+ * Thrown when a bundle push is rejected because the remote has been updated
435
+ * since the local bundle was last pulled.
436
+ *
437
+ * Unlike `ConflictError` (per-record), this is a whole-bundle conflict —
438
+ * the remote's bundle handle has changed. The caller must pull the new
439
+ * bundle, merge, and re-push. `remoteVersion` is the handle of the newer
440
+ * remote bundle for use in diagnostics.
441
+ */
442
+ declare class BundleVersionConflictError extends NoydbError {
443
+ /** The bundle handle of the newer remote version that rejected the push. */
444
+ readonly remoteVersion: string;
445
+ constructor(remoteVersion: string, message?: string);
446
+ }
447
+ /**
448
+ * Thrown when a sync operation (push or pull) fails due to a network error.
449
+ *
450
+ * NOYDB's offline-first design means network errors are expected during sync.
451
+ * Callers should catch `NetworkError`, surface connectivity status in the UI,
452
+ * and rely on the `SyncScheduler` to retry when connectivity is restored.
453
+ */
454
+ declare class NetworkError extends NoydbError {
455
+ constructor(message?: string);
456
+ }
457
+ /**
458
+ * Thrown when `collection.get(id)` is called with an ID that does not exist.
459
+ *
460
+ * NOYDB collections are memory-first, so this error is synchronous and cheap —
461
+ * it does not make a network round-trip. Callers that expect the record to be
462
+ * absent should use `collection.getOrNull(id)` instead.
463
+ */
464
+ declare class NotFoundError extends NoydbError {
465
+ constructor(message?: string);
466
+ }
467
+ /**
468
+ * Thrown when application-level validation fails before encryption.
469
+ *
470
+ * Distinct from `SchemaValidationError` (Standard Schema v1 validator)
471
+ * and `MissingTranslationError` (i18nText). `ValidationError` is the
472
+ * general-purpose validation base — use it for custom guards in `put()`
473
+ * hooks or store middleware.
474
+ */
475
+ declare class ValidationError extends NoydbError {
476
+ constructor(message?: string);
477
+ }
478
+ /**
479
+ * Thrown when a Standard Schema v1 validator rejects a record on
480
+ * `put()` (input validation) or on read (output validation). Carries
481
+ * the raw issue list so callers can render field-level errors.
482
+ *
483
+ * `direction` distinguishes the two cases:
484
+ * - `'input'`: the user passed bad data into `put()`. This is a
485
+ * normal error case that application code should handle — typically
486
+ * by showing validation messages in the UI.
487
+ * - `'output'`: stored data does not match the current schema. This
488
+ * indicates a schema drift (the schema was changed without
489
+ * migrating the existing records) and should be treated as a bug
490
+ * — the application should not swallow it silently.
491
+ *
492
+ * The `issues` type is deliberately `readonly unknown[]` on this class
493
+ * so that `errors.ts` doesn't need to import from `schema.ts` (and
494
+ * create a dependency cycle). Callers who know they're holding a
495
+ * `SchemaValidationError` can cast to the more precise
496
+ * `readonly StandardSchemaV1Issue[]` from `schema.ts`.
497
+ */
498
+ declare class SchemaValidationError extends NoydbError {
499
+ readonly issues: readonly unknown[];
500
+ readonly direction: 'input' | 'output';
501
+ constructor(message: string, issues: readonly unknown[], direction: 'input' | 'output');
502
+ }
503
+ /**
504
+ * Thrown when `.groupBy().aggregate()` produces more than the hard
505
+ * cardinality cap (default 100_000 groups)..
506
+ *
507
+ * The cap exists because `.groupBy()` materializes one bucket per
508
+ * distinct key value in memory, and runaway cardinality — a groupBy
509
+ * on a high-uniqueness field like `id` or `createdAt` — is almost
510
+ * always a query mistake rather than legitimate use. A hard error is
511
+ * better than silent OOM: the consumer sees an actionable message
512
+ * naming the field and the observed cardinality, with guidance to
513
+ * either narrow the query with `.where()` or accept the ceiling
514
+ * override.
515
+ *
516
+ * A separate one-shot warning fires at 10% of the cap (10_000
517
+ * groups) so consumers get a heads-up before the hard error — same
518
+ * pattern as `JoinTooLargeError` and the `.join()` row ceiling.
519
+ *
520
+ * **Not overridable in.** The 100k cap is a fixed constant so
521
+ * the failure mode is consistent across the codebase; a
522
+ * `{ maxGroups }` override can be added later without a break if a
523
+ * real consumer asks.
524
+ */
525
+ declare class GroupCardinalityError extends NoydbError {
526
+ /** The field being grouped on. */
527
+ readonly field: string;
528
+ /** Observed number of distinct groups at the moment the cap tripped. */
529
+ readonly cardinality: number;
530
+ /** The cap that was exceeded. */
531
+ readonly maxGroups: number;
532
+ constructor(field: string, cardinality: number, maxGroups: number);
533
+ }
534
+ /**
535
+ * Thrown in lazy mode when a `.query()` / `.where()` / `.orderBy()` clause
536
+ * references a field that does not have a declared index.
537
+ *
538
+ * Lazy-mode queries only work when every touched field is indexed.
539
+ * This is deliberate — silent scan-fallback would hide the performance
540
+ * cliff that lazy-mode indexes exist to prevent.
541
+ *
542
+ * Payload:
543
+ * - `collection` — name of the collection queried
544
+ * - `touchedFields` — every field referenced by the query (filter + order)
545
+ * - `missingFields` — subset of `touchedFields` that have no declared index
546
+ */
547
+ declare class IndexRequiredError extends NoydbError {
548
+ readonly collection: string;
549
+ readonly touchedFields: readonly string[];
550
+ readonly missingFields: readonly string[];
551
+ constructor(args: {
552
+ collection: string;
553
+ touchedFields: readonly string[];
554
+ missingFields: readonly string[];
555
+ });
556
+ }
557
+ /**
558
+ * Thrown (or surfaced via the `index:write-partial` event) when one or more
559
+ * per-indexed-field side-car writes fail after the main record write has
560
+ * already succeeded.
561
+ *
562
+ * Not thrown out of `.put()` / `.delete()` directly — those succeed when the
563
+ * main record succeeds. Instead, `IndexWriteFailureError` instances are collected
564
+ * into the session-scoped reconcile queue and emitted on the Collection
565
+ * emitter as `index:write-partial`.
566
+ *
567
+ * Payload:
568
+ * - `recordId` — the id of the main record whose side-car writes failed
569
+ * - `field` — the indexed field whose side-car write failed
570
+ * - `op` — `'put'` or `'delete'`, indicating which mutation was in flight
571
+ * - `cause` — the underlying error from the store
572
+ */
573
+ declare class IndexWriteFailureError extends NoydbError {
574
+ readonly recordId: string;
575
+ readonly field: string;
576
+ readonly op: 'put' | 'delete';
577
+ readonly cause: unknown;
578
+ constructor(args: {
579
+ recordId: string;
580
+ field: string;
581
+ op: 'put' | 'delete';
582
+ cause: unknown;
583
+ });
584
+ }
585
+ /**
586
+ * Thrown by `readNoydbBundle()` when the body bytes don't match
587
+ * the integrity hash declared in the bundle header — i.e. someone
588
+ * modified the bytes between write and read.
589
+ *
590
+ * Distinct from a generic `Error` (which would be thrown for
591
+ * format violations like a missing magic prefix or malformed
592
+ * header JSON) so consumers can pattern-match the corruption case
593
+ * and handle it differently from a producer bug. A
594
+ * `BundleIntegrityError` indicates "the bytes you got are not
595
+ * what was written"; a plain `Error` from `parsePrefixAndHeader`
596
+ * indicates "what was written wasn't a valid bundle in the first
597
+ * place."
598
+ *
599
+ * Also thrown when decompression fails after the integrity hash
600
+ * passed — that's a producer bug (the wrong algorithm byte was
601
+ * written) but it surfaces with the same error class because the
602
+ * end result is "the body cannot be turned back into a dump."
603
+ */
604
+ declare class BundleIntegrityError extends NoydbError {
605
+ constructor(message: string);
606
+ }
607
+ /**
608
+ * Thrown when `vault.collection()` is called with a name that is
609
+ * reserved for NOYDB internal use (any name starting with `_dict_`).
610
+ *
611
+ * Dictionary collections are accessed exclusively via
612
+ * `vault.dictionary(name)` — attempting to open one as a regular
613
+ * collection would bypass the dictionary invariants (ACL, rename
614
+ * tracking, reserved-name policy).
615
+ */
616
+ declare class ReservedCollectionNameError extends NoydbError {
617
+ /** The rejected collection name. */
618
+ readonly collectionName: string;
619
+ constructor(collectionName: string);
620
+ }
621
+ /**
622
+ * Thrown by `DictionaryHandle.get()` and `DictionaryHandle.delete()` when
623
+ * the requested key does not exist in the dictionary.
624
+ *
625
+ * Distinct from `NotFoundError` (which is for data records) so callers
626
+ * can distinguish "data record missing" from "dictionary key missing"
627
+ * without inspecting error messages.
628
+ */
629
+ declare class DictKeyMissingError extends NoydbError {
630
+ /** The dictionary name. */
631
+ readonly dictionaryName: string;
632
+ /** The key that was not found. */
633
+ readonly key: string;
634
+ constructor(dictionaryName: string, key: string);
635
+ }
636
+ /**
637
+ * Thrown by `DictionaryHandle.delete()` in strict mode when the key to
638
+ * be deleted is still referenced by one or more records.
639
+ *
640
+ * The caller must either rename the key first (the only sanctioned
641
+ * mass-mutation path) or pass `{ mode: 'warn' }` to skip the check
642
+ * (development only).
643
+ */
644
+ declare class DictKeyInUseError extends NoydbError {
645
+ /** The dictionary name. */
646
+ readonly dictionaryName: string;
647
+ /** The key that is still referenced. */
648
+ readonly key: string;
649
+ /** Name of the first collection found to reference this key. */
650
+ readonly usedBy: string;
651
+ /** Number of records in `usedBy` that reference this key. */
652
+ readonly count: number;
653
+ constructor(dictionaryName: string, key: string, usedBy: string, count: number);
654
+ }
655
+ /**
656
+ * Thrown by `Collection.put()` when an `i18nText` field is missing one
657
+ * or more required translations.
658
+ *
659
+ * The `missing` array names each locale code that was absent from the
660
+ * field value. The `field` property names the field so callers can
661
+ * render a field-level error message without parsing the string.
662
+ */
663
+ declare class MissingTranslationError extends NoydbError {
664
+ /** The field name whose translation(s) are missing. */
665
+ readonly field: string;
666
+ /** Locale codes that were required but absent. */
667
+ readonly missing: readonly string[];
668
+ constructor(field: string, missing: readonly string[], message?: string);
669
+ }
670
+ /**
671
+ * Thrown when reading an `i18nText` field without specifying a locale —
672
+ * either at the call site (`get(id, { locale })`) or on the vault
673
+ * (`openVault(name, { locale })`).
674
+ *
675
+ * Also thrown when `resolveI18nText()` exhausts the fallback chain and
676
+ * no translation is available for the requested locale.
677
+ *
678
+ * The `field` property names the field that triggered the error so the
679
+ * caller can surface it in the UI.
680
+ */
681
+ declare class LocaleNotSpecifiedError extends NoydbError {
682
+ /** The field name that required a locale. */
683
+ readonly field: string;
684
+ constructor(field: string, message?: string);
685
+ }
686
+ /**
687
+ * Thrown when a collection has an `i18nText` field with
688
+ * `autoTranslate: true` but no `plaintextTranslator` was configured
689
+ * on `createNoydb()`.
690
+ *
691
+ * The error is raised at `put()` time (not at schema construction) so
692
+ * the mis-configuration is surfaced by the first write rather than
693
+ * silently at startup.
694
+ */
695
+ declare class TranslatorNotConfiguredError extends NoydbError {
696
+ /** The field that requested auto-translation. */
697
+ readonly field: string;
698
+ /** The collection the put was targeting. */
699
+ readonly collection: string;
700
+ constructor(field: string, collection: string);
701
+ }
702
+ /**
703
+ * Thrown when `Vault.load()` finds that a backup's hash chain
704
+ * doesn't verify, or that its embedded `ledgerHead.hash` doesn't
705
+ * match the chain head reconstructed from the loaded entries.
706
+ *
707
+ * Distinct from `BackupCorruptedError` so callers can choose to
708
+ * recover from one but not the other (e.g., a corrupted JSON file is
709
+ * unrecoverable; a chain mismatch might mean the backup is from an
710
+ * incompatible noy-db version).
711
+ */
712
+ declare class BackupLedgerError extends NoydbError {
713
+ /** First-broken-entry index, if known. */
714
+ readonly divergedAt?: number;
715
+ constructor(message: string, divergedAt?: number);
716
+ }
717
+ /**
718
+ * Thrown when `Vault.load()` finds that the backup's data
719
+ * collection content doesn't match the ledger's recorded
720
+ * `payloadHash`es. This is the "envelope was tampered with after
721
+ * dump" detection — the chain itself can be intact, but if any
722
+ * encrypted record bytes were swapped, this check catches it.
723
+ */
724
+ declare class BackupCorruptedError extends NoydbError {
725
+ /** The (collection, id) pair whose envelope failed the hash check. */
726
+ readonly collection: string;
727
+ readonly id: string;
728
+ constructor(collection: string, id: string, message: string);
729
+ }
730
+ /**
731
+ * Thrown by `resolveSession()` when the session token's `expiresAt`
732
+ * timestamp is in the past. The session key is also removed from the
733
+ * in-memory store when this is thrown, so retrying with the same sessionId
734
+ * will produce `SessionNotFoundError`.
735
+ *
736
+ * Separate from `SessionNotFoundError` so callers can distinguish between
737
+ * "session is gone" (key store cleared, tab reloaded) and "session is
738
+ * still in the store but has exceeded its lifetime" (idle timeout, absolute
739
+ * timeout, policy-driven expiry). The remediation differs: expired sessions
740
+ * should prompt a fresh unlock; not-found sessions may indicate a bug or a
741
+ * cross-tab scenario where the session was never established.
742
+ */
743
+ declare class SessionExpiredError extends NoydbError {
744
+ readonly sessionId: string;
745
+ constructor(sessionId: string);
746
+ }
747
+ /**
748
+ * Thrown by `resolveSession()` when the session key cannot be found in
749
+ * the module-level store. This happens when:
750
+ * - The session was explicitly revoked via `revokeSession()`.
751
+ * - The JS context was reloaded (tab navigation, page refresh, worker restart).
752
+ * - `Noydb.close()` was called (which calls `revokeAllSessions()`).
753
+ * - The sessionId is wrong or was generated by a different JS context.
754
+ *
755
+ * The session token (if the caller holds it) is permanently useless after
756
+ * this error — the key is gone and cannot be recovered.
757
+ */
758
+ declare class SessionNotFoundError extends NoydbError {
759
+ readonly sessionId: string;
760
+ constructor(sessionId: string);
761
+ }
762
+ /**
763
+ * Thrown when a session policy blocks an operation — for example,
764
+ * `requireReAuthFor: ['export']` is set and the caller attempts to
765
+ * call `exportStream()` without re-authenticating for this session.
766
+ *
767
+ * The `operation` field names the specific operation that was blocked
768
+ * (e.g. `'export'`, `'grant'`, `'rotate'`) so the caller can surface
769
+ * a targeted prompt ("Please re-enter your passphrase to export data").
770
+ */
771
+ declare class SessionPolicyError extends NoydbError {
772
+ readonly operation: string;
773
+ constructor(operation: string, message?: string);
774
+ }
775
+ /**
776
+ * Thrown when a `.join()` would exceed its configured row ceiling on
777
+ * either side. The ceiling defaults to 50,000 per side and can be
778
+ * overridden via the `{ maxRows }` option on `.join()`.
779
+ *
780
+ * Carries both row counts so the error message can show which side
781
+ * tripped the limit (e.g. "left had 60,000 rows, right had 1,200,
782
+ * max was 50,000"). The `side` field is machine-readable so test
783
+ * code and devtools can match on it without regex-parsing the
784
+ * message.
785
+ *
786
+ * The row ceiling exists because joins are bounded in-memory
787
+ * operations over materialized record sets. Consumers whose
788
+ * collections genuinely exceed the ceiling should track
789
+ * (streaming joins over `scan()`) or filter the left side further
790
+ * with `where()` / `limit()` before joining.
791
+ */
792
+ declare class JoinTooLargeError extends NoydbError {
793
+ readonly leftRows: number;
794
+ readonly rightRows: number;
795
+ readonly maxRows: number;
796
+ readonly side: 'left' | 'right';
797
+ constructor(opts: {
798
+ leftRows: number;
799
+ rightRows: number;
800
+ maxRows: number;
801
+ side: 'left' | 'right';
802
+ message: string;
803
+ });
804
+ }
805
+ /**
806
+ * Thrown by `.join()` in strict `ref()` mode when a left-side record
807
+ * points at a right-side id that does not exist in the target
808
+ * collection.
809
+ *
810
+ * Distinct from `RefIntegrityError` so test code can pattern-match
811
+ * on the *read-time* dangling case without catching *write-time*
812
+ * integrity violations. Both indicate "ref points at nothing" but
813
+ * happen at different lifecycle phases and deserve different
814
+ * remediation in documentation: a RefIntegrityError on `put()`
815
+ * means the input is invalid; a DanglingReferenceError on `.join()`
816
+ * means stored data has drifted and `vault.checkIntegrity()`
817
+ * is the right tool to find the full set of orphans.
818
+ */
819
+ declare class DanglingReferenceError extends NoydbError {
820
+ readonly field: string;
821
+ readonly target: string;
822
+ readonly refId: string;
823
+ constructor(opts: {
824
+ field: string;
825
+ target: string;
826
+ refId: string;
827
+ message: string;
828
+ });
829
+ }
830
+ /**
831
+ * Thrown by {@link sanitizeFilename} when an input filename cannot be
832
+ * made safe — NUL byte, empty after normalization, missing
833
+ * `opaqueId` for the opaque profile, `..` segment, or a `maxBytes`
834
+ * cap too small to hold a single code point.
835
+ */
836
+ declare class FilenameSanitizationError extends NoydbError {
837
+ constructor(message: string);
838
+ }
839
+ /**
840
+ * Thrown when a write target resolves OUTSIDE the requested
841
+ * directory after sanitization — the canonical Zip-Slip class. The
842
+ * sanitizer's job is to strip path-traversal segments; this error
843
+ * is the defense-in-depth fallback at the FS write site.
844
+ */
845
+ declare class PathEscapeError extends NoydbError {
846
+ readonly attempted: string;
847
+ readonly targetDir: string;
848
+ constructor(opts: {
849
+ attempted: string;
850
+ targetDir: string;
851
+ });
852
+ }
853
+
854
+ /**
855
+ * Foreign-key references — the soft-FK mechanism.
856
+ *
857
+ * A collection declares its references as metadata at construction
858
+ * time:
859
+ *
860
+ * ```ts
861
+ * import { ref } from '@noy-db/hub'
862
+ *
863
+ * const invoices = company.collection<Invoice>('invoices', {
864
+ * refs: {
865
+ * clientId: ref('clients'), // default: strict
866
+ * categoryId: ref('categories', 'warn'),
867
+ * parentId: ref('invoices', 'cascade'), // self-reference OK
868
+ * },
869
+ * })
870
+ * ```
871
+ *
872
+ * Three modes:
873
+ *
874
+ * - **strict** — the default. `put()` rejects records whose
875
+ * reference target doesn't exist, and `delete()` of the target
876
+ * rejects if any strict-referencing records still exist.
877
+ * Matches SQL's default FK semantics.
878
+ *
879
+ * - **warn** — both operations succeed unconditionally. Broken
880
+ * references surface only through
881
+ * `vault.checkIntegrity()`, which walks every collection
882
+ * and reports orphans. Use when you want soft validation for
883
+ * imports from messy sources.
884
+ *
885
+ * - **cascade** — `put()` is same as warn. `delete()` of the
886
+ * target deletes every referencing record. Cycles are detected
887
+ * and broken via an in-progress set, so mutual cascades
888
+ * terminate instead of recursing forever.
889
+ *
890
+ * Cross-vault refs are explicitly rejected: if the target
891
+ * name contains a `/`, `ref()` throws `RefScopeError`. Cross-
892
+ * vault refs need an auth story (multi-keyring reads) that
893
+ * doesn't ship — tracked for.
894
+ */
895
+
896
+ /** The three enforcement modes. Default for new refs is `'strict'`. */
897
+ type RefMode = 'strict' | 'warn' | 'cascade';
898
+ /**
899
+ * Descriptor returned by `ref()`. Collections accept a
900
+ * `Record<string, RefDescriptor>` in their options. The key is the
901
+ * field name on the record (top-level only — dotted paths are out of
902
+ * scope), the value describes which target collection the
903
+ * field references and under what mode.
904
+ *
905
+ * The descriptor carries only plain data so it can be serialized,
906
+ * passed around, and introspected without any class machinery.
907
+ */
908
+ interface RefDescriptor {
909
+ readonly target: string;
910
+ readonly mode: RefMode;
911
+ }
912
+ /**
913
+ * Thrown when a strict reference is violated — either `put()` with a
914
+ * missing target id, or `delete()` of a target that still has
915
+ * strict-referencing records.
916
+ *
917
+ * Carries structured detail so UI code (and a potential future
918
+ * devtools panel) can render "client X cannot be deleted because
919
+ * invoices 1, 2, and 3 reference it" instead of a bare error string.
920
+ */
921
+ declare class RefIntegrityError extends NoydbError {
922
+ readonly collection: string;
923
+ readonly id: string;
924
+ readonly field: string;
925
+ readonly refTo: string;
926
+ readonly refId: string | null;
927
+ constructor(opts: {
928
+ collection: string;
929
+ id: string;
930
+ field: string;
931
+ refTo: string;
932
+ refId: string | null;
933
+ message: string;
934
+ });
935
+ }
936
+ /**
937
+ * Thrown when `ref()` is called with a target name that looks like
938
+ * a cross-vault reference (contains a `/`). Separate error
939
+ * class because the fix is different: RefIntegrityError means "data
940
+ * is wrong"; RefScopeError means "the ref declaration is wrong".
941
+ */
942
+ declare class RefScopeError extends NoydbError {
943
+ constructor(target: string);
944
+ }
945
+ /**
946
+ * Helper constructor. Thin wrapper around the object literal so user
947
+ * code reads like `ref('clients')` instead of `{ target: 'clients',
948
+ * mode: 'strict' }` — this is the only ergonomics reason it exists.
949
+ *
950
+ * Validates the target name eagerly so a misconfigured ref declaration
951
+ * fails at collection construction time, not at the first put.
952
+ */
953
+ declare function ref(target: string, mode?: RefMode): RefDescriptor;
954
+ /**
955
+ * Per-vault registry of reference declarations.
956
+ *
957
+ * The registry is populated by `Collection` constructors (which pass
958
+ * their `refs` option through the Vault) and consulted by the
959
+ * Vault on every `put` / `delete` and by `checkIntegrity`. A
960
+ * single instance lives on the Vault for its lifetime; there's
961
+ * no global state.
962
+ *
963
+ * The data structure is two parallel maps:
964
+ *
965
+ * - `outbound`: `collection → { field → RefDescriptor }` — what
966
+ * refs does `collection` declare? Used on put to check
967
+ * strict-target-exists and on checkIntegrity to walk each
968
+ * collection's outbound refs.
969
+ *
970
+ * - `inbound`: `target → Array<{ collection, field, mode }>` —
971
+ * which collections reference `target`? Used on delete to find
972
+ * the records that might be affected by cascade / strict.
973
+ *
974
+ * The two views are kept in sync by `register()` and never mutated
975
+ * otherwise — refs can't be unregistered at runtime in.
976
+ */
977
+ declare class RefRegistry {
978
+ private readonly outbound;
979
+ private readonly inbound;
980
+ /**
981
+ * Register the refs declared by a single collection. Idempotent in
982
+ * the happy path — calling twice with the same data is a no-op.
983
+ * Calling twice with DIFFERENT data throws, because silent
984
+ * overrides would be confusing ("I changed the ref and it doesn't
985
+ * update" vs "I declared the same collection twice with different
986
+ * refs and the second call won").
987
+ */
988
+ register(collection: string, refs: Record<string, RefDescriptor>): void;
989
+ /** Get the outbound refs declared by a collection (or `{}` if none). */
990
+ getOutbound(collection: string): Record<string, RefDescriptor>;
991
+ /** Get the inbound refs that target a given collection (or `[]`). */
992
+ getInbound(target: string): ReadonlyArray<{
993
+ collection: string;
994
+ field: string;
995
+ mode: RefMode;
996
+ }>;
997
+ /**
998
+ * Iterate every (collection → refs) pair that has at least one
999
+ * declared reference. Used by `checkIntegrity` to walk the full
1000
+ * universe of outbound refs without needing to track collection
1001
+ * names elsewhere.
1002
+ */
1003
+ entries(): Array<[string, Record<string, RefDescriptor>]>;
1004
+ /** Clear the registry. Test-only escape hatch; never called from production code. */
1005
+ clear(): void;
1006
+ }
1007
+ /**
1008
+ * Shape of a single violation reported by `vault.checkIntegrity()`.
1009
+ *
1010
+ * `refId` is the value we saw in the referencing field — it's the
1011
+ * ID we expected to find in `refTo`, but didn't. Left as `unknown`
1012
+ * because records are loosely typed at the integrity-check layer.
1013
+ */
1014
+ interface RefViolation {
1015
+ readonly collection: string;
1016
+ readonly id: string;
1017
+ readonly field: string;
1018
+ readonly refTo: string;
1019
+ readonly refId: unknown;
1020
+ readonly mode: RefMode;
1021
+ }
1022
+
1023
+ /**
1024
+ * Query DSL `.join()` — eager, single-FK, intra-vault joins.
1025
+ *
1026
+ * resolves a ref()-declared foreign key into an attached
1027
+ * right-side record under an alias, using one of two planner paths
1028
+ * selected automatically:
1029
+ *
1030
+ * - **nested-loop** — right-side source exposes `lookupById`, so
1031
+ * each left row costs O(1). This is the common path for joins
1032
+ * against a Collection, which backs `lookupById` with a Map
1033
+ * lookup.
1034
+ * - **hash** — right-side has only `snapshot()`. Build a
1035
+ * `Map<id, record>` once, probe per left row. Same asymptotic
1036
+ * cost for our collections, but the path exists as a fallback
1037
+ * for custom QuerySource implementations and as an explicit
1038
+ * test-only override via `{ strategy: 'hash' }`.
1039
+ *
1040
+ * Scope:
1041
+ *
1042
+ * - Equi-joins on declared `ref()` fields only. Joins on
1043
+ * undeclared fields throw at plan time with an actionable error
1044
+ * naming the field and collection.
1045
+ * - Same-vault only. Cross-vault correlation goes
1046
+ * through `queryAcross`; this is an architectural
1047
+ * invariant, not a limitation we plan to lift.
1048
+ * - Hard row ceiling via `JoinTooLargeError` — default 50k per
1049
+ * side, override via `{ maxRows }`. Warns at 80% of the ceiling
1050
+ * on the existing warn channel.
1051
+ * - Three ref-mode behaviors on dangling refs:
1052
+ * strict → `DanglingReferenceError`,
1053
+ * warn → attach `null` with a one-shot warning,
1054
+ * cascade → attach `null` silently (cascade is a delete-time
1055
+ * mode; any dangling refs still present at read time are
1056
+ * mid-flight cascades or orphans from earlier, not a DSL error).
1057
+ *
1058
+ * Partition-awareness seam:
1059
+ *
1060
+ * Every `JoinLeg` carries a `partitionScope` field that is always
1061
+ * `'all'` in. The executor never reads this field.
1062
+ * partition-aware joins will start populating it from `where()`
1063
+ * predicates on the partition key without changing the planner's
1064
+ * external shape — this is the whole reason it exists now.
1065
+ *
1066
+ * Joins stay OUT of the ledger: reads don't touch `_ledger/`,
1067
+ * including joined reads.
1068
+ */
1069
+
1070
+ /** Planner strategy for a single join leg. Auto-selected unless overridden. */
1071
+ type JoinStrategy = 'hash' | 'nested';
1072
+ /** Default per-side row ceiling before `.join()` throws `JoinTooLargeError`. */
1073
+ declare const DEFAULT_JOIN_MAX_ROWS = 50000;
1074
+ /**
1075
+ * Internal representation of a single join leg in the query plan.
1076
+ *
1077
+ * This is the primary place where constraint #1 is honored:
1078
+ * every leg carries a `partitionScope` field that is always `'all'`
1079
+ * in and is never read by the executor. partition-aware
1080
+ * joins will start populating it from `where()` predicates on the
1081
+ * partition key without changing the planner's external shape.
1082
+ */
1083
+ interface JoinLeg {
1084
+ /** Field on the left-side record holding the foreign key value. */
1085
+ readonly field: string;
1086
+ /** Alias key under which the joined right-side record attaches. */
1087
+ readonly as: string;
1088
+ /** Target collection name, resolved from the `ref()` declaration. */
1089
+ readonly target: string;
1090
+ /** Ref mode controlling behavior on dangling refs at read time. */
1091
+ readonly mode: RefMode;
1092
+ /** Manual planner strategy override. `undefined` → auto-select. */
1093
+ readonly strategy: JoinStrategy | undefined;
1094
+ /** Per-side row ceiling override. `undefined` → DEFAULT_JOIN_MAX_ROWS. */
1095
+ readonly maxRows: number | undefined;
1096
+ /**
1097
+ * Partition scope for future partition-aware joins. Always `'all'`
1098
+ * today — the executor never reads this field. Future versions will
1099
+ * populate it from `where()` predicates without breaking the
1100
+ * planner's external shape. Do not remove even though it looks
1101
+ * unused today — that's the whole point of having it.
1102
+ */
1103
+ readonly partitionScope: 'all' | readonly string[];
1104
+ /**
1105
+ * When `true`, this is a dictionary join. The executor
1106
+ * resolves the left-field value against the dict snapshot and
1107
+ * attaches `{ ...labels, key }` rather than a right-side record.
1108
+ * `target` holds the dictionary name (not a collection name).
1109
+ */
1110
+ readonly isDictJoin?: true;
1111
+ }
1112
+ /**
1113
+ * Minimal shape of a joinable right-side record source.
1114
+ *
1115
+ * Collections implement this structurally via their `QuerySource`;
1116
+ * sources without `lookupById` force the hash-join fallback. Kept as
1117
+ * a thin interface so tests can wire up plain-object sources without
1118
+ * pulling in the full Collection class.
1119
+ *
1120
+ * The optional `subscribe` is used by `Query.live()` to merge
1121
+ * right-side change streams into the live re-run trigger. Sources
1122
+ * that omit `subscribe` still work for live joins — they just
1123
+ * don't drive re-fires when their right side mutates. Collection
1124
+ * implements `subscribe` by hooking into the existing per-
1125
+ * vault event emitter.
1126
+ */
1127
+ interface JoinableSource {
1128
+ snapshot(): readonly unknown[];
1129
+ lookupById?(id: string): unknown;
1130
+ /**
1131
+ * Subscribe to mutations on this source. The callback fires
1132
+ * AFTER the underlying record set has been updated. Returns an
1133
+ * unsubscribe function. Optional — sources without this method
1134
+ * cannot trigger live-join re-fires from their side.
1135
+ */
1136
+ subscribe?(cb: () => void): () => void;
1137
+ }
1138
+ /**
1139
+ * Join resolution context attached to a `Query` when it's constructed
1140
+ * from a `Collection`. Holds everything the `.join()` method needs to
1141
+ * translate a field name into a target collection + ref mode, and
1142
+ * everything the executor needs to read the right side.
1143
+ *
1144
+ * Kept as a structural interface so `Vault` can implement it
1145
+ * without `Query` needing to import `Vault` (circular-import
1146
+ * avoid). The Collection wires this up in its `query()` method using
1147
+ * the `joinResolver` back-reference the Vault passes in.
1148
+ */
1149
+ interface JoinContext {
1150
+ /** Name of the left-side (owning) collection. */
1151
+ readonly leftCollection: string;
1152
+ /** Look up a `RefDescriptor` by field name on the left collection. */
1153
+ resolveRef(field: string): RefDescriptor | null;
1154
+ /** Resolve a right-side source by target collection name. */
1155
+ resolveSource(collectionName: string): JoinableSource | null;
1156
+ /**
1157
+ * Resolve a dictKey join source. Returns a `JoinableSource`
1158
+ * whose snapshot exposes `{ key, ...labels }` records, keyed by the
1159
+ * stable dictionary key. `null` when the field is not a dictKey.
1160
+ *
1161
+ * The source is built from the compartment's in-memory dictionary
1162
+ * snapshot — same data as `DictionaryHandle.list()`, O(1) per lookup.
1163
+ */
1164
+ resolveDictSource?(field: string): JoinableSource | null;
1165
+ }
1166
+ /**
1167
+ * Apply every join leg in the plan against a base set of left-side
1168
+ * rows. Called by the query executor after `where` / `orderBy` /
1169
+ * `offset` / `limit` have narrowed the left set.
1170
+ *
1171
+ * Each leg attaches a `leg.as` field to every row. Returns a new
1172
+ * array of plain objects — the original left rows are not mutated
1173
+ * (structural sharing is fine for the inner fields, but the
1174
+ * top-level object is a fresh clone so consumers can further mutate
1175
+ * safely).
1176
+ *
1177
+ * **Ordering:** joins run AFTER orderBy / limit / offset in v1.
1178
+ * This keeps the planner simple and means queries like "top 10
1179
+ * invoices with client" sort and paginate the left side first, then
1180
+ * join. Sorting *by* a joined field is out of scope for — users
1181
+ * can post-sort the result array in userland or wait for
1182
+ * (multi-FK chaining) which can be layered on top.
1183
+ *
1184
+ * **Multi-FK chaining:** each leg's `maxRows` is enforced
1185
+ * against the current left-row count independently. Because
1186
+ * joins are equi-joins on the target's primary key (one-to-one or
1187
+ * one-to-null), the left row count is constant across legs — no
1188
+ * cartesian blowup. The per-leg left-side check is still necessary
1189
+ * so that a later leg with a tighter ceiling correctly fires on a
1190
+ * query like `.join('a', { maxRows: 100_000 }).join('b', { maxRows: 50 })`,
1191
+ * which should throw on the second leg if the left set exceeds 50.
1192
+ */
1193
+ declare function applyJoins(rows: readonly unknown[], joins: readonly JoinLeg[], context: JoinContext): unknown[];
1194
+ /**
1195
+ * Test-only: reset the join warning deduplication state between
1196
+ * tests. Production code never calls this — the dedup state is
1197
+ * intentionally process-scoped so a noisy query doesn't spam the
1198
+ * console once per component render.
1199
+ */
1200
+ declare function resetJoinWarnings(): void;
1201
+
1202
+ /**
1203
+ * Reactive query primitive — `query.live()`.
1204
+ *
1205
+ * produces a `LiveQuery<T>` that re-runs the query and
1206
+ * updates its `value` whenever any source feeding it (the left
1207
+ * collection AND every right-side collection a join leg points at)
1208
+ * mutates.
1209
+ *
1210
+ * Framework-agnostic by design. The Vue layer wraps a `LiveQuery`
1211
+ * in a Vue `Ref<T[]>` by subscribing once and copying `value` into
1212
+ * the ref on every notification. React/Solid/Svelte adapters do the
1213
+ * same with their own primitives. Core never depends on a UI
1214
+ * framework.
1215
+ *
1216
+ * **Error semantics.** A `.live()` query may throw at re-run time —
1217
+ * a strict-mode `DanglingReferenceError` is the most common case
1218
+ * (a right-side record was deleted out-of-band, leaving a left
1219
+ * row's FK pointing at nothing). When the re-run throws, the
1220
+ * `LiveQuery` catches the error and stores it in the `error`
1221
+ * field; it does NOT propagate the throw out of the source's
1222
+ * change handler, because doing so would tear down whatever
1223
+ * upstream emitter is dispatching. Listeners check `error` after
1224
+ * each notification and render an error state in the UI.
1225
+ *
1226
+ * **Dedup of right-side subscriptions.** A multi-FK chain that
1227
+ * joins the same target twice (e.g.
1228
+ * `.join('billingClientId').join('shippingClientId')`, both
1229
+ * pointing at `clients`) only subscribes to that target once. We
1230
+ * dedup by target collection name, on the assumption that
1231
+ * `resolveSource(name)` returns a single subscribable source per
1232
+ * vault + name. Vault's `resolveSource` reads from
1233
+ * `collectionCache` so this assumption holds.
1234
+ *
1235
+ * **What .live() does NOT do in v1:**
1236
+ * - No granular delta updates — the whole query re-runs on every
1237
+ * change. Granular delta tracking is a v2 optimization once
1238
+ * the API is stable.
1239
+ * - No batching of bursty changes — one event in, one re-run
1240
+ * out. Batching with microtask coalescing is a v2 enhancement.
1241
+ * - No async notifications — every notification is synchronous
1242
+ * within the source's change handler.
1243
+ * - No re-planning under live mutations — the planner picks once
1244
+ * at subscription time and reuses the same plan for every
1245
+ * re-run.
1246
+ */
1247
+ /**
1248
+ * The reactive primitive returned by `Query.live()`.
1249
+ *
1250
+ * Listeners can read the current `value` snapshot at any time and
1251
+ * subscribe to changes via `.subscribe(cb)`. The `error` field
1252
+ * carries the most recent re-run error, if any — read it after
1253
+ * each notification to render error state.
1254
+ *
1255
+ * Always call `stop()` when the live query is no longer needed.
1256
+ * Without it, the upstream change-stream subscriptions stay live
1257
+ * forever and the query keeps re-running on every mutation.
1258
+ */
1259
+ interface LiveQuery<T> {
1260
+ /**
1261
+ * Current snapshot of the query result. Updated in place on
1262
+ * every upstream change. The reference returned is the same
1263
+ * `readonly T[]` array — consumers that want change detection by
1264
+ * reference should copy: `const arr = [...live.value]`.
1265
+ */
1266
+ readonly value: readonly T[];
1267
+ /**
1268
+ * Most recent re-run error, or `null` on success. Set when the
1269
+ * executor throws (e.g. `DanglingReferenceError` in strict mode
1270
+ * after a right-side delete). Cleared on the next successful
1271
+ * re-run.
1272
+ */
1273
+ readonly error: Error | null;
1274
+ /**
1275
+ * Register a notification callback. Fires AFTER `value` and
1276
+ * `error` have been updated for a given upstream change.
1277
+ * Returns an unsubscribe function.
1278
+ *
1279
+ * The first call to `subscribe` does NOT fire the callback
1280
+ * immediately — call sites that want the initial value should
1281
+ * read `live.value` directly before subscribing.
1282
+ */
1283
+ subscribe(cb: () => void): () => void;
1284
+ /**
1285
+ * Tear down every upstream subscription and clear the listener
1286
+ * set. Idempotent — calling twice is safe. After `stop()`, the
1287
+ * query no longer re-runs and `subscribe()` becomes a no-op
1288
+ * (the returned unsubscribe is still callable and is also a
1289
+ * no-op).
1290
+ */
1291
+ stop(): void;
1292
+ }
1293
+ /**
1294
+ * Internal subscription handle for an upstream source — left or
1295
+ * right side. The contract is just `subscribe(cb): unsubscribe`,
1296
+ * matching the existing `QuerySource.subscribe` and the new
1297
+ * `JoinableSource.subscribe` (added in ).
1298
+ */
1299
+ interface LiveUpstream {
1300
+ subscribe(cb: () => void): () => void;
1301
+ }
1302
+ /**
1303
+ * Build a LiveQuery from a `recompute` callback (typically the
1304
+ * Query's bound `toArray`) and a list of upstream sources to
1305
+ * subscribe to.
1306
+ *
1307
+ * The recompute fires once synchronously to populate the initial
1308
+ * value, then re-fires every time any upstream notifies. Errors
1309
+ * thrown by recompute are caught and stored in `error` instead of
1310
+ * propagating — see the file docstring for the rationale.
1311
+ */
1312
+ declare function buildLiveQuery<T>(recompute: () => T[], upstreams: readonly LiveUpstream[]): LiveQuery<T>;
1313
+
1314
+ /**
1315
+ * Chainable, immutable query builder.
1316
+ *
1317
+ * Each builder operation returns a NEW Query — the underlying plan is never
1318
+ * mutated. This makes plans safe to share, cache, and serialize.
1319
+ */
1320
+
1321
+ interface OrderBy {
1322
+ readonly field: string;
1323
+ readonly direction: 'asc' | 'desc';
1324
+ }
1325
+ /**
1326
+ * A complete query plan: zero-or-more clauses, optional ordering, pagination,
1327
+ * and optional joins.
1328
+ *
1329
+ * Plans are JSON-serializable as long as no FilterClause is present and no
1330
+ * join leg carries a manual `strategy` override (JoinLeg itself is plain
1331
+ * data, so it serializes cleanly).
1332
+ *
1333
+ * Plans are intentionally NOT parametric on T — see `predicate.ts` FilterClause
1334
+ * for the variance reasoning. The public `Query<T>` API attaches the type tag.
1335
+ */
1336
+ interface QueryPlan {
1337
+ readonly clauses: readonly Clause[];
1338
+ readonly orderBy: readonly OrderBy[];
1339
+ readonly limit: number | undefined;
1340
+ readonly offset: number;
1341
+ /**
1342
+ * Zero-or-more join legs to apply after where/orderBy/limit/offset.
1343
+ * Each leg attaches a resolved right-side record (or null) under its
1344
+ * alias. See `query/join.ts` for the full semantics.
1345
+ */
1346
+ readonly joins: readonly JoinLeg[];
1347
+ }
1348
+ /**
1349
+ * Source of records that a query executes against.
1350
+ *
1351
+ * The interface is non-parametric to keep variance friendly: callers cast
1352
+ * their typed source (e.g. `QuerySource<Invoice>`) into this opaque shape.
1353
+ *
1354
+ * `getIndexes` and `lookupById` are optional fast-path hooks. When both are
1355
+ * present and a where clause matches an indexed field, the executor uses
1356
+ * the index to skip a linear scan. Sources without these methods (or with
1357
+ * `getIndexes` returning `null`) always fall back to a linear scan.
1358
+ */
1359
+ interface QuerySource<T> {
1360
+ /** Snapshot of all current records. The query never mutates this array. */
1361
+ snapshot(): readonly T[];
1362
+ /** Subscribe to mutations; returns an unsubscribe function. */
1363
+ subscribe?(cb: () => void): () => void;
1364
+ /** Index store for the indexed-fast-path. Optional. */
1365
+ getIndexes?(): CollectionIndexes | null;
1366
+ /** O(1) record lookup by id, used to materialize index hits. */
1367
+ lookupById?(id: string): T | undefined;
1368
+ }
1369
+ /**
1370
+ * The chainable builder. All methods return a new Query — the original
1371
+ * remains unchanged. Terminal methods (`toArray`, `first`, `count`,
1372
+ * `subscribe`) execute the plan against the source.
1373
+ *
1374
+ * Type parameter T flows through the public API for ergonomics, but the
1375
+ * internal storage uses `unknown` so Collection<T> stays covariant.
1376
+ *
1377
+ * The optional `joinContext` is attached when the Query is constructed
1378
+ * via `Collection.query()` (Collection passes in a context built from
1379
+ * the Vault's join resolver). A Query constructed via `new Query`
1380
+ * directly — e.g. from tests with a plain-object source — has no
1381
+ * joinContext, and calling `.join()` on it throws with an actionable
1382
+ * error. See `query/join.ts` for the full design.
1383
+ */
1384
+ declare class Query<T> {
1385
+ private readonly source;
1386
+ private readonly plan;
1387
+ private readonly joinContext;
1388
+ private readonly aggregateStrategy;
1389
+ constructor(source: QuerySource<T>, plan?: QueryPlan, joinContext?: JoinContext, aggregateStrategy?: AggregateStrategy);
1390
+ /** Add a field comparison. Multiple where() calls are AND-combined. */
1391
+ where(field: string, op: Operator, value: unknown): Query<T>;
1392
+ /**
1393
+ * Logical OR group. Pass a callback that builds a sub-query.
1394
+ * Each clause inside the callback is OR-combined; the group itself
1395
+ * joins the parent plan with AND.
1396
+ */
1397
+ or(builder: (q: Query<T>) => Query<T>): Query<T>;
1398
+ /**
1399
+ * Logical AND group. Same shape as `or()` but every clause inside the group
1400
+ * must match. Useful for explicit grouping inside a larger OR.
1401
+ */
1402
+ and(builder: (q: Query<T>) => Query<T>): Query<T>;
1403
+ /** Escape hatch: add an arbitrary predicate function. Not serializable. */
1404
+ filter(fn: (record: T) => boolean): Query<T>;
1405
+ /** Sort by a field. Subsequent calls are tie-breakers. */
1406
+ orderBy(field: string, direction?: 'asc' | 'desc'): Query<T>;
1407
+ /** Cap the result size. */
1408
+ limit(n: number): Query<T>;
1409
+ /** Skip the first N matching records (after ordering). */
1410
+ offset(n: number): Query<T>;
1411
+ /**
1412
+ * Resolve a `ref()`-declared foreign key and attach the right-side
1413
+ * record under `opts.as`. — eager, single-FK, intra-
1414
+ * vault joins.
1415
+ *
1416
+ * ```ts
1417
+ * const rows = invoices.query()
1418
+ * .where('status', '==', 'open')
1419
+ * .join('clientId', { as: 'client' })
1420
+ * .toArray()
1421
+ * // → [{ id, amount, client: { id, name, ... } }, ...]
1422
+ * ```
1423
+ *
1424
+ * Preconditions:
1425
+ * - The Query must have a `joinContext` (constructed via
1426
+ * `Collection.query()`, not `new Query`).
1427
+ * - `field` must have a matching `refs: { [field]: ref('<target>') }`
1428
+ * declaration on the left collection.
1429
+ * - The target collection must be reachable via the vault
1430
+ * (either currently open or openable on demand).
1431
+ *
1432
+ * Strategy:
1433
+ * - Nested-loop against `lookupById` when the target source
1434
+ * provides it (the common path for Collection targets).
1435
+ * - Hash join otherwise, or when `{ strategy: 'hash' }` is
1436
+ * explicitly passed for test purposes.
1437
+ *
1438
+ * Ref-mode semantics on dangling refs (left record has a non-null
1439
+ * FK value pointing at a right-side id that doesn't exist):
1440
+ * - `strict` → throws `DanglingReferenceError` with the full
1441
+ * field / target / refId context.
1442
+ * - `warn` → attaches `null` and emits a one-shot warning per
1443
+ * unique dangling pair.
1444
+ * - `cascade` → attaches `null` silently. Cascade is a
1445
+ * delete-time mode; dangling refs visible at read time are
1446
+ * either mid-flight cascades or pre-existing orphans, not a
1447
+ * DSL-level error.
1448
+ *
1449
+ * A left-side record whose FK field is `null` / `undefined` is NOT
1450
+ * a dangling ref — it's "no reference at all", always allowed
1451
+ * regardless of mode.
1452
+ *
1453
+ * The return type widens `T` with `Record<As, R | null>`. The `R`
1454
+ * parameter is optional — supply it explicitly for type-checked
1455
+ * access to the joined fields:
1456
+ *
1457
+ * ```ts
1458
+ * invoices.query().join<'client', Client>('clientId', { as: 'client' })
1459
+ * // ^^^^^^^^^^^^^^^^^^^ alias literal + right-side type
1460
+ * ```
1461
+ *
1462
+ * Without the generic, the joined field is typed as `unknown`, which
1463
+ * still works but requires a cast to access its properties.
1464
+ *
1465
+ * Joins stay intra-vault by construction — cross-vault
1466
+ * correlation goes through `Noydb.queryAcross`, not
1467
+ * `.join()`.
1468
+ */
1469
+ join<As extends string, R = unknown>(field: string, opts: {
1470
+ as: As;
1471
+ strategy?: JoinStrategy;
1472
+ maxRows?: number;
1473
+ }): Query<T & Record<As, R | null>>;
1474
+ /**
1475
+ * Execute the plan and return the matching records. When the plan
1476
+ * carries any join legs, they are applied after `where` / `orderBy`
1477
+ * / `limit` / `offset` narrow the left set. See the `.join()` doc
1478
+ * for the ordering rationale.
1479
+ */
1480
+ toArray(): T[];
1481
+ /** Return the first matching record, or null. Joins are applied. */
1482
+ first(): T | null;
1483
+ /**
1484
+ * Return the number of matching records (after where/filter,
1485
+ * before limit). **Joins are NOT applied** — count() reports the
1486
+ * left-side cardinality, because joins in are projection-only
1487
+ * (they attach an aliased field; they never filter). Running joins
1488
+ * here just to discard the aliases would be wasteful, and in strict
1489
+ * mode it could throw `DanglingReferenceError` for a call whose
1490
+ * intent is purely to count.
1491
+ */
1492
+ count(): number;
1493
+ /**
1494
+ * Reduce the matching records through a named set of reducers.
1495
+ * the aggregation terminal.
1496
+ *
1497
+ * ```ts
1498
+ * const { total, n, avgAmount } = invoices.query()
1499
+ * .where('status', '==', 'open')
1500
+ * .aggregate({
1501
+ * total: sum('amount'),
1502
+ * n: count(),
1503
+ * avgAmount: avg('amount'),
1504
+ * })
1505
+ * .run()
1506
+ * ```
1507
+ *
1508
+ * Returns an `Aggregation<R>` wrapper with two terminals:
1509
+ * - `.run(): R` — synchronous one-shot reduction
1510
+ * - `.live(): LiveAggregation<R>` — reactive primitive that
1511
+ * re-runs the reduction whenever the source notifies of a
1512
+ * change. Always call `live.stop()` when finished.
1513
+ *
1514
+ * The reducer spec is bound here once and reused by both
1515
+ * terminals — this is why `.aggregate()` returns a wrapper instead
1516
+ * of being a direct terminal. Consumers who only need the static
1517
+ * value read `.run()`; consumers wiring a reactive UI read
1518
+ * `.live()`.
1519
+ *
1520
+ * Joins are intentionally NOT applied to aggregations in —
1521
+ * the same logic as `.count()`. Joins in are projection-only
1522
+ * (they attach an aliased field and never filter), so running
1523
+ * them just to throw the aliases away would be wasteful. If you
1524
+ * need a reducer that reads a joined field, open an issue —
1525
+ * aggregations-across-joins is explicitly out of scope for v1.
1526
+ *
1527
+ * Every reducer factory accepts an optional `{ seed }` parameter
1528
+ * that is plumbed through the protocol but unused by the
1529
+ * executor — that's constraint #2. When partition-aware
1530
+ * aggregation lands, the seed will carry running state across
1531
+ * partition boundaries without an API break.
1532
+ */
1533
+ aggregate<Spec extends AggregateSpec>(spec: Spec): Aggregation<AggregateResult<Spec>>;
1534
+ /**
1535
+ * Partition matching records into buckets keyed by a field, then
1536
+ * terminate with `.aggregate(spec)` to compute per-bucket
1537
+ * reducers..
1538
+ *
1539
+ * ```ts
1540
+ * const byClient = invoices.query()
1541
+ * .where('status', '==', 'open')
1542
+ * .groupBy('clientId')
1543
+ * .aggregate({ total: sum('amount'), n: count() })
1544
+ * .run()
1545
+ * // → [ { clientId: 'c1', total: 5250, n: 3 }, … ]
1546
+ * ```
1547
+ *
1548
+ * Result rows carry the group key value under the grouping field
1549
+ * name plus every reducer output from the spec. Buckets are
1550
+ * emitted in first-seen order — consumers who want a specific
1551
+ * ordering should `.sort()` downstream.
1552
+ *
1553
+ * **Cardinality caps:** a one-shot warning fires at 10_000
1554
+ * distinct groups; `GroupCardinalityError` throws at 100_000.
1555
+ * Grouping on a high-uniqueness field like `id` or `createdAt` is
1556
+ * almost always a query mistake — the error message names the
1557
+ * field and observed cardinality and suggests narrowing with
1558
+ * `.where()` first.
1559
+ *
1560
+ * **Null / undefined keys:** records with a missing or explicitly
1561
+ * `null` group field get their own buckets. `Map`-based
1562
+ * partitioning distinguishes `undefined` from `null`, so the two
1563
+ * cases do NOT merge. Consumers who want them merged should
1564
+ * coalesce upstream with `.filter()`.
1565
+ *
1566
+ * **Joins are not applied** — same rationale as `.count()` and
1567
+ * `.aggregate()`. Joined fields in are projection-only, so
1568
+ * running a join inside a grouping pipeline would be wasteful and
1569
+ * could trigger `DanglingReferenceError` in strict mode for a
1570
+ * call whose intent is purely to bucket-and-reduce. Grouping by
1571
+ * a joined field is explicitly out of scope for — file an
1572
+ * issue if a real consumer needs it.
1573
+ *
1574
+ * **Filter clauses (`.filter(fn)`):** grouped queries still
1575
+ * support filter clauses in the underlying plan — they run in
1576
+ * the same candidate/filter pipeline that `.aggregate()` uses.
1577
+ * The performance caveat is the same: filter clauses cost O(N)
1578
+ * per record and can't be index-accelerated.
1579
+ */
1580
+ groupBy<F extends string>(field: F): GroupedQuery<T, F>;
1581
+ /**
1582
+ * Re-run the query whenever the source notifies of changes.
1583
+ * Returns an unsubscribe function. The callback receives the latest result.
1584
+ * Throws if the source does not support subscriptions.
1585
+ *
1586
+ * **For joined queries, prefer `.live()`** — `subscribe()`
1587
+ * only re-fires on LEFT-side changes, so joined data can be
1588
+ * stale if the right side mutates between emissions. `.live()`
1589
+ * merges change streams from every join target.
1590
+ */
1591
+ subscribe(cb: (result: T[]) => void): () => void;
1592
+ /**
1593
+ * Reactive terminal — returns a `LiveQuery<T>` that re-runs the
1594
+ * query and updates its `value` whenever any source feeding it
1595
+ * mutates..
1596
+ *
1597
+ * For non-joined queries, `.live()` is a convenience over the
1598
+ * existing `.subscribe()` callback shape: a hand-rolled reactive
1599
+ * primitive with `value` / `error` fields and a `subscribe(cb)`
1600
+ * notification channel. Frame-agnostic — Vue / React / Solid
1601
+ * adapters wrap it in their own primitive.
1602
+ *
1603
+ * For joined queries, `.live()` additionally subscribes to every
1604
+ * join target's change stream. Mutations on a right-side
1605
+ * collection (insert / update / delete of a client referenced by
1606
+ * an invoice) re-fire the live query and re-evaluate every
1607
+ * dependent left row. Right-side targets are deduped by
1608
+ * collection name, so a chain that joins the same target twice
1609
+ * (e.g. billing client + shipping client → both 'clients') only
1610
+ * subscribes once.
1611
+ *
1612
+ * **Ref-mode behavior on right-side disappearance** — matches the
1613
+ * eager `.toArray()` contract from :
1614
+ * - `strict` → re-run throws `DanglingReferenceError`. The
1615
+ * LiveQuery catches the throw, stores it in `live.error`, and
1616
+ * notifies listeners (the throw does NOT propagate out of
1617
+ * the source's change handler — that would tear down the
1618
+ * emitter). Consumers check `live.error` after each
1619
+ * notification and render an error state in the UI.
1620
+ * - `warn` → joined value flips to `null`; the existing
1621
+ * warn-channel deduplication keeps repeated re-runs from
1622
+ * spamming the console.
1623
+ * - `cascade` → no special handling needed; the cascade-
1624
+ * delete mechanism propagates the right-side delete into the
1625
+ * left collection on the next tick, and the live query
1626
+ * naturally re-fires with the orphaned left rows gone.
1627
+ *
1628
+ * Always call `live.stop()` when finished — it tears down every
1629
+ * upstream subscription. The Vue layer's `onUnmounted` hook
1630
+ * should call `stop()` automatically; raw consumers must do it
1631
+ * themselves.
1632
+ *
1633
+ * **Limitations:**
1634
+ * - No granular delta updates — the whole query re-runs on
1635
+ * every change.
1636
+ * - No microtask batching — bursty changes produce one re-run
1637
+ * per change.
1638
+ * - No re-planning under live mutations — the planner picks
1639
+ * once at subscription time and reuses the same plan.
1640
+ * - Streaming live joins are deferred.
1641
+ */
1642
+ live(): LiveQuery<T>;
1643
+ /**
1644
+ * Return the plan as a JSON-friendly object. FilterClause entries are
1645
+ * stripped (their `fn` cannot be serialized) and replaced with
1646
+ * { type: 'filter', fn: '[function]' } so devtools can still see them.
1647
+ */
1648
+ toPlan(): unknown;
1649
+ }
1650
+ /**
1651
+ * Execute a plan against a snapshot of records.
1652
+ * Pure function — same input, same output, no side effects.
1653
+ *
1654
+ * Records are typed as `unknown` because plans are non-parametric; callers
1655
+ * cast the return type at the API surface (see `Query.toArray()`).
1656
+ */
1657
+ declare function executePlan(records: readonly unknown[], plan: QueryPlan): unknown[];
1658
+
1659
+ /**
1660
+ * Streaming scan builder with filter + aggregate support.
1661
+ *
1662
+ * `Collection.scan()` now returns a `ScanBuilder<T>` that
1663
+ * implements `AsyncIterable<T>` (for existing `for await … of`
1664
+ * consumers) AND exposes chainable `.where()` / `.filter()` clauses
1665
+ * plus a `.aggregate(spec)` async terminal that reduces the scan
1666
+ * stream through the same reducer protocol as `Query.aggregate()`
1667
+ *.
1668
+ *
1669
+ * **Memory model:** O(reducers), not O(records). The aggregate
1670
+ * terminal initializes one state per reducer, iterates through the
1671
+ * scan one record at a time via `for await`, applies every reducer's
1672
+ * `step` per record, and never collects the stream into an array.
1673
+ * This is what makes `scan().aggregate()` suitable for collections
1674
+ * that don't fit in memory — the bound is a code-level invariant
1675
+ * visible in the function body, not a runtime assertion.
1676
+ *
1677
+ * **Paginated iteration:** the builder holds a `pageProvider`
1678
+ * closure that maps `(cursor, limit) → Promise<page>`, plumbed by
1679
+ * `Collection.scan()` to `collection.listPage(...)`. The page
1680
+ * iterator walks cursors forward until exhaustion, same as the
1681
+ * previous async-generator `scan()` did.
1682
+ *
1683
+ * **Backward compatibility:** existing `for await (const rec of
1684
+ * collection.scan()) { … }` code continues to work because
1685
+ * `ScanBuilder` implements `[Symbol.asyncIterator]`. The previous
1686
+ * signature returned an `AsyncIterableIterator<T>` (which has both
1687
+ * `[Symbol.asyncIterator]` and `.next()`). We verified at grep time
1688
+ * that no call sites use `.next()` on the scan result directly, so
1689
+ * the narrowed interface is safe.
1690
+ *
1691
+ * **Immutability:** each `.where()` / `.filter()` call returns a
1692
+ * fresh builder sharing the same page provider and page size. This
1693
+ * lets a base scan be reused for multiple parallel aggregations:
1694
+ *
1695
+ * ```ts
1696
+ * const scan = invoices.scan()
1697
+ * const [open, paid] = await Promise.all([
1698
+ * scan.where('status', '==', 'open').aggregate({ n: count() }),
1699
+ * scan.where('status', '==', 'paid').aggregate({ n: count() }),
1700
+ * ])
1701
+ * ```
1702
+ *
1703
+ * Note that each aggregation pays a full scan — there's no shared
1704
+ * iteration across the two. Multi-way aggregation in a single pass
1705
+ * is out of scope; consumers who need it should build a compound spec
1706
+ * and run a single `.aggregate({ openN, paidN })` at the DSL level.
1707
+ *
1708
+ * **Out of scope for (tracked separately):**
1709
+ * - `scan().aggregate().live()` — unbounded scan + change-stream
1710
+ * reconciliation is a design problem, not just a code one
1711
+ * - `scan().groupBy().aggregate()` — high-cardinality grouping on
1712
+ * huge collections would re-introduce the O(groups) memory
1713
+ * problem that aggregate fixes
1714
+ * - Parallel scan across pages — race-safe page cursor contracts
1715
+ * are not in the adapter API yet
1716
+ * - `scan().join(...)` — tracked under (streaming join)
1717
+ */
1718
+
1719
+ /**
1720
+ * Page provider — the Collection-shaped hook the builder calls to
1721
+ * walk cursors forward. Kept as a structural interface so tests can
1722
+ * wire up a synthetic provider without pulling in the full
1723
+ * Collection class. Collection's `listPage` matches this shape
1724
+ * exactly.
1725
+ */
1726
+ interface ScanPageProvider<T> {
1727
+ listPage(opts: {
1728
+ cursor?: string;
1729
+ limit?: number;
1730
+ }): Promise<{
1731
+ items: T[];
1732
+ nextCursor: string | null;
1733
+ }>;
1734
+ }
1735
+ /**
1736
+ * Chainable streaming scan. Implements `AsyncIterable<T>` for
1737
+ * drop-in use with `for await … of`; adds `.where()` / `.filter()`
1738
+ * chainable clauses and a `.aggregate(spec)` async terminal.
1739
+ *
1740
+ * The builder is immutable per operation — each chained call
1741
+ * returns a fresh `ScanBuilder` sharing the same page provider and
1742
+ * page size. The original builder is never mutated, so it's safe
1743
+ * to reuse across multiple parallel consumers.
1744
+ */
1745
+ declare class ScanBuilder<T> implements AsyncIterable<T> {
1746
+ private readonly pageProvider;
1747
+ private readonly pageSize;
1748
+ private readonly clauses;
1749
+ /**
1750
+ * Zero-or-more join legs to apply per record as the stream flows.
1751
+ * Each leg attaches the resolved right-side record (or null) under
1752
+ * its alias. — streaming joins.
1753
+ *
1754
+ * Joins are evaluated AFTER clauses, so a `where()` filtered-out
1755
+ * record never triggers a right-side lookup. This is the same
1756
+ * ordering as `Query.toArray()` (clauses first, joins after) and
1757
+ * keeps the streaming path from doing wasted work.
1758
+ */
1759
+ private readonly joins;
1760
+ /**
1761
+ * Join resolution context. Required for `.join()` to translate a
1762
+ * field name into a target collection + ref mode and to resolve
1763
+ * the right-side `JoinableSource`. Optional because tests
1764
+ * construct ScanBuilder directly with synthetic page providers
1765
+ * that don't know about ref() — calling `.join()` without a
1766
+ * context throws with an actionable error.
1767
+ */
1768
+ private readonly joinContext;
1769
+ constructor(pageProvider: ScanPageProvider<T>, pageSize?: number, clauses?: readonly Clause[], joins?: readonly JoinLeg[], joinContext?: JoinContext);
1770
+ /**
1771
+ * Add a field comparison. Runs per record as the scan stream
1772
+ * flows through, so non-matching records are dropped before they
1773
+ * reach `.aggregate()` or the iteration consumer. Multiple
1774
+ * `.where()` calls are AND-combined — same semantics as
1775
+ * `Query.where()`.
1776
+ *
1777
+ * Clauses cannot use the secondary-index fast path here because
1778
+ * the scan sources records from the adapter's paginator, not from
1779
+ * the in-memory cache where indexes live. Index-accelerated scans
1780
+ * are a future optimization — the current implementation
1781
+ * evaluates clauses per record in O(1) per clause.
1782
+ */
1783
+ where(field: string, op: Operator, value: unknown): ScanBuilder<T>;
1784
+ /**
1785
+ * Escape hatch: add an arbitrary predicate function. Same
1786
+ * non-serializable caveat as `Query.filter()` — filter clauses
1787
+ * don't round-trip through `toPlan()`. Prefer `.where()` when
1788
+ * possible.
1789
+ */
1790
+ filter(fn: (record: T) => boolean): ScanBuilder<T>;
1791
+ /**
1792
+ * Resolve a `ref()`-declared foreign key per record as the scan
1793
+ * stream flows, attaching the right-side record (or null) under
1794
+ * `opts.as`. — streaming joins over `scan()`.
1795
+ *
1796
+ * ```ts
1797
+ * for await (const inv of invoices.scan().join('clientId', { as: 'client' })) {
1798
+ * await processInvoice(inv) // inv.client is attached
1799
+ * }
1800
+ *
1801
+ * // Or terminate with .aggregate() for streaming joined aggregation
1802
+ * const { total } = await invoices.scan()
1803
+ * .where('status', '==', 'open')
1804
+ * .join('clientId', { as: 'client' })
1805
+ * .aggregate({ total: sum('amount') })
1806
+ * ```
1807
+ *
1808
+ * **The key difference from eager `.join()`:** the LEFT
1809
+ * side streams page-by-page from the adapter and is never
1810
+ * materialized. Memory ceiling on the left is O(pageSize), not
1811
+ * O(rowCount). This is what makes streaming joins suitable for
1812
+ * collections that exceed the eager join's 50_000-row ceiling.
1813
+ *
1814
+ * **Right-side strategy** is auto-selected per leg:
1815
+ * - **Indexed** — right source exposes `lookupById`, so each
1816
+ * left row costs O(1). This is the common path for
1817
+ * Collection right sides, which back `lookupById` with a Map
1818
+ * lookup over the in-memory cache. The right collection must
1819
+ * be in eager mode (the same constraint as eager join's
1820
+ * `querySourceForJoin` from ).
1821
+ * - **Hash** — right source has only `snapshot()`. Build a
1822
+ * `Map<id, record>` once at iteration start, probe per left
1823
+ * row. Same correctness, same per-row cost as the indexed
1824
+ * path; the difference is the upfront cost of materializing
1825
+ * the right side once.
1826
+ *
1827
+ * Both strategies hold the right side in memory for the duration
1828
+ * of the iteration. The "streaming" property applies to the LEFT
1829
+ * side only — true left-and-right streaming joins (where neither
1830
+ * side fits in memory) require a sort-merge join planner that's
1831
+ * out of scope for.
1832
+ *
1833
+ * **Ref-mode semantics** match eager `.join()` exactly:
1834
+ * - `strict` → throws `DanglingReferenceError` mid-stream
1835
+ * when a left record points at a non-existent right id.
1836
+ * The throw aborts the async iterator — consumers should
1837
+ * wrap the `for await` in try/catch if they want to recover.
1838
+ * - `warn` → attaches `null` and emits a one-shot warning
1839
+ * per unique dangling pair (deduped via the same warn
1840
+ * channel as eager join).
1841
+ * - `cascade` → attaches `null` silently. A delete-time mode;
1842
+ * dangling refs at read time are mid-flight or pre-existing
1843
+ * orphans, not a DSL error.
1844
+ *
1845
+ * Left records with null/undefined FK values attach `null`
1846
+ * regardless of mode — same "no reference at all" policy as
1847
+ * eager join and write-time `enforceRefsOnPut`.
1848
+ *
1849
+ * **Multi-FK chaining** is supported via repeated `.join()`
1850
+ * calls: each leg resolves an independent ref. Each leg
1851
+ * independently picks its right-side strategy and applies its
1852
+ * own ref mode.
1853
+ *
1854
+ * **Joins are NOT applied** to a `.aggregate()` terminal that
1855
+ * doesn't reference joined fields — wait, that's not quite
1856
+ * right. The streaming path actually DOES apply joins before
1857
+ * `.aggregate()` because the join attaches a field that the
1858
+ * spec might reference. Unlike `Query.aggregate()` (which skips
1859
+ * joins entirely as a projection-only short-circuit), the
1860
+ * streaming aggregation can't know whether the spec touches a
1861
+ * joined field, so it always applies joins. Consumers who want
1862
+ * unjoined streaming aggregation should leave `.join()` off the
1863
+ * chain — the chain is composable for a reason.
1864
+ *
1865
+ * constraint #1 — every JoinLeg carries `partitionScope:
1866
+ * 'all'` plumbed through but never read by. Same seam as
1867
+ * eager join.
1868
+ */
1869
+ join<As extends string, R = unknown>(field: string, opts: {
1870
+ as: As;
1871
+ }): ScanBuilder<T & Record<As, R | null>>;
1872
+ /**
1873
+ * Iterate the scan as an async iterable. Walks the page
1874
+ * provider's cursors forward until exhaustion, applying every
1875
+ * clause per record — only matching records are yielded.
1876
+ *
1877
+ * Backward-compatible with the previous async-generator `scan()`
1878
+ * return type for `for await … of` consumers.
1879
+ */
1880
+ [Symbol.asyncIterator](): AsyncIterator<T>;
1881
+ /**
1882
+ * Per-leg right-side resolution state. Built once at iteration
1883
+ * start and reused for every left record. Two strategies:
1884
+ *
1885
+ * - `lookupById`: present when the right source exposes the
1886
+ * hook directly (typical Collection right side). Per-row
1887
+ * cost is O(1).
1888
+ * - `hashByPrimaryKey`: built from `snapshot()` when no
1889
+ * lookupById. Per-row cost is O(1) after the upfront O(N)
1890
+ * materialization. Same as eager join's hash strategy.
1891
+ *
1892
+ * `warnedKeys` is the per-leg dedup set for ref-mode 'warn'. We
1893
+ * key on `field→target:refId` so the same dangling pair only
1894
+ * warns once per iteration. The dedup is per-iteration, not
1895
+ * per-process — a long-running scan that re-iterates would warn
1896
+ * again, which is the desired behavior (the data may have
1897
+ * changed between iterations).
1898
+ */
1899
+ private buildJoinResolvers;
1900
+ /**
1901
+ * Resolve a single join leg for one left record and return the
1902
+ * left record with the joined field attached under
1903
+ * `leg.as`. Pure function over `(left, resolver)`; never
1904
+ * mutates the input.
1905
+ *
1906
+ * Ref-mode dispatch matches eager `applyJoins` from :
1907
+ * - null/undefined FK → attach null silently (always allowed)
1908
+ * - dangling FK + strict → throw `DanglingReferenceError`
1909
+ * - dangling FK + warn → attach null, warn-once per pair
1910
+ * - dangling FK + cascade → attach null silently
1911
+ */
1912
+ private applyOneJoinStreaming;
1913
+ /**
1914
+ * Reduce the scan stream through a named set of reducers and
1915
+ * return the final aggregated shape.
1916
+ *
1917
+ * Memory is O(reducers): one mutable state slot per spec key.
1918
+ * Records flow through the pipeline one at a time via
1919
+ * `for await` and are discarded after their `step()` is applied
1920
+ * — never collected into an array. This is the distinguishing
1921
+ * property from `Query.aggregate()`, which materializes the full
1922
+ * match set first.
1923
+ *
1924
+ * Reuses the same reducer protocol as `Query.aggregate()`,
1925
+ * so `count()`, `sum(field)`, `avg(field)`, `min(field)`,
1926
+ * `max(field)` all work unchanged. The `{ seed }` parameter
1927
+ * plumbing from constraint #2 is honored transparently — the
1928
+ * factories ignore it in and the scan executor never
1929
+ * touches the per-reducer state construction.
1930
+ *
1931
+ * **Returns a Promise**, unlike `Query.aggregate().run()` which
1932
+ * is synchronous. The scan is inherently async because it walks
1933
+ * adapter pages, so the terminal has to be too. Consumers
1934
+ * destructure with await:
1935
+ *
1936
+ * ```ts
1937
+ * const { total, n } = await invoices.scan()
1938
+ * .where('year', '==', 2025)
1939
+ * .aggregate({ total: sum('amount'), n: count() })
1940
+ * ```
1941
+ *
1942
+ * **No `.live()` in.** `scan().aggregate().live()` would
1943
+ * require reconciling an unbounded streaming iteration with a
1944
+ * change-stream subscription — a design problem, not just a code
1945
+ * one. Consumers with huge collections and live needs should
1946
+ * narrow with `.where()` enough to fit in the 50k `query()`
1947
+ * limit and use `query().aggregate().live()` instead.
1948
+ */
1949
+ aggregate<Spec extends AggregateSpec>(spec: Spec): Promise<AggregateResult<Spec>>;
1950
+ /**
1951
+ * Evaluate the clause list against a single record. Linear in
1952
+ * the clause count; short-circuits on first false. Clauses on a
1953
+ * scan are always re-evaluated per record — no index-accelerated
1954
+ * path, because the stream sources records from the adapter
1955
+ * paginator, not from the in-memory cache where indexes live.
1956
+ */
1957
+ private recordMatches;
1958
+ }
1959
+
1960
+ export { RefIntegrityError as $, AlreadyElevatedError as A, BackupCorruptedError as B, ConflictError as C, DictKeyInUseError as D, ElevationExpiredError as E, FilenameSanitizationError as F, GroupCardinalityError as G, PermissionDeniedError as H, ImportCapabilityError as I, type JoinContext as J, KeyringCorruptError as K, LocaleNotSpecifiedError as L, MissingTranslationError as M, NoydbError as N, type OrderBy as O, PathEscapeError as P, PrivilegeEscalationError as Q, ReservedCollectionNameError as R, SessionExpiredError as S, TranslatorNotConfiguredError as T, Query as U, type QueryPlan as V, type QuerySource as W, ReadOnlyAtInstantError as X, ReadOnlyError as Y, ReadOnlyFrameError as Z, type RefDescriptor as _, DictKeyMissingError as a, type RefMode as a0, RefRegistry as a1, RefScopeError as a2, type RefViolation as a3, ScanBuilder as a4, type ScanPageProvider as a5, SchemaValidationError as a6, StoreCapabilityError as a7, TamperedError as a8, TierDemoteDeniedError as a9, TierNotGrantedError as aa, ValidationError as ab, applyJoins as ac, buildLiveQuery as ad, executePlan as ae, ref as af, resetJoinWarnings as ag, SessionNotFoundError as b, SessionPolicyError as c, BackupLedgerError as d, BundleIntegrityError as e, BundleVersionConflictError as f, DEFAULT_JOIN_MAX_ROWS as g, DanglingReferenceError as h, DecryptionError as i, DelegationTargetMissingError as j, ExportCapabilityError as k, IndexRequiredError as l, IndexWriteFailureError as m, InvalidKeyError as n, type JoinLeg as o, type JoinStrategy as p, JoinTooLargeError as q, type JoinableSource as r, KeyringExpiredError as s, LedgerContentionError as t, type LiveQuery as u, type LiveUpstream as v, NetworkError as w, NoAccessError as x, NotFoundError as y, PeriodClosedError as z };