@tanstack/db
Version:
A reactive client store for building super fast apps on sync
125 lines (124 loc) • 4.52 kB
text/typescript
import { InitialQueryBuilder, QueryBuilder } from './builder/index.js';
import { Collection } from '../collection.js';
import { CollectionConfig, UtilsRecord } from '../types.js';
import { Context, GetResult } from './builder/types.js';
/**
* Configuration interface for live query collection options
*
* @example
* ```typescript
* const config: LiveQueryCollectionConfig<any, any> = {
* // id is optional - will auto-generate "live-query-1", "live-query-2", etc.
* query: (q) => q
* .from({ comment: commentsCollection })
* .join(
* { user: usersCollection },
* ({ comment, user }) => eq(comment.user_id, user.id)
* )
* .where(({ comment }) => eq(comment.active, true))
* .select(({ comment, user }) => ({
* id: comment.id,
* content: comment.content,
* authorName: user.name,
* })),
* // getKey is optional - defaults to using stream key
* getKey: (item) => item.id,
* }
* ```
*/
export interface LiveQueryCollectionConfig<TContext extends Context, TResult extends object = GetResult<TContext> & object> {
/**
* Unique identifier for the collection
* If not provided, defaults to `live-query-${number}` with auto-incrementing number
*/
id?: string;
/**
* Query builder function that defines the live query
*/
query: ((q: InitialQueryBuilder) => QueryBuilder<TContext>) | QueryBuilder<TContext>;
/**
* Function to extract the key from result items
* If not provided, defaults to using the key from the D2 stream
*/
getKey?: (item: TResult) => string | number;
/**
* Optional schema for validation
*/
schema?: CollectionConfig<TResult>[`schema`];
/**
* Optional mutation handlers
*/
onInsert?: CollectionConfig<TResult>[`onInsert`];
onUpdate?: CollectionConfig<TResult>[`onUpdate`];
onDelete?: CollectionConfig<TResult>[`onDelete`];
/**
* Start sync / the query immediately
*/
startSync?: boolean;
/**
* GC time for the collection
*/
gcTime?: number;
}
/**
* Creates live query collection options for use with createCollection
*
* @example
* ```typescript
* const options = liveQueryCollectionOptions({
* // id is optional - will auto-generate if not provided
* query: (q) => q
* .from({ post: postsCollection })
* .where(({ post }) => eq(post.published, true))
* .select(({ post }) => ({
* id: post.id,
* title: post.title,
* content: post.content,
* })),
* // getKey is optional - will use stream key if not provided
* })
*
* const collection = createCollection(options)
* ```
*
* @param config - Configuration options for the live query collection
* @returns Collection options that can be passed to createCollection
*/
export declare function liveQueryCollectionOptions<TContext extends Context, TResult extends object = GetResult<TContext>>(config: LiveQueryCollectionConfig<TContext, TResult>): CollectionConfig<TResult>;
/**
* Creates a live query collection directly
*
* @example
* ```typescript
* // Minimal usage - just pass a query function
* const activeUsers = createLiveQueryCollection(
* (q) => q
* .from({ user: usersCollection })
* .where(({ user }) => eq(user.active, true))
* .select(({ user }) => ({ id: user.id, name: user.name }))
* )
*
* // Full configuration with custom options
* const searchResults = createLiveQueryCollection({
* id: "search-results", // Custom ID (auto-generated if omitted)
* query: (q) => q
* .from({ post: postsCollection })
* .where(({ post }) => like(post.title, `%${searchTerm}%`))
* .select(({ post }) => ({
* id: post.id,
* title: post.title,
* excerpt: post.excerpt,
* })),
* getKey: (item) => item.id, // Custom key function (uses stream key if omitted)
* utils: {
* updateSearchTerm: (newTerm: string) => {
* // Custom utility functions
* }
* }
* })
* ```
*/
export declare function createLiveQueryCollection<TContext extends Context, TResult extends object = GetResult<TContext>>(query: (q: InitialQueryBuilder) => QueryBuilder<TContext>): Collection<TResult, string | number, {}>;
export declare function createLiveQueryCollection<TContext extends Context, TResult extends object = GetResult<TContext>, TUtils extends UtilsRecord = {}>(config: LiveQueryCollectionConfig<TContext, TResult> & {
utils?: TUtils;
}): Collection<TResult, string | number, TUtils>;