@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
package/src/collection.ts CHANGED
@@ -1,13 +1,51 @@
1
1
  import { withArrayChangeTracking, withChangeTracking } from "./proxy"
2
2
  import { SortedMap } from "./SortedMap"
3
+ import {
4
+ createSingleRowRefProxy,
5
+ toExpression,
6
+ } from "./query/builder/ref-proxy"
7
+ import { BTreeIndex } from "./indexes/btree-index.js"
8
+ import { IndexProxy, LazyIndexWrapper } from "./indexes/lazy-index.js"
9
+ import { ensureIndexForExpression } from "./indexes/auto-index.js"
3
10
  import { createTransaction, getActiveTransaction } from "./transactions"
11
+ import {
12
+ CollectionInErrorStateError,
13
+ CollectionIsInErrorStateError,
14
+ CollectionRequiresConfigError,
15
+ CollectionRequiresSyncConfigError,
16
+ DeleteKeyNotFoundError,
17
+ DuplicateKeyError,
18
+ DuplicateKeySyncError,
19
+ InvalidCollectionStatusTransitionError,
20
+ InvalidSchemaError,
21
+ KeyUpdateNotAllowedError,
22
+ MissingDeleteHandlerError,
23
+ MissingInsertHandlerError,
24
+ MissingUpdateArgumentError,
25
+ MissingUpdateHandlerError,
26
+ NegativeActiveSubscribersError,
27
+ NoKeysPassedToDeleteError,
28
+ NoKeysPassedToUpdateError,
29
+ NoPendingSyncTransactionCommitError,
30
+ NoPendingSyncTransactionWriteError,
31
+ SchemaMustBeSynchronousError,
32
+ SchemaValidationError,
33
+ SyncCleanupError,
34
+ SyncTransactionAlreadyCommittedError,
35
+ SyncTransactionAlreadyCommittedWriteError,
36
+ UndefinedKeyError,
37
+ UpdateKeyNotFoundError,
38
+ } from "./errors"
39
+ import { createFilteredCallback, currentStateAsChanges } from "./change-events"
4
40
  import type { Transaction } from "./transactions"
5
41
  import type { StandardSchemaV1 } from "@standard-schema/spec"
42
+ import type { SingleRowRefProxy } from "./query/builder/ref-proxy"
6
43
  import type {
7
44
  ChangeListener,
8
45
  ChangeMessage,
9
46
  CollectionConfig,
10
47
  CollectionStatus,
48
+ CurrentStateAsChangesOptions,
11
49
  Fn,
12
50
  InsertConfig,
13
51
  OperationConfig,
@@ -16,10 +54,13 @@ import type {
16
54
  ResolveInsertInput,
17
55
  ResolveType,
18
56
  StandardSchema,
57
+ SubscribeChangesOptions,
19
58
  Transaction as TransactionType,
20
59
  TransactionWithMutations,
21
60
  UtilsRecord,
22
61
  } from "./types"
62
+ import type { IndexOptions } from "./indexes/index-options.js"
63
+ import type { BaseIndex, IndexResolver } from "./indexes/base-index.js"
23
64
 
24
65
  // Store collections in memory
25
66
  export const collectionsStore = new Map<string, CollectionImpl<any, any, any>>()
@@ -163,35 +204,6 @@ export function createCollection<
163
204
  >
164
205
  }
165
206
 
166
- /**
167
- * Custom error class for schema validation errors
168
- */
169
- export class SchemaValidationError extends Error {
170
- type: `insert` | `update`
171
- issues: ReadonlyArray<{
172
- message: string
173
- path?: ReadonlyArray<string | number | symbol>
174
- }>
175
-
176
- constructor(
177
- type: `insert` | `update`,
178
- issues: ReadonlyArray<{
179
- message: string
180
- path?: ReadonlyArray<string | number | symbol>
181
- }>,
182
- message?: string
183
- ) {
184
- const defaultMessage = `${type === `insert` ? `Insert` : `Update`} validation failed: ${issues
185
- .map((issue) => `\n- ${issue.message} - path: ${issue.path}`)
186
- .join(``)}`
187
-
188
- super(message || defaultMessage)
189
- this.name = `SchemaValidationError`
190
- this.type = type
191
- this.issues = issues
192
- }
193
- }
194
-
195
207
  export class CollectionImpl<
196
208
  T extends object = Record<string, unknown>,
