@tanstack/db 0.0.27 → 0.0.30

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 (167) hide show
  1. package/dist/cjs/change-events.cjs +141 -0
  2. package/dist/cjs/change-events.cjs.map +1 -0
  3. package/dist/cjs/change-events.d.cts +49 -0
  4. package/dist/cjs/collection.cjs +234 -86
  5. package/dist/cjs/collection.cjs.map +1 -1
  6. package/dist/cjs/collection.d.cts +95 -20
  7. package/dist/cjs/errors.cjs +509 -1
  8. package/dist/cjs/errors.cjs.map +1 -1
  9. package/dist/cjs/errors.d.cts +225 -1
  10. package/dist/cjs/index.cjs +82 -3
  11. package/dist/cjs/index.cjs.map +1 -1
  12. package/dist/cjs/index.d.cts +5 -1
  13. package/dist/cjs/indexes/auto-index.cjs +64 -0
  14. package/dist/cjs/indexes/auto-index.cjs.map +1 -0
  15. package/dist/cjs/indexes/auto-index.d.cts +9 -0
  16. package/dist/cjs/indexes/base-index.cjs +46 -0
  17. package/dist/cjs/indexes/base-index.cjs.map +1 -0
  18. package/dist/cjs/indexes/base-index.d.cts +54 -0
  19. package/dist/cjs/indexes/btree-index.cjs +191 -0
  20. package/dist/cjs/indexes/btree-index.cjs.map +1 -0
  21. package/dist/cjs/indexes/btree-index.d.cts +74 -0
  22. package/dist/cjs/indexes/index-options.d.cts +13 -0
  23. package/dist/cjs/indexes/lazy-index.cjs +193 -0
  24. package/dist/cjs/indexes/lazy-index.cjs.map +1 -0
  25. package/dist/cjs/indexes/lazy-index.d.cts +96 -0
  26. package/dist/cjs/local-storage.cjs +9 -15
  27. package/dist/cjs/local-storage.cjs.map +1 -1
  28. package/dist/cjs/query/builder/functions.cjs +11 -0
  29. package/dist/cjs/query/builder/functions.cjs.map +1 -1
  30. package/dist/cjs/query/builder/functions.d.cts +4 -0
  31. package/dist/cjs/query/builder/index.cjs +6 -7
  32. package/dist/cjs/query/builder/index.cjs.map +1 -1
  33. package/dist/cjs/query/builder/ref-proxy.cjs +37 -0
  34. package/dist/cjs/query/builder/ref-proxy.cjs.map +1 -1
  35. package/dist/cjs/query/builder/ref-proxy.d.cts +12 -0
  36. package/dist/cjs/query/compiler/evaluators.cjs +83 -58
  37. package/dist/cjs/query/compiler/evaluators.cjs.map +1 -1
  38. package/dist/cjs/query/compiler/evaluators.d.cts +8 -0
  39. package/dist/cjs/query/compiler/expressions.cjs +61 -0
  40. package/dist/cjs/query/compiler/expressions.cjs.map +1 -0
  41. package/dist/cjs/query/compiler/expressions.d.cts +25 -0
  42. package/dist/cjs/query/compiler/group-by.cjs +5 -10
  43. package/dist/cjs/query/compiler/group-by.cjs.map +1 -1
  44. package/dist/cjs/query/compiler/index.cjs +23 -17
  45. package/dist/cjs/query/compiler/index.cjs.map +1 -1
  46. package/dist/cjs/query/compiler/index.d.cts +12 -3
  47. package/dist/cjs/query/compiler/joins.cjs +61 -12
  48. package/dist/cjs/query/compiler/joins.cjs.map +1 -1
  49. package/dist/cjs/query/compiler/order-by.cjs +4 -34
  50. package/dist/cjs/query/compiler/order-by.cjs.map +1 -1
  51. package/dist/cjs/query/compiler/types.d.cts +2 -2
  52. package/dist/cjs/query/live-query-collection.cjs +54 -12
  53. package/dist/cjs/query/live-query-collection.cjs.map +1 -1
  54. package/dist/cjs/query/optimizer.cjs +45 -7
  55. package/dist/cjs/query/optimizer.cjs.map +1 -1
  56. package/dist/cjs/query/optimizer.d.cts +13 -3
  57. package/dist/cjs/transactions.cjs +5 -4
  58. package/dist/cjs/transactions.cjs.map +1 -1
  59. package/dist/cjs/types.d.cts +31 -0
  60. package/dist/cjs/utils/array-utils.d.cts +8 -0
  61. package/dist/cjs/utils/btree.cjs +677 -0
  62. package/dist/cjs/utils/btree.cjs.map +1 -0
  63. package/dist/cjs/utils/btree.d.cts +197 -0
  64. package/dist/cjs/utils/comparison.cjs +52 -0
  65. package/dist/cjs/utils/comparison.cjs.map +1 -0
  66. package/dist/cjs/utils/comparison.d.cts +11 -0
  67. package/dist/cjs/utils/index-optimization.cjs +270 -0
  68. package/dist/cjs/utils/index-optimization.cjs.map +1 -0
  69. package/dist/cjs/utils/index-optimization.d.cts +29 -0
  70. package/dist/esm/change-events.d.ts +49 -0
  71. package/dist/esm/change-events.js +141 -0
  72. package/dist/esm/change-events.js.map +1 -0
  73. package/dist/esm/collection.d.ts +95 -20
  74. package/dist/esm/collection.js +232 -84
  75. package/dist/esm/collection.js.map +1 -1
  76. package/dist/esm/errors.d.ts +225 -1
  77. package/dist/esm/errors.js +510 -2
  78. package/dist/esm/errors.js.map +1 -1
  79. package/dist/esm/index.d.ts +5 -1
  80. package/dist/esm/index.js +81 -2
  81. package/dist/esm/index.js.map +1 -1
  82. package/dist/esm/indexes/auto-index.d.ts +9 -0
  83. package/dist/esm/indexes/auto-index.js +64 -0
  84. package/dist/esm/indexes/auto-index.js.map +1 -0
  85. package/dist/esm/indexes/base-index.d.ts +54 -0
  86. package/dist/esm/indexes/base-index.js +46 -0
  87. package/dist/esm/indexes/base-index.js.map +1 -0
  88. package/dist/esm/indexes/btree-index.d.ts +74 -0
  89. package/dist/esm/indexes/btree-index.js +191 -0
  90. package/dist/esm/indexes/btree-index.js.map +1 -0
  91. package/dist/esm/indexes/index-options.d.ts +13 -0
  92. package/dist/esm/indexes/lazy-index.d.ts +96 -0
  93. package/dist/esm/indexes/lazy-index.js +193 -0
  94. package/dist/esm/indexes/lazy-index.js.map +1 -0
  95. package/dist/esm/local-storage.js +9 -15
  96. package/dist/esm/local-storage.js.map +1 -1
  97. package/dist/esm/query/builder/functions.d.ts +4 -0
  98. package/dist/esm/query/builder/functions.js +11 -0
  99. package/dist/esm/query/builder/functions.js.map +1 -1
  100. package/dist/esm/query/builder/index.js +6 -7
  101. package/dist/esm/query/builder/index.js.map +1 -1
  102. package/dist/esm/query/builder/ref-proxy.d.ts +12 -0
  103. package/dist/esm/query/builder/ref-proxy.js +37 -0
  104. package/dist/esm/query/builder/ref-proxy.js.map +1 -1
  105. package/dist/esm/query/compiler/evaluators.d.ts +8 -0
  106. package/dist/esm/query/compiler/evaluators.js +84 -59
  107. package/dist/esm/query/compiler/evaluators.js.map +1 -1
  108. package/dist/esm/query/compiler/expressions.d.ts +25 -0
  109. package/dist/esm/query/compiler/expressions.js +61 -0
  110. package/dist/esm/query/compiler/expressions.js.map +1 -0
  111. package/dist/esm/query/compiler/group-by.js +5 -10
  112. package/dist/esm/query/compiler/group-by.js.map +1 -1
  113. package/dist/esm/query/compiler/index.d.ts +12 -3
  114. package/dist/esm/query/compiler/index.js +23 -17
  115. package/dist/esm/query/compiler/index.js.map +1 -1
  116. package/dist/esm/query/compiler/joins.js +61 -12
  117. package/dist/esm/query/compiler/joins.js.map +1 -1
  118. package/dist/esm/query/compiler/order-by.js +1 -31
  119. package/dist/esm/query/compiler/order-by.js.map +1 -1
  120. package/dist/esm/query/compiler/types.d.ts +2 -2
  121. package/dist/esm/query/live-query-collection.js +54 -12
  122. package/dist/esm/query/live-query-collection.js.map +1 -1
  123. package/dist/esm/query/optimizer.d.ts +13 -3
  124. package/dist/esm/query/optimizer.js +40 -2
  125. package/dist/esm/query/optimizer.js.map +1 -1
  126. package/dist/esm/transactions.js +5 -4
  127. package/dist/esm/transactions.js.map +1 -1
  128. package/dist/esm/types.d.ts +31 -0
  129. package/dist/esm/utils/array-utils.d.ts +8 -0
  130. package/dist/esm/utils/btree.d.ts +197 -0
  131. package/dist/esm/utils/btree.js +677 -0
  132. package/dist/esm/utils/btree.js.map +1 -0
  133. package/dist/esm/utils/comparison.d.ts +11 -0
  134. package/dist/esm/utils/comparison.js +52 -0
  135. package/dist/esm/utils/comparison.js.map +1 -0
  136. package/dist/esm/utils/index-optimization.d.ts +29 -0
  137. package/dist/esm/utils/index-optimization.js +270 -0
  138. package/dist/esm/utils/index-optimization.js.map +1 -0
  139. package/package.json +1 -1
  140. package/src/change-events.ts +257 -0
  141. package/src/collection.ts +316 -105
  142. package/src/errors.ts +545 -1
  143. package/src/index.ts +7 -1
  144. package/src/indexes/auto-index.ts +108 -0
  145. package/src/indexes/base-index.ts +119 -0
  146. package/src/indexes/btree-index.ts +263 -0
  147. package/src/indexes/index-options.ts +42 -0
  148. package/src/indexes/lazy-index.ts +251 -0
  149. package/src/local-storage.ts +16 -17
  150. package/src/query/builder/functions.ts +14 -0
  151. package/src/query/builder/index.ts +12 -7
  152. package/src/query/builder/ref-proxy.ts +65 -0
  153. package/src/query/compiler/evaluators.ts +114 -62
  154. package/src/query/compiler/expressions.ts +92 -0
  155. package/src/query/compiler/group-by.ts +10 -10
  156. package/src/query/compiler/index.ts +52 -22
  157. package/src/query/compiler/joins.ts +114 -15
  158. package/src/query/compiler/order-by.ts +1 -45
  159. package/src/query/compiler/types.ts +2 -2
  160. package/src/query/live-query-collection.ts +95 -15
  161. package/src/query/optimizer.ts +94 -5
  162. package/src/transactions.ts +10 -4
  163. package/src/types.ts +38 -0
  164. package/src/utils/array-utils.ts +28 -0
  165. package/src/utils/btree.ts +1010 -0
  166. package/src/utils/comparison.ts +79 -0
  167. package/src/utils/index-optimization.ts +546 -0
