Skip to content

Commit ae11e1d

Browse files
committed
checkpoint
1 parent 7a077a8 commit ae11e1d

15 files changed

+346
-349
lines changed

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

Lines changed: 86 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,60 +8,68 @@ import type { Ref } from "./types.js"
88
type ExpressionLike = BasicExpression | RefProxy<any> | Ref<any> | any
99

1010
// Helper type to extract the underlying type from various expression types
11-
type ExtractType<T> =
12-
T extends RefProxy<infer U> ? U
13-
: T extends Ref<infer U> ? U
14-
: T extends BasicExpression<infer U> ? U
15-
: T extends undefined ? undefined
16-
: T extends null ? null
17-
: T
11+
type ExtractType<T> =
12+
T extends RefProxy<infer U>
13+
? U
14+
: T extends Ref<infer U>
15+
? U
16+
: T extends BasicExpression<infer U>
17+
? U
18+
: T extends undefined
19+
? undefined
20+
: T extends null
21+
? null
22+
: T
1823

1924
// Helper type to determine aggregate return type based on input nullability
20-
type AggregateReturnType<T> = ExtractType<T> extends infer U
21-
? U extends number | undefined | null
22-
? Aggregate<U>
23-
: U extends number
24-
? Aggregate<number>
25-
: Aggregate<number | undefined | null>
26-
: Aggregate<number | undefined | null>
25+
type AggregateReturnType<T> =
26+
ExtractType<T> extends infer U
27+
? U extends number | undefined | null
28+
? Aggregate<U>
29+
: U extends number
30+
? Aggregate<number>
31+
: Aggregate<number | undefined | null>
32+
: Aggregate<number | undefined | null>
2733

2834
// Helper type to determine string function return type based on input nullability
29-
type StringFunctionReturnType<T> = ExtractType<T> extends infer U
30-
? U extends string | undefined | null
31-
? BasicExpression<U>
32-
: U extends string
33-
? BasicExpression<string>
34-
: BasicExpression<string | undefined | null>
35-
: BasicExpression<string | undefined | null>
36-
37-
// Helper type to determine numeric function return type based on input nullability
38-
// This handles string, array, and number inputs for functions like length()
39-
type NumericFunctionReturnType<T> = ExtractType<T> extends infer U
40-
? U extends string
41-
? BasicExpression<number>
42-
: U extends string | undefined
43-
? BasicExpression<number | undefined>
44-
: U extends string | null
45-
? BasicExpression<number | null>
46-
: U extends string | undefined | null
47-
? BasicExpression<number | undefined | null>
48-
: U extends Array<any>
49-
? BasicExpression<number>
50-
: U extends Array<any> | undefined
51-
? BasicExpression<number | undefined>
52-
: U extends Array<any> | null
53-
? BasicExpression<number | null>
54-
: U extends Array<any> | undefined | null
55-
? BasicExpression<number | undefined | null>
56-
: U extends number | undefined | null
35+
type StringFunctionReturnType<T> =
36+
ExtractType<T> extends infer U
37+
? U extends string | undefined | null
5738
? BasicExpression<U>
58-
: U extends number
39+
: U extends string
40+
? BasicExpression<string>
41+
: BasicExpression<string | undefined | null>
42+
: BasicExpression<string | undefined | null>
43+
44+
// Helper type to determine numeric function return type based on input nullability
45+
// This handles string, array, and number inputs for functions like length()
46+
type NumericFunctionReturnType<T> =
47+
ExtractType<T> extends infer U
48+
? U extends string
5949
? BasicExpression<number>
60-
: BasicExpression<number | undefined | null>
61-
: BasicExpression<number | undefined | null>
50+
: U extends string | undefined
51+
? BasicExpression<number | undefined>
52+
: U extends string | null
53+
? BasicExpression<number | null>
54+
: U extends string | undefined | null
55+
? BasicExpression<number | undefined | null>
56+
: U extends Array<any>
57+
? BasicExpression<number>
58+
: U extends Array<any> | undefined
59+
? BasicExpression<number | undefined>
60+
: U extends Array<any> | null
61+
? BasicExpression<number | null>
62+
: U extends Array<any> | undefined | null
63+
? BasicExpression<number | undefined | null>
64+
: U extends number | undefined | null
65+
? BasicExpression<U>
66+
: U extends number
67+
? BasicExpression<number>
68+
: BasicExpression<number | undefined | null>
69+
: BasicExpression<number | undefined | null>
6270

