@tanstack/db 0.5.32 → 0.5.33
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/index.cjs +2 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -0
- package/dist/cjs/query/effect.cjs +602 -0
- package/dist/cjs/query/effect.cjs.map +1 -0
- package/dist/cjs/query/effect.d.cts +94 -0
- package/dist/cjs/query/live/collection-config-builder.cjs +5 -74
- package/dist/cjs/query/live/collection-config-builder.cjs.map +1 -1
- package/dist/cjs/query/live/collection-subscriber.cjs +33 -100
- package/dist/cjs/query/live/collection-subscriber.cjs.map +1 -1
- package/dist/cjs/query/live/collection-subscriber.d.cts +0 -1
- package/dist/cjs/query/live/utils.cjs +179 -0
- package/dist/cjs/query/live/utils.cjs.map +1 -0
- package/dist/cjs/query/live/utils.d.cts +109 -0
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/query/effect.d.ts +94 -0
- package/dist/esm/query/effect.js +602 -0
- package/dist/esm/query/effect.js.map +1 -0
- package/dist/esm/query/live/collection-config-builder.js +1 -70
- package/dist/esm/query/live/collection-config-builder.js.map +1 -1
- package/dist/esm/query/live/collection-subscriber.d.ts +0 -1
- package/dist/esm/query/live/collection-subscriber.js +31 -98
- package/dist/esm/query/live/collection-subscriber.js.map +1 -1
- package/dist/esm/query/live/utils.d.ts +109 -0
- package/dist/esm/query/live/utils.js +179 -0
- package/dist/esm/query/live/utils.js.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +11 -0
- package/src/query/effect.ts +1119 -0
- package/src/query/live/collection-config-builder.ts +6 -132
- package/src/query/live/collection-subscriber.ts +40 -156
- package/src/query/live/utils.ts +356 -0
package/dist/esm/index.d.ts
CHANGED
|
@@ -17,6 +17,7 @@ export * from './indexes/btree-index.js';
|
|
|
17
17
|
export * from './indexes/lazy-index.js';
|
|
18
18
|
export { type IndexOptions } from './indexes/index-options.js';
|
|
19
19
|
export * from './query/expression-helpers.js';
|
|
20
|
+
export { createEffect, type DeltaEvent, type DeltaType, type EffectConfig, type EffectContext, type Effect, type EffectQueryInput, } from './query/effect.js';
|
|
20
21
|
export type { Collection } from './collection/index.js';
|
|
21
22
|
export { IR };
|
|
22
23
|
export { operators, type OperatorName } from './query/builder/functions.js';
|
package/dist/esm/index.js
CHANGED
|
@@ -13,6 +13,7 @@ import { BaseIndex, IndexOperation } from "./indexes/base-index.js";
|
|
|
13
13
|
import { BTreeIndex } from "./indexes/btree-index.js";
|
|
14
14
|
import { IndexProxy, LazyIndexWrapper } from "./indexes/lazy-index.js";
|
|
15
15
|
import { extractFieldPath, extractSimpleComparisons, extractValue, parseLoadSubsetOptions, parseOrderByExpression, parseWhereExpression, walkExpression } from "./query/expression-helpers.js";
|
|
16
|
+
import { createEffect } from "./query/effect.js";
|
|
16
17
|
import { add, and, avg, coalesce, concat, count, eq, gt, gte, ilike, inArray, isNull, isUndefined, length, like, lower, lt, lte, max, min, not, operators, or, sum, upper } from "./query/builder/functions.js";
|
|
17
18
|
import { BaseQueryBuilder, Query } from "./query/builder/index.js";
|
|
18
19
|
import { DeduplicatedLoadSubset } from "./query/subset-dedupe.js";
|
|
@@ -131,6 +132,7 @@ export {
|
|
|
131
132
|
createArrayChangeProxy,
|
|
132
133
|
createChangeProxy,
|
|
133
134
|
createCollection,
|
|
135
|
+
createEffect,
|
|
134
136
|
createLiveQueryCollection,
|
|
135
137
|
createOptimisticAction,
|
|
136
138
|
createPacedMutations,
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { InitialQueryBuilder, QueryBuilder } from './builder/index.js';
|
|
2
|
+
import { Context } from './builder/types.js';
|
|
3
|
+
/** Event types for query result deltas */
|
|
4
|
+
export type DeltaType = 'enter' | 'exit' | 'update';
|
|
5
|
+
/** Delta event emitted when a row enters, exits, or updates within a query result */
|
|
6
|
+
export type DeltaEvent<TRow extends object = Record<string, unknown>, TKey extends string | number = string | number> = {
|
|
7
|
+
type: 'enter';
|
|
8
|
+
key: TKey;
|
|
9
|
+
/** Current value for the entering row */
|
|
10
|
+
value: TRow;
|
|
11
|
+
metadata?: Record<string, unknown>;
|
|
12
|
+
} | {
|
|
13
|
+
type: 'exit';
|
|
14
|
+
key: TKey;
|
|
15
|
+
/** Current value for the exiting row */
|
|
16
|
+
value: TRow;
|
|
17
|
+
metadata?: Record<string, unknown>;
|
|
18
|
+
} | {
|
|
19
|
+
type: 'update';
|
|
20
|
+
key: TKey;
|
|
21
|
+
/** Current value after the update */
|
|
22
|
+
value: TRow;
|
|
23
|
+
/** Previous value before the batch */
|
|
24
|
+
previousValue: TRow;
|
|
25
|
+
metadata?: Record<string, unknown>;
|
|
26
|
+
};
|
|
27
|
+
/** Context passed to effect handlers */
|
|
28
|
+
export interface EffectContext {
|
|
29
|
+
/** ID of this effect (auto-generated if not provided) */
|
|
30
|
+
effectId: string;
|
|
31
|
+
/** Aborted when effect.dispose() is called */
|
|
32
|
+
signal: AbortSignal;
|
|
33
|
+
}
|
|
34
|
+
/** Query input - can be a builder function or a prebuilt query */
|
|
35
|
+
export type EffectQueryInput<TContext extends Context> = ((q: InitialQueryBuilder) => QueryBuilder<TContext>) | QueryBuilder<TContext>;
|
|
36
|
+
type EffectEventHandler<TRow extends object = Record<string, unknown>, TKey extends string | number = string | number> = (event: DeltaEvent<TRow, TKey>, ctx: EffectContext) => void | Promise<void>;
|
|
37
|
+
type EffectBatchHandler<TRow extends object = Record<string, unknown>, TKey extends string | number = string | number> = (events: Array<DeltaEvent<TRow, TKey>>, ctx: EffectContext) => void | Promise<void>;
|
|
38
|
+
/** Effect configuration */
|
|
39
|
+
export interface EffectConfig<TRow extends object = Record<string, unknown>, TKey extends string | number = string | number> {
|
|
40
|
+
/** Optional ID for debugging/tracing */
|
|
41
|
+
id?: string;
|
|
42
|
+
/** Query to watch for deltas */
|
|
43
|
+
query: EffectQueryInput<any>;
|
|
44
|
+
/** Called once for each row entering the query result */
|
|
45
|
+
onEnter?: EffectEventHandler<TRow, TKey>;
|
|
46
|
+
/** Called once for each row updating within the query result */
|
|
47
|
+
onUpdate?: EffectEventHandler<TRow, TKey>;
|
|
48
|
+
/** Called once for each row exiting the query result */
|
|
49
|
+
onExit?: EffectEventHandler<TRow, TKey>;
|
|
50
|
+
/** Called once per graph run with all delta events from that batch */
|
|
51
|
+
onBatch?: EffectBatchHandler<TRow, TKey>;
|
|
52
|
+
/** Error handler for exceptions thrown by effect callbacks */
|
|
53
|
+
onError?: (error: Error, event: DeltaEvent<TRow, TKey>) => void;
|
|
54
|
+
/**
|
|
55
|
+
* Called when a source collection enters an error or cleaned-up state.
|
|
56
|
+
* The effect is automatically disposed after this callback fires.
|
|
57
|
+
* If not provided, the error is logged to console.error.
|
|
58
|
+
*/
|
|
59
|
+
onSourceError?: (error: Error) => void;
|
|
60
|
+
/**
|
|
61
|
+
* Skip deltas during initial collection load.
|
|
62
|
+
* Defaults to false (process all deltas including initial sync).
|
|
63
|
+
* Set to true for effects that should only process new changes.
|
|
64
|
+
*/
|
|
65
|
+
skipInitial?: boolean;
|
|
66
|
+
}
|
|
67
|
+
/** Handle returned by createEffect */
|
|
68
|
+
export interface Effect {
|
|
69
|
+
/** Dispose the effect. Returns a promise that resolves when in-flight handlers complete. */
|
|
70
|
+
dispose: () => Promise<void>;
|
|
71
|
+
/** Whether this effect has been disposed */
|
|
72
|
+
readonly disposed: boolean;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Creates a reactive effect that fires handlers when rows enter, exit, or
|
|
76
|
+
* update within a query result. Effects process deltas only — they do not
|
|
77
|
+
* maintain or require the full materialised query result.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```typescript
|
|
81
|
+
* const effect = createEffect({
|
|
82
|
+
* query: (q) => q.from({ msg: messagesCollection })
|
|
83
|
+
* .where(({ msg }) => eq(msg.role, 'user')),
|
|
84
|
+
* onEnter: async (event) => {
|
|
85
|
+
* await generateResponse(event.value)
|
|
86
|
+
* },
|
|
87
|
+
* })
|
|
88
|
+
*
|
|
89
|
+
* // Later: stop the effect
|
|
90
|
+
* await effect.dispose()
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export declare function createEffect<TRow extends object = Record<string, unknown>, TKey extends string | number = string | number>(config: EffectConfig<TRow, TKey>): Effect;
|
|
94
|
+
export {};
|