@tanstack/db
Version:
A reactive client store for building super fast apps on sync
58 lines (57 loc) • 1.96 kB
text/typescript
import { InitialQueryBuilder, QueryBuilder } from './builder/index.js';
import { Context, InferResultType } from './builder/types.js';
/**
* Configuration options for queryOnce
*/
export interface QueryOnceConfig<TContext extends Context> {
/**
* Query builder function that defines the query
*/
query: ((q: InitialQueryBuilder) => QueryBuilder<TContext>) | QueryBuilder<TContext>;
}
/**
* Executes a one-shot query and returns the results as an array.
*
* This function creates a live query collection, preloads it, extracts the results,
* and automatically cleans up the collection. It's ideal for:
* - AI/LLM context building
* - Data export
* - Background processing
* - Testing
*
* @param queryFn - A function that receives the query builder and returns a query
* @returns A promise that resolves to an array of query results
*
* @example
* ```typescript
* // Basic query
* const users = await queryOnce((q) =>
* q.from({ user: usersCollection })
* )
*
* // With filtering and projection
* const activeUserNames = await queryOnce((q) =>
* q.from({ user: usersCollection })
* .where(({ user }) => eq(user.active, true))
* .select(({ user }) => ({ name: user.name }))
* )
* ```
*/
export declare function queryOnce<TContext extends Context>(queryFn: (q: InitialQueryBuilder) => QueryBuilder<TContext>): Promise<InferResultType<TContext>>;
/**
* Executes a one-shot query using a configuration object.
*
* @param config - Configuration object with the query function
* @returns A promise that resolves to an array of query results
*
* @example
* ```typescript
* const recentOrders = await queryOnce({
* query: (q) =>
* q.from({ order: ordersCollection })
* .orderBy(({ order }) => desc(order.createdAt))
* .limit(100),
* })
* ```
*/
export declare function queryOnce<TContext extends Context>(config: QueryOnceConfig<TContext>): Promise<InferResultType<TContext>>;