MCPcopy Index your code
hub / github.com/TanStack/db / createFilterFunction

Function createFilterFunction

packages/db/src/collection/change-events.ts:189–220  ·  view source on GitHub ↗
(
  whereCallback: (row: SingleRowRefProxy<T>) => any,
)

Source from the content-addressed store, hash-verified

187 * @returns A function that takes an item and returns true if it matches the filter
188 */
189export function createFilterFunction<T extends object>(
190 whereCallback: (row: SingleRowRefProxy<T>) => any,
191): (item: T) => boolean {
192 return (item: T): boolean => {
193 try {
194 // First try the RefProxy approach for query builder functions
195 const singleRowRefProxy = createSingleRowRefProxy<T>()
196 const whereExpression = whereCallback(singleRowRefProxy)
197 const expression = toExpression(whereExpression)
198 const evaluator = compileSingleRowExpression(expression)
199 const result = evaluator(item as Record<string, unknown>)
200 // WHERE clauses should always evaluate to boolean predicates (Kevin's feedback)
201 return toBooleanPredicate(result)
202 } catch {
203 // If RefProxy approach fails (e.g., arithmetic operations), fall back to direct evaluation
204 try {
205 // Create a simple proxy that returns actual values for arithmetic operations
206 const simpleProxy = new Proxy(item as any, {
207 get(target, prop) {
208 return target[prop]
209 },
210 }) as SingleRowRefProxy<T>
211
212 const result = whereCallback(simpleProxy)
213 return toBooleanPredicate(result)
214 } catch {
215 // If both approaches fail, exclude the item
216 return false
217 }
218 }
219 }
220}
221
222/**
223 * Creates a filter function from a pre-compiled expression

Callers

nothing calls this directly

Calls 4

createSingleRowRefProxyFunction · 0.90
toExpressionFunction · 0.90
toBooleanPredicateFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…