@noy-db/hub 0.1.0-pre.3

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 (195) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +197 -0
  3. package/dist/aggregate/index.cjs +476 -0
  4. package/dist/aggregate/index.cjs.map +1 -0
  5. package/dist/aggregate/index.d.cts +38 -0
  6. package/dist/aggregate/index.d.ts +38 -0
  7. package/dist/aggregate/index.js +53 -0
  8. package/dist/aggregate/index.js.map +1 -0
  9. package/dist/blobs/index.cjs +1480 -0
  10. package/dist/blobs/index.cjs.map +1 -0
  11. package/dist/blobs/index.d.cts +45 -0
  12. package/dist/blobs/index.d.ts +45 -0
  13. package/dist/blobs/index.js +48 -0
  14. package/dist/blobs/index.js.map +1 -0
  15. package/dist/bundle/index.cjs +436 -0
  16. package/dist/bundle/index.cjs.map +1 -0
  17. package/dist/bundle/index.d.cts +7 -0
  18. package/dist/bundle/index.d.ts +7 -0
  19. package/dist/bundle/index.js +40 -0
  20. package/dist/bundle/index.js.map +1 -0
  21. package/dist/chunk-2QR2PQTT.js +217 -0
  22. package/dist/chunk-2QR2PQTT.js.map +1 -0
  23. package/dist/chunk-4OWFYIDQ.js +79 -0
  24. package/dist/chunk-4OWFYIDQ.js.map +1 -0
  25. package/dist/chunk-5AATM2M2.js +90 -0
  26. package/dist/chunk-5AATM2M2.js.map +1 -0
  27. package/dist/chunk-ACLDOTNQ.js +543 -0
  28. package/dist/chunk-ACLDOTNQ.js.map +1 -0
  29. package/dist/chunk-BTDCBVJW.js +160 -0
  30. package/dist/chunk-BTDCBVJW.js.map +1 -0
  31. package/dist/chunk-CIMZBAZB.js +72 -0
  32. package/dist/chunk-CIMZBAZB.js.map +1 -0
  33. package/dist/chunk-E445ICYI.js +365 -0
  34. package/dist/chunk-E445ICYI.js.map +1 -0
  35. package/dist/chunk-EXQRC2L4.js +722 -0
  36. package/dist/chunk-EXQRC2L4.js.map +1 -0
  37. package/dist/chunk-FZU343FL.js +32 -0
  38. package/dist/chunk-FZU343FL.js.map +1 -0
  39. package/dist/chunk-GJILMRPO.js +354 -0
  40. package/dist/chunk-GJILMRPO.js.map +1 -0
  41. package/dist/chunk-GOUT6DND.js +1285 -0
  42. package/dist/chunk-GOUT6DND.js.map +1 -0
  43. package/dist/chunk-J66GRPNH.js +111 -0
  44. package/dist/chunk-J66GRPNH.js.map +1 -0
  45. package/dist/chunk-M2F2JAWB.js +464 -0
  46. package/dist/chunk-M2F2JAWB.js.map +1 -0
  47. package/dist/chunk-M5INGEFC.js +84 -0
  48. package/dist/chunk-M5INGEFC.js.map +1 -0
  49. package/dist/chunk-M62XNWRA.js +72 -0
  50. package/dist/chunk-M62XNWRA.js.map +1 -0
  51. package/dist/chunk-MR4424N3.js +275 -0
  52. package/dist/chunk-MR4424N3.js.map +1 -0
  53. package/dist/chunk-NPC4LFV5.js +132 -0
  54. package/dist/chunk-NPC4LFV5.js.map +1 -0
  55. package/dist/chunk-NXFEYLVG.js +311 -0
  56. package/dist/chunk-NXFEYLVG.js.map +1 -0
  57. package/dist/chunk-R36SIKES.js +79 -0
  58. package/dist/chunk-R36SIKES.js.map +1 -0
  59. package/dist/chunk-TDR6T5CJ.js +381 -0
  60. package/dist/chunk-TDR6T5CJ.js.map +1 -0
  61. package/dist/chunk-UF3BUNQZ.js +1 -0
  62. package/dist/chunk-UF3BUNQZ.js.map +1 -0
  63. package/dist/chunk-UQFSPSWG.js +1109 -0
  64. package/dist/chunk-UQFSPSWG.js.map +1 -0
  65. package/dist/chunk-USKYUS74.js +793 -0
  66. package/dist/chunk-USKYUS74.js.map +1 -0
  67. package/dist/chunk-XCL3WP6J.js +121 -0
  68. package/dist/chunk-XCL3WP6J.js.map +1 -0
  69. package/dist/chunk-XHFOENR2.js +680 -0
  70. package/dist/chunk-XHFOENR2.js.map +1 -0
  71. package/dist/chunk-ZFKD4QMV.js +430 -0
  72. package/dist/chunk-ZFKD4QMV.js.map +1 -0
  73. package/dist/chunk-ZLMV3TUA.js +490 -0
  74. package/dist/chunk-ZLMV3TUA.js.map +1 -0
  75. package/dist/chunk-ZRG4V3F5.js +17 -0
  76. package/dist/chunk-ZRG4V3F5.js.map +1 -0
  77. package/dist/consent/index.cjs +204 -0
  78. package/dist/consent/index.cjs.map +1 -0
  79. package/dist/consent/index.d.cts +24 -0
  80. package/dist/consent/index.d.ts +24 -0
  81. package/dist/consent/index.js +23 -0
  82. package/dist/consent/index.js.map +1 -0
  83. package/dist/crdt/index.cjs +152 -0
  84. package/dist/crdt/index.cjs.map +1 -0
  85. package/dist/crdt/index.d.cts +30 -0
  86. package/dist/crdt/index.d.ts +30 -0
  87. package/dist/crdt/index.js +24 -0
  88. package/dist/crdt/index.js.map +1 -0
  89. package/dist/crypto-IVKU7YTT.js +44 -0
  90. package/dist/crypto-IVKU7YTT.js.map +1 -0
  91. package/dist/delegation-XDJCBTI2.js +16 -0
  92. package/dist/delegation-XDJCBTI2.js.map +1 -0
  93. package/dist/dev-unlock-CeXic1xC.d.cts +263 -0
  94. package/dist/dev-unlock-KrKkcqD3.d.ts +263 -0
  95. package/dist/hash-9KO1BGxh.d.cts +63 -0
  96. package/dist/hash-ChfJjRjQ.d.ts +63 -0
  97. package/dist/history/index.cjs +1215 -0
  98. package/dist/history/index.cjs.map +1 -0
  99. package/dist/history/index.d.cts +62 -0
  100. package/dist/history/index.d.ts +62 -0
  101. package/dist/history/index.js +79 -0
  102. package/dist/history/index.js.map +1 -0
  103. package/dist/i18n/index.cjs +746 -0
  104. package/dist/i18n/index.cjs.map +1 -0
  105. package/dist/i18n/index.d.cts +38 -0
  106. package/dist/i18n/index.d.ts +38 -0
  107. package/dist/i18n/index.js +55 -0
  108. package/dist/i18n/index.js.map +1 -0
  109. package/dist/index-BRHBCmLt.d.ts +1940 -0
  110. package/dist/index-C8kQtmOk.d.ts +380 -0
  111. package/dist/index-DN-J-5wT.d.cts +1940 -0
  112. package/dist/index-DhjMjz7L.d.cts +380 -0
  113. package/dist/index.cjs +14756 -0
  114. package/dist/index.cjs.map +1 -0
  115. package/dist/index.d.cts +269 -0
  116. package/dist/index.d.ts +269 -0
  117. package/dist/index.js +6085 -0
  118. package/dist/index.js.map +1 -0
  119. package/dist/indexing/index.cjs +736 -0
  120. package/dist/indexing/index.cjs.map +1 -0
  121. package/dist/indexing/index.d.cts +36 -0
  122. package/dist/indexing/index.d.ts +36 -0
  123. package/dist/indexing/index.js +77 -0
  124. package/dist/indexing/index.js.map +1 -0
  125. package/dist/lazy-builder-BwEoBQZ9.d.ts +304 -0
  126. package/dist/lazy-builder-CZVLKh0Z.d.cts +304 -0
  127. package/dist/ledger-2NX4L7PN.js +33 -0
  128. package/dist/ledger-2NX4L7PN.js.map +1 -0
  129. package/dist/mime-magic-CBBSOkjm.d.cts +50 -0
  130. package/dist/mime-magic-CBBSOkjm.d.ts +50 -0
  131. package/dist/periods/index.cjs +1035 -0
  132. package/dist/periods/index.cjs.map +1 -0
  133. package/dist/periods/index.d.cts +21 -0
  134. package/dist/periods/index.d.ts +21 -0
  135. package/dist/periods/index.js +25 -0
  136. package/dist/periods/index.js.map +1 -0
  137. package/dist/predicate-SBHmi6D0.d.cts +161 -0
  138. package/dist/predicate-SBHmi6D0.d.ts +161 -0
  139. package/dist/query/index.cjs +1957 -0
  140. package/dist/query/index.cjs.map +1 -0
  141. package/dist/query/index.d.cts +3 -0
  142. package/dist/query/index.d.ts +3 -0
  143. package/dist/query/index.js +62 -0
  144. package/dist/query/index.js.map +1 -0
  145. package/dist/session/index.cjs +487 -0
  146. package/dist/session/index.cjs.map +1 -0
  147. package/dist/session/index.d.cts +45 -0
  148. package/dist/session/index.d.ts +45 -0
  149. package/dist/session/index.js +44 -0
  150. package/dist/session/index.js.map +1 -0
  151. package/dist/shadow/index.cjs +133 -0
  152. package/dist/shadow/index.cjs.map +1 -0
  153. package/dist/shadow/index.d.cts +16 -0
  154. package/dist/shadow/index.d.ts +16 -0
  155. package/dist/shadow/index.js +20 -0
  156. package/dist/shadow/index.js.map +1 -0
  157. package/dist/store/index.cjs +1069 -0
  158. package/dist/store/index.cjs.map +1 -0
  159. package/dist/store/index.d.cts +491 -0
  160. package/dist/store/index.d.ts +491 -0
  161. package/dist/store/index.js +34 -0
  162. package/dist/store/index.js.map +1 -0
  163. package/dist/strategy-BSxFXGzb.d.cts +110 -0
  164. package/dist/strategy-BSxFXGzb.d.ts +110 -0
  165. package/dist/strategy-D-SrOLCl.d.cts +548 -0
  166. package/dist/strategy-D-SrOLCl.d.ts +548 -0
  167. package/dist/sync/index.cjs +1062 -0
  168. package/dist/sync/index.cjs.map +1 -0
  169. package/dist/sync/index.d.cts +42 -0
  170. package/dist/sync/index.d.ts +42 -0
  171. package/dist/sync/index.js +28 -0
  172. package/dist/sync/index.js.map +1 -0
  173. package/dist/team/index.cjs +1233 -0
  174. package/dist/team/index.cjs.map +1 -0
  175. package/dist/team/index.d.cts +117 -0
  176. package/dist/team/index.d.ts +117 -0
  177. package/dist/team/index.js +39 -0
  178. package/dist/team/index.js.map +1 -0
  179. package/dist/tx/index.cjs +212 -0
  180. package/dist/tx/index.cjs.map +1 -0
  181. package/dist/tx/index.d.cts +20 -0
  182. package/dist/tx/index.d.ts +20 -0
  183. package/dist/tx/index.js +20 -0
  184. package/dist/tx/index.js.map +1 -0
  185. package/dist/types-BZpCZB8N.d.ts +7526 -0
  186. package/dist/types-Bfs0qr5F.d.cts +7526 -0
  187. package/dist/ulid-COREQ2RQ.js +9 -0
  188. package/dist/ulid-COREQ2RQ.js.map +1 -0
  189. package/dist/util/index.cjs +230 -0
  190. package/dist/util/index.cjs.map +1 -0
  191. package/dist/util/index.d.cts +77 -0
  192. package/dist/util/index.d.ts +77 -0
  193. package/dist/util/index.js +190 -0
  194. package/dist/util/index.js.map +1 -0
  195. package/package.json +244 -0