197
209
  TKey extends string | number = string | number,
@@ -214,6 +226,12 @@ export class CollectionImpl<
214
226
  // Cached size for performance
215
227
  private _size = 0
216
228
 
229
+ // Index storage
230
+ private lazyIndexes = new Map<number, LazyIndexWrapper<TKey>>()
231
+ private resolvedIndexes = new Map<number, BaseIndex<TKey>>()
232
+ private isIndexesResolved = false
233
+ private indexCounter = 0
234
+
217
235
  // Event system
218
236
  private changeListeners = new Set<ChangeListener<T, TKey>>()
219
237
  private changeKeyListeners = new Map<TKey, Set<ChangeListener<T, TKey>>>()
@@ -323,15 +341,11 @@ export class CollectionImpl<
323
341
  private validateCollectionUsable(operation: string): void {
324
342
  switch (this._status) {
325
343
  case `error`:
326
- throw new Error(
327
- `Cannot perform ${operation} on collection "${this.id}" - collection is in error state. ` +
328
- `Try calling cleanup() and restarting the collection.`
329
- )
344
+ throw new CollectionInErrorStateError(operation, this.id)
330
345
  case `cleaned-up`:
331
- throw new Error(
332
- `Cannot perform ${operation} on collection "${this.id}" - collection has been cleaned up. ` +
333
- `The collection will automatically restart on next access.`
334
- )
346
+ // Automatically restart the collection when operations are called on cleaned-up collections
347
+ this.startSync()
348
+ break
335
349
  }
336
350
  }
337
351
 
@@ -360,9 +374,7 @@ export class CollectionImpl<
360
374
  }
361
375
 
362
376
  if (!validTransitions[from].includes(to)) {
363
- throw new Error(
364
- `Invalid collection status transition from "${from}" to "${to}" for collection "${this.id}"`
365
- )
377
+ throw new InvalidCollectionStatusTransitionError(from, to, this.id)
366
378
  }
367
379
  }
368
380
 
@@ -373,6 +385,14 @@ export class CollectionImpl<
373
385
  private setStatus(newStatus: CollectionStatus): void {
374
386
  this.validateStatusTransition(this._status, newStatus)
375
387
  this._status = newStatus
388
+
389
+ // Resolve indexes when collection becomes ready
390
+ if (newStatus === `ready` && !this.isIndexesResolved) {
391
+ // Resolve indexes asynchronously without blocking
392
+ this.resolveAllIndexes().catch((error) => {
393
+ console.warn(`Failed to resolve indexes:`, error)
394
+ })
395
+ }
376
396
  }
377
397
 
