@tanstack/db 0.0.26 → 0.0.29
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/change-events.cjs +141 -0
- package/dist/cjs/change-events.cjs.map +1 -0
- package/dist/cjs/change-events.d.cts +49 -0
- package/dist/cjs/collection.cjs +236 -90
- package/dist/cjs/collection.cjs.map +1 -1
- package/dist/cjs/collection.d.cts +95 -20
- package/dist/cjs/errors.cjs +509 -1
- package/dist/cjs/errors.cjs.map +1 -1
- package/dist/cjs/errors.d.cts +225 -1
- package/dist/cjs/index.cjs +82 -3
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +5 -1
- package/dist/cjs/indexes/auto-index.cjs +64 -0
- package/dist/cjs/indexes/auto-index.cjs.map +1 -0
- package/dist/cjs/indexes/auto-index.d.cts +9 -0
- package/dist/cjs/indexes/base-index.cjs +46 -0
- package/dist/cjs/indexes/base-index.cjs.map +1 -0
- package/dist/cjs/indexes/base-index.d.cts +54 -0
- package/dist/cjs/indexes/index-options.d.cts +13 -0
- package/dist/cjs/indexes/lazy-index.cjs +193 -0
- package/dist/cjs/indexes/lazy-index.cjs.map +1 -0
- package/dist/cjs/indexes/lazy-index.d.cts +96 -0
- package/dist/cjs/indexes/ordered-index.cjs +227 -0
- package/dist/cjs/indexes/ordered-index.cjs.map +1 -0
- package/dist/cjs/indexes/ordered-index.d.cts +72 -0
- package/dist/cjs/local-storage.cjs +9 -15
- package/dist/cjs/local-storage.cjs.map +1 -1
- package/dist/cjs/query/builder/functions.cjs +11 -0
- package/dist/cjs/query/builder/functions.cjs.map +1 -1
- package/dist/cjs/query/builder/functions.d.cts +4 -0
- package/dist/cjs/query/builder/index.cjs +6 -7
- package/dist/cjs/query/builder/index.cjs.map +1 -1
- package/dist/cjs/query/builder/ref-proxy.cjs +37 -0
- package/dist/cjs/query/builder/ref-proxy.cjs.map +1 -1
- package/dist/cjs/query/builder/ref-proxy.d.cts +12 -0
- package/dist/cjs/query/compiler/evaluators.cjs +83 -58
- package/dist/cjs/query/compiler/evaluators.cjs.map +1 -1
- package/dist/cjs/query/compiler/evaluators.d.cts +8 -0
- package/dist/cjs/query/compiler/expressions.cjs +61 -0
- package/dist/cjs/query/compiler/expressions.cjs.map +1 -0
- package/dist/cjs/query/compiler/expressions.d.cts +25 -0
- package/dist/cjs/query/compiler/group-by.cjs +5 -10
- package/dist/cjs/query/compiler/group-by.cjs.map +1 -1
- package/dist/cjs/query/compiler/index.cjs +23 -17
- package/dist/cjs/query/compiler/index.cjs.map +1 -1
- package/dist/cjs/query/compiler/index.d.cts +12 -3
- package/dist/cjs/query/compiler/joins.cjs +61 -12
- package/dist/cjs/query/compiler/joins.cjs.map +1 -1
- package/dist/cjs/query/compiler/order-by.cjs +4 -34
- package/dist/cjs/query/compiler/order-by.cjs.map +1 -1
- package/dist/cjs/query/compiler/types.d.cts +2 -2
- package/dist/cjs/query/live-query-collection.cjs +54 -12
- package/dist/cjs/query/live-query-collection.cjs.map +1 -1
- package/dist/cjs/query/optimizer.cjs +45 -7
- package/dist/cjs/query/optimizer.cjs.map +1 -1
- package/dist/cjs/query/optimizer.d.cts +13 -3
- package/dist/cjs/transactions.cjs +5 -4
- package/dist/cjs/transactions.cjs.map +1 -1
- package/dist/cjs/types.d.cts +31 -0
- package/dist/cjs/utils/array-utils.cjs +18 -0
- package/dist/cjs/utils/array-utils.cjs.map +1 -0
- package/dist/cjs/utils/array-utils.d.cts +8 -0
- package/dist/cjs/utils/comparison.cjs +52 -0
- package/dist/cjs/utils/comparison.cjs.map +1 -0
- package/dist/cjs/utils/comparison.d.cts +11 -0
- package/dist/cjs/utils/index-optimization.cjs +270 -0
- package/dist/cjs/utils/index-optimization.cjs.map +1 -0
- package/dist/cjs/utils/index-optimization.d.cts +29 -0
- package/dist/esm/change-events.d.ts +49 -0
- package/dist/esm/change-events.js +141 -0
- package/dist/esm/change-events.js.map +1 -0
- package/dist/esm/collection.d.ts +95 -20
- package/dist/esm/collection.js +234 -88
- package/dist/esm/collection.js.map +1 -1
- package/dist/esm/errors.d.ts +225 -1
- package/dist/esm/errors.js +510 -2
- package/dist/esm/errors.js.map +1 -1
- package/dist/esm/index.d.ts +5 -1
- package/dist/esm/index.js +81 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/indexes/auto-index.d.ts +9 -0
- package/dist/esm/indexes/auto-index.js +64 -0
- package/dist/esm/indexes/auto-index.js.map +1 -0
- package/dist/esm/indexes/base-index.d.ts +54 -0
- package/dist/esm/indexes/base-index.js +46 -0
- package/dist/esm/indexes/base-index.js.map +1 -0
- package/dist/esm/indexes/index-options.d.ts +13 -0
- package/dist/esm/indexes/lazy-index.d.ts +96 -0
- package/dist/esm/indexes/lazy-index.js +193 -0
- package/dist/esm/indexes/lazy-index.js.map +1 -0
- package/dist/esm/indexes/ordered-index.d.ts +72 -0
- package/dist/esm/indexes/ordered-index.js +227 -0
- package/dist/esm/indexes/ordered-index.js.map +1 -0
- package/dist/esm/local-storage.js +9 -15
- package/dist/esm/local-storage.js.map +1 -1
- package/dist/esm/query/builder/functions.d.ts +4 -0
- package/dist/esm/query/builder/functions.js +11 -0
- package/dist/esm/query/builder/functions.js.map +1 -1
- package/dist/esm/query/builder/index.js +6 -7
- package/dist/esm/query/builder/index.js.map +1 -1
- package/dist/esm/query/builder/ref-proxy.d.ts +12 -0
- package/dist/esm/query/builder/ref-proxy.js +37 -0
- package/dist/esm/query/builder/ref-proxy.js.map +1 -1
- package/dist/esm/query/compiler/evaluators.d.ts +8 -0
- package/dist/esm/query/compiler/evaluators.js +84 -59
- package/dist/esm/query/compiler/evaluators.js.map +1 -1
- package/dist/esm/query/compiler/expressions.d.ts +25 -0
- package/dist/esm/query/compiler/expressions.js +61 -0
- package/dist/esm/query/compiler/expressions.js.map +1 -0
- package/dist/esm/query/compiler/group-by.js +5 -10
- package/dist/esm/query/compiler/group-by.js.map +1 -1
- package/dist/esm/query/compiler/index.d.ts +12 -3
- package/dist/esm/query/compiler/index.js +23 -17
- package/dist/esm/query/compiler/index.js.map +1 -1
- package/dist/esm/query/compiler/joins.js +61 -12
- package/dist/esm/query/compiler/joins.js.map +1 -1
- package/dist/esm/query/compiler/order-by.js +1 -31
- package/dist/esm/query/compiler/order-by.js.map +1 -1
- package/dist/esm/query/compiler/types.d.ts +2 -2
- package/dist/esm/query/live-query-collection.js +54 -12
- package/dist/esm/query/live-query-collection.js.map +1 -1
- package/dist/esm/query/optimizer.d.ts +13 -3
- package/dist/esm/query/optimizer.js +40 -2
- package/dist/esm/query/optimizer.js.map +1 -1
- package/dist/esm/transactions.js +5 -4
- package/dist/esm/transactions.js.map +1 -1
- package/dist/esm/types.d.ts +31 -0
- package/dist/esm/utils/array-utils.d.ts +8 -0
- package/dist/esm/utils/array-utils.js +18 -0
- package/dist/esm/utils/array-utils.js.map +1 -0
- package/dist/esm/utils/comparison.d.ts +11 -0
- package/dist/esm/utils/comparison.js +52 -0
- package/dist/esm/utils/comparison.js.map +1 -0
- package/dist/esm/utils/index-optimization.d.ts +29 -0
- package/dist/esm/utils/index-optimization.js +270 -0
- package/dist/esm/utils/index-optimization.js.map +1 -0
- package/package.json +3 -2
- package/src/change-events.ts +257 -0
- package/src/collection.ts +321 -110
- package/src/errors.ts +545 -1
- package/src/index.ts +7 -1
- package/src/indexes/auto-index.ts +108 -0
- package/src/indexes/base-index.ts +119 -0
- package/src/indexes/index-options.ts +42 -0
- package/src/indexes/lazy-index.ts +251 -0
- package/src/indexes/ordered-index.ts +305 -0
- package/src/local-storage.ts +16 -17
- package/src/query/builder/functions.ts +14 -0
- package/src/query/builder/index.ts +12 -7
- package/src/query/builder/ref-proxy.ts +65 -0
- package/src/query/compiler/evaluators.ts +114 -62
- package/src/query/compiler/expressions.ts +92 -0
- package/src/query/compiler/group-by.ts +10 -10
- package/src/query/compiler/index.ts +52 -22
- package/src/query/compiler/joins.ts +114 -15
- package/src/query/compiler/order-by.ts +1 -45
- package/src/query/compiler/types.ts +2 -2
- package/src/query/live-query-collection.ts +95 -15
- package/src/query/optimizer.ts +94 -5
- package/src/transactions.ts +10 -4
- package/src/types.ts +38 -0
- package/src/utils/array-utils.ts +28 -0
- package/src/utils/comparison.ts +79 -0
- package/src/utils/index-optimization.ts +546 -0
package/dist/esm/collection.d.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import { SortedMap } from './SortedMap.js';
|
|
2
|
+
import { OrderedIndex } from './indexes/ordered-index.js';
|
|
3
|
+
import { IndexProxy } from './indexes/lazy-index.js';
|
|
2
4
|
import { Transaction } from './transactions.js';
|
|
3
|
-
import { ChangeListener, ChangeMessage, CollectionConfig, CollectionStatus, Fn, InsertConfig, OperationConfig, OptimisticChangeMessage, ResolveInsertInput, ResolveType, Transaction as TransactionType, UtilsRecord } from './types.js';
|
|
4
5
|
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
6
|
+
import { SingleRowRefProxy } from './query/builder/ref-proxy.js';
|
|
7
|
+
import { ChangeListener, ChangeMessage, CollectionConfig, CollectionStatus, CurrentStateAsChangesOptions, Fn, InsertConfig, OperationConfig, OptimisticChangeMessage, ResolveInsertInput, ResolveType, SubscribeChangesOptions, Transaction as TransactionType, UtilsRecord } from './types.js';
|
|
8
|
+
import { IndexOptions } from './indexes/index-options.js';
|
|
9
|
+
import { BaseIndex, IndexResolver } from './indexes/base-index.js';
|
|
5
10
|
export declare const collectionsStore: Map<string, CollectionImpl<any, any, any, StandardSchemaV1<unknown, unknown>, any>>;
|
|
6
11
|
interface PendingSyncedTransaction<T extends object = Record<string, unknown>> {
|
|
7
12
|
committed: boolean;
|
|
@@ -93,20 +98,6 @@ export interface Collection<T extends object = Record<string, unknown>, TKey ext
|
|
|
93
98
|
export declare function createCollection<TExplicit = unknown, TKey extends string | number = string | number, TUtils extends UtilsRecord = {}, TSchema extends StandardSchemaV1 = StandardSchemaV1, TFallback extends object = Record<string, unknown>>(options: CollectionConfig<ResolveType<TExplicit, TSchema, TFallback>, TKey, TSchema, ResolveInsertInput<TExplicit, TSchema, TFallback>> & {
|
|
94
99
|
utils?: TUtils;
|
|
95
100
|
}): Collection<ResolveType<TExplicit, TSchema, TFallback>, TKey, TUtils, TSchema, ResolveInsertInput<TExplicit, TSchema, TFallback>>;
|
|
96
|
-
/**
|
|
97
|
-
* Custom error class for schema validation errors
|
|
98
|
-
*/
|
|
99
|
-
export declare class SchemaValidationError extends Error {
|
|
100
|
-
type: `insert` | `update`;
|
|
101
|
-
issues: ReadonlyArray<{
|
|
102
|
-
message: string;
|
|
103
|
-
path?: ReadonlyArray<string | number | symbol>;
|
|
104
|
-
}>;
|
|
105
|
-
constructor(type: `insert` | `update`, issues: ReadonlyArray<{
|
|
106
|
-
message: string;
|
|
107
|
-
path?: ReadonlyArray<string | number | symbol>;
|
|
108
|
-
}>, message?: string);
|
|
109
|
-
}
|
|
110
101
|
export declare class CollectionImpl<T extends object = Record<string, unknown>, TKey extends string | number = string | number, TUtils extends UtilsRecord = {}, TSchema extends StandardSchemaV1 = StandardSchemaV1, TInsertInput extends object = T> {
|
|
111
102
|
config: CollectionConfig<T, TKey, TSchema, TInsertInput>;
|
|
112
103
|
transactions: SortedMap<string, Transaction<any>>;
|
|
@@ -116,6 +107,10 @@ export declare class CollectionImpl<T extends object = Record<string, unknown>,
|
|
|
116
107
|
optimisticUpserts: Map<TKey, T>;
|
|
117
108
|
optimisticDeletes: Set<TKey>;
|
|
118
109
|
private _size;
|
|
110
|
+
private lazyIndexes;
|
|
111
|
+
private resolvedIndexes;
|
|
112
|
+
private isIndexesResolved;
|
|
113
|
+
private indexCounter;
|
|
119
114
|
private changeListeners;
|
|
120
115
|
private changeKeyListeners;
|
|
121
116
|
utils: Record<string, Fn>;
|
|
@@ -293,6 +288,56 @@ export declare class CollectionImpl<T extends object = Record<string, unknown>,
|
|
|
293
288
|
private ensureStandardSchema;
|
|
294
289
|
getKeyFromItem(item: T): TKey;
|
|
295
290
|
generateGlobalKey(key: any, item: any): string;
|
|
291
|
+
/**
|
|
292
|
+
* Creates an index on a collection for faster queries.
|
|
293
|
+
* Indexes significantly improve query performance by allowing binary search
|
|
294
|
+
* and range queries instead of full scans.
|
|
295
|
+
*
|
|
296
|
+
* @template TResolver - The type of the index resolver (constructor or async loader)
|
|
297
|
+
* @param indexCallback - Function that extracts the indexed value from each item
|
|
298
|
+
* @param config - Configuration including index type and type-specific options
|
|
299
|
+
* @returns An index proxy that provides access to the index when ready
|
|
300
|
+
*
|
|
301
|
+
* @example
|
|
302
|
+
* // Create a default ordered index
|
|
303
|
+
* const ageIndex = collection.createIndex((row) => row.age)
|
|
304
|
+
*
|
|
305
|
+
* // Create a ordered index with custom options
|
|
306
|
+
* const ageIndex = collection.createIndex((row) => row.age, {
|
|
307
|
+
* indexType: OrderedIndex,
|
|
308
|
+
* options: { compareFn: customComparator },
|
|
309
|
+
* name: 'age_btree'
|
|
310
|
+
* })
|
|
311
|
+
*
|
|
312
|
+
* // Create an async-loaded index
|
|
313
|
+
* const textIndex = collection.createIndex((row) => row.content, {
|
|
314
|
+
* indexType: async () => {
|
|
315
|
+
* const { FullTextIndex } = await import('./indexes/fulltext.js')
|
|
316
|
+
* return FullTextIndex
|
|
317
|
+
* },
|
|
318
|
+
* options: { language: 'en' }
|
|
319
|
+
* })
|
|
320
|
+
*/
|
|
321
|
+
createIndex<TResolver extends IndexResolver<TKey> = typeof OrderedIndex>(indexCallback: (row: SingleRowRefProxy<T>) => any, config?: IndexOptions<TResolver>): IndexProxy<TKey>;
|
|
322
|
+
/**
|
|
323
|
+
* Resolve all lazy indexes (called when collection first syncs)
|
|
324
|
+
* @private
|
|
325
|
+
*/
|
|
326
|
+
private resolveAllIndexes;
|
|
327
|
+
/**
|
|
328
|
+
* Resolve a single index immediately
|
|
329
|
+
* @private
|
|
330
|
+
*/
|
|
331
|
+
private resolveSingleIndex;
|
|
332
|
+
/**
|
|
333
|
+
* Get resolved indexes for query optimization
|
|
334
|
+
*/
|
|
335
|
+
get indexes(): Map<number, BaseIndex<TKey>>;
|
|
336
|
+
/**
|
|
337
|
+
* Updates all indexes when the collection changes
|
|
338
|
+
* @private
|
|
339
|
+
*/
|
|
340
|
+
private updateIndexes;
|
|
296
341
|
private deepEqual;
|
|
297
342
|
private validateData;
|
|
298
343
|
/**
|
|
@@ -445,13 +490,27 @@ export declare class CollectionImpl<T extends object = Record<string, unknown>,
|
|
|
445
490
|
toArrayWhenReady(): Promise<Array<T>>;
|
|
446
491
|
/**
|
|
447
492
|
* Returns the current state of the collection as an array of changes
|
|
493
|
+
* @param options - Options including optional where filter
|
|
448
494
|
* @returns An array of changes
|
|
495
|
+
* @example
|
|
496
|
+
* // Get all items as changes
|
|
497
|
+
* const allChanges = collection.currentStateAsChanges()
|
|
498
|
+
*
|
|
499
|
+
* // Get only items matching a condition
|
|
500
|
+
* const activeChanges = collection.currentStateAsChanges({
|
|
501
|
+
* where: (row) => row.status === 'active'
|
|
502
|
+
* })
|
|
503
|
+
*
|
|
504
|
+
* // Get only items using a pre-compiled expression
|
|
505
|
+
* const activeChanges = collection.currentStateAsChanges({
|
|
506
|
+
* whereExpression: eq(row.status, 'active')
|
|
507
|
+
* })
|
|
449
508
|
*/
|
|
450
|
-
currentStateAsChanges(): Array<ChangeMessage<T>>;
|
|
509
|
+
currentStateAsChanges(options?: CurrentStateAsChangesOptions<T>): Array<ChangeMessage<T>>;
|
|
451
510
|
/**
|
|
452
511
|
* Subscribe to changes in the collection
|
|
453
512
|
* @param callback - Function called when items change
|
|
454
|
-
* @param options
|
|
513
|
+
* @param options - Subscription options including includeInitialState and where filter
|
|
455
514
|
* @returns Unsubscribe function - Call this to stop listening for changes
|
|
456
515
|
* @example
|
|
457
516
|
* // Basic subscription
|
|
@@ -468,10 +527,26 @@ export declare class CollectionImpl<T extends object = Record<string, unknown>,
|
|
|
468
527
|
* const unsubscribe = collection.subscribeChanges((changes) => {
|
|
469
528
|
* updateUI(changes)
|
|
470
529
|
* }, { includeInitialState: true })
|
|
530
|
+
*
|
|
531
|
+
* @example
|
|
532
|
+
* // Subscribe only to changes matching a condition
|
|
533
|
+
* const unsubscribe = collection.subscribeChanges((changes) => {
|
|
534
|
+
* updateUI(changes)
|
|
535
|
+
* }, {
|
|
536
|
+
* includeInitialState: true,
|
|
537
|
+
* where: (row) => row.status === 'active'
|
|
538
|
+
* })
|
|
539
|
+
*
|
|
540
|
+
* @example
|
|
541
|
+
* // Subscribe using a pre-compiled expression
|
|
542
|
+
* const unsubscribe = collection.subscribeChanges((changes) => {
|
|
543
|
+
* updateUI(changes)
|
|
544
|
+
* }, {
|
|
545
|
+
* includeInitialState: true,
|
|
546
|
+
* whereExpression: eq(row.status, 'active')
|
|
547
|
+
* })
|
|
471
548
|
*/
|
|
472
|
-
subscribeChanges(callback: (changes: Array<ChangeMessage<T>>) => void,
|
|
473
|
-
includeInitialState?: boolean;
|
|
474
|
-
}): () => void;
|
|
549
|
+
subscribeChanges(callback: (changes: Array<ChangeMessage<T>>) => void, options?: SubscribeChangesOptions<T>): () => void;
|
|
475
550
|
/**
|
|
476
551
|
* Subscribe to changes for a specific key
|
|
477
552
|
*/
|