@tanstack/db 0.5.11 → 0.5.13

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 (225) hide show
  1. package/dist/cjs/SortedMap.cjs +40 -26
  2. package/dist/cjs/SortedMap.cjs.map +1 -1
  3. package/dist/cjs/SortedMap.d.cts +10 -15
  4. package/dist/cjs/collection/change-events.cjs.map +1 -1
  5. package/dist/cjs/collection/changes.cjs +2 -0
  6. package/dist/cjs/collection/changes.cjs.map +1 -1
  7. package/dist/cjs/collection/events.cjs.map +1 -1
  8. package/dist/cjs/collection/events.d.cts +12 -4
  9. package/dist/cjs/collection/index.cjs +2 -1
  10. package/dist/cjs/collection/index.cjs.map +1 -1
  11. package/dist/cjs/collection/indexes.cjs.map +1 -1
  12. package/dist/cjs/collection/lifecycle.cjs.map +1 -1
  13. package/dist/cjs/collection/mutations.cjs +5 -2
  14. package/dist/cjs/collection/mutations.cjs.map +1 -1
  15. package/dist/cjs/collection/state.cjs +6 -5
  16. package/dist/cjs/collection/state.cjs.map +1 -1
  17. package/dist/cjs/collection/state.d.cts +4 -1
  18. package/dist/cjs/collection/subscription.cjs +91 -57
  19. package/dist/cjs/collection/subscription.cjs.map +1 -1
  20. package/dist/cjs/collection/subscription.d.cts +26 -4
  21. package/dist/cjs/collection/sync.cjs +11 -6
  22. package/dist/cjs/collection/sync.cjs.map +1 -1
  23. package/dist/cjs/errors.cjs +9 -0
  24. package/dist/cjs/errors.cjs.map +1 -1
  25. package/dist/cjs/errors.d.cts +3 -0
  26. package/dist/cjs/event-emitter.cjs.map +1 -1
  27. package/dist/cjs/index.cjs +2 -0
  28. package/dist/cjs/index.cjs.map +1 -1
  29. package/dist/cjs/index.d.cts +1 -1
  30. package/dist/cjs/indexes/auto-index.cjs.map +1 -1
  31. package/dist/cjs/indexes/base-index.cjs.map +1 -1
  32. package/dist/cjs/indexes/btree-index.cjs +8 -6
  33. package/dist/cjs/indexes/btree-index.cjs.map +1 -1
  34. package/dist/cjs/indexes/lazy-index.cjs.map +1 -1
  35. package/dist/cjs/indexes/reverse-index.cjs.map +1 -1
  36. package/dist/cjs/local-only.cjs.map +1 -1
  37. package/dist/cjs/local-storage.cjs.map +1 -1
  38. package/dist/cjs/optimistic-action.cjs.map +1 -1
  39. package/dist/cjs/paced-mutations.cjs.map +1 -1
  40. package/dist/cjs/proxy.cjs.map +1 -1
  41. package/dist/cjs/query/builder/functions.cjs.map +1 -1
  42. package/dist/cjs/query/builder/index.cjs.map +1 -1
  43. package/dist/cjs/query/builder/ref-proxy.cjs.map +1 -1
  44. package/dist/cjs/query/compiler/evaluators.cjs.map +1 -1
  45. package/dist/cjs/query/compiler/expressions.cjs.map +1 -1
  46. package/dist/cjs/query/compiler/group-by.cjs.map +1 -1
  47. package/dist/cjs/query/compiler/index.cjs.map +1 -1
  48. package/dist/cjs/query/compiler/joins.cjs.map +1 -1
  49. package/dist/cjs/query/compiler/order-by.cjs +91 -38
  50. package/dist/cjs/query/compiler/order-by.cjs.map +1 -1
  51. package/dist/cjs/query/compiler/order-by.d.cts +6 -2
  52. package/dist/cjs/query/compiler/select.cjs.map +1 -1
  53. package/dist/cjs/query/expression-helpers.cjs.map +1 -1
  54. package/dist/cjs/query/index.d.cts +1 -1
  55. package/dist/cjs/query/ir.cjs.map +1 -1
  56. package/dist/cjs/query/live/collection-config-builder.cjs.map +1 -1
  57. package/dist/cjs/query/live/collection-registry.cjs.map +1 -1
  58. package/dist/cjs/query/live/collection-subscriber.cjs +30 -15
  59. package/dist/cjs/query/live/collection-subscriber.cjs.map +1 -1
  60. package/dist/cjs/query/live/internal.cjs.map +1 -1
  61. package/dist/cjs/query/live-query-collection.cjs.map +1 -1
  62. package/dist/cjs/query/optimizer.cjs.map +1 -1
  63. package/dist/cjs/query/predicate-utils.cjs +19 -2
  64. package/dist/cjs/query/predicate-utils.cjs.map +1 -1
  65. package/dist/cjs/query/predicate-utils.d.cts +32 -1
  66. package/dist/cjs/query/subset-dedupe.cjs.map +1 -1
  67. package/dist/cjs/scheduler.cjs.map +1 -1
  68. package/dist/cjs/strategies/debounceStrategy.cjs.map +1 -1
  69. package/dist/cjs/strategies/queueStrategy.cjs.map +1 -1
  70. package/dist/cjs/strategies/throttleStrategy.cjs.map +1 -1
  71. package/dist/cjs/transactions.cjs.map +1 -1
  72. package/dist/cjs/types.d.cts +53 -8
  73. package/dist/cjs/utils/browser-polyfills.cjs.map +1 -1
  74. package/dist/cjs/utils/btree.cjs.map +1 -1
  75. package/dist/cjs/utils/comparison.cjs.map +1 -1
  76. package/dist/cjs/utils/cursor.cjs +39 -0
  77. package/dist/cjs/utils/cursor.cjs.map +1 -0
  78. package/dist/cjs/utils/cursor.d.cts +18 -0
  79. package/dist/cjs/utils/index-optimization.cjs.map +1 -1
  80. package/dist/cjs/utils.cjs.map +1 -1
  81. package/dist/esm/SortedMap.d.ts +10 -15
  82. package/dist/esm/SortedMap.js +40 -26
  83. package/dist/esm/SortedMap.js.map +1 -1
  84. package/dist/esm/collection/change-events.js.map +1 -1
  85. package/dist/esm/collection/changes.js +2 -0
  86. package/dist/esm/collection/changes.js.map +1 -1
  87. package/dist/esm/collection/events.d.ts +12 -4
  88. package/dist/esm/collection/events.js.map +1 -1
  89. package/dist/esm/collection/index.js +2 -1
  90. package/dist/esm/collection/index.js.map +1 -1
  91. package/dist/esm/collection/indexes.js.map +1 -1
  92. package/dist/esm/collection/lifecycle.js.map +1 -1
  93. package/dist/esm/collection/mutations.js +6 -3
  94. package/dist/esm/collection/mutations.js.map +1 -1
  95. package/dist/esm/collection/state.d.ts +4 -1
  96. package/dist/esm/collection/state.js +6 -5
  97. package/dist/esm/collection/state.js.map +1 -1
  98. package/dist/esm/collection/subscription.d.ts +26 -4
  99. package/dist/esm/collection/subscription.js +92 -58
  100. package/dist/esm/collection/subscription.js.map +1 -1
  101. package/dist/esm/collection/sync.js +11 -6
  102. package/dist/esm/collection/sync.js.map +1 -1
  103. package/dist/esm/errors.d.ts +3 -0
  104. package/dist/esm/errors.js +9 -0
  105. package/dist/esm/errors.js.map +1 -1
  106. package/dist/esm/event-emitter.js.map +1 -1
  107. package/dist/esm/index.d.ts +1 -1
  108. package/dist/esm/index.js +4 -2
  109. package/dist/esm/indexes/auto-index.js.map +1 -1
  110. package/dist/esm/indexes/base-index.js.map +1 -1
  111. package/dist/esm/indexes/btree-index.js +8 -6
  112. package/dist/esm/indexes/btree-index.js.map +1 -1
  113. package/dist/esm/indexes/lazy-index.js.map +1 -1
  114. package/dist/esm/indexes/reverse-index.js.map +1 -1
  115. package/dist/esm/local-only.js.map +1 -1
  116. package/dist/esm/local-storage.js.map +1 -1
  117. package/dist/esm/optimistic-action.js.map +1 -1
  118. package/dist/esm/paced-mutations.js.map +1 -1
  119. package/dist/esm/proxy.js.map +1 -1
  120. package/dist/esm/query/builder/functions.js.map +1 -1
  121. package/dist/esm/query/builder/index.js.map +1 -1
  122. package/dist/esm/query/builder/ref-proxy.js.map +1 -1
  123. package/dist/esm/query/compiler/evaluators.js.map +1 -1
  124. package/dist/esm/query/compiler/expressions.js.map +1 -1
  125. package/dist/esm/query/compiler/group-by.js.map +1 -1
  126. package/dist/esm/query/compiler/index.js.map +1 -1
  127. package/dist/esm/query/compiler/joins.js.map +1 -1
  128. package/dist/esm/query/compiler/order-by.d.ts +6 -2
  129. package/dist/esm/query/compiler/order-by.js +91 -38
  130. package/dist/esm/query/compiler/order-by.js.map +1 -1
  131. package/dist/esm/query/compiler/select.js.map +1 -1
  132. package/dist/esm/query/expression-helpers.js.map +1 -1
  133. package/dist/esm/query/index.d.ts +1 -1
  134. package/dist/esm/query/ir.js.map +1 -1
  135. package/dist/esm/query/live/collection-config-builder.js.map +1 -1
  136. package/dist/esm/query/live/collection-registry.js.map +1 -1
  137. package/dist/esm/query/live/collection-subscriber.js +30 -15
  138. package/dist/esm/query/live/collection-subscriber.js.map +1 -1
  139. package/dist/esm/query/live/internal.js.map +1 -1
  140. package/dist/esm/query/live-query-collection.js.map +1 -1
  141. package/dist/esm/query/optimizer.js.map +1 -1
  142. package/dist/esm/query/predicate-utils.d.ts +32 -1
  143. package/dist/esm/query/predicate-utils.js +19 -2
  144. package/dist/esm/query/predicate-utils.js.map +1 -1
  145. package/dist/esm/query/subset-dedupe.js.map +1 -1
  146. package/dist/esm/scheduler.js.map +1 -1
  147. package/dist/esm/strategies/debounceStrategy.js.map +1 -1
  148. package/dist/esm/strategies/queueStrategy.js.map +1 -1
  149. package/dist/esm/strategies/throttleStrategy.js.map +1 -1
  150. package/dist/esm/transactions.js.map +1 -1
  151. package/dist/esm/types.d.ts +53 -8
  152. package/dist/esm/utils/browser-polyfills.js.map +1 -1
  153. package/dist/esm/utils/btree.js.map +1 -1
  154. package/dist/esm/utils/comparison.js.map +1 -1
  155. package/dist/esm/utils/cursor.d.ts +18 -0
  156. package/dist/esm/utils/cursor.js +39 -0
  157. package/dist/esm/utils/cursor.js.map +1 -0
  158. package/dist/esm/utils/index-optimization.js.map +1 -1
  159. package/dist/esm/utils.js.map +1 -1
  160. package/package.json +30 -28
  161. package/src/SortedMap.ts +50 -31
  162. package/src/collection/change-events.ts +20 -20
  163. package/src/collection/changes.ts +16 -12
  164. package/src/collection/events.ts +20 -10
  165. package/src/collection/index.ts +47 -46
  166. package/src/collection/indexes.ts +14 -14
  167. package/src/collection/lifecycle.ts +16 -16
  168. package/src/collection/mutations.ts +25 -20
  169. package/src/collection/state.ts +43 -36
  170. package/src/collection/subscription.ts +171 -90
  171. package/src/collection/sync.ts +34 -22
  172. package/src/duplicate-instance-check.ts +1 -1
  173. package/src/errors.ts +49 -40
  174. package/src/event-emitter.ts +5 -5
  175. package/src/index.ts +21 -21
  176. package/src/indexes/auto-index.ts +11 -11
  177. package/src/indexes/base-index.ts +13 -13
  178. package/src/indexes/btree-index.ts +21 -17
  179. package/src/indexes/index-options.ts +3 -3
  180. package/src/indexes/lazy-index.ts +8 -8
  181. package/src/indexes/reverse-index.ts +5 -5
  182. package/src/local-only.ts +12 -12
  183. package/src/local-storage.ts +17 -17
  184. package/src/optimistic-action.ts +5 -5
  185. package/src/paced-mutations.ts +6 -6
  186. package/src/proxy.ts +43 -43
  187. package/src/query/builder/functions.ts +28 -28
  188. package/src/query/builder/index.ts +22 -22
  189. package/src/query/builder/ref-proxy.ts +4 -4
  190. package/src/query/builder/types.ts +8 -8
  191. package/src/query/compiler/evaluators.ts +9 -9
  192. package/src/query/compiler/expressions.ts +6 -6
  193. package/src/query/compiler/group-by.ts +24 -24
  194. package/src/query/compiler/index.ts +44 -44
  195. package/src/query/compiler/joins.ts +37 -37
  196. package/src/query/compiler/order-by.ts +170 -77
  197. package/src/query/compiler/select.ts +13 -13
  198. package/src/query/compiler/types.ts +2 -2
  199. package/src/query/expression-helpers.ts +16 -16
  200. package/src/query/index.ts +10 -9
  201. package/src/query/ir.ts +13 -13
  202. package/src/query/live/collection-config-builder.ts +53 -53
  203. package/src/query/live/collection-registry.ts +6 -6
  204. package/src/query/live/collection-subscriber.ts +87 -48
  205. package/src/query/live/internal.ts +1 -1
  206. package/src/query/live/types.ts +4 -4
  207. package/src/query/live-query-collection.ts +15 -15
  208. package/src/query/optimizer.ts +29 -29
  209. package/src/query/predicate-utils.ts +105 -50
  210. package/src/query/subset-dedupe.ts +6 -6
  211. package/src/scheduler.ts +3 -3
  212. package/src/strategies/debounceStrategy.ts +6 -6
  213. package/src/strategies/index.ts +4 -4
  214. package/src/strategies/queueStrategy.ts +5 -5
  215. package/src/strategies/throttleStrategy.ts +6 -6
  216. package/src/strategies/types.ts +2 -2
  217. package/src/transactions.ts +9 -9
  218. package/src/types.ts +76 -18
  219. package/src/utils/array-utils.ts +1 -1
  220. package/src/utils/browser-polyfills.ts +2 -2
  221. package/src/utils/btree.ts +22 -22
  222. package/src/utils/comparison.ts +3 -3
  223. package/src/utils/cursor.ts +78 -0
  224. package/src/utils/index-optimization.ts +14 -14
  225. package/src/utils.ts +4 -4
