@tanstack/db
Version:
A reactive client store for building super fast apps on sync
62 lines (61 loc) • 3.33 kB
TypeScript
import { ChangeMessage, CollectionLike, CurrentStateAsChangesOptions, SubscribeChangesOptions } from '../types.js';
import { BasicExpression } from '../query/ir.js';
import { WithVirtualProps } from '../virtual-props.js';
/**
* Yields visible entries, enriched with virtual properties, whose stored row
* passes `prefilter`.
*/
export type StoredRowScan<T extends object, TKey extends string | number> = (prefilter: (row: object) => boolean) => Iterable<[TKey, WithVirtualProps<T, TKey>]>;
/**
* A test on a stored row that is false only when `expression` must be false
* on the row's enriched copy, so a scan can skip enriching rows that fail.
* It reads one `eq(field, literal)` conjunct. The copy holds each enumerable
* own root field of the stored row and lacks the others, so its field is the
* stored value or `undefined`, which matches no literal.
*/
export declare function compileStoredRowPrefilter(expression: BasicExpression<boolean>): ((row: object) => boolean) | undefined;
/**
* Returns the current state of the collection as an array of changes
* @param collection - The collection to get changes from
* @param options - Options including optional where filter, orderBy, and limit
* @returns An array of changes
* @example
* // Get all items as changes
* const allChanges = currentStateAsChanges(collection)
*
* // Get only items matching a condition
* const activeChanges = currentStateAsChanges(collection, {
* where: (row) => row.status === 'active'
* })
*
* // Get only items using a pre-compiled expression
* const activeChanges = currentStateAsChanges(collection, {
* where: eq(row.status, 'active')
* })
*
* // Get items ordered by name with limit
* const topUsers = currentStateAsChanges(collection, {
* orderBy: [{ expression: row.name, compareOptions: { direction: 'asc' } }],
* limit: 10
* })
*
* // Get active users ordered by score (highest score first)
* const topActiveUsers = currentStateAsChanges(collection, {
* where: eq(row.status, 'active'),
* orderBy: [{ expression: row.score, compareOptions: { direction: 'desc' } }],
* })
*/
export declare function currentStateAsChanges<T extends object, TKey extends string | number>(collection: CollectionLike<WithVirtualProps<T, TKey>, TKey>, options?: CurrentStateAsChangesOptions, scanStoredRows?: StoredRowScan<T, TKey>): Array<ChangeMessage<WithVirtualProps<T, TKey>, TKey>> | void;
/**
* Creates a filter function from a pre-compiled expression
* @param expression - The pre-compiled expression to evaluate
* @returns A function that takes an item and returns true if it matches the filter
*/
export declare function createFilterFunctionFromExpression<T extends object>(expression: BasicExpression<boolean>): (item: T) => boolean;
/**
* Creates a filtered callback that only calls the original callback with changes that match the where clause
* @param originalCallback - The original callback to filter
* @param options - The subscription options containing the where clause
* @returns A filtered callback function
*/
export declare function createFilteredCallback<T extends object, TKey extends string | number = string | number>(originalCallback: (changes: Array<ChangeMessage<T>>) => void, options: SubscribeChangesOptions<T, TKey>): (changes: Array<ChangeMessage<T>>) => boolean;