@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.
- package/dist/cjs/SortedMap.cjs +40 -26
- package/dist/cjs/SortedMap.cjs.map +1 -1
- package/dist/cjs/SortedMap.d.cts +10 -15
- package/dist/cjs/collection/change-events.cjs.map +1 -1
- package/dist/cjs/collection/changes.cjs +2 -0
- package/dist/cjs/collection/changes.cjs.map +1 -1
- package/dist/cjs/collection/events.cjs.map +1 -1
- package/dist/cjs/collection/events.d.cts +12 -4
- package/dist/cjs/collection/index.cjs +2 -1
- package/dist/cjs/collection/index.cjs.map +1 -1
- package/dist/cjs/collection/indexes.cjs.map +1 -1
- package/dist/cjs/collection/lifecycle.cjs.map +1 -1
- package/dist/cjs/collection/mutations.cjs +5 -2
- package/dist/cjs/collection/mutations.cjs.map +1 -1
- package/dist/cjs/collection/state.cjs +6 -5
- package/dist/cjs/collection/state.cjs.map +1 -1
- package/dist/cjs/collection/state.d.cts +4 -1
- package/dist/cjs/collection/subscription.cjs +91 -57
- package/dist/cjs/collection/subscription.cjs.map +1 -1
- package/dist/cjs/collection/subscription.d.cts +26 -4
- package/dist/cjs/collection/sync.cjs +11 -6
- package/dist/cjs/collection/sync.cjs.map +1 -1
- package/dist/cjs/errors.cjs +9 -0
- package/dist/cjs/errors.cjs.map +1 -1
- package/dist/cjs/errors.d.cts +3 -0
- package/dist/cjs/event-emitter.cjs.map +1 -1
- package/dist/cjs/index.cjs +2 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -1
- package/dist/cjs/indexes/auto-index.cjs.map +1 -1
- package/dist/cjs/indexes/base-index.cjs.map +1 -1
- package/dist/cjs/indexes/btree-index.cjs +8 -6
- package/dist/cjs/indexes/btree-index.cjs.map +1 -1
- package/dist/cjs/indexes/lazy-index.cjs.map +1 -1
- package/dist/cjs/indexes/reverse-index.cjs.map +1 -1
- package/dist/cjs/local-only.cjs.map +1 -1
- package/dist/cjs/local-storage.cjs.map +1 -1
- package/dist/cjs/optimistic-action.cjs.map +1 -1
- package/dist/cjs/paced-mutations.cjs.map +1 -1
- package/dist/cjs/proxy.cjs.map +1 -1
- package/dist/cjs/query/builder/functions.cjs.map +1 -1
- package/dist/cjs/query/builder/index.cjs.map +1 -1
- package/dist/cjs/query/builder/ref-proxy.cjs.map +1 -1
- package/dist/cjs/query/compiler/evaluators.cjs.map +1 -1
- package/dist/cjs/query/compiler/expressions.cjs.map +1 -1
- package/dist/cjs/query/compiler/group-by.cjs.map +1 -1
- package/dist/cjs/query/compiler/index.cjs.map +1 -1
- package/dist/cjs/query/compiler/joins.cjs.map +1 -1
- package/dist/cjs/query/compiler/order-by.cjs +91 -38
- package/dist/cjs/query/compiler/order-by.cjs.map +1 -1
- package/dist/cjs/query/compiler/order-by.d.cts +6 -2
- package/dist/cjs/query/compiler/select.cjs.map +1 -1
- package/dist/cjs/query/expression-helpers.cjs.map +1 -1
- package/dist/cjs/query/index.d.cts +1 -1
- package/dist/cjs/query/ir.cjs.map +1 -1
- package/dist/cjs/query/live/collection-config-builder.cjs.map +1 -1
- package/dist/cjs/query/live/collection-registry.cjs.map +1 -1
- package/dist/cjs/query/live/collection-subscriber.cjs +30 -15
- package/dist/cjs/query/live/collection-subscriber.cjs.map +1 -1
- package/dist/cjs/query/live/internal.cjs.map +1 -1
- package/dist/cjs/query/live-query-collection.cjs.map +1 -1
- package/dist/cjs/query/optimizer.cjs.map +1 -1
- package/dist/cjs/query/predicate-utils.cjs +19 -2
- package/dist/cjs/query/predicate-utils.cjs.map +1 -1
- package/dist/cjs/query/predicate-utils.d.cts +32 -1
- package/dist/cjs/query/subset-dedupe.cjs.map +1 -1
- package/dist/cjs/scheduler.cjs.map +1 -1
- package/dist/cjs/strategies/debounceStrategy.cjs.map +1 -1
- package/dist/cjs/strategies/queueStrategy.cjs.map +1 -1
- package/dist/cjs/strategies/throttleStrategy.cjs.map +1 -1
- package/dist/cjs/transactions.cjs.map +1 -1
- package/dist/cjs/types.d.cts +53 -8
- package/dist/cjs/utils/browser-polyfills.cjs.map +1 -1
- package/dist/cjs/utils/btree.cjs.map +1 -1
- package/dist/cjs/utils/comparison.cjs.map +1 -1
- package/dist/cjs/utils/cursor.cjs +39 -0
- package/dist/cjs/utils/cursor.cjs.map +1 -0
- package/dist/cjs/utils/cursor.d.cts +18 -0
- package/dist/cjs/utils/index-optimization.cjs.map +1 -1
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/esm/SortedMap.d.ts +10 -15
- package/dist/esm/SortedMap.js +40 -26
- package/dist/esm/SortedMap.js.map +1 -1
- package/dist/esm/collection/change-events.js.map +1 -1
- package/dist/esm/collection/changes.js +2 -0
- package/dist/esm/collection/changes.js.map +1 -1
- package/dist/esm/collection/events.d.ts +12 -4
- package/dist/esm/collection/events.js.map +1 -1
- package/dist/esm/collection/index.js +2 -1
- package/dist/esm/collection/index.js.map +1 -1
- package/dist/esm/collection/indexes.js.map +1 -1
- package/dist/esm/collection/lifecycle.js.map +1 -1
- package/dist/esm/collection/mutations.js +6 -3
- package/dist/esm/collection/mutations.js.map +1 -1
- package/dist/esm/collection/state.d.ts +4 -1
- package/dist/esm/collection/state.js +6 -5
- package/dist/esm/collection/state.js.map +1 -1
- package/dist/esm/collection/subscription.d.ts +26 -4
- package/dist/esm/collection/subscription.js +92 -58
- package/dist/esm/collection/subscription.js.map +1 -1
- package/dist/esm/collection/sync.js +11 -6
- package/dist/esm/collection/sync.js.map +1 -1
- package/dist/esm/errors.d.ts +3 -0
- package/dist/esm/errors.js +9 -0
- package/dist/esm/errors.js.map +1 -1
- package/dist/esm/event-emitter.js.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +4 -2
- package/dist/esm/indexes/auto-index.js.map +1 -1
- package/dist/esm/indexes/base-index.js.map +1 -1
- package/dist/esm/indexes/btree-index.js +8 -6
- package/dist/esm/indexes/btree-index.js.map +1 -1
- package/dist/esm/indexes/lazy-index.js.map +1 -1
- package/dist/esm/indexes/reverse-index.js.map +1 -1
- package/dist/esm/local-only.js.map +1 -1
- package/dist/esm/local-storage.js.map +1 -1
- package/dist/esm/optimistic-action.js.map +1 -1
- package/dist/esm/paced-mutations.js.map +1 -1
- package/dist/esm/proxy.js.map +1 -1
- package/dist/esm/query/builder/functions.js.map +1 -1
- package/dist/esm/query/builder/index.js.map +1 -1
- package/dist/esm/query/builder/ref-proxy.js.map +1 -1
- package/dist/esm/query/compiler/evaluators.js.map +1 -1
- package/dist/esm/query/compiler/expressions.js.map +1 -1
- package/dist/esm/query/compiler/group-by.js.map +1 -1
- package/dist/esm/query/compiler/index.js.map +1 -1
- package/dist/esm/query/compiler/joins.js.map +1 -1
- package/dist/esm/query/compiler/order-by.d.ts +6 -2
- package/dist/esm/query/compiler/order-by.js +91 -38
- package/dist/esm/query/compiler/order-by.js.map +1 -1
- package/dist/esm/query/compiler/select.js.map +1 -1
- package/dist/esm/query/expression-helpers.js.map +1 -1
- package/dist/esm/query/index.d.ts +1 -1
- package/dist/esm/query/ir.js.map +1 -1
- package/dist/esm/query/live/collection-config-builder.js.map +1 -1
- package/dist/esm/query/live/collection-registry.js.map +1 -1
- package/dist/esm/query/live/collection-subscriber.js +30 -15
- package/dist/esm/query/live/collection-subscriber.js.map +1 -1
- package/dist/esm/query/live/internal.js.map +1 -1
- package/dist/esm/query/live-query-collection.js.map +1 -1
- package/dist/esm/query/optimizer.js.map +1 -1
- package/dist/esm/query/predicate-utils.d.ts +32 -1
- package/dist/esm/query/predicate-utils.js +19 -2
- package/dist/esm/query/predicate-utils.js.map +1 -1
- package/dist/esm/query/subset-dedupe.js.map +1 -1
- package/dist/esm/scheduler.js.map +1 -1
- package/dist/esm/strategies/debounceStrategy.js.map +1 -1
- package/dist/esm/strategies/queueStrategy.js.map +1 -1
- package/dist/esm/strategies/throttleStrategy.js.map +1 -1
- package/dist/esm/transactions.js.map +1 -1
- package/dist/esm/types.d.ts +53 -8
- package/dist/esm/utils/browser-polyfills.js.map +1 -1
- package/dist/esm/utils/btree.js.map +1 -1
- package/dist/esm/utils/comparison.js.map +1 -1
- package/dist/esm/utils/cursor.d.ts +18 -0
- package/dist/esm/utils/cursor.js +39 -0
- package/dist/esm/utils/cursor.js.map +1 -0
- package/dist/esm/utils/index-optimization.js.map +1 -1
- package/dist/esm/utils.js.map +1 -1
- package/package.json +30 -28
- package/src/SortedMap.ts +50 -31
- package/src/collection/change-events.ts +20 -20
- package/src/collection/changes.ts +16 -12
- package/src/collection/events.ts +20 -10
- package/src/collection/index.ts +47 -46
- package/src/collection/indexes.ts +14 -14
- package/src/collection/lifecycle.ts +16 -16
- package/src/collection/mutations.ts +25 -20
- package/src/collection/state.ts +43 -36
- package/src/collection/subscription.ts +171 -90
- package/src/collection/sync.ts +34 -22
- package/src/duplicate-instance-check.ts +1 -1
- package/src/errors.ts +49 -40
- package/src/event-emitter.ts +5 -5
- package/src/index.ts +21 -21
- package/src/indexes/auto-index.ts +11 -11
- package/src/indexes/base-index.ts +13 -13
- package/src/indexes/btree-index.ts +21 -17
- package/src/indexes/index-options.ts +3 -3
- package/src/indexes/lazy-index.ts +8 -8
- package/src/indexes/reverse-index.ts +5 -5
- package/src/local-only.ts +12 -12
- package/src/local-storage.ts +17 -17
- package/src/optimistic-action.ts +5 -5
- package/src/paced-mutations.ts +6 -6
- package/src/proxy.ts +43 -43
- package/src/query/builder/functions.ts +28 -28
- package/src/query/builder/index.ts +22 -22
- package/src/query/builder/ref-proxy.ts +4 -4
- package/src/query/builder/types.ts +8 -8
- package/src/query/compiler/evaluators.ts +9 -9
- package/src/query/compiler/expressions.ts +6 -6
- package/src/query/compiler/group-by.ts +24 -24
- package/src/query/compiler/index.ts +44 -44
- package/src/query/compiler/joins.ts +37 -37
- package/src/query/compiler/order-by.ts +170 -77
- package/src/query/compiler/select.ts +13 -13
- package/src/query/compiler/types.ts +2 -2
- package/src/query/expression-helpers.ts +16 -16
- package/src/query/index.ts +10 -9
- package/src/query/ir.ts +13 -13
- package/src/query/live/collection-config-builder.ts +53 -53
- package/src/query/live/collection-registry.ts +6 -6
- package/src/query/live/collection-subscriber.ts +87 -48
- package/src/query/live/internal.ts +1 -1
- package/src/query/live/types.ts +4 -4
- package/src/query/live-query-collection.ts +15 -15
- package/src/query/optimizer.ts +29 -29
- package/src/query/predicate-utils.ts +105 -50
- package/src/query/subset-dedupe.ts +6 -6
- package/src/scheduler.ts +3 -3
- package/src/strategies/debounceStrategy.ts +6 -6
- package/src/strategies/index.ts +4 -4
- package/src/strategies/queueStrategy.ts +5 -5
- package/src/strategies/throttleStrategy.ts +6 -6
- package/src/strategies/types.ts +2 -2
- package/src/transactions.ts +9 -9
- package/src/types.ts +76 -18
- package/src/utils/array-utils.ts +1 -1
- package/src/utils/browser-polyfills.ts +2 -2
- package/src/utils/btree.ts +22 -22
- package/src/utils/comparison.ts +3 -3
- package/src/utils/cursor.ts +78 -0
- package/src/utils/index-optimization.ts +14 -14
- package/src/utils.ts +4 -4
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { compileSingleRowExpression } from
|
|
2
|
-
import { comparisonFunctions } from
|
|
3
|
-
import { DEFAULT_COMPARE_OPTIONS, deepEquals } from
|
|
4
|
-
import type { RangeQueryOptions } from
|
|
5
|
-
import type { CompareOptions } from
|
|
6
|
-
import type { BasicExpression, OrderByDirection } from
|
|
1
|
+
import { compileSingleRowExpression } from '../query/compiler/evaluators.js'
|
|
2
|
+
import { comparisonFunctions } from '../query/builder/functions.js'
|
|
3
|
+
import { DEFAULT_COMPARE_OPTIONS, deepEquals } from '../utils.js'
|
|
4
|
+
import type { RangeQueryOptions } from './btree-index.js'
|
|
5
|
+
import type { CompareOptions } from '../query/builder/types.js'
|
|
6
|
+
import type { BasicExpression, OrderByDirection } from '../query/ir.js'
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Operations that indexes can support, imported from available comparison functions
|
|
@@ -46,12 +46,12 @@ export interface IndexInterface<
|
|
|
46
46
|
take: (
|
|
47
47
|
n: number,
|
|
48
48
|
from?: TKey,
|
|
49
|
-
filterFn?: (key: TKey) => boolean
|
|
49
|
+
filterFn?: (key: TKey) => boolean,
|
|
50
50
|
) => Array<TKey>
|
|
51
51
|
takeReversed: (
|
|
52
52
|
n: number,
|
|
53
53
|
from?: TKey,
|
|
54
|
-
filterFn?: (key: TKey) => boolean
|
|
54
|
+
filterFn?: (key: TKey) => boolean,
|
|
55
55
|
) => Array<TKey>
|
|
56
56
|
|
|
57
57
|
get keyCount(): number
|
|
@@ -90,7 +90,7 @@ export abstract class BaseIndex<
|
|
|
90
90
|
id: number,
|
|
91
91
|
expression: BasicExpression,
|
|
92
92
|
name?: string,
|
|
93
|
-
options?: any
|
|
93
|
+
options?: any,
|
|
94
94
|
) {
|
|
95
95
|
this.id = id
|
|
96
96
|
this.expression = expression
|
|
@@ -109,12 +109,12 @@ export abstract class BaseIndex<
|
|
|
109
109
|
abstract take(
|
|
110
110
|
n: number,
|
|
111
111
|
from?: TKey,
|
|
112
|
-
filterFn?: (key: TKey) => boolean
|
|
112
|
+
filterFn?: (key: TKey) => boolean,
|
|
113
113
|
): Array<TKey>
|
|
114
114
|
abstract takeReversed(
|
|
115
115
|
n: number,
|
|
116
116
|
from?: TKey,
|
|
117
|
-
filterFn?: (key: TKey) => boolean
|
|
117
|
+
filterFn?: (key: TKey) => boolean,
|
|
118
118
|
): Array<TKey>
|
|
119
119
|
abstract get keyCount(): number
|
|
120
120
|
abstract equalityLookup(value: any): Set<TKey>
|
|
@@ -155,7 +155,7 @@ export abstract class BaseIndex<
|
|
|
155
155
|
|
|
156
156
|
return deepEquals(
|
|
157
157
|
thisCompareOptionsWithoutDirection,
|
|
158
|
-
compareOptionsWithoutDirection
|
|
158
|
+
compareOptionsWithoutDirection,
|
|
159
159
|
)
|
|
160
160
|
}
|
|
161
161
|
|
|
@@ -203,7 +203,7 @@ export type IndexConstructor<TKey extends string | number = string | number> =
|
|
|
203
203
|
id: number,
|
|
204
204
|
expression: BasicExpression,
|
|
205
205
|
name?: string,
|
|
206
|
-
options?: any
|
|
206
|
+
options?: any,
|
|
207
207
|
) => BaseIndex<TKey>
|
|
208
208
|
|
|
209
209
|
/**
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import type {
|
|
6
|
-
import type {
|
|
1
|
+
import { compareKeys } from '@tanstack/db-ivm'
|
|
2
|
+
import { BTree } from '../utils/btree.js'
|
|
3
|
+
import { defaultComparator, normalizeValue } from '../utils/comparison.js'
|
|
4
|
+
import { BaseIndex } from './base-index.js'
|
|
5
|
+
import type { CompareOptions } from '../query/builder/types.js'
|
|
6
|
+
import type { BasicExpression } from '../query/ir.js'
|
|
7
|
+
import type { IndexOperation } from './base-index.js'
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Options for Ordered index
|
|
@@ -51,7 +52,7 @@ export class BTreeIndex<
|
|
|
51
52
|
id: number,
|
|
52
53
|
expression: BasicExpression,
|
|
53
54
|
name?: string,
|
|
54
|
-
options?: any
|
|
55
|
+
options?: any,
|
|
55
56
|
) {
|
|
56
57
|
super(id, expression, name, options)
|
|
57
58
|
this.compareFn = options?.compareFn ?? defaultComparator
|
|
@@ -72,7 +73,7 @@ export class BTreeIndex<
|
|
|
72
73
|
indexedValue = this.evaluateIndexExpression(item)
|
|
73
74
|
} catch (error) {
|
|
74
75
|
throw new Error(
|
|
75
|
-
`Failed to evaluate index expression for key ${key}: ${error}
|
|
76
|
+
`Failed to evaluate index expression for key ${key}: ${error}`,
|
|
76
77
|
)
|
|
77
78
|
}
|
|
78
79
|
|
|
@@ -104,7 +105,7 @@ export class BTreeIndex<
|
|
|
104
105
|
} catch (error) {
|
|
105
106
|
console.warn(
|
|
106
107
|
`Failed to evaluate index expression for key ${key} during removal:`,
|
|
107
|
-
error
|
|
108
|
+
error,
|
|
108
109
|
)
|
|
109
110
|
return
|
|
110
111
|
}
|
|
@@ -238,7 +239,7 @@ export class BTreeIndex<
|
|
|
238
239
|
if (keys) {
|
|
239
240
|
keys.forEach((key) => result.add(key))
|
|
240
241
|
}
|
|
241
|
-
}
|
|
242
|
+
},
|
|
242
243
|
)
|
|
243
244
|
|
|
244
245
|
return result
|
|
@@ -261,7 +262,8 @@ export class BTreeIndex<
|
|
|
261
262
|
n: number,
|
|
262
263
|
nextPair: (k?: any) => [any, any] | undefined,
|
|
263
264
|
from?: any,
|
|
264
|
-
filterFn?: (key: TKey) => boolean
|
|
265
|
+
filterFn?: (key: TKey) => boolean,
|
|
266
|
+
reversed: boolean = false,
|
|
265
267
|
): Array<TKey> {
|
|
266
268
|
const keysInResult: Set<TKey> = new Set()
|
|
267
269
|
const result: Array<TKey> = []
|
|
@@ -271,10 +273,12 @@ export class BTreeIndex<
|
|
|
271
273
|
while ((pair = nextPair(key)) !== undefined && result.length < n) {
|
|
272
274
|
key = pair[0]
|
|
273
275
|
const keys = this.valueMap.get(key)
|
|
274
|
-
if (keys) {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
276
|
+
if (keys && keys.size > 0) {
|
|
277
|
+
// Sort keys for deterministic order, reverse if needed
|
|
278
|
+
const sorted = Array.from(keys).sort(compareKeys)
|
|
279
|
+
if (reversed) sorted.reverse()
|
|
280
|
+
for (const ks of sorted) {
|
|
281
|
+
if (result.length >= n) break
|
|
278
282
|
if (!keysInResult.has(ks) && (filterFn?.(ks) ?? true)) {
|
|
279
283
|
result.push(ks)
|
|
280
284
|
keysInResult.add(ks)
|
|
@@ -306,10 +310,10 @@ export class BTreeIndex<
|
|
|
306
310
|
takeReversed(
|
|
307
311
|
n: number,
|
|
308
312
|
from?: any,
|
|
309
|
-
filterFn?: (key: TKey) => boolean
|
|
313
|
+
filterFn?: (key: TKey) => boolean,
|
|
310
314
|
): Array<TKey> {
|
|
311
315
|
const nextPair = (k?: any) => this.orderedEntries.nextLowerPair(k)
|
|
312
|
-
return this.takeInternal(n, nextPair, from, filterFn)
|
|
316
|
+
return this.takeInternal(n, nextPair, from, filterFn, true)
|
|
313
317
|
}
|
|
314
318
|
|
|
315
319
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { IndexConstructor, IndexResolver } from
|
|
1
|
+
import type { IndexConstructor, IndexResolver } from './base-index.js'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Enhanced index options that support both sync and async resolvers
|
|
@@ -11,7 +11,7 @@ export interface IndexOptions<TResolver extends IndexResolver = IndexResolver> {
|
|
|
11
11
|
id: number,
|
|
12
12
|
expr: any,
|
|
13
13
|
name?: string,
|
|
14
|
-
options?: infer O
|
|
14
|
+
options?: infer O,
|
|
15
15
|
) => any
|
|
16
16
|
? O
|
|
17
17
|
: never
|
|
@@ -20,7 +20,7 @@ export interface IndexOptions<TResolver extends IndexResolver = IndexResolver> {
|
|
|
20
20
|
id: number,
|
|
21
21
|
expr: any,
|
|
22
22
|
name?: string,
|
|
23
|
-
options?: infer O
|
|
23
|
+
options?: infer O,
|
|
24
24
|
) => any
|
|
25
25
|
? O
|
|
26
26
|
: never
|
|
@@ -2,14 +2,14 @@ import type {
|
|
|
2
2
|
BaseIndex,
|
|
3
3
|
IndexConstructor,
|
|
4
4
|
IndexResolver,
|
|
5
|
-
} from
|
|
6
|
-
import type { BasicExpression } from
|
|
5
|
+
} from './base-index.js'
|
|
6
|
+
import type { BasicExpression } from '../query/ir.js'
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Utility to determine if a resolver is a constructor or async loader
|
|
10
10
|
*/
|
|
11
11
|
function isConstructor<TKey extends string | number>(
|
|
12
|
-
resolver: IndexResolver<TKey
|
|
12
|
+
resolver: IndexResolver<TKey>,
|
|
13
13
|
): resolver is IndexConstructor<TKey> {
|
|
14
14
|
// Check if it's a function with a prototype (constructor)
|
|
15
15
|
return (
|
|
@@ -23,7 +23,7 @@ function isConstructor<TKey extends string | number>(
|
|
|
23
23
|
* Resolve index constructor from resolver
|
|
24
24
|
*/
|
|
25
25
|
async function resolveIndexConstructor<TKey extends string | number>(
|
|
26
|
-
resolver: IndexResolver<TKey
|
|
26
|
+
resolver: IndexResolver<TKey>,
|
|
27
27
|
): Promise<IndexConstructor<TKey>> {
|
|
28
28
|
if (isConstructor(resolver)) {
|
|
29
29
|
return resolver
|
|
@@ -46,7 +46,7 @@ export class LazyIndexWrapper<TKey extends string | number = string | number> {
|
|
|
46
46
|
private name: string | undefined,
|
|
47
47
|
private resolver: IndexResolver<TKey>,
|
|
48
48
|
private options: any,
|
|
49
|
-
private collectionEntries?: Iterable<[TKey, any]
|
|
49
|
+
private collectionEntries?: Iterable<[TKey, any]>,
|
|
50
50
|
) {
|
|
51
51
|
// For synchronous constructors, resolve immediately
|
|
52
52
|
if (isConstructor(this.resolver)) {
|
|
@@ -54,7 +54,7 @@ export class LazyIndexWrapper<TKey extends string | number = string | number> {
|
|
|
54
54
|
this.id,
|
|
55
55
|
this.expression,
|
|
56
56
|
this.name,
|
|
57
|
-
this.options
|
|
57
|
+
this.options,
|
|
58
58
|
)
|
|
59
59
|
// Build with initial data if provided
|
|
60
60
|
if (this.collectionEntries) {
|
|
@@ -92,7 +92,7 @@ export class LazyIndexWrapper<TKey extends string | number = string | number> {
|
|
|
92
92
|
getResolved(): BaseIndex<TKey> {
|
|
93
93
|
if (!this.resolvedIndex) {
|
|
94
94
|
throw new Error(
|
|
95
|
-
`Index ${this.id} has not been resolved yet. Ensure collection is synced
|
|
95
|
+
`Index ${this.id} has not been resolved yet. Ensure collection is synced.`,
|
|
96
96
|
)
|
|
97
97
|
}
|
|
98
98
|
return this.resolvedIndex
|
|
@@ -131,7 +131,7 @@ export class LazyIndexWrapper<TKey extends string | number = string | number> {
|
|
|
131
131
|
export class IndexProxy<TKey extends string | number = string | number> {
|
|
132
132
|
constructor(
|
|
133
133
|
private indexId: number,
|
|
134
|
-
private lazyIndex: LazyIndexWrapper<TKey
|
|
134
|
+
private lazyIndex: LazyIndexWrapper<TKey>,
|
|
135
135
|
) {}
|
|
136
136
|
|
|
137
137
|
/**
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { CompareOptions } from
|
|
2
|
-
import type { OrderByDirection } from
|
|
3
|
-
import type { IndexInterface, IndexOperation, IndexStats } from
|
|
4
|
-
import type { RangeQueryOptions } from
|
|
1
|
+
import type { CompareOptions } from '../query/builder/types'
|
|
2
|
+
import type { OrderByDirection } from '../query/ir'
|
|
3
|
+
import type { IndexInterface, IndexOperation, IndexStats } from './base-index'
|
|
4
|
+
import type { RangeQueryOptions } from './btree-index'
|
|
5
5
|
|
|
6
6
|
export class ReverseIndex<
|
|
7
7
|
TKey extends string | number,
|
|
@@ -43,7 +43,7 @@ export class ReverseIndex<
|
|
|
43
43
|
takeReversed(
|
|
44
44
|
n: number,
|
|
45
45
|
from?: any,
|
|
46
|
-
filterFn?: (key: TKey) => boolean
|
|
46
|
+
filterFn?: (key: TKey) => boolean,
|
|
47
47
|
): Array<TKey> {
|
|
48
48
|
return this.originalIndex.take(n, from, filterFn)
|
|
49
49
|
}
|
package/src/local-only.ts
CHANGED
|
@@ -9,9 +9,9 @@ import type {
|
|
|
9
9
|
SyncConfig,
|
|
10
10
|
UpdateMutationFnParams,
|
|
11
11
|
UtilsRecord,
|
|
12
|
-
} from
|
|
13
|
-
import type { Collection } from
|
|
14
|
-
import type { StandardSchemaV1 } from
|
|
12
|
+
} from './types'
|
|
13
|
+
import type { Collection } from './collection/index'
|
|
14
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec'
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* Configuration interface for Local-only collection options
|
|
@@ -152,7 +152,7 @@ export function localOnlyCollectionOptions<
|
|
|
152
152
|
>(
|
|
153
153
|
config: LocalOnlyCollectionConfig<InferSchemaOutput<T>, T, TKey> & {
|
|
154
154
|
schema: T
|
|
155
|
-
}
|
|
155
|
+
},
|
|
156
156
|
): LocalOnlyCollectionOptionsResult<InferSchemaOutput<T>, TKey, T> & {
|
|
157
157
|
schema: T
|
|
158
158
|
}
|
|
@@ -165,7 +165,7 @@ export function localOnlyCollectionOptions<
|
|
|
165
165
|
>(
|
|
166
166
|
config: LocalOnlyCollectionConfig<T, never, TKey> & {
|
|
167
167
|
schema?: never // prohibit schema
|
|
168
|
-
}
|
|
168
|
+
},
|
|
169
169
|
): LocalOnlyCollectionOptionsResult<T, TKey> & {
|
|
170
170
|
schema?: never // no schema in the result
|
|
171
171
|
}
|
|
@@ -175,7 +175,7 @@ export function localOnlyCollectionOptions<
|
|
|
175
175
|
TSchema extends StandardSchemaV1 = never,
|
|
176
176
|
TKey extends string | number = string | number,
|
|
177
177
|
>(
|
|
178
|
-
config: LocalOnlyCollectionConfig<T, TSchema, TKey
|
|
178
|
+
config: LocalOnlyCollectionConfig<T, TSchema, TKey>,
|
|
179
179
|
): LocalOnlyCollectionOptionsResult<T, TKey, TSchema> & {
|
|
180
180
|
schema?: StandardSchemaV1
|
|
181
181
|
} {
|
|
@@ -189,7 +189,7 @@ export function localOnlyCollectionOptions<
|
|
|
189
189
|
* Wraps the user's onInsert handler to also confirm the transaction immediately
|
|
190
190
|
*/
|
|
191
191
|
const wrappedOnInsert = async (
|
|
192
|
-
params: InsertMutationFnParams<T, TKey, LocalOnlyCollectionUtils
|
|
192
|
+
params: InsertMutationFnParams<T, TKey, LocalOnlyCollectionUtils>,
|
|
193
193
|
) => {
|
|
194
194
|
// Call user handler first if provided
|
|
195
195
|
let handlerResult
|
|
@@ -207,7 +207,7 @@ export function localOnlyCollectionOptions<
|
|
|
207
207
|
* Wrapper for onUpdate handler that also confirms the transaction immediately
|
|
208
208
|
*/
|
|
209
209
|
const wrappedOnUpdate = async (
|
|
210
|
-
params: UpdateMutationFnParams<T, TKey, LocalOnlyCollectionUtils
|
|
210
|
+
params: UpdateMutationFnParams<T, TKey, LocalOnlyCollectionUtils>,
|
|
211
211
|
) => {
|
|
212
212
|
// Call user handler first if provided
|
|
213
213
|
let handlerResult
|
|
@@ -225,7 +225,7 @@ export function localOnlyCollectionOptions<
|
|
|
225
225
|
* Wrapper for onDelete handler that also confirms the transaction immediately
|
|
226
226
|
*/
|
|
227
227
|
const wrappedOnDelete = async (
|
|
228
|
-
params: DeleteMutationFnParams<T, TKey, LocalOnlyCollectionUtils
|
|
228
|
+
params: DeleteMutationFnParams<T, TKey, LocalOnlyCollectionUtils>,
|
|
229
229
|
) => {
|
|
230
230
|
// Call user handler first if provided
|
|
231
231
|
let handlerResult
|
|
@@ -249,7 +249,7 @@ export function localOnlyCollectionOptions<
|
|
|
249
249
|
const collectionMutations = transaction.mutations.filter(
|
|
250
250
|
(m) =>
|
|
251
251
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
252
|
-
m.collection === syncResult.collection
|
|
252
|
+
m.collection === syncResult.collection,
|
|
253
253
|
)
|
|
254
254
|
|
|
255
255
|
if (collectionMutations.length === 0) {
|
|
@@ -258,7 +258,7 @@ export function localOnlyCollectionOptions<
|
|
|
258
258
|
|
|
259
259
|
// Persist the mutations through sync
|
|
260
260
|
syncResult.confirmOperationsSync(
|
|
261
|
-
collectionMutations as Array<PendingMutation<T
|
|
261
|
+
collectionMutations as Array<PendingMutation<T>>,
|
|
262
262
|
)
|
|
263
263
|
}
|
|
264
264
|
|
|
@@ -289,7 +289,7 @@ export function localOnlyCollectionOptions<
|
|
|
289
289
|
* @returns Object with sync configuration and confirmOperationsSync function
|
|
290
290
|
*/
|
|
291
291
|
function createLocalOnlySync<T extends object, TKey extends string | number>(
|
|
292
|
-
initialData?: Array<T
|
|
292
|
+
initialData?: Array<T>,
|
|
293
293
|
) {
|
|
294
294
|
// Capture sync functions and collection for transaction confirmation
|
|
295
295
|
let syncBegin: (() => void) | null = null
|
package/src/local-storage.ts
CHANGED
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
InvalidStorageObjectFormatError,
|
|
4
4
|
SerializationError,
|
|
5
5
|
StorageKeyRequiredError,
|
|
6
|
-
} from
|
|
6
|
+
} from './errors'
|
|
7
7
|
import type {
|
|
8
8
|
BaseCollectionConfig,
|
|
9
9
|
CollectionConfig,
|
|
@@ -14,8 +14,8 @@ import type {
|
|
|
14
14
|
SyncConfig,
|
|
15
15
|
UpdateMutationFnParams,
|
|
16
16
|
UtilsRecord,
|
|
17
|
-
} from
|
|
18
|
-
import type { StandardSchemaV1 } from
|
|
17
|
+
} from './types'
|
|
18
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec'
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
21
|
* Storage API interface - subset of DOM Storage that we need
|
|
@@ -28,11 +28,11 @@ export type StorageApi = Pick<Storage, `getItem` | `setItem` | `removeItem`>
|
|
|
28
28
|
export type StorageEventApi = {
|
|
29
29
|
addEventListener: (
|
|
30
30
|
type: `storage`,
|
|
31
|
-
listener: (event: StorageEvent) => void
|
|
31
|
+
listener: (event: StorageEvent) => void,
|
|
32
32
|
) => void
|
|
33
33
|
removeEventListener: (
|
|
34
34
|
type: `storage`,
|
|
35
|
-
listener: (event: StorageEvent) => void
|
|
35
|
+
listener: (event: StorageEvent) => void,
|
|
36
36
|
) => void
|
|
37
37
|
}
|
|
38
38
|
|
|
@@ -132,14 +132,14 @@ export interface LocalStorageCollectionUtils extends UtilsRecord {
|
|
|
132
132
|
function validateJsonSerializable(
|
|
133
133
|
parser: Parser,
|
|
134
134
|
value: any,
|
|
135
|
-
operation: string
|
|
135
|
+
operation: string,
|
|
136
136
|
): void {
|
|
137
137
|
try {
|
|
138
138
|
parser.stringify(value)
|
|
139
139
|
} catch (error) {
|
|
140
140
|
throw new SerializationError(
|
|
141
141
|
operation,
|
|
142
|
-
error instanceof Error ? error.message : String(error)
|
|
142
|
+
error instanceof Error ? error.message : String(error),
|
|
143
143
|
)
|
|
144
144
|
}
|
|
145
145
|
}
|
|
@@ -319,7 +319,7 @@ export function localStorageCollectionOptions<
|
|
|
319
319
|
>(
|
|
320
320
|
config: LocalStorageCollectionConfig<InferSchemaOutput<T>, T, TKey> & {
|
|
321
321
|
schema: T
|
|
322
|
-
}
|
|
322
|
+
},
|
|
323
323
|
): CollectionConfig<
|
|
324
324
|
InferSchemaOutput<T>,
|
|
325
325
|
TKey,
|
|
@@ -339,7 +339,7 @@ export function localStorageCollectionOptions<
|
|
|
339
339
|
>(
|
|
340
340
|
config: LocalStorageCollectionConfig<T, never, TKey> & {
|
|
341
341
|
schema?: never // prohibit schema
|
|
342
|
-
}
|
|
342
|
+
},
|
|
343
343
|
): CollectionConfig<T, TKey, never, LocalStorageCollectionUtils> & {
|
|
344
344
|
id: string
|
|
345
345
|
utils: LocalStorageCollectionUtils
|
|
@@ -347,7 +347,7 @@ export function localStorageCollectionOptions<
|
|
|
347
347
|
}
|
|
348
348
|
|
|
349
349
|
export function localStorageCollectionOptions(
|
|
350
|
-
config: LocalStorageCollectionConfig<any, any, string | number
|
|
350
|
+
config: LocalStorageCollectionConfig<any, any, string | number>,
|
|
351
351
|
): Omit<
|
|
352
352
|
CollectionConfig<any, string | number, any, LocalStorageCollectionUtils>,
|
|
353
353
|
`id`
|
|
@@ -388,7 +388,7 @@ export function localStorageCollectionOptions(
|
|
|
388
388
|
storageEventApi,
|
|
389
389
|
parser,
|
|
390
390
|
config.getKey,
|
|
391
|
-
lastKnownData
|
|
391
|
+
lastKnownData,
|
|
392
392
|
)
|
|
393
393
|
|
|
394
394
|
/**
|
|
@@ -396,7 +396,7 @@ export function localStorageCollectionOptions(
|
|
|
396
396
|
* @param dataMap - Map of items with version tracking to save to storage
|
|
397
397
|
*/
|
|
398
398
|
const saveToStorage = (
|
|
399
|
-
dataMap: Map<string | number, StoredItem<any
|
|
399
|
+
dataMap: Map<string | number, StoredItem<any>>,
|
|
400
400
|
): void => {
|
|
401
401
|
try {
|
|
402
402
|
// Convert Map to object format for storage
|
|
@@ -409,7 +409,7 @@ export function localStorageCollectionOptions(
|
|
|
409
409
|
} catch (error) {
|
|
410
410
|
console.error(
|
|
411
411
|
`[LocalStorageCollection] Error saving data to storage key "${config.storageKey}":`,
|
|
412
|
-
error
|
|
412
|
+
error,
|
|
413
413
|
)
|
|
414
414
|
throw error
|
|
415
415
|
}
|
|
@@ -631,7 +631,7 @@ export function localStorageCollectionOptions(
|
|
|
631
631
|
function loadFromStorage<T extends object>(
|
|
632
632
|
storageKey: string,
|
|
633
633
|
storage: StorageApi,
|
|
634
|
-
parser: Parser
|
|
634
|
+
parser: Parser,
|
|
635
635
|
): Map<string | number, StoredItem<T>> {
|
|
636
636
|
try {
|
|
637
637
|
const rawData = storage.getItem(storageKey)
|
|
@@ -671,7 +671,7 @@ function loadFromStorage<T extends object>(
|
|
|
671
671
|
} catch (error) {
|
|
672
672
|
console.warn(
|
|
673
673
|
`[LocalStorageCollection] Error loading data from storage key "${storageKey}":`,
|
|
674
|
-
error
|
|
674
|
+
error,
|
|
675
675
|
)
|
|
676
676
|
return new Map()
|
|
677
677
|
}
|
|
@@ -693,7 +693,7 @@ function createLocalStorageSync<T extends object>(
|
|
|
693
693
|
storageEventApi: StorageEventApi,
|
|
694
694
|
parser: Parser,
|
|
695
695
|
_getKey: (item: T) => string | number,
|
|
696
|
-
lastKnownData: Map<string | number, StoredItem<T
|
|
696
|
+
lastKnownData: Map<string | number, StoredItem<T>>,
|
|
697
697
|
): SyncConfig<T> & {
|
|
698
698
|
manualTrigger?: () => void
|
|
699
699
|
collection: any
|
|
@@ -710,7 +710,7 @@ function createLocalStorageSync<T extends object>(
|
|
|
710
710
|
*/
|
|
711
711
|
const findChanges = (
|
|
712
712
|
oldData: Map<string | number, StoredItem<T>>,
|
|
713
|
-
newData: Map<string | number, StoredItem<T
|
|
713
|
+
newData: Map<string | number, StoredItem<T>>,
|
|
714
714
|
): Array<{
|
|
715
715
|
type: `insert` | `update` | `delete`
|
|
716
716
|
key: string | number
|
package/src/optimistic-action.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { createTransaction } from
|
|
2
|
-
import { OnMutateMustBeSynchronousError } from
|
|
3
|
-
import { isPromiseLike } from
|
|
4
|
-
import type { CreateOptimisticActionsOptions, Transaction } from
|
|
1
|
+
import { createTransaction } from './transactions'
|
|
2
|
+
import { OnMutateMustBeSynchronousError } from './errors'
|
|
3
|
+
import { isPromiseLike } from './utils/type-guards'
|
|
4
|
+
import type { CreateOptimisticActionsOptions, Transaction } from './types'
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Creates an optimistic action function that applies local optimistic updates immediately
|
|
@@ -52,7 +52,7 @@ import type { CreateOptimisticActionsOptions, Transaction } from "./types"
|
|
|
52
52
|
* @returns A function that accepts variables of type TVariables and returns a Transaction
|
|
53
53
|
*/
|
|
54
54
|
export function createOptimisticAction<TVariables = unknown>(
|
|
55
|
-
options: CreateOptimisticActionsOptions<TVariables
|
|
55
|
+
options: CreateOptimisticActionsOptions<TVariables>,
|
|
56
56
|
) {
|
|
57
57
|
const { mutationFn, onMutate, ...config } = options
|
|
58
58
|
|
package/src/paced-mutations.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { createTransaction } from
|
|
2
|
-
import type { MutationFn, Transaction } from
|
|
3
|
-
import type { Strategy } from
|
|
1
|
+
import { createTransaction } from './transactions'
|
|
2
|
+
import type { MutationFn, Transaction } from './types'
|
|
3
|
+
import type { Strategy } from './strategies/types'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Configuration for creating a paced mutations manager
|
|
@@ -88,7 +88,7 @@ export function createPacedMutations<
|
|
|
88
88
|
TVariables = unknown,
|
|
89
89
|
T extends object = Record<string, unknown>,
|
|
90
90
|
>(
|
|
91
|
-
config: PacedMutationsConfig<TVariables, T
|
|
91
|
+
config: PacedMutationsConfig<TVariables, T>,
|
|
92
92
|
): (variables: TVariables) => Transaction<T> {
|
|
93
93
|
const { onMutate, mutationFn, strategy, ...transactionConfig } = config
|
|
94
94
|
|
|
@@ -99,13 +99,13 @@ export function createPacedMutations<
|
|
|
99
99
|
const commitCallback = () => {
|
|
100
100
|
if (!activeTransaction) {
|
|
101
101
|
throw new Error(
|
|
102
|
-
`Strategy callback called but no active transaction exists. This indicates a bug in the strategy implementation
|
|
102
|
+
`Strategy callback called but no active transaction exists. This indicates a bug in the strategy implementation.`,
|
|
103
103
|
)
|
|
104
104
|
}
|
|
105
105
|
|
|
106
106
|
if (activeTransaction.state !== `pending`) {
|
|
107
107
|
throw new Error(
|
|
108
|
-
`Strategy callback called but active transaction is in state "${activeTransaction.state}". Expected "pending"
|
|
108
|
+
`Strategy callback called but active transaction is in state "${activeTransaction.state}". Expected "pending".`,
|
|
109
109
|
)
|
|
110
110
|
}
|
|
111
111
|
|