@@ -1,6 +1,6 @@
1
- import { EventEmitter } from "../event-emitter.js"
2
- import type { Collection } from "./index.js"
3
- import type { CollectionStatus } from "../types.js"
1
+ import { EventEmitter } from '../event-emitter.js'
2
+ import type { Collection } from './index.js'
3
+ import type { CollectionStatus } from '../types.js'
4
4
 
5
5
  /**
6
6
  * Event emitted when the collection status changes
@@ -43,10 +43,19 @@ export interface CollectionLoadingSubsetChangeEvent {
43
43
  loadingSubsetTransition: `start` | `end`
44
44
  }
45
45
 
46
+ /**
47
+ * Event emitted when the collection is truncated (all data cleared)
48
+ */
49
+ export interface CollectionTruncateEvent {
50
+ type: `truncate`
51
+ collection: Collection<any, any, any, any, any>
52
+ }
53
+
46
54
  export type AllCollectionEvents = {
47
- "status:change": CollectionStatusChangeEvent
48
- "subscribers:change": CollectionSubscribersChangeEvent
49
- "loadingSubset:change": CollectionLoadingSubsetChangeEvent
55
+ 'status:change': CollectionStatusChangeEvent
56
+ 'subscribers:change': CollectionSubscribersChangeEvent
57
+ 'loadingSubset:change': CollectionLoadingSubsetChangeEvent
58
+ truncate: CollectionTruncateEvent
50
59
  } & {
51
60
  [K in CollectionStatus as `status:${K}`]: CollectionStatusEvent<K>
52
61
  }
