parseWhereExpression

Function: parseWhereExpression()

ts
function parseWhereExpression<T>(expr, options): T | null;
function parseWhereExpression<T>(expr, options): T | null;

Defined in: packages/db/src/query/expression-helpers.ts:201

Parses a WHERE expression into a custom format using provided handlers.

This is the main helper for converting TanStack DB where clauses into your API's filter format. You provide handlers for each operator, and this function traverses the expression tree and calls the appropriate handlers.

Type Parameters

T

T = any

Parameters

expr

The WHERE expression to parse

BasicExpression<boolean> | null | undefined

options

ParseWhereOptions<T>

Configuration with handler functions for each operator

Returns

T | null

The parsed result in your custom format

Examples

typescript
// REST API with query parameters
const filters = parseWhereExpression(where, {
  handlers: {
    eq: (field, value) => ({ [field.join('.')]: value }),
    lt: (field, value) => ({ [`${field.join('.')}_lt`]: value }),
    gt: (field, value) => ({ [`${field.join('.')}_gt`]: value }),
    and: (...filters) => Object.assign({}, ...filters),
    or: (...filters) => ({ $or: filters })
  }
})
// Returns: { category: 'electronics', price_lt: 100 }
// REST API with query parameters
const filters = parseWhereExpression(where, {
  handlers: {
    eq: (field, value) => ({ [field.join('.')]: value }),
    lt: (field, value) => ({ [`${field.join('.')}_lt`]: value }),
    gt: (field, value) => ({ [`${field.join('.')}_gt`]: value }),
    and: (...filters) => Object.assign({}, ...filters),
    or: (...filters) => ({ $or: filters })
  }
})
// Returns: { category: 'electronics', price_lt: 100 }
typescript
// GraphQL where clause
const where = parseWhereExpression(whereExpr, {
  handlers: {
    eq: (field, value) => ({ [field.join('_')]: { _eq: value } }),
    lt: (field, value) => ({ [field.join('_')]: { _lt: value } }),
    and: (...filters) => ({ _and: filters })
  }
})
// GraphQL where clause
const where = parseWhereExpression(whereExpr, {
  handlers: {
    eq: (field, value) => ({ [field.join('_')]: { _eq: value } }),
    lt: (field, value) => ({ [field.join('_')]: { _lt: value } }),
    and: (...filters) => ({ _and: filters })
  }
})
Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.

Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.