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

Function toExpression

packages/db/src/query/builder/ref-proxy.ts:294–334  ·  view source on GitHub ↗
(value: any)

Source from the content-addressed store, hash-verified

292export function toExpression<T = any>(value: T): BasicExpression<T>
293export function toExpression(value: RefProxy<any>): BasicExpression<any>
294export function toExpression(value: any): BasicExpression<any> {
295 if (isRefProxy(value)) {
296 return new PropRef(value.__path)
297 }
298 // toArray(), concat(toArray()), and materialize() must be used as direct
299 // select fields, not inside expressions
300 if (
301 value &&
302 typeof value === `object` &&
303 (value.__brand === `ToArrayWrapper` ||
304 value.__brand === `ConcatToArrayWrapper` ||
305 value.__brand === `CaseWhenWrapper` ||
306 value.__brand === `MaterializeWrapper`)
307 ) {
308 const name =
309 value.__brand === `ToArrayWrapper`
310 ? `toArray()`
311 : value.__brand === `ConcatToArrayWrapper`
312 ? `concat(toArray())`
313 : value.__brand === `CaseWhenWrapper`
314 ? `caseWhen()`
315 : `materialize()`
316 throw new Error(
317 `${name} cannot be used inside expressions (e.g., coalesce(), eq(), not()). ` +
318 `Use ${name} directly as a select field value instead.`,
319 )
320 }
321 // If it's already an Expression (Func, Ref, Value) or Agg, return it directly
322 if (
323 value &&
324 typeof value === `object` &&
325 `type` in value &&
326 (value.type === `func` ||
327 value.type === `ref` ||
328 value.type === `val` ||
329 value.type === `agg`)
330 ) {
331 return value
332 }
333 return new Value(value)
334}
335
336/**
337 * Type guard to check if a value is a RefProxy

Callers 15

createIndexMethod · 0.90
createFilterFunctionFunction · 0.90
subscribeChangesMethod · 0.85
whereMethod · 0.85
havingMethod · 0.85
makeOrderByClauseMethod · 0.85
groupByMethod · 0.85
toExprFunction · 0.85
buildConditionalSelectFunction · 0.85
eqFunction · 0.85
gtFunction · 0.85
gteFunction · 0.85

Calls 1

isRefProxyFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…