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

Method where

packages/db/src/query/builder/index.ts:449–474  ·  view source on GitHub ↗

* Filter rows based on a condition * * @param callback - A function that receives table references and returns an expression * @returns A QueryBuilder with the where condition applied * * @example * ```ts * // Simple condition * query * .from({ users: usersCollection })

(callback: WhereCallback<TContext>)

Source from the content-addressed store, hash-verified

447 * ```
448 */
449 where(callback: WhereCallback<TContext>): QueryBuilder<TContext> {
450 const aliases = this._getCurrentAliases()
451 const refProxy = createRefProxy(aliases) as RefsForContext<TContext>
452 const rawExpression = callback(refProxy)
453
454 // Allow bare boolean column references like `.where(({ u }) => u.active)`
455 // by converting ref proxies to PropRef expressions, the same way helper
456 // functions like `not()` and `eq()` do via `toExpression()`.
457 const expression = isRefProxy(rawExpression)
458 ? toExpression(rawExpression)
459 : rawExpression
460
461 // Validate that the callback returned a valid expression
462 // This catches common mistakes like using JavaScript comparison operators (===, !==, etc.)
463 // which return boolean primitives instead of expression objects
464 if (!isExpressionLike(expression)) {
465 throw new InvalidWhereExpressionError(getValueTypeName(expression))
466 }
467
468 const existingWhere = this.query.where || []
469
470 return new BaseQueryBuilder({
471 ...this.query,
472 where: [...existingWhere, expression],
473 }) as any
474 }
475
476 /**
477 * Filter grouped rows based on aggregate conditions

Calls 6

_getCurrentAliasesMethod · 0.95
createRefProxyFunction · 0.85
isRefProxyFunction · 0.85
toExpressionFunction · 0.85
isExpressionLikeFunction · 0.85
getValueTypeNameFunction · 0.85

Tested by 15

createBasicTestsFunction · 0.64
testJoinTypeFunction · 0.64
createJoinTestsFunction · 0.64
createSubqueryTestsFunction · 0.64
createJoinSubqueryTestsFunction · 0.64
createDistinctTestsFunction · 0.64
buildIncludesQueryFunction · 0.64
buildNestedQueryFunction · 0.64
buildToArrayQueryFunction · 0.64
buildToArrayToArrayQueryFunction · 0.64