@@ -56,9 +65,10 @@ export type CollectionEvent =
56
65
  | CollectionStatusChangeEvent
57
66
  | CollectionSubscribersChangeEvent
58
67
  | CollectionLoadingSubsetChangeEvent
68
+ | CollectionTruncateEvent
59
69
 
60
70
  export type CollectionEventHandler<T extends keyof AllCollectionEvents> = (
61
- event: AllCollectionEvents[T]
71
+ event: AllCollectionEvents[T],
62
72
  ) => void
63
73
 
64
74
  export class CollectionEventsManager extends EventEmitter<AllCollectionEvents> {
@@ -78,14 +88,14 @@ export class CollectionEventsManager extends EventEmitter<AllCollectionEvents> {
78
88
  */
79
89
  emit<T extends keyof AllCollectionEvents>(
80
90
  event: T,
81
- eventPayload: AllCollectionEvents[T]
91
+ eventPayload: AllCollectionEvents[T],
82
92
  ): void {
83
93
  this.emitInner(event, eventPayload)
84
94
  }
85
95
 
86
96
  emitStatusChange<T extends CollectionStatus>(
87
97
  status: T,
88
- previousStatus: CollectionStatus
98
+ previousStatus: CollectionStatus,
89
99
  ) {
90
100
  this.emit(`status:change`, {
91
101
  type: `status:change`,
@@ -106,7 +116,7 @@ export class CollectionEventsManager extends EventEmitter<AllCollectionEvents> {
106
116
 
107
117
  emitSubscribersChange(
108
118
  subscriberCount: number,
109
- previousSubscriberCount: number
119
+ previousSubscriberCount: number,
110
120
  ) {
111
121
  this.emit(`subscribers:change`, {
112
122
  type: `subscribers:change`,
@@ -1,20 +1,20 @@
1
1
  import {
2
2
  CollectionRequiresConfigError,
3
3
  CollectionRequiresSyncConfigError,
4
- } from "../errors"
5
- import { currentStateAsChanges } from "./change-events"
6
-
7
- import { CollectionStateManager } from "./state"
8
- import { CollectionChangesManager } from "./changes"
9
- import { CollectionLifecycleManager } from "./lifecycle.js"
10
- import { CollectionSyncManager } from "./sync"
11
- import { CollectionIndexesManager } from "./indexes"
12
- import { CollectionMutationsManager } from "./mutations"
13
- import { CollectionEventsManager } from "./events.js"
14
- import type { CollectionSubscription } from "./subscription"
15
- import type { AllCollectionEvents, CollectionEventHandler } from "./events.js"
16
- import type { BaseIndex, IndexResolver } from "../indexes/base-index.js"
17
- import type { IndexOptions } from "../indexes/index-options.js"
4
+ } from '../errors'
5
+ import { currentStateAsChanges } from './change-events'
6
+
7
+ import { CollectionStateManager } from './state'
8
+ import { CollectionChangesManager } from './changes'
9
+ import { CollectionLifecycleManager } from './lifecycle.js'
10
+ import { CollectionSyncManager } from './sync'
11
+ import { CollectionIndexesManager } from './indexes'
12
+ import { CollectionMutationsManager } from './mutations'
13
+ import { CollectionEventsManager } from './events.js'
14
+ import type { CollectionSubscription } from './subscription'
15
+ import type { AllCollectionEvents, CollectionEventHandler } from './events.js'
16
+ import type { BaseIndex, IndexResolver } from '../indexes/base-index.js'
17
+ import type { IndexOptions } from '../indexes/index-options.js'
18
18
  import type {
19
19
  ChangeMessage,
20
20
  CollectionConfig,
@@ -32,11 +32,11 @@ import type {
32
32
  Transaction as TransactionType,
33
33
  UtilsRecord,
34
34
  WritableDeep,
35
- } from "../types"
36
- import type { SingleRowRefProxy } from "../query/builder/ref-proxy"
37
- import type { StandardSchemaV1 } from "@standard-schema/spec"
38
- import type { BTreeIndex } from "../indexes/btree-index.js"
39
- import type { IndexProxy } from "../indexes/lazy-index.js"
35
+ } from '../types'
36
+ import type { SingleRowRefProxy } from '../query/builder/ref-proxy'
37
+ import type { StandardSchemaV1 } from '@standard-schema/spec'
38
+ import type { BTreeIndex } from '../indexes/btree-index.js'
39
+ import type { IndexProxy } from '../indexes/lazy-index.js'
40
40
 
41
41
  /**
42
42
  * Enhanced Collection interface that includes both data type T and utilities TUtils
@@ -142,7 +142,7 @@ export function createCollection<
142
142
  > & {
143
143
  schema: T
144
144
  utils: TUtils // Required utils
145
- } & NonSingleResult
145
+ } & NonSingleResult,
146
146
  ): Collection<InferSchemaOutput<T>, TKey, TUtils, T, InferSchemaInput<T>> &
147
147
  NonSingleResult
148
148
 
@@ -155,7 +155,7 @@ export function createCollection<
155
155
  >(
156
156
  options: CollectionConfig<InferSchemaOutput<T>, TKey, T, TUtils> & {
157
157
  schema: T
158
- } & NonSingleResult
158
+ } & NonSingleResult,
159
159
  ): Collection<
160
160
  InferSchemaOutput<T>,
161
161
  TKey,
@@ -177,7 +177,7 @@ export function createCollection<
177
177
  > & {
178
178
  schema: T
179
179
  utils: TUtils // Required utils
180
- } & SingleResult
180
+ } & SingleResult,
181
181
  ): Collection<InferSchemaOutput<T>, TKey, TUtils, T, InferSchemaInput<T>> &
182
182
  SingleResult
183
183
 
@@ -189,7 +189,7 @@ export function createCollection<
189
189
  >(
190
190
  options: CollectionConfig<InferSchemaOutput<T>, TKey, T, TUtils> & {
191
191
  schema: T
192
- } & SingleResult
192
+ } & SingleResult,
193
193
  ): Collection<InferSchemaOutput<T>, TKey, TUtils, T, InferSchemaInput<T>> &
194
194
  SingleResult
195
195
 
@@ -203,7 +203,7 @@ export function createCollection<
203
203
  options: Omit<CollectionConfig<T, TKey, never, TUtils>, `utils`> & {
204
204
  schema?: never // prohibit schema if an explicit type is provided
205
205
  utils: TUtils // Required utils
206
- } & NonSingleResult
206
+ } & NonSingleResult,
207
207
  ): Collection<T, TKey, TUtils, never, T> & NonSingleResult
208
208
 
209
209
  // Overload for when no schema is provided
@@ -215,7 +215,7 @@ export function createCollection<
215
215
  >(
216
216
  options: CollectionConfig<T, TKey, never, TUtils> & {
217
217
  schema?: never // prohibit schema if an explicit type is provided
218
- } & NonSingleResult
218
+ } & NonSingleResult,
219
219
  ): Collection<T, TKey, TUtils, never, T> & NonSingleResult
220
220
 
221
221
  // Overload for when no schema is provided, singleResult is true, and utils is required
@@ -228,7 +228,7 @@ export function createCollection<
228
228
  options: Omit<CollectionConfig<T, TKey, never, TUtils>, `utils`> & {
229
229
  schema?: never // prohibit schema if an explicit type is provided
230
230
  utils: TUtils // Required utils
231
- } & SingleResult
231
+ } & SingleResult,
232
232
  ): Collection<T, TKey, TUtils, never, T> & SingleResult
233
233
 
234
234
  // Overload for when no schema is provided and singleResult is true
@@ -240,17 +240,17 @@ export function createCollection<
240
240
  >(
241
241
  options: CollectionConfig<T, TKey, never, TUtils> & {
242
242
  schema?: never // prohibit schema if an explicit type is provided
243
- } & SingleResult
243
+ } & SingleResult,
244
244
  ): Collection<T, TKey, TUtils, never, T> & SingleResult
245
245
 
246
246
  // Implementation
247
247
  export function createCollection(
248
248
  options: CollectionConfig<any, string | number, any, UtilsRecord> & {
249
249
  schema?: StandardSchemaV1
250
- }
250
+ },
251
251
  ): Collection<any, string | number, UtilsRecord, any, any> {
252
252
  const collection = new CollectionImpl<any, string | number, any, any, any>(
253
- options
253
+ options,
254
254
  )
255
255
 
256
256
  // Attach utils to collection
@@ -365,6 +365,7 @@ export class CollectionImpl<
365
365
  lifecycle: this._lifecycle,
366
366
  changes: this._changes,
367
367
  indexes: this._indexes,
368
+ events: this._events,
368
369
  })
369
370
  this._sync.setDeps({
370
371
  collection: this, // Required for passing to config.sync callback
@@ -500,7 +501,7 @@ export class CollectionImpl<
500
501
  * Execute a callback for each entry in the collection
501
502
  */
502
503
  public forEach(
503
- callbackfn: (value: TOutput, key: TKey, index: number) => void
504
+ callbackfn: (value: TOutput, key: TKey, index: number) => void,
504
505
  ): void {
505
506
  return this._state.forEach(callbackfn)
506
507
  }
@@ -509,7 +510,7 @@ export class CollectionImpl<
509
510
  * Create a new array with the results of calling a function for each entry in the collection
510
511
  */
511
512
  public map<U>(
512
- callbackfn: (value: TOutput, key: TKey, index: number) => U
513
+ callbackfn: (value: TOutput, key: TKey, index: number) => U,
513
514
  ): Array<U> {
514
515
  return this._state.map(callbackfn)
515
516
  }
@@ -553,7 +554,7 @@ export class CollectionImpl<
553
554
  */
554
555
  public createIndex<TResolver extends IndexResolver<TKey> = typeof BTreeIndex>(
555
556
  indexCallback: (row: SingleRowRefProxy<TOutput>) => any,
556
- config: IndexOptions<TResolver> = {}
557
+ config: IndexOptions<TResolver> = {},
557
558
  ): IndexProxy<TKey> {
558
559
  return this._indexes.createIndex(indexCallback, config)
559
560
  }
@@ -571,7 +572,7 @@ export class CollectionImpl<
571
572
  public validateData(
572
573
  data: unknown,
573
574
  type: `insert` | `update`,
574
- key?: TKey
575
+ key?: TKey,
575
576
  ): TOutput | never {
576
577
  return this._mutations.validateData(data, type, key)
577
578
  }
@@ -664,27 +665,27 @@ export class CollectionImpl<
664
665
  // Overload 1: Update multiple items with a callback
665
666
  update(
666
667
  key: Array<TKey | unknown>,
667
- callback: (drafts: Array<WritableDeep<TInput>>) => void
668
+ callback: (drafts: Array<WritableDeep<TInput>>) => void,
668
669
  ): TransactionType
669
670
 
670
671
  // Overload 2: Update multiple items with config and a callback
671
672
  update(
672
673
  keys: Array<TKey | unknown>,
673
674
  config: OperationConfig,
674
- callback: (drafts: Array<WritableDeep<TInput>>) => void
675
+ callback: (drafts: Array<WritableDeep<TInput>>) => void,
675
676
  ): TransactionType
676
677
 
677
678
  // Overload 3: Update a single item with a callback
678
679
  update(
679
680
  id: TKey | unknown,
680
- callback: (draft: WritableDeep<TInput>) => void
681
+ callback: (draft: WritableDeep<TInput>) => void,
681
682
  ): TransactionType
682
683
 
683
684
  // Overload 4: Update a single item with config and a callback
684
685
  update(
685
686
  id: TKey | unknown,
686
687
  config: OperationConfig,
687
- callback: (draft: WritableDeep<TInput>) => void
688
+ callback: (draft: WritableDeep<TInput>) => void,
688
689
  ): TransactionType
689
690
 
690
691
  update(
@@ -695,7 +696,7 @@ export class CollectionImpl<
695
696
  | OperationConfig,
696
697
  maybeCallback?:
697
698
  | ((draft: WritableDeep<TInput>) => void)
698
- | ((drafts: Array<WritableDeep<TInput>>) => void)
699
+ | ((drafts: Array<WritableDeep<TInput>>) => void),
699
700
  ) {
700
701
  return this._mutations.update(keys, configOrCallback, maybeCallback)
701
702
  }
@@ -732,7 +733,7 @@ export class CollectionImpl<
732
733
  */
733
734
  delete = (
734
735
  keys: Array<TKey> | TKey,
735
- config?: OperationConfig
736
+ config?: OperationConfig,
736
737
  ): TransactionType<any> => {
737
738
  return this._mutations.delete(keys, config)
738
739
  }
@@ -821,7 +822,7 @@ export class CollectionImpl<
821
822
  * })
822
823
  */
823
824
  public currentStateAsChanges(
824
- options: CurrentStateAsChangesOptions = {}
825
+ options: CurrentStateAsChangesOptions = {},
825
826
  ): Array<ChangeMessage<TOutput>> | void {
826
827
  return currentStateAsChanges(this, options)
827
828
  }
@@ -867,7 +868,7 @@ export class CollectionImpl<
867
868
  */
868
869
  public subscribeChanges(
869
870
  callback: (changes: Array<ChangeMessage<TOutput>>) => void,
870
- options: SubscribeChangesOptions = {}
871
+ options: SubscribeChangesOptions = {},
871
872
  ): CollectionSubscription {
872
873
  return this._changes.subscribeChanges(callback, options)
873
874
  }
@@ -877,7 +878,7 @@ export class CollectionImpl<
877
878
  */
878
879
  public on<T extends keyof AllCollectionEvents>(
879
880
  event: T,
880
- callback: CollectionEventHandler<T>
881
+ callback: CollectionEventHandler<T>,
881
882
  ) {
882
883
  return this._events.on(event, callback)
883
884
  }
@@ -887,7 +888,7 @@ export class CollectionImpl<
887
888
  */
888
889
  public once<T extends keyof AllCollectionEvents>(
889
890
  event: T,
890
- callback: CollectionEventHandler<T>
891
+ callback: CollectionEventHandler<T>,
891
892
  ) {
892
893
  return this._events.once(event, callback)
893
894
  }
@@ -897,7 +898,7 @@ export class CollectionImpl<
897
898
  */
898
899
  public off<T extends keyof AllCollectionEvents>(
899
900
  event: T,
900
- callback: CollectionEventHandler<T>
901
+ callback: CollectionEventHandler<T>,
901
902
  ) {
902
903
  this._events.off(event, callback)
903
904
  }
@@ -907,7 +908,7 @@ export class CollectionImpl<
907
908
  */
908
909
  public waitFor<T extends keyof AllCollectionEvents>(
909
910
  event: T,
910
- timeout?: number
911
+ timeout?: number,
911
912
  ) {
912
913
  return this._events.waitFor(event, timeout)
913
914
  }
@@ -923,7 +924,7 @@ export class CollectionImpl<
923
924
  }
924
925
 
925
926
  function buildCompareOptionsFromConfig(
926
- config: CollectionConfig<any, any, any>
927
+ config: CollectionConfig<any, any, any>,
927
928
  ): StringCollationConfig {
928
929
  if (config.defaultStringCollation) {
929
930
  const options = config.defaultStringCollation
@@ -1,16 +1,16 @@
1
- import { IndexProxy, LazyIndexWrapper } from "../indexes/lazy-index"
1
+ import { IndexProxy, LazyIndexWrapper } from '../indexes/lazy-index'
2
2
  import {
3
3
  createSingleRowRefProxy,
4
4
  toExpression,
5
- } from "../query/builder/ref-proxy"
6
- import { BTreeIndex } from "../indexes/btree-index"
7
- import type { StandardSchemaV1 } from "@standard-schema/spec"
8
- import type { BaseIndex, IndexResolver } from "../indexes/base-index"
9
- import type { ChangeMessage } from "../types"
10
- import type { IndexOptions } from "../indexes/index-options"
11
- import type { SingleRowRefProxy } from "../query/builder/ref-proxy"
12
- import type { CollectionLifecycleManager } from "./lifecycle"
13
- import type { CollectionStateManager } from "./state"
5
+ } from '../query/builder/ref-proxy'
6
+ import { BTreeIndex } from '../indexes/btree-index'
7
+ import type { StandardSchemaV1 } from '@standard-schema/spec'
8
+ import type { BaseIndex, IndexResolver } from '../indexes/base-index'
9
+ import type { ChangeMessage } from '../types'
10
+ import type { IndexOptions } from '../indexes/index-options'
11
+ import type { SingleRowRefProxy } from '../query/builder/ref-proxy'
12
+ import type { CollectionLifecycleManager } from './lifecycle'
13
+ import type { CollectionStateManager } from './state'
14
14
 
15
15
  export class CollectionIndexesManager<
16
16
  TOutput extends object = Record<string, unknown>,
@@ -41,7 +41,7 @@ export class CollectionIndexesManager<
41
41
  */
42
42
  public createIndex<TResolver extends IndexResolver<TKey> = typeof BTreeIndex>(
43
43
  indexCallback: (row: SingleRowRefProxy<TOutput>) => any,
44
- config: IndexOptions<TResolver> = {}
44
+ config: IndexOptions<TResolver> = {},
45
45
  ): IndexProxy<TKey> {
46
46
  this.lifecycle.validateCollectionUsable(`createIndex`)
47
47
 
@@ -60,7 +60,7 @@ export class CollectionIndexesManager<
60
60
  config.name,
61
61
  resolver,
62
62
  config.options,
63
- this.state.entries()
63
+ this.state.entries(),
64
64
  )
65
65
 
66
66
  this.lazyIndexes.set(indexId, lazyIndex)
@@ -109,7 +109,7 @@ export class CollectionIndexesManager<
109
109
 
110
110
  this.resolvedIndexes.set(indexId, resolvedIndex)
111
111
  return { indexId, resolvedIndex }
112
- }
112
+ },
113
113
  )
114
114
 
115
115
  await Promise.all(resolutionPromises)
@@ -121,7 +121,7 @@ export class CollectionIndexesManager<
121
121
  */
122
122
  private async resolveSingleIndex(
123
123
  indexId: number,
124
- lazyIndex: LazyIndexWrapper<TKey>
124
+ lazyIndex: LazyIndexWrapper<TKey>,
125
125
  ): Promise<BaseIndex<TKey>> {
126
126
  const resolvedIndex = await lazyIndex.resolve()
127
127
  resolvedIndex.build(this.state.entries())
@@ -2,19 +2,19 @@ import {
2
2
  CollectionInErrorStateError,
3
3
  CollectionStateError,
4
4
  InvalidCollectionStatusTransitionError,
5
- } from "../errors"
5
+ } from '../errors'
6
6
  import {
7
7
  safeCancelIdleCallback,
8
8
  safeRequestIdleCallback,
9
- } from "../utils/browser-polyfills"
10
- import type { IdleCallbackDeadline } from "../utils/browser-polyfills"
11
- import type { StandardSchemaV1 } from "@standard-schema/spec"
12
- import type { CollectionConfig, CollectionStatus } from "../types"
13
- import type { CollectionEventsManager } from "./events"
14
- import type { CollectionIndexesManager } from "./indexes"
15
- import type { CollectionChangesManager } from "./changes"
16
- import type { CollectionSyncManager } from "./sync"
17
- import type { CollectionStateManager } from "./state"
9
+ } from '../utils/browser-polyfills'
10
+ import type { IdleCallbackDeadline } from '../utils/browser-polyfills'
11
+ import type { StandardSchemaV1 } from '@standard-schema/spec'
12
+ import type { CollectionConfig, CollectionStatus } from '../types'
13
+ import type { CollectionEventsManager } from './events'
14
+ import type { CollectionIndexesManager } from './indexes'
15
+ import type { CollectionChangesManager } from './changes'
16
+ import type { CollectionSyncManager } from './sync'
17
+ import type { CollectionStateManager } from './state'
18
18
 
19
19
  export class CollectionLifecycleManager<
20
20
  TOutput extends object = Record<string, unknown>,
@@ -64,7 +64,7 @@ export class CollectionLifecycleManager<
64
64
  */
65
65
  public validateStatusTransition(
66
66
  from: CollectionStatus,
67
- to: CollectionStatus
67
+ to: CollectionStatus,
68
68
  ): void {
69
69
  if (from === to) {
70
70
  // Allow same state transitions
@@ -78,7 +78,7 @@ export class CollectionLifecycleManager<
78
78
  loading: [`ready`, `error`, `cleaned-up`],
79
79
  ready: [`cleaned-up`, `error`],
80
80
  error: [`cleaned-up`, `idle`],
81
- "cleaned-up": [`loading`, `error`],
81
+ 'cleaned-up': [`loading`, `error`],
82
82
  }
83
83
 
84
84
  if (!validTransitions[from].includes(to)) {
@@ -92,14 +92,14 @@ export class CollectionLifecycleManager<
92
92
  */
93
93
  public setStatus(
94
94
  newStatus: CollectionStatus,
95
- allowReady: boolean = false
95
+ allowReady: boolean = false,
96
96
  ): void {
97
97
  if (newStatus === `ready` && !allowReady) {
98
98
  // setStatus('ready') is an internal method that should not be called directly
99
99
  // Instead, use markReady to transition to ready triggering the necessary events
100
100
  // and side effects.
101
101
  throw new CollectionStateError(
102
- `You can't directly call "setStatus('ready'). You must use markReady instead.`
102
+ `You can't directly call "setStatus('ready'). You must use markReady instead.`,
103
103
  )
104
104
  }
105
105
  this.validateStatusTransition(this.status, newStatus)
@@ -112,7 +112,7 @@ export class CollectionLifecycleManager<
112
112
  this.indexes.resolveAllIndexes().catch((error) => {
113
113
  console.warn(
114
114
  `${this.config.id ? `[${this.config.id}] ` : ``}Failed to resolve indexes:`,
115
- error
115
+ error,
116
116
  )
117
117
  })
118
118
  }
@@ -235,7 +235,7 @@ export class CollectionLifecycleManager<
235
235
  this.idleCallbackId = null
236
236
  }
237
237
  },
238
- { timeout: 1000 }
238
+ { timeout: 1000 },
239
239
  )
240
240
  }