@@ -0,0 +1,736 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/indexing/index.ts
21
+ var indexing_exports = {};
22
+ __export(indexing_exports, {
23
+ COMPOSITE_DELIMITER: () => COMPOSITE_DELIMITER,
24
+ CollectionIndexes: () => CollectionIndexes,
25
+ IDX_PREFIX: () => IDX_PREFIX,
26
+ LazyQuery: () => LazyQuery,
27
+ PersistedCollectionIndex: () => PersistedCollectionIndex,
28
+ compositeKey: () => compositeKey,
29
+ decodeIdxId: () => decodeIdxId,
30
+ encodeIdxId: () => encodeIdxId,
31
+ isIdxId: () => isIdxId,
32
+ withIndexing: () => withIndexing
33
+ });
34
+ module.exports = __toCommonJS(indexing_exports);
35
+
36
+ // src/query/predicate.ts
37
+ function readPath(record, path) {
38
+ if (record === null || record === void 0) return void 0;
39
+ if (!path.includes(".")) {
40
+ return record[path];
41
+ }
42
+ const segments = path.split(".");
43
+ let cursor = record;
44
+ for (const segment of segments) {
45
+ if (cursor === null || cursor === void 0) return void 0;
46
+ cursor = cursor[segment];
47
+ }
48
+ return cursor;
49
+ }
50
+ function evaluateFieldClause(record, clause) {
51
+ const actual = readPath(record, clause.field);
52
+ const { op, value } = clause;
53
+ switch (op) {
54
+ case "==":
55
+ return actual === value;
56
+ case "!=":
57
+ return actual !== value;
58
+ case "<":
59
+ return isComparable(actual, value) && actual < value;
60
+ case "<=":
61
+ return isComparable(actual, value) && actual <= value;
62
+ case ">":
63
+ return isComparable(actual, value) && actual > value;
64
+ case ">=":
65
+ return isComparable(actual, value) && actual >= value;
66
+ case "in":
67
+ return Array.isArray(value) && value.includes(actual);
68
+ case "contains":
69
+ if (typeof actual === "string") return typeof value === "string" && actual.includes(value);
70
+ if (Array.isArray(actual)) return actual.includes(value);
71
+ return false;
72
+ case "startsWith":
73
+ return typeof actual === "string" && typeof value === "string" && actual.startsWith(value);
74
+ case "between": {
75
+ if (!Array.isArray(value) || value.length !== 2) return false;
76
+ const [lo, hi] = value;
77
+ if (!isComparable(actual, lo) || !isComparable(actual, hi)) return false;
78
+ return actual >= lo && actual <= hi;
79
+ }
80
+ default: {
81
+ const _exhaustive = op;
82
+ void _exhaustive;
83
+ return false;
84
+ }
85
+ }
86
+ }
87
+ function isComparable(a, b) {
88
+ if (typeof a === "number" && typeof b === "number") return true;
89
+ if (typeof a === "string" && typeof b === "string") return true;
90
+ if (a instanceof Date && b instanceof Date) return true;
91
+ return false;
92
+ }
93
+ function evaluateClause(record, clause) {
94
+ switch (clause.type) {
95
+ case "field":
96
+ return evaluateFieldClause(record, clause);
97
+ case "filter":
98
+ return clause.fn(record);
99
+ case "group":
100
+ if (clause.op === "and") {
101
+ for (const child of clause.clauses) {
102
+ if (!evaluateClause(record, child)) return false;
103
+ }
104
+ return true;
105
+ } else {
106
+ for (const child of clause.clauses) {
107
+ if (evaluateClause(record, child)) return true;
108
+ }
109
+ return false;
110
+ }
111
+ }
112
+ }
113
+
114
+ // src/indexing/eager-indexes.ts
115
+ var CollectionIndexes = class {
116
+ indexes = /* @__PURE__ */ new Map();
117
+ /**
118
+ * Declare an index. Subsequent record additions are tracked under it.
119
+ * Calling this twice for the same field is a no-op (idempotent).
120
+ */
121
+ declare(field) {
122
+ if (this.indexes.has(field)) return;
123
+ this.indexes.set(field, { field, buckets: /* @__PURE__ */ new Map() });
124
+ }
125
+ /** True if the given field has a declared index. */
126
+ has(field) {
127
+ return this.indexes.has(field);
128
+ }
129
+ /** All declared field names, in declaration order. */
130
+ fields() {
131
+ return [...this.indexes.keys()];
132
+ }
133
+ /**
134
+ * Build all declared indexes from a snapshot of records.
135
+ * Called once per hydration. O(N × indexes.size).
136
+ */
137
+ build(records) {
138
+ for (const idx of this.indexes.values()) {
139
+ idx.buckets.clear();
140
+ for (const { id, record } of records) {
141
+ addToIndex(idx, id, record);
142
+ }
143
+ }
144
+ }
145
+ /**
146
+ * Insert or update a single record across all indexes.
147
+ * Called by `Collection.put()` after the encrypted write succeeds.
148
+ *
149
+ * If `previousRecord` is provided, the record is removed from any old
150
+ * buckets first — this is the update path. Pass `null` for fresh adds.
151
+ */
152
+ upsert(id, newRecord, previousRecord) {
153
+ if (this.indexes.size === 0) return;
154
+ if (previousRecord !== null) {
155
+ this.remove(id, previousRecord);
156
+ }
157
+ for (const idx of this.indexes.values()) {
158
+ addToIndex(idx, id, newRecord);
159
+ }
160
+ }
161
+ /**
162
+ * Remove a record from all indexes. Called by `Collection.delete()`
163
+ * (and as the first half of `upsert` for the update path).
164
+ */
165
+ remove(id, record) {
166
+ if (this.indexes.size === 0) return;
167
+ for (const idx of this.indexes.values()) {
168
+ removeFromIndex(idx, id, record);
169
+ }
170
+ }
171
+ /** Drop all index data. Called when the collection is invalidated. */
172
+ clear() {
173
+ for (const idx of this.indexes.values()) {
174
+ idx.buckets.clear();
175
+ }
176
+ }
177
+ /**
178
+ * Equality lookup: return the set of record ids whose `field` matches
179
+ * the given value. Returns `null` if no index covers the field — the
180
+ * caller should fall back to a linear scan.
181
+ *
182
+ * The returned Set is a reference to the index's internal storage —
183
+ * callers must NOT mutate it.
184
+ */
185
+ lookupEqual(field, value) {
186
+ const idx = this.indexes.get(field);
187
+ if (!idx) return null;
188
+ const key = stringifyKey(value);
189
+ return idx.buckets.get(key) ?? EMPTY_SET;
190
+ }
191
+ /**
192
+ * Set lookup: return the union of record ids whose `field` matches any
193
+ * of the given values. Returns `null` if no index covers the field.
194
+ */
195
+ lookupIn(field, values) {
196
+ const idx = this.indexes.get(field);
197
+ if (!idx) return null;
198
+ const out = /* @__PURE__ */ new Set();
199
+ for (const value of values) {
200
+ const key = stringifyKey(value);
201
+ const bucket = idx.buckets.get(key);
202
+ if (bucket) {
203
+ for (const id of bucket) out.add(id);
204
+ }
205
+ }
206
+ return out;
207
+ }
208
+ };
209
+ var EMPTY_SET = /* @__PURE__ */ new Set();
210
+ function stringifyKey(value) {
211
+ if (value === null || value === void 0) return "\0NULL\0";
212
+ if (typeof value === "string") return value;
213
+ if (typeof value === "number" || typeof value === "boolean") return String(value);
214
+ if (value instanceof Date) return value.toISOString();
215
+ return "\0OBJECT\0";
216
+ }
217
+ function addToIndex(idx, id, record) {
218
+ const value = readPath(record, idx.field);
219
+ if (value === null || value === void 0) return;
220
+ const key = stringifyKey(value);
221
+ let bucket = idx.buckets.get(key);
222
+ if (!bucket) {
223
+ bucket = /* @__PURE__ */ new Set();
224
+ idx.buckets.set(key, bucket);
225
+ }
226
+ bucket.add(id);
227
+ }
228
+ function removeFromIndex(idx, id, record) {
229
+ const value = readPath(record, idx.field);
230
+ if (value === null || value === void 0) return;
231
+ const key = stringifyKey(value);
232
+ const bucket = idx.buckets.get(key);
233
+ if (!bucket) return;
234
+ bucket.delete(id);
235
+ if (bucket.size === 0) idx.buckets.delete(key);
236
+ }
237
+
238
+ // src/indexing/persisted-indexes.ts
239
+ var IDX_PREFIX = "_idx/";
240
+ function encodeIdxId(field, recordId) {
241
+ return `${IDX_PREFIX}${field}/${recordId}`;
242
+ }
243
+ function decodeIdxId(id) {
244
+ if (!id.startsWith(IDX_PREFIX)) return null;
245
+ const rest = id.slice(IDX_PREFIX.length);
246
+ const firstSlash = rest.indexOf("/");
247
+ if (firstSlash <= 0) return null;
248
+ const field = rest.slice(0, firstSlash);
249
+ const recordId = rest.slice(firstSlash + 1);
250
+ if (recordId.length === 0) return null;
251
+ return { field, recordId };
252
+ }
253
+ function isIdxId(id) {
254
+ return decodeIdxId(id) !== null;
255
+ }
256
+ var COMPOSITE_DELIMITER = "|";
257
+ function compositeKey(fields) {
258
+ return fields.join(COMPOSITE_DELIMITER);
259
+ }
260
+ var PersistedCollectionIndex = class {
261
+ indexes = /* @__PURE__ */ new Map();
262
+ defs = /* @__PURE__ */ new Map();
263
+ /**
264
+ * Declare a single-field index. Subsequent `upsert` / `ingest` calls
265
+ * populate the in-memory mirror; calls before `declare` are no-ops
266
+ * (tolerant bulk-load ordering). Idempotent.
267
+ */
268
+ declare(field) {
269
+ if (this.indexes.has(field)) return;
270
+ this.indexes.set(field, { buckets: /* @__PURE__ */ new Map(), values: /* @__PURE__ */ new Map() });
271
+ this.defs.set(field, { kind: "single", field, key: field });
272
+ }
273
+ /**
274
+ * Declare a composite (multi-field) index. The synthetic
275
+ * key is `fields.join('|')`; it doubles as the in-memory map key and
276
+ * the `_idx/<key>/<recordId>` side-car field segment. Callers upsert
277
+ * and lookup via the same `key` as single-field indexes, just with a
278
+ * tuple value (JSON-stringified for bucketing).
279
+ */
280
+ declareComposite(fields) {
281
+ if (fields.length === 0) {
282
+ throw new Error("declareComposite: fields array must be non-empty");
283
+ }
284
+ for (const f of fields) {
285
+ if (f.includes(COMPOSITE_DELIMITER)) {
286
+ throw new Error(
287
+ `declareComposite: field "${f}" contains the composite delimiter "${COMPOSITE_DELIMITER}" \u2014 pick a different field name or open an issue to add hash-based composite keys.`
288
+ );
289
+ }
290
+ }
291
+ const key = compositeKey(fields);
292
+ if (this.indexes.has(key)) return;
293
+ this.indexes.set(key, { buckets: /* @__PURE__ */ new Map(), values: /* @__PURE__ */ new Map() });
294
+ this.defs.set(key, { kind: "composite", fields: [...fields], key });
295
+ }
296
+ /**
297
+ * Every declared index's structured definition. Collection walks this
298
+ * when materialising side-cars on put/delete so it can extract a
299
+ * single-field value or a composite tuple appropriately.
300
+ */
301
+ definitions() {
302
+ return [...this.defs.values()];
303
+ }
304
+ /** True if `field` has been declared as indexable on this mirror. */
305
+ has(field) {
306
+ return this.indexes.has(field);
307
+ }
308
+ /** All declared field names, in declaration order. */
309
+ fields() {
310
+ return [...this.indexes.keys()];
311
+ }
312
+ /**
313
+ * Bulk-load the mirror from decrypted index bodies. Intended to be
314
+ * called once per field after reading the collection's `_idx/<field>/*`
315
+ * side-cars. Safe to call twice with the same rows — bucket Sets
316
+ * deduplicate recordIds. If `field` is not declared, this is a no-op
317
+ * (tolerates the case where bulk-load runs before `declare()` lands).
318
+ */
319
+ ingest(field, rows) {
320
+ const state = this.indexes.get(field);
321
+ if (!state) return;
322
+ for (const row of rows) {
323
+ addToState(state, row.recordId, row.value);
324
+ }
325
+ }
326
+ /**
327
+ * Incrementally update a record's index entry for one field. Called by
328
+ * `Collection.put()` after the main write succeeds. If
329
+ * `previousValue` is non-null, the record is removed from the old
330
+ * bucket first — this is the update path. Pass `null` for fresh adds.
331
+ * No-op if the field is not declared.
332
+ */
333
+ upsert(recordId, field, newValue, previousValue) {
334
+ const state = this.indexes.get(field);
335
+ if (!state) return;
336
+ if (previousValue !== null && previousValue !== void 0) {
337
+ removeFromState(state, recordId, previousValue);
338
+ }
339
+ addToState(state, recordId, newValue);
340
+ }
341
+ /**
342
+ * Remove a record from the index for one field. Called by
343
+ * `Collection.delete()`. No-op if the field is not declared or
344
+ * the record isn't in the bucket. Empty buckets are dropped to keep
345
+ * the Map clean.
346
+ */
347
+ remove(recordId, field, value) {
348
+ const state = this.indexes.get(field);
349
+ if (!state) return;
350
+ removeFromState(state, recordId, value);
351
+ }
352
+ /**
353
+ * Drop all bucket data while preserving field declarations. Called on
354
+ * invalidation (incoming sync changes, keyring rotation) — the next
355
+ * query re-populates via `ingest`.
356
+ */
357
+ clear() {
358
+ for (const state of this.indexes.values()) {
359
+ state.buckets.clear();
360
+ state.values.clear();
361
+ }
362
+ }
363
+ /**
364
+ * Equality lookup — return the set of record ids whose `field` matches
365
+ * `value`. Returns `null` if the field is not declared (caller falls
366
+ * back to scan or throws `IndexRequiredError`). Returns a shared empty
367
+ * set if the field is declared but no record matches — that set MUST
368
+ * NOT be mutated by the caller.
369
+ */
370
+ lookupEqual(field, value) {
371
+ const state = this.indexes.get(field);
372
+ if (!state) return null;
373
+ const key = stringifyKey2(value);
374
+ return state.buckets.get(key) ?? EMPTY_SET2;
375
+ }
376
+ /**
377
+ * Set lookup — return the union of record ids whose `field` matches any
378
+ * of `values`. Returns `null` if the field is not declared. Returns a
379
+ * fresh (non-shared) Set — safe for the caller to mutate.
380
+ */
381
+ lookupIn(field, values) {
382
+ const state = this.indexes.get(field);
383
+ if (!state) return null;
384
+ const out = /* @__PURE__ */ new Set();
385
+ for (const value of values) {
386
+ const bucket = state.buckets.get(stringifyKey2(value));
387
+ if (bucket) for (const id of bucket) out.add(id);
388
+ }
389
+ return out;
390
+ }
391
+ /**
392
+ * Range lookup. Return record ids whose indexed value
393
+ * satisfies the predicate. Comparison happens on the ORIGINAL TYPED
394
+ * value carried in `state.values` — so numeric `<` sorts numerically,
395
+ * not lexicographically on `String(n)`. Returns `null` if the field
396
+ * is not declared.
397
+ *
398
+ * Supported ops: `'<'`, `'<='`, `'>'`, `'>='`, `'between'`. For
399
+ * `'between'`, `value` is `[lo, hi]` and both bounds are inclusive
400
+ * (matches the eager-mode operator contract in `predicate.ts`).
401
+ */
402
+ lookupRange(field, op, value) {
403
+ const state = this.indexes.get(field);
404
+ if (!state) return null;
405
+ const out = /* @__PURE__ */ new Set();
406
+ for (const [recordId, live] of state.values) {
407
+ if (live === void 0 || live === null) continue;
408
+ if (matchesRange(live, op, value)) out.add(recordId);
409
+ }
410
+ return out;
411
+ }
412
+ /**
413
+ * Sorted iteration — return every entry on `field` as an
414
+ * `OrderedEntry[]`, sorted by the ORIGINAL TYPED value (#275: no more
415
+ * `'10' < '2'` surprises on numeric fields). Consumers paginate with
416
+ * a numeric offset. `OrderedEntry.value` is the typed value.
417
+ */
418
+ orderedBy(field, dir) {
419
+ const state = this.indexes.get(field);
420
+ if (!state) return null;
421
+ const entries = [];
422
+ for (const [recordId, value] of state.values) {
423
+ entries.push({ recordId, value });
424
+ }
425
+ entries.sort((a, b) => compareTyped(a.value, b.value));
426
+ if (dir === "desc") entries.reverse();
427
+ return entries;
428
+ }
429
+ };
430
+ var EMPTY_SET2 = /* @__PURE__ */ new Set();
431
+ function stringifyKey2(value) {
432
+ if (value === null || value === void 0) return "\0NULL\0";
433
+ if (typeof value === "string") return value;
434
+ if (typeof value === "number" || typeof value === "boolean") return String(value);
435
+ if (value instanceof Date) return value.toISOString();
436
+ if (Array.isArray(value)) {
437
+ const parts = [];
438
+ for (const el of value) parts.push(stringifyKey2(el));
439
+ return JSON.stringify(parts);
440
+ }
441
+ return "\0OBJECT\0";
442
+ }
443
+ function addToState(state, recordId, value) {
444
+ if (value === null || value === void 0) return;
445
+ const key = stringifyKey2(value);
446
+ let bucket = state.buckets.get(key);
447
+ if (!bucket) {
448
+ bucket = /* @__PURE__ */ new Set();
449
+ state.buckets.set(key, bucket);
450
+ }
451
+ bucket.add(recordId);
452
+ state.values.set(recordId, value);
453
+ }
454
+ function removeFromState(state, recordId, value) {
455
+ if (value === null || value === void 0) return;
456
+ const key = stringifyKey2(value);
457
+ const bucket = state.buckets.get(key);
458
+ if (bucket) {
459
+ bucket.delete(recordId);
460
+ if (bucket.size === 0) state.buckets.delete(key);
461
+ }
462
+ state.values.delete(recordId);
463
+ }
464
+ function matchesRange(live, op, bound) {
465
+ if (op === "between") {
466
+ if (!Array.isArray(bound) || bound.length !== 2) return false;
467
+ return compareTyped(live, bound[0]) >= 0 && compareTyped(live, bound[1]) <= 0;
468
+ }
469
+ const cmp = compareTyped(live, bound);
470
+ switch (op) {
471
+ case "<":
472
+ return cmp < 0;
473
+ case "<=":
474
+ return cmp <= 0;
475
+ case ">":
476
+ return cmp > 0;
477
+ case ">=":
478
+ return cmp >= 0;
479
+ }
480
+ }
481
+ function compareTyped(a, b) {
482
+ if (a === void 0 || a === null) return b === void 0 || b === null ? 0 : 1;
483
+ if (b === void 0 || b === null) return -1;
484
+ if (typeof a === "number" && typeof b === "number") return a - b;
485
+ if (a instanceof Date && b instanceof Date) return a.getTime() - b.getTime();
486
+ if (typeof a === "string" && typeof b === "string") return a < b ? -1 : a > b ? 1 : 0;
487
+ if (typeof a === "boolean" && typeof b === "boolean") {
488
+ return a === b ? 0 : a ? 1 : -1;
489
+ }
490
+ return 0;
491
+ }
492
+
493
+ // src/indexing/active.ts
494
+ function withIndexing() {
495
+ return {
496
+ createState({ defs, lazy }) {
497
+ if (lazy) {
498
+ const persisted = new PersistedCollectionIndex();
499
+ declareAll(persisted, defs);
500
+ return makeLazyState(persisted);
501
+ }
502
+ const eager = new CollectionIndexes();
503
+ for (const def of defs) {
504
+ if (typeof def === "string") {
505
+ eager.declare(def);
506
+ } else if (Array.isArray(def)) {
507
+ for (const f of def) eager.declare(f);
508
+ } else {
509
+ for (const f of def.fields) eager.declare(f);
510
+ }
511
+ }
512
+ return makeEagerState(eager);
513
+ }
514
+ };
515
+ }
516
+ function declareAll(persisted, defs) {
517
+ for (const def of defs) {
518
+ if (typeof def === "string") {
519
+ persisted.declare(def);
520
+ } else if (Array.isArray(def)) {
521
+ persisted.declareComposite(def);
522
+ } else {
523
+ persisted.declareComposite(def.fields);
524
+ }
525
+ }
526
+ }
527
+ function makeEagerState(eager) {
528
+ return {
529
+ isEnabled: true,
530
+ getEagerIndexes: () => eager,
531
+ getPersistedIndexes: () => null
532
+ };
533
+ }
534
+ function makeLazyState(persisted) {
535
+ return {
536
+ isEnabled: true,
537
+ getEagerIndexes: () => null,
538
+ getPersistedIndexes: () => persisted
539
+ };
540
+ }
541
+
542
+ // src/errors.ts
543
+ var NoydbError = class extends Error {
544
+ /** Machine-readable error code. Stable across library versions. */
545
+ code;
546
+ constructor(code, message) {
547
+ super(message);
548
+ this.name = "NoydbError";
549
+ this.code = code;
550
+ }
551
+ };
552
+ var IndexRequiredError = class extends NoydbError {
553
+ collection;
554
+ touchedFields;
555
+ missingFields;
556
+ constructor(args) {
557
+ super(
558
+ "INDEX_REQUIRED",
559
+ `Collection "${args.collection}": query references unindexed fields in lazy mode (missing: ${args.missingFields.join(", ")}). Declare an index on each field, or use collection.scan() for non-indexed iteration.`
560
+ );
561
+ this.name = "IndexRequiredError";
562
+ this.collection = args.collection;
563
+ this.touchedFields = [...args.touchedFields];
564
+ this.missingFields = [...args.missingFields];
565
+ }
566
+ };
567
+
568
+ // src/indexing/lazy-builder.ts
569
+ var EMPTY_PLAN = {
570
+ clauses: [],
571
+ orderBy: [],
572
+ limit: void 0,
573
+ offset: 0
574
+ };
575
+ var LazyQuery = class _LazyQuery {
576
+ source;
577
+ plan;
578
+ constructor(source, plan = EMPTY_PLAN) {
579
+ this.source = source;
580
+ this.plan = plan;
581
+ }
582
+ where(field, op, value) {
583
+ const clause = { type: "field", field, op, value };
584
+ return new _LazyQuery(this.source, {
585
+ ...this.plan,
586
+ clauses: [...this.plan.clauses, clause]
587
+ });
588
+ }
589
+ orderBy(field, direction = "asc") {
590
+ return new _LazyQuery(this.source, {
591
+ ...this.plan,
592
+ orderBy: [...this.plan.orderBy, { field, direction }]
593
+ });
594
+ }
595
+ limit(n) {
596
+ return new _LazyQuery(this.source, { ...this.plan, limit: n });
597
+ }
598
+ offset(n) {
599
+ return new _LazyQuery(this.source, { ...this.plan, offset: n });
600
+ }
601
+ async toArray() {
602
+ await this.source.ensurePersistedIndexesLoaded();
603
+ const touchedFields = collectTouchedFields(this.plan);
604
+ const missingFields = touchedFields.filter((f) => !isFieldIndexed(f, this.source.persistedIndexes));
605
+ if (missingFields.length > 0) {
606
+ throw new IndexRequiredError({
607
+ collection: this.source.collectionName,
608
+ touchedFields,
609
+ missingFields
610
+ });
611
+ }
612
+ const candidateIds = this.resolveCandidateIds();
613
+ if (candidateIds === null) {
614
+ throw new IndexRequiredError({
615
+ collection: this.source.collectionName,
616
+ touchedFields,
617
+ missingFields: touchedFields
618
+ });
619
+ }
620
+ const records = [];
621
+ for (const id of candidateIds) {
622
+ const record = await this.source.getRecord(id);
623
+ if (record === null) continue;
624
+ if (!matchesAll(record, this.plan.clauses)) continue;
625
+ records.push(record);
626
+ }
627
+ const sorted = this.plan.orderBy.length > 0 ? sortRecords(records, this.plan.orderBy) : records;
628
+ const offset = this.plan.offset > 0 ? this.plan.offset : 0;
629
+ const limited = this.plan.limit === void 0 ? sorted.slice(offset) : sorted.slice(offset, offset + this.plan.limit);
630
+ return limited;
631
+ }
632
+ async first() {
633
+ const out = await this.limit(1).toArray();
634
+ return out.length > 0 ? out[0] : null;
635
+ }
636
+ async count() {
637
+ const out = await this.toArray();
638
+ return out.length;
639
+ }
640
+ /**
641
+ * Resolve the candidate record-id set to decrypt. Returns null when the
642
+ * query has no usable driver — no `==`/`in` clause and no `orderBy`
643
+ * clause that can scope the scan. Callers interpret null as
644
+ * IndexRequiredError (see `toArray`).
645
+ */
646
+ resolveCandidateIds() {
647
+ const idx = this.source.persistedIndexes;
648
+ const eqMap = /* @__PURE__ */ new Map();
649
+ for (const clause of this.plan.clauses) {
650
+ if (clause.op === "==") eqMap.set(clause.field, clause.value);
651
+ }
652
+ if (eqMap.size >= 2) {
653
+ for (const def of idx.definitions()) {
654
+ if (def.kind !== "composite") continue;
655
+ if (def.fields.every((f) => eqMap.has(f))) {
656
+ const tuple = def.fields.map((f) => eqMap.get(f));
657
+ const ids = idx.lookupEqual(def.key, tuple);
658
+ if (ids) return [...ids];
659
+ }
660
+ }
661
+ }
662
+ for (const clause of this.plan.clauses) {
663
+ if (clause.op === "==") {
664
+ const ids = idx.lookupEqual(clause.field, clause.value);
665
+ if (ids) return [...ids];
666
+ } else if (clause.op === "in" && Array.isArray(clause.value)) {
667
+ const ids = idx.lookupIn(clause.field, clause.value);
668
+ if (ids) return [...ids];
669
+ } else if (isRangeOp(clause.op)) {
670
+ const ids = idx.lookupRange(clause.field, clause.op, clause.value);
671
+ if (ids) return [...ids];
672
+ }
673
+ }
674
+ if (this.plan.orderBy.length > 0) {
675
+ const primary = this.plan.orderBy[0];
676
+ const entries = idx.orderedBy(primary.field, primary.direction);
677
+ if (entries) return entries.map((e) => e.recordId);
678
+ }
679
+ return null;
680
+ }
681
+ };
682
+ function isFieldIndexed(field, idx) {
683
+ if (idx.has(field)) return true;
684
+ for (const def of idx.definitions()) {
685
+ if (def.kind === "composite" && def.fields.includes(field)) return true;
686
+ }
687
+ return false;
688
+ }
689
+ function isRangeOp(op) {
690
+ return op === "<" || op === "<=" || op === ">" || op === ">=" || op === "between";
691
+ }
692
+ function collectTouchedFields(plan) {
693
+ const seen = /* @__PURE__ */ new Set();
694
+ for (const c of plan.clauses) seen.add(c.field);
695
+ for (const o of plan.orderBy) seen.add(o.field);
696
+ return [...seen];
697
+ }
698
+ function matchesAll(record, clauses) {
699
+ for (const c of clauses) {
700
+ if (!evaluateClause(record, c)) return false;
701
+ }
702
+ return true;
703
+ }
704
+ function sortRecords(records, orderBy) {
705
+ return [...records].sort((a, b) => {
706
+ for (const { field, direction } of orderBy) {
707
+ const av = readPath(a, field);
708
+ const bv = readPath(b, field);
709
+ const cmp = compareValues(av, bv);
710
+ if (cmp !== 0) return direction === "asc" ? cmp : -cmp;
711
+ }
712
+ return 0;
713
+ });
714
+ }
715
+ function compareValues(a, b) {
716
+ if (a === void 0 || a === null) return b === void 0 || b === null ? 0 : 1;
717
+ if (b === void 0 || b === null) return -1;
718
+ if (typeof a === "number" && typeof b === "number") return a - b;
719
+ if (typeof a === "string" && typeof b === "string") return a < b ? -1 : a > b ? 1 : 0;
720
+ if (a instanceof Date && b instanceof Date) return a.getTime() - b.getTime();
721
+ return 0;
722
+ }
723
+ // Annotate the CommonJS export names for ESM import in node:
724
+ 0 && (module.exports = {
725
+ COMPOSITE_DELIMITER,
726
+ CollectionIndexes,
727
+ IDX_PREFIX,
728
+ LazyQuery,
729
+ PersistedCollectionIndex,
730
+ compositeKey,
731
+ decodeIdxId,
732
+ encodeIdxId,
733
+ isIdxId,
734
+ withIndexing
735
+ });
736
+ //# sourceMappingURL=index.cjs.map