UNPKG

@tanstack/db-collections

Version:

A collection for (aspirationally) every way of loading your data

81 lines (80 loc) 2.95 kB
import { CollectionConfig, MutationFnParams, SyncConfig, UtilsRecord } from '@tanstack/db'; import { Row, ShapeStreamOptions } from '@electric-sql/client'; /** * Configuration interface for Electric collection options */ export interface ElectricCollectionConfig<T extends Row<unknown>> { /** * Configuration options for the ElectricSQL ShapeStream */ shapeOptions: ShapeStreamOptions; /** * All standard Collection configuration properties */ id?: string; schema?: CollectionConfig<T>[`schema`]; getKey: CollectionConfig<T>[`getKey`]; sync?: CollectionConfig<T>[`sync`]; /** * Optional asynchronous handler function called before an insert operation * Must return an object containing a txid string * @param params Object containing transaction and mutation information * @returns Promise resolving to an object with txid */ onInsert?: (params: MutationFnParams<T>) => Promise<{ txid: string; } | undefined>; /** * Optional asynchronous handler function called before an update operation * Must return an object containing a txid string * @param params Object containing transaction and mutation information * @returns Promise resolving to an object with txid */ onUpdate?: (params: MutationFnParams<T>) => Promise<{ txid: string; } | undefined>; /** * Optional asynchronous handler function called before a delete operation * Must return an object containing a txid string * @param params Object containing transaction and mutation information * @returns Promise resolving to an object with txid */ onDelete?: (params: MutationFnParams<T>) => Promise<{ txid: string; } | undefined>; } /** * Type for the awaitTxId utility function */ export type AwaitTxIdFn = (txId: string, timeout?: number) => Promise<boolean>; /** * Electric collection utilities type */ export interface ElectricCollectionUtils extends UtilsRecord { awaitTxId: AwaitTxIdFn; } /** * Creates Electric collection options for use with a standard Collection * * @param config - Configuration options for the Electric collection * @returns Collection options with utilities */ export declare function electricCollectionOptions<T extends Row<unknown>>(config: ElectricCollectionConfig<T>): { sync: SyncConfig<T, string | number>; onInsert: ((params: MutationFnParams<T>) => Promise<{}>) | undefined; onUpdate: ((params: MutationFnParams<T>) => Promise<{ txid: string; } | undefined>) | undefined; onDelete: ((params: MutationFnParams<T>) => Promise<{ txid: string; } | undefined>) | undefined; utils: { awaitTxId: AwaitTxIdFn; }; /** * All standard Collection configuration properties */ id?: string; schema?: import('@tanstack/db').StandardSchema<T> | undefined; getKey: (item: T) => string | number; };