241
241
 
@@ -1,8 +1,9 @@
1
- import { withArrayChangeTracking, withChangeTracking } from "../proxy"
2
- import { createTransaction, getActiveTransaction } from "../transactions"
1
+ import { withArrayChangeTracking, withChangeTracking } from '../proxy'
2
+ import { createTransaction, getActiveTransaction } from '../transactions'
3
3
  import {
4
4
  DeleteKeyNotFoundError,
5
5
  DuplicateKeyError,
6
+ InvalidKeyError,
6
7
  InvalidSchemaError,
7
8
  KeyUpdateNotAllowedError,
8
9
  MissingDeleteHandlerError,
@@ -15,9 +16,9 @@ import {
15
16
  SchemaValidationError,
16
17
  UndefinedKeyError,
17
18
  UpdateKeyNotFoundError,
18
- } from "../errors"
19
- import type { Collection, CollectionImpl } from "./index.js"
20
- import type { StandardSchemaV1 } from "@standard-schema/spec"
19
+ } from '../errors'
20
+ import type { Collection, CollectionImpl } from './index.js'
21
+ import type { StandardSchemaV1 } from '@standard-schema/spec'
21
22
  import type {
22
23
  CollectionConfig,
23
24
  InsertConfig,
@@ -28,9 +29,9 @@ import type {
28
29
  TransactionWithMutations,
29
30
  UtilsRecord,
30
31
  WritableDeep,
31
- } from "../types"
32
- import type { CollectionLifecycleManager } from "./lifecycle"
33
- import type { CollectionStateManager } from "./state"
32
+ } from '../types'
33
+ import type { CollectionLifecycleManager } from './lifecycle'
34
+ import type { CollectionStateManager } from './state'
34
35
 