@@ -0,0 +1,191 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const comparison = require("../utils/comparison.cjs");
4
+ const btree = require("../utils/btree.cjs");
5
+ const baseIndex = require("./base-index.cjs");
6
+ class BTreeIndex extends baseIndex.BaseIndex {
7
+ constructor(id, expression, name, options) {
8
+ super(id, expression, name, options);
9
+ this.supportedOperations = /* @__PURE__ */ new Set([
10
+ `eq`,
11
+ `gt`,
12
+ `gte`,
13
+ `lt`,
14
+ `lte`,
15
+ `in`
16
+ ]);
17
+ this.valueMap = /* @__PURE__ */ new Map();
18
+ this.indexedKeys = /* @__PURE__ */ new Set();
19
+ this.compareFn = comparison.ascComparator;
20
+ this.compareFn = (options == null ? void 0 : options.compareFn) ?? comparison.ascComparator;
21
+ this.orderedEntries = new btree.BTree(this.compareFn);
22
+ }
23
+ initialize(_options) {
24
+ }
25
+ /**
26
+ * Adds a value to the index
27
+ */
28
+ add(key, item) {
29
+ let indexedValue;
30
+ try {
31
+ indexedValue = this.evaluateIndexExpression(item);
32
+ } catch (error) {
33
+ throw new Error(
34
+ `Failed to evaluate index expression for key ${key}: ${error}`
35
+ );
36
+ }
37
+ if (this.valueMap.has(indexedValue)) {
38
+ this.valueMap.get(indexedValue).add(key);
39
+ } else {
40
+ const keySet = /* @__PURE__ */ new Set([key]);
41
+ this.valueMap.set(indexedValue, keySet);
42
+ this.orderedEntries.set(indexedValue, void 0);
43
+ }
44
+ this.indexedKeys.add(key);
45
+ this.updateTimestamp();
46
+ }
47
+ /**
48
+ * Removes a value from the index
49
+ */
50
+ remove(key, item) {
51
+ let indexedValue;
52
+ try {
53
+ indexedValue = this.evaluateIndexExpression(item);
54
+ } catch (error) {
55
+ console.warn(
56
+ `Failed to evaluate index expression for key ${key} during removal:`,
57
+ error
58
+ );
59
+ return;
60
+ }
61
+ if (this.valueMap.has(indexedValue)) {
62
+ const keySet = this.valueMap.get(indexedValue);
63
+ keySet.delete(key);
64
+ if (keySet.size === 0) {
65
+ this.valueMap.delete(indexedValue);
66
+ this.orderedEntries.delete(indexedValue);
67
+ }
68
+ }
69
+ this.indexedKeys.delete(key);
70
+ this.updateTimestamp();
71
+ }
72
+ /**
73
+ * Updates a value in the index
74
+ */
75
+ update(key, oldItem, newItem) {
76
+ this.remove(key, oldItem);
77
+ this.add(key, newItem);
78
+ }
79
+ /**
80
+ * Builds the index from a collection of entries
81
+ */
82
+ build(entries) {
83
+ this.clear();
84
+ for (const [key, item] of entries) {
85
+ this.add(key, item);
86
+ }
87
+ }
88
+ /**
89
+ * Clears all data from the index
90
+ */
91
+ clear() {
92
+ this.orderedEntries.clear();
93
+ this.valueMap.clear();
94
+ this.indexedKeys.clear();
95
+ this.updateTimestamp();
96
+ }
97
+ /**
98
+ * Performs a lookup operation
99
+ */
100
+ lookup(operation, value) {
101
+ const startTime = performance.now();
102
+ let result;
103
+ switch (operation) {
104
+ case `eq`:
105
+ result = this.equalityLookup(value);
106
+ break;
107
+ case `gt`:
108
+ result = this.rangeQuery({ from: value, fromInclusive: false });
109
+ break;
110
+ case `gte`:
111
+ result = this.rangeQuery({ from: value, fromInclusive: true });
112
+ break;
113
+ case `lt`:
114
+ result = this.rangeQuery({ to: value, toInclusive: false });
115
+ break;
116
+ case `lte`:
117
+ result = this.rangeQuery({ to: value, toInclusive: true });
118
+ break;
119
+ case `in`:
120
+ result = this.inArrayLookup(value);
121
+ break;
122
+ default:
123
+ throw new Error(`Operation ${operation} not supported by BTreeIndex`);
124
+ }
125
+ this.trackLookup(startTime);
126
+ return result;
127
+ }
128
+ /**
129
+ * Gets the number of indexed keys
130
+ */
131
+ get keyCount() {
132
+ return this.indexedKeys.size;
133
+ }
134
+ // Public methods for backward compatibility (used by tests)
135
+ /**
136
+ * Performs an equality lookup
137
+ */
138
+ equalityLookup(value) {
139
+ return new Set(this.valueMap.get(value) ?? []);
140
+ }
141
+ /**
142
+ * Performs a range query with options
143
+ * This is more efficient for compound queries like "WHERE a > 5 AND a < 10"
144
+ */
145
+ rangeQuery(options = {}) {
146
+ const { from, to, fromInclusive = true, toInclusive = true } = options;
147
+ const result = /* @__PURE__ */ new Set();
148
+ const fromKey = from ?? this.orderedEntries.minKey();
149
+ const toKey = to ?? this.orderedEntries.maxKey();
150
+ this.orderedEntries.forRange(
151
+ fromKey,
152
+ toKey,
153
+ toInclusive,
154
+ (indexedValue, _) => {
155
+ if (!fromInclusive && this.compareFn(indexedValue, from) === 0) {
156
+ return;
157
+ }
158
+ const keys = this.valueMap.get(indexedValue);
159
+ if (keys) {
160
+ keys.forEach((key) => result.add(key));
161
+ }
162
+ }
163
+ );
164
+ return result;
165
+ }
166
+ /**
167
+ * Performs an IN array lookup
168
+ */
169
+ inArrayLookup(values) {
170
+ const result = /* @__PURE__ */ new Set();
171
+ for (const value of values) {
172
+ const keys = this.valueMap.get(value);
173
+ if (keys) {
174
+ keys.forEach((key) => result.add(key));
175
+ }
176
+ }
177
+ return result;
178
+ }
179
+ // Getter methods for testing compatibility
180
+ get indexedKeysSet() {
181
+ return this.indexedKeys;
182
+ }
183
+ get orderedEntriesArray() {
184
+ return this.orderedEntries.keysArray().map((key) => [key, this.valueMap.get(key) ?? /* @__PURE__ */ new Set()]);
185
+ }
186
+ get valueMapData() {
187
+ return this.valueMap;
188
+ }
189
+ }
190
+ exports.BTreeIndex = BTreeIndex;
191
+ //# sourceMappingURL=btree-index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"btree-index.cjs","sources":["../../../src/indexes/btree-index.ts"],"sourcesContent":["import { ascComparator } from \"../utils/comparison.js\"\nimport { BTree } from \"../utils/btree.js\"\nimport { BaseIndex } from \"./base-index.js\"\nimport type { BasicExpression } from \"../query/ir.js\"\nimport type { IndexOperation } from \"./base-index.js\"\n\n/**\n * Options for Ordered index\n */\nexport interface BTreeIndexOptions {\n compareFn?: (a: any, b: any) => number\n}\n\n/**\n * Options for range queries\n */\nexport interface RangeQueryOptions {\n from?: any\n to?: any\n fromInclusive?: boolean\n toInclusive?: boolean\n}\n\n/**\n * B+Tree index for sorted data with range queries\n * This maintains items in sorted order and provides efficient range operations\n */\nexport class BTreeIndex<\n TKey extends string | number = string | number,\n> extends BaseIndex<TKey> {\n public readonly supportedOperations = new Set<IndexOperation>([\n `eq`,\n `gt`,\n `gte`,\n `lt`,\n `lte`,\n `in`,\n ])\n\n // Internal data structures - private to hide implementation details\n // The `orderedEntries` B+ tree is used for efficient range queries\n // The `valueMap` is used for O(1) lookups of PKs by indexed value\n private orderedEntries: BTree<any, undefined> // we don't associate values with the keys of the B+ tree (the keys are indexed values)\n private valueMap = new Map<any, Set<TKey>>() // instead we store a mapping of indexed values to a set of PKs\n private indexedKeys = new Set<TKey>()\n private compareFn: (a: any, b: any) => number = ascComparator\n\n constructor(\n id: number,\n expression: BasicExpression,\n name?: string,\n options?: any\n ) {\n super(id, expression, name, options)\n this.compareFn = options?.compareFn ?? ascComparator\n this.orderedEntries = new BTree(this.compareFn)\n }\n\n protected initialize(_options?: BTreeIndexOptions): void {}\n\n /**\n * Adds a value to the index\n */\n add(key: TKey, item: any): void {\n let indexedValue: any\n try {\n indexedValue = this.evaluateIndexExpression(item)\n } catch (error) {\n throw new Error(\n `Failed to evaluate index expression for key ${key}: ${error}`\n )\n }\n\n // Check if this value already exists\n if (this.valueMap.has(indexedValue)) {\n // Add to existing set\n this.valueMap.get(indexedValue)!.add(key)\n } else {\n // Create new set for this value\n const keySet = new Set<TKey>([key])\n this.valueMap.set(indexedValue, keySet)\n this.orderedEntries.set(indexedValue, undefined)\n }\n\n this.indexedKeys.add(key)\n this.updateTimestamp()\n }\n\n /**\n * Removes a value from the index\n */\n remove(key: TKey, item: any): void {\n let indexedValue: any\n try {\n indexedValue = this.evaluateIndexExpression(item)\n } catch (error) {\n console.warn(\n `Failed to evaluate index expression for key ${key} during removal:`,\n error\n )\n return\n }\n\n if (this.valueMap.has(indexedValue)) {\n const keySet = this.valueMap.get(indexedValue)!\n keySet.delete(key)\n\n // If set is now empty, remove the entry entirely\n if (keySet.size === 0) {\n this.valueMap.delete(indexedValue)\n\n // Remove from ordered entries\n this.orderedEntries.delete(indexedValue)\n }\n }\n\n this.indexedKeys.delete(key)\n this.updateTimestamp()\n }\n\n /**\n * Updates a value in the index\n */\n update(key: TKey, oldItem: any, newItem: any): void {\n this.remove(key, oldItem)\n this.add(key, newItem)\n }\n\n /**\n * Builds the index from a collection of entries\n */\n build(entries: Iterable<[TKey, any]>): void {\n this.clear()\n\n for (const [key, item] of entries) {\n this.add(key, item)\n }\n }\n\n /**\n * Clears all data from the index\n */\n clear(): void {\n this.orderedEntries.clear()\n this.valueMap.clear()\n this.indexedKeys.clear()\n this.updateTimestamp()\n }\n\n /**\n * Performs a lookup operation\n */\n lookup(operation: IndexOperation, value: any): Set<TKey> {\n const startTime = performance.now()\n\n let result: Set<TKey>\n\n switch (operation) {\n case `eq`:\n result = this.equalityLookup(value)\n break\n case `gt`:\n result = this.rangeQuery({ from: value, fromInclusive: false })\n break\n case `gte`:\n result = this.rangeQuery({ from: value, fromInclusive: true })\n break\n case `lt`:\n result = this.rangeQuery({ to: value, toInclusive: false })\n break\n case `lte`:\n result = this.rangeQuery({ to: value, toInclusive: true })\n break\n case `in`:\n result = this.inArrayLookup(value)\n break\n default:\n throw new Error(`Operation ${operation} not supported by BTreeIndex`)\n }\n\n this.trackLookup(startTime)\n return result\n }\n\n /**\n * Gets the number of indexed keys\n */\n get keyCount(): number {\n return this.indexedKeys.size\n }\n\n // Public methods for backward compatibility (used by tests)\n\n /**\n * Performs an equality lookup\n */\n equalityLookup(value: any): Set<TKey> {\n return new Set(this.valueMap.get(value) ?? [])\n }\n\n /**\n * Performs a range query with options\n * This is more efficient for compound queries like \"WHERE a > 5 AND a < 10\"\n */\n rangeQuery(options: RangeQueryOptions = {}): Set<TKey> {\n const { from, to, fromInclusive = true, toInclusive = true } = options\n const result = new Set<TKey>()\n\n const fromKey = from ?? this.orderedEntries.minKey()\n const toKey = to ?? this.orderedEntries.maxKey()\n\n this.orderedEntries.forRange(\n fromKey,\n toKey,\n toInclusive,\n (indexedValue, _) => {\n if (!fromInclusive && this.compareFn(indexedValue, from) === 0) {\n // the B+ tree `forRange` method does not support exclusive lower bounds\n // so we need to exclude it manually\n return\n }\n\n const keys = this.valueMap.get(indexedValue)\n if (keys) {\n keys.forEach((key) => result.add(key))\n }\n }\n )\n\n return result\n }\n\n /**\n * Performs an IN array lookup\n */\n inArrayLookup(values: Array<any>): Set<TKey> {\n const result = new Set<TKey>()\n\n for (const value of values) {\n const keys = this.valueMap.get(value)\n if (keys) {\n keys.forEach((key) => result.add(key))\n }\n }\n\n return result\n }\n\n // Getter methods for testing compatibility\n get indexedKeysSet(): Set<TKey> {\n return this.indexedKeys\n }\n\n get orderedEntriesArray(): Array<[any, Set<TKey>]> {\n return this.orderedEntries\n .keysArray()\n .map((key) => [key, this.valueMap.get(key) ?? new Set()])\n }\n\n get valueMapData(): Map<any, Set<TKey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sBAA+C;AACjD,WAAO,KAAK,eACT,UAAA,EACA,IAAI,CAAC,QAAQ,CAAC,KAAK,KAAK,SAAS,IAAI,GAAG,KAAK,oBAAI,IAAA,CAAK,CAAC;AAAA,EAC5D;AAAA,EAEA,IAAI,eAAoC;AACtC,WAAO,KAAK;AAAA,EACd;AACF;;"}
@@ -0,0 +1,74 @@
1
+ import { BaseIndex, IndexOperation } from './base-index.js';
2
+ import { BasicExpression } from '../query/ir.js';
3
+ /**
4
+ * Options for Ordered index
5
+ */
6
+ export interface BTreeIndexOptions {
7
+ compareFn?: (a: any, b: any) => number;
8
+ }
9
+ /**
10
+ * Options for range queries
11
+ */
12
+ export interface RangeQueryOptions {
13
+ from?: any;
14
+ to?: any;
15
+ fromInclusive?: boolean;
16
+ toInclusive?: boolean;
17
+ }
18
+ /**
19
+ * B+Tree index for sorted data with range queries
20
+ * This maintains items in sorted order and provides efficient range operations
21
+ */
22
+ export declare class BTreeIndex<TKey extends string | number = string | number> extends BaseIndex<TKey> {
23
+ readonly supportedOperations: Set<"eq" | "gt" | "gte" | "lt" | "lte" | "in" | "like" | "ilike">;
24
+ private orderedEntries;
25
+ private valueMap;
26
+ private indexedKeys;
27
+ private compareFn;
28
+ constructor(id: number, expression: BasicExpression, name?: string, options?: any);
29
+ protected initialize(_options?: BTreeIndexOptions): void;
30
+ /**
31
+ * Adds a value to the index
32
+ */
33
+ add(key: TKey, item: any): void;
34
+ /**
35
+ * Removes a value from the index
36
+ */
37
+ remove(key: TKey, item: any): void;
38
+ /**
39
+ * Updates a value in the index
40
+ */
41
+ update(key: TKey, oldItem: any, newItem: any): void;
42
+ /**
43
+ * Builds the index from a collection of entries
44
+ */
45
+ build(entries: Iterable<[TKey, any]>): void;
46
+ /**
47
+ * Clears all data from the index
48
+ */
49
+ clear(): void;
50
+ /**
51
+ * Performs a lookup operation
52
+ */
53
+ lookup(operation: IndexOperation, value: any): Set<TKey>;
54
+ /**
55
+ * Gets the number of indexed keys
56
+ */
57
+ get keyCount(): number;
58
+ /**
59
+ * Performs an equality lookup
60
+ */
61
+ equalityLookup(value: any): Set<TKey>;
62
+ /**
63
+ * Performs a range query with options
64
+ * This is more efficient for compound queries like "WHERE a > 5 AND a < 10"
65
+ */
66
+ rangeQuery(options?: RangeQueryOptions): Set<TKey>;
67
+ /**
68
+ * Performs an IN array lookup
69
+ */
70
+ inArrayLookup(values: Array<any>): Set<TKey>;
71
+ get indexedKeysSet(): Set<TKey>;
72
+ get orderedEntriesArray(): Array<[any, Set<TKey>]>;
73
+ get valueMapData(): Map<any, Set<TKey>>;
74
+ }
@@ -0,0 +1,13 @@
1
+ import { IndexConstructor, IndexResolver } from './base-index.js';
2
+ /**
3
+ * Enhanced index options that support both sync and async resolvers
4
+ */
5
+ export interface IndexOptions<TResolver extends IndexResolver = IndexResolver> {
6
+ name?: string;
7
+ indexType?: TResolver;
8
+ options?: TResolver extends IndexConstructor<any> ? TResolver extends new (id: string, expr: any, name?: string, options?: infer O) => any ? O : never : TResolver extends () => Promise<infer TCtor> ? TCtor extends new (id: string, expr: any, name?: string, options?: infer O) => any ? O : never : never;
9
+ }
10
+ /**
11
+ * Utility type to extract the constructed index type from a resolver
12
+ */
13
+ export type ResolvedIndexType<TResolver extends IndexResolver> = TResolver extends IndexConstructor<any> ? InstanceType<TResolver> : TResolver extends () => Promise<IndexConstructor<any>> ? TResolver extends () => Promise<infer TCtor> ? TCtor extends IndexConstructor<any> ? InstanceType<TCtor> : never : never : never;
@@ -0,0 +1,193 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ function isConstructor(resolver) {
4
+ return typeof resolver === `function` && resolver.prototype !== void 0 && resolver.prototype.constructor === resolver;
5
+ }
6
+ async function resolveIndexConstructor(resolver) {
7
+ if (isConstructor(resolver)) {
8
+ return resolver;
9
+ } else {
10
+ return await resolver();
11
+ }
12
+ }
13
+ class LazyIndexWrapper {
14
+ constructor(id, expression, name, resolver, options, collectionEntries) {
15
+ this.id = id;
16
+ this.expression = expression;
17
+ this.name = name;
18
+ this.resolver = resolver;
19
+ this.options = options;
20
+ this.collectionEntries = collectionEntries;
21
+ this.indexPromise = null;
22
+ this.resolvedIndex = null;
23
+ if (isConstructor(this.resolver)) {
24
+ this.resolvedIndex = new this.resolver(
25
+ this.id,
26
+ this.expression,
27
+ this.name,
28
+ this.options
29
+ );
30
+ if (this.collectionEntries) {
31
+ this.resolvedIndex.build(this.collectionEntries);
32
+ }
33
+ }
34
+ }
35
+ /**
36
+ * Resolve the actual index
37
+ */
38
+ async resolve() {
39
+ if (this.resolvedIndex) {
40
+ return this.resolvedIndex;
41
+ }
42
+ if (!this.indexPromise) {
43
+ this.indexPromise = this.createIndex();
44
+ }
45
+ this.resolvedIndex = await this.indexPromise;
46
+ return this.resolvedIndex;
47
+ }
48
+ /**
49
+ * Check if already resolved
50
+ */
51
+ isResolved() {
52
+ return this.resolvedIndex !== null;
53
+ }
54
+ /**
55
+ * Get resolved index (throws if not ready)
56
+ */
57
+ getResolved() {
58
+ if (!this.resolvedIndex) {
59
+ throw new Error(
60
+ `Index ${this.id} has not been resolved yet. Ensure collection is synced.`
61
+ );
62
+ }
63
+ return this.resolvedIndex;
64
+ }
65
+ /**
66
+ * Get the index ID
67
+ */
68
+ getId() {
69
+ return this.id;
70
+ }
71
+ /**
72
+ * Get the index name
73
+ */
74
+ getName() {
75
+ return this.name;
76
+ }
77
+ /**
78
+ * Get the index expression
79
+ */
80
+ getExpression() {
81
+ return this.expression;
82
+ }
83
+ async createIndex() {
84
+ const IndexClass = await resolveIndexConstructor(this.resolver);
85
+ return new IndexClass(this.id, this.expression, this.name, this.options);
86
+ }
87
+ }
88
+ class IndexProxy {
89
+ constructor(indexId, lazyIndex) {
90
+ this.indexId = indexId;
91
+ this.lazyIndex = lazyIndex;
92
+ }
93
+ /**
94
+ * Get the resolved index (throws if not ready)
95
+ */
96
+ get index() {
97
+ return this.lazyIndex.getResolved();
98
+ }
99
+ /**
100
+ * Check if index is ready
101
+ */
102
+ get isReady() {
103
+ return this.lazyIndex.isResolved();
104
+ }
105
+ /**
106
+ * Wait for index to be ready
107
+ */
108
+ async whenReady() {
109
+ return await this.lazyIndex.resolve();
110
+ }
111
+ /**
112
+ * Get the index ID
113
+ */
114
+ get id() {
115
+ return this.indexId;
116
+ }
117
+ /**
118
+ * Get the index name (throws if not ready)
119
+ */
120
+ get name() {
121
+ if (this.isReady) {
122
+ return this.index.name;
123
+ }
124
+ return this.lazyIndex.getName();
125
+ }
126
+ /**
127
+ * Get the index expression (available immediately)
128
+ */
129
+ get expression() {
130
+ return this.lazyIndex.getExpression();
131
+ }
132
+ /**
133
+ * Check if index supports an operation (throws if not ready)
134
+ */
135
+ supports(operation) {
136
+ return this.index.supports(operation);
137
+ }
138
+ /**
139
+ * Get index statistics (throws if not ready)
140
+ */
141
+ getStats() {
142
+ return this.index.getStats();
143
+ }
144
+ /**
145
+ * Check if index matches a field path (available immediately)
146
+ */
147
+ matchesField(fieldPath) {
148
+ const expr = this.expression;
149
+ return expr.type === `ref` && expr.path.length === fieldPath.length && expr.path.every((part, i) => part === fieldPath[i]);
150
+ }
151
+ /**
152
+ * Get the key count (throws if not ready)
153
+ */
154
+ get keyCount() {
155
+ return this.index.keyCount;
156
+ }
157
+ // Test compatibility properties - delegate to resolved index
158
+ get indexedKeysSet() {
159
+ const resolved = this.index;
160
+ return resolved.indexedKeysSet;
161
+ }
162
+ get orderedEntriesArray() {
163
+ const resolved = this.index;
164
+ return resolved.orderedEntriesArray;
165
+ }
166
+ get valueMapData() {
167
+ const resolved = this.index;
168
+ return resolved.valueMapData;
169
+ }
170
+ // BTreeIndex compatibility methods
171
+ equalityLookup(value) {
172
+ var _a;
173
+ const resolved = this.index;
174
+ return ((_a = resolved.equalityLookup) == null ? void 0 : _a.call(resolved, value)) ?? /* @__PURE__ */ new Set();
175
+ }
176
+ rangeQuery(options) {
177
+ var _a;
178
+ const resolved = this.index;
179
+ return ((_a = resolved.rangeQuery) == null ? void 0 : _a.call(resolved, options)) ?? /* @__PURE__ */ new Set();
180
+ }
181
+ inArrayLookup(values) {
182
+ var _a;
183
+ const resolved = this.index;
184
+ return ((_a = resolved.inArrayLookup) == null ? void 0 : _a.call(resolved, values)) ?? /* @__PURE__ */ new Set();
185
+ }
186
+ // Internal method for the collection to get the lazy wrapper
187
+ _getLazyWrapper() {
188
+ return this.lazyIndex;
189
+ }
190
+ }
191
+ exports.IndexProxy = IndexProxy;
192
+ exports.LazyIndexWrapper = LazyIndexWrapper;
193
+ //# sourceMappingURL=lazy-index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lazy-index.cjs","sources":["../../../src/indexes/lazy-index.ts"],"sourcesContent":["import type {\n BaseIndex,\n IndexConstructor,\n IndexResolver,\n} from \"./base-index.js\"\nimport type { BasicExpression } from \"../query/ir.js\"\n\n/**\n * Utility to determine if a resolver is a constructor or async loader\n */\nfunction isConstructor<TKey extends string | number>(\n resolver: IndexResolver<TKey>\n): resolver is IndexConstructor<TKey> {\n // Check if it's a function with a prototype (constructor)\n return (\n typeof resolver === `function` &&\n resolver.prototype !== undefined &&\n resolver.prototype.constructor === resolver\n )\n}\n\n/**\n * Resolve index constructor from resolver\n */\nasync function resolveIndexConstructor<TKey extends string | number>(\n resolver: IndexResolver<TKey>\n): Promise<IndexConstructor<TKey>> {\n if (isConstructor(resolver)) {\n return resolver\n } else {\n // It's an async loader function\n return await resolver()\n }\n}\n\n/**\n * Wrapper that defers index creation until first sync\n */\nexport class LazyIndexWrapper<TKey extends string | number = string | number> {\n private indexPromise: Promise<BaseIndex<TKey>> | null = null\n private resolvedIndex: BaseIndex<TKey> | null = null\n\n constructor(\n private id: number,\n private expression: BasicExpression,\n private name: string | undefined,\n private resolver: IndexResolver<TKey>,\n private options: any,\n private collectionEntries?: Iterable<[TKey, any]>\n ) {\n // For synchronous constructors, resolve immediately\n if (isConstructor(this.resolver)) {\n this.resolvedIndex = new this.resolver(\n this.id,\n this.expression,\n this.name,\n this.options\n )\n // Build with initial data if provided\n if (this.collectionEntries) {\n this.resolvedIndex.build(this.collectionEntries)\n }\n }\n }\n\n /**\n * Resolve the actual index\n */\n async resolve(): Promise<BaseIndex<TKey>> {\n if (this.resolvedIndex) {\n return this.resolvedIndex\n }\n\n if (!this.indexPromise) {\n this.indexPromise = this.createIndex()\n }\n\n this.resolvedIndex = await this.indexPromise\n return this.resolvedIndex\n }\n\n /**\n * Check if already resolved\n */\n isResolved(): boolean {\n return this.resolvedIndex !== null\n }\n\n /**\n * Get resolved index (throws if not ready)\n */\n getResolved(): BaseIndex<TKey> {\n if (!this.resolvedIndex) {\n throw new Error(\n `Index ${this.id} has not been resolved yet. Ensure collection is synced.`\n )\n }\n return this.resolvedIndex\n }\n\n /**\n * Get the index ID\n */\n getId(): number {\n return this.id\n }\n\n /**\n * Get the index name\n */\n getName(): string | undefined {\n return this.name\n }\n\n /**\n * Get the index expression\n */\n getExpression(): BasicExpression {\n return this.expression\n }\n\n private async createIndex(): Promise<BaseIndex<TKey>> {\n const IndexClass = await resolveIndexConstructor(this.resolver)\n return new IndexClass(this.id, this.expression, this.name, this.options)\n }\n}\n\n/**\n * Proxy that provides synchronous interface while index loads asynchronously\n */\nexport class IndexProxy<TKey extends string | number = string | number> {\n constructor(\n private indexId: number,\n private lazyIndex: LazyIndexWrapper<TKey>\n ) {}\n\n /**\n * Get the resolved index (throws if not ready)\n */\n get index(): BaseIndex<TKey> {\n return this.lazyIndex.getResolved()\n }\n\n /**\n * Check if index is ready\n */\n get isReady(): boolean {\n return this.lazyIndex.isResolved()\n }\n\n /**\n * Wait for index to be ready\n */\n async whenReady(): Promise<BaseIndex<TKey>> {\n return await this.lazyIndex.resolve()\n }\n\n /**\n * Get the index ID\n */\n get id(): number {\n return this.indexId\n }\n\n /**\n * Get the index name (throws if not ready)\n */\n get name(): string | undefined {\n if (this.isReady) {\n return this.index.name\n }\n return this.lazyIndex.getName()\n }\n\n /**\n * Get the index expression (available immediately)\n */\n get expression(): BasicExpression {\n return this.lazyIndex.getExpression()\n }\n\n /**\n * Check if index supports an operation (throws if not ready)\n */\n supports(operation: any): boolean {\n return this.index.supports(operation)\n }\n\n /**\n * Get index statistics (throws if not ready)\n */\n getStats() {\n return this.index.getStats()\n }\n\n /**\n * Check if index matches a field path (available immediately)\n */\n matchesField(fieldPath: Array<string>): boolean {\n const expr = this.expression\n return (\n expr.type === `ref` &&\n expr.path.length === fieldPath.length &&\n expr.path.every((part, i) => part === fieldPath[i])\n )\n }\n\n /**\n * Get the key count (throws if not ready)\n */\n get keyCount(): number {\n return this.index.keyCount\n }\n\n // Test compatibility properties - delegate to resolved index\n get indexedKeysSet(): Set<TKey> {\n const resolved = this.index as any\n return resolved.indexedKeysSet\n }\n\n get orderedEntriesArray(): Array<[any, Set<TKey>]> {\n const resolved = this.index as any\n return resolved.orderedEntriesArray\n }\n\n get valueMapData(): Map<any, Set<TKey>> {\n const resolved = this.index as any\n return resolved.valueMapData\n }\n\n // BTreeIndex compatibility methods\n equalityLookup(value: any): Set<TKey> {\n const resolved = this.index as any\n return resolved.equalityLookup?.(value) ?? new Set()\n }\n\n rangeQuery(options: any): Set<TKey> {\n const resolved = this.index as any\n return resolved.rangeQuery?.(options) ?? new Set()\n }\n\n inArrayLookup(values: Array<any>): Set<TKey> {\n const resolved = this.index as any\n return resolved.inArrayLookup?.(values) ?? new Set()\n }\n\n // Internal method for the collection to get the lazy wrapper\n _getLazyWrapper(): LazyIndexWrapper<TKey> {\n return this.lazyIndex\n }\n}\n"],"names":[],"mappings":";;AAUA,SAAS,cACP,UACoC;AAEpC,SACE,OAAO,aAAa,cACpB,SAAS,cAAc,UACvB,SAAS,UAAU,gBAAgB;AAEvC;AAKA,eAAe,wBACb,UACiC;AACjC,MAAI,cAAc,QAAQ,GAAG;AAC3B,WAAO;AAAA,EACT,OAAO;AAEL,WAAO,MAAM,SAAA;AAAA,EACf;AACF;AAKO,MAAM,iBAAiE;AAAA,EAI5E,YACU,IACA,YACA,MACA,UACA,SACA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AACjD,UAAM,WAAW,KAAK;AACtB,WAAO,SAAS;AAAA,EAClB;AAAA,EAEA,IAAI,eAAoC;AACtC,UAAM,WAAW,KAAK;AACtB,WAAO,SAAS;AAAA,EAClB;AAAA;AAAA,EAGA,eAAe,OAAuB;;AACpC,UAAM,WAAW,KAAK;AACtB,aAAO,cAAS,mBAAT,kCAA0B,+BAAc,IAAA;AAAA,EACjD;AAAA,EAEA,WAAW,SAAyB;;AAClC,UAAM,WAAW,KAAK;AACtB,aAAO,cAAS,eAAT,kCAAsB,iCAAgB,IAAA;AAAA,EAC/C;AAAA,EAEA,cAAc,QAA+B;;AAC3C,UAAM,WAAW,KAAK;AACtB,aAAO,cAAS,kBAAT,kCAAyB,gCAAe,IAAA;AAAA,EACjD;AAAA;AAAA,EAGA,kBAA0C;AACxC,WAAO,KAAK;AAAA,EACd;AACF;;;"}
@@ -0,0 +1,96 @@
1
+ import { BaseIndex, IndexResolver } from './base-index.js';
2
+ import { BasicExpression } from '../query/ir.js';
3
+ /**
4
+ * Wrapper that defers index creation until first sync
5
+ */
6
+ export declare class LazyIndexWrapper<TKey extends string | number = string | number> {
7
+ private id;
8
+ private expression;
9
+ private name;
10
+ private resolver;
11
+ private options;
12
+ private collectionEntries?;
13
+ private indexPromise;
14
+ private resolvedIndex;
15
+ constructor(id: number, expression: BasicExpression, name: string | undefined, resolver: IndexResolver<TKey>, options: any, collectionEntries?: Iterable<[TKey, any]> | undefined);
16
+ /**
17
+ * Resolve the actual index
18
+ */
19
+ resolve(): Promise<BaseIndex<TKey>>;
20
+ /**
21
+ * Check if already resolved
22
+ */
23
+ isResolved(): boolean;
24
+ /**
25
+ * Get resolved index (throws if not ready)
26
+ */
27
+ getResolved(): BaseIndex<TKey>;
28
+ /**
29
+ * Get the index ID
30
+ */
31
+ getId(): number;
32
+ /**
33
+ * Get the index name
34
+ */
35
+ getName(): string | undefined;
36
+ /**
37
+ * Get the index expression
38
+ */
39
+ getExpression(): BasicExpression;
40
+ private createIndex;
41
+ }
42
+ /**
43
+ * Proxy that provides synchronous interface while index loads asynchronously
44
+ */
45
+ export declare class IndexProxy<TKey extends string | number = string | number> {
46
+ private indexId;
47
+ private lazyIndex;
48
+ constructor(indexId: number, lazyIndex: LazyIndexWrapper<TKey>);
49
+ /**
50
+ * Get the resolved index (throws if not ready)
51
+ */
52
+ get index(): BaseIndex<TKey>;
53
+ /**
54
+ * Check if index is ready
55
+ */
56
+ get isReady(): boolean;
57
+ /**
58
+ * Wait for index to be ready
59
+ */
60
+ whenReady(): Promise<BaseIndex<TKey>>;
61
+ /**
62
+ * Get the index ID
63
+ */
64
+ get id(): number;
65
+ /**
66
+ * Get the index name (throws if not ready)
67
+ */
68
+ get name(): string | undefined;
69
+ /**
70
+ * Get the index expression (available immediately)
71
+ */
72
+ get expression(): BasicExpression;
73
+ /**
74
+ * Check if index supports an operation (throws if not ready)
75
+ */
76
+ supports(operation: any): boolean;
77
+ /**
78
+ * Get index statistics (throws if not ready)
79
+ */
80
+ getStats(): import('./base-index.js').IndexStats;
81
+ /**
82
+ * Check if index matches a field path (available immediately)
83
+ */
84
+ matchesField(fieldPath: Array<string>): boolean;
85
+ /**
86
+ * Get the key count (throws if not ready)
87
+ */
88
+ get keyCount(): number;
89
+ get indexedKeysSet(): Set<TKey>;
90
+ get orderedEntriesArray(): Array<[any, Set<TKey>]>;
91
+ get valueMapData(): Map<any, Set<TKey>>;
92
+ equalityLookup(value: any): Set<TKey>;
93
+ rangeQuery(options: any): Set<TKey>;
94
+ inArrayLookup(values: Array<any>): Set<TKey>;
95
+ _getLazyWrapper(): LazyIndexWrapper<TKey>;
96
+ }
@@ -1,11 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const errors = require("./errors.cjs");
3
4
  function validateJsonSerializable(value, operation) {
4
5
  try {
5
6
  JSON.stringify(value);
6
7
  } catch (error) {
7
- throw new Error(
8
- `Cannot ${operation} item because it cannot be JSON serialized: ${error instanceof Error ? error.message : String(error)}`
8
+ throw new errors.SerializationError(
9
+ operation,
10
+ error instanceof Error ? error.message : String(error)
9
11
  );
10
12
  }
11
13
  }
@@ -14,19 +16,15 @@ function generateUuid() {
14
16
  }
15
17
  function localStorageCollectionOptions(config) {
16
18
  if (!config.storageKey) {
17
- throw new Error(`[LocalStorageCollection] storageKey must be provided.`);
19
+ throw new errors.StorageKeyRequiredError();
18
20
  }
19
21
  const storage = config.storage || (typeof window !== `undefined` ? window.localStorage : null);
20
22
  if (!storage) {
21
- throw new Error(
22
- `[LocalStorageCollection] No storage available. Please provide a storage option or ensure window.localStorage is available.`
23
- );
23
+ throw new errors.NoStorageAvailableError();
24
24
  }
25
25
  const storageEventApi = config.storageEventApi || (typeof window !== `undefined` ? window : null);
26
26
  if (!storageEventApi) {
27
- throw new Error(
28
- `[LocalStorageCollection] No storage event API available. Please provide a storageEventApi option or ensure window is available.`
29
- );
27
+ throw new errors.NoStorageEventApiError();
30
28
  }
31
29
  const lastKnownData = /* @__PURE__ */ new Map();
32
30
  const sync = createLocalStorageSync(
@@ -167,15 +165,11 @@ function loadFromStorage(storageKey, storage) {
167
165
  const storedItem = value;
168
166
  dataMap.set(key, storedItem);
169
167
  } else {
170
- throw new Error(
171
- `[LocalStorageCollection] Invalid data format in storage key "${storageKey}" for key "${key}".`
172
- );
168
+ throw new errors.InvalidStorageDataFormatError(storageKey, key);
173
169
  }
174
170
  });
175
171
  } else {
176
- throw new Error(
177
- `[LocalStorageCollection] Invalid data format in storage key "${storageKey}". Expected object format.`
178
- );
172
+ throw new errors.InvalidStorageObjectFormatError(storageKey);
179
173
  }
180
174
  return dataMap;
181
175
  } catch (error) {