import { z } from 'zod'; /** * TanStack DB Collection Options for dotdo * * Provides CollectionOptions factory for creating TanStack DB collections * that sync with dotdo backends via WebSocket. * * @example * ```tsx * import { CollectionOptions } from '@dotdo/react/tanstack' * import { useCollection } from '@tanstack/db' * * const taskOptions = CollectionOptions({ * doUrl: 'wss://my-do.workers.dev/do/123', * collection: 'Task', * schema: TaskSchema, * }) * * function TaskList() { * const collection = useCollection(taskOptions) * // ... * } * ``` * * @module @dotdo/react/tanstack */ /** * Callbacks used by TanStack DB collection for sync operations */ interface CollectionCallbacks { /** Called at the start of a transaction */ begin: () => void; /** Called with initial or batch data */ onData: (items: T[]) => void; /** Called when a new item is inserted */ onInsert: (item: T) => void; /** Called when an item is updated */ onUpdate: (item: T) => void; /** Called when an item is deleted */ onDelete: (item: { id: string; }) => void; /** Called to commit a transaction */ commit: (info: { txid: number; }) => void; /** Called when a sync error occurs (optional) */ onError?: (error: Error) => void; } /** * Configuration for dotdo collection */ interface DotdoCollectionConfig { /** WebSocket URL for the Durable Object (will append /sync) */ doUrl: string; /** Collection name (Noun type) */ collection: string; /** Optional branch for branched data */ branch?: string; /** Zod schema for validating items */ schema: z.ZodSchema; /** Optional fetch options for RPC calls */ fetchOptions?: RequestInit; /** Optional transaction timeout in ms (default: 30000) */ transactionTimeout?: number; } /** * Mutation from TanStack DB transaction */ interface Mutation { /** Unique key for the item */ key: string; /** Modified item data (for insert) */ modified?: T; /** Original item data (for update/delete) */ original?: T; /** Changed fields (for update) */ changes?: Partial; } /** * Mutation context from TanStack DB */ interface MutationContext { /** Transaction containing mutations */ transaction: { /** Array of mutations in this transaction */ mutations: Array>; }; /** Collection reference */ collection: unknown; } /** * Options returned by CollectionOptions for TanStack DB */ interface DotdoCollectionOptionsResult { /** Unique identifier for this collection */ id: string; /** Zod schema for validation */ schema: z.ZodSchema; /** Function to extract key from item */ getKey: (item: T) => string; /** Subscribe to collection changes */ subscribe: (callbacks: CollectionCallbacks) => () => void; /** Handle insert mutations */ onInsert: (ctx: MutationContext) => Promise<{ txid: number; }>; /** Handle update mutations */ onUpdate: (ctx: MutationContext) => Promise<{ txid: number; }>; /** Handle delete mutations */ onDelete: (ctx: MutationContext) => Promise<{ txid: number; }>; } /** * Create collection options for TanStack DB with dotdo sync. * * Provides full integration with dotdo backends: * - WebSocket sync for real-time updates via SyncClient * - Cap'n Web RPC for mutations with optimistic updates * - Automatic reconnection and error handling * * @param config - Configuration for the collection * @returns Collection options compatible with TanStack DB * * @example * ```tsx * import { CollectionOptions } from '@dotdo/react/tanstack' * import { z } from 'zod' * * const TaskSchema = z.object({ * $id: z.string(), * title: z.string(), * status: z.enum(['todo', 'done']), * }) * * const taskOptions = CollectionOptions({ * doUrl: 'wss://api.example.com.ai/do/workspace', * collection: 'Task', * schema: TaskSchema, * }) * ``` */ declare function CollectionOptions(config: DotdoCollectionConfig): DotdoCollectionOptionsResult; export { type CollectionCallbacks, type DotdoCollectionConfig as CollectionConfig, CollectionOptions, type DotdoCollectionOptionsResult as CollectionOptionsResult, type Mutation, type MutationContext };