Skip to content

Commit 620ebea

Browse files
authored
export the new isUndefined and isNull query builder functions (#515)
1 parent 7d813be commit 620ebea

File tree

7 files changed

+27
-42
lines changed

7 files changed

+27
-42
lines changed

.changeset/fast-humans-pick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tanstack/db": patch
3+
---
4+
5+
export the new `isUndefined` and `isNull` query builder functions

packages/db/src/query/builder/functions.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,20 +230,10 @@ export function isUndefined(value: ExpressionLike): BasicExpression<boolean> {
230230
return new Func(`isUndefined`, [toExpression(value)])
231231
}
232232

233-
export function isNotUndefined(
234-
value: ExpressionLike
235-
): BasicExpression<boolean> {
236-
return new Func(`isNotUndefined`, [toExpression(value)])
237-
}
238-
239233
export function isNull(value: ExpressionLike): BasicExpression<boolean> {
240234
return new Func(`isNull`, [toExpression(value)])
241235
}
242236

243-
export function isNotNull(value: ExpressionLike): BasicExpression<boolean> {
244-
return new Func(`isNotNull`, [toExpression(value)])
245-
}
246-
247237
export function inArray(
248238
value: ExpressionLike,
249239
array: ExpressionLike

packages/db/src/query/compiler/evaluators.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -345,27 +345,13 @@ function compileFunction(func: Func, isSingleRow: boolean): (data: any) => any {
345345
return value === undefined
346346
}
347347
}
348-
case `isNotUndefined`: {
349-
const arg = compiledArgs[0]!
350-
return (data) => {
351-
const value = arg(data)
352-
return value !== undefined
353-
}
354-
}
355348
case `isNull`: {
356349
const arg = compiledArgs[0]!
357350
return (data) => {
358351
const value = arg(data)
359352
return value === null
360353
}
361354
}
362-
case `isNotNull`: {
363-
const arg = compiledArgs[0]!
364-
return (data) => {
365-
const value = arg(data)
366-
return value !== null
367-
}
368-
}
369355

370356
default:
371357
throw new UnknownFunctionError(func.name)

packages/db/src/query/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export {
2525
inArray,
2626
like,
2727
ilike,
28+
isUndefined,
29+
isNull,
2830
// Functions
2931
upper,
3032
lower,

packages/db/tests/query/group-by.test.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import {
99
eq,
1010
gt,
1111
gte,
12-
isNotUndefined,
12+
isUndefined,
1313
lt,
1414
max,
1515
min,
16+
not,
1617
or,
1718
sum,
1819
} from "../../src/query/builder/functions.js"
@@ -1097,7 +1098,7 @@ function createGroupByTests(autoIndex: `off` | `eager`): void {
10971098
query: (q) =>
10981099
q
10991100
.from({ orders: ordersCollection })
1100-
.where(({ orders }) => isNotUndefined(orders.customer))
1101+
.where(({ orders }) => not(isUndefined(orders.customer)))
11011102
.groupBy(({ orders }) => orders.customer?.tier)
11021103
.select(({ orders }) => ({
11031104
tier: orders.customer?.tier,
@@ -1127,7 +1128,7 @@ function createGroupByTests(autoIndex: `off` | `eager`): void {
11271128
query: (q) =>
11281129
q
11291130
.from({ orders: ordersCollection })
1130-
.where(({ orders }) => isNotUndefined(orders.customer?.address))
1131+
.where(({ orders }) => not(isUndefined(orders.customer?.address)))
11311132
.groupBy(({ orders }) => orders.customer?.address.state)
11321133
.select(({ orders }) => ({
11331134
state: orders.customer?.address.state,
@@ -1156,7 +1157,7 @@ function createGroupByTests(autoIndex: `off` | `eager`): void {
11561157
query: (q) =>
11571158
q
11581159
.from({ orders: ordersCollection })
1159-
.where(({ orders }) => isNotUndefined(orders.shipping))
1160+
.where(({ orders }) => not(isUndefined(orders.shipping)))
11601161
.groupBy(({ orders }) => orders.shipping?.method)
11611162
.select(({ orders }) => ({
11621163
method: orders.shipping?.method,
@@ -1191,8 +1192,8 @@ function createGroupByTests(autoIndex: `off` | `eager`): void {
11911192
.from({ orders: ordersCollection })
11921193
.where(({ orders }) =>
11931194
and(
1194-
isNotUndefined(orders.customer),
1195-
isNotUndefined(orders.shipping)
1195+
not(isUndefined(orders.customer)),
1196+
not(isUndefined(orders.shipping))
11961197
)
11971198
)
11981199
.groupBy(({ orders }) => orders.customer?.tier)
@@ -1231,7 +1232,7 @@ function createGroupByTests(autoIndex: `off` | `eager`): void {
12311232
q
12321233
.from({ orders: ordersCollection })
12331234
.where(({ orders }) =>
1234-
isNotUndefined(orders.customer?.preferences)
1235+
not(isUndefined(orders.customer?.preferences))
12351236
)
12361237
.groupBy(({ orders }) => orders.customer?.preferences.newsletter)
12371238
.select(({ orders }) => ({
@@ -1267,10 +1268,10 @@ function createGroupByTests(autoIndex: `off` | `eager`): void {
12671268
q
12681269
.from({ orders: ordersCollection })
12691270
.groupBy(({ orders }) =>
1270-
isNotUndefined(orders.shipping?.tracking)
1271+
not(isUndefined(orders.shipping?.tracking))
12711272
)
12721273
.select(({ orders }) => ({
1273-
tracking_status: isNotUndefined(orders.shipping?.tracking),
1274+
tracking_status: not(isUndefined(orders.shipping?.tracking)),
12741275
order_count: count(orders.id),
12751276
total_amount: sum(orders.amount),
12761277
})),
@@ -1295,7 +1296,7 @@ function createGroupByTests(autoIndex: `off` | `eager`): void {
12951296
query: (q) =>
12961297
q
12971298
.from({ orders: ordersCollection })
1298-
.where(({ orders }) => isNotUndefined(orders.customer))
1299+
.where(({ orders }) => not(isUndefined(orders.customer)))
12991300
.groupBy(({ orders }) => orders.customer?.tier)
13001301
.select(({ orders }) => ({
13011302
tier: orders.customer?.tier,

packages/db/tests/query/order-by.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import { createLiveQueryCollection } from "../../src/query/live-query-collection
55
import {
66
eq,
77
gt,
8-
isNotUndefined,
8+
isUndefined,
99
max,
10+
not,
1011
} from "../../src/query/builder/functions.js"
1112

1213
type Person = {
@@ -1709,7 +1710,7 @@ function createOrderByTests(autoIndex: `off` | `eager`): void {
17091710
const collection = createLiveQueryCollection((q) =>
17101711
q
17111712
.from({ persons: personsCollection })
1712-
.where(({ persons }) => isNotUndefined(persons.address))
1713+
.where(({ persons }) => not(isUndefined(persons.address)))
17131714
.orderBy(({ persons }) => persons.address?.coordinates.lat, `asc`)
17141715
.select(({ persons }) => ({
17151716
id: persons.id,

packages/db/tests/query/where.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import {
1111
gt,
1212
gte,
1313
inArray,
14-
isNotNull,
15-
isNotUndefined,
14+
isNull,
15+
isUndefined,
1616
length,
1717
like,
1818
lower,
@@ -1440,7 +1440,7 @@ function createWhereTests(autoIndex: `off` | `eager`): void {
14401440
.select(({ emp }) => ({
14411441
id: emp.id,
14421442
name: emp.name,
1443-
hasAddress: isNotNull(emp.contact?.address),
1443+
hasAddress: not(isNull(emp.contact?.address)),
14441444
})),
14451445
})
14461446

@@ -1455,7 +1455,7 @@ function createWhereTests(autoIndex: `off` | `eager`): void {
14551455
query: (q) =>
14561456
q
14571457
.from({ emp: employeesCollection })
1458-
.where(({ emp }) => isNotUndefined(emp.profile))
1458+
.where(({ emp }) => not(isUndefined(emp.profile)))
14591459
.select(({ emp }) => ({
14601460
id: emp.id,
14611461
name: emp.name,
@@ -1528,7 +1528,7 @@ function createWhereTests(autoIndex: `off` | `eager`): void {
15281528
query: (q) =>
15291529
q
15301530
.from({ emp: employeesCollection })
1531-
.where(({ emp }) => isNotUndefined(emp.contact?.emergency))
1531+
.where(({ emp }) => not(isUndefined(emp.contact?.emergency)))
15321532
.select(({ emp }) => ({
15331533
id: emp.id,
15341534
name: emp.name,
@@ -1617,7 +1617,7 @@ function createWhereTests(autoIndex: `off` | `eager`): void {
16171617
query: (q) =>
16181618
q
16191619
.from({ emp: employeesCollection })
1620-
.where(({ emp }) => isNotUndefined(emp.profile?.skills))
1620+
.where(({ emp }) => not(isUndefined(emp.profile?.skills)))
16211621
.select(({ emp }) => ({
16221622
id: emp.id,
16231623
name: emp.name,

0 commit comments

Comments
 (0)