378
398
  /**
@@ -384,7 +404,7 @@ export class CollectionImpl<
384
404
  constructor(config: CollectionConfig<T, TKey, TSchema, TInsertInput>) {
385
405
  // eslint-disable-next-line
386
406
  if (!config) {
387
- throw new Error(`Collection requires a config`)
407
+ throw new CollectionRequiresConfigError()
388
408
  }
389
409
  if (config.id) {
390
410
  this.id = config.id
@@ -394,14 +414,18 @@ export class CollectionImpl<
394
414
 
395
415
  // eslint-disable-next-line
396
416
  if (!config.sync) {
397
- throw new Error(`Collection requires a sync config`)
417
+ throw new CollectionRequiresSyncConfigError()
398
418
  }
399
419
 
400
420
  this.transactions = new SortedMap<string, Transaction<any>>((a, b) =>
401
421
  a.compareCreatedAt(b)
402
422
  )
403
423
 
404
- this.config = config
424
+ // Set default values for optional config properties
425
+ this.config = {
426
+ ...config,
427
+ autoIndex: config.autoIndex ?? `eager`,
428
+ }
405
429
 
406
430
  // Store in global collections store
407
431
  collectionsStore.set(this.id, this)
@@ -453,12 +477,10 @@ export class CollectionImpl<
453
477
  this.pendingSyncedTransactions.length - 1
454
478
  ]
455
479
  if (!pendingTransaction) {
456
- throw new Error(`No pending sync transaction to write to`)
480
+ throw new NoPendingSyncTransactionWriteError()
457
481
  }
458
482
  if (pendingTransaction.committed) {
459
- throw new Error(
460
- `The pending sync transaction is already committed, you can't still write to it.`
461
- )
483
+ throw new SyncTransactionAlreadyCommittedWriteError()
462
484
  }
463
485
  const key = this.getKeyFromItem(messageWithoutKey.value)
464
486
 
@@ -470,9 +492,7 @@ export class CollectionImpl<
470
492
  (op) => op.key === key && op.type === `delete`
471
493
  )
472
494
  ) {
473
- throw new Error(
474
- `Cannot insert document with key "${key}" from sync because it already exists in the collection "${this.id}"`
475
- )
495
+ throw new DuplicateKeySyncError(key, this.id)
476
496
  }
477
497
  }
478
498
 
@@ -488,12 +508,10 @@ export class CollectionImpl<
488
508
  this.pendingSyncedTransactions.length - 1
489
509
  ]
490
510
  if (!pendingTransaction) {
491
- throw new Error(`No pending sync transaction to commit`)
511
+ throw new NoPendingSyncTransactionCommitError()
492
512
  }
493
513
  if (pendingTransaction.committed) {
494
- throw new Error(
495
- `The pending sync transaction is already committed, you can't commit it again.`
496
- )
514
+ throw new SyncTransactionAlreadyCommittedError()
497
515
  }
498
516
 
499
517
  pendingTransaction.committed = true
@@ -535,7 +553,7 @@ export class CollectionImpl<
535
553
  }
536
554
 
537
555
  if (this._status === `error`) {
538
- reject(new Error(`Collection is in error state`))
556
+ reject(new CollectionIsInErrorStateError())
539
557
  return
540
558
  }
541
559
 
@@ -580,16 +598,12 @@ export class CollectionImpl<
580
598
  queueMicrotask(() => {
581
599
  if (error instanceof Error) {
582
600
  // Preserve the original error and stack trace
583
- const wrappedError = new Error(
584
- `Collection "${this.id}" sync cleanup function threw an error: ${error.message}`
585
- )
601
+ const wrappedError = new SyncCleanupError(this.id, error)
586
602
  wrappedError.cause = error
587
603
  wrappedError.stack = error.stack
588
604
  throw wrappedError
589
605
  } else {
590
- throw new Error(
591
- `Collection "${this.id}" sync cleanup function threw an error: ${String(error)}`
592
- )
606
+ throw new SyncCleanupError(this.id, error as Error | string)
593
607
  }
594
608
  })
595
609
  }
@@ -666,9 +680,7 @@ export class CollectionImpl<
666
680
  this.activeSubscribersCount = 0
667
681
  this.startGCTimer()
668
682
  } else if (this.activeSubscribersCount < 0) {
669
- throw new Error(
670
- `Active subscribers count is negative - this should never happen`
671
- )
683
+ throw new NegativeActiveSubscribersError()
672
684
  }
673
685
  }
674
686
 
@@ -772,8 +784,16 @@ export class CollectionImpl<
772
784
  return true
773
785
  })
774
786
 
787
+ // Update indexes for the filtered events
788
+ if (filteredEvents.length > 0) {
789
+ this.updateIndexes(filteredEvents)
790
+ }
775
791
  this.emitEvents(filteredEvents)
776
792
  } else {
793
+ // Update indexes for all events
794
+ if (filteredEventsBySyncStatus.length > 0) {
795
+ this.updateIndexes(filteredEventsBySyncStatus)
796
+ }
777
797
  // Emit all events if no pending sync transactions
778
798
  this.emitEvents(filteredEventsBySyncStatus)
779
799
  }
@@ -1217,6 +1237,11 @@ export class CollectionImpl<
1217
1237
  // Update cached size after synced data changes
1218
1238
  this._size = this.calculateSize()
1219
1239
 
1240
+ // Update indexes for all events before emitting
1241
+ if (events.length > 0) {
1242
+ this.updateIndexes(events)
1243
+ }
1244
+
1220
1245
  // End batching and emit all events (combines any batched events with sync events)
1221
1246
  this.emitEvents(events, true)
1222
1247
 
@@ -1246,7 +1271,7 @@ export class CollectionImpl<
1246
1271
  return schema as StandardSchema<T>
1247
1272
  }
1248
1273
 
1249
- throw new Error(`Schema must implement the standard-schema interface`)
1274
+ throw new InvalidSchemaError()
1250
1275
  }
1251
1276
 
1252
1277
  public getKeyFromItem(item: T): TKey {
@@ -1255,14 +1280,167 @@ export class CollectionImpl<
1255
1280
 
1256
1281
  public generateGlobalKey(key: any, item: any): string {
1257
1282
  if (typeof key === `undefined`) {
1258
- throw new Error(
1259
- `An object was created without a defined key: ${JSON.stringify(item)}`
1260
- )
1283
+ throw new UndefinedKeyError(item)
1261
1284
  }
1262
1285
 
1263
1286
  return `KEY::${this.id}/${key}`
1264
1287
  }
1265
1288
 
1289
+ /**
1290
+ * Creates an index on a collection for faster queries.
1291
+ * Indexes significantly improve query performance by allowing binary search
1292
+ * and range queries instead of full scans.
1293
+ *
1294
+ * @template TResolver - The type of the index resolver (constructor or async loader)
1295
+ * @param indexCallback - Function that extracts the indexed value from each item
1296
+ * @param config - Configuration including index type and type-specific options
1297
+ * @returns An index proxy that provides access to the index when ready
1298
+ *
1299
+ * @example
1300
+ * // Create a default B+ tree index
1301
+ * const ageIndex = collection.createIndex((row) => row.age)
1302
+ *
1303
+ * // Create a ordered index with custom options
1304
+ * const ageIndex = collection.createIndex((row) => row.age, {
1305
+ * indexType: BTreeIndex,
1306
+ * options: { compareFn: customComparator },
1307
+ * name: 'age_btree'
1308
+ * })
1309
+ *
1310
+ * // Create an async-loaded index
1311
+ * const textIndex = collection.createIndex((row) => row.content, {
1312
+ * indexType: async () => {
1313
+ * const { FullTextIndex } = await import('./indexes/fulltext.js')
1314
+ * return FullTextIndex
1315
+ * },
1316
+ * options: { language: 'en' }
1317
+ * })
1318
+ */
1319
+ public createIndex<TResolver extends IndexResolver<TKey> = typeof BTreeIndex>(
1320
+ indexCallback: (row: SingleRowRefProxy<T>) => any,
1321
+ config: IndexOptions<TResolver> = {}
1322
+ ): IndexProxy<TKey> {
1323
+ this.validateCollectionUsable(`createIndex`)
1324
+
1325
+ const indexId = ++this.indexCounter
1326
+ const singleRowRefProxy = createSingleRowRefProxy<T>()
1327
+ const indexExpression = indexCallback(singleRowRefProxy)
1328
+ const expression = toExpression(indexExpression)
1329
+
1330
+ // Default to BTreeIndex if no type specified
1331
+ const resolver = config.indexType ?? (BTreeIndex as unknown as TResolver)
1332
+
1333
+ // Create lazy wrapper
1334
+ const lazyIndex = new LazyIndexWrapper<TKey>(
1335
+ indexId,
1336
+ expression,
1337
+ config.name,
1338
+ resolver,
1339
+ config.options,
1340
+ this.entries()
1341
+ )
1342
+
1343
+ this.lazyIndexes.set(indexId, lazyIndex)
1344
+
1345
+ // For BTreeIndex, resolve immediately and synchronously
1346
+ if ((resolver as unknown) === BTreeIndex) {
1347
+ try {
1348
+ const resolvedIndex = lazyIndex.getResolved()
1349
+ this.resolvedIndexes.set(indexId, resolvedIndex)
1350
+ } catch (error) {
1351
+ console.warn(`Failed to resolve BTreeIndex:`, error)
1352
+ }
1353
+ } else if (typeof resolver === `function` && resolver.prototype) {
1354
+ // Other synchronous constructors - resolve immediately
1355
+ try {
1356
+ const resolvedIndex = lazyIndex.getResolved()
1357
+ this.resolvedIndexes.set(indexId, resolvedIndex)
1358
+ } catch {
1359
+ // Fallback to async resolution
1360
+ this.resolveSingleIndex(indexId, lazyIndex).catch((error) => {
1361
+ console.warn(`Failed to resolve single index:`, error)
1362
+ })
1363
+ }
1364
+ } else if (this.isIndexesResolved) {
1365
+ // Async loader but indexes are already resolved - resolve this one
1366
+ this.resolveSingleIndex(indexId, lazyIndex).catch((error) => {
1367
+ console.warn(`Failed to resolve single index:`, error)
1368
+ })
1369
+ }
1370
+
1371
+ return new IndexProxy(indexId, lazyIndex)
1372
+ }
1373
+
1374
+ /**
1375
+ * Resolve all lazy indexes (called when collection first syncs)
1376
+ * @private
1377
+ */
1378
+ private async resolveAllIndexes(): Promise<void> {
1379
+ if (this.isIndexesResolved) return
1380
+
1381
+ const resolutionPromises = Array.from(this.lazyIndexes.entries()).map(
1382
+ async ([indexId, lazyIndex]) => {
1383
+ const resolvedIndex = await lazyIndex.resolve()
1384
+
1385
+ // Build index with current data
1386
+ resolvedIndex.build(this.entries())
1387
+
1388
+ this.resolvedIndexes.set(indexId, resolvedIndex)
1389
+ return { indexId, resolvedIndex }
1390
+ }
1391
+ )
1392
+
1393
+ await Promise.all(resolutionPromises)
1394
+ this.isIndexesResolved = true
1395
+ }
1396
+
1397
+ /**
1398
+ * Resolve a single index immediately
1399
+ * @private
1400
+ */
1401
+ private async resolveSingleIndex(
1402
+ indexId: number,
1403
+ lazyIndex: LazyIndexWrapper<TKey>
1404
+ ): Promise<BaseIndex<TKey>> {
1405
+ const resolvedIndex = await lazyIndex.resolve()
1406
+ resolvedIndex.build(this.entries())
1407
+ this.resolvedIndexes.set(indexId, resolvedIndex)
1408
+ return resolvedIndex
1409
+ }
1410
+
1411
+ /**
1412
+ * Get resolved indexes for query optimization
1413
+ */
1414
+ get indexes(): Map<number, BaseIndex<TKey>> {
1415
+ return this.resolvedIndexes
1416
+ }
1417
+
1418
+ /**
1419
+ * Updates all indexes when the collection changes
1420
+ * @private
1421
+ */
1422
+ private updateIndexes(changes: Array<ChangeMessage<T, TKey>>): void {
1423
+ for (const index of this.resolvedIndexes.values()) {
1424
+ for (const change of changes) {
1425
+ switch (change.type) {
1426
+ case `insert`:
1427
+ index.add(change.key, change.value)
1428
+ break
1429
+ case `update`:
1430
+ if (change.previousValue) {
1431
+ index.update(change.key, change.previousValue, change.value)
1432
+ } else {
1433
+ index.add(change.key, change.value)
1434
+ }
1435
+ break
1436
+ case `delete`:
1437
+ index.remove(change.key, change.value)
1438
+ break
1439
+ }
1440
+ }
1441
+ }
1442
+ }
1443
+
1266
1444
  private deepEqual(a: any, b: any): boolean {
1267
1445
  if (a === b) return true
1268
1446
  if (a == null || b == null) return false
@@ -1314,7 +1492,7 @@ export class CollectionImpl<
1314
1492
 
1315
1493
  // Ensure validation is synchronous
1316
1494
  if (result instanceof Promise) {
1317
- throw new TypeError(`Schema validation must be synchronous`)
1495
+ throw new SchemaMustBeSynchronousError()
1318
1496
  }
1319
1497
 
1320
1498
  // If validation fails, throw a SchemaValidationError with the issues
@@ -1337,7 +1515,7 @@ export class CollectionImpl<
1337
1515
 
1338
1516
  // Ensure validation is synchronous
1339
1517
  if (result instanceof Promise) {
1340
- throw new TypeError(`Schema validation must be synchronous`)
1518
+ throw new SchemaMustBeSynchronousError()
1341
1519
  }
1342
1520
 
1343
1521
  // If validation fails, throw a SchemaValidationError with the issues
@@ -1397,9 +1575,7 @@ export class CollectionImpl<
1397
1575
 
1398
1576
  // If no ambient transaction exists, check for an onInsert handler early
1399
1577
  if (!ambientTransaction && !this.config.onInsert) {
1400
- throw new Error(
1401
- `Collection.insert called directly (not within an explicit transaction) but no 'onInsert' handler is configured.`
1402
- )
1578
+ throw new MissingInsertHandlerError()
1403
1579
  }
1404
1580
 
1405
1581
  const items = Array.isArray(data) ? data : [data]
@@ -1413,7 +1589,7 @@ export class CollectionImpl<
1413
1589
  // Check if an item with this ID already exists in the collection
1414
1590
  const key = this.getKeyFromItem(validatedData)
1415
1591
  if (this.has(key)) {
1416
- throw `Cannot insert document with ID "${key}" because it already exists in the collection`
1592
+ throw new DuplicateKeyError(key)
1417
1593
  }
1418
1594
  const globalKey = this.generateGlobalKey(key, item)
1419
1595
 
@@ -1552,7 +1728,7 @@ export class CollectionImpl<
1552
1728
  maybeCallback?: (draft: TItem | Array<TItem>) => void
1553
1729
  ) {
1554
1730
  if (typeof keys === `undefined`) {
1555
- throw new Error(`The first argument to update is missing`)
1731
+ throw new MissingUpdateArgumentError()
1556
1732
  }
1557
1733
 
1558
1734
  this.validateCollectionUsable(`update`)
@@ -1561,16 +1737,14 @@ export class CollectionImpl<
1561
1737
 
1562
1738
  // If no ambient transaction exists, check for an onUpdate handler early
1563
1739
  if (!ambientTransaction && !this.config.onUpdate) {
1564
- throw new Error(
1565
- `Collection.update called directly (not within an explicit transaction) but no 'onUpdate' handler is configured.`
1566
- )
1740
+ throw new MissingUpdateHandlerError()
1567
1741
  }
1568
1742
 
1569
1743
  const isArray = Array.isArray(keys)
1570
1744
  const keysArray = isArray ? keys : [keys]
1571
1745
 
1572
1746
  if (isArray && keysArray.length === 0) {
1573
- throw new Error(`No keys were passed to update`)
1747
+ throw new NoKeysPassedToUpdateError()
1574
1748
  }
1575
1749
 
1576
1750
  const callback =
@@ -1582,9 +1756,7 @@ export class CollectionImpl<
1582
1756
  const currentObjects = keysArray.map((key) => {
1583
1757
  const item = this.get(key)
1584
1758
  if (!item) {
1585
- throw new Error(
1586
- `The key "${key}" was passed to update but an object for this key was not found in the collection`
1587
- )
1759
+ throw new UpdateKeyNotFoundError(key)
1588
1760
  }
1589
1761
 
1590
1762
  return item
@@ -1635,9 +1807,7 @@ export class CollectionImpl<
1635
1807
  const modifiedItemId = this.getKeyFromItem(modifiedItem)
1636
1808
 
1637
1809
  if (originalItemId !== modifiedItemId) {
1638
- throw new Error(
1639
- `Updating the key of an item is not allowed. Original key: "${originalItemId}", Attempted new key: "${modifiedItemId}". Please delete the old item and create a new one if a key change is necessary.`
1640
- )
1810
+ throw new KeyUpdateNotAllowedError(originalItemId, modifiedItemId)
1641
1811
  }
1642
1812
 
1643
1813
  const globalKey = this.generateGlobalKey(modifiedItemId, modifiedItem)
@@ -1751,13 +1921,11 @@ export class CollectionImpl<
1751
1921
 
1752
1922
  // If no ambient transaction exists, check for an onDelete handler early
1753
1923
  if (!ambientTransaction && !this.config.onDelete) {
1754
- throw new Error(
1755
- `Collection.delete called directly (not within an explicit transaction) but no 'onDelete' handler is configured.`
1756
- )
1924
+ throw new MissingDeleteHandlerError()
1757
1925
  }
1758
1926
 
1759
1927
  if (Array.isArray(keys) && keys.length === 0) {
1760
- throw new Error(`No keys were passed to delete`)
1928
+ throw new NoKeysPassedToDeleteError()
1761
1929
  }
1762
1930
 
1763
1931
  const keysArray = Array.isArray(keys) ? keys : [keys]
@@ -1765,9 +1933,7 @@ export class CollectionImpl<
1765
1933
 
1766
1934
  for (const key of keysArray) {
1767
1935
  if (!this.has(key)) {
1768
- throw new Error(
1769
- `Collection.delete was called with key '${key}' but there is no item in the collection with this key`
1770
- )
1936
+ throw new DeleteKeyNotFoundError(key)
1771
1937
  }
1772
1938
  const globalKey = this.generateGlobalKey(key, this.get(key)!)
1773
1939
  const mutation: PendingMutation<T, `delete`, this> = {
@@ -1903,20 +2069,32 @@ export class CollectionImpl<
1903
2069
 
1904
2070
  /**
1905
2071
  * Returns the current state of the collection as an array of changes
2072
+ * @param options - Options including optional where filter
1906
2073
  * @returns An array of changes
2074
+ * @example
2075
+ * // Get all items as changes
2076
+ * const allChanges = collection.currentStateAsChanges()
2077
+ *
2078
+ * // Get only items matching a condition
2079
+ * const activeChanges = collection.currentStateAsChanges({
2080
+ * where: (row) => row.status === 'active'
2081
+ * })
2082
+ *
2083
+ * // Get only items using a pre-compiled expression
2084
+ * const activeChanges = collection.currentStateAsChanges({
2085
+ * whereExpression: eq(row.status, 'active')
2086
+ * })
1907
2087
  */
1908
- public currentStateAsChanges(): Array<ChangeMessage<T>> {
1909
- return Array.from(this.entries()).map(([key, value]) => ({
1910
- type: `insert`,
1911
- key,
1912
- value,
1913
- }))
2088
+ public currentStateAsChanges(
2089
+ options: CurrentStateAsChangesOptions<T> = {}
2090
+ ): Array<ChangeMessage<T>> {
2091
+ return currentStateAsChanges(this, options)
1914
2092
  }
1915
2093
 
1916
2094
  /**
1917
2095
  * Subscribe to changes in the collection
1918
2096
  * @param callback - Function called when items change
1919
- * @param options.includeInitialState - If true, immediately calls callback with current data
2097
+ * @param options - Subscription options including includeInitialState and where filter
1920
2098
  * @returns Unsubscribe function - Call this to stop listening for changes
1921
2099
  * @example
1922
2100
  * // Basic subscription
@@ -1933,24 +2111,57 @@ export class CollectionImpl<
1933
2111
  * const unsubscribe = collection.subscribeChanges((changes) => {
1934
2112
  * updateUI(changes)
1935
2113
  * }, { includeInitialState: true })
2114
+ *
2115
+ * @example
2116
+ * // Subscribe only to changes matching a condition
2117
+ * const unsubscribe = collection.subscribeChanges((changes) => {
2118
+ * updateUI(changes)
2119
+ * }, {
2120
+ * includeInitialState: true,
2121
+ * where: (row) => row.status === 'active'
2122
+ * })
2123
+ *
2124
+ * @example
2125
+ * // Subscribe using a pre-compiled expression
2126
+ * const unsubscribe = collection.subscribeChanges((changes) => {
2127
+ * updateUI(changes)
2128
+ * }, {
2129
+ * includeInitialState: true,
2130
+ * whereExpression: eq(row.status, 'active')
2131
+ * })
1936
2132
  */
1937
2133
  public subscribeChanges(
1938
2134
  callback: (changes: Array<ChangeMessage<T>>) => void,
1939
- { includeInitialState = false }: { includeInitialState?: boolean } = {}
2135
+ options: SubscribeChangesOptions<T> = {}
1940
2136
  ): () => void {
1941
2137
  // Start sync and track subscriber
1942
2138
  this.addSubscriber()
1943
2139
 
1944
- if (includeInitialState) {
1945
- // First send the current state as changes
1946
- callback(this.currentStateAsChanges())
2140
+ // Auto-index for where expressions if enabled
2141
+ if (options.whereExpression) {
2142
+ ensureIndexForExpression(options.whereExpression, this)
2143
+ }
2144
+
2145
+ // Create a filtered callback if where clause is provided
2146
+ const filteredCallback =
2147
+ options.where || options.whereExpression
2148
+ ? createFilteredCallback(callback, options)
2149
+ : callback
2150
+
2151
+ if (options.includeInitialState) {
2152
+ // First send the current state as changes (filtered if needed)
2153
+ const initialChanges = this.currentStateAsChanges({
2154
+ where: options.where,
2155
+ whereExpression: options.whereExpression,
2156
+ })
2157
+ filteredCallback(initialChanges)
1947
2158
  }
1948
2159
 
1949
2160
  // Add to batched listeners
1950
- this.changeListeners.add(callback)
2161
+ this.changeListeners.add(filteredCallback)
1951
2162
 
1952
2163
  return () => {
1953
- this.changeListeners.delete(callback)
2164
+ this.changeListeners.delete(filteredCallback)
1954
2165
  this.removeSubscriber()
1955
2166
  }
1956
2167
  }