@tanstack/db-collections
Version:
A collection for (aspirationally) every way of loading your data
54 lines (53 loc) • 2.66 kB
TypeScript
import { QueryClient, QueryFunctionContext, QueryKey, QueryObserverOptions } from '@tanstack/query-core';
import { CollectionConfig, UtilsRecord } from '@tanstack/db';
export interface QueryCollectionConfig<TItem extends object, TError = unknown, TQueryKey extends QueryKey = QueryKey> {
queryKey: TQueryKey;
queryFn: (context: QueryFunctionContext<TQueryKey>) => Promise<Array<TItem>>;
queryClient: QueryClient;
enabled?: boolean;
refetchInterval?: QueryObserverOptions<Array<TItem>, TError, Array<TItem>, Array<TItem>, TQueryKey>[`refetchInterval`];
retry?: QueryObserverOptions<Array<TItem>, TError, Array<TItem>, Array<TItem>, TQueryKey>[`retry`];
retryDelay?: QueryObserverOptions<Array<TItem>, TError, Array<TItem>, Array<TItem>, TQueryKey>[`retryDelay`];
staleTime?: QueryObserverOptions<Array<TItem>, TError, Array<TItem>, Array<TItem>, TQueryKey>[`staleTime`];
id?: string;
getKey: CollectionConfig<TItem>[`getKey`];
schema?: CollectionConfig<TItem>[`schema`];
sync?: CollectionConfig<TItem>[`sync`];
/**
* Optional asynchronous handler function called before an insert operation
* @param params Object containing transaction and mutation information
* @returns Promise resolving to void or { refetch?: boolean } to control refetching
*/
onInsert?: CollectionConfig<TItem>[`onInsert`];
/**
* Optional asynchronous handler function called before an update operation
* @param params Object containing transaction and mutation information
* @returns Promise resolving to void or { refetch?: boolean } to control refetching
*/
onUpdate?: CollectionConfig<TItem>[`onUpdate`];
/**
* Optional asynchronous handler function called before a delete operation
* @param params Object containing transaction and mutation information
* @returns Promise resolving to void or { refetch?: boolean } to control refetching
*/
onDelete?: CollectionConfig<TItem>[`onDelete`];
}
/**
* Type for the refetch utility function
*/
export type RefetchFn = () => Promise<void>;
/**
* Query collection utilities type
*/
export interface QueryCollectionUtils extends UtilsRecord {
refetch: RefetchFn;
}
/**
* Creates query collection options for use with a standard Collection
*
* @param config - Configuration options for the Query collection
* @returns Collection options with utilities
*/
export declare function queryCollectionOptions<TItem extends object, TError = unknown, TQueryKey extends QueryKey = QueryKey>(config: QueryCollectionConfig<TItem, TError, TQueryKey>): CollectionConfig<TItem> & {
utils: QueryCollectionUtils;
};