Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

minusWherePredicates.md

73 lines2.0 KB

1---
2id: minusWherePredicates
3title: minusWherePredicates
4---
5​
6# Function: minusWherePredicates()
7​
8```ts
9function minusWherePredicates(fromPredicate, subtractPredicate):
10 | BasicExpression<boolean>
11 | null;
12```
13​
14Defined in: [packages/db/src/query/predicate-utils.ts:338](https://github.com/TanStack/db/blob/main/packages/db/src/query/predicate-utils.ts#L338)
15​
16Compute the difference between two where predicates: `fromPredicate AND NOT(subtractPredicate)`.
17Returns the simplified predicate, or null if the difference cannot be simplified
18(in which case the caller should fetch the full fromPredicate).
19​
20## Parameters
21​
22### fromPredicate
23​
24The predicate to subtract from
25​
26[`BasicExpression`](../../@tanstack/namespaces/IR/type-aliases/BasicExpression.md)\<`boolean`\> | `undefined`
27​
28### subtractPredicate
29​
30The predicate to subtract
31​
32[`BasicExpression`](../../@tanstack/namespaces/IR/type-aliases/BasicExpression.md)\<`boolean`\> | `undefined`
33​
34## Returns
35​
36 \| [`BasicExpression`](../../@tanstack/namespaces/IR/type-aliases/BasicExpression.md)\<`boolean`\>
37 \| `null`
38​
39The simplified difference, or null if cannot be simplified
40​
41## Examples
42​
43```ts
44// Range difference
45minusWherePredicates(
46 gt(ref('age'), val(10)), // age > 10
47 gt(ref('age'), val(20)) // age > 20
48) // → age > 10 AND age <= 20
49```
50​
51```ts
52// Set difference
53minusWherePredicates(
54 inOp(ref('status'), ['A', 'B', 'C', 'D']), // status IN ['A','B','C','D']
55 inOp(ref('status'), ['B', 'C']) // status IN ['B','C']
56) // → status IN ['A', 'D']
57```
58​
59```ts
60// Common conditions
61minusWherePredicates(
62 and(gt(ref('age'), val(10)), eq(ref('status'), val('active'))), // age > 10 AND status = 'active'
63 and(gt(ref('age'), val(20)), eq(ref('status'), val('active'))) // age > 20 AND status = 'active'
64) // → age > 10 AND age <= 20 AND status = 'active'
65```
66​
67```ts
68// Complete overlap - empty result
69minusWherePredicates(
70 gt(ref('age'), val(20)), // age > 20
71 gt(ref('age'), val(10)) // age > 10
72) // → {type: 'val', value: false} (empty set)
73```