|
| 1 | +import { createLiveQueryCollection } from './live-query-collection.js' |
| 2 | +import type { InitialQueryBuilder, QueryBuilder } from './builder/index.js' |
| 3 | +import type { Context, InferResultType } from './builder/types.js' |
| 4 | + |
| 5 | +/** |
| 6 | + * Configuration options for queryOnce |
| 7 | + */ |
| 8 | +export interface QueryOnceConfig<TContext extends Context> { |
| 9 | + /** |
| 10 | + * Query builder function that defines the query |
| 11 | + */ |
| 12 | + query: |
| 13 | + | ((q: InitialQueryBuilder) => QueryBuilder<TContext>) |
| 14 | + | QueryBuilder<TContext> |
| 15 | + // Future: timeout, signal, etc. |
| 16 | +} |
| 17 | + |
| 18 | +// Overload 1: Simple query function returning array (non-single result) |
| 19 | +/** |
| 20 | + * Executes a one-shot query and returns the results as an array. |
| 21 | + * |
| 22 | + * This function creates a live query collection, preloads it, extracts the results, |
| 23 | + * and automatically cleans up the collection. It's ideal for: |
| 24 | + * - AI/LLM context building |
| 25 | + * - Data export |
| 26 | + * - Background processing |
| 27 | + * - Testing |
| 28 | + * |
| 29 | + * @param queryFn - A function that receives the query builder and returns a query |
| 30 | + * @returns A promise that resolves to an array of query results |
| 31 | + * |
| 32 | + * @example |
| 33 | + * ```typescript |
| 34 | + * // Basic query |
| 35 | + * const users = await queryOnce((q) => |
| 36 | + * q.from({ user: usersCollection }) |
| 37 | + * ) |
| 38 | + * |
| 39 | + * // With filtering and projection |
| 40 | + * const activeUserNames = await queryOnce((q) => |
| 41 | + * q.from({ user: usersCollection }) |
| 42 | + * .where(({ user }) => eq(user.active, true)) |
| 43 | + * .select(({ user }) => ({ name: user.name })) |
| 44 | + * ) |
| 45 | + * ``` |
| 46 | + */ |
| 47 | +export function queryOnce<TContext extends Context>( |
| 48 | + queryFn: (q: InitialQueryBuilder) => QueryBuilder<TContext>, |
| 49 | +): Promise<InferResultType<TContext>> |
| 50 | + |
| 51 | +// Overload 2: Config object form returning array (non-single result) |
| 52 | +/** |
| 53 | + * Executes a one-shot query using a configuration object. |
| 54 | + * |
| 55 | + * @param config - Configuration object with the query function |
| 56 | + * @returns A promise that resolves to an array of query results |
| 57 | + * |
| 58 | + * @example |
| 59 | + * ```typescript |
| 60 | + * const recentOrders = await queryOnce({ |
| 61 | + * query: (q) => |
| 62 | + * q.from({ order: ordersCollection }) |
| 63 | + * .orderBy(({ order }) => desc(order.createdAt)) |
| 64 | + * .limit(100), |
| 65 | + * }) |
| 66 | + * ``` |
| 67 | + */ |
| 68 | +export function queryOnce<TContext extends Context>( |
| 69 | + config: QueryOnceConfig<TContext>, |
| 70 | +): Promise<InferResultType<TContext>> |
| 71 | + |
| 72 | +// Implementation |
| 73 | +export async function queryOnce<TContext extends Context>( |
| 74 | + configOrQuery: |
| 75 | + | QueryOnceConfig<TContext> |
| 76 | + | ((q: InitialQueryBuilder) => QueryBuilder<TContext>), |
| 77 | +): Promise<InferResultType<TContext>> { |
| 78 | + // Normalize input |
| 79 | + const config: QueryOnceConfig<TContext> = |
| 80 | + typeof configOrQuery === `function` |
| 81 | + ? { query: configOrQuery } |
| 82 | + : configOrQuery |
| 83 | + |
| 84 | + const query = (q: InitialQueryBuilder) => { |
| 85 | + const queryConfig = config.query |
| 86 | + return typeof queryConfig === `function` ? queryConfig(q) : queryConfig |
| 87 | + } |
| 88 | + |
| 89 | + // Create collection with minimal GC time; preload handles sync start |
| 90 | + const collection = createLiveQueryCollection({ |
| 91 | + query, |
| 92 | + gcTime: 1, // Cleanup in next tick when no subscribers (0 disables GC) |
| 93 | + }) |
| 94 | + |
| 95 | + try { |
| 96 | + // Wait for initial data load |
| 97 | + await collection.preload() |
| 98 | + |
| 99 | + // Check if this is a single-result query (findOne was called) |
| 100 | + const isSingleResult = |
| 101 | + (collection.config as { singleResult?: boolean }).singleResult === true |
| 102 | + |
| 103 | + // Extract and return results |
| 104 | + if (isSingleResult) { |
| 105 | + const first = collection.values().next().value as |
| 106 | + | InferResultType<TContext> |
| 107 | + | undefined |
| 108 | + return first as InferResultType<TContext> |
| 109 | + } |
| 110 | + return collection.toArray as InferResultType<TContext> |
| 111 | + } finally { |
| 112 | + // Always cleanup, even on error |
| 113 | + await collection.cleanup() |
| 114 | + } |
| 115 | +} |
0 commit comments