@noy-db/hub 0.2.0-pre.11 → 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.
- package/README.md +126 -0
- package/dist/aggregate/index.cjs +289 -12
- package/dist/aggregate/index.cjs.map +1 -1
- package/dist/aggregate/index.d.cts +2 -2
- package/dist/aggregate/index.d.ts +2 -2
- package/dist/aggregate/index.js +7 -7
- package/dist/aggregate/index.js.map +1 -1
- package/dist/attestation/index.cjs.map +1 -1
- package/dist/attestation/index.d.cts +3 -3
- package/dist/attestation/index.d.ts +3 -3
- package/dist/attestation/index.js +6 -6
- package/dist/blobs/index.cjs +28 -0
- package/dist/blobs/index.cjs.map +1 -1
- package/dist/blobs/index.d.cts +4 -4
- package/dist/blobs/index.d.ts +4 -4
- package/dist/blobs/index.js +5 -5
- package/dist/bundle/index.cjs +1452 -16
- package/dist/bundle/index.cjs.map +1 -1
- package/dist/bundle/index.d.cts +5 -5
- package/dist/bundle/index.d.ts +5 -5
- package/dist/bundle/index.js +9 -9
- package/dist/{chunk-7CEGU63S.js → chunk-4BHFNKTP.js} +2 -2
- package/dist/{chunk-5OEJ6GOT.js → chunk-5ARRXIVR.js} +2 -2
- package/dist/{chunk-77DWLQRY.js → chunk-6AD5TBF2.js} +31 -3
- package/dist/chunk-6AD5TBF2.js.map +1 -0
- package/dist/{chunk-YM7LFCG7.js → chunk-6BYBVRZU.js} +3 -3
- package/dist/{chunk-73YLDCNF.js → chunk-7JJE3OMJ.js} +5 -5
- package/dist/{chunk-GKI4SDP7.js → chunk-7LVRIW4G.js} +4 -4
- package/dist/{chunk-O6EJ6WTI.js → chunk-AGRC7NQQ.js} +62 -2
- package/dist/chunk-AGRC7NQQ.js.map +1 -0
- package/dist/{chunk-IMYKDWB4.js → chunk-B7GGYNKQ.js} +2 -2
- package/dist/{chunk-BDV7INMP.js → chunk-BXOUVUES.js} +4 -4
- package/dist/{chunk-FO3UEG4S.js → chunk-C2CIIQRG.js} +2 -2
- package/dist/{chunk-ZROPXHJY.js → chunk-CHBXWJZQ.js} +2 -2
- package/dist/{chunk-RYIL3PI2.js → chunk-CILT6V3V.js} +2 -2
- package/dist/{chunk-YVZRTCGG.js → chunk-DLTU4M2I.js} +6 -6
- package/dist/{chunk-GAUEWM7D.js → chunk-EKNUBIIQ.js} +4 -4
- package/dist/{chunk-QCXNMCHN.js → chunk-GFPR7VJS.js} +4 -4
- package/dist/{chunk-V2PZC6AW.js → chunk-HBAJDI2N.js} +5 -5
- package/dist/{chunk-PVUUIWHY.js → chunk-HLGDYFWR.js} +10 -3
- package/dist/chunk-HLGDYFWR.js.map +1 -0
- package/dist/{chunk-RRNA5GKT.js → chunk-IEPT7HVP.js} +2 -2
- package/dist/{chunk-R233SLY3.js → chunk-IUBHXEPJ.js} +2 -2
- package/dist/{chunk-CH22FZHT.js → chunk-L6BYRCYB.js} +2 -2
- package/dist/{chunk-PC3ZZBTO.js → chunk-LOA2VCMS.js} +5 -5
- package/dist/{chunk-TGALXXLV.js → chunk-LSEW3ZZ2.js} +3 -3
- package/dist/{chunk-Y26YV5R3.js → chunk-LWSD4QPT.js} +3 -3
- package/dist/{chunk-SLV4LAKX.js → chunk-LYNNZEQD.js} +1 -1
- package/dist/chunk-LYNNZEQD.js.map +1 -0
- package/dist/{chunk-26NK23DZ.js → chunk-M45IRXDM.js} +3 -3
- package/dist/{chunk-CXJG63MA.js → chunk-NP6EZT44.js} +20 -6
- package/dist/chunk-NP6EZT44.js.map +1 -0
- package/dist/{chunk-ZBBW7YQN.js → chunk-O53RIZCC.js} +5 -5
- package/dist/chunk-OPDTLHFA.js +783 -0
- package/dist/chunk-OPDTLHFA.js.map +1 -0
- package/dist/{chunk-LSTBFLL2.js → chunk-P3Z5Y2TS.js} +2 -2
- package/dist/{chunk-QSOYKKMD.js → chunk-P4EDT5ZP.js} +2 -2
- package/dist/{chunk-PC6ZEDRL.js → chunk-RHQYVHFH.js} +2 -2
- package/dist/{chunk-3DGHRDCX.js → chunk-RRDWXNBQ.js} +3 -3
- package/dist/{chunk-6MFH4BMK.js → chunk-SJJQKNMP.js} +4 -4
- package/dist/{chunk-EBVBE7UK.js → chunk-SZ4N3IL5.js} +5 -5
- package/dist/{chunk-MPOLUAMI.js → chunk-TMHJEYW7.js} +497 -57
- package/dist/chunk-TMHJEYW7.js.map +1 -0
- package/dist/{chunk-JQ4NEJJ6.js → chunk-UA6G45ME.js} +3 -3
- package/dist/{chunk-6YLPHBKR.js → chunk-UOC7JMZO.js} +13 -4
- package/dist/chunk-UOC7JMZO.js.map +1 -0
- package/dist/{chunk-DAP2XL7Q.js → chunk-VOXMU6LB.js} +2 -2
- package/dist/chunk-WNRGOVLG.js +64 -0
- package/dist/chunk-WNRGOVLG.js.map +1 -0
- package/dist/{chunk-YW5DBAPG.js → chunk-WUG3E423.js} +4 -4
- package/dist/{chunk-LJXYPGRH.js → chunk-XHM2SARW.js} +3 -3
- package/dist/{chunk-RC6SU5NO.js → chunk-XSIFXX54.js} +2 -2
- package/dist/{chunk-CXFOITNS.js → chunk-ZC7MNVYN.js} +2 -2
- package/dist/{chunk-6T2UDBKG.js → chunk-ZCFS7U4J.js} +2 -2
- package/dist/consent/index.cjs.map +1 -1
- package/dist/consent/index.d.cts +4 -4
- package/dist/consent/index.d.ts +4 -4
- package/dist/consent/index.js +3 -3
- package/dist/{crypto-2CRLG4F4.js → crypto-AJB72OKN.js} +3 -3
- package/dist/{delegation-ZTRT2PRV.js → delegation-6FCWDRUS.js} +5 -5
- package/dist/derivations/index.cjs.map +1 -1
- package/dist/derivations/index.d.cts +5 -5
- package/dist/derivations/index.d.ts +5 -5
- package/dist/derivations/index.js +4 -4
- package/dist/{dev-unlock-BH6y3Hx0.d.ts → dev-unlock-D3mpVFRc.d.ts} +1 -1
- package/dist/{dev-unlock-H1Xwxc3U.d.cts → dev-unlock-ckqa_Nso.d.cts} +1 -1
- package/dist/executor-7KSCEIFA.js +8 -0
- package/dist/executor-D2QMNGRJ.js +8 -0
- package/dist/executor-O5AZK7UW.js +11 -0
- package/dist/{fanout-sidecar-F3ZRFU4H.js → fanout-sidecar-ZSKEQ6NI.js} +2 -2
- package/dist/guards/index.cjs +53 -1
- package/dist/guards/index.cjs.map +1 -1
- package/dist/guards/index.d.cts +12 -6
- package/dist/guards/index.d.ts +12 -6
- package/dist/guards/index.js +5 -3
- package/dist/{hash-D89JdDbj.d.ts → hash-CTZVkXLx.d.ts} +1 -1
- package/dist/{hash-_sDFvtmX.d.cts → hash-rDSSd_oW.d.cts} +1 -1
- package/dist/history/index.cjs.map +1 -1
- package/dist/history/index.d.cts +5 -5
- package/dist/history/index.d.ts +5 -5
- package/dist/history/index.js +5 -5
- package/dist/i18n/index.cjs.map +1 -1
- package/dist/i18n/index.d.cts +4 -4
- package/dist/i18n/index.d.ts +4 -4
- package/dist/i18n/index.js +6 -6
- package/dist/immutable-guard-C51vAHuh.d.cts +67 -0
- package/dist/immutable-guard-DyD0qg2k.d.ts +67 -0
- package/dist/index-CkFHr4OP.d.ts +1190 -0
- package/dist/index-Cmop06zJ.d.cts +1190 -0
- package/dist/index.cjs +1620 -58
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +46 -13
- package/dist/index.d.ts +46 -13
- package/dist/index.js +76 -44
- package/dist/index.js.map +1 -1
- package/dist/indexing/index.cjs.map +1 -1
- package/dist/indexing/index.js +2 -2
- package/dist/issue-YIYG4OW5.js +12 -0
- package/dist/{ledger-NYCGJX2D.js → ledger-5JMVF7PY.js} +5 -5
- package/dist/materialized-views/index.cjs.map +1 -1
- package/dist/materialized-views/index.d.cts +5 -6
- package/dist/materialized-views/index.d.ts +5 -6
- package/dist/materialized-views/index.js +6 -6
- package/dist/noydb-D5SLAJ6V.js +34 -0
- package/dist/overlay-views/index.cjs.map +1 -1
- package/dist/overlay-views/index.d.cts +5 -5
- package/dist/overlay-views/index.d.ts +5 -5
- package/dist/overlay-views/index.js +4 -4
- package/dist/periods/index.cjs.map +1 -1
- package/dist/periods/index.d.cts +4 -4
- package/dist/periods/index.d.ts +4 -4
- package/dist/periods/index.js +5 -5
- package/dist/{public-envelope-QOXZEHKH.js → public-envelope-PFLZI5MO.js} +4 -4
- package/dist/query/index.cjs +293 -10
- package/dist/query/index.cjs.map +1 -1
- package/dist/query/index.d.cts +2 -2
- package/dist/query/index.d.ts +2 -2
- package/dist/query/index.js +4 -4
- package/dist/registry-BVQ5ITMF.js +8 -0
- package/dist/registry-JLP3QOLD.js +8 -0
- package/dist/{registry-ST2VNFZC.js → registry-NCY445U5.js} +3 -3
- package/dist/{revoke-RT7QYB4G.js → revoke-7RLGQWZ7.js} +6 -6
- package/dist/session/index.cjs.map +1 -1
- package/dist/session/index.d.cts +5 -5
- package/dist/session/index.d.ts +5 -5
- package/dist/session/index.js +3 -3
- package/dist/shadow/index.cjs.map +1 -1
- package/dist/shadow/index.d.cts +4 -4
- package/dist/shadow/index.d.ts +4 -4
- package/dist/shadow/index.js +2 -2
- package/dist/{signer-QNU66JF5.js → signer-6JF44I4A.js} +5 -5
- package/dist/snapshots/index.cjs.map +1 -1
- package/dist/snapshots/index.d.cts +4 -4
- package/dist/snapshots/index.d.ts +4 -4
- package/dist/snapshots/index.js +4 -4
- package/dist/{stale-VKXSXJF4.js → stale-UBLP3RJ3.js} +2 -2
- package/dist/store/index.cjs.map +1 -1
- package/dist/store/index.d.cts +4 -4
- package/dist/store/index.d.ts +4 -4
- package/dist/store/index.js +2 -2
- package/dist/strategy-rtpKDfTC.d.cts +2029 -0
- package/dist/strategy-rtpKDfTC.d.ts +2029 -0
- package/dist/sync/index.cjs.map +1 -1
- package/dist/sync/index.d.cts +3 -3
- package/dist/sync/index.d.ts +3 -3
- package/dist/sync/index.js +4 -4
- package/dist/team/index.cjs.map +1 -1
- package/dist/team/index.d.cts +4 -4
- package/dist/team/index.d.ts +4 -4
- package/dist/team/index.js +8 -8
- package/dist/tx/index.cjs +8 -1
- package/dist/tx/index.cjs.map +1 -1
- package/dist/tx/index.d.cts +4 -4
- package/dist/tx/index.d.ts +4 -4
- package/dist/tx/index.js +3 -3
- package/dist/{types-DiSXn3a4.d.cts → types-BGwjsDef.d.cts} +511 -6
- package/dist/{types-CD8mc8zR.d.ts → types-DRdfwgTG.d.ts} +511 -6
- package/dist/{ulid-DQ1hcJvZ.d.cts → ulid-D4d0Xto3.d.cts} +1 -1
- package/dist/{ulid-8p83wbR4.d.ts → ulid-DOTPZ5_h.d.ts} +1 -1
- package/dist/util/index.cjs.map +1 -1
- package/dist/util/index.js +1 -1
- package/dist/vault-group-Z4KB75ZH.js +450 -0
- package/dist/vault-group-Z4KB75ZH.js.map +1 -0
- package/dist/{with-derivation-DWMTpgEH.d.ts → with-derivation-B082Y_WQ.d.ts} +1 -1
- package/dist/{with-derivation-CVT7-dUt.d.cts → with-derivation-CB1EdcFF.d.cts} +1 -1
- package/dist/{with-materialized-view-BTTU3BNK.d.cts → with-materialized-view-CzRg1Dpr.d.cts} +1 -1
- package/dist/{with-materialized-view-X0CoL8-L.d.ts → with-materialized-view-Dw4SwjKl.d.ts} +1 -1
- package/dist/{with-overlayed-view-DcacRRsS.d.cts → with-overlayed-view-C9YFKXzn.d.cts} +1 -1
- package/dist/{with-overlayed-view-DQjO_DSG.d.ts → with-overlayed-view-CaCXeW26.d.ts} +1 -1
- package/package.json +3 -3
- package/dist/chunk-2LPPNWF6.js +0 -340
- package/dist/chunk-2LPPNWF6.js.map +0 -1
- package/dist/chunk-6YLPHBKR.js.map +0 -1
- package/dist/chunk-77DWLQRY.js.map +0 -1
- package/dist/chunk-C3WE6UJY.js +0 -19
- package/dist/chunk-C3WE6UJY.js.map +0 -1
- package/dist/chunk-CXJG63MA.js.map +0 -1
- package/dist/chunk-MPOLUAMI.js.map +0 -1
- package/dist/chunk-O6EJ6WTI.js.map +0 -1
- package/dist/chunk-PVUUIWHY.js.map +0 -1
- package/dist/chunk-SLV4LAKX.js.map +0 -1
- package/dist/executor-S76VN45G.js +0 -8
- package/dist/executor-UCXLIGLW.js +0 -11
- package/dist/executor-ZCNZJMGR.js +0 -8
- package/dist/index-B8bjExET.d.cts +0 -2434
- package/dist/index-DfUbNad8.d.ts +0 -2434
- package/dist/issue-IVTVSKWW.js +0 -12
- package/dist/noydb-SH4RLE47.js +0 -34
- package/dist/registry-UFIK7CSR.js +0 -8
- package/dist/registry-ZGYYSM5I.js +0 -8
- package/dist/strategy-CT2LCKAX.d.cts +0 -613
- package/dist/strategy-CT2LCKAX.d.ts +0 -613
- package/dist/with-guard-BRvt53da.d.ts +0 -18
- package/dist/with-guard-Dx2zZnTA.d.cts +0 -18
- /package/dist/{chunk-7CEGU63S.js.map → chunk-4BHFNKTP.js.map} +0 -0
- /package/dist/{chunk-5OEJ6GOT.js.map → chunk-5ARRXIVR.js.map} +0 -0
- /package/dist/{chunk-YM7LFCG7.js.map → chunk-6BYBVRZU.js.map} +0 -0
- /package/dist/{chunk-73YLDCNF.js.map → chunk-7JJE3OMJ.js.map} +0 -0
- /package/dist/{chunk-GKI4SDP7.js.map → chunk-7LVRIW4G.js.map} +0 -0
- /package/dist/{chunk-IMYKDWB4.js.map → chunk-B7GGYNKQ.js.map} +0 -0
- /package/dist/{chunk-BDV7INMP.js.map → chunk-BXOUVUES.js.map} +0 -0
- /package/dist/{chunk-FO3UEG4S.js.map → chunk-C2CIIQRG.js.map} +0 -0
- /package/dist/{chunk-ZROPXHJY.js.map → chunk-CHBXWJZQ.js.map} +0 -0
- /package/dist/{chunk-RYIL3PI2.js.map → chunk-CILT6V3V.js.map} +0 -0
- /package/dist/{chunk-YVZRTCGG.js.map → chunk-DLTU4M2I.js.map} +0 -0
- /package/dist/{chunk-GAUEWM7D.js.map → chunk-EKNUBIIQ.js.map} +0 -0
- /package/dist/{chunk-QCXNMCHN.js.map → chunk-GFPR7VJS.js.map} +0 -0
- /package/dist/{chunk-V2PZC6AW.js.map → chunk-HBAJDI2N.js.map} +0 -0
- /package/dist/{chunk-RRNA5GKT.js.map → chunk-IEPT7HVP.js.map} +0 -0
- /package/dist/{chunk-R233SLY3.js.map → chunk-IUBHXEPJ.js.map} +0 -0
- /package/dist/{chunk-CH22FZHT.js.map → chunk-L6BYRCYB.js.map} +0 -0
- /package/dist/{chunk-PC3ZZBTO.js.map → chunk-LOA2VCMS.js.map} +0 -0
- /package/dist/{chunk-TGALXXLV.js.map → chunk-LSEW3ZZ2.js.map} +0 -0
- /package/dist/{chunk-Y26YV5R3.js.map → chunk-LWSD4QPT.js.map} +0 -0
- /package/dist/{chunk-26NK23DZ.js.map → chunk-M45IRXDM.js.map} +0 -0
- /package/dist/{chunk-ZBBW7YQN.js.map → chunk-O53RIZCC.js.map} +0 -0
- /package/dist/{chunk-LSTBFLL2.js.map → chunk-P3Z5Y2TS.js.map} +0 -0
- /package/dist/{chunk-QSOYKKMD.js.map → chunk-P4EDT5ZP.js.map} +0 -0
- /package/dist/{chunk-PC6ZEDRL.js.map → chunk-RHQYVHFH.js.map} +0 -0
- /package/dist/{chunk-3DGHRDCX.js.map → chunk-RRDWXNBQ.js.map} +0 -0
- /package/dist/{chunk-6MFH4BMK.js.map → chunk-SJJQKNMP.js.map} +0 -0
- /package/dist/{chunk-EBVBE7UK.js.map → chunk-SZ4N3IL5.js.map} +0 -0
- /package/dist/{chunk-JQ4NEJJ6.js.map → chunk-UA6G45ME.js.map} +0 -0
- /package/dist/{chunk-DAP2XL7Q.js.map → chunk-VOXMU6LB.js.map} +0 -0
- /package/dist/{chunk-YW5DBAPG.js.map → chunk-WUG3E423.js.map} +0 -0
- /package/dist/{chunk-LJXYPGRH.js.map → chunk-XHM2SARW.js.map} +0 -0
- /package/dist/{chunk-RC6SU5NO.js.map → chunk-XSIFXX54.js.map} +0 -0
- /package/dist/{chunk-CXFOITNS.js.map → chunk-ZC7MNVYN.js.map} +0 -0
- /package/dist/{chunk-6T2UDBKG.js.map → chunk-ZCFS7U4J.js.map} +0 -0
- /package/dist/{crypto-2CRLG4F4.js.map → crypto-AJB72OKN.js.map} +0 -0
- /package/dist/{delegation-ZTRT2PRV.js.map → delegation-6FCWDRUS.js.map} +0 -0
- /package/dist/{executor-S76VN45G.js.map → executor-7KSCEIFA.js.map} +0 -0
- /package/dist/{executor-UCXLIGLW.js.map → executor-D2QMNGRJ.js.map} +0 -0
- /package/dist/{executor-ZCNZJMGR.js.map → executor-O5AZK7UW.js.map} +0 -0
- /package/dist/{fanout-sidecar-F3ZRFU4H.js.map → fanout-sidecar-ZSKEQ6NI.js.map} +0 -0
- /package/dist/{issue-IVTVSKWW.js.map → issue-YIYG4OW5.js.map} +0 -0
- /package/dist/{ledger-NYCGJX2D.js.map → ledger-5JMVF7PY.js.map} +0 -0
- /package/dist/{noydb-SH4RLE47.js.map → noydb-D5SLAJ6V.js.map} +0 -0
- /package/dist/{public-envelope-QOXZEHKH.js.map → public-envelope-PFLZI5MO.js.map} +0 -0
- /package/dist/{registry-ST2VNFZC.js.map → registry-BVQ5ITMF.js.map} +0 -0
- /package/dist/{registry-UFIK7CSR.js.map → registry-JLP3QOLD.js.map} +0 -0
- /package/dist/{registry-ZGYYSM5I.js.map → registry-NCY445U5.js.map} +0 -0
- /package/dist/{revoke-RT7QYB4G.js.map → revoke-7RLGQWZ7.js.map} +0 -0
- /package/dist/{signer-QNU66JF5.js.map → signer-6JF44I4A.js.map} +0 -0
- /package/dist/{stale-VKXSXJF4.js.map → stale-UBLP3RJ3.js.map} +0 -0
|
@@ -0,0 +1,1190 @@
|
|
|
1
|
+
import { C as CollectionIndexes, a as Clause, O as Operator } from './predicate-Bt5ft-9c.js';
|
|
2
|
+
import { a7 as NoydbError, au as MoneyDescriptor, A as AggregateStrategy, f as AggregateSpec, g as Aggregation, e as AggregateResult, k as GroupedQuery, l as GroupedQueryN } from './strategy-rtpKDfTC.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Foreign-key references — the soft-FK mechanism.
|
|
6
|
+
*
|
|
7
|
+
* A collection declares its references as metadata at construction
|
|
8
|
+
* time:
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { ref } from '@noy-db/hub'
|
|
12
|
+
*
|
|
13
|
+
* const invoices = company.collection<Invoice>('invoices', {
|
|
14
|
+
* refs: {
|
|
15
|
+
* clientId: ref('clients'), // default: strict
|
|
16
|
+
* categoryId: ref('categories', 'warn'),
|
|
17
|
+
* parentId: ref('invoices', 'cascade'), // self-reference OK
|
|
18
|
+
* },
|
|
19
|
+
* })
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* Three modes:
|
|
23
|
+
*
|
|
24
|
+
* - **strict** — the default. `put()` rejects records whose
|
|
25
|
+
* reference target doesn't exist, and `delete()` of the target
|
|
26
|
+
* rejects if any strict-referencing records still exist.
|
|
27
|
+
* Matches SQL's default FK semantics.
|
|
28
|
+
*
|
|
29
|
+
* - **warn** — both operations succeed unconditionally. Broken
|
|
30
|
+
* references surface only through
|
|
31
|
+
* `vault.checkIntegrity()`, which walks every collection
|
|
32
|
+
* and reports orphans. Use when you want soft validation for
|
|
33
|
+
* imports from messy sources.
|
|
34
|
+
*
|
|
35
|
+
* - **cascade** — `put()` is same as warn. `delete()` of the
|
|
36
|
+
* target deletes every referencing record. Cycles are detected
|
|
37
|
+
* and broken via an in-progress set, so mutual cascades
|
|
38
|
+
* terminate instead of recursing forever.
|
|
39
|
+
*
|
|
40
|
+
* Cross-vault refs are explicitly rejected: if the target
|
|
41
|
+
* name contains a `/`, `ref()` throws `RefScopeError`. Cross-
|
|
42
|
+
* vault refs need an auth story (multi-keyring reads) that
|
|
43
|
+
* doesn't ship — tracked for.
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
/** The three enforcement modes. Default for new refs is `'strict'`. */
|
|
47
|
+
type RefMode = 'strict' | 'warn' | 'cascade';
|
|
48
|
+
/**
|
|
49
|
+
* Descriptor returned by `ref()`. Collections accept a
|
|
50
|
+
* `Record<string, RefDescriptor>` in their options. The key is the
|
|
51
|
+
* field name on the record (top-level only — dotted paths are out of
|
|
52
|
+
* scope), the value describes which target collection the
|
|
53
|
+
* field references and under what mode.
|
|
54
|
+
*
|
|
55
|
+
* The descriptor carries only plain data so it can be serialized,
|
|
56
|
+
* passed around, and introspected without any class machinery.
|
|
57
|
+
*/
|
|
58
|
+
interface RefDescriptor {
|
|
59
|
+
readonly target: string;
|
|
60
|
+
readonly mode: RefMode;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Thrown when a strict reference is violated — either `put()` with a
|
|
64
|
+
* missing target id, or `delete()` of a target that still has
|
|
65
|
+
* strict-referencing records.
|
|
66
|
+
*
|
|
67
|
+
* Carries structured detail so UI code (and a potential future
|
|
68
|
+
* devtools panel) can render "client X cannot be deleted because
|
|
69
|
+
* invoices 1, 2, and 3 reference it" instead of a bare error string.
|
|
70
|
+
*/
|
|
71
|
+
declare class RefIntegrityError extends NoydbError {
|
|
72
|
+
readonly collection: string;
|
|
73
|
+
readonly id: string;
|
|
74
|
+
readonly field: string;
|
|
75
|
+
readonly refTo: string;
|
|
76
|
+
readonly refId: string | null;
|
|
77
|
+
constructor(opts: {
|
|
78
|
+
collection: string;
|
|
79
|
+
id: string;
|
|
80
|
+
field: string;
|
|
81
|
+
refTo: string;
|
|
82
|
+
refId: string | null;
|
|
83
|
+
message: string;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Thrown when `ref()` is called with a target name that looks like
|
|
88
|
+
* a cross-vault reference (contains a `/`). Separate error
|
|
89
|
+
* class because the fix is different: RefIntegrityError means "data
|
|
90
|
+
* is wrong"; RefScopeError means "the ref declaration is wrong".
|
|
91
|
+
*/
|
|
92
|
+
declare class RefScopeError extends NoydbError {
|
|
93
|
+
constructor(target: string);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Helper constructor. Thin wrapper around the object literal so user
|
|
97
|
+
* code reads like `ref('clients')` instead of `{ target: 'clients',
|
|
98
|
+
* mode: 'strict' }` — this is the only ergonomics reason it exists.
|
|
99
|
+
*
|
|
100
|
+
* Validates the target name eagerly so a misconfigured ref declaration
|
|
101
|
+
* fails at collection construction time, not at the first put.
|
|
102
|
+
*/
|
|
103
|
+
declare function ref(target: string, mode?: RefMode): RefDescriptor;
|
|
104
|
+
/**
|
|
105
|
+
* Per-vault registry of reference declarations.
|
|
106
|
+
*
|
|
107
|
+
* The registry is populated by `Collection` constructors (which pass
|
|
108
|
+
* their `refs` option through the Vault) and consulted by the
|
|
109
|
+
* Vault on every `put` / `delete` and by `checkIntegrity`. A
|
|
110
|
+
* single instance lives on the Vault for its lifetime; there's
|
|
111
|
+
* no global state.
|
|
112
|
+
*
|
|
113
|
+
* The data structure is two parallel maps:
|
|
114
|
+
*
|
|
115
|
+
* - `outbound`: `collection → { field → RefDescriptor }` — what
|
|
116
|
+
* refs does `collection` declare? Used on put to check
|
|
117
|
+
* strict-target-exists and on checkIntegrity to walk each
|
|
118
|
+
* collection's outbound refs.
|
|
119
|
+
*
|
|
120
|
+
* - `inbound`: `target → Array<{ collection, field, mode }>` —
|
|
121
|
+
* which collections reference `target`? Used on delete to find
|
|
122
|
+
* the records that might be affected by cascade / strict.
|
|
123
|
+
*
|
|
124
|
+
* The two views are kept in sync by `register()` and never mutated
|
|
125
|
+
* otherwise — refs can't be unregistered at runtime in.
|
|
126
|
+
*/
|
|
127
|
+
declare class RefRegistry {
|
|
128
|
+
private readonly outbound;
|
|
129
|
+
private readonly inbound;
|
|
130
|
+
/**
|
|
131
|
+
* Register the refs declared by a single collection. Idempotent in
|
|
132
|
+
* the happy path — calling twice with the same data is a no-op.
|
|
133
|
+
* Calling twice with DIFFERENT data throws, because silent
|
|
134
|
+
* overrides would be confusing ("I changed the ref and it doesn't
|
|
135
|
+
* update" vs "I declared the same collection twice with different
|
|
136
|
+
* refs and the second call won").
|
|
137
|
+
*/
|
|
138
|
+
register(collection: string, refs: Record<string, RefDescriptor>): void;
|
|
139
|
+
/** Get the outbound refs declared by a collection (or `{}` if none). */
|
|
140
|
+
getOutbound(collection: string): Record<string, RefDescriptor>;
|
|
141
|
+
/** Get the inbound refs that target a given collection (or `[]`). */
|
|
142
|
+
getInbound(target: string): ReadonlyArray<{
|
|
143
|
+
collection: string;
|
|
144
|
+
field: string;
|
|
145
|
+
mode: RefMode;
|
|
146
|
+
}>;
|
|
147
|
+
/**
|
|
148
|
+
* Iterate every (collection → refs) pair that has at least one
|
|
149
|
+
* declared reference. Used by `checkIntegrity` to walk the full
|
|
150
|
+
* universe of outbound refs without needing to track collection
|
|
151
|
+
* names elsewhere.
|
|
152
|
+
*/
|
|
153
|
+
entries(): Array<[string, Record<string, RefDescriptor>]>;
|
|
154
|
+
/** Clear the registry. Test-only escape hatch; never called from production code. */
|
|
155
|
+
clear(): void;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Shape of a single violation reported by `vault.checkIntegrity()`.
|
|
159
|
+
*
|
|
160
|
+
* `refId` is the value we saw in the referencing field — it's the
|
|
161
|
+
* ID we expected to find in `refTo`, but didn't. Left as `unknown`
|
|
162
|
+
* because records are loosely typed at the integrity-check layer.
|
|
163
|
+
*/
|
|
164
|
+
interface RefViolation {
|
|
165
|
+
readonly collection: string;
|
|
166
|
+
readonly id: string;
|
|
167
|
+
readonly field: string;
|
|
168
|
+
readonly refTo: string;
|
|
169
|
+
readonly refId: unknown;
|
|
170
|
+
readonly mode: RefMode;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Query DSL `.join()` — eager, single-FK, intra-vault joins.
|
|
175
|
+
*
|
|
176
|
+
* resolves a ref()-declared foreign key into an attached
|
|
177
|
+
* right-side record under an alias, using one of two planner paths
|
|
178
|
+
* selected automatically:
|
|
179
|
+
*
|
|
180
|
+
* - **nested-loop** — right-side source exposes `lookupById`, so
|
|
181
|
+
* each left row costs O(1). This is the common path for joins
|
|
182
|
+
* against a Collection, which backs `lookupById` with a Map
|
|
183
|
+
* lookup.
|
|
184
|
+
* - **hash** — right-side has only `snapshot()`. Build a
|
|
185
|
+
* `Map<id, record>` once, probe per left row. Same asymptotic
|
|
186
|
+
* cost for our collections, but the path exists as a fallback
|
|
187
|
+
* for custom QuerySource implementations and as an explicit
|
|
188
|
+
* test-only override via `{ strategy: 'hash' }`.
|
|
189
|
+
*
|
|
190
|
+
* Scope:
|
|
191
|
+
*
|
|
192
|
+
* - Equi-joins on declared `ref()` fields only. Joins on
|
|
193
|
+
* undeclared fields throw at plan time with an actionable error
|
|
194
|
+
* naming the field and collection.
|
|
195
|
+
* - Same-vault only. Cross-vault correlation goes
|
|
196
|
+
* through `queryAcross`; this is an architectural
|
|
197
|
+
* invariant, not a limitation we plan to lift.
|
|
198
|
+
* - Hard row ceiling via `JoinTooLargeError` — default 50k per
|
|
199
|
+
* side, override via `{ maxRows }`. Warns at 80% of the ceiling
|
|
200
|
+
* on the existing warn channel.
|
|
201
|
+
* - Three ref-mode behaviors on dangling refs:
|
|
202
|
+
* strict → `DanglingReferenceError`,
|
|
203
|
+
* warn → attach `null` with a one-shot warning,
|
|
204
|
+
* cascade → attach `null` silently (cascade is a delete-time
|
|
205
|
+
* mode; any dangling refs still present at read time are
|
|
206
|
+
* mid-flight cascades or orphans from earlier, not a DSL error).
|
|
207
|
+
*
|
|
208
|
+
* Partition-awareness seam:
|
|
209
|
+
*
|
|
210
|
+
* Every `JoinLeg` carries a `partitionScope` field that is always
|
|
211
|
+
* `'all'` in. The executor never reads this field.
|
|
212
|
+
* partition-aware joins will start populating it from `where()`
|
|
213
|
+
* predicates on the partition key without changing the planner's
|
|
214
|
+
* external shape — this is the whole reason it exists now.
|
|
215
|
+
*
|
|
216
|
+
* Joins stay OUT of the ledger: reads don't touch `_ledger/`,
|
|
217
|
+
* including joined reads.
|
|
218
|
+
*/
|
|
219
|
+
|
|
220
|
+
/** Planner strategy for a single join leg. Auto-selected unless overridden. */
|
|
221
|
+
type JoinStrategy = 'hash' | 'nested';
|
|
222
|
+
/** Default per-side row ceiling before `.join()` throws `JoinTooLargeError`. */
|
|
223
|
+
declare const DEFAULT_JOIN_MAX_ROWS = 50000;
|
|
224
|
+
/**
|
|
225
|
+
* Internal representation of a single join leg in the query plan.
|
|
226
|
+
*
|
|
227
|
+
* This is the primary place where constraint #1 is honored:
|
|
228
|
+
* every leg carries a `partitionScope` field that is always `'all'`
|
|
229
|
+
* in and is never read by the executor. partition-aware
|
|
230
|
+
* joins will start populating it from `where()` predicates on the
|
|
231
|
+
* partition key without changing the planner's external shape.
|
|
232
|
+
*/
|
|
233
|
+
interface JoinLeg {
|
|
234
|
+
/** Field on the left-side record holding the foreign key value. */
|
|
235
|
+
readonly field: string;
|
|
236
|
+
/** Alias key under which the joined right-side record attaches. */
|
|
237
|
+
readonly as: string;
|
|
238
|
+
/** Target collection name, resolved from the `ref()` declaration. */
|
|
239
|
+
readonly target: string;
|
|
240
|
+
/** Ref mode controlling behavior on dangling refs at read time. */
|
|
241
|
+
readonly mode: RefMode;
|
|
242
|
+
/** Manual planner strategy override. `undefined` → auto-select. */
|
|
243
|
+
readonly strategy: JoinStrategy | undefined;
|
|
244
|
+
/** Per-side row ceiling override. `undefined` → DEFAULT_JOIN_MAX_ROWS. */
|
|
245
|
+
readonly maxRows: number | undefined;
|
|
246
|
+
/**
|
|
247
|
+
* Partition scope for future partition-aware joins. Always `'all'`
|
|
248
|
+
* today — the executor never reads this field. Future versions will
|
|
249
|
+
* populate it from `where()` predicates without breaking the
|
|
250
|
+
* planner's external shape. Do not remove even though it looks
|
|
251
|
+
* unused today — that's the whole point of having it.
|
|
252
|
+
*/
|
|
253
|
+
readonly partitionScope: 'all' | readonly string[];
|
|
254
|
+
/**
|
|
255
|
+
* When `true`, this is a dictionary join. The executor
|
|
256
|
+
* resolves the left-field value against the dict snapshot and
|
|
257
|
+
* attaches `{ ...labels, key }` rather than a right-side record.
|
|
258
|
+
* `target` holds the dictionary name (not a collection name).
|
|
259
|
+
*/
|
|
260
|
+
readonly isDictJoin?: true;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Minimal shape of a joinable right-side record source.
|
|
264
|
+
*
|
|
265
|
+
* Collections implement this structurally via their `QuerySource`;
|
|
266
|
+
* sources without `lookupById` force the hash-join fallback. Kept as
|
|
267
|
+
* a thin interface so tests can wire up plain-object sources without
|
|
268
|
+
* pulling in the full Collection class.
|
|
269
|
+
*
|
|
270
|
+
* The optional `subscribe` is used by `Query.live()` to merge
|
|
271
|
+
* right-side change streams into the live re-run trigger. Sources
|
|
272
|
+
* that omit `subscribe` still work for live joins — they just
|
|
273
|
+
* don't drive re-fires when their right side mutates. Collection
|
|
274
|
+
* implements `subscribe` by hooking into the existing per-
|
|
275
|
+
* vault event emitter.
|
|
276
|
+
*/
|
|
277
|
+
interface JoinableSource {
|
|
278
|
+
snapshot(): readonly unknown[];
|
|
279
|
+
lookupById?(id: string): unknown;
|
|
280
|
+
/**
|
|
281
|
+
* Subscribe to mutations on this source. The callback fires
|
|
282
|
+
* AFTER the underlying record set has been updated. Returns an
|
|
283
|
+
* unsubscribe function. Optional — sources without this method
|
|
284
|
+
* cannot trigger live-join re-fires from their side.
|
|
285
|
+
*/
|
|
286
|
+
subscribe?(cb: () => void): () => void;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Join resolution context attached to a `Query` when it's constructed
|
|
290
|
+
* from a `Collection`. Holds everything the `.join()` method needs to
|
|
291
|
+
* translate a field name into a target collection + ref mode, and
|
|
292
|
+
* everything the executor needs to read the right side.
|
|
293
|
+
*
|
|
294
|
+
* Kept as a structural interface so `Vault` can implement it
|
|
295
|
+
* without `Query` needing to import `Vault` (circular-import
|
|
296
|
+
* avoid). The Collection wires this up in its `query()` method using
|
|
297
|
+
* the `joinResolver` back-reference the Vault passes in.
|
|
298
|
+
*/
|
|
299
|
+
interface JoinContext {
|
|
300
|
+
/** Name of the left-side (owning) collection. */
|
|
301
|
+
readonly leftCollection: string;
|
|
302
|
+
/** Look up a `RefDescriptor` by field name on the left collection. */
|
|
303
|
+
resolveRef(field: string): RefDescriptor | null;
|
|
304
|
+
/** Resolve a right-side source by target collection name. */
|
|
305
|
+
resolveSource(collectionName: string): JoinableSource | null;
|
|
306
|
+
/**
|
|
307
|
+
* Resolve a dictKey join source. Returns a `JoinableSource`
|
|
308
|
+
* whose snapshot exposes `{ key, ...labels }` records, keyed by the
|
|
309
|
+
* stable dictionary key. `null` when the field is not a dictKey.
|
|
310
|
+
*
|
|
311
|
+
* The source is built from the compartment's in-memory dictionary
|
|
312
|
+
* snapshot — same data as `DictionaryHandle.list()`, O(1) per lookup.
|
|
313
|
+
*/
|
|
314
|
+
resolveDictSource?(field: string): JoinableSource | null;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Apply every join leg in the plan against a base set of left-side
|
|
318
|
+
* rows. Called by the query executor after `where` / `orderBy` /
|
|
319
|
+
* `offset` / `limit` have narrowed the left set.
|
|
320
|
+
*
|
|
321
|
+
* Each leg attaches a `leg.as` field to every row. Returns a new
|
|
322
|
+
* array of plain objects — the original left rows are not mutated
|
|
323
|
+
* (structural sharing is fine for the inner fields, but the
|
|
324
|
+
* top-level object is a fresh clone so consumers can further mutate
|
|
325
|
+
* safely).
|
|
326
|
+
*
|
|
327
|
+
* **Ordering:** joins run AFTER orderBy / limit / offset in v1.
|
|
328
|
+
* This keeps the planner simple and means queries like "top 10
|
|
329
|
+
* invoices with client" sort and paginate the left side first, then
|
|
330
|
+
* join. Sorting *by* a joined field is out of scope for — users
|
|
331
|
+
* can post-sort the result array in userland or wait for
|
|
332
|
+
* (multi-FK chaining) which can be layered on top.
|
|
333
|
+
*
|
|
334
|
+
* **Multi-FK chaining:** each leg's `maxRows` is enforced
|
|
335
|
+
* against the current left-row count independently. Because
|
|
336
|
+
* joins are equi-joins on the target's primary key (one-to-one or
|
|
337
|
+
* one-to-null), the left row count is constant across legs — no
|
|
338
|
+
* cartesian blowup. The per-leg left-side check is still necessary
|
|
339
|
+
* so that a later leg with a tighter ceiling correctly fires on a
|
|
340
|
+
* query like `.join('a', { maxRows: 100_000 }).join('b', { maxRows: 50 })`,
|
|
341
|
+
* which should throw on the second leg if the left set exceeds 50.
|
|
342
|
+
*/
|
|
343
|
+
declare function applyJoins(rows: readonly unknown[], joins: readonly JoinLeg[], context: JoinContext): unknown[];
|
|
344
|
+
/**
|
|
345
|
+
* Test-only: reset the join warning deduplication state between
|
|
346
|
+
* tests. Production code never calls this — the dedup state is
|
|
347
|
+
* intentionally process-scoped so a noisy query doesn't spam the
|
|
348
|
+
* console once per component render.
|
|
349
|
+
*/
|
|
350
|
+
declare function resetJoinWarnings(): void;
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Reactive query primitive — `query.live()`.
|
|
354
|
+
*
|
|
355
|
+
* produces a `LiveQuery<T>` that re-runs the query and
|
|
356
|
+
* updates its `value` whenever any source feeding it (the left
|
|
357
|
+
* collection AND every right-side collection a join leg points at)
|
|
358
|
+
* mutates.
|
|
359
|
+
*
|
|
360
|
+
* Framework-agnostic by design. The Vue layer wraps a `LiveQuery`
|
|
361
|
+
* in a Vue `Ref<T[]>` by subscribing once and copying `value` into
|
|
362
|
+
* the ref on every notification. React/Solid/Svelte adapters do the
|
|
363
|
+
* same with their own primitives. Core never depends on a UI
|
|
364
|
+
* framework.
|
|
365
|
+
*
|
|
366
|
+
* **Error semantics.** A `.live()` query may throw at re-run time —
|
|
367
|
+
* a strict-mode `DanglingReferenceError` is the most common case
|
|
368
|
+
* (a right-side record was deleted out-of-band, leaving a left
|
|
369
|
+
* row's FK pointing at nothing). When the re-run throws, the
|
|
370
|
+
* `LiveQuery` catches the error and stores it in the `error`
|
|
371
|
+
* field; it does NOT propagate the throw out of the source's
|
|
372
|
+
* change handler, because doing so would tear down whatever
|
|
373
|
+
* upstream emitter is dispatching. Listeners check `error` after
|
|
374
|
+
* each notification and render an error state in the UI.
|
|
375
|
+
*
|
|
376
|
+
* **Dedup of right-side subscriptions.** A multi-FK chain that
|
|
377
|
+
* joins the same target twice (e.g.
|
|
378
|
+
* `.join('billingClientId').join('shippingClientId')`, both
|
|
379
|
+
* pointing at `clients`) only subscribes to that target once. We
|
|
380
|
+
* dedup by target collection name, on the assumption that
|
|
381
|
+
* `resolveSource(name)` returns a single subscribable source per
|
|
382
|
+
* vault + name. Vault's `resolveSource` reads from
|
|
383
|
+
* `collectionCache` so this assumption holds.
|
|
384
|
+
*
|
|
385
|
+
* **What .live() does NOT do in v1:**
|
|
386
|
+
* - No granular delta updates — the whole query re-runs on every
|
|
387
|
+
* change. Granular delta tracking is a v2 optimization once
|
|
388
|
+
* the API is stable.
|
|
389
|
+
* - No batching of bursty changes — one event in, one re-run
|
|
390
|
+
* out. Batching with microtask coalescing is a v2 enhancement.
|
|
391
|
+
* - No async notifications — every notification is synchronous
|
|
392
|
+
* within the source's change handler.
|
|
393
|
+
* - No re-planning under live mutations — the planner picks once
|
|
394
|
+
* at subscription time and reuses the same plan for every
|
|
395
|
+
* re-run.
|
|
396
|
+
*/
|
|
397
|
+
/**
|
|
398
|
+
* The reactive primitive returned by `Query.live()`.
|
|
399
|
+
*
|
|
400
|
+
* Listeners can read the current `value` snapshot at any time and
|
|
401
|
+
* subscribe to changes via `.subscribe(cb)`. The `error` field
|
|
402
|
+
* carries the most recent re-run error, if any — read it after
|
|
403
|
+
* each notification to render error state.
|
|
404
|
+
*
|
|
405
|
+
* Always call `stop()` when the live query is no longer needed.
|
|
406
|
+
* Without it, the upstream change-stream subscriptions stay live
|
|
407
|
+
* forever and the query keeps re-running on every mutation.
|
|
408
|
+
*/
|
|
409
|
+
interface LiveQuery<T> {
|
|
410
|
+
/**
|
|
411
|
+
* Current snapshot of the query result. Updated in place on
|
|
412
|
+
* every upstream change. The reference returned is the same
|
|
413
|
+
* `readonly T[]` array — consumers that want change detection by
|
|
414
|
+
* reference should copy: `const arr = [...live.value]`.
|
|
415
|
+
*/
|
|
416
|
+
readonly value: readonly T[];
|
|
417
|
+
/**
|
|
418
|
+
* Most recent re-run error, or `null` on success. Set when the
|
|
419
|
+
* executor throws (e.g. `DanglingReferenceError` in strict mode
|
|
420
|
+
* after a right-side delete). Cleared on the next successful
|
|
421
|
+
* re-run.
|
|
422
|
+
*/
|
|
423
|
+
readonly error: Error | null;
|
|
424
|
+
/**
|
|
425
|
+
* Register a notification callback. Fires AFTER `value` and
|
|
426
|
+
* `error` have been updated for a given upstream change.
|
|
427
|
+
* Returns an unsubscribe function.
|
|
428
|
+
*
|
|
429
|
+
* The first call to `subscribe` does NOT fire the callback
|
|
430
|
+
* immediately — call sites that want the initial value should
|
|
431
|
+
* read `live.value` directly before subscribing.
|
|
432
|
+
*/
|
|
433
|
+
subscribe(cb: () => void): () => void;
|
|
434
|
+
/**
|
|
435
|
+
* Tear down every upstream subscription and clear the listener
|
|
436
|
+
* set. Idempotent — calling twice is safe. After `stop()`, the
|
|
437
|
+
* query no longer re-runs and `subscribe()` becomes a no-op
|
|
438
|
+
* (the returned unsubscribe is still callable and is also a
|
|
439
|
+
* no-op).
|
|
440
|
+
*/
|
|
441
|
+
stop(): void;
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Internal subscription handle for an upstream source — left or
|
|
445
|
+
* right side. The contract is just `subscribe(cb): unsubscribe`,
|
|
446
|
+
* matching the existing `QuerySource.subscribe` and the new
|
|
447
|
+
* `JoinableSource.subscribe` (added in ).
|
|
448
|
+
*/
|
|
449
|
+
interface LiveUpstream {
|
|
450
|
+
subscribe(cb: () => void): () => void;
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Build a LiveQuery from a `recompute` callback (typically the
|
|
454
|
+
* Query's bound `toArray`) and a list of upstream sources to
|
|
455
|
+
* subscribe to.
|
|
456
|
+
*
|
|
457
|
+
* The recompute fires once synchronously to populate the initial
|
|
458
|
+
* value, then re-fires every time any upstream notifies. Errors
|
|
459
|
+
* thrown by recompute are caught and stored in `error` instead of
|
|
460
|
+
* propagating — see the file docstring for the rationale.
|
|
461
|
+
*/
|
|
462
|
+
declare function buildLiveQuery<T>(recompute: () => T[], upstreams: readonly LiveUpstream[]): LiveQuery<T>;
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Chainable, immutable query builder.
|
|
466
|
+
*
|
|
467
|
+
* Each builder operation returns a NEW Query — the underlying plan is never
|
|
468
|
+
* mutated. This makes plans safe to share, cache, and serialize.
|
|
469
|
+
*/
|
|
470
|
+
|
|
471
|
+
interface OrderBy {
|
|
472
|
+
readonly field: string;
|
|
473
|
+
readonly direction: 'asc' | 'desc';
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* A complete query plan: zero-or-more clauses, optional ordering, pagination,
|
|
477
|
+
* and optional joins.
|
|
478
|
+
*
|
|
479
|
+
* Plans are JSON-serializable as long as no FilterClause is present and no
|
|
480
|
+
* join leg carries a manual `strategy` override (JoinLeg itself is plain
|
|
481
|
+
* data, so it serializes cleanly).
|
|
482
|
+
*
|
|
483
|
+
* Plans are intentionally NOT parametric on T — see `predicate.ts` FilterClause
|
|
484
|
+
* for the variance reasoning. The public `Query<T>` API attaches the type tag.
|
|
485
|
+
*/
|
|
486
|
+
interface QueryPlan {
|
|
487
|
+
readonly clauses: readonly Clause[];
|
|
488
|
+
readonly orderBy: readonly OrderBy[];
|
|
489
|
+
readonly limit: number | undefined;
|
|
490
|
+
readonly offset: number;
|
|
491
|
+
/**
|
|
492
|
+
* Zero-or-more join legs to apply after where/orderBy/limit/offset.
|
|
493
|
+
* Each leg attaches a resolved right-side record (or null) under its
|
|
494
|
+
* alias. See `query/join.ts` for the full semantics.
|
|
495
|
+
*/
|
|
496
|
+
readonly joins: readonly JoinLeg[];
|
|
497
|
+
}
|
|
498
|
+
/** Default row ceiling for cross-join expansion. Matches JoinTooLargeError's ceiling. */
|
|
499
|
+
declare const DEFAULT_CROSS_JOIN_MAX_ROWS = 50000;
|
|
500
|
+
/**
|
|
501
|
+
* Source of records that a query executes against.
|
|
502
|
+
*
|
|
503
|
+
* The interface is non-parametric to keep variance friendly: callers cast
|
|
504
|
+
* their typed source (e.g. `QuerySource<Invoice>`) into this opaque shape.
|
|
505
|
+
*
|
|
506
|
+
* `getIndexes` and `lookupById` are optional fast-path hooks. When both are
|
|
507
|
+
* present and a where clause matches an indexed field, the executor uses
|
|
508
|
+
* the index to skip a linear scan. Sources without these methods (or with
|
|
509
|
+
* `getIndexes` returning `null`) always fall back to a linear scan.
|
|
510
|
+
*/
|
|
511
|
+
interface QuerySource<T> {
|
|
512
|
+
/** Snapshot of all current records. The query never mutates this array. */
|
|
513
|
+
snapshot(): readonly T[];
|
|
514
|
+
/** Subscribe to mutations; returns an unsubscribe function. */
|
|
515
|
+
subscribe?(cb: () => void): () => void;
|
|
516
|
+
/** Index store for the indexed-fast-path. Optional. */
|
|
517
|
+
getIndexes?(): CollectionIndexes | null;
|
|
518
|
+
/** O(1) record lookup by id, used to materialize index hits. */
|
|
519
|
+
lookupById?(id: string): T | undefined;
|
|
520
|
+
/**
|
|
521
|
+
* Money field descriptors for the backing collection, used to rewrite
|
|
522
|
+
* `sum`/`min`/`max` over money fields into exact BigInt reducers.
|
|
523
|
+
*/
|
|
524
|
+
moneyFields?: Record<string, MoneyDescriptor>;
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* The chainable builder. All methods return a new Query — the original
|
|
528
|
+
* remains unchanged. Terminal methods (`toArray`, `first`, `count`,
|
|
529
|
+
* `subscribe`) execute the plan against the source.
|
|
530
|
+
*
|
|
531
|
+
* Type parameter T flows through the public API for ergonomics, but the
|
|
532
|
+
* internal storage uses `unknown` so Collection<T> stays covariant.
|
|
533
|
+
*
|
|
534
|
+
* The optional `joinContext` is attached when the Query is constructed
|
|
535
|
+
* via `Collection.query()` (Collection passes in a context built from
|
|
536
|
+
* the Vault's join resolver). A Query constructed via `new Query`
|
|
537
|
+
* directly — e.g. from tests with a plain-object source — has no
|
|
538
|
+
* joinContext, and calling `.join()` on it throws with an actionable
|
|
539
|
+
* error. See `query/join.ts` for the full design.
|
|
540
|
+
*/
|
|
541
|
+
/**
|
|
542
|
+
* Declared deterministic predicate. Carries the consumer's
|
|
543
|
+
* stable `hash` (for function-body identity), the function itself,
|
|
544
|
+
* and is keyed by name when registered on a `Query<T>` via
|
|
545
|
+
* `_withPredicates()`.
|
|
546
|
+
*/
|
|
547
|
+
interface DeclaredPredicate {
|
|
548
|
+
hash: string;
|
|
549
|
+
fn: (record: unknown, ctx?: unknown) => boolean;
|
|
550
|
+
}
|
|
551
|
+
declare class Query<T> {
|
|
552
|
+
private readonly source;
|
|
553
|
+
private readonly plan;
|
|
554
|
+
private readonly joinContext;
|
|
555
|
+
private readonly aggregateStrategy;
|
|
556
|
+
private readonly predicates;
|
|
557
|
+
constructor(source: QuerySource<T>, plan?: QueryPlan, joinContext?: JoinContext, aggregateStrategy?: AggregateStrategy, predicates?: ReadonlyMap<string, DeclaredPredicate>);
|
|
558
|
+
/**
|
|
559
|
+
* @internal — accessor for the materialized-view dependency
|
|
560
|
+
* analyzer. Not part of the public API; consumers should use the
|
|
561
|
+
* builder methods, not inspect the plan directly.
|
|
562
|
+
*/
|
|
563
|
+
_plan(): QueryPlan;
|
|
564
|
+
/**
|
|
565
|
+
* @internal — accessor for the materialized-view dependency
|
|
566
|
+
* analyzer. Returns the join resolution context (or `undefined` for
|
|
567
|
+
* queries constructed without a Collection backing).
|
|
568
|
+
*/
|
|
569
|
+
_joinContext(): JoinContext | undefined;
|
|
570
|
+
/**
|
|
571
|
+
* @internal — clone this Query with a declared-predicate map
|
|
572
|
+
* attached. Used by the materialized-view registry to enable
|
|
573
|
+
* `.wherePredicate(name, ctx?)` for the MV's query callback.
|
|
574
|
+
* Consumers don't call this directly.
|
|
575
|
+
*/
|
|
576
|
+
_withPredicates(predicates: ReadonlyMap<string, DeclaredPredicate>): Query<T>;
|
|
577
|
+
/**
|
|
578
|
+
* Filter by a registered deterministic predicate. Requires
|
|
579
|
+
* the Query to have been augmented with a predicates map (typically
|
|
580
|
+
* via the materialized-view registry — bare Queries constructed
|
|
581
|
+
* outside an MV throw on `.wherePredicate()`).
|
|
582
|
+
*
|
|
583
|
+
* `ctx` is an optional opaque value passed verbatim to the predicate
|
|
584
|
+
* function. Both `predicateHash` (from the registration) and a
|
|
585
|
+
* canonical-JSON hash of `ctx` fold into the MV's `queryHash`, so
|
|
586
|
+
* either changing forces refresh on next visit.
|
|
587
|
+
*/
|
|
588
|
+
wherePredicate(name: string, ctx?: unknown): Query<T>;
|
|
589
|
+
/** Add a field comparison. Multiple where() calls are AND-combined. */
|
|
590
|
+
where(field: string, op: Operator, value: unknown): Query<T>;
|
|
591
|
+
/**
|
|
592
|
+
* Logical OR group. Pass a callback that builds a sub-query.
|
|
593
|
+
* Each clause inside the callback is OR-combined; the group itself
|
|
594
|
+
* joins the parent plan with AND.
|
|
595
|
+
*/
|
|
596
|
+
or(builder: (q: Query<T>) => Query<T>): Query<T>;
|
|
597
|
+
/**
|
|
598
|
+
* Logical AND group. Same shape as `or()` but every clause inside the group
|
|
599
|
+
* must match. Useful for explicit grouping inside a larger OR.
|
|
600
|
+
*/
|
|
601
|
+
and(builder: (q: Query<T>) => Query<T>): Query<T>;
|
|
602
|
+
/** Escape hatch: add an arbitrary predicate function. Not serializable. */
|
|
603
|
+
filter(fn: (record: T) => boolean): Query<T>;
|
|
604
|
+
/** Sort by a field. Subsequent calls are tie-breakers. */
|
|
605
|
+
orderBy(field: string, direction?: 'asc' | 'desc'): Query<T>;
|
|
606
|
+
/** Cap the result size. */
|
|
607
|
+
limit(n: number): Query<T>;
|
|
608
|
+
/** Skip the first N matching records (after ordering). */
|
|
609
|
+
offset(n: number): Query<T>;
|
|
610
|
+
/**
|
|
611
|
+
* Resolve a `ref()`-declared foreign key and attach the right-side
|
|
612
|
+
* record under `opts.as`. — eager, single-FK, intra-
|
|
613
|
+
* vault joins.
|
|
614
|
+
*
|
|
615
|
+
* ```ts
|
|
616
|
+
* const rows = invoices.query()
|
|
617
|
+
* .where('status', '==', 'open')
|
|
618
|
+
* .join('clientId', { as: 'client' })
|
|
619
|
+
* .toArray()
|
|
620
|
+
* // → [{ id, amount, client: { id, name, ... } }, ...]
|
|
621
|
+
* ```
|
|
622
|
+
*
|
|
623
|
+
* Preconditions:
|
|
624
|
+
* - The Query must have a `joinContext` (constructed via
|
|
625
|
+
* `Collection.query()`, not `new Query`).
|
|
626
|
+
* - `field` must have a matching `refs: { [field]: ref('<target>') }`
|
|
627
|
+
* declaration on the left collection.
|
|
628
|
+
* - The target collection must be reachable via the vault
|
|
629
|
+
* (either currently open or openable on demand).
|
|
630
|
+
*
|
|
631
|
+
* Strategy:
|
|
632
|
+
* - Nested-loop against `lookupById` when the target source
|
|
633
|
+
* provides it (the common path for Collection targets).
|
|
634
|
+
* - Hash join otherwise, or when `{ strategy: 'hash' }` is
|
|
635
|
+
* explicitly passed for test purposes.
|
|
636
|
+
*
|
|
637
|
+
* Ref-mode semantics on dangling refs (left record has a non-null
|
|
638
|
+
* FK value pointing at a right-side id that doesn't exist):
|
|
639
|
+
* - `strict` → throws `DanglingReferenceError` with the full
|
|
640
|
+
* field / target / refId context.
|
|
641
|
+
* - `warn` → attaches `null` and emits a one-shot warning per
|
|
642
|
+
* unique dangling pair.
|
|
643
|
+
* - `cascade` → attaches `null` silently. Cascade is a
|
|
644
|
+
* delete-time mode; dangling refs visible at read time are
|
|
645
|
+
* either mid-flight cascades or pre-existing orphans, not a
|
|
646
|
+
* DSL-level error.
|
|
647
|
+
*
|
|
648
|
+
* A left-side record whose FK field is `null` / `undefined` is NOT
|
|
649
|
+
* a dangling ref — it's "no reference at all", always allowed
|
|
650
|
+
* regardless of mode.
|
|
651
|
+
*
|
|
652
|
+
* The return type widens `T` with `Record<As, R | null>`. The `R`
|
|
653
|
+
* parameter is optional — supply it explicitly for type-checked
|
|
654
|
+
* access to the joined fields:
|
|
655
|
+
*
|
|
656
|
+
* ```ts
|
|
657
|
+
* invoices.query().join<'client', Client>('clientId', { as: 'client' })
|
|
658
|
+
* // ^^^^^^^^^^^^^^^^^^^ alias literal + right-side type
|
|
659
|
+
* ```
|
|
660
|
+
*
|
|
661
|
+
* Without the generic, the joined field is typed as `unknown`, which
|
|
662
|
+
* still works but requires a cast to access its properties.
|
|
663
|
+
*
|
|
664
|
+
* Joins stay intra-vault by construction — cross-vault
|
|
665
|
+
* correlation goes through `Noydb.queryAcross`, not
|
|
666
|
+
* `.join()`.
|
|
667
|
+
*/
|
|
668
|
+
join<As extends string, R = unknown>(field: string, opts: {
|
|
669
|
+
as: As;
|
|
670
|
+
strategy?: JoinStrategy;
|
|
671
|
+
maxRows?: number;
|
|
672
|
+
}): Query<T & Record<As, R | null>>;
|
|
673
|
+
/**
|
|
674
|
+
* Cartesian-product cross-join against `target` collection. Each result row
|
|
675
|
+
* carries the original `T` fields plus `result[as]` populated from every
|
|
676
|
+
* right-side row (or the filtered subset when `on:` is supplied).
|
|
677
|
+
*
|
|
678
|
+
* **Order matters:** `.where().crossJoin()` filters BEFORE expanding (cheaper);
|
|
679
|
+
* `.crossJoin().where('alias.field', ...)` filters AFTER (required when the
|
|
680
|
+
* where clause references the aliased fields).
|
|
681
|
+
*
|
|
682
|
+
* **Cost ceiling:** `CrossJoinTooLargeError` fires before allocation when
|
|
683
|
+
* `leftRows × rightRows` (or the cumulative lateral count) exceeds the limit.
|
|
684
|
+
* Default: 50,000 rows. Override per-clause with `{ maxRows: N }`.
|
|
685
|
+
*
|
|
686
|
+
* **`on:` shapes:**
|
|
687
|
+
* - `on: (left) => TTarget[]` — subset form (most efficient)
|
|
688
|
+
* - `on: (left) => (right) => boolean` — predicate form
|
|
689
|
+
* - `on: { predicate: 'name' }` — MV-safe, hash-tracked form
|
|
690
|
+
* (requires the Query to have been augmented via `_withPredicates`)
|
|
691
|
+
*
|
|
692
|
+
* Requires a JoinContext (constructed via `collection.query()`).
|
|
693
|
+
*/
|
|
694
|
+
crossJoin<TTarget = unknown, As extends string = string>(target: string, opts: {
|
|
695
|
+
as: As;
|
|
696
|
+
on?: ((left: T) => unknown[] | ((right: TTarget) => boolean)) | {
|
|
697
|
+
readonly predicate: string;
|
|
698
|
+
};
|
|
699
|
+
maxRows?: number;
|
|
700
|
+
}): Query<T & {
|
|
701
|
+
[K in As]: TTarget;
|
|
702
|
+
}>;
|
|
703
|
+
/**
|
|
704
|
+
* Execute the plan and return the matching records. When the plan
|
|
705
|
+
* carries any join legs, they are applied after `where` / `orderBy`
|
|
706
|
+
* / `limit` / `offset` narrow the left set. See the `.join()` doc
|
|
707
|
+
* for the ordering rationale.
|
|
708
|
+
*/
|
|
709
|
+
toArray(): T[];
|
|
710
|
+
/** Return the first matching record, or null. Joins are applied. */
|
|
711
|
+
first(): T | null;
|
|
712
|
+
/**
|
|
713
|
+
* Return the number of matching records (after where/filter,
|
|
714
|
+
* before limit). **Joins are NOT applied** — count() reports the
|
|
715
|
+
* left-side cardinality, because joins in are projection-only
|
|
716
|
+
* (they attach an aliased field; they never filter). Running joins
|
|
717
|
+
* here just to discard the aliases would be wasteful, and in strict
|
|
718
|
+
* mode it could throw `DanglingReferenceError` for a call whose
|
|
719
|
+
* intent is purely to count.
|
|
720
|
+
*/
|
|
721
|
+
count(): number;
|
|
722
|
+
/**
|
|
723
|
+
* Reduce the matching records through a named set of reducers.
|
|
724
|
+
* the aggregation terminal.
|
|
725
|
+
*
|
|
726
|
+
* ```ts
|
|
727
|
+
* const { total, n, avgAmount } = invoices.query()
|
|
728
|
+
* .where('status', '==', 'open')
|
|
729
|
+
* .aggregate({
|
|
730
|
+
* total: sum('amount'),
|
|
731
|
+
* n: count(),
|
|
732
|
+
* avgAmount: avg('amount'),
|
|
733
|
+
* })
|
|
734
|
+
* .run()
|
|
735
|
+
* ```
|
|
736
|
+
*
|
|
737
|
+
* Returns an `Aggregation<R>` wrapper with two terminals:
|
|
738
|
+
* - `.run(): R` — synchronous one-shot reduction
|
|
739
|
+
* - `.live(): LiveAggregation<R>` — reactive primitive that
|
|
740
|
+
* re-runs the reduction whenever the source notifies of a
|
|
741
|
+
* change. Always call `live.stop()` when finished.
|
|
742
|
+
*
|
|
743
|
+
* The reducer spec is bound here once and reused by both
|
|
744
|
+
* terminals — this is why `.aggregate()` returns a wrapper instead
|
|
745
|
+
* of being a direct terminal. Consumers who only need the static
|
|
746
|
+
* value read `.run()`; consumers wiring a reactive UI read
|
|
747
|
+
* `.live()`.
|
|
748
|
+
*
|
|
749
|
+
* Joins are intentionally NOT applied to aggregations in —
|
|
750
|
+
* the same logic as `.count()`. Joins in are projection-only
|
|
751
|
+
* (they attach an aliased field and never filter), so running
|
|
752
|
+
* them just to throw the aliases away would be wasteful. If you
|
|
753
|
+
* need a reducer that reads a joined field, open an issue —
|
|
754
|
+
* aggregations-across-joins is explicitly out of scope for v1.
|
|
755
|
+
*
|
|
756
|
+
* Every reducer factory accepts an optional `{ seed }` parameter
|
|
757
|
+
* that is plumbed through the protocol but unused by the
|
|
758
|
+
* executor — that's constraint #2. When partition-aware
|
|
759
|
+
* aggregation lands, the seed will carry running state across
|
|
760
|
+
* partition boundaries without an API break.
|
|
761
|
+
*/
|
|
762
|
+
aggregate<Spec extends AggregateSpec>(spec: Spec): Aggregation<AggregateResult<Spec>>;
|
|
763
|
+
/**
|
|
764
|
+
* Partition matching records into buckets keyed by a field, then
|
|
765
|
+
* terminate with `.aggregate(spec)` to compute per-bucket
|
|
766
|
+
* reducers..
|
|
767
|
+
*
|
|
768
|
+
* ```ts
|
|
769
|
+
* const byClient = invoices.query()
|
|
770
|
+
* .where('status', '==', 'open')
|
|
771
|
+
* .groupBy('clientId')
|
|
772
|
+
* .aggregate({ total: sum('amount'), n: count() })
|
|
773
|
+
* .run()
|
|
774
|
+
* // → [ { clientId: 'c1', total: 5250, n: 3 }, … ]
|
|
775
|
+
* ```
|
|
776
|
+
*
|
|
777
|
+
* Result rows carry the group key value under the grouping field
|
|
778
|
+
* name plus every reducer output from the spec. Buckets are
|
|
779
|
+
* emitted in first-seen order — consumers who want a specific
|
|
780
|
+
* ordering should `.sort()` downstream.
|
|
781
|
+
*
|
|
782
|
+
* **Cardinality caps:** a one-shot warning fires at 10_000
|
|
783
|
+
* distinct groups; `GroupCardinalityError` throws at 100_000.
|
|
784
|
+
* Grouping on a high-uniqueness field like `id` or `createdAt` is
|
|
785
|
+
* almost always a query mistake — the error message names the
|
|
786
|
+
* field and observed cardinality and suggests narrowing with
|
|
787
|
+
* `.where()` first.
|
|
788
|
+
*
|
|
789
|
+
* **Null / undefined keys:** records with a missing or explicitly
|
|
790
|
+
* `null` group field get their own buckets. `Map`-based
|
|
791
|
+
* partitioning distinguishes `undefined` from `null`, so the two
|
|
792
|
+
* cases do NOT merge. Consumers who want them merged should
|
|
793
|
+
* coalesce upstream with `.filter()`.
|
|
794
|
+
*
|
|
795
|
+
* **Joins are not applied** — same rationale as `.count()` and
|
|
796
|
+
* `.aggregate()`. Joined fields in are projection-only, so
|
|
797
|
+
* running a join inside a grouping pipeline would be wasteful and
|
|
798
|
+
* could trigger `DanglingReferenceError` in strict mode for a
|
|
799
|
+
* call whose intent is purely to bucket-and-reduce. Grouping by
|
|
800
|
+
* a joined field is explicitly out of scope for — file an
|
|
801
|
+
* issue if a real consumer needs it.
|
|
802
|
+
*
|
|
803
|
+
* **Filter clauses (`.filter(fn)`):** grouped queries still
|
|
804
|
+
* support filter clauses in the underlying plan — they run in
|
|
805
|
+
* the same candidate/filter pipeline that `.aggregate()` uses.
|
|
806
|
+
* The performance caveat is the same: filter clauses cost O(N)
|
|
807
|
+
* per record and can't be index-accelerated.
|
|
808
|
+
*/
|
|
809
|
+
groupBy<F extends string>(field: F): GroupedQuery<T, F>;
|
|
810
|
+
groupBy<F extends readonly [string, string, ...string[]]>(...fields: F): GroupedQueryN<T, F>;
|
|
811
|
+
/**
|
|
812
|
+
* Re-run the query whenever the source notifies of changes.
|
|
813
|
+
* Returns an unsubscribe function. The callback receives the latest result.
|
|
814
|
+
* Throws if the source does not support subscriptions.
|
|
815
|
+
*
|
|
816
|
+
* **For joined queries, prefer `.live()`** — `subscribe()`
|
|
817
|
+
* only re-fires on LEFT-side changes, so joined data can be
|
|
818
|
+
* stale if the right side mutates between emissions. `.live()`
|
|
819
|
+
* merges change streams from every join target.
|
|
820
|
+
*/
|
|
821
|
+
subscribe(cb: (result: T[]) => void): () => void;
|
|
822
|
+
/**
|
|
823
|
+
* Reactive terminal — returns a `LiveQuery<T>` that re-runs the
|
|
824
|
+
* query and updates its `value` whenever any source feeding it
|
|
825
|
+
* mutates..
|
|
826
|
+
*
|
|
827
|
+
* For non-joined queries, `.live()` is a convenience over the
|
|
828
|
+
* existing `.subscribe()` callback shape: a hand-rolled reactive
|
|
829
|
+
* primitive with `value` / `error` fields and a `subscribe(cb)`
|
|
830
|
+
* notification channel. Frame-agnostic — Vue / React / Solid
|
|
831
|
+
* adapters wrap it in their own primitive.
|
|
832
|
+
*
|
|
833
|
+
* For joined queries, `.live()` additionally subscribes to every
|
|
834
|
+
* join target's change stream. Mutations on a right-side
|
|
835
|
+
* collection (insert / update / delete of a client referenced by
|
|
836
|
+
* an invoice) re-fire the live query and re-evaluate every
|
|
837
|
+
* dependent left row. Right-side targets are deduped by
|
|
838
|
+
* collection name, so a chain that joins the same target twice
|
|
839
|
+
* (e.g. billing client + shipping client → both 'clients') only
|
|
840
|
+
* subscribes once.
|
|
841
|
+
*
|
|
842
|
+
* **Ref-mode behavior on right-side disappearance** — matches the
|
|
843
|
+
* eager `.toArray()` contract from :
|
|
844
|
+
* - `strict` → re-run throws `DanglingReferenceError`. The
|
|
845
|
+
* LiveQuery catches the throw, stores it in `live.error`, and
|
|
846
|
+
* notifies listeners (the throw does NOT propagate out of
|
|
847
|
+
* the source's change handler — that would tear down the
|
|
848
|
+
* emitter). Consumers check `live.error` after each
|
|
849
|
+
* notification and render an error state in the UI.
|
|
850
|
+
* - `warn` → joined value flips to `null`; the existing
|
|
851
|
+
* warn-channel deduplication keeps repeated re-runs from
|
|
852
|
+
* spamming the console.
|
|
853
|
+
* - `cascade` → no special handling needed; the cascade-
|
|
854
|
+
* delete mechanism propagates the right-side delete into the
|
|
855
|
+
* left collection on the next tick, and the live query
|
|
856
|
+
* naturally re-fires with the orphaned left rows gone.
|
|
857
|
+
*
|
|
858
|
+
* Always call `live.stop()` when finished — it tears down every
|
|
859
|
+
* upstream subscription. The Vue layer's `onUnmounted` hook
|
|
860
|
+
* should call `stop()` automatically; raw consumers must do it
|
|
861
|
+
* themselves.
|
|
862
|
+
*
|
|
863
|
+
* **Limitations:**
|
|
864
|
+
* - No granular delta updates — the whole query re-runs on
|
|
865
|
+
* every change.
|
|
866
|
+
* - No microtask batching — bursty changes produce one re-run
|
|
867
|
+
* per change.
|
|
868
|
+
* - No re-planning under live mutations — the planner picks
|
|
869
|
+
* once at subscription time and reuses the same plan.
|
|
870
|
+
* - Streaming live joins are deferred.
|
|
871
|
+
*/
|
|
872
|
+
live(): LiveQuery<T>;
|
|
873
|
+
/**
|
|
874
|
+
* Return the plan as a JSON-friendly object. FilterClause entries are
|
|
875
|
+
* stripped (their `fn` cannot be serialized) and replaced with
|
|
876
|
+
* { type: 'filter', fn: '[function]' } so devtools can still see them.
|
|
877
|
+
*/
|
|
878
|
+
toPlan(): unknown;
|
|
879
|
+
}
|
|
880
|
+
/**
|
|
881
|
+
* Execute a plan against a snapshot of records.
|
|
882
|
+
* Pure function — same input, same output, no side effects.
|
|
883
|
+
*
|
|
884
|
+
* Records are typed as `unknown` because plans are non-parametric; callers
|
|
885
|
+
* cast the return type at the API surface (see `Query.toArray()`).
|
|
886
|
+
*/
|
|
887
|
+
declare function executePlan(records: readonly unknown[], plan: QueryPlan): unknown[];
|
|
888
|
+
|
|
889
|
+
/**
|
|
890
|
+
* Streaming scan builder with filter + aggregate support.
|
|
891
|
+
*
|
|
892
|
+
* `Collection.scan()` now returns a `ScanBuilder<T>` that
|
|
893
|
+
* implements `AsyncIterable<T>` (for existing `for await … of`
|
|
894
|
+
* consumers) AND exposes chainable `.where()` / `.filter()` clauses
|
|
895
|
+
* plus a `.aggregate(spec)` async terminal that reduces the scan
|
|
896
|
+
* stream through the same reducer protocol as `Query.aggregate()`
|
|
897
|
+
*.
|
|
898
|
+
*
|
|
899
|
+
* **Memory model:** O(reducers), not O(records). The aggregate
|
|
900
|
+
* terminal initializes one state per reducer, iterates through the
|
|
901
|
+
* scan one record at a time via `for await`, applies every reducer's
|
|
902
|
+
* `step` per record, and never collects the stream into an array.
|
|
903
|
+
* This is what makes `scan().aggregate()` suitable for collections
|
|
904
|
+
* that don't fit in memory — the bound is a code-level invariant
|
|
905
|
+
* visible in the function body, not a runtime assertion.
|
|
906
|
+
*
|
|
907
|
+
* **Paginated iteration:** the builder holds a `pageProvider`
|
|
908
|
+
* closure that maps `(cursor, limit) → Promise<page>`, plumbed by
|
|
909
|
+
* `Collection.scan()` to `collection.listPage(...)`. The page
|
|
910
|
+
* iterator walks cursors forward until exhaustion, same as the
|
|
911
|
+
* previous async-generator `scan()` did.
|
|
912
|
+
*
|
|
913
|
+
* **Backward compatibility:** existing `for await (const rec of
|
|
914
|
+
* collection.scan()) { … }` code continues to work because
|
|
915
|
+
* `ScanBuilder` implements `[Symbol.asyncIterator]`. The previous
|
|
916
|
+
* signature returned an `AsyncIterableIterator<T>` (which has both
|
|
917
|
+
* `[Symbol.asyncIterator]` and `.next()`). We verified at grep time
|
|
918
|
+
* that no call sites use `.next()` on the scan result directly, so
|
|
919
|
+
* the narrowed interface is safe.
|
|
920
|
+
*
|
|
921
|
+
* **Immutability:** each `.where()` / `.filter()` call returns a
|
|
922
|
+
* fresh builder sharing the same page provider and page size. This
|
|
923
|
+
* lets a base scan be reused for multiple parallel aggregations:
|
|
924
|
+
*
|
|
925
|
+
* ```ts
|
|
926
|
+
* const scan = invoices.scan()
|
|
927
|
+
* const [open, paid] = await Promise.all([
|
|
928
|
+
* scan.where('status', '==', 'open').aggregate({ n: count() }),
|
|
929
|
+
* scan.where('status', '==', 'paid').aggregate({ n: count() }),
|
|
930
|
+
* ])
|
|
931
|
+
* ```
|
|
932
|
+
*
|
|
933
|
+
* Note that each aggregation pays a full scan — there's no shared
|
|
934
|
+
* iteration across the two. Multi-way aggregation in a single pass
|
|
935
|
+
* is out of scope; consumers who need it should build a compound spec
|
|
936
|
+
* and run a single `.aggregate({ openN, paidN })` at the DSL level.
|
|
937
|
+
*
|
|
938
|
+
* **Out of scope for (tracked separately):**
|
|
939
|
+
* - `scan().aggregate().live()` — unbounded scan + change-stream
|
|
940
|
+
* reconciliation is a design problem, not just a code one
|
|
941
|
+
* - `scan().groupBy().aggregate()` — high-cardinality grouping on
|
|
942
|
+
* huge collections would re-introduce the O(groups) memory
|
|
943
|
+
* problem that aggregate fixes
|
|
944
|
+
* - Parallel scan across pages — race-safe page cursor contracts
|
|
945
|
+
* are not in the adapter API yet
|
|
946
|
+
* - `scan().join(...)` — tracked under (streaming join)
|
|
947
|
+
*/
|
|
948
|
+
|
|
949
|
+
/**
|
|
950
|
+
* Page provider — the Collection-shaped hook the builder calls to
|
|
951
|
+
* walk cursors forward. Kept as a structural interface so tests can
|
|
952
|
+
* wire up a synthetic provider without pulling in the full
|
|
953
|
+
* Collection class. Collection's `listPage` matches this shape
|
|
954
|
+
* exactly.
|
|
955
|
+
*/
|
|
956
|
+
interface ScanPageProvider<T> {
|
|
957
|
+
listPage(opts: {
|
|
958
|
+
cursor?: string;
|
|
959
|
+
limit?: number;
|
|
960
|
+
}): Promise<{
|
|
961
|
+
items: T[];
|
|
962
|
+
nextCursor: string | null;
|
|
963
|
+
}>;
|
|
964
|
+
}
|
|
965
|
+
/**
|
|
966
|
+
* Chainable streaming scan. Implements `AsyncIterable<T>` for
|
|
967
|
+
* drop-in use with `for await … of`; adds `.where()` / `.filter()`
|
|
968
|
+
* chainable clauses and a `.aggregate(spec)` async terminal.
|
|
969
|
+
*
|
|
970
|
+
* The builder is immutable per operation — each chained call
|
|
971
|
+
* returns a fresh `ScanBuilder` sharing the same page provider and
|
|
972
|
+
* page size. The original builder is never mutated, so it's safe
|
|
973
|
+
* to reuse across multiple parallel consumers.
|
|
974
|
+
*/
|
|
975
|
+
declare class ScanBuilder<T> implements AsyncIterable<T> {
|
|
976
|
+
private readonly pageProvider;
|
|
977
|
+
private readonly pageSize;
|
|
978
|
+
private readonly clauses;
|
|
979
|
+
/**
|
|
980
|
+
* Zero-or-more join legs to apply per record as the stream flows.
|
|
981
|
+
* Each leg attaches the resolved right-side record (or null) under
|
|
982
|
+
* its alias. — streaming joins.
|
|
983
|
+
*
|
|
984
|
+
* Joins are evaluated AFTER clauses, so a `where()` filtered-out
|
|
985
|
+
* record never triggers a right-side lookup. This is the same
|
|
986
|
+
* ordering as `Query.toArray()` (clauses first, joins after) and
|
|
987
|
+
* keeps the streaming path from doing wasted work.
|
|
988
|
+
*/
|
|
989
|
+
private readonly joins;
|
|
990
|
+
/**
|
|
991
|
+
* Join resolution context. Required for `.join()` to translate a
|
|
992
|
+
* field name into a target collection + ref mode and to resolve
|
|
993
|
+
* the right-side `JoinableSource`. Optional because tests
|
|
994
|
+
* construct ScanBuilder directly with synthetic page providers
|
|
995
|
+
* that don't know about ref() — calling `.join()` without a
|
|
996
|
+
* context throws with an actionable error.
|
|
997
|
+
*/
|
|
998
|
+
private readonly joinContext;
|
|
999
|
+
constructor(pageProvider: ScanPageProvider<T>, pageSize?: number, clauses?: readonly Clause[], joins?: readonly JoinLeg[], joinContext?: JoinContext);
|
|
1000
|
+
/**
|
|
1001
|
+
* Add a field comparison. Runs per record as the scan stream
|
|
1002
|
+
* flows through, so non-matching records are dropped before they
|
|
1003
|
+
* reach `.aggregate()` or the iteration consumer. Multiple
|
|
1004
|
+
* `.where()` calls are AND-combined — same semantics as
|
|
1005
|
+
* `Query.where()`.
|
|
1006
|
+
*
|
|
1007
|
+
* Clauses cannot use the secondary-index fast path here because
|
|
1008
|
+
* the scan sources records from the adapter's paginator, not from
|
|
1009
|
+
* the in-memory cache where indexes live. Index-accelerated scans
|
|
1010
|
+
* are a future optimization — the current implementation
|
|
1011
|
+
* evaluates clauses per record in O(1) per clause.
|
|
1012
|
+
*/
|
|
1013
|
+
where(field: string, op: Operator, value: unknown): ScanBuilder<T>;
|
|
1014
|
+
/**
|
|
1015
|
+
* Escape hatch: add an arbitrary predicate function. Same
|
|
1016
|
+
* non-serializable caveat as `Query.filter()` — filter clauses
|
|
1017
|
+
* don't round-trip through `toPlan()`. Prefer `.where()` when
|
|
1018
|
+
* possible.
|
|
1019
|
+
*/
|
|
1020
|
+
filter(fn: (record: T) => boolean): ScanBuilder<T>;
|
|
1021
|
+
/**
|
|
1022
|
+
* Resolve a `ref()`-declared foreign key per record as the scan
|
|
1023
|
+
* stream flows, attaching the right-side record (or null) under
|
|
1024
|
+
* `opts.as`. — streaming joins over `scan()`.
|
|
1025
|
+
*
|
|
1026
|
+
* ```ts
|
|
1027
|
+
* for await (const inv of invoices.scan().join('clientId', { as: 'client' })) {
|
|
1028
|
+
* await processInvoice(inv) // inv.client is attached
|
|
1029
|
+
* }
|
|
1030
|
+
*
|
|
1031
|
+
* // Or terminate with .aggregate() for streaming joined aggregation
|
|
1032
|
+
* const { total } = await invoices.scan()
|
|
1033
|
+
* .where('status', '==', 'open')
|
|
1034
|
+
* .join('clientId', { as: 'client' })
|
|
1035
|
+
* .aggregate({ total: sum('amount') })
|
|
1036
|
+
* ```
|
|
1037
|
+
*
|
|
1038
|
+
* **The key difference from eager `.join()`:** the LEFT
|
|
1039
|
+
* side streams page-by-page from the adapter and is never
|
|
1040
|
+
* materialized. Memory ceiling on the left is O(pageSize), not
|
|
1041
|
+
* O(rowCount). This is what makes streaming joins suitable for
|
|
1042
|
+
* collections that exceed the eager join's 50_000-row ceiling.
|
|
1043
|
+
*
|
|
1044
|
+
* **Right-side strategy** is auto-selected per leg:
|
|
1045
|
+
* - **Indexed** — right source exposes `lookupById`, so each
|
|
1046
|
+
* left row costs O(1). This is the common path for
|
|
1047
|
+
* Collection right sides, which back `lookupById` with a Map
|
|
1048
|
+
* lookup over the in-memory cache. The right collection must
|
|
1049
|
+
* be in eager mode (the same constraint as eager join's
|
|
1050
|
+
* `querySourceForJoin` from ).
|
|
1051
|
+
* - **Hash** — right source has only `snapshot()`. Build a
|
|
1052
|
+
* `Map<id, record>` once at iteration start, probe per left
|
|
1053
|
+
* row. Same correctness, same per-row cost as the indexed
|
|
1054
|
+
* path; the difference is the upfront cost of materializing
|
|
1055
|
+
* the right side once.
|
|
1056
|
+
*
|
|
1057
|
+
* Both strategies hold the right side in memory for the duration
|
|
1058
|
+
* of the iteration. The "streaming" property applies to the LEFT
|
|
1059
|
+
* side only — true left-and-right streaming joins (where neither
|
|
1060
|
+
* side fits in memory) require a sort-merge join planner that's
|
|
1061
|
+
* out of scope for.
|
|
1062
|
+
*
|
|
1063
|
+
* **Ref-mode semantics** match eager `.join()` exactly:
|
|
1064
|
+
* - `strict` → throws `DanglingReferenceError` mid-stream
|
|
1065
|
+
* when a left record points at a non-existent right id.
|
|
1066
|
+
* The throw aborts the async iterator — consumers should
|
|
1067
|
+
* wrap the `for await` in try/catch if they want to recover.
|
|
1068
|
+
* - `warn` → attaches `null` and emits a one-shot warning
|
|
1069
|
+
* per unique dangling pair (deduped via the same warn
|
|
1070
|
+
* channel as eager join).
|
|
1071
|
+
* - `cascade` → attaches `null` silently. A delete-time mode;
|
|
1072
|
+
* dangling refs at read time are mid-flight or pre-existing
|
|
1073
|
+
* orphans, not a DSL error.
|
|
1074
|
+
*
|
|
1075
|
+
* Left records with null/undefined FK values attach `null`
|
|
1076
|
+
* regardless of mode — same "no reference at all" policy as
|
|
1077
|
+
* eager join and write-time `enforceRefsOnPut`.
|
|
1078
|
+
*
|
|
1079
|
+
* **Multi-FK chaining** is supported via repeated `.join()`
|
|
1080
|
+
* calls: each leg resolves an independent ref. Each leg
|
|
1081
|
+
* independently picks its right-side strategy and applies its
|
|
1082
|
+
* own ref mode.
|
|
1083
|
+
*
|
|
1084
|
+
* **Joins are NOT applied** to a `.aggregate()` terminal that
|
|
1085
|
+
* doesn't reference joined fields — wait, that's not quite
|
|
1086
|
+
* right. The streaming path actually DOES apply joins before
|
|
1087
|
+
* `.aggregate()` because the join attaches a field that the
|
|
1088
|
+
* spec might reference. Unlike `Query.aggregate()` (which skips
|
|
1089
|
+
* joins entirely as a projection-only short-circuit), the
|
|
1090
|
+
* streaming aggregation can't know whether the spec touches a
|
|
1091
|
+
* joined field, so it always applies joins. Consumers who want
|
|
1092
|
+
* unjoined streaming aggregation should leave `.join()` off the
|
|
1093
|
+
* chain — the chain is composable for a reason.
|
|
1094
|
+
*
|
|
1095
|
+
* constraint #1 — every JoinLeg carries `partitionScope:
|
|
1096
|
+
* 'all'` plumbed through but never read by. Same seam as
|
|
1097
|
+
* eager join.
|
|
1098
|
+
*/
|
|
1099
|
+
join<As extends string, R = unknown>(field: string, opts: {
|
|
1100
|
+
as: As;
|
|
1101
|
+
}): ScanBuilder<T & Record<As, R | null>>;
|
|
1102
|
+
/**
|
|
1103
|
+
* Iterate the scan as an async iterable. Walks the page
|
|
1104
|
+
* provider's cursors forward until exhaustion, applying every
|
|
1105
|
+
* clause per record — only matching records are yielded.
|
|
1106
|
+
*
|
|
1107
|
+
* Backward-compatible with the previous async-generator `scan()`
|
|
1108
|
+
* return type for `for await … of` consumers.
|
|
1109
|
+
*/
|
|
1110
|
+
[Symbol.asyncIterator](): AsyncIterator<T>;
|
|
1111
|
+
/**
|
|
1112
|
+
* Per-leg right-side resolution state. Built once at iteration
|
|
1113
|
+
* start and reused for every left record. Two strategies:
|
|
1114
|
+
*
|
|
1115
|
+
* - `lookupById`: present when the right source exposes the
|
|
1116
|
+
* hook directly (typical Collection right side). Per-row
|
|
1117
|
+
* cost is O(1).
|
|
1118
|
+
* - `hashByPrimaryKey`: built from `snapshot()` when no
|
|
1119
|
+
* lookupById. Per-row cost is O(1) after the upfront O(N)
|
|
1120
|
+
* materialization. Same as eager join's hash strategy.
|
|
1121
|
+
*
|
|
1122
|
+
* `warnedKeys` is the per-leg dedup set for ref-mode 'warn'. We
|
|
1123
|
+
* key on `field→target:refId` so the same dangling pair only
|
|
1124
|
+
* warns once per iteration. The dedup is per-iteration, not
|
|
1125
|
+
* per-process — a long-running scan that re-iterates would warn
|
|
1126
|
+
* again, which is the desired behavior (the data may have
|
|
1127
|
+
* changed between iterations).
|
|
1128
|
+
*/
|
|
1129
|
+
private buildJoinResolvers;
|
|
1130
|
+
/**
|
|
1131
|
+
* Resolve a single join leg for one left record and return the
|
|
1132
|
+
* left record with the joined field attached under
|
|
1133
|
+
* `leg.as`. Pure function over `(left, resolver)`; never
|
|
1134
|
+
* mutates the input.
|
|
1135
|
+
*
|
|
1136
|
+
* Ref-mode dispatch matches eager `applyJoins` from :
|
|
1137
|
+
* - null/undefined FK → attach null silently (always allowed)
|
|
1138
|
+
* - dangling FK + strict → throw `DanglingReferenceError`
|
|
1139
|
+
* - dangling FK + warn → attach null, warn-once per pair
|
|
1140
|
+
* - dangling FK + cascade → attach null silently
|
|
1141
|
+
*/
|
|
1142
|
+
private applyOneJoinStreaming;
|
|
1143
|
+
/**
|
|
1144
|
+
* Reduce the scan stream through a named set of reducers and
|
|
1145
|
+
* return the final aggregated shape.
|
|
1146
|
+
*
|
|
1147
|
+
* Memory is O(reducers): one mutable state slot per spec key.
|
|
1148
|
+
* Records flow through the pipeline one at a time via
|
|
1149
|
+
* `for await` and are discarded after their `step()` is applied
|
|
1150
|
+
* — never collected into an array. This is the distinguishing
|
|
1151
|
+
* property from `Query.aggregate()`, which materializes the full
|
|
1152
|
+
* match set first.
|
|
1153
|
+
*
|
|
1154
|
+
* Reuses the same reducer protocol as `Query.aggregate()`,
|
|
1155
|
+
* so `count()`, `sum(field)`, `avg(field)`, `min(field)`,
|
|
1156
|
+
* `max(field)` all work unchanged. The `{ seed }` parameter
|
|
1157
|
+
* plumbing from constraint #2 is honored transparently — the
|
|
1158
|
+
* factories ignore it in and the scan executor never
|
|
1159
|
+
* touches the per-reducer state construction.
|
|
1160
|
+
*
|
|
1161
|
+
* **Returns a Promise**, unlike `Query.aggregate().run()` which
|
|
1162
|
+
* is synchronous. The scan is inherently async because it walks
|
|
1163
|
+
* adapter pages, so the terminal has to be too. Consumers
|
|
1164
|
+
* destructure with await:
|
|
1165
|
+
*
|
|
1166
|
+
* ```ts
|
|
1167
|
+
* const { total, n } = await invoices.scan()
|
|
1168
|
+
* .where('year', '==', 2025)
|
|
1169
|
+
* .aggregate({ total: sum('amount'), n: count() })
|
|
1170
|
+
* ```
|
|
1171
|
+
*
|
|
1172
|
+
* **No `.live()` in.** `scan().aggregate().live()` would
|
|
1173
|
+
* require reconciling an unbounded streaming iteration with a
|
|
1174
|
+
* change-stream subscription — a design problem, not just a code
|
|
1175
|
+
* one. Consumers with huge collections and live needs should
|
|
1176
|
+
* narrow with `.where()` enough to fit in the 50k `query()`
|
|
1177
|
+
* limit and use `query().aggregate().live()` instead.
|
|
1178
|
+
*/
|
|
1179
|
+
aggregate<Spec extends AggregateSpec>(spec: Spec): Promise<AggregateResult<Spec>>;
|
|
1180
|
+
/**
|
|
1181
|
+
* Evaluate the clause list against a single record. Linear in
|
|
1182
|
+
* the clause count; short-circuits on first false. Clauses on a
|
|
1183
|
+
* scan are always re-evaluated per record — no index-accelerated
|
|
1184
|
+
* path, because the stream sources records from the adapter
|
|
1185
|
+
* paginator, not from the in-memory cache where indexes live.
|
|
1186
|
+
*/
|
|
1187
|
+
private recordMatches;
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
export { DEFAULT_CROSS_JOIN_MAX_ROWS as D, type JoinContext as J, type LiveQuery as L, type OrderBy as O, Query as Q, type RefDescriptor as R, ScanBuilder as S, DEFAULT_JOIN_MAX_ROWS as a, type JoinLeg as b, type JoinStrategy as c, type JoinableSource as d, type LiveUpstream as e, type QueryPlan as f, type QuerySource as g, RefIntegrityError as h, type RefMode as i, RefRegistry as j, RefScopeError as k, type RefViolation as l, type ScanPageProvider as m, applyJoins as n, buildLiveQuery as o, executePlan as p, resetJoinWarnings as q, ref as r };
|