@electric-sql/d2ts
Version:
D2TS is a TypeScript implementation of Differential Dataflow.
89 lines (88 loc) • 3.6 kB
TypeScript
import { D2, RootStreamBuilder } from '../d2.js';
import { type Version, type Antichain } from '../order.js';
import { IStreamBuilder, Message, KeyValue } from '../types.js';
import { DifferenceStreamReader, DifferenceStreamWriter, UnaryOperator } from '../graph.js';
import { type Row, type ShapeStreamInterface, type ChangeMessage } from '@electric-sql/client';
export interface ElectricStreamToD2InputOptions<T extends Row<unknown> = Row> {
/** D2 Graph to send messages to */
graph: D2;
/** The Electric ShapeStream to consume */
stream: ShapeStreamInterface<T>;
/** The D2 input stream to send messages to */
input: RootStreamBuilder<[key: string, T]>;
/** Optional function to convert LSN to version number/Version */
lsnToVersion?: (lsn: number) => number | Version;
/** Optional function to convert LSN to frontier number/Antichain */
lsnToFrontier?: (lsn: number) => number | Antichain;
/** Initial LSN */
initialLsn?: number;
/** When to run the graph */
runOn?: 'up-to-date' | 'lsn-advance' | false;
/** Whether to log debug messages */
debug?: boolean | typeof console.log;
}
/**
* Connects an Electric ShapeStream to a D2 input stream
* IMPORTANT: Requires the ShapeStream to be configured with `replica: 'full'`
* @param options Configuration options
* @param options.stream The Electric ShapeStream to consume
* @param options.input The D2 input stream to send messages to
* @param options.lsnToVersion Optional function to convert LSN to version number/Version
* @param options.lsnToFrontier Optional function to convert LSN to frontier number/Antichain
* @returns The input stream for chaining
*
* @example
* ```ts
* // Create D2 graph
* const graph = new D2({ initialFrontier: 0 })
*
* // Create D2 input
* const input = graph.newInput<any>()
*
* // Configure the pipeline
* input
* .pipe(
* map(([key, data]) => data.value),
* filter(value => value > 10)
* )
*
* // Finalize graph
* graph.finalize()
*
* // Create Electric stream (example)
* const electricStream = new ShapeStream({
* url: 'http://localhost:3000/v1/shape',
* params: {
* table: 'items',
* replica: 'full',
* }
* })
*
* // Connect Electric stream to D2 input
* electricStreamToD2Input({
* stream: electricStream,
* input,
* })
* ```
*/
export declare function electricStreamToD2Input<T extends Row<unknown> = Row>({ graph, stream, input, lsnToVersion, lsnToFrontier, initialLsn, runOn, debug, }: ElectricStreamToD2InputOptions<T>): RootStreamBuilder<[key: string, T]>;
/**
* Operator that outputs the messages from the stream in ElectricSQL format
*
* TODO: Have two modes `replica=default` and `replica=full` to match the two modes
* of core Electric.
*/
export declare class OutputElectricMessagesOperator<K, V> extends UnaryOperator<[
K,
V
]> {
#private;
constructor(id: number, inputA: DifferenceStreamReader<[K, V]>, output: DifferenceStreamWriter<[K, V]>, fn: (data: ChangeMessage<Row<V>>[]) => void, initialFrontier: Antichain);
transformMessages(messages: Message<[K, V]>[]): ChangeMessage<Row<V>>[];
run(): void;
}
/**
* Outputs the messages from the stream in ElectricSQL format
* @param fn - The function to call with a batch of messages
*/
export declare function outputElectricMessages<K extends T extends KeyValue<infer K extends string, infer _V> ? K : never, V extends T extends KeyValue<K, infer V> ? V : never, T>(fn: (data: ChangeMessage<Row<V>>[]) => void): (stream: IStreamBuilder<T>) => IStreamBuilder<KeyValue<K, V>>;