35
36
  export class CollectionMutationsManager<
36
37
  TOutput extends object = Record<string, unknown>,
@@ -72,7 +73,7 @@ export class CollectionMutationsManager<
72
73
  public validateData(
73
74
  data: unknown,
74
75
  type: `insert` | `update`,
75
- key?: TKey
76
+ key?: TKey,
76
77
  ): TOutput | never {
77
78
  if (!this.config.schema) return data as TOutput
78
79
 
@@ -113,7 +114,7 @@ export class CollectionMutationsManager<
113
114
  const validatedMergedData = result.value as TOutput
114
115
  const modifiedKeys = Object.keys(data)
115
116
  const extractedChanges = Object.fromEntries(
116
- modifiedKeys.map((k) => [k, validatedMergedData[k as keyof TOutput]])
117
+ modifiedKeys.map((k) => [k, validatedMergedData[k as keyof TOutput]]),
117
118
  ) as TOutput
118
119
 
119
120
  return extractedChanges
@@ -141,8 +142,12 @@ export class CollectionMutationsManager<
141
142
  }
142
143
 
143
144
  public generateGlobalKey(key: any, item: any): string {
144
- if (typeof key === `undefined`) {
145
- throw new UndefinedKeyError(item)
145
+ if (typeof key !== `string` && typeof key !== `number`) {
146
+ // Preserve specific error for undefined keys
147
+ if (typeof key === `undefined`) {
148
+ throw new UndefinedKeyError(item)
149
+ }
150
+ throw new InvalidKeyError(key, item)
146
151
  }
147
152
 
148
153
  return `KEY::${this.id}/${key}`
@@ -189,7 +194,7 @@ export class CollectionMutationsManager<
189
194
  Object.keys(item).map((k) => [
190
195
  k,
191
196
  validatedData[k as keyof typeof validatedData],
192
- ])
197
+ ]),
193
198
  ) as TInput,