6371
// Helper type for binary numeric operations (combines nullability of both operands)
64-
type BinaryNumericReturnType<T1, T2> =
72+
type BinaryNumericReturnType<T1, T2> =
6573
ExtractType<T1> extends infer U1
6674
? ExtractType<T2> extends infer U2
6775
? U1 extends number
@@ -206,7 +214,9 @@ export function isUndefined(value: ExpressionLike): BasicExpression<boolean> {
206214
return new Func(`isUndefined`, [toExpression(value)])
207215
}
208216

209-
export function isNotUndefined(value: ExpressionLike): BasicExpression<boolean> {
217+
export function isNotUndefined(
218+
value: ExpressionLike
219+
): BasicExpression<boolean> {
210220
return new Func(`isNotUndefined`, [toExpression(value)])
211221
}
212222

@@ -231,17 +241,21 @@ export function like(
231241
| RefProxy<string | null>
232242
| RefProxy<string | undefined>
233243
| string
234-
| string | null
235-
| string | undefined
244+
| string
245+
| null
246+
| string
247+
| undefined
236248
| BasicExpression<string>
237249
| BasicExpression<string | null>
238250
| BasicExpression<string | undefined>
239251
| null
240252
| undefined,
241253
right:
242254
| string
243-
| string | null
244-
| string | undefined
255+
| string
256+
| null
257+
| string
258+
| undefined
245259
| RefProxy<string>
246260
| RefProxy<string | null>
247261
| RefProxy<string | undefined>
@@ -261,17 +275,21 @@ export function ilike(
261275
| RefProxy<string | null>
262276
| RefProxy<string | undefined>
263277
| string
264-
| string | null
265-
| string | undefined
278+
| string
279+
| null
280+
| string
281+
| undefined
266282
| BasicExpression<string>
267283
| BasicExpression<string | null>
268284
| BasicExpression<string | undefined>
269285
| null
270286
| undefined,
271287
right:
272288
| string
273-
| string | null
274-
| string | undefined
289+
| string
290+
| null
291+
| string
292+
| undefined
275293
| RefProxy<string>
276294
| RefProxy<string | null>
277295
| RefProxy<string | undefined>
@@ -286,15 +304,21 @@ export function ilike(
286304

287305
// Functions
288306

289-
export function upper<T extends ExpressionLike>(arg: T): StringFunctionReturnType<T> {
307+
export function upper<T extends ExpressionLike>(
308+
arg: T
309+
): StringFunctionReturnType<T> {
290310
return new Func(`upper`, [toExpression(arg)]) as StringFunctionReturnType<T>
291311
}
292312

293-
export function lower<T extends ExpressionLike>(arg: T): StringFunctionReturnType<T> {
313+
export function lower<T extends ExpressionLike>(
314+
arg: T
315+
): StringFunctionReturnType<T> {
294316
return new Func(`lower`, [toExpression(arg)]) as StringFunctionReturnType<T>
295317
}
296318

297-
export function length<T extends ExpressionLike>(arg: T): NumericFunctionReturnType<T> {
319+
export function length<T extends ExpressionLike>(
320+
arg: T
321+
): NumericFunctionReturnType<T> {
298322
return new Func(`length`, [toExpression(arg)]) as NumericFunctionReturnType<T>
299323
}
300324

@@ -318,7 +342,10 @@ export function add<T1 extends ExpressionLike, T2 extends ExpressionLike>(
318342
left: T1,
319343
right: T2
320344
): BinaryNumericReturnType<T1, T2> {
321-
return new Func(`add`, [toExpression(left), toExpression(right)]) as BinaryNumericReturnType<T1, T2>
345+
return new Func(`add`, [
346+
toExpression(left),
347+
toExpression(right),
348+
]) as BinaryNumericReturnType<T1, T2>
322349
}
323350

324351
// Aggregates

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,10 +556,11 @@ export class BaseQueryBuilder<TContext extends Context = Context> {
556556
? result.map((r) => toExpression(r))
557557
: [toExpression(result)]
558558

559-
// Replace existing groupBy expressions instead of extending them
559+
// Extend existing groupBy expressions (multiple groupBy calls should accumulate)
560+
const existingGroupBy = this.query.groupBy || []
560561
return new BaseQueryBuilder({
561562
...this.query,
562-
groupBy: newExpressions,
563+
groupBy: [...existingGroupBy, ...newExpressions],
563564
}) as any
564565
}
565566

0 commit comments

Comments
 (0)