Open History to see the last commit
1---
2id: parseWhereExpression
3title: parseWhereExpression
4---
5
6# Function: parseWhereExpression()
7
8```ts
9function parseWhereExpression<T>(expr, options): T | null;
10```
11
12Defined in: [packages/db/src/query/expression-helpers.ts:201](https://github.com/TanStack/db/blob/main/packages/db/src/query/expression-helpers.ts#L201)
13
14Parses a WHERE expression into a custom format using provided handlers.
15
16This is the main helper for converting TanStack DB where clauses into your API's filter format.
17You provide handlers for each operator, and this function traverses the expression tree
18and calls the appropriate handlers.
19
20## Type Parameters
21
22### T
23
24`T` = `any`
25
26## Parameters
27
28### expr
29
30The WHERE expression to parse
31
32`BasicExpression`\<`boolean`\> | `null` | `undefined`
33
34### options
35
36[`ParseWhereOptions`](../interfaces/ParseWhereOptions.md)\<`T`\>
37
38Configuration with handler functions for each operator
39
40## Returns
41
42`T` \| `null`
43
44The parsed result in your custom format
45
46## Examples
47
48```typescript
49// REST API with query parameters
50const filters = parseWhereExpression(where, {
51 handlers: {
52 eq: (field, value) => ({ [field.join('.')]: value }),
53 lt: (field, value) => ({ [`${field.join('.')}_lt`]: value }),
54 gt: (field, value) => ({ [`${field.join('.')}_gt`]: value }),
55 and: (...filters) => Object.assign({}, ...filters),
56 or: (...filters) => ({ $or: filters })
57 }
58})
59// Returns: { category: 'electronics', price_lt: 100 }
60```
61
62```typescript
63// GraphQL where clause
64const where = parseWhereExpression(whereExpr, {
65 handlers: {
66 eq: (field, value) => ({ [field.join('_')]: { _eq: value } }),
67 lt: (field, value) => ({ [field.join('_')]: { _lt: value } }),
68 and: (...filters) => ({ _and: filters })
69 }
70})
71```