194
199
  globalKey,
195
200
  key,
@@ -255,7 +260,7 @@ export class CollectionMutationsManager<
255
260
  | OperationConfig,
256
261
  maybeCallback?:
257
262
  | ((draft: WritableDeep<TInput>) => void)
258
- | ((drafts: Array<WritableDeep<TInput>>) => void)
263
+ | ((drafts: Array<WritableDeep<TInput>>) => void),
259
264
  ) {
260
265
  if (typeof keys === `undefined`) {
261
266
  throw new MissingUpdateArgumentError()
@@ -298,12 +303,12 @@ export class CollectionMutationsManager<
298
303
  // Use the proxy to track changes for all objects
299
304
  changesArray = withArrayChangeTracking(
300
305
  currentObjects,
301
- callback as (draft: Array<TInput>) => void
306
+ callback as (draft: Array<TInput>) => void,
302
307
  )
303
308
  } else {
304
309
  const result = withChangeTracking(
305
310
  currentObjects[0]!,
306
- callback as (draft: TInput) => void
311
+ callback as (draft: TInput) => void,
307
312
  )
308
313
  changesArray = [result]
309
314
  }
@@ -329,14 +334,14 @@ export class CollectionMutationsManager<
329
334
  const validatedUpdatePayload = this.validateData(
330
335
  itemChanges,
331
336
  `update`,
332
- key
337
+ key,
333
338
  )
334
339
 
335
340
  // Construct the full modified item by applying the validated update payload to the original item
336
341
  const modifiedItem = Object.assign(
337
342
  {},
338
343
  originalItem,
339
- validatedUpdatePayload
344
+ validatedUpdatePayload,
340
345
  )
341
346
 
342
347
  // Check if the ID of the item is being changed
@@ -361,7 +366,7 @@ export class CollectionMutationsManager<
361
366
  Object.keys(itemChanges).map((k) => [
362
367
  k,
363
368
  modifiedItem[k as keyof typeof modifiedItem],
364
- ])
369
+ ]),
365
370
  ) as TInput,
366
371
  globalKey,
367
372
  key,
@@ -444,7 +449,7 @@ export class CollectionMutationsManager<
444
449
  */
445
450
  delete = (
446
451
  keys: Array<TKey> | TKey,
447
- config?: OperationConfig
452
+ config?: OperationConfig,
448
453
  ): TransactionType<any> => {
449
454
  const state = this.state
450
455
  this.lifecycle.validateCollectionUsable(`delete`)