MCPcopy Create free account
hub / github.com/TanStack/db / parseWhereExpression

Function parseWhereExpression

packages/db/src/query/expression-helpers.ts:201–248  ·  view source on GitHub ↗
(
  expr: BasicExpression<boolean> | undefined | null,
  options: ParseWhereOptions<T>,
)

Source from the content-addressed store, hash-verified

199 * ```
200 */
201export function parseWhereExpression<T = any>(
202 expr: BasicExpression<boolean> | undefined | null,
203 options: ParseWhereOptions<T>,
204): T | null {
205 if (!expr) return null
206
207 const { handlers, onUnknownOperator } = options
208
209 // Handle value expressions
210 if (expr.type === `val`) {
211 return expr.value as unknown as T
212 }
213
214 // Handle property references
215 if (expr.type === `ref`) {
216 return expr.path as unknown as T
217 }
218
219 // Handle function expressions
220 // After checking val and ref, expr must be func
221 const { name, args } = expr
222 const handler = handlers[name]
223
224 if (!handler) {
225 if (onUnknownOperator) {
226 return onUnknownOperator(name, args)
227 }
228 throw new Error(
229 `No handler provided for operator: ${name}. Available handlers: ${Object.keys(handlers).join(`, `)}`,
230 )
231 }
232
233 // Parse arguments recursively
234 const parsedArgs = args.map((arg: BasicExpression) => {
235 // For refs, extract the field path
236 if (arg.type === `ref`) {
237 return arg.path
238 }
239 // For values, extract the value
240 if (arg.type === `val`) {
241 return arg.value
242 }
243 // For nested functions, recurse (after checking ref and val, must be func)
244 return parseWhereExpression(arg, options)
245 })
246
247 return handler(...parsedArgs)
248}
249
250/**
251 * Parses an ORDER BY expression into a simple array of sort specifications.

Callers 1

Calls 3

joinMethod · 0.45
keysMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected