@noy-db/hub 0.2.0-pre.10 → 0.2.0-pre.12

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 (266) hide show
  1. package/README.md +126 -0
  2. package/dist/aggregate/index.cjs +289 -12
  3. package/dist/aggregate/index.cjs.map +1 -1
  4. package/dist/aggregate/index.d.cts +2 -2
  5. package/dist/aggregate/index.d.ts +2 -2
  6. package/dist/aggregate/index.js +7 -7
  7. package/dist/aggregate/index.js.map +1 -1
  8. package/dist/attestation/index.cjs.map +1 -1
  9. package/dist/attestation/index.d.cts +3 -3
  10. package/dist/attestation/index.d.ts +3 -3
  11. package/dist/attestation/index.js +6 -6
  12. package/dist/blobs/index.cjs +28 -0
  13. package/dist/blobs/index.cjs.map +1 -1
  14. package/dist/blobs/index.d.cts +4 -4
  15. package/dist/blobs/index.d.ts +4 -4
  16. package/dist/blobs/index.js +5 -5
  17. package/dist/bundle/index.cjs +1468 -19
  18. package/dist/bundle/index.cjs.map +1 -1
  19. package/dist/bundle/index.d.cts +5 -5
  20. package/dist/bundle/index.d.ts +5 -5
  21. package/dist/bundle/index.js +9 -9
  22. package/dist/{chunk-7CEGU63S.js → chunk-4BHFNKTP.js} +2 -2
  23. package/dist/{chunk-5OEJ6GOT.js → chunk-5ARRXIVR.js} +2 -2
  24. package/dist/{chunk-DRXIZOFV.js → chunk-6AD5TBF2.js} +31 -3
  25. package/dist/chunk-6AD5TBF2.js.map +1 -0
  26. package/dist/{chunk-YM7LFCG7.js → chunk-6BYBVRZU.js} +3 -3
  27. package/dist/{chunk-5IXJGFF2.js → chunk-7JJE3OMJ.js} +5 -5
  28. package/dist/{chunk-HHOO7HGH.js → chunk-7LVRIW4G.js} +4 -4
  29. package/dist/{chunk-O6EJ6WTI.js → chunk-AGRC7NQQ.js} +62 -2
  30. package/dist/chunk-AGRC7NQQ.js.map +1 -0
  31. package/dist/{chunk-IMYKDWB4.js → chunk-B7GGYNKQ.js} +2 -2
  32. package/dist/{chunk-BDV7INMP.js → chunk-BXOUVUES.js} +4 -4
  33. package/dist/{chunk-FO3UEG4S.js → chunk-C2CIIQRG.js} +2 -2
  34. package/dist/{chunk-ZROPXHJY.js → chunk-CHBXWJZQ.js} +2 -2
  35. package/dist/{chunk-RYIL3PI2.js → chunk-CILT6V3V.js} +2 -2
  36. package/dist/{chunk-PXTQPZO4.js → chunk-DLTU4M2I.js} +6 -6
  37. package/dist/{chunk-GAUEWM7D.js → chunk-EKNUBIIQ.js} +4 -4
  38. package/dist/{chunk-HQSQC2XL.js → chunk-GFPR7VJS.js} +17 -4
  39. package/dist/chunk-GFPR7VJS.js.map +1 -0
  40. package/dist/{chunk-6EOXTJS2.js → chunk-HBAJDI2N.js} +5 -5
  41. package/dist/{chunk-PVUUIWHY.js → chunk-HLGDYFWR.js} +10 -3
  42. package/dist/chunk-HLGDYFWR.js.map +1 -0
  43. package/dist/{chunk-RRNA5GKT.js → chunk-IEPT7HVP.js} +2 -2
  44. package/dist/{chunk-R233SLY3.js → chunk-IUBHXEPJ.js} +2 -2
  45. package/dist/{chunk-CH22FZHT.js → chunk-L6BYRCYB.js} +2 -2
  46. package/dist/{chunk-5OX6XVNS.js → chunk-LOA2VCMS.js} +5 -5
  47. package/dist/{chunk-BB27JMWB.js → chunk-LSEW3ZZ2.js} +3 -3
  48. package/dist/{chunk-Y26YV5R3.js → chunk-LWSD4QPT.js} +3 -3
  49. package/dist/{chunk-WIRRPTFH.js → chunk-LYNNZEQD.js} +1 -1
  50. package/dist/chunk-LYNNZEQD.js.map +1 -0
  51. package/dist/{chunk-26NK23DZ.js → chunk-M45IRXDM.js} +3 -3
  52. package/dist/{chunk-CXJG63MA.js → chunk-NP6EZT44.js} +20 -6
  53. package/dist/chunk-NP6EZT44.js.map +1 -0
  54. package/dist/{chunk-GNHAC43Q.js → chunk-O53RIZCC.js} +5 -5
  55. package/dist/chunk-OPDTLHFA.js +783 -0
  56. package/dist/chunk-OPDTLHFA.js.map +1 -0
  57. package/dist/{chunk-LSTBFLL2.js → chunk-P3Z5Y2TS.js} +2 -2
  58. package/dist/{chunk-QSOYKKMD.js → chunk-P4EDT5ZP.js} +2 -2
  59. package/dist/{chunk-PC6ZEDRL.js → chunk-RHQYVHFH.js} +2 -2
  60. package/dist/{chunk-3LPV6BXR.js → chunk-RRDWXNBQ.js} +3 -3
  61. package/dist/{chunk-4CLICFEY.js → chunk-SJJQKNMP.js} +4 -4
  62. package/dist/{chunk-TY32C732.js → chunk-SZ4N3IL5.js} +5 -5
  63. package/dist/{chunk-4USCAEDT.js → chunk-TMHJEYW7.js} +502 -60
  64. package/dist/chunk-TMHJEYW7.js.map +1 -0
  65. package/dist/{chunk-2N62W5YP.js → chunk-UA6G45ME.js} +3 -3
  66. package/dist/{chunk-6YLPHBKR.js → chunk-UOC7JMZO.js} +13 -4
  67. package/dist/chunk-UOC7JMZO.js.map +1 -0
  68. package/dist/{chunk-DAP2XL7Q.js → chunk-VOXMU6LB.js} +2 -2
  69. package/dist/chunk-WNRGOVLG.js +64 -0
  70. package/dist/chunk-WNRGOVLG.js.map +1 -0
  71. package/dist/{chunk-DJRWA3Q5.js → chunk-WUG3E423.js} +4 -4
  72. package/dist/{chunk-PM3QYWUU.js → chunk-XHM2SARW.js} +3 -3
  73. package/dist/{chunk-RC6SU5NO.js → chunk-XSIFXX54.js} +2 -2
  74. package/dist/{chunk-CXFOITNS.js → chunk-ZC7MNVYN.js} +2 -2
  75. package/dist/{chunk-6T2UDBKG.js → chunk-ZCFS7U4J.js} +2 -2
  76. package/dist/consent/index.cjs.map +1 -1
  77. package/dist/consent/index.d.cts +4 -4
  78. package/dist/consent/index.d.ts +4 -4
  79. package/dist/consent/index.js +3 -3
  80. package/dist/{crypto-2CRLG4F4.js → crypto-AJB72OKN.js} +3 -3
  81. package/dist/{delegation-ZTRT2PRV.js → delegation-6FCWDRUS.js} +5 -5
  82. package/dist/derivations/index.cjs.map +1 -1
  83. package/dist/derivations/index.d.cts +5 -5
  84. package/dist/derivations/index.d.ts +5 -5
  85. package/dist/derivations/index.js +4 -4
  86. package/dist/{dev-unlock-BOEYl1xl.d.ts → dev-unlock-D3mpVFRc.d.ts} +1 -1
  87. package/dist/{dev-unlock-AglVnkPY.d.cts → dev-unlock-ckqa_Nso.d.cts} +1 -1
  88. package/dist/executor-7KSCEIFA.js +8 -0
  89. package/dist/executor-D2QMNGRJ.js +8 -0
  90. package/dist/executor-O5AZK7UW.js +11 -0
  91. package/dist/{fanout-sidecar-OKPMMPLG.js → fanout-sidecar-ZSKEQ6NI.js} +2 -2
  92. package/dist/guards/index.cjs +53 -1
  93. package/dist/guards/index.cjs.map +1 -1
  94. package/dist/guards/index.d.cts +12 -6
  95. package/dist/guards/index.d.ts +12 -6
  96. package/dist/guards/index.js +5 -3
  97. package/dist/{hash-B9m3_fhj.d.ts → hash-CTZVkXLx.d.ts} +1 -1
  98. package/dist/{hash-RVqz2zi8.d.cts → hash-rDSSd_oW.d.cts} +1 -1
  99. package/dist/history/index.cjs.map +1 -1
  100. package/dist/history/index.d.cts +5 -5
  101. package/dist/history/index.d.ts +5 -5
  102. package/dist/history/index.js +5 -5
  103. package/dist/i18n/index.cjs.map +1 -1
  104. package/dist/i18n/index.d.cts +4 -4
  105. package/dist/i18n/index.d.ts +4 -4
  106. package/dist/i18n/index.js +6 -6
  107. package/dist/immutable-guard-C51vAHuh.d.cts +67 -0
  108. package/dist/immutable-guard-DyD0qg2k.d.ts +67 -0
  109. package/dist/index-CkFHr4OP.d.ts +1190 -0
  110. package/dist/index-Cmop06zJ.d.cts +1190 -0
  111. package/dist/index.cjs +1636 -61
  112. package/dist/index.cjs.map +1 -1
  113. package/dist/index.d.cts +46 -13
  114. package/dist/index.d.ts +46 -13
  115. package/dist/index.js +76 -44
  116. package/dist/index.js.map +1 -1
  117. package/dist/indexing/index.cjs.map +1 -1
  118. package/dist/indexing/index.js +2 -2
  119. package/dist/issue-YIYG4OW5.js +12 -0
  120. package/dist/{ledger-O7FXOG3D.js → ledger-5JMVF7PY.js} +5 -5
  121. package/dist/materialized-views/index.cjs.map +1 -1
  122. package/dist/materialized-views/index.d.cts +5 -6
  123. package/dist/materialized-views/index.d.ts +5 -6
  124. package/dist/materialized-views/index.js +6 -6
  125. package/dist/noydb-D5SLAJ6V.js +34 -0
  126. package/dist/overlay-views/index.cjs.map +1 -1
  127. package/dist/overlay-views/index.d.cts +5 -5
  128. package/dist/overlay-views/index.d.ts +5 -5
  129. package/dist/overlay-views/index.js +4 -4
  130. package/dist/periods/index.cjs.map +1 -1
  131. package/dist/periods/index.d.cts +4 -4
  132. package/dist/periods/index.d.ts +4 -4
  133. package/dist/periods/index.js +5 -5
  134. package/dist/{public-envelope-HMYHZIRH.js → public-envelope-PFLZI5MO.js} +4 -4
  135. package/dist/query/index.cjs +293 -10
  136. package/dist/query/index.cjs.map +1 -1
  137. package/dist/query/index.d.cts +2 -2
  138. package/dist/query/index.d.ts +2 -2
  139. package/dist/query/index.js +4 -4
  140. package/dist/registry-BVQ5ITMF.js +8 -0
  141. package/dist/registry-JLP3QOLD.js +8 -0
  142. package/dist/{registry-ST2VNFZC.js → registry-NCY445U5.js} +3 -3
  143. package/dist/{revoke-S6JMSLUN.js → revoke-7RLGQWZ7.js} +6 -6
  144. package/dist/session/index.cjs.map +1 -1
  145. package/dist/session/index.d.cts +5 -5
  146. package/dist/session/index.d.ts +5 -5
  147. package/dist/session/index.js +3 -3
  148. package/dist/shadow/index.cjs.map +1 -1
  149. package/dist/shadow/index.d.cts +4 -4
  150. package/dist/shadow/index.d.ts +4 -4
  151. package/dist/shadow/index.js +2 -2
  152. package/dist/{signer-7NPTB3SQ.js → signer-6JF44I4A.js} +5 -5
  153. package/dist/snapshots/index.cjs.map +1 -1
  154. package/dist/snapshots/index.d.cts +4 -4
  155. package/dist/snapshots/index.d.ts +4 -4
  156. package/dist/snapshots/index.js +4 -4
  157. package/dist/{stale-VKXSXJF4.js → stale-UBLP3RJ3.js} +2 -2
  158. package/dist/store/index.cjs.map +1 -1
  159. package/dist/store/index.d.cts +4 -4
  160. package/dist/store/index.d.ts +4 -4
  161. package/dist/store/index.js +2 -2
  162. package/dist/strategy-rtpKDfTC.d.cts +2029 -0
  163. package/dist/strategy-rtpKDfTC.d.ts +2029 -0
  164. package/dist/sync/index.cjs.map +1 -1
  165. package/dist/sync/index.d.cts +3 -3
  166. package/dist/sync/index.d.ts +3 -3
  167. package/dist/sync/index.js +4 -4
  168. package/dist/team/index.cjs.map +1 -1
  169. package/dist/team/index.d.cts +4 -4
  170. package/dist/team/index.d.ts +4 -4
  171. package/dist/team/index.js +8 -8
  172. package/dist/tx/index.cjs +8 -1
  173. package/dist/tx/index.cjs.map +1 -1
  174. package/dist/tx/index.d.cts +4 -4
  175. package/dist/tx/index.d.ts +4 -4
  176. package/dist/tx/index.js +3 -3
  177. package/dist/{types-n2_IfwlQ.d.cts → types-BGwjsDef.d.cts} +520 -6
  178. package/dist/{types-CaNQm4i8.d.ts → types-DRdfwgTG.d.ts} +520 -6
  179. package/dist/{ulid-CLMjmyhG.d.cts → ulid-D4d0Xto3.d.cts} +1 -1
  180. package/dist/{ulid-B9SMWj5i.d.ts → ulid-DOTPZ5_h.d.ts} +1 -1
  181. package/dist/util/index.cjs.map +1 -1
  182. package/dist/util/index.js +1 -1
  183. package/dist/vault-group-Z4KB75ZH.js +450 -0
  184. package/dist/vault-group-Z4KB75ZH.js.map +1 -0
  185. package/dist/{with-derivation-CVIOPTUf.d.ts → with-derivation-B082Y_WQ.d.ts} +1 -1
  186. package/dist/{with-derivation-aKrtS7Jj.d.cts → with-derivation-CB1EdcFF.d.cts} +1 -1
  187. package/dist/{with-materialized-view-C1eA1_T_.d.cts → with-materialized-view-CzRg1Dpr.d.cts} +1 -1
  188. package/dist/{with-materialized-view-DaYaE8-Q.d.ts → with-materialized-view-Dw4SwjKl.d.ts} +1 -1
  189. package/dist/{with-overlayed-view-DleJfKcV.d.cts → with-overlayed-view-C9YFKXzn.d.cts} +1 -1
  190. package/dist/{with-overlayed-view-DQsh2p8H.d.ts → with-overlayed-view-CaCXeW26.d.ts} +1 -1
  191. package/package.json +3 -3
  192. package/dist/chunk-2LPPNWF6.js +0 -340
  193. package/dist/chunk-2LPPNWF6.js.map +0 -1
  194. package/dist/chunk-4USCAEDT.js.map +0 -1
  195. package/dist/chunk-6YLPHBKR.js.map +0 -1
  196. package/dist/chunk-C3WE6UJY.js +0 -19
  197. package/dist/chunk-C3WE6UJY.js.map +0 -1
  198. package/dist/chunk-CXJG63MA.js.map +0 -1
  199. package/dist/chunk-DRXIZOFV.js.map +0 -1
  200. package/dist/chunk-HQSQC2XL.js.map +0 -1
  201. package/dist/chunk-O6EJ6WTI.js.map +0 -1
  202. package/dist/chunk-PVUUIWHY.js.map +0 -1
  203. package/dist/chunk-WIRRPTFH.js.map +0 -1
  204. package/dist/executor-S76VN45G.js +0 -8
  205. package/dist/executor-UCXLIGLW.js +0 -11
  206. package/dist/executor-ZCNZJMGR.js +0 -8
  207. package/dist/index-B8bjExET.d.cts +0 -2434
  208. package/dist/index-DfUbNad8.d.ts +0 -2434
  209. package/dist/issue-3W6IVLKH.js +0 -12
  210. package/dist/noydb-YAZNH5TI.js +0 -34
  211. package/dist/registry-UFIK7CSR.js +0 -8
  212. package/dist/registry-ZGYYSM5I.js +0 -8
  213. package/dist/strategy-CT2LCKAX.d.cts +0 -613
  214. package/dist/strategy-CT2LCKAX.d.ts +0 -613
  215. package/dist/with-guard-DZQbPzoP.d.cts +0 -18
  216. package/dist/with-guard-DseETUrF.d.ts +0 -18
  217. /package/dist/{chunk-7CEGU63S.js.map → chunk-4BHFNKTP.js.map} +0 -0
  218. /package/dist/{chunk-5OEJ6GOT.js.map → chunk-5ARRXIVR.js.map} +0 -0
  219. /package/dist/{chunk-YM7LFCG7.js.map → chunk-6BYBVRZU.js.map} +0 -0
  220. /package/dist/{chunk-5IXJGFF2.js.map → chunk-7JJE3OMJ.js.map} +0 -0
  221. /package/dist/{chunk-HHOO7HGH.js.map → chunk-7LVRIW4G.js.map} +0 -0
  222. /package/dist/{chunk-IMYKDWB4.js.map → chunk-B7GGYNKQ.js.map} +0 -0
  223. /package/dist/{chunk-BDV7INMP.js.map → chunk-BXOUVUES.js.map} +0 -0
  224. /package/dist/{chunk-FO3UEG4S.js.map → chunk-C2CIIQRG.js.map} +0 -0
  225. /package/dist/{chunk-ZROPXHJY.js.map → chunk-CHBXWJZQ.js.map} +0 -0
  226. /package/dist/{chunk-RYIL3PI2.js.map → chunk-CILT6V3V.js.map} +0 -0
  227. /package/dist/{chunk-PXTQPZO4.js.map → chunk-DLTU4M2I.js.map} +0 -0
  228. /package/dist/{chunk-GAUEWM7D.js.map → chunk-EKNUBIIQ.js.map} +0 -0
  229. /package/dist/{chunk-6EOXTJS2.js.map → chunk-HBAJDI2N.js.map} +0 -0
  230. /package/dist/{chunk-RRNA5GKT.js.map → chunk-IEPT7HVP.js.map} +0 -0
  231. /package/dist/{chunk-R233SLY3.js.map → chunk-IUBHXEPJ.js.map} +0 -0
  232. /package/dist/{chunk-CH22FZHT.js.map → chunk-L6BYRCYB.js.map} +0 -0
  233. /package/dist/{chunk-5OX6XVNS.js.map → chunk-LOA2VCMS.js.map} +0 -0
  234. /package/dist/{chunk-BB27JMWB.js.map → chunk-LSEW3ZZ2.js.map} +0 -0
  235. /package/dist/{chunk-Y26YV5R3.js.map → chunk-LWSD4QPT.js.map} +0 -0
  236. /package/dist/{chunk-26NK23DZ.js.map → chunk-M45IRXDM.js.map} +0 -0
  237. /package/dist/{chunk-GNHAC43Q.js.map → chunk-O53RIZCC.js.map} +0 -0
  238. /package/dist/{chunk-LSTBFLL2.js.map → chunk-P3Z5Y2TS.js.map} +0 -0
  239. /package/dist/{chunk-QSOYKKMD.js.map → chunk-P4EDT5ZP.js.map} +0 -0
  240. /package/dist/{chunk-PC6ZEDRL.js.map → chunk-RHQYVHFH.js.map} +0 -0
  241. /package/dist/{chunk-3LPV6BXR.js.map → chunk-RRDWXNBQ.js.map} +0 -0
  242. /package/dist/{chunk-4CLICFEY.js.map → chunk-SJJQKNMP.js.map} +0 -0
  243. /package/dist/{chunk-TY32C732.js.map → chunk-SZ4N3IL5.js.map} +0 -0
  244. /package/dist/{chunk-2N62W5YP.js.map → chunk-UA6G45ME.js.map} +0 -0
  245. /package/dist/{chunk-DAP2XL7Q.js.map → chunk-VOXMU6LB.js.map} +0 -0
  246. /package/dist/{chunk-DJRWA3Q5.js.map → chunk-WUG3E423.js.map} +0 -0
  247. /package/dist/{chunk-PM3QYWUU.js.map → chunk-XHM2SARW.js.map} +0 -0
  248. /package/dist/{chunk-RC6SU5NO.js.map → chunk-XSIFXX54.js.map} +0 -0
  249. /package/dist/{chunk-CXFOITNS.js.map → chunk-ZC7MNVYN.js.map} +0 -0
  250. /package/dist/{chunk-6T2UDBKG.js.map → chunk-ZCFS7U4J.js.map} +0 -0
  251. /package/dist/{crypto-2CRLG4F4.js.map → crypto-AJB72OKN.js.map} +0 -0
  252. /package/dist/{delegation-ZTRT2PRV.js.map → delegation-6FCWDRUS.js.map} +0 -0
  253. /package/dist/{executor-S76VN45G.js.map → executor-7KSCEIFA.js.map} +0 -0
  254. /package/dist/{executor-UCXLIGLW.js.map → executor-D2QMNGRJ.js.map} +0 -0
  255. /package/dist/{executor-ZCNZJMGR.js.map → executor-O5AZK7UW.js.map} +0 -0
  256. /package/dist/{fanout-sidecar-OKPMMPLG.js.map → fanout-sidecar-ZSKEQ6NI.js.map} +0 -0
  257. /package/dist/{issue-3W6IVLKH.js.map → issue-YIYG4OW5.js.map} +0 -0
  258. /package/dist/{ledger-O7FXOG3D.js.map → ledger-5JMVF7PY.js.map} +0 -0
  259. /package/dist/{noydb-YAZNH5TI.js.map → noydb-D5SLAJ6V.js.map} +0 -0
  260. /package/dist/{public-envelope-HMYHZIRH.js.map → public-envelope-PFLZI5MO.js.map} +0 -0
  261. /package/dist/{registry-ST2VNFZC.js.map → registry-BVQ5ITMF.js.map} +0 -0
  262. /package/dist/{registry-UFIK7CSR.js.map → registry-JLP3QOLD.js.map} +0 -0
  263. /package/dist/{registry-ZGYYSM5I.js.map → registry-NCY445U5.js.map} +0 -0
  264. /package/dist/{revoke-S6JMSLUN.js.map → revoke-7RLGQWZ7.js.map} +0 -0
  265. /package/dist/{signer-7NPTB3SQ.js.map → signer-6JF44I4A.js.map} +0 -0
  266. /package/dist/{stale-VKXSXJF4.js.map → stale-UBLP3RJ3.js.map} +0 -0
@@ -1,2434 +0,0 @@
1
- import { C as CollectionIndexes, a as Clause, O as Operator } from './predicate-Bt5ft-9c.js';
2
- import { A as AggregateStrategy, b as AggregateSpec, c as Aggregation, a as AggregateResult, g as GroupedQuery, h as GroupedQueryN } from './strategy-CT2LCKAX.js';
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
- * │ ├─ CrossJoinTooLargeError — cross-join row ceiling exceeded
34
- * │ ├─ CrossJoinSourceUnknownError — target collection not in vault
35
- * │ ├─ DanglingReferenceError — strict ref() points at nothing
36
- * │ ├─ GroupCardinalityError — groupBy bucket cap exceeded
37
- * │ ├─ IndexRequiredError — lazy-mode query touches unindexed field
38
- * │ ├─ IndexWriteFailureError — index side-car put/delete failed post-main
39
- * │ ├─ UniqueConstraintError — duplicate value on unique index
40
- * │ └─ UnsupportedIndexOptionError — unique+lazy or unique+crdt at registration
41
- * ├─ i18n / Dictionary errors
42
- * │ ├─ ReservedCollectionNameError
43
- * │ ├─ DictKeyMissingError
44
- * │ ├─ DictKeyInUseError
45
- * │ ├─ MissingTranslationError
46
- * │ ├─ LocaleNotSpecifiedError
47
- * │ └─ TranslatorNotConfiguredError
48
- * ├─ Backup errors
49
- * │ ├─ BackupLedgerError — hash-chain verification failed
50
- * │ └─ BackupCorruptedError — envelope hash mismatch in dump
51
- * ├─ Bundle errors
52
- * │ └─ BundleIntegrityError — .noydb body sha256 mismatch
53
- * ├─ Session errors
54
- * │ ├─ SessionExpiredError
55
- * │ ├─ SessionNotFoundError
56
- * │ └─ SessionPolicyError
57
- * └─ Snapshot errors
58
- * └─ SnapshotNotFoundError — snapshot key absent from snapshot store
59
- * ```
60
- *
61
- * ## Catching all NOYDB errors
62
- *
63
- * ```ts
64
- * import { NoydbError, InvalidKeyError, ConflictError } from '@noy-db/hub'
65
- *
66
- * try {
67
- * await vault.unlock(passphrase)
68
- * } catch (e) {
69
- * if (e instanceof InvalidKeyError) { showBadPassphraseUI(); return }
70
- * if (e instanceof NoydbError) { logToSentry(e.code, e); return }
71
- * throw e // unexpected — re-throw
72
- * }
73
- * ```
74
- *
75
- * @module
76
- */
77
- /**
78
- * Base class for all NOYDB errors.
79
- *
80
- * Every error thrown by `@noy-db/hub` extends this class, so consumers can
81
- * catch all NOYDB errors in a single `catch (e) { if (e instanceof NoydbError) ... }`
82
- * block. The `code` field is a machine-readable string (e.g. `'DECRYPTION_FAILED'`)
83
- * suitable for `switch` statements and logging pipelines.
84
- */
85
- declare class NoydbError extends Error {
86
- /** Machine-readable error code. Stable across library versions. */
87
- readonly code: string;
88
- constructor(code: string, message: string);
89
- }
90
- /**
91
- * Thrown when AES-GCM decryption fails.
92
- *
93
- * The most common cause is a wrong passphrase or a corrupted ciphertext.
94
- * A `DecryptionError` at the wrong passphrase level is caught internally
95
- * and re-thrown as `InvalidKeyError` — so in practice this surfaces for
96
- * per-record corruption rather than authentication failures.
97
- */
98
- declare class DecryptionError extends NoydbError {
99
- constructor(message?: string);
100
- }
101
- /**
102
- * Thrown when GCM tag verification fails, indicating the ciphertext was
103
- * modified after encryption.
104
- *
105
- * AES-256-GCM is authenticated encryption — the tag over the ciphertext
106
- * is checked on every decrypt. If any byte was flipped (accidental
107
- * corruption or deliberate tampering), decryption throws this error.
108
- * Treat it as a security alert: the stored bytes are not what NOYDB wrote.
109
- */
110
- declare class TamperedError extends NoydbError {
111
- constructor(message?: string);
112
- }
113
- /**
114
- * Thrown when key unwrapping fails, typically because the passphrase is wrong
115
- * or the keyring file is corrupted.
116
- *
117
- * NOYDB uses AES-KW (RFC 3394) to wrap DEKs with the KEK. If AES-KW
118
- * unwrapping fails, it means either the KEK was derived from the wrong
119
- * passphrase (PBKDF2 with 600K iterations) or the keyring bytes are
120
- * corrupted. This is the error shown to the user on a failed unlock attempt.
121
- */
122
- declare class InvalidKeyError extends NoydbError {
123
- constructor(message?: string);
124
- }
125
- /**
126
- * Thrown when a keyring's wrapped-DEK set unwraps partially — at least
127
- * one DEK succeeds (proving the KEK is correct) but at least one fails.
128
- * The passphrase is right; the failed entries are corrupted.
129
- *
130
- * This is distinct from {@link InvalidKeyError} so that
131
- * `NoydbOptions.onInvalidKey: 'reset'` does NOT fire — resetting on
132
- * partial corruption would destroy the still-valid DEKs and the data
133
- * they protect, which is silent data loss in response to a feature
134
- * designed for stale-credential recovery.
135
- */
136
- declare class KeyringCorruptError extends NoydbError {
137
- readonly failedCollections: readonly string[];
138
- readonly intactCount: number;
139
- constructor(opts: {
140
- failedCollections: readonly string[];
141
- intactCount: number;
142
- message?: string;
143
- });
144
- }
145
- /**
146
- * Thrown when the authenticated user does not have a DEK for the requested
147
- * collection — i.e. the collection is not in their keyring at all.
148
- *
149
- * This is the "no key for this door" error. It is different from
150
- * `ReadOnlyError` (user has a key but it only grants ro) and from
151
- * `PermissionDeniedError` (user's role doesn't allow the operation).
152
- */
153
- declare class NoAccessError extends NoydbError {
154
- constructor(message?: string);
155
- }
156
- /**
157
- * Thrown when a user with read-only (`ro`) permission attempts a write
158
- * operation (`put` or `delete`) on a collection.
159
- *
160
- * The user has a DEK for the collection (they can decrypt and read), but
161
- * their keyring grants only `ro`. To fix: re-grant the user with `rw`
162
- * permission, or do not attempt writes as a viewer/client role.
163
- */
164
- declare class ReadOnlyError extends NoydbError {
165
- constructor(message?: string);
166
- }
167
- /**
168
- * Thrown when a write is attempted against a historical view produced
169
- * by `vault.at(timestamp)`. Time-machine views are read-only by
170
- * contract — mutating the past would require either the shadow-vault
171
- * mechanism or a ledger-history rewrite (which breaks
172
- * the tamper-evidence guarantee).
173
- *
174
- * Distinct from {@link ReadOnlyError} (keyring-level) and
175
- * {@link PermissionDeniedError} (role-level): this error is about the
176
- * *view* being historical, independent of the caller's permissions.
177
- */
178
- declare class ReadOnlyAtInstantError extends NoydbError {
179
- constructor(operation: string, timestamp: string);
180
- }
181
- /**
182
- * Thrown when a write is attempted against a shadow-vault frame
183
- * produced by `vault.frame()`. Frames are read-only by contract —
184
- * the use case is screen-sharing / demos / compliance review where
185
- * the operator wants to prevent accidental edits.
186
- *
187
- * Behavioural enforcement only — the underlying keyring still holds
188
- * write-capable DEKs. See {@link VaultFrame} for the full caveat.
189
- */
190
- declare class ReadOnlyFrameError extends NoydbError {
191
- constructor(operation: string);
192
- }
193
- /**
194
- * Thrown when the authenticated user's role does not permit the requested
195
- * operation — e.g. a `viewer` calling `grantAccess()`, or an `operator`
196
- * calling `rotateKeys()`.
197
- *
198
- * This is a role-level check (what the user's role allows), distinct from
199
- * `NoAccessError` (collection not in keyring) and `ReadOnlyError` (in
200
- * keyring, but write not allowed).
201
- */
202
- declare class PermissionDeniedError extends NoydbError {
203
- constructor(message?: string);
204
- }
205
- /**
206
- * Thrown when an `@noy-db/as-*` export is attempted without the
207
- * required capability bit on the invoking keyring.
208
- *
209
- * Two sub-cases discriminated by the `tier` field:
210
- *
211
- * - `tier: 'plaintext'` — a plaintext-tier export (`as-xlsx`,
212
- * `as-csv`, `as-blob`, `as-zip`, …) was attempted but the
213
- * keyring's `exportCapability.plaintext` does not include the
214
- * requested `format` (nor the `'*'` wildcard). Default for every
215
- * role is `plaintext: []` — the owner must positively grant.
216
- * - `tier: 'bundle'` — an encrypted `as-noydb` bundle export was
217
- * attempted but the keyring's `exportCapability.bundle` is
218
- * `false`. Default for `owner`/`admin` is `true`; for
219
- * `operator`/`viewer`/`client` it is `false`.
220
- *
221
- * Distinct from `PermissionDeniedError` (role-level check) and
222
- * `NoAccessError` (collection not readable). Surfaces separately so
223
- * UI layers can show a "request the export capability from your
224
- * admin" flow rather than a generic permission error.
225
- */
226
- declare class ExportCapabilityError extends NoydbError {
227
- readonly tier: 'plaintext' | 'bundle';
228
- readonly format?: string;
229
- readonly userId: string;
230
- constructor(opts: {
231
- tier: 'plaintext' | 'bundle';
232
- userId: string;
233
- format?: string;
234
- message?: string;
235
- });
236
- }
237
- /**
238
- * Thrown when a keyring file's `expires_at` cutoff has passed.
239
- * Surfaced by `loadKeyring` before any DEK unwrap is attempted —
240
- * past the cutoff the slot refuses to open even with the right
241
- * passphrase. Distinct from PBKDF2 / unwrap errors so consumer code
242
- * can show a precise "this bundle slot has expired" message instead
243
- * of the generic decryption-failure UX.
244
- *
245
- * Used predominantly on `BundleRecipient` slots produced by
246
- * `writeNoydbBundle({ recipients: [...] })` to time-box audit access.
247
- */
248
- declare class KeyringExpiredError extends NoydbError {
249
- readonly userId: string;
250
- readonly expiresAt: string;
251
- constructor(opts: {
252
- userId: string;
253
- expiresAt: string;
254
- });
255
- }
256
- /**
257
- * Thrown when an `@noy-db/as-*` import is attempted but the invoking
258
- * keyring lacks the required import-capability bit.
259
- *
260
- * - `tier: 'plaintext'` — a plaintext-tier import (`as-csv`, `as-json`,
261
- * `as-ndjson`, `as-zip`, …) was attempted but the keyring's
262
- * `importCapability.plaintext` does not include the requested
263
- * `format` (nor the `'*'` wildcard).
264
- * - `tier: 'bundle'` — a `.noydb` bundle import was attempted but the
265
- * keyring's `importCapability.bundle` is not `true`.
266
- *
267
- * Default for every role on every dimension is closed — owners and
268
- * admins must positively grant the capability. Distinct from
269
- * `PermissionDeniedError` and `NoAccessError` so UI layers can show a
270
- * specific "request the import capability" flow.
271
- */
272
- declare class ImportCapabilityError extends NoydbError {
273
- readonly tier: 'plaintext' | 'bundle';
274
- readonly format?: string;
275
- readonly userId: string;
276
- constructor(opts: {
277
- tier: 'plaintext' | 'bundle';
278
- userId: string;
279
- format?: string;
280
- message?: string;
281
- });
282
- }
283
- /**
284
- * Thrown when a grant would give the grantee a permission the grantor
285
- * does not themselves hold — the "admin cannot grant what admin cannot
286
- * do" rule from the admin-delegation work.
287
- *
288
- * Distinct from `PermissionDeniedError` so callers can tell the two
289
- * cases apart in logs and tests:
290
- *
291
- * - `PermissionDeniedError` — "you are not allowed to perform this
292
- * operation at all" (wrong role).
293
- * - `PrivilegeEscalationError` — "you are allowed to grant, but not
294
- * with these specific permissions" (widening attempt).
295
- *
296
- * Under the admin model the grantee of an admin-grants-admin call
297
- * inherits the caller's entire DEK set by construction, so this error
298
- * is structurally unreachable in typical flows. The check and error
299
- * class exist so that future per-collection admin scoping cannot
300
- * accidentally bypass the subset rule — the guard is already wired in.
301
- *
302
- * `offendingCollection` carries the first collection name that failed
303
- * the subset check, to make the violation actionable in error output.
304
- */
305
- /**
306
- * Thrown when a caller invokes an API that requires an optional
307
- * store capability the active store does not implement.
308
- *
309
- * Today the only call site is `Noydb.listAccessibleVaults()`,
310
- * which depends on the optional `NoydbStore.listVaults()`
311
- * method. The error message names the missing method and the calling
312
- * API so consumers know exactly which combination is unsupported,
313
- * and the `capability` field is machine-readable so library code can
314
- * pattern-match in catch blocks (e.g. fall back to a candidate-list
315
- * shape).
316
- *
317
- * The class lives in `errors.ts` rather than as a generic
318
- * `ValidationError` because the diagnostic shape is different: a
319
- * `ValidationError` says "the inputs you passed are wrong"; this
320
- * error says "the inputs are fine, but the store you wired up
321
- * doesn't support what you're asking for." Different fix, different
322
- * documentation.
323
- */
324
- declare class StoreCapabilityError extends NoydbError {
325
- /** The store method/capability that was missing. */
326
- readonly capability: string;
327
- constructor(capability: string, callerApi: string, storeName?: string);
328
- }
329
- declare class PrivilegeEscalationError extends NoydbError {
330
- readonly offendingCollection: string;
331
- constructor(offendingCollection: string, message?: string);
332
- }
333
- /**
334
- * Thrown by `Collection.put` / `.delete` when the target record's
335
- * envelope `_ts` falls within a closed accounting period.
336
- *
337
- * Distinct from `ReadOnlyError` (keyring-level), `ReadOnlyAtInstantError`
338
- * (historical view), and `ReadOnlyFrameError` (shadow vault): this
339
- * error is about the STORED RECORD being sealed by an operator call
340
- * to `vault.closePeriod()`, independent of caller permissions or
341
- * view type. The `periodName` and `endDate` fields name the sealing
342
- * period so audit UIs can surface a "this record is locked in
343
- * FY2026-Q1 (closed 2026-03-31)" message without parsing the error
344
- * string.
345
- *
346
- * To apply a correction after close, book a compensating entry in a
347
- * new period rather than unlocking the old one. Re-opening a closed
348
- * period is deliberately unsupported.
349
- */
350
- declare class PeriodClosedError extends NoydbError {
351
- readonly periodName: string;
352
- readonly endDate: string;
353
- readonly recordTs: string;
354
- constructor(periodName: string, endDate: string, recordTs: string);
355
- }
356
- /**
357
- * Thrown when a `put()` or `delete()` is rejected by a guard's `check`
358
- * function. The `reason` is the message the guard supplied — typically a
359
- * short business description (e.g. "invoice is issued"). The full
360
- * collection + id are surfaced so audit UIs can link back to the record.
361
- */
362
- declare class RecordLockedError extends NoydbError {
363
- readonly collection: string;
364
- readonly id: string;
365
- readonly reason: string;
366
- constructor(collection: string, id: string, reason: string);
367
- }
368
- /**
369
- * Thrown when a `put()` changes one or more fields that are frozen by a
370
- * `frozenFields` guard. The `fields` list contains the specific paths
371
- * that were detected as changed.
372
- */
373
- declare class FieldFrozenError extends NoydbError {
374
- readonly collection: string;
375
- readonly id: string;
376
- readonly fields: readonly string[];
377
- constructor(collection: string, id: string, fields: readonly string[]);
378
- }
379
- /**
380
- * Thrown by an amendment invariant when the proposed change-set violates
381
- * the declared business rule (e.g. disbursement total not preserved).
382
- * Triggers a full transaction rollback via the existing revert pass.
383
- */
384
- declare class InvariantError extends NoydbError {
385
- constructor(message: string);
386
- }
387
- /**
388
- * Thrown at `withTransactions({ amendment: true })` open if the caller's
389
- * role is not in the guard's allowed amendment roles. Fail-fast: thrown
390
- * before any writes are attempted.
391
- */
392
- declare class AmendmentForbiddenError extends NoydbError {
393
- readonly userId: string;
394
- readonly role: string;
395
- constructor(userId: string, role: string);
396
- }
397
- /**
398
- * Thrown by `listUsersWithEnvelopes` when the vault's user directory
399
- * has been disabled (via `db.setDirectoryEnabled(vault, false)`) and
400
- * the caller's role is neither `owner` nor `admin`. Owner/admin can
401
- * still enumerate users — the toggle is a UX privacy switch, not a
402
- * security boundary.
403
- *
404
- * Honest caveat: this is a UX flag, not a privacy guarantee. The
405
- * envelope ciphertext is still in the store, the keyring file is
406
- * still listed at `_keyring/*`, and anyone with direct store read
407
- * access can count keyrings without going through the hub. See
408
- * `docs/subsystems/user-envelope.md` → "Directory visibility".
409
- */
410
- declare class DirectoryDisabledError extends NoydbError {
411
- readonly vault: string;
412
- constructor(vault: string);
413
- }
414
- /**
415
- * Thrown when a user tries to act at a tier they are not cleared for.
416
- *
417
- * This is the umbrella error for tier write refusals:
418
- * - `put({ tier: N })` when the user's keyring lacks tier-N DEK.
419
- * - `elevate(id, N)` when the caller cannot reach tier N.
420
- *
421
- * Distinct from `TierAccessDeniedError` which covers *read* refusals on
422
- * the invisibility/ghost path.
423
- */
424
- declare class TierNotGrantedError extends NoydbError {
425
- readonly tier: number;
426
- readonly collection: string;
427
- constructor(collection: string, tier: number);
428
- }
429
- /**
430
- * Thrown when an elevated-handle operation runs after the elevation's
431
- * TTL expired. Reads continue at the original tier; only writes
432
- * through the scoped handle flip to throwing once expired.
433
- */
434
- declare class ElevationExpiredError extends NoydbError {
435
- readonly tier: number;
436
- readonly expiresAt: number;
437
- constructor(opts: {
438
- tier: number;
439
- expiresAt: number;
440
- });
441
- }
442
- /**
443
- * Thrown by `vault.elevate(...)` when an elevation is already active
444
- * on the vault. Adopters must `release()` the existing handle before
445
- * starting a new elevation.
446
- */
447
- declare class AlreadyElevatedError extends NoydbError {
448
- readonly activeTier: number;
449
- constructor(activeTier: number);
450
- }
451
- /**
452
- * Thrown when `demote()` is called by someone who is not the original
453
- * elevator and not an owner.
454
- */
455
- declare class TierDemoteDeniedError extends NoydbError {
456
- constructor(id: string, tier: number);
457
- }
458
- /**
459
- * Thrown when `db.delegate()` is called against a user that has no
460
- * keyring in the target vault — the delegation token cannot be
461
- * constructed without the target user's KEK wrap.
462
- */
463
- declare class DelegationTargetMissingError extends NoydbError {
464
- readonly toUser: string;
465
- constructor(toUser: string);
466
- }
467
- /**
468
- * Thrown when a `put()` detects an optimistic concurrency conflict.
469
- *
470
- * NOYDB uses version numbers (`_v`) for optimistic locking. If a `put()`
471
- * is called with `expectedVersion: N` but the stored record is at version
472
- * `M ≠ N`, the write is rejected and the caller must re-read, re-apply their
473
- * change, and retry. The `version` field carries the actual stored version
474
- * so callers can decide whether to retry or surface the conflict to the user.
475
- */
476
- declare class ConflictError extends NoydbError {
477
- /** The actual stored version at the time of conflict. */
478
- readonly version: number;
479
- constructor(version: number, message?: string);
480
- }
481
- /**
482
- * Thrown by `LedgerStore.append()` after exhausting its CAS retry
483
- * budget under multi-writer contention. Two browser tabs, a
484
- * web app + an offline mobile peer, or a server worker pool all
485
- * producing ledger entries against the same vault can race on the
486
- * "read head, write head+1" cycle; the optimistic-CAS retry loop
487
- * resolves the race for `casAtomic: true` stores, but pathological
488
- * contention (or a buggy peer) can still exhaust the budget. When
489
- * that happens, the chain is intact — the failed writer simply
490
- * couldn't claim a slot. Caller's choice whether to retry, queue,
491
- * or surface the failure to the user.
492
- */
493
- declare class LedgerContentionError extends NoydbError {
494
- readonly attempts: number;
495
- constructor(attempts: number);
496
- }
497
- /**
498
- * Thrown when a bundle push is rejected because the remote has been updated
499
- * since the local bundle was last pulled.
500
- *
501
- * Unlike `ConflictError` (per-record), this is a whole-bundle conflict —
502
- * the remote's bundle handle has changed. The caller must pull the new
503
- * bundle, merge, and re-push. `remoteVersion` is the handle of the newer
504
- * remote bundle for use in diagnostics.
505
- */
506
- declare class BundleVersionConflictError extends NoydbError {
507
- /** The bundle handle of the newer remote version that rejected the push. */
508
- readonly remoteVersion: string;
509
- constructor(remoteVersion: string, message?: string);
510
- }
511
- /**
512
- * Thrown when a sync operation (push or pull) fails due to a network error.
513
- *
514
- * NOYDB's offline-first design means network errors are expected during sync.
515
- * Callers should catch `NetworkError`, surface connectivity status in the UI,
516
- * and rely on the `SyncScheduler` to retry when connectivity is restored.
517
- */
518
- declare class NetworkError extends NoydbError {
519
- constructor(message?: string);
520
- }
521
- /**
522
- * Thrown when `collection.get(id)` is called with an ID that does not exist.
523
- *
524
- * NOYDB collections are memory-first, so this error is synchronous and cheap —
525
- * it does not make a network round-trip. Callers that expect the record to be
526
- * absent should use `collection.getOrNull(id)` instead.
527
- */
528
- declare class NotFoundError extends NoydbError {
529
- constructor(message?: string);
530
- }
531
- /**
532
- * Thrown when application-level validation fails before encryption.
533
- *
534
- * Distinct from `SchemaValidationError` (Standard Schema v1 validator)
535
- * and `MissingTranslationError` (i18nText). `ValidationError` is the
536
- * general-purpose validation base — use it for custom guards in `put()`
537
- * hooks or store middleware.
538
- */
539
- declare class ValidationError extends NoydbError {
540
- constructor(message?: string);
541
- }
542
- /**
543
- * Thrown when a Standard Schema v1 validator rejects a record on
544
- * `put()` (input validation) or on read (output validation). Carries
545
- * the raw issue list so callers can render field-level errors.
546
- *
547
- * `direction` distinguishes the two cases:
548
- * - `'input'`: the user passed bad data into `put()`. This is a
549
- * normal error case that application code should handle — typically
550
- * by showing validation messages in the UI.
551
- * - `'output'`: stored data does not match the current schema. This
552
- * indicates a schema drift (the schema was changed without
553
- * migrating the existing records) and should be treated as a bug
554
- * — the application should not swallow it silently.
555
- *
556
- * The `issues` type is deliberately `readonly unknown[]` on this class
557
- * so that `errors.ts` doesn't need to import from `schema.ts` (and
558
- * create a dependency cycle). Callers who know they're holding a
559
- * `SchemaValidationError` can cast to the more precise
560
- * `readonly StandardSchemaV1Issue[]` from `schema.ts`.
561
- */
562
- declare class SchemaValidationError extends NoydbError {
563
- readonly issues: readonly unknown[];
564
- readonly direction: 'input' | 'output';
565
- constructor(message: string, issues: readonly unknown[], direction: 'input' | 'output');
566
- }
567
- /** Base for schema-evolution strategy rejections. */
568
- declare class SchemaUpdateError extends NoydbError {
569
- constructor(code: string, message: string);
570
- }
571
- /** A non-additive schema change was rejected by the `additiveOnly()` strategy. */
572
- declare class NonAdditiveSchemaChangeError extends SchemaUpdateError {
573
- constructor(message: string);
574
- }
575
- /** A schema change was rejected by the `lockSchema()` strategy. */
576
- declare class SchemaLockedError extends SchemaUpdateError {
577
- constructor(message: string);
578
- }
579
- /** Write attempted while a schema cutover fence is up (draining/migrating, or this collection has a pending cutover). */
580
- declare class SchemaFenceError extends SchemaUpdateError {
581
- constructor(message: string);
582
- }
583
- /** Write attempted by a client whose generation snapshot is behind the live fence — reload required. */
584
- declare class MigrationRequiredError extends SchemaUpdateError {
585
- constructor(message: string);
586
- }
587
- /** A coordinated cutover timed out waiting for active clients to quiesce. */
588
- declare class QuiesceTimeoutError extends SchemaUpdateError {
589
- constructor(message: string);
590
- }
591
- /**
592
- * Thrown when `.groupBy().aggregate()` produces more than the hard
593
- * cardinality cap (default 100_000 groups)..
594
- *
595
- * The cap exists because `.groupBy()` materializes one bucket per
596
- * distinct key value in memory, and runaway cardinality — a groupBy
597
- * on a high-uniqueness field like `id` or `createdAt` — is almost
598
- * always a query mistake rather than legitimate use. A hard error is
599
- * better than silent OOM: the consumer sees an actionable message
600
- * naming the field and the observed cardinality, with guidance to
601
- * either narrow the query with `.where()` or accept the ceiling
602
- * override.
603
- *
604
- * A separate one-shot warning fires at 10% of the cap (10_000
605
- * groups) so consumers get a heads-up before the hard error — same
606
- * pattern as `JoinTooLargeError` and the `.join()` row ceiling.
607
- *
608
- * **Not overridable in.** The 100k cap is a fixed constant so
609
- * the failure mode is consistent across the codebase; a
610
- * `{ maxGroups }` override can be added later without a break if a
611
- * real consumer asks.
612
- */
613
- declare class GroupCardinalityError extends NoydbError {
614
- /** The field being grouped on. */
615
- readonly field: string;
616
- /** Observed number of distinct groups at the moment the cap tripped. */
617
- readonly cardinality: number;
618
- /** The cap that was exceeded. */
619
- readonly maxGroups: number;
620
- constructor(field: string, cardinality: number, maxGroups: number);
621
- }
622
- /**
623
- * Thrown in lazy mode when a `.query()` / `.where()` / `.orderBy()` clause
624
- * references a field that does not have a declared index.
625
- *
626
- * Lazy-mode queries only work when every touched field is indexed.
627
- * This is deliberate — silent scan-fallback would hide the performance
628
- * cliff that lazy-mode indexes exist to prevent.
629
- *
630
- * Payload:
631
- * - `collection` — name of the collection queried
632
- * - `touchedFields` — every field referenced by the query (filter + order)
633
- * - `missingFields` — subset of `touchedFields` that have no declared index
634
- */
635
- declare class IndexRequiredError extends NoydbError {
636
- readonly collection: string;
637
- readonly touchedFields: readonly string[];
638
- readonly missingFields: readonly string[];
639
- constructor(args: {
640
- collection: string;
641
- touchedFields: readonly string[];
642
- missingFields: readonly string[];
643
- });
644
- }
645
- /**
646
- * Thrown by `Collection.put()` when writing a record would violate a
647
- * unique-index constraint — the same field value (or composite field
648
- * tuple) is already held by a *different* record id in the collection.
649
- *
650
- * Properties:
651
- * - `collection` — name of the collection the write was targeting
652
- * - `recordId` — the id of the record being written (the would-be violator)
653
- * - `fields` — the constrained field(s), e.g. `['taxId']` or `['workerId','employerEntityId']`
654
- * - `conflictingId` — the id of the record already holding the value
655
- *
656
- * Null-distinct semantics: if any constrained field is `null`/`undefined`,
657
- * the row is exempt (the constraint does not fire). This matches standard
658
- * SQL NULL-distinct behavior.
659
- */
660
- declare class UniqueConstraintError extends NoydbError {
661
- readonly collection: string;
662
- readonly recordId: string;
663
- readonly fields: readonly string[];
664
- readonly conflictingId: string;
665
- constructor(collection: string, recordId: string, fields: readonly string[], conflictingId: string);
666
- }
667
- /**
668
- * Thrown at collection registration when an index option is declared that
669
- * is incompatible with the collection's operating mode.
670
- *
671
- * Currently covers two cases:
672
- * - `unique: true` on a lazy-mode (`prefetch: false`) collection — lazy mode
673
- * does not pre-load all records, so an in-memory uniqueness map cannot be
674
- * maintained reliably.
675
- * - `unique: true` on a CRDT collection (`crdt: 'lww-map' | 'rga' | 'yjs'`) —
676
- * CRDT put() short-circuits the unique-constraint check, so enforcement would
677
- * silently not fire.
678
- *
679
- * Both cases are caught eagerly at `vault.collection()` time so the developer
680
- * sees the incompatibility immediately rather than shipping silently-ignored
681
- * constraints.
682
- *
683
- * The `option` field names the incompatible option (`'unique'`) so catch blocks
684
- * can pattern-match without inspecting the error message.
685
- */
686
- declare class UnsupportedIndexOptionError extends NoydbError {
687
- readonly option: string;
688
- constructor(option: string, message: string);
689
- }
690
- /**
691
- * Thrown (or surfaced via the `index:write-partial` event) when one or more
692
- * per-indexed-field side-car writes fail after the main record write has
693
- * already succeeded.
694
- *
695
- * Not thrown out of `.put()` / `.delete()` directly — those succeed when the
696
- * main record succeeds. Instead, `IndexWriteFailureError` instances are collected
697
- * into the session-scoped reconcile queue and emitted on the Collection
698
- * emitter as `index:write-partial`.
699
- *
700
- * Payload:
701
- * - `recordId` — the id of the main record whose side-car writes failed
702
- * - `field` — the indexed field whose side-car write failed
703
- * - `op` — `'put'` or `'delete'`, indicating which mutation was in flight
704
- * - `cause` — the underlying error from the store
705
- */
706
- declare class IndexWriteFailureError extends NoydbError {
707
- readonly recordId: string;
708
- readonly field: string;
709
- readonly op: 'put' | 'delete';
710
- readonly cause: unknown;
711
- constructor(args: {
712
- recordId: string;
713
- field: string;
714
- op: 'put' | 'delete';
715
- cause: unknown;
716
- });
717
- }
718
- /**
719
- * Thrown by `readNoydbBundle()` when the body bytes don't match
720
- * the integrity hash declared in the bundle header — i.e. someone
721
- * modified the bytes between write and read.
722
- *
723
- * Distinct from a generic `Error` (which would be thrown for
724
- * format violations like a missing magic prefix or malformed
725
- * header JSON) so consumers can pattern-match the corruption case
726
- * and handle it differently from a producer bug. A
727
- * `BundleIntegrityError` indicates "the bytes you got are not
728
- * what was written"; a plain `Error` from `parsePrefixAndHeader`
729
- * indicates "what was written wasn't a valid bundle in the first
730
- * place."
731
- *
732
- * Also thrown when decompression fails after the integrity hash
733
- * passed — that's a producer bug (the wrong algorithm byte was
734
- * written) but it surfaces with the same error class because the
735
- * end result is "the body cannot be turned back into a dump."
736
- */
737
- declare class BundleIntegrityError extends NoydbError {
738
- constructor(message: string);
739
- }
740
- /**
741
- * Thrown by `readNoydbBundle` when the bundle carries
742
- * sealed per-user passphrases but no supplied `SealingKeyProvider`
743
- * has a `.id` (= `pid`) matching the sealed entry's `pid`.
744
- *
745
- * Carries the failing pid + the user id so the recipient can
746
- * surface an actionable prompt:
747
- *
748
- * ```
749
- * BundleSealMismatchError: bundle carries sealed passphrase for user "alice"
750
- * under provider "macos-keychain:com.acme.app/alice@acme.example",
751
- * but no registered provider matches that pid.
752
- * ```
753
- *
754
- * Three resolution paths the message names (per foundation §11.9.4):
755
- *
756
- * 1. Configure a provider matching the pid and retry import.
757
- * 2. Pass `attemptUnsealAcrossProviders: true` to try each
758
- * registered provider regardless of pid.
759
- * 3. Inspect without unsealing — pass no `sealingProviders` to
760
- * receive the sealed entries unmodified for offline analysis.
761
- */
762
- declare class BundleSealMismatchError extends NoydbError {
763
- readonly userId: string;
764
- readonly pid: string;
765
- constructor(userId: string, pid: string);
766
- }
767
- /**
768
- * Thrown when `vault.collection()` is called with a name that is
769
- * reserved for NOYDB internal use (any name starting with `_dict_`).
770
- *
771
- * Dictionary collections are accessed exclusively via
772
- * `vault.dictionary(name)` — attempting to open one as a regular
773
- * collection would bypass the dictionary invariants (ACL, rename
774
- * tracking, reserved-name policy).
775
- */
776
- declare class ReservedCollectionNameError extends NoydbError {
777
- /** The rejected collection name. */
778
- readonly collectionName: string;
779
- constructor(collectionName: string);
780
- }
781
- /**
782
- * Thrown by `DictionaryHandle.get()` and `DictionaryHandle.delete()` when
783
- * the requested key does not exist in the dictionary.
784
- *
785
- * Distinct from `NotFoundError` (which is for data records) so callers
786
- * can distinguish "data record missing" from "dictionary key missing"
787
- * without inspecting error messages.
788
- */
789
- declare class DictKeyMissingError extends NoydbError {
790
- /** The dictionary name. */
791
- readonly dictionaryName: string;
792
- /** The key that was not found. */
793
- readonly key: string;
794
- constructor(dictionaryName: string, key: string);
795
- }
796
- /**
797
- * Thrown by `DictionaryHandle.delete()` in strict mode when the key to
798
- * be deleted is still referenced by one or more records.
799
- *
800
- * The caller must either rename the key first (the only sanctioned
801
- * mass-mutation path) or pass `{ mode: 'warn' }` to skip the check
802
- * (development only).
803
- */
804
- declare class DictKeyInUseError extends NoydbError {
805
- /** The dictionary name. */
806
- readonly dictionaryName: string;
807
- /** The key that is still referenced. */
808
- readonly key: string;
809
- /** Name of the first collection found to reference this key. */
810
- readonly usedBy: string;
811
- /** Number of records in `usedBy` that reference this key. */
812
- readonly count: number;
813
- constructor(dictionaryName: string, key: string, usedBy: string, count: number);
814
- }
815
- /**
816
- * Thrown by `Collection.put()` when an `i18nText` field is missing one
817
- * or more required translations.
818
- *
819
- * The `missing` array names each locale code that was absent from the
820
- * field value. The `field` property names the field so callers can
821
- * render a field-level error message without parsing the string.
822
- */
823
- declare class MissingTranslationError extends NoydbError {
824
- /** The field name whose translation(s) are missing. */
825
- readonly field: string;
826
- /** Locale codes that were required but absent. */
827
- readonly missing: readonly string[];
828
- constructor(field: string, missing: readonly string[], message?: string);
829
- }
830
- /**
831
- * Thrown when reading an `i18nText` field without specifying a locale —
832
- * either at the call site (`get(id, { locale })`) or on the vault
833
- * (`openVault(name, { locale })`).
834
- *
835
- * Also thrown when `resolveI18nText()` exhausts the fallback chain and
836
- * no translation is available for the requested locale.
837
- *
838
- * The `field` property names the field that triggered the error so the
839
- * caller can surface it in the UI.
840
- */
841
- declare class LocaleNotSpecifiedError extends NoydbError {
842
- /** The field name that required a locale. */
843
- readonly field: string;
844
- constructor(field: string, message?: string);
845
- }
846
- /**
847
- * Thrown at write time when an `i18nText` slot's value contains
848
- * characters outside the script set allowed for that locale, and the
849
- * field's `onScriptViolation` policy is `'reject'` (the default).
850
- *
851
- * Distinct from {@link MissingTranslationError} (write-shape) and
852
- * {@link LocaleNotSpecifiedError} (read-hole) so callers can tell a
853
- * wrong-script value from a missing one.
854
- */
855
- declare class ScriptViolationError extends NoydbError {
856
- /** The field whose value violated its script constraint. */
857
- readonly field: string;
858
- /** The locale slot (e.g. `'en'`) that was checked. */
859
- readonly locale: string;
860
- /** The Unicode scripts allowed for this slot. */
861
- readonly expected: readonly string[];
862
- /** A short sample of the offending characters, for diagnostics. */
863
- readonly sample: string;
864
- constructor(field: string, locale: string, expected: readonly string[], sample: string, message?: string);
865
- }
866
- /**
867
- * Thrown when a collection has an `i18nText` field with
868
- * `autoTranslate: true` but no `plaintextTranslator` was configured
869
- * on `createNoydb()`.
870
- *
871
- * The error is raised at `put()` time (not at schema construction) so
872
- * the mis-configuration is surfaced by the first write rather than
873
- * silently at startup.
874
- */
875
- declare class TranslatorNotConfiguredError extends NoydbError {
876
- /** The field that requested auto-translation. */
877
- readonly field: string;
878
- /** The collection the put was targeting. */
879
- readonly collection: string;
880
- constructor(field: string, collection: string);
881
- }
882
- /**
883
- * Thrown when `Vault.load()` finds that a backup's hash chain
884
- * doesn't verify, or that its embedded `ledgerHead.hash` doesn't
885
- * match the chain head reconstructed from the loaded entries.
886
- *
887
- * Distinct from `BackupCorruptedError` so callers can choose to
888
- * recover from one but not the other (e.g., a corrupted JSON file is
889
- * unrecoverable; a chain mismatch might mean the backup is from an
890
- * incompatible noy-db version).
891
- */
892
- declare class BackupLedgerError extends NoydbError {
893
- /** First-broken-entry index, if known. */
894
- readonly divergedAt?: number;
895
- constructor(message: string, divergedAt?: number);
896
- }
897
- /**
898
- * Thrown when `Vault.load()` finds that the backup's data
899
- * collection content doesn't match the ledger's recorded
900
- * `payloadHash`es. This is the "envelope was tampered with after
901
- * dump" detection — the chain itself can be intact, but if any
902
- * encrypted record bytes were swapped, this check catches it.
903
- */
904
- declare class BackupCorruptedError extends NoydbError {
905
- /** The (collection, id) pair whose envelope failed the hash check. */
906
- readonly collection: string;
907
- readonly id: string;
908
- constructor(collection: string, id: string, message: string);
909
- }
910
- /**
911
- * Thrown by partition-extraction primitives when the
912
- * transitive-closure walk fails — e.g. the FK graph is deeper than
913
- * `maxDepth`, signalling a runaway or unexpectedly cyclic graph.
914
- */
915
- declare class PartitionExtractionError extends NoydbError {
916
- constructor(message: string);
917
- }
918
- /**
919
- * Thrown by `adoptPartition` when the transfer seal can't be
920
- * opened — a wrong/short transfer key (AES-GCM auth-tag failure) or a
921
- * malformed sealed payload.
922
- */
923
- declare class TransferSealError extends NoydbError {
924
- constructor(message: string);
925
- }
926
- /**
927
- * Thrown when an adoption-lifecycle precondition fails — re-adopting a
928
- * partition already consumed in this store, or owner-creation on a
929
- * vault that isn't in the adopted-unowned state.
930
- */
931
- declare class AdoptionStateError extends NoydbError {
932
- constructor(message: string);
933
- }
934
- /** Document-attestation failures: undeclared field-schema, non-owner issue, missing field, signer failure. */
935
- declare class AttestationError extends NoydbError {
936
- constructor(message: string);
937
- }
938
- /**
939
- * Thrown by `resolveSession()` when the session token's `expiresAt`
940
- * timestamp is in the past. The session key is also removed from the
941
- * in-memory store when this is thrown, so retrying with the same sessionId
942
- * will produce `SessionNotFoundError`.
943
- *
944
- * Separate from `SessionNotFoundError` so callers can distinguish between
945
- * "session is gone" (key store cleared, tab reloaded) and "session is
946
- * still in the store but has exceeded its lifetime" (idle timeout, absolute
947
- * timeout, policy-driven expiry). The remediation differs: expired sessions
948
- * should prompt a fresh unlock; not-found sessions may indicate a bug or a
949
- * cross-tab scenario where the session was never established.
950
- */
951
- declare class SessionExpiredError extends NoydbError {
952
- readonly sessionId: string;
953
- constructor(sessionId: string);
954
- }
955
- /**
956
- * Thrown by `resolveSession()` when the session key cannot be found in
957
- * the module-level store. This happens when:
958
- * - The session was explicitly revoked via `revokeSession()`.
959
- * - The JS context was reloaded (tab navigation, page refresh, worker restart).
960
- * - `Noydb.close()` was called (which calls `revokeAllSessions()`).
961
- * - The sessionId is wrong or was generated by a different JS context.
962
- *
963
- * The session token (if the caller holds it) is permanently useless after
964
- * this error — the key is gone and cannot be recovered.
965
- */
966
- declare class SessionNotFoundError extends NoydbError {
967
- readonly sessionId: string;
968
- constructor(sessionId: string);
969
- }
970
- /**
971
- * Thrown when a session policy blocks an operation — for example,
972
- * `requireReAuthFor: ['export']` is set and the caller attempts to
973
- * call `exportStream()` without re-authenticating for this session.
974
- *
975
- * The `operation` field names the specific operation that was blocked
976
- * (e.g. `'export'`, `'grant'`, `'rotate'`) so the caller can surface
977
- * a targeted prompt ("Please re-enter your passphrase to export data").
978
- */
979
- declare class SessionPolicyError extends NoydbError {
980
- readonly operation: string;
981
- constructor(operation: string, message?: string);
982
- }
983
- /**
984
- * Thrown when a `.join()` would exceed its configured row ceiling on
985
- * either side. The ceiling defaults to 50,000 per side and can be
986
- * overridden via the `{ maxRows }` option on `.join()`.
987
- *
988
- * Carries both row counts so the error message can show which side
989
- * tripped the limit (e.g. "left had 60,000 rows, right had 1,200,
990
- * max was 50,000"). The `side` field is machine-readable so test
991
- * code and devtools can match on it without regex-parsing the
992
- * message.
993
- *
994
- * The row ceiling exists because joins are bounded in-memory
995
- * operations over materialized record sets. Consumers whose
996
- * collections genuinely exceed the ceiling should track
997
- * (streaming joins over `scan()`) or filter the left side further
998
- * with `where()` / `limit()` before joining.
999
- */
1000
- declare class JoinTooLargeError extends NoydbError {
1001
- readonly leftRows: number;
1002
- readonly rightRows: number;
1003
- readonly maxRows: number;
1004
- readonly side: 'left' | 'right';
1005
- constructor(opts: {
1006
- leftRows: number;
1007
- rightRows: number;
1008
- maxRows: number;
1009
- side: 'left' | 'right';
1010
- message: string;
1011
- });
1012
- }
1013
- /**
1014
- * Thrown by `.crossJoin()` when the cumulative cartesian product (or lateral
1015
- * filtered count) exceeds the configured ceiling. Check before allocating.
1016
- * Mirrors the pattern of `JoinTooLargeError` and the `.join()` row ceiling.
1017
- *
1018
- * @see CrossJoinClause.maxRows — per-clause override
1019
- * @see DEFAULT_CROSS_JOIN_MAX_ROWS — package default (50_000)
1020
- */
1021
- declare class CrossJoinTooLargeError extends NoydbError {
1022
- readonly target: string;
1023
- readonly expected: number;
1024
- readonly limit: number;
1025
- constructor(opts: {
1026
- target: string;
1027
- expected: number;
1028
- limit: number;
1029
- });
1030
- }
1031
- /**
1032
- * Thrown at cross-join execution time when the target collection is not
1033
- * reachable from the current vault. The left collection is included in the
1034
- * message for context.
1035
- */
1036
- declare class CrossJoinSourceUnknownError extends NoydbError {
1037
- readonly target: string;
1038
- readonly leftCollection: string;
1039
- constructor(target: string, leftCollection: string);
1040
- }
1041
- /**
1042
- * Thrown by `.join()` in strict `ref()` mode when a left-side record
1043
- * points at a right-side id that does not exist in the target
1044
- * collection.
1045
- *
1046
- * Distinct from `RefIntegrityError` so test code can pattern-match
1047
- * on the *read-time* dangling case without catching *write-time*
1048
- * integrity violations. Both indicate "ref points at nothing" but
1049
- * happen at different lifecycle phases and deserve different
1050
- * remediation in documentation: a RefIntegrityError on `put()`
1051
- * means the input is invalid; a DanglingReferenceError on `.join()`
1052
- * means stored data has drifted and `vault.checkIntegrity()`
1053
- * is the right tool to find the full set of orphans.
1054
- */
1055
- declare class DanglingReferenceError extends NoydbError {
1056
- readonly field: string;
1057
- readonly target: string;
1058
- readonly refId: string;
1059
- constructor(opts: {
1060
- field: string;
1061
- target: string;
1062
- refId: string;
1063
- message: string;
1064
- });
1065
- }
1066
- /**
1067
- * Thrown by {@link sanitizeFilename} when an input filename cannot be
1068
- * made safe — NUL byte, empty after normalization, missing
1069
- * `opaqueId` for the opaque profile, `..` segment, or a `maxBytes`
1070
- * cap too small to hold a single code point.
1071
- */
1072
- declare class FilenameSanitizationError extends NoydbError {
1073
- constructor(message: string);
1074
- }
1075
- /**
1076
- * Thrown when a write target resolves OUTSIDE the requested
1077
- * directory after sanitization — the canonical Zip-Slip class. The
1078
- * sanitizer's job is to strip path-traversal segments; this error
1079
- * is the defense-in-depth fallback at the FS write site.
1080
- */
1081
- declare class PathEscapeError extends NoydbError {
1082
- readonly attempted: string;
1083
- readonly targetDir: string;
1084
- constructor(opts: {
1085
- attempted: string;
1086
- targetDir: string;
1087
- });
1088
- }
1089
- /**
1090
- * Thrown at vault open if the derivation graph contains a cycle.
1091
- * `path` is the offending chain (e.g. `['a', 'b', 'c', 'a']`).
1092
- */
1093
- declare class DerivationCycleError extends NoydbError {
1094
- readonly path: readonly string[];
1095
- constructor(path: readonly string[]);
1096
- }
1097
- /**
1098
- * Thrown when a cascade of source → output → source → … exceeds the
1099
- * configured `maxDepth` (default 5).
1100
- */
1101
- declare class DerivationDepthError extends NoydbError {
1102
- readonly limit: number;
1103
- readonly attempted: number;
1104
- constructor(limit: number, attempted: number);
1105
- }
1106
- /**
1107
- * Thrown at registration if a `withDerivation` strategy references an
1108
- * output `collection` that isn't otherwise declared (no schema, no use
1109
- * elsewhere). Surfacing this early catches typos in collection names.
1110
- */
1111
- declare class DerivationOutputUnknownError extends NoydbError {
1112
- readonly collection: string;
1113
- constructor(collection: string);
1114
- }
1115
- /**
1116
- * Thrown when the user's `derive` function returns a value that doesn't
1117
- * match the declared output spec (e.g. wrong shape, wrong key set).
1118
- */
1119
- declare class DerivationOutputShapeError extends NoydbError {
1120
- readonly outputKey: string;
1121
- constructor(outputKey: string, detail: string);
1122
- }
1123
- /**
1124
- * Thrown by array-shape derivations when the `derive` function
1125
- * returns more rows than the output's `maxFanout` cap. The cap exists
1126
- * to keep dispatch cost bounded — without it a single source-row
1127
- * update could fan out to thousands of derived rows, dominating the
1128
- * write path.
1129
- *
1130
- * Defaults to `maxFanout: 64`. Raise on the output spec for
1131
- * carry-forward expansion cases (e.g. monthly rows across multi-year
1132
- * contracts).
1133
- */
1134
- declare class DerivationCapExceededError extends NoydbError {
1135
- readonly outputKey: string;
1136
- readonly returned: number;
1137
- readonly maxFanout: number;
1138
- constructor(outputKey: string, returned: number, maxFanout: number);
1139
- }
1140
- /**
1141
- * Thrown at vault open if the materialized-view graph contains a
1142
- * cycle. `path` is the offending chain (e.g. `['a-mv', 'b-mv', 'a-mv']`).
1143
- * Detected by the same shared DFS that catches `DerivationCycleError`;
1144
- * surfaces with a distinct error type so consumers can disambiguate.
1145
- */
1146
- declare class MaterializedViewCycleError extends NoydbError {
1147
- readonly path: readonly string[];
1148
- constructor(path: readonly string[]);
1149
- }
1150
- /**
1151
- * Thrown at MV registration if the query references a source
1152
- * collection that isn't declared on the vault. Surfacing this early
1153
- * catches typos in collection names.
1154
- */
1155
- declare class MaterializedViewSourceUnknownError extends NoydbError {
1156
- readonly mvName: string;
1157
- readonly collection: string;
1158
- constructor(mvName: string, collection: string);
1159
- }
1160
- /**
1161
- * Thrown by the MV executor when a refresh produces more rows than
1162
- * the configured ceiling. Default ceiling is 100k rows; override
1163
- * per-MV via `maxRows`. Mirrors `JoinTooLargeError` /
1164
- * `GroupCardinalityError` from the query DSL — the explosion is
1165
- * detected BEFORE writes hit the store, so the source-write
1166
- * transaction can roll back cleanly via strict-mode.
1167
- */
1168
- declare class MaterializedViewTooLargeError extends NoydbError {
1169
- readonly mvName: string;
1170
- readonly expected: number;
1171
- readonly limit: number;
1172
- constructor(mvName: string, expected: number, limit: number);
1173
- }
1174
- /**
1175
- * Thrown by `withMaterializedView()` at registration time when the
1176
- * strategy is structurally malformed. Distinct from
1177
- * `MaterializedViewSourceUnknownError` (the source list is well-formed
1178
- * but names a collection the vault doesn't know) and
1179
- * `MaterializedViewCycleError` (the source graph has a cycle): this
1180
- * error fires before either check, at the moment the spec is being
1181
- * normalized.
1182
- *
1183
- * Today the trigger cases are all about the `query` / `unionSources`
1184
- * dichotomy:
1185
- * - both `query` and `unionSources` were set (mutually exclusive),
1186
- * - neither `query` nor `unionSources` was set,
1187
- * - `unionSources` has fewer than 2 arms,
1188
- * - two arms in `unionSources` reference the same `collection`.
1189
- *
1190
- * The error message is prefixed with `[noy-db] withMaterializedView:`
1191
- * so it's grep-friendly in logs and looks consistent with the existing
1192
- * `ValidationError` messages from the same factory.
1193
- */
1194
- declare class MaterializedViewConfigError extends NoydbError {
1195
- constructor(message: string);
1196
- }
1197
- /**
1198
- * Thrown at vault open when a `withOverlayedView` declaration uses
1199
- * another virtual-overlay name as its `base`. Multi-overlay stacking
1200
- * is a v2 non-goal — the shallow expansion in
1201
- * `QueryDependencyAnalyzer` would truncate at the inner overlay
1202
- * name, leaving downstream MVs silently stale.
1203
- */
1204
- declare class OverlayBaseIsVirtualError extends NoydbError {
1205
- readonly overlayName: string;
1206
- readonly base: string;
1207
- constructor(overlayName: string, base: string);
1208
- }
1209
- /**
1210
- * Thrown at vault open when a `withOverlayedView`'s `overlay`
1211
- * references an unknown collection or an MV-owned collection. The
1212
- * overlay collection is user-writable; MV-owned collections aren't.
1213
- */
1214
- declare class OverlayCollectionUnavailableError extends NoydbError {
1215
- readonly overlayName: string;
1216
- readonly overlay: string;
1217
- constructor(overlayName: string, overlay: string);
1218
- }
1219
- /**
1220
- * Thrown at vault open when a `withOverlayedView`'s virtual `name`
1221
- * collides with an MV output or a concrete source collection.
1222
- */
1223
- declare class OverlayNameCollisionError extends NoydbError {
1224
- readonly overlayName: string;
1225
- constructor(overlayName: string);
1226
- }
1227
- /**
1228
- * Thrown by the virtual overlay's `put(id, record)` when the
1229
- * consumer-supplied `id` doesn't match `rowKey(record)`. Catches
1230
- * fat-finger separator typos that would otherwise silently produce
1231
- * orphaned overlay rows. Direct writes to the underlying overlay
1232
- * collection (bypass the virtual layer) skip this validation.
1233
- */
1234
- declare class OverlayIdMismatchError extends NoydbError {
1235
- readonly actual: string;
1236
- readonly expected: string;
1237
- constructor(actual: string, expected: string);
1238
- }
1239
- /**
1240
- * Thrown when a requested snapshot version does not exist in the
1241
- * snapshot store — either it was never created, was pruned by the
1242
- * retention policy, or was deleted manually.
1243
- *
1244
- * The `version` field carries the key that was looked up so callers
1245
- * can surface an actionable "snapshot X not found" message without
1246
- * parsing the error string.
1247
- */
1248
- declare class SnapshotNotFoundError extends NoydbError {
1249
- readonly version: string;
1250
- constructor(version: string);
1251
- }
1252
-
1253
- /**
1254
- * Foreign-key references — the soft-FK mechanism.
1255
- *
1256
- * A collection declares its references as metadata at construction
1257
- * time:
1258
- *
1259
- * ```ts
1260
- * import { ref } from '@noy-db/hub'
1261
- *
1262
- * const invoices = company.collection<Invoice>('invoices', {
1263
- * refs: {
1264
- * clientId: ref('clients'), // default: strict
1265
- * categoryId: ref('categories', 'warn'),
1266
- * parentId: ref('invoices', 'cascade'), // self-reference OK
1267
- * },
1268
- * })
1269
- * ```
1270
- *
1271
- * Three modes:
1272
- *
1273
- * - **strict** — the default. `put()` rejects records whose
1274
- * reference target doesn't exist, and `delete()` of the target
1275
- * rejects if any strict-referencing records still exist.
1276
- * Matches SQL's default FK semantics.
1277
- *
1278
- * - **warn** — both operations succeed unconditionally. Broken
1279
- * references surface only through
1280
- * `vault.checkIntegrity()`, which walks every collection
1281
- * and reports orphans. Use when you want soft validation for
1282
- * imports from messy sources.
1283
- *
1284
- * - **cascade** — `put()` is same as warn. `delete()` of the
1285
- * target deletes every referencing record. Cycles are detected
1286
- * and broken via an in-progress set, so mutual cascades
1287
- * terminate instead of recursing forever.
1288
- *
1289
- * Cross-vault refs are explicitly rejected: if the target
1290
- * name contains a `/`, `ref()` throws `RefScopeError`. Cross-
1291
- * vault refs need an auth story (multi-keyring reads) that
1292
- * doesn't ship — tracked for.
1293
- */
1294
-
1295
- /** The three enforcement modes. Default for new refs is `'strict'`. */
1296
- type RefMode = 'strict' | 'warn' | 'cascade';
1297
- /**
1298
- * Descriptor returned by `ref()`. Collections accept a
1299
- * `Record<string, RefDescriptor>` in their options. The key is the
1300
- * field name on the record (top-level only — dotted paths are out of
1301
- * scope), the value describes which target collection the
1302
- * field references and under what mode.
1303
- *
1304
- * The descriptor carries only plain data so it can be serialized,
1305
- * passed around, and introspected without any class machinery.
1306
- */
1307
- interface RefDescriptor {
1308
- readonly target: string;
1309
- readonly mode: RefMode;
1310
- }
1311
- /**
1312
- * Thrown when a strict reference is violated — either `put()` with a
1313
- * missing target id, or `delete()` of a target that still has
1314
- * strict-referencing records.
1315
- *
1316
- * Carries structured detail so UI code (and a potential future
1317
- * devtools panel) can render "client X cannot be deleted because
1318
- * invoices 1, 2, and 3 reference it" instead of a bare error string.
1319
- */
1320
- declare class RefIntegrityError extends NoydbError {
1321
- readonly collection: string;
1322
- readonly id: string;
1323
- readonly field: string;
1324
- readonly refTo: string;
1325
- readonly refId: string | null;
1326
- constructor(opts: {
1327
- collection: string;
1328
- id: string;
1329
- field: string;
1330
- refTo: string;
1331
- refId: string | null;
1332
- message: string;
1333
- });
1334
- }
1335
- /**
1336
- * Thrown when `ref()` is called with a target name that looks like
1337
- * a cross-vault reference (contains a `/`). Separate error
1338
- * class because the fix is different: RefIntegrityError means "data
1339
- * is wrong"; RefScopeError means "the ref declaration is wrong".
1340
- */
1341
- declare class RefScopeError extends NoydbError {
1342
- constructor(target: string);
1343
- }
1344
- /**
1345
- * Helper constructor. Thin wrapper around the object literal so user
1346
- * code reads like `ref('clients')` instead of `{ target: 'clients',
1347
- * mode: 'strict' }` — this is the only ergonomics reason it exists.
1348
- *
1349
- * Validates the target name eagerly so a misconfigured ref declaration
1350
- * fails at collection construction time, not at the first put.
1351
- */
1352
- declare function ref(target: string, mode?: RefMode): RefDescriptor;
1353
- /**
1354
- * Per-vault registry of reference declarations.
1355
- *
1356
- * The registry is populated by `Collection` constructors (which pass
1357
- * their `refs` option through the Vault) and consulted by the
1358
- * Vault on every `put` / `delete` and by `checkIntegrity`. A
1359
- * single instance lives on the Vault for its lifetime; there's
1360
- * no global state.
1361
- *
1362
- * The data structure is two parallel maps:
1363
- *
1364
- * - `outbound`: `collection → { field → RefDescriptor }` — what
1365
- * refs does `collection` declare? Used on put to check
1366
- * strict-target-exists and on checkIntegrity to walk each
1367
- * collection's outbound refs.
1368
- *
1369
- * - `inbound`: `target → Array<{ collection, field, mode }>` —
1370
- * which collections reference `target`? Used on delete to find
1371
- * the records that might be affected by cascade / strict.
1372
- *
1373
- * The two views are kept in sync by `register()` and never mutated
1374
- * otherwise — refs can't be unregistered at runtime in.
1375
- */
1376
- declare class RefRegistry {
1377
- private readonly outbound;
1378
- private readonly inbound;
1379
- /**
1380
- * Register the refs declared by a single collection. Idempotent in
1381
- * the happy path — calling twice with the same data is a no-op.
1382
- * Calling twice with DIFFERENT data throws, because silent
1383
- * overrides would be confusing ("I changed the ref and it doesn't
1384
- * update" vs "I declared the same collection twice with different
1385
- * refs and the second call won").
1386
- */
1387
- register(collection: string, refs: Record<string, RefDescriptor>): void;
1388
- /** Get the outbound refs declared by a collection (or `{}` if none). */
1389
- getOutbound(collection: string): Record<string, RefDescriptor>;
1390
- /** Get the inbound refs that target a given collection (or `[]`). */
1391
- getInbound(target: string): ReadonlyArray<{
1392
- collection: string;
1393
- field: string;
1394
- mode: RefMode;
1395
- }>;
1396
- /**
1397
- * Iterate every (collection → refs) pair that has at least one
1398
- * declared reference. Used by `checkIntegrity` to walk the full
1399
- * universe of outbound refs without needing to track collection
1400
- * names elsewhere.
1401
- */
1402
- entries(): Array<[string, Record<string, RefDescriptor>]>;
1403
- /** Clear the registry. Test-only escape hatch; never called from production code. */
1404
- clear(): void;
1405
- }
1406
- /**
1407
- * Shape of a single violation reported by `vault.checkIntegrity()`.
1408
- *
1409
- * `refId` is the value we saw in the referencing field — it's the
1410
- * ID we expected to find in `refTo`, but didn't. Left as `unknown`
1411
- * because records are loosely typed at the integrity-check layer.
1412
- */
1413
- interface RefViolation {
1414
- readonly collection: string;
1415
- readonly id: string;
1416
- readonly field: string;
1417
- readonly refTo: string;
1418
- readonly refId: unknown;
1419
- readonly mode: RefMode;
1420
- }
1421
-
1422
- /**
1423
- * Query DSL `.join()` — eager, single-FK, intra-vault joins.
1424
- *
1425
- * resolves a ref()-declared foreign key into an attached
1426
- * right-side record under an alias, using one of two planner paths
1427
- * selected automatically:
1428
- *
1429
- * - **nested-loop** — right-side source exposes `lookupById`, so
1430
- * each left row costs O(1). This is the common path for joins
1431
- * against a Collection, which backs `lookupById` with a Map
1432
- * lookup.
1433
- * - **hash** — right-side has only `snapshot()`. Build a
1434
- * `Map<id, record>` once, probe per left row. Same asymptotic
1435
- * cost for our collections, but the path exists as a fallback
1436
- * for custom QuerySource implementations and as an explicit
1437
- * test-only override via `{ strategy: 'hash' }`.
1438
- *
1439
- * Scope:
1440
- *
1441
- * - Equi-joins on declared `ref()` fields only. Joins on
1442
- * undeclared fields throw at plan time with an actionable error
1443
- * naming the field and collection.
1444
- * - Same-vault only. Cross-vault correlation goes
1445
- * through `queryAcross`; this is an architectural
1446
- * invariant, not a limitation we plan to lift.
1447
- * - Hard row ceiling via `JoinTooLargeError` — default 50k per
1448
- * side, override via `{ maxRows }`. Warns at 80% of the ceiling
1449
- * on the existing warn channel.
1450
- * - Three ref-mode behaviors on dangling refs:
1451
- * strict → `DanglingReferenceError`,
1452
- * warn → attach `null` with a one-shot warning,
1453
- * cascade → attach `null` silently (cascade is a delete-time
1454
- * mode; any dangling refs still present at read time are
1455
- * mid-flight cascades or orphans from earlier, not a DSL error).
1456
- *
1457
- * Partition-awareness seam:
1458
- *
1459
- * Every `JoinLeg` carries a `partitionScope` field that is always
1460
- * `'all'` in. The executor never reads this field.
1461
- * partition-aware joins will start populating it from `where()`
1462
- * predicates on the partition key without changing the planner's
1463
- * external shape — this is the whole reason it exists now.
1464
- *
1465
- * Joins stay OUT of the ledger: reads don't touch `_ledger/`,
1466
- * including joined reads.
1467
- */
1468
-
1469
- /** Planner strategy for a single join leg. Auto-selected unless overridden. */
1470
- type JoinStrategy = 'hash' | 'nested';
1471
- /** Default per-side row ceiling before `.join()` throws `JoinTooLargeError`. */
1472
- declare const DEFAULT_JOIN_MAX_ROWS = 50000;
1473
- /**
1474
- * Internal representation of a single join leg in the query plan.
1475
- *
1476
- * This is the primary place where constraint #1 is honored:
1477
- * every leg carries a `partitionScope` field that is always `'all'`
1478
- * in and is never read by the executor. partition-aware
1479
- * joins will start populating it from `where()` predicates on the
1480
- * partition key without changing the planner's external shape.
1481
- */
1482
- interface JoinLeg {
1483
- /** Field on the left-side record holding the foreign key value. */
1484
- readonly field: string;
1485
- /** Alias key under which the joined right-side record attaches. */
1486
- readonly as: string;
1487
- /** Target collection name, resolved from the `ref()` declaration. */
1488
- readonly target: string;
1489
- /** Ref mode controlling behavior on dangling refs at read time. */
1490
- readonly mode: RefMode;
1491
- /** Manual planner strategy override. `undefined` → auto-select. */
1492
- readonly strategy: JoinStrategy | undefined;
1493
- /** Per-side row ceiling override. `undefined` → DEFAULT_JOIN_MAX_ROWS. */
1494
- readonly maxRows: number | undefined;
1495
- /**
1496
- * Partition scope for future partition-aware joins. Always `'all'`
1497
- * today — the executor never reads this field. Future versions will
1498
- * populate it from `where()` predicates without breaking the
1499
- * planner's external shape. Do not remove even though it looks
1500
- * unused today — that's the whole point of having it.
1501
- */
1502
- readonly partitionScope: 'all' | readonly string[];
1503
- /**
1504
- * When `true`, this is a dictionary join. The executor
1505
- * resolves the left-field value against the dict snapshot and
1506
- * attaches `{ ...labels, key }` rather than a right-side record.
1507
- * `target` holds the dictionary name (not a collection name).
1508
- */
1509
- readonly isDictJoin?: true;
1510
- }
1511
- /**
1512
- * Minimal shape of a joinable right-side record source.
1513
- *
1514
- * Collections implement this structurally via their `QuerySource`;
1515
- * sources without `lookupById` force the hash-join fallback. Kept as
1516
- * a thin interface so tests can wire up plain-object sources without
1517
- * pulling in the full Collection class.
1518
- *
1519
- * The optional `subscribe` is used by `Query.live()` to merge
1520
- * right-side change streams into the live re-run trigger. Sources
1521
- * that omit `subscribe` still work for live joins — they just
1522
- * don't drive re-fires when their right side mutates. Collection
1523
- * implements `subscribe` by hooking into the existing per-
1524
- * vault event emitter.
1525
- */
1526
- interface JoinableSource {
1527
- snapshot(): readonly unknown[];
1528
- lookupById?(id: string): unknown;
1529
- /**
1530
- * Subscribe to mutations on this source. The callback fires
1531
- * AFTER the underlying record set has been updated. Returns an
1532
- * unsubscribe function. Optional — sources without this method
1533
- * cannot trigger live-join re-fires from their side.
1534
- */
1535
- subscribe?(cb: () => void): () => void;
1536
- }
1537
- /**
1538
- * Join resolution context attached to a `Query` when it's constructed
1539
- * from a `Collection`. Holds everything the `.join()` method needs to
1540
- * translate a field name into a target collection + ref mode, and
1541
- * everything the executor needs to read the right side.
1542
- *
1543
- * Kept as a structural interface so `Vault` can implement it
1544
- * without `Query` needing to import `Vault` (circular-import
1545
- * avoid). The Collection wires this up in its `query()` method using
1546
- * the `joinResolver` back-reference the Vault passes in.
1547
- */
1548
- interface JoinContext {
1549
- /** Name of the left-side (owning) collection. */
1550
- readonly leftCollection: string;
1551
- /** Look up a `RefDescriptor` by field name on the left collection. */
1552
- resolveRef(field: string): RefDescriptor | null;
1553
- /** Resolve a right-side source by target collection name. */
1554
- resolveSource(collectionName: string): JoinableSource | null;
1555
- /**
1556
- * Resolve a dictKey join source. Returns a `JoinableSource`
1557
- * whose snapshot exposes `{ key, ...labels }` records, keyed by the
1558
- * stable dictionary key. `null` when the field is not a dictKey.
1559
- *
1560
- * The source is built from the compartment's in-memory dictionary
1561
- * snapshot — same data as `DictionaryHandle.list()`, O(1) per lookup.
1562
- */
1563
- resolveDictSource?(field: string): JoinableSource | null;
1564
- }
1565
- /**
1566
- * Apply every join leg in the plan against a base set of left-side
1567
- * rows. Called by the query executor after `where` / `orderBy` /
1568
- * `offset` / `limit` have narrowed the left set.
1569
- *
1570
- * Each leg attaches a `leg.as` field to every row. Returns a new
1571
- * array of plain objects — the original left rows are not mutated
1572
- * (structural sharing is fine for the inner fields, but the
1573
- * top-level object is a fresh clone so consumers can further mutate
1574
- * safely).
1575
- *
1576
- * **Ordering:** joins run AFTER orderBy / limit / offset in v1.
1577
- * This keeps the planner simple and means queries like "top 10
1578
- * invoices with client" sort and paginate the left side first, then
1579
- * join. Sorting *by* a joined field is out of scope for — users
1580
- * can post-sort the result array in userland or wait for
1581
- * (multi-FK chaining) which can be layered on top.
1582
- *
1583
- * **Multi-FK chaining:** each leg's `maxRows` is enforced
1584
- * against the current left-row count independently. Because
1585
- * joins are equi-joins on the target's primary key (one-to-one or
1586
- * one-to-null), the left row count is constant across legs — no
1587
- * cartesian blowup. The per-leg left-side check is still necessary
1588
- * so that a later leg with a tighter ceiling correctly fires on a
1589
- * query like `.join('a', { maxRows: 100_000 }).join('b', { maxRows: 50 })`,
1590
- * which should throw on the second leg if the left set exceeds 50.
1591
- */
1592
- declare function applyJoins(rows: readonly unknown[], joins: readonly JoinLeg[], context: JoinContext): unknown[];
1593
- /**
1594
- * Test-only: reset the join warning deduplication state between
1595
- * tests. Production code never calls this — the dedup state is
1596
- * intentionally process-scoped so a noisy query doesn't spam the
1597
- * console once per component render.
1598
- */
1599
- declare function resetJoinWarnings(): void;
1600
-
1601
- /**
1602
- * Reactive query primitive — `query.live()`.
1603
- *
1604
- * produces a `LiveQuery<T>` that re-runs the query and
1605
- * updates its `value` whenever any source feeding it (the left
1606
- * collection AND every right-side collection a join leg points at)
1607
- * mutates.
1608
- *
1609
- * Framework-agnostic by design. The Vue layer wraps a `LiveQuery`
1610
- * in a Vue `Ref<T[]>` by subscribing once and copying `value` into
1611
- * the ref on every notification. React/Solid/Svelte adapters do the
1612
- * same with their own primitives. Core never depends on a UI
1613
- * framework.
1614
- *
1615
- * **Error semantics.** A `.live()` query may throw at re-run time —
1616
- * a strict-mode `DanglingReferenceError` is the most common case
1617
- * (a right-side record was deleted out-of-band, leaving a left
1618
- * row's FK pointing at nothing). When the re-run throws, the
1619
- * `LiveQuery` catches the error and stores it in the `error`
1620
- * field; it does NOT propagate the throw out of the source's
1621
- * change handler, because doing so would tear down whatever
1622
- * upstream emitter is dispatching. Listeners check `error` after
1623
- * each notification and render an error state in the UI.
1624
- *
1625
- * **Dedup of right-side subscriptions.** A multi-FK chain that
1626
- * joins the same target twice (e.g.
1627
- * `.join('billingClientId').join('shippingClientId')`, both
1628
- * pointing at `clients`) only subscribes to that target once. We
1629
- * dedup by target collection name, on the assumption that
1630
- * `resolveSource(name)` returns a single subscribable source per
1631
- * vault + name. Vault's `resolveSource` reads from
1632
- * `collectionCache` so this assumption holds.
1633
- *
1634
- * **What .live() does NOT do in v1:**
1635
- * - No granular delta updates — the whole query re-runs on every
1636
- * change. Granular delta tracking is a v2 optimization once
1637
- * the API is stable.
1638
- * - No batching of bursty changes — one event in, one re-run
1639
- * out. Batching with microtask coalescing is a v2 enhancement.
1640
- * - No async notifications — every notification is synchronous
1641
- * within the source's change handler.
1642
- * - No re-planning under live mutations — the planner picks once
1643
- * at subscription time and reuses the same plan for every
1644
- * re-run.
1645
- */
1646
- /**
1647
- * The reactive primitive returned by `Query.live()`.
1648
- *
1649
- * Listeners can read the current `value` snapshot at any time and
1650
- * subscribe to changes via `.subscribe(cb)`. The `error` field
1651
- * carries the most recent re-run error, if any — read it after
1652
- * each notification to render error state.
1653
- *
1654
- * Always call `stop()` when the live query is no longer needed.
1655
- * Without it, the upstream change-stream subscriptions stay live
1656
- * forever and the query keeps re-running on every mutation.
1657
- */
1658
- interface LiveQuery<T> {
1659
- /**
1660
- * Current snapshot of the query result. Updated in place on
1661
- * every upstream change. The reference returned is the same
1662
- * `readonly T[]` array — consumers that want change detection by
1663
- * reference should copy: `const arr = [...live.value]`.
1664
- */
1665
- readonly value: readonly T[];
1666
- /**
1667
- * Most recent re-run error, or `null` on success. Set when the
1668
- * executor throws (e.g. `DanglingReferenceError` in strict mode
1669
- * after a right-side delete). Cleared on the next successful
1670
- * re-run.
1671
- */
1672
- readonly error: Error | null;
1673
- /**
1674
- * Register a notification callback. Fires AFTER `value` and
1675
- * `error` have been updated for a given upstream change.
1676
- * Returns an unsubscribe function.
1677
- *
1678
- * The first call to `subscribe` does NOT fire the callback
1679
- * immediately — call sites that want the initial value should
1680
- * read `live.value` directly before subscribing.
1681
- */
1682
- subscribe(cb: () => void): () => void;
1683
- /**
1684
- * Tear down every upstream subscription and clear the listener
1685
- * set. Idempotent — calling twice is safe. After `stop()`, the
1686
- * query no longer re-runs and `subscribe()` becomes a no-op
1687
- * (the returned unsubscribe is still callable and is also a
1688
- * no-op).
1689
- */
1690
- stop(): void;
1691
- }
1692
- /**
1693
- * Internal subscription handle for an upstream source — left or
1694
- * right side. The contract is just `subscribe(cb): unsubscribe`,
1695
- * matching the existing `QuerySource.subscribe` and the new
1696
- * `JoinableSource.subscribe` (added in ).
1697
- */
1698
- interface LiveUpstream {
1699
- subscribe(cb: () => void): () => void;
1700
- }
1701
- /**
1702
- * Build a LiveQuery from a `recompute` callback (typically the
1703
- * Query's bound `toArray`) and a list of upstream sources to
1704
- * subscribe to.
1705
- *
1706
- * The recompute fires once synchronously to populate the initial
1707
- * value, then re-fires every time any upstream notifies. Errors
1708
- * thrown by recompute are caught and stored in `error` instead of
1709
- * propagating — see the file docstring for the rationale.
1710
- */
1711
- declare function buildLiveQuery<T>(recompute: () => T[], upstreams: readonly LiveUpstream[]): LiveQuery<T>;
1712
-
1713
- /**
1714
- * Chainable, immutable query builder.
1715
- *
1716
- * Each builder operation returns a NEW Query — the underlying plan is never
1717
- * mutated. This makes plans safe to share, cache, and serialize.
1718
- */
1719
-
1720
- interface OrderBy {
1721
- readonly field: string;
1722
- readonly direction: 'asc' | 'desc';
1723
- }
1724
- /**
1725
- * A complete query plan: zero-or-more clauses, optional ordering, pagination,
1726
- * and optional joins.
1727
- *
1728
- * Plans are JSON-serializable as long as no FilterClause is present and no
1729
- * join leg carries a manual `strategy` override (JoinLeg itself is plain
1730
- * data, so it serializes cleanly).
1731
- *
1732
- * Plans are intentionally NOT parametric on T — see `predicate.ts` FilterClause
1733
- * for the variance reasoning. The public `Query<T>` API attaches the type tag.
1734
- */
1735
- interface QueryPlan {
1736
- readonly clauses: readonly Clause[];
1737
- readonly orderBy: readonly OrderBy[];
1738
- readonly limit: number | undefined;
1739
- readonly offset: number;
1740
- /**
1741
- * Zero-or-more join legs to apply after where/orderBy/limit/offset.
1742
- * Each leg attaches a resolved right-side record (or null) under its
1743
- * alias. See `query/join.ts` for the full semantics.
1744
- */
1745
- readonly joins: readonly JoinLeg[];
1746
- }
1747
- /** Default row ceiling for cross-join expansion. Matches JoinTooLargeError's ceiling. */
1748
- declare const DEFAULT_CROSS_JOIN_MAX_ROWS = 50000;
1749
- /**
1750
- * Source of records that a query executes against.
1751
- *
1752
- * The interface is non-parametric to keep variance friendly: callers cast
1753
- * their typed source (e.g. `QuerySource<Invoice>`) into this opaque shape.
1754
- *
1755
- * `getIndexes` and `lookupById` are optional fast-path hooks. When both are
1756
- * present and a where clause matches an indexed field, the executor uses
1757
- * the index to skip a linear scan. Sources without these methods (or with
1758
- * `getIndexes` returning `null`) always fall back to a linear scan.
1759
- */
1760
- interface QuerySource<T> {
1761
- /** Snapshot of all current records. The query never mutates this array. */
1762
- snapshot(): readonly T[];
1763
- /** Subscribe to mutations; returns an unsubscribe function. */
1764
- subscribe?(cb: () => void): () => void;
1765
- /** Index store for the indexed-fast-path. Optional. */
1766
- getIndexes?(): CollectionIndexes | null;
1767
- /** O(1) record lookup by id, used to materialize index hits. */
1768
- lookupById?(id: string): T | undefined;
1769
- }
1770
- /**
1771
- * The chainable builder. All methods return a new Query — the original
1772
- * remains unchanged. Terminal methods (`toArray`, `first`, `count`,
1773
- * `subscribe`) execute the plan against the source.
1774
- *
1775
- * Type parameter T flows through the public API for ergonomics, but the
1776
- * internal storage uses `unknown` so Collection<T> stays covariant.
1777
- *
1778
- * The optional `joinContext` is attached when the Query is constructed
1779
- * via `Collection.query()` (Collection passes in a context built from
1780
- * the Vault's join resolver). A Query constructed via `new Query`
1781
- * directly — e.g. from tests with a plain-object source — has no
1782
- * joinContext, and calling `.join()` on it throws with an actionable
1783
- * error. See `query/join.ts` for the full design.
1784
- */
1785
- /**
1786
- * Declared deterministic predicate. Carries the consumer's
1787
- * stable `hash` (for function-body identity), the function itself,
1788
- * and is keyed by name when registered on a `Query<T>` via
1789
- * `_withPredicates()`.
1790
- */
1791
- interface DeclaredPredicate {
1792
- hash: string;
1793
- fn: (record: unknown, ctx?: unknown) => boolean;
1794
- }
1795
- declare class Query<T> {
1796
- private readonly source;
1797
- private readonly plan;
1798
- private readonly joinContext;
1799
- private readonly aggregateStrategy;
1800
- private readonly predicates;
1801
- constructor(source: QuerySource<T>, plan?: QueryPlan, joinContext?: JoinContext, aggregateStrategy?: AggregateStrategy, predicates?: ReadonlyMap<string, DeclaredPredicate>);
1802
- /**
1803
- * @internal — accessor for the materialized-view dependency
1804
- * analyzer. Not part of the public API; consumers should use the
1805
- * builder methods, not inspect the plan directly.
1806
- */
1807
- _plan(): QueryPlan;
1808
- /**
1809
- * @internal — accessor for the materialized-view dependency
1810
- * analyzer. Returns the join resolution context (or `undefined` for
1811
- * queries constructed without a Collection backing).
1812
- */
1813
- _joinContext(): JoinContext | undefined;
1814
- /**
1815
- * @internal — clone this Query with a declared-predicate map
1816
- * attached. Used by the materialized-view registry to enable
1817
- * `.wherePredicate(name, ctx?)` for the MV's query callback.
1818
- * Consumers don't call this directly.
1819
- */
1820
- _withPredicates(predicates: ReadonlyMap<string, DeclaredPredicate>): Query<T>;
1821
- /**
1822
- * Filter by a registered deterministic predicate. Requires
1823
- * the Query to have been augmented with a predicates map (typically
1824
- * via the materialized-view registry — bare Queries constructed
1825
- * outside an MV throw on `.wherePredicate()`).
1826
- *
1827
- * `ctx` is an optional opaque value passed verbatim to the predicate
1828
- * function. Both `predicateHash` (from the registration) and a
1829
- * canonical-JSON hash of `ctx` fold into the MV's `queryHash`, so
1830
- * either changing forces refresh on next visit.
1831
- */
1832
- wherePredicate(name: string, ctx?: unknown): Query<T>;
1833
- /** Add a field comparison. Multiple where() calls are AND-combined. */
1834
- where(field: string, op: Operator, value: unknown): Query<T>;
1835
- /**
1836
- * Logical OR group. Pass a callback that builds a sub-query.
1837
- * Each clause inside the callback is OR-combined; the group itself
1838
- * joins the parent plan with AND.
1839
- */
1840
- or(builder: (q: Query<T>) => Query<T>): Query<T>;
1841
- /**
1842
- * Logical AND group. Same shape as `or()` but every clause inside the group
1843
- * must match. Useful for explicit grouping inside a larger OR.
1844
- */
1845
- and(builder: (q: Query<T>) => Query<T>): Query<T>;
1846
- /** Escape hatch: add an arbitrary predicate function. Not serializable. */
1847
- filter(fn: (record: T) => boolean): Query<T>;
1848
- /** Sort by a field. Subsequent calls are tie-breakers. */
1849
- orderBy(field: string, direction?: 'asc' | 'desc'): Query<T>;
1850
- /** Cap the result size. */
1851
- limit(n: number): Query<T>;
1852
- /** Skip the first N matching records (after ordering). */
1853
- offset(n: number): Query<T>;
1854
- /**
1855
- * Resolve a `ref()`-declared foreign key and attach the right-side
1856
- * record under `opts.as`. — eager, single-FK, intra-
1857
- * vault joins.
1858
- *
1859
- * ```ts
1860
- * const rows = invoices.query()
1861
- * .where('status', '==', 'open')
1862
- * .join('clientId', { as: 'client' })
1863
- * .toArray()
1864
- * // → [{ id, amount, client: { id, name, ... } }, ...]
1865
- * ```
1866
- *
1867
- * Preconditions:
1868
- * - The Query must have a `joinContext` (constructed via
1869
- * `Collection.query()`, not `new Query`).
1870
- * - `field` must have a matching `refs: { [field]: ref('<target>') }`
1871
- * declaration on the left collection.
1872
- * - The target collection must be reachable via the vault
1873
- * (either currently open or openable on demand).
1874
- *
1875
- * Strategy:
1876
- * - Nested-loop against `lookupById` when the target source
1877
- * provides it (the common path for Collection targets).
1878
- * - Hash join otherwise, or when `{ strategy: 'hash' }` is
1879
- * explicitly passed for test purposes.
1880
- *
1881
- * Ref-mode semantics on dangling refs (left record has a non-null
1882
- * FK value pointing at a right-side id that doesn't exist):
1883
- * - `strict` → throws `DanglingReferenceError` with the full
1884
- * field / target / refId context.
1885
- * - `warn` → attaches `null` and emits a one-shot warning per
1886
- * unique dangling pair.
1887
- * - `cascade` → attaches `null` silently. Cascade is a
1888
- * delete-time mode; dangling refs visible at read time are
1889
- * either mid-flight cascades or pre-existing orphans, not a
1890
- * DSL-level error.
1891
- *
1892
- * A left-side record whose FK field is `null` / `undefined` is NOT
1893
- * a dangling ref — it's "no reference at all", always allowed
1894
- * regardless of mode.
1895
- *
1896
- * The return type widens `T` with `Record<As, R | null>`. The `R`
1897
- * parameter is optional — supply it explicitly for type-checked
1898
- * access to the joined fields:
1899
- *
1900
- * ```ts
1901
- * invoices.query().join<'client', Client>('clientId', { as: 'client' })
1902
- * // ^^^^^^^^^^^^^^^^^^^ alias literal + right-side type
1903
- * ```
1904
- *
1905
- * Without the generic, the joined field is typed as `unknown`, which
1906
- * still works but requires a cast to access its properties.
1907
- *
1908
- * Joins stay intra-vault by construction — cross-vault
1909
- * correlation goes through `Noydb.queryAcross`, not
1910
- * `.join()`.
1911
- */
1912
- join<As extends string, R = unknown>(field: string, opts: {
1913
- as: As;
1914
- strategy?: JoinStrategy;
1915
- maxRows?: number;
1916
- }): Query<T & Record<As, R | null>>;
1917
- /**
1918
- * Cartesian-product cross-join against `target` collection. Each result row
1919
- * carries the original `T` fields plus `result[as]` populated from every
1920
- * right-side row (or the filtered subset when `on:` is supplied).
1921
- *
1922
- * **Order matters:** `.where().crossJoin()` filters BEFORE expanding (cheaper);
1923
- * `.crossJoin().where('alias.field', ...)` filters AFTER (required when the
1924
- * where clause references the aliased fields).
1925
- *
1926
- * **Cost ceiling:** `CrossJoinTooLargeError` fires before allocation when
1927
- * `leftRows × rightRows` (or the cumulative lateral count) exceeds the limit.
1928
- * Default: 50,000 rows. Override per-clause with `{ maxRows: N }`.
1929
- *
1930
- * **`on:` shapes:**
1931
- * - `on: (left) => TTarget[]` — subset form (most efficient)
1932
- * - `on: (left) => (right) => boolean` — predicate form
1933
- * - `on: { predicate: 'name' }` — MV-safe, hash-tracked form
1934
- * (requires the Query to have been augmented via `_withPredicates`)
1935
- *
1936
- * Requires a JoinContext (constructed via `collection.query()`).
1937
- */
1938
- crossJoin<TTarget = unknown, As extends string = string>(target: string, opts: {
1939
- as: As;
1940
- on?: ((left: T) => unknown[] | ((right: TTarget) => boolean)) | {
1941
- readonly predicate: string;
1942
- };
1943
- maxRows?: number;
1944
- }): Query<T & {
1945
- [K in As]: TTarget;
1946
- }>;
1947
- /**
1948
- * Execute the plan and return the matching records. When the plan
1949
- * carries any join legs, they are applied after `where` / `orderBy`
1950
- * / `limit` / `offset` narrow the left set. See the `.join()` doc
1951
- * for the ordering rationale.
1952
- */
1953
- toArray(): T[];
1954
- /** Return the first matching record, or null. Joins are applied. */
1955
- first(): T | null;
1956
- /**
1957
- * Return the number of matching records (after where/filter,
1958
- * before limit). **Joins are NOT applied** — count() reports the
1959
- * left-side cardinality, because joins in are projection-only
1960
- * (they attach an aliased field; they never filter). Running joins
1961
- * here just to discard the aliases would be wasteful, and in strict
1962
- * mode it could throw `DanglingReferenceError` for a call whose
1963
- * intent is purely to count.
1964
- */
1965
- count(): number;
1966
- /**
1967
- * Reduce the matching records through a named set of reducers.
1968
- * the aggregation terminal.
1969
- *
1970
- * ```ts
1971
- * const { total, n, avgAmount } = invoices.query()
1972
- * .where('status', '==', 'open')
1973
- * .aggregate({
1974
- * total: sum('amount'),
1975
- * n: count(),
1976
- * avgAmount: avg('amount'),
1977
- * })
1978
- * .run()
1979
- * ```
1980
- *
1981
- * Returns an `Aggregation<R>` wrapper with two terminals:
1982
- * - `.run(): R` — synchronous one-shot reduction
1983
- * - `.live(): LiveAggregation<R>` — reactive primitive that
1984
- * re-runs the reduction whenever the source notifies of a
1985
- * change. Always call `live.stop()` when finished.
1986
- *
1987
- * The reducer spec is bound here once and reused by both
1988
- * terminals — this is why `.aggregate()` returns a wrapper instead
1989
- * of being a direct terminal. Consumers who only need the static
1990
- * value read `.run()`; consumers wiring a reactive UI read
1991
- * `.live()`.
1992
- *
1993
- * Joins are intentionally NOT applied to aggregations in —
1994
- * the same logic as `.count()`. Joins in are projection-only
1995
- * (they attach an aliased field and never filter), so running
1996
- * them just to throw the aliases away would be wasteful. If you
1997
- * need a reducer that reads a joined field, open an issue —
1998
- * aggregations-across-joins is explicitly out of scope for v1.
1999
- *
2000
- * Every reducer factory accepts an optional `{ seed }` parameter
2001
- * that is plumbed through the protocol but unused by the
2002
- * executor — that's constraint #2. When partition-aware
2003
- * aggregation lands, the seed will carry running state across
2004
- * partition boundaries without an API break.
2005
- */
2006
- aggregate<Spec extends AggregateSpec>(spec: Spec): Aggregation<AggregateResult<Spec>>;
2007
- /**
2008
- * Partition matching records into buckets keyed by a field, then
2009
- * terminate with `.aggregate(spec)` to compute per-bucket
2010
- * reducers..
2011
- *
2012
- * ```ts
2013
- * const byClient = invoices.query()
2014
- * .where('status', '==', 'open')
2015
- * .groupBy('clientId')
2016
- * .aggregate({ total: sum('amount'), n: count() })
2017
- * .run()
2018
- * // → [ { clientId: 'c1', total: 5250, n: 3 }, … ]
2019
- * ```
2020
- *
2021
- * Result rows carry the group key value under the grouping field
2022
- * name plus every reducer output from the spec. Buckets are
2023
- * emitted in first-seen order — consumers who want a specific
2024
- * ordering should `.sort()` downstream.
2025
- *
2026
- * **Cardinality caps:** a one-shot warning fires at 10_000
2027
- * distinct groups; `GroupCardinalityError` throws at 100_000.
2028
- * Grouping on a high-uniqueness field like `id` or `createdAt` is
2029
- * almost always a query mistake — the error message names the
2030
- * field and observed cardinality and suggests narrowing with
2031
- * `.where()` first.
2032
- *
2033
- * **Null / undefined keys:** records with a missing or explicitly
2034
- * `null` group field get their own buckets. `Map`-based
2035
- * partitioning distinguishes `undefined` from `null`, so the two
2036
- * cases do NOT merge. Consumers who want them merged should
2037
- * coalesce upstream with `.filter()`.
2038
- *
2039
- * **Joins are not applied** — same rationale as `.count()` and
2040
- * `.aggregate()`. Joined fields in are projection-only, so
2041
- * running a join inside a grouping pipeline would be wasteful and
2042
- * could trigger `DanglingReferenceError` in strict mode for a
2043
- * call whose intent is purely to bucket-and-reduce. Grouping by
2044
- * a joined field is explicitly out of scope for — file an
2045
- * issue if a real consumer needs it.
2046
- *
2047
- * **Filter clauses (`.filter(fn)`):** grouped queries still
2048
- * support filter clauses in the underlying plan — they run in
2049
- * the same candidate/filter pipeline that `.aggregate()` uses.
2050
- * The performance caveat is the same: filter clauses cost O(N)
2051
- * per record and can't be index-accelerated.
2052
- */
2053
- groupBy<F extends string>(field: F): GroupedQuery<T, F>;
2054
- groupBy<F extends readonly [string, string, ...string[]]>(...fields: F): GroupedQueryN<T, F>;
2055
- /**
2056
- * Re-run the query whenever the source notifies of changes.
2057
- * Returns an unsubscribe function. The callback receives the latest result.
2058
- * Throws if the source does not support subscriptions.
2059
- *
2060
- * **For joined queries, prefer `.live()`** — `subscribe()`
2061
- * only re-fires on LEFT-side changes, so joined data can be
2062
- * stale if the right side mutates between emissions. `.live()`
2063
- * merges change streams from every join target.
2064
- */
2065
- subscribe(cb: (result: T[]) => void): () => void;
2066
- /**
2067
- * Reactive terminal — returns a `LiveQuery<T>` that re-runs the
2068
- * query and updates its `value` whenever any source feeding it
2069
- * mutates..
2070
- *
2071
- * For non-joined queries, `.live()` is a convenience over the
2072
- * existing `.subscribe()` callback shape: a hand-rolled reactive
2073
- * primitive with `value` / `error` fields and a `subscribe(cb)`
2074
- * notification channel. Frame-agnostic — Vue / React / Solid
2075
- * adapters wrap it in their own primitive.
2076
- *
2077
- * For joined queries, `.live()` additionally subscribes to every
2078
- * join target's change stream. Mutations on a right-side
2079
- * collection (insert / update / delete of a client referenced by
2080
- * an invoice) re-fire the live query and re-evaluate every
2081
- * dependent left row. Right-side targets are deduped by
2082
- * collection name, so a chain that joins the same target twice
2083
- * (e.g. billing client + shipping client → both 'clients') only
2084
- * subscribes once.
2085
- *
2086
- * **Ref-mode behavior on right-side disappearance** — matches the
2087
- * eager `.toArray()` contract from :
2088
- * - `strict` → re-run throws `DanglingReferenceError`. The
2089
- * LiveQuery catches the throw, stores it in `live.error`, and
2090
- * notifies listeners (the throw does NOT propagate out of
2091
- * the source's change handler — that would tear down the
2092
- * emitter). Consumers check `live.error` after each
2093
- * notification and render an error state in the UI.
2094
- * - `warn` → joined value flips to `null`; the existing
2095
- * warn-channel deduplication keeps repeated re-runs from
2096
- * spamming the console.
2097
- * - `cascade` → no special handling needed; the cascade-
2098
- * delete mechanism propagates the right-side delete into the
2099
- * left collection on the next tick, and the live query
2100
- * naturally re-fires with the orphaned left rows gone.
2101
- *
2102
- * Always call `live.stop()` when finished — it tears down every
2103
- * upstream subscription. The Vue layer's `onUnmounted` hook
2104
- * should call `stop()` automatically; raw consumers must do it
2105
- * themselves.
2106
- *
2107
- * **Limitations:**
2108
- * - No granular delta updates — the whole query re-runs on
2109
- * every change.
2110
- * - No microtask batching — bursty changes produce one re-run
2111
- * per change.
2112
- * - No re-planning under live mutations — the planner picks
2113
- * once at subscription time and reuses the same plan.
2114
- * - Streaming live joins are deferred.
2115
- */
2116
- live(): LiveQuery<T>;
2117
- /**
2118
- * Return the plan as a JSON-friendly object. FilterClause entries are
2119
- * stripped (their `fn` cannot be serialized) and replaced with
2120
- * { type: 'filter', fn: '[function]' } so devtools can still see them.
2121
- */
2122
- toPlan(): unknown;
2123
- }
2124
- /**
2125
- * Execute a plan against a snapshot of records.
2126
- * Pure function — same input, same output, no side effects.
2127
- *
2128
- * Records are typed as `unknown` because plans are non-parametric; callers
2129
- * cast the return type at the API surface (see `Query.toArray()`).
2130
- */
2131
- declare function executePlan(records: readonly unknown[], plan: QueryPlan): unknown[];
2132
-
2133
- /**
2134
- * Streaming scan builder with filter + aggregate support.
2135
- *
2136
- * `Collection.scan()` now returns a `ScanBuilder<T>` that
2137
- * implements `AsyncIterable<T>` (for existing `for await … of`
2138
- * consumers) AND exposes chainable `.where()` / `.filter()` clauses
2139
- * plus a `.aggregate(spec)` async terminal that reduces the scan
2140
- * stream through the same reducer protocol as `Query.aggregate()`
2141
- *.
2142
- *
2143
- * **Memory model:** O(reducers), not O(records). The aggregate
2144
- * terminal initializes one state per reducer, iterates through the
2145
- * scan one record at a time via `for await`, applies every reducer's
2146
- * `step` per record, and never collects the stream into an array.
2147
- * This is what makes `scan().aggregate()` suitable for collections
2148
- * that don't fit in memory — the bound is a code-level invariant
2149
- * visible in the function body, not a runtime assertion.
2150
- *
2151
- * **Paginated iteration:** the builder holds a `pageProvider`
2152
- * closure that maps `(cursor, limit) → Promise<page>`, plumbed by
2153
- * `Collection.scan()` to `collection.listPage(...)`. The page
2154
- * iterator walks cursors forward until exhaustion, same as the
2155
- * previous async-generator `scan()` did.
2156
- *
2157
- * **Backward compatibility:** existing `for await (const rec of
2158
- * collection.scan()) { … }` code continues to work because
2159
- * `ScanBuilder` implements `[Symbol.asyncIterator]`. The previous
2160
- * signature returned an `AsyncIterableIterator<T>` (which has both
2161
- * `[Symbol.asyncIterator]` and `.next()`). We verified at grep time
2162
- * that no call sites use `.next()` on the scan result directly, so
2163
- * the narrowed interface is safe.
2164
- *
2165
- * **Immutability:** each `.where()` / `.filter()` call returns a
2166
- * fresh builder sharing the same page provider and page size. This
2167
- * lets a base scan be reused for multiple parallel aggregations:
2168
- *
2169
- * ```ts
2170
- * const scan = invoices.scan()
2171
- * const [open, paid] = await Promise.all([
2172
- * scan.where('status', '==', 'open').aggregate({ n: count() }),
2173
- * scan.where('status', '==', 'paid').aggregate({ n: count() }),
2174
- * ])
2175
- * ```
2176
- *
2177
- * Note that each aggregation pays a full scan — there's no shared
2178
- * iteration across the two. Multi-way aggregation in a single pass
2179
- * is out of scope; consumers who need it should build a compound spec
2180
- * and run a single `.aggregate({ openN, paidN })` at the DSL level.
2181
- *
2182
- * **Out of scope for (tracked separately):**
2183
- * - `scan().aggregate().live()` — unbounded scan + change-stream
2184
- * reconciliation is a design problem, not just a code one
2185
- * - `scan().groupBy().aggregate()` — high-cardinality grouping on
2186
- * huge collections would re-introduce the O(groups) memory
2187
- * problem that aggregate fixes
2188
- * - Parallel scan across pages — race-safe page cursor contracts
2189
- * are not in the adapter API yet
2190
- * - `scan().join(...)` — tracked under (streaming join)
2191
- */
2192
-
2193
- /**
2194
- * Page provider — the Collection-shaped hook the builder calls to
2195
- * walk cursors forward. Kept as a structural interface so tests can
2196
- * wire up a synthetic provider without pulling in the full
2197
- * Collection class. Collection's `listPage` matches this shape
2198
- * exactly.
2199
- */
2200
- interface ScanPageProvider<T> {
2201
- listPage(opts: {
2202
- cursor?: string;
2203
- limit?: number;
2204
- }): Promise<{
2205
- items: T[];
2206
- nextCursor: string | null;
2207
- }>;
2208
- }
2209
- /**
2210
- * Chainable streaming scan. Implements `AsyncIterable<T>` for
2211
- * drop-in use with `for await … of`; adds `.where()` / `.filter()`
2212
- * chainable clauses and a `.aggregate(spec)` async terminal.
2213
- *
2214
- * The builder is immutable per operation — each chained call
2215
- * returns a fresh `ScanBuilder` sharing the same page provider and
2216
- * page size. The original builder is never mutated, so it's safe
2217
- * to reuse across multiple parallel consumers.
2218
- */
2219
- declare class ScanBuilder<T> implements AsyncIterable<T> {
2220
- private readonly pageProvider;
2221
- private readonly pageSize;
2222
- private readonly clauses;
2223
- /**
2224
- * Zero-or-more join legs to apply per record as the stream flows.
2225
- * Each leg attaches the resolved right-side record (or null) under
2226
- * its alias. — streaming joins.
2227
- *
2228
- * Joins are evaluated AFTER clauses, so a `where()` filtered-out
2229
- * record never triggers a right-side lookup. This is the same
2230
- * ordering as `Query.toArray()` (clauses first, joins after) and
2231
- * keeps the streaming path from doing wasted work.
2232
- */
2233
- private readonly joins;
2234
- /**
2235
- * Join resolution context. Required for `.join()` to translate a
2236
- * field name into a target collection + ref mode and to resolve
2237
- * the right-side `JoinableSource`. Optional because tests
2238
- * construct ScanBuilder directly with synthetic page providers
2239
- * that don't know about ref() — calling `.join()` without a
2240
- * context throws with an actionable error.
2241
- */
2242
- private readonly joinContext;
2243
- constructor(pageProvider: ScanPageProvider<T>, pageSize?: number, clauses?: readonly Clause[], joins?: readonly JoinLeg[], joinContext?: JoinContext);
2244
- /**
2245
- * Add a field comparison. Runs per record as the scan stream
2246
- * flows through, so non-matching records are dropped before they
2247
- * reach `.aggregate()` or the iteration consumer. Multiple
2248
- * `.where()` calls are AND-combined — same semantics as
2249
- * `Query.where()`.
2250
- *
2251
- * Clauses cannot use the secondary-index fast path here because
2252
- * the scan sources records from the adapter's paginator, not from
2253
- * the in-memory cache where indexes live. Index-accelerated scans
2254
- * are a future optimization — the current implementation
2255
- * evaluates clauses per record in O(1) per clause.
2256
- */
2257
- where(field: string, op: Operator, value: unknown): ScanBuilder<T>;
2258
- /**
2259
- * Escape hatch: add an arbitrary predicate function. Same
2260
- * non-serializable caveat as `Query.filter()` — filter clauses
2261
- * don't round-trip through `toPlan()`. Prefer `.where()` when
2262
- * possible.
2263
- */
2264
- filter(fn: (record: T) => boolean): ScanBuilder<T>;
2265
- /**
2266
- * Resolve a `ref()`-declared foreign key per record as the scan
2267
- * stream flows, attaching the right-side record (or null) under
2268
- * `opts.as`. — streaming joins over `scan()`.
2269
- *
2270
- * ```ts
2271
- * for await (const inv of invoices.scan().join('clientId', { as: 'client' })) {
2272
- * await processInvoice(inv) // inv.client is attached
2273
- * }
2274
- *
2275
- * // Or terminate with .aggregate() for streaming joined aggregation
2276
- * const { total } = await invoices.scan()
2277
- * .where('status', '==', 'open')
2278
- * .join('clientId', { as: 'client' })
2279
- * .aggregate({ total: sum('amount') })
2280
- * ```
2281
- *
2282
- * **The key difference from eager `.join()`:** the LEFT
2283
- * side streams page-by-page from the adapter and is never
2284
- * materialized. Memory ceiling on the left is O(pageSize), not
2285
- * O(rowCount). This is what makes streaming joins suitable for
2286
- * collections that exceed the eager join's 50_000-row ceiling.
2287
- *
2288
- * **Right-side strategy** is auto-selected per leg:
2289
- * - **Indexed** — right source exposes `lookupById`, so each
2290
- * left row costs O(1). This is the common path for
2291
- * Collection right sides, which back `lookupById` with a Map
2292
- * lookup over the in-memory cache. The right collection must
2293
- * be in eager mode (the same constraint as eager join's
2294
- * `querySourceForJoin` from ).
2295
- * - **Hash** — right source has only `snapshot()`. Build a
2296
- * `Map<id, record>` once at iteration start, probe per left
2297
- * row. Same correctness, same per-row cost as the indexed
2298
- * path; the difference is the upfront cost of materializing
2299
- * the right side once.
2300
- *
2301
- * Both strategies hold the right side in memory for the duration
2302
- * of the iteration. The "streaming" property applies to the LEFT
2303
- * side only — true left-and-right streaming joins (where neither
2304
- * side fits in memory) require a sort-merge join planner that's
2305
- * out of scope for.
2306
- *
2307
- * **Ref-mode semantics** match eager `.join()` exactly:
2308
- * - `strict` → throws `DanglingReferenceError` mid-stream
2309
- * when a left record points at a non-existent right id.
2310
- * The throw aborts the async iterator — consumers should
2311
- * wrap the `for await` in try/catch if they want to recover.
2312
- * - `warn` → attaches `null` and emits a one-shot warning
2313
- * per unique dangling pair (deduped via the same warn
2314
- * channel as eager join).
2315
- * - `cascade` → attaches `null` silently. A delete-time mode;
2316
- * dangling refs at read time are mid-flight or pre-existing
2317
- * orphans, not a DSL error.
2318
- *
2319
- * Left records with null/undefined FK values attach `null`
2320
- * regardless of mode — same "no reference at all" policy as
2321
- * eager join and write-time `enforceRefsOnPut`.
2322
- *
2323
- * **Multi-FK chaining** is supported via repeated `.join()`
2324
- * calls: each leg resolves an independent ref. Each leg
2325
- * independently picks its right-side strategy and applies its
2326
- * own ref mode.
2327
- *
2328
- * **Joins are NOT applied** to a `.aggregate()` terminal that
2329
- * doesn't reference joined fields — wait, that's not quite
2330
- * right. The streaming path actually DOES apply joins before
2331
- * `.aggregate()` because the join attaches a field that the
2332
- * spec might reference. Unlike `Query.aggregate()` (which skips
2333
- * joins entirely as a projection-only short-circuit), the
2334
- * streaming aggregation can't know whether the spec touches a
2335
- * joined field, so it always applies joins. Consumers who want
2336
- * unjoined streaming aggregation should leave `.join()` off the
2337
- * chain — the chain is composable for a reason.
2338
- *
2339
- * constraint #1 — every JoinLeg carries `partitionScope:
2340
- * 'all'` plumbed through but never read by. Same seam as
2341
- * eager join.
2342
- */
2343
- join<As extends string, R = unknown>(field: string, opts: {
2344
- as: As;
2345
- }): ScanBuilder<T & Record<As, R | null>>;
2346
- /**
2347
- * Iterate the scan as an async iterable. Walks the page
2348
- * provider's cursors forward until exhaustion, applying every
2349
- * clause per record — only matching records are yielded.
2350
- *
2351
- * Backward-compatible with the previous async-generator `scan()`
2352
- * return type for `for await … of` consumers.
2353
- */
2354
- [Symbol.asyncIterator](): AsyncIterator<T>;
2355
- /**
2356
- * Per-leg right-side resolution state. Built once at iteration
2357
- * start and reused for every left record. Two strategies:
2358
- *
2359
- * - `lookupById`: present when the right source exposes the
2360
- * hook directly (typical Collection right side). Per-row
2361
- * cost is O(1).
2362
- * - `hashByPrimaryKey`: built from `snapshot()` when no
2363
- * lookupById. Per-row cost is O(1) after the upfront O(N)
2364
- * materialization. Same as eager join's hash strategy.
2365
- *
2366
- * `warnedKeys` is the per-leg dedup set for ref-mode 'warn'. We
2367
- * key on `field→target:refId` so the same dangling pair only
2368
- * warns once per iteration. The dedup is per-iteration, not
2369
- * per-process — a long-running scan that re-iterates would warn
2370
- * again, which is the desired behavior (the data may have
2371
- * changed between iterations).
2372
- */
2373
- private buildJoinResolvers;
2374
- /**
2375
- * Resolve a single join leg for one left record and return the
2376
- * left record with the joined field attached under
2377
- * `leg.as`. Pure function over `(left, resolver)`; never
2378
- * mutates the input.
2379
- *
2380
- * Ref-mode dispatch matches eager `applyJoins` from :
2381
- * - null/undefined FK → attach null silently (always allowed)
2382
- * - dangling FK + strict → throw `DanglingReferenceError`
2383
- * - dangling FK + warn → attach null, warn-once per pair
2384
- * - dangling FK + cascade → attach null silently
2385
- */
2386
- private applyOneJoinStreaming;
2387
- /**
2388
- * Reduce the scan stream through a named set of reducers and
2389
- * return the final aggregated shape.
2390
- *
2391
- * Memory is O(reducers): one mutable state slot per spec key.
2392
- * Records flow through the pipeline one at a time via
2393
- * `for await` and are discarded after their `step()` is applied
2394
- * — never collected into an array. This is the distinguishing
2395
- * property from `Query.aggregate()`, which materializes the full
2396
- * match set first.
2397
- *
2398
- * Reuses the same reducer protocol as `Query.aggregate()`,
2399
- * so `count()`, `sum(field)`, `avg(field)`, `min(field)`,
2400
- * `max(field)` all work unchanged. The `{ seed }` parameter
2401
- * plumbing from constraint #2 is honored transparently — the
2402
- * factories ignore it in and the scan executor never
2403
- * touches the per-reducer state construction.
2404
- *
2405
- * **Returns a Promise**, unlike `Query.aggregate().run()` which
2406
- * is synchronous. The scan is inherently async because it walks
2407
- * adapter pages, so the terminal has to be too. Consumers
2408
- * destructure with await:
2409
- *
2410
- * ```ts
2411
- * const { total, n } = await invoices.scan()
2412
- * .where('year', '==', 2025)
2413
- * .aggregate({ total: sum('amount'), n: count() })
2414
- * ```
2415
- *
2416
- * **No `.live()` in.** `scan().aggregate().live()` would
2417
- * require reconciling an unbounded streaming iteration with a
2418
- * change-stream subscription — a design problem, not just a code
2419
- * one. Consumers with huge collections and live needs should
2420
- * narrow with `.where()` enough to fit in the 50k `query()`
2421
- * limit and use `query().aggregate().live()` instead.
2422
- */
2423
- aggregate<Spec extends AggregateSpec>(spec: Spec): Promise<AggregateResult<Spec>>;
2424
- /**
2425
- * Evaluate the clause list against a single record. Linear in
2426
- * the clause count; short-circuits on first false. Clauses on a
2427
- * scan are always re-evaluated per record — no index-accelerated
2428
- * path, because the stream sources records from the adapter
2429
- * paginator, not from the in-memory cache where indexes live.
2430
- */
2431
- private recordMatches;
2432
- }
2433
-
2434
- export { ImportCapabilityError as $, AmendmentForbiddenError as A, BackupCorruptedError as B, ConflictError as C, DictKeyInUseError as D, CrossJoinSourceUnknownError as E, FieldFrozenError as F, CrossJoinTooLargeError as G, DEFAULT_CROSS_JOIN_MAX_ROWS as H, InvariantError as I, DEFAULT_JOIN_MAX_ROWS as J, DanglingReferenceError as K, LocaleNotSpecifiedError as L, MissingTranslationError as M, NoydbError as N, OverlayBaseIsVirtualError as O, PartitionExtractionError as P, Query as Q, ReservedCollectionNameError as R, ScriptViolationError as S, TranslatorNotConfiguredError as T, DecryptionError as U, DelegationTargetMissingError as V, DirectoryDisabledError as W, ElevationExpiredError as X, ExportCapabilityError as Y, FilenameSanitizationError as Z, GroupCardinalityError as _, DictKeyMissingError as a, IndexRequiredError as a0, IndexWriteFailureError as a1, InvalidKeyError as a2, type JoinContext as a3, type JoinLeg as a4, type JoinStrategy as a5, JoinTooLargeError as a6, type JoinableSource as a7, KeyringCorruptError as a8, KeyringExpiredError as a9, type ScanPageProvider as aA, SchemaFenceError as aB, SchemaLockedError as aC, SchemaUpdateError as aD, SchemaValidationError as aE, StoreCapabilityError as aF, TamperedError as aG, TierDemoteDeniedError as aH, TierNotGrantedError as aI, UniqueConstraintError as aJ, UnsupportedIndexOptionError as aK, ValidationError as aL, applyJoins as aM, buildLiveQuery as aN, executePlan as aO, ref as aP, resetJoinWarnings as aQ, LedgerContentionError as aa, type LiveQuery as ab, type LiveUpstream as ac, MigrationRequiredError as ad, NetworkError as ae, NoAccessError as af, NonAdditiveSchemaChangeError as ag, NotFoundError as ah, type OrderBy as ai, PathEscapeError as aj, PeriodClosedError as ak, PermissionDeniedError as al, PrivilegeEscalationError as am, type QueryPlan as an, type QuerySource as ao, QuiesceTimeoutError as ap, ReadOnlyAtInstantError as aq, ReadOnlyError as ar, ReadOnlyFrameError as as, type RefDescriptor as at, RefIntegrityError as au, type RefMode as av, RefRegistry as aw, RefScopeError as ax, type RefViolation as ay, ScanBuilder as az, SessionExpiredError as b, SessionNotFoundError as c, SessionPolicyError as d, RecordLockedError as e, SnapshotNotFoundError as f, DerivationCapExceededError as g, DerivationCycleError as h, DerivationDepthError as i, DerivationOutputShapeError as j, DerivationOutputUnknownError as k, OverlayCollectionUnavailableError as l, OverlayIdMismatchError as m, OverlayNameCollisionError as n, AttestationError as o, AdoptionStateError as p, BackupLedgerError as q, BundleIntegrityError as r, BundleSealMismatchError as s, BundleVersionConflictError as t, TransferSealError as u, MaterializedViewConfigError as v, MaterializedViewCycleError as w, MaterializedViewSourceUnknownError as x, MaterializedViewTooLargeError as y, AlreadyElevatedError as z };