@simplysm/orm-common 14.0.100 → 14.0.102
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/db-context.d.ts +0 -17
- package/dist/db-context.d.ts.map +1 -1
- package/dist/db-context.js +0 -17
- package/dist/db-context.js.map +1 -1
- package/dist/errors/db-transaction-error.d.ts +0 -15
- package/dist/errors/db-transaction-error.d.ts.map +1 -1
- package/dist/errors/db-transaction-error.js +0 -15
- package/dist/errors/db-transaction-error.js.map +1 -1
- package/dist/exec/executable.d.ts +0 -24
- package/dist/exec/executable.d.ts.map +1 -1
- package/dist/exec/executable.js +0 -24
- package/dist/exec/executable.js.map +1 -1
- package/dist/exec/queryable.d.ts +0 -300
- package/dist/exec/queryable.d.ts.map +1 -1
- package/dist/exec/queryable.js +0 -213
- package/dist/exec/queryable.js.map +1 -1
- package/dist/expr/expr.d.ts +0 -689
- package/dist/expr/expr.d.ts.map +1 -1
- package/dist/expr/expr.js +0 -689
- package/dist/expr/expr.js.map +1 -1
- package/dist/schema/factory/column-builder.d.ts +0 -86
- package/dist/schema/factory/column-builder.d.ts.map +1 -1
- package/dist/schema/factory/column-builder.js +0 -74
- package/dist/schema/factory/column-builder.js.map +1 -1
- package/dist/schema/factory/index-builder.d.ts +0 -60
- package/dist/schema/factory/index-builder.d.ts.map +1 -1
- package/dist/schema/factory/index-builder.js +0 -60
- package/dist/schema/factory/index-builder.js.map +1 -1
- package/dist/schema/factory/relation-builder.d.ts +0 -83
- package/dist/schema/factory/relation-builder.d.ts.map +1 -1
- package/dist/schema/factory/relation-builder.js +0 -77
- package/dist/schema/factory/relation-builder.js.map +1 -1
- package/dist/schema/procedure-builder.d.ts +0 -87
- package/dist/schema/procedure-builder.d.ts.map +1 -1
- package/dist/schema/procedure-builder.js +0 -87
- package/dist/schema/procedure-builder.js.map +1 -1
- package/dist/schema/table-builder.d.ts +0 -127
- package/dist/schema/table-builder.d.ts.map +1 -1
- package/dist/schema/table-builder.js +0 -127
- package/dist/schema/table-builder.js.map +1 -1
- package/dist/schema/view-builder.d.ts +0 -76
- package/dist/schema/view-builder.d.ts.map +1 -1
- package/dist/schema/view-builder.js +0 -76
- package/dist/schema/view-builder.js.map +1 -1
- package/dist/types/column.d.ts +0 -38
- package/dist/types/column.d.ts.map +1 -1
- package/dist/types/column.js +0 -13
- package/dist/types/column.js.map +1 -1
- package/dist/types/db.d.ts +0 -54
- package/dist/types/db.d.ts.map +1 -1
- package/dist/types/db.js +0 -8
- package/dist/types/db.js.map +1 -1
- package/dist/utils/result-parser.d.ts +0 -21
- package/dist/utils/result-parser.d.ts.map +1 -1
- package/dist/utils/result-parser.js +0 -24
- package/dist/utils/result-parser.js.map +1 -1
- package/package.json +2 -2
- package/src/db-context.ts +0 -17
- package/src/errors/db-transaction-error.ts +0 -15
- package/src/exec/executable.ts +0 -24
- package/src/exec/queryable.ts +0 -300
- package/src/expr/expr.ts +0 -689
- package/src/schema/factory/column-builder.ts +0 -86
- package/src/schema/factory/index-builder.ts +0 -60
- package/src/schema/factory/relation-builder.ts +0 -83
- package/src/schema/procedure-builder.ts +0 -87
- package/src/schema/table-builder.ts +0 -127
- package/src/schema/view-builder.ts +0 -76
- package/src/types/column.ts +0 -38
- package/src/types/db.ts +0 -54
- package/src/utils/result-parser.ts +0 -24
package/src/expr/expr.ts
CHANGED
|
@@ -34,27 +34,6 @@ export interface SwitchExprBuilder<TPrimitive extends ColumnPrimitive> {
|
|
|
34
34
|
* SQL 문자열 대신 JSON AST(Expr)를 생성하며, QueryBuilder가
|
|
35
35
|
* 각 DBMS(MySQL, MSSQL, PostgreSQL)로 변환
|
|
36
36
|
*
|
|
37
|
-
* @example
|
|
38
|
-
* ```typescript
|
|
39
|
-
* // WHERE condition
|
|
40
|
-
* db.user().where((u) => [
|
|
41
|
-
* expr.eq(u.status, "active"),
|
|
42
|
-
* expr.gt(u.age, 18),
|
|
43
|
-
* ])
|
|
44
|
-
*
|
|
45
|
-
* // SELECT expression
|
|
46
|
-
* db.user().select((u) => ({
|
|
47
|
-
* name: expr.concat(u.firstName, " ", u.lastName),
|
|
48
|
-
* age: expr.dateDiff("year", u.birthDate, expr.val("DateOnly", DateOnly.today())),
|
|
49
|
-
* }))
|
|
50
|
-
*
|
|
51
|
-
* // Aggregate function
|
|
52
|
-
* db.order().groupBy((o) => o.userId).select((o) => ({
|
|
53
|
-
* userId: o.userId,
|
|
54
|
-
* total: expr.sum(o.amount),
|
|
55
|
-
* }))
|
|
56
|
-
* ```
|
|
57
|
-
*
|
|
58
37
|
* @see {@link Queryable} Query builder 클래스
|
|
59
38
|
* @see {@link ExprUnit} Expression 래퍼 클래스
|
|
60
39
|
*/
|
|
@@ -69,21 +48,6 @@ export const expr = {
|
|
|
69
48
|
* @param dataType - 값의 데이터 타입 ("string", "number", "boolean", "DateTime", "DateOnly", "Time", "Uuid", "Buffer")
|
|
70
49
|
* @param value - 래핑할 값 (undefined 허용)
|
|
71
50
|
* @returns 래핑된 ExprUnit 인스턴스
|
|
72
|
-
*
|
|
73
|
-
* @example
|
|
74
|
-
* ```typescript
|
|
75
|
-
* // String value
|
|
76
|
-
* expr.val("string", "active")
|
|
77
|
-
*
|
|
78
|
-
* // Number value
|
|
79
|
-
* expr.val("number", 100)
|
|
80
|
-
*
|
|
81
|
-
* // Date value
|
|
82
|
-
* expr.val("DateOnly", DateOnly.today())
|
|
83
|
-
*
|
|
84
|
-
* // undefined value
|
|
85
|
-
* expr.val("string", undefined)
|
|
86
|
-
* ```
|
|
87
51
|
*/
|
|
88
52
|
val<TStr extends ColumnPrimitiveStr, T extends ColumnPrimitiveMap[TStr] | undefined>(
|
|
89
53
|
dataType: TStr,
|
|
@@ -102,12 +66,6 @@ export const expr = {
|
|
|
102
66
|
* @param dataType - Column 데이터 타입
|
|
103
67
|
* @param path - Column 경로 (테이블 별칭, column 이름 등)
|
|
104
68
|
* @returns Column 참조 ExprUnit 인스턴스
|
|
105
|
-
*
|
|
106
|
-
* @example
|
|
107
|
-
* ```typescript
|
|
108
|
-
* // Direct column reference (internal use)
|
|
109
|
-
* expr.col("string", "T1", "name")
|
|
110
|
-
* ```
|
|
111
69
|
*/
|
|
112
70
|
col<TStr extends ColumnPrimitiveStr>(
|
|
113
71
|
dataType: ColumnPrimitiveStr,
|
|
@@ -124,18 +82,6 @@ export const expr = {
|
|
|
124
82
|
*
|
|
125
83
|
* @param dataType - 반환 값의 데이터 타입
|
|
126
84
|
* @returns 태그드 템플릿 함수
|
|
127
|
-
*
|
|
128
|
-
* @example
|
|
129
|
-
* ```typescript
|
|
130
|
-
* // Using MySQL JSON function
|
|
131
|
-
* db.user().select((u) => ({
|
|
132
|
-
* name: u.name,
|
|
133
|
-
* data: expr.raw("string")`JSON_EXTRACT(${u.metadata}, '$.email')`,
|
|
134
|
-
* }))
|
|
135
|
-
*
|
|
136
|
-
* // PostgreSQL array function
|
|
137
|
-
* expr.raw("number")`ARRAY_LENGTH(${u.tags}, 1)`
|
|
138
|
-
* ```
|
|
139
85
|
*/
|
|
140
86
|
raw<T extends ColumnPrimitiveStr>(
|
|
141
87
|
dataType: T,
|
|
@@ -169,12 +115,6 @@ export const expr = {
|
|
|
169
115
|
* @param source - 비교할 column 또는 expression
|
|
170
116
|
* @param target - 비교 대상 값 또는 expression
|
|
171
117
|
* @returns WHERE 조건 expression
|
|
172
|
-
*
|
|
173
|
-
* @example
|
|
174
|
-
* ```typescript
|
|
175
|
-
* db.user().where((u) => [expr.eq(u.status, "active")])
|
|
176
|
-
* // WHERE status <=> 'active' (MySQL)
|
|
177
|
-
* ```
|
|
178
118
|
*/
|
|
179
119
|
eq<T extends ColumnPrimitive>(source: ExprUnit<T>, target: ExprInput<T>): WhereExprUnit {
|
|
180
120
|
return new WhereExprUnit({
|
|
@@ -190,12 +130,6 @@ export const expr = {
|
|
|
190
130
|
* @param source - 비교할 column 또는 expression
|
|
191
131
|
* @param target - 비교 대상 값 또는 expression
|
|
192
132
|
* @returns WHERE 조건 expression
|
|
193
|
-
*
|
|
194
|
-
* @example
|
|
195
|
-
* ```typescript
|
|
196
|
-
* db.user().where((u) => [expr.gt(u.age, 18)])
|
|
197
|
-
* // WHERE age > 18
|
|
198
|
-
* ```
|
|
199
133
|
*/
|
|
200
134
|
gt<T extends ColumnPrimitive>(source: ExprUnit<T>, target: ExprInput<T>): WhereExprUnit {
|
|
201
135
|
return new WhereExprUnit({
|
|
@@ -211,12 +145,6 @@ export const expr = {
|
|
|
211
145
|
* @param source - 비교할 column 또는 expression
|
|
212
146
|
* @param target - 비교 대상 값 또는 expression
|
|
213
147
|
* @returns WHERE 조건 expression
|
|
214
|
-
*
|
|
215
|
-
* @example
|
|
216
|
-
* ```typescript
|
|
217
|
-
* db.user().where((u) => [expr.lt(u.score, 60)])
|
|
218
|
-
* // WHERE score < 60
|
|
219
|
-
* ```
|
|
220
148
|
*/
|
|
221
149
|
lt<T extends ColumnPrimitive>(source: ExprUnit<T>, target: ExprInput<T>): WhereExprUnit {
|
|
222
150
|
return new WhereExprUnit({
|
|
@@ -232,12 +160,6 @@ export const expr = {
|
|
|
232
160
|
* @param source - 비교할 column 또는 expression
|
|
233
161
|
* @param target - 비교 대상 값 또는 expression
|
|
234
162
|
* @returns WHERE 조건 expression
|
|
235
|
-
*
|
|
236
|
-
* @example
|
|
237
|
-
* ```typescript
|
|
238
|
-
* db.user().where((u) => [expr.gte(u.age, 18)])
|
|
239
|
-
* // WHERE age >= 18
|
|
240
|
-
* ```
|
|
241
163
|
*/
|
|
242
164
|
gte<T extends ColumnPrimitive>(source: ExprUnit<T>, target: ExprInput<T>): WhereExprUnit {
|
|
243
165
|
return new WhereExprUnit({
|
|
@@ -253,12 +175,6 @@ export const expr = {
|
|
|
253
175
|
* @param source - 비교할 column 또는 expression
|
|
254
176
|
* @param target - 비교 대상 값 또는 expression
|
|
255
177
|
* @returns WHERE 조건 expression
|
|
256
|
-
*
|
|
257
|
-
* @example
|
|
258
|
-
* ```typescript
|
|
259
|
-
* db.user().where((u) => [expr.lte(u.score, 100)])
|
|
260
|
-
* // WHERE score <= 100
|
|
261
|
-
* ```
|
|
262
178
|
*/
|
|
263
179
|
lte<T extends ColumnPrimitive>(source: ExprUnit<T>, target: ExprInput<T>): WhereExprUnit {
|
|
264
180
|
return new WhereExprUnit({
|
|
@@ -277,17 +193,6 @@ export const expr = {
|
|
|
277
193
|
* @param from - 시작 값 (undefined이면 하한 없음)
|
|
278
194
|
* @param to - 끝 값 (undefined이면 상한 없음)
|
|
279
195
|
* @returns WHERE 조건 expression
|
|
280
|
-
*
|
|
281
|
-
* @example
|
|
282
|
-
* ```typescript
|
|
283
|
-
* // Specify range
|
|
284
|
-
* db.user().where((u) => [expr.between(u.age, 18, 65)])
|
|
285
|
-
* // WHERE age BETWEEN 18 AND 65
|
|
286
|
-
*
|
|
287
|
-
* // Specify only lower bound
|
|
288
|
-
* db.user().where((u) => [expr.between(u.age, 18, undefined)])
|
|
289
|
-
* // WHERE age >= 18
|
|
290
|
-
* ```
|
|
291
196
|
*/
|
|
292
197
|
between<T extends ColumnPrimitive>(
|
|
293
198
|
source: ExprUnit<T>,
|
|
@@ -311,12 +216,6 @@ export const expr = {
|
|
|
311
216
|
*
|
|
312
217
|
* @param source - Column or expression to check
|
|
313
218
|
* @returns WHERE condition expression
|
|
314
|
-
*
|
|
315
|
-
* @example
|
|
316
|
-
* ```typescript
|
|
317
|
-
* db.user().where((u) => [expr.null(u.deletedAt)])
|
|
318
|
-
* // WHERE deletedAt IS NULL
|
|
319
|
-
* ```
|
|
320
219
|
*/
|
|
321
220
|
null<T extends ColumnPrimitive>(source: ExprUnit<T>): WhereExprUnit {
|
|
322
221
|
return new WhereExprUnit({
|
|
@@ -338,16 +237,6 @@ export const expr = {
|
|
|
338
237
|
* @param source - Column or expression to search
|
|
339
238
|
* @param pattern - Search pattern (%, _ wildcards available)
|
|
340
239
|
* @returns WHERE condition expression
|
|
341
|
-
*
|
|
342
|
-
* @example
|
|
343
|
-
* ```typescript
|
|
344
|
-
* // Prefix search
|
|
345
|
-
* db.user().where((u) => [expr.like(u.name, "John%")])
|
|
346
|
-
* // WHERE name LIKE 'John%' ESCAPE '\'
|
|
347
|
-
*
|
|
348
|
-
* // Contains search
|
|
349
|
-
* db.user().where((u) => [expr.like(u.email, "%@gmail.com")])
|
|
350
|
-
* ```
|
|
351
240
|
*/
|
|
352
241
|
like(
|
|
353
242
|
source: ExprUnit<string | undefined>,
|
|
@@ -368,12 +257,6 @@ export const expr = {
|
|
|
368
257
|
* @param source - Column or expression to search
|
|
369
258
|
* @param pattern - Regular expression pattern
|
|
370
259
|
* @returns WHERE condition expression
|
|
371
|
-
*
|
|
372
|
-
* @example
|
|
373
|
-
* ```typescript
|
|
374
|
-
* db.user().where((u) => [expr.regexp(u.email, "^[a-z]+@")])
|
|
375
|
-
* // MySQL: WHERE email REGEXP '^[a-z]+@'
|
|
376
|
-
* ```
|
|
377
260
|
*/
|
|
378
261
|
regexp(
|
|
379
262
|
source: ExprUnit<string | undefined>,
|
|
@@ -396,12 +279,6 @@ export const expr = {
|
|
|
396
279
|
* @param source - Column or expression to compare
|
|
397
280
|
* @param values - List of values to compare against
|
|
398
281
|
* @returns WHERE condition expression
|
|
399
|
-
*
|
|
400
|
-
* @example
|
|
401
|
-
* ```typescript
|
|
402
|
-
* db.user().where((u) => [expr.in(u.status, ["active", "pending"])])
|
|
403
|
-
* // WHERE status IN ('active', 'pending')
|
|
404
|
-
* ```
|
|
405
282
|
*/
|
|
406
283
|
in<T extends ColumnPrimitive>(source: ExprUnit<T>, values: ExprInput<T>[]): WhereExprUnit {
|
|
407
284
|
return new WhereExprUnit({
|
|
@@ -420,19 +297,6 @@ export const expr = {
|
|
|
420
297
|
* @param query - Queryable that returns a single column
|
|
421
298
|
* @returns WHERE condition expression
|
|
422
299
|
* @throws {Error} When the subquery does not return a single column
|
|
423
|
-
*
|
|
424
|
-
* @example
|
|
425
|
-
* ```typescript
|
|
426
|
-
* db.user().where((u) => [
|
|
427
|
-
* expr.inQuery(
|
|
428
|
-
* u.id,
|
|
429
|
-
* db.order()
|
|
430
|
-
* .where((o) => [expr.gt(o.amount, 1000)])
|
|
431
|
-
* .select((o) => ({ userId: o.userId }))
|
|
432
|
-
* ),
|
|
433
|
-
* ])
|
|
434
|
-
* // WHERE id IN (SELECT userId FROM Order WHERE amount > 1000)
|
|
435
|
-
* ```
|
|
436
300
|
*/
|
|
437
301
|
inQuery<T extends ColumnPrimitive, TData extends Record<string, T>>(
|
|
438
302
|
source: ExprUnit<T>,
|
|
@@ -456,17 +320,6 @@ export const expr = {
|
|
|
456
320
|
*
|
|
457
321
|
* @param query - Queryable to check for existence
|
|
458
322
|
* @returns WHERE condition expression
|
|
459
|
-
*
|
|
460
|
-
* @example
|
|
461
|
-
* ```typescript
|
|
462
|
-
* // Query users who have orders
|
|
463
|
-
* db.user().where((u) => [
|
|
464
|
-
* expr.exists(
|
|
465
|
-
* db.order().where((o) => [expr.eq(o.userId, u.id)])
|
|
466
|
-
* ),
|
|
467
|
-
* ])
|
|
468
|
-
* // WHERE EXISTS (SELECT 1 FROM Order WHERE userId = User.id)
|
|
469
|
-
* ```
|
|
470
323
|
*/
|
|
471
324
|
exists(query: Queryable<any, any>): WhereExprUnit {
|
|
472
325
|
const { select: _, ...queryDefWithoutSelect } = query.getSelectQueryDef(); // EXISTS는 SELECT 절이 불필요, 패킷 크기 절약
|
|
@@ -485,12 +338,6 @@ export const expr = {
|
|
|
485
338
|
*
|
|
486
339
|
* @param arg - Condition to negate
|
|
487
340
|
* @returns Negated WHERE condition expression
|
|
488
|
-
*
|
|
489
|
-
* @example
|
|
490
|
-
* ```typescript
|
|
491
|
-
* db.user().where((u) => [expr.not(expr.eq(u.status, "deleted"))])
|
|
492
|
-
* // WHERE NOT (status <=> 'deleted')
|
|
493
|
-
* ```
|
|
494
341
|
*/
|
|
495
342
|
not(arg: WhereExprUnit): WhereExprUnit {
|
|
496
343
|
return new WhereExprUnit({
|
|
@@ -506,17 +353,6 @@ export const expr = {
|
|
|
506
353
|
*
|
|
507
354
|
* @param conditions - List of conditions to combine with AND
|
|
508
355
|
* @returns Combined WHERE condition expression
|
|
509
|
-
*
|
|
510
|
-
* @example
|
|
511
|
-
* ```typescript
|
|
512
|
-
* db.user().where((u) => [
|
|
513
|
-
* expr.and([
|
|
514
|
-
* expr.eq(u.status, "active"),
|
|
515
|
-
* expr.gte(u.age, 18),
|
|
516
|
-
* ]),
|
|
517
|
-
* ])
|
|
518
|
-
* // WHERE (status <=> 'active' AND age >= 18)
|
|
519
|
-
* ```
|
|
520
356
|
*/
|
|
521
357
|
and(conditions: WhereExprUnit[]): WhereExprUnit {
|
|
522
358
|
if (conditions.length === 0) {
|
|
@@ -533,17 +369,6 @@ export const expr = {
|
|
|
533
369
|
*
|
|
534
370
|
* @param conditions - List of conditions to combine with OR
|
|
535
371
|
* @returns Combined WHERE condition expression
|
|
536
|
-
*
|
|
537
|
-
* @example
|
|
538
|
-
* ```typescript
|
|
539
|
-
* db.user().where((u) => [
|
|
540
|
-
* expr.or([
|
|
541
|
-
* expr.eq(u.status, "active"),
|
|
542
|
-
* expr.eq(u.status, "pending"),
|
|
543
|
-
* ]),
|
|
544
|
-
* ])
|
|
545
|
-
* // WHERE (status <=> 'active' OR status <=> 'pending')
|
|
546
|
-
* ```
|
|
547
372
|
*/
|
|
548
373
|
or(conditions: WhereExprUnit[]): WhereExprUnit {
|
|
549
374
|
if (conditions.length === 0) {
|
|
@@ -566,14 +391,6 @@ export const expr = {
|
|
|
566
391
|
*
|
|
567
392
|
* @param args - Strings to concatenate
|
|
568
393
|
* @returns Concatenated string expression
|
|
569
|
-
*
|
|
570
|
-
* @example
|
|
571
|
-
* ```typescript
|
|
572
|
-
* db.user().select((u) => ({
|
|
573
|
-
* fullName: expr.concat(u.firstName, " ", u.lastName),
|
|
574
|
-
* }))
|
|
575
|
-
* // SELECT CONCAT(firstName, ' ', lastName) AS fullName
|
|
576
|
-
* ```
|
|
577
394
|
*/
|
|
578
395
|
concat(...args: ExprInput<string | undefined>[]): ExprUnit<string> {
|
|
579
396
|
return new ExprUnit("string", {
|
|
@@ -588,14 +405,6 @@ export const expr = {
|
|
|
588
405
|
* @param source - Original string
|
|
589
406
|
* @param length - Number of characters to extract
|
|
590
407
|
* @returns Extracted string expression
|
|
591
|
-
*
|
|
592
|
-
* @example
|
|
593
|
-
* ```typescript
|
|
594
|
-
* db.user().select((u) => ({
|
|
595
|
-
* initial: expr.left(u.name, 1),
|
|
596
|
-
* }))
|
|
597
|
-
* // SELECT LEFT(name, 1) AS initial
|
|
598
|
-
* ```
|
|
599
408
|
*/
|
|
600
409
|
left<T extends string | undefined>(source: ExprUnit<T>, length: ExprInput<number>): ExprUnit<T> {
|
|
601
410
|
return new ExprUnit("string", {
|
|
@@ -611,14 +420,6 @@ export const expr = {
|
|
|
611
420
|
* @param source - Original string
|
|
612
421
|
* @param length - Number of characters to extract
|
|
613
422
|
* @returns Extracted string expression
|
|
614
|
-
*
|
|
615
|
-
* @example
|
|
616
|
-
* ```typescript
|
|
617
|
-
* db.phone().select((p) => ({
|
|
618
|
-
* lastFour: expr.right(p.number, 4),
|
|
619
|
-
* }))
|
|
620
|
-
* // SELECT RIGHT(number, 4) AS lastFour
|
|
621
|
-
* ```
|
|
622
423
|
*/
|
|
623
424
|
right<T extends string | undefined>(source: ExprUnit<T>, length: ExprInput<number>): ExprUnit<T> {
|
|
624
425
|
return new ExprUnit("string", {
|
|
@@ -633,14 +434,6 @@ export const expr = {
|
|
|
633
434
|
*
|
|
634
435
|
* @param source - Original string
|
|
635
436
|
* @returns String expression with whitespace removed
|
|
636
|
-
*
|
|
637
|
-
* @example
|
|
638
|
-
* ```typescript
|
|
639
|
-
* db.user().select((u) => ({
|
|
640
|
-
* name: expr.trim(u.name),
|
|
641
|
-
* }))
|
|
642
|
-
* // SELECT TRIM(name) AS name
|
|
643
|
-
* ```
|
|
644
437
|
*/
|
|
645
438
|
trim<T extends string | undefined>(source: ExprUnit<T>): ExprUnit<T> {
|
|
646
439
|
return new ExprUnit("string", {
|
|
@@ -658,15 +451,6 @@ export const expr = {
|
|
|
658
451
|
* @param length - Target length
|
|
659
452
|
* @param fillString - String to use for padding
|
|
660
453
|
* @returns Padded string expression
|
|
661
|
-
*
|
|
662
|
-
* @example
|
|
663
|
-
* ```typescript
|
|
664
|
-
* db.order().select((o) => ({
|
|
665
|
-
* orderNo: expr.padStart(expr.cast(o.id, { type: "varchar", length: 10 }), 8, "0"),
|
|
666
|
-
* }))
|
|
667
|
-
* // SELECT LPAD(CAST(id AS VARCHAR(10)), 8, '0') AS orderNo
|
|
668
|
-
* // Result: "00000123"
|
|
669
|
-
* ```
|
|
670
454
|
*/
|
|
671
455
|
padStart<T extends string | undefined>(
|
|
672
456
|
source: ExprUnit<T>,
|
|
@@ -688,14 +472,6 @@ export const expr = {
|
|
|
688
472
|
* @param from - String to find
|
|
689
473
|
* @param to - Replacement string
|
|
690
474
|
* @returns Replaced string expression
|
|
691
|
-
*
|
|
692
|
-
* @example
|
|
693
|
-
* ```typescript
|
|
694
|
-
* db.user().select((u) => ({
|
|
695
|
-
* phone: expr.replace(u.phone, "-", ""),
|
|
696
|
-
* }))
|
|
697
|
-
* // SELECT REPLACE(phone, '-', '') AS phone
|
|
698
|
-
* ```
|
|
699
475
|
*/
|
|
700
476
|
replace<T extends string | undefined>(
|
|
701
477
|
source: ExprUnit<T>,
|
|
@@ -715,14 +491,6 @@ export const expr = {
|
|
|
715
491
|
*
|
|
716
492
|
* @param source - Original string
|
|
717
493
|
* @returns Uppercase string expression
|
|
718
|
-
*
|
|
719
|
-
* @example
|
|
720
|
-
* ```typescript
|
|
721
|
-
* db.user().select((u) => ({
|
|
722
|
-
* code: expr.upper(u.code),
|
|
723
|
-
* }))
|
|
724
|
-
* // SELECT UPPER(code) AS code
|
|
725
|
-
* ```
|
|
726
494
|
*/
|
|
727
495
|
upper<T extends string | undefined>(source: ExprUnit<T>): ExprUnit<T> {
|
|
728
496
|
return new ExprUnit("string", {
|
|
@@ -736,14 +504,6 @@ export const expr = {
|
|
|
736
504
|
*
|
|
737
505
|
* @param source - Original string
|
|
738
506
|
* @returns Lowercase string expression
|
|
739
|
-
*
|
|
740
|
-
* @example
|
|
741
|
-
* ```typescript
|
|
742
|
-
* db.user().select((u) => ({
|
|
743
|
-
* email: expr.lower(u.email),
|
|
744
|
-
* }))
|
|
745
|
-
* // SELECT LOWER(email) AS email
|
|
746
|
-
* ```
|
|
747
507
|
*/
|
|
748
508
|
lower<T extends string | undefined>(source: ExprUnit<T>): ExprUnit<T> {
|
|
749
509
|
return new ExprUnit("string", {
|
|
@@ -757,14 +517,6 @@ export const expr = {
|
|
|
757
517
|
*
|
|
758
518
|
* @param source - Original string
|
|
759
519
|
* @returns Character count
|
|
760
|
-
*
|
|
761
|
-
* @example
|
|
762
|
-
* ```typescript
|
|
763
|
-
* db.user().select((u) => ({
|
|
764
|
-
* nameLength: expr.length(u.name),
|
|
765
|
-
* }))
|
|
766
|
-
* // SELECT CHAR_LENGTH(name) AS nameLength
|
|
767
|
-
* ```
|
|
768
520
|
*/
|
|
769
521
|
length(source: ExprUnit<string | undefined>): ExprUnit<number> {
|
|
770
522
|
return new ExprUnit("number", {
|
|
@@ -780,14 +532,6 @@ export const expr = {
|
|
|
780
532
|
*
|
|
781
533
|
* @param source - Original string
|
|
782
534
|
* @returns Byte count
|
|
783
|
-
*
|
|
784
|
-
* @example
|
|
785
|
-
* ```typescript
|
|
786
|
-
* db.user().select((u) => ({
|
|
787
|
-
* byteLen: expr.byteLength(u.name),
|
|
788
|
-
* }))
|
|
789
|
-
* // SELECT OCTET_LENGTH(name) AS byteLen
|
|
790
|
-
* ```
|
|
791
535
|
*/
|
|
792
536
|
byteLength(source: ExprUnit<string | undefined>): ExprUnit<number> {
|
|
793
537
|
return new ExprUnit("number", {
|
|
@@ -805,15 +549,6 @@ export const expr = {
|
|
|
805
549
|
* @param start - Start position (starting from 1)
|
|
806
550
|
* @param length - Length to extract (to the end if omitted)
|
|
807
551
|
* @returns Extracted string expression
|
|
808
|
-
*
|
|
809
|
-
* @example
|
|
810
|
-
* ```typescript
|
|
811
|
-
* db.user().select((u) => ({
|
|
812
|
-
* // From "Hello World", 5 characters starting at index 1: "Hello"
|
|
813
|
-
* prefix: expr.substring(u.name, 1, 5),
|
|
814
|
-
* }))
|
|
815
|
-
* // SELECT SUBSTRING(name, 1, 5) AS prefix
|
|
816
|
-
* ```
|
|
817
552
|
*/
|
|
818
553
|
substring<T extends string | undefined>(
|
|
819
554
|
source: ExprUnit<T>,
|
|
@@ -836,15 +571,6 @@ export const expr = {
|
|
|
836
571
|
* @param source - String to search in
|
|
837
572
|
* @param search - String to find
|
|
838
573
|
* @returns Position (starting from 1, 0 if not found)
|
|
839
|
-
*
|
|
840
|
-
* @example
|
|
841
|
-
* ```typescript
|
|
842
|
-
* db.user().select((u) => ({
|
|
843
|
-
* atPos: expr.indexOf(u.email, "@"),
|
|
844
|
-
* }))
|
|
845
|
-
* // SELECT LOCATE('@', email) AS atPos (MySQL)
|
|
846
|
-
* // "john@example.com" → 5
|
|
847
|
-
* ```
|
|
848
574
|
*/
|
|
849
575
|
indexOf(source: ExprUnit<string | undefined>, search: ExprInput<string>): ExprUnit<number> {
|
|
850
576
|
return new ExprUnit("number", {
|
|
@@ -863,14 +589,6 @@ export const expr = {
|
|
|
863
589
|
*
|
|
864
590
|
* @param source - Original number
|
|
865
591
|
* @returns Absolute value expression
|
|
866
|
-
*
|
|
867
|
-
* @example
|
|
868
|
-
* ```typescript
|
|
869
|
-
* db.account().select((a) => ({
|
|
870
|
-
* balance: expr.abs(a.balance),
|
|
871
|
-
* }))
|
|
872
|
-
* // SELECT ABS(balance) AS balance
|
|
873
|
-
* ```
|
|
874
592
|
*/
|
|
875
593
|
abs<T extends number | undefined>(source: ExprUnit<T>): ExprUnit<T> {
|
|
876
594
|
return new ExprUnit("number", {
|
|
@@ -885,15 +603,6 @@ export const expr = {
|
|
|
885
603
|
* @param source - Original number
|
|
886
604
|
* @param digits - Number of decimal places
|
|
887
605
|
* @returns Rounded number expression
|
|
888
|
-
*
|
|
889
|
-
* @example
|
|
890
|
-
* ```typescript
|
|
891
|
-
* db.product().select((p) => ({
|
|
892
|
-
* price: expr.round(p.price, 2),
|
|
893
|
-
* }))
|
|
894
|
-
* // SELECT ROUND(price, 2) AS price
|
|
895
|
-
* // 123.456 → 123.46
|
|
896
|
-
* ```
|
|
897
606
|
*/
|
|
898
607
|
round<T extends number | undefined>(source: ExprUnit<T>, digits: number): ExprUnit<T> {
|
|
899
608
|
return new ExprUnit("number", {
|
|
@@ -908,15 +617,6 @@ export const expr = {
|
|
|
908
617
|
*
|
|
909
618
|
* @param source - Original number
|
|
910
619
|
* @returns Ceiling number expression
|
|
911
|
-
*
|
|
912
|
-
* @example
|
|
913
|
-
* ```typescript
|
|
914
|
-
* db.order().select((o) => ({
|
|
915
|
-
* pages: expr.ceil(expr.multiply(o.itemCount, 0.1)),
|
|
916
|
-
* }))
|
|
917
|
-
* // SELECT CEILING(itemCount / 10) AS pages
|
|
918
|
-
* // 25 / 10 = 2.5 → 3
|
|
919
|
-
* ```
|
|
920
620
|
*/
|
|
921
621
|
ceil<T extends number | undefined>(source: ExprUnit<T>): ExprUnit<T> {
|
|
922
622
|
return new ExprUnit("number", {
|
|
@@ -930,15 +630,6 @@ export const expr = {
|
|
|
930
630
|
*
|
|
931
631
|
* @param source - Original number
|
|
932
632
|
* @returns Floor number expression
|
|
933
|
-
*
|
|
934
|
-
* @example
|
|
935
|
-
* ```typescript
|
|
936
|
-
* db.user().select((u) => ({
|
|
937
|
-
* ageGroup: expr.floor(expr.multiply(u.age, 0.1)),
|
|
938
|
-
* }))
|
|
939
|
-
* // SELECT FLOOR(age / 10) AS ageGroup
|
|
940
|
-
* // 25 / 10 = 2.5 → 2
|
|
941
|
-
* ```
|
|
942
633
|
*/
|
|
943
634
|
floor<T extends number | undefined>(source: ExprUnit<T>): ExprUnit<T> {
|
|
944
635
|
return new ExprUnit("number", {
|
|
@@ -956,14 +647,6 @@ export const expr = {
|
|
|
956
647
|
*
|
|
957
648
|
* @param source - DateTime or DateOnly expression
|
|
958
649
|
* @returns Year (4-digit number)
|
|
959
|
-
*
|
|
960
|
-
* @example
|
|
961
|
-
* ```typescript
|
|
962
|
-
* db.user().select((u) => ({
|
|
963
|
-
* birthYear: expr.year(u.birthDate),
|
|
964
|
-
* }))
|
|
965
|
-
* // SELECT YEAR(birthDate) AS birthYear
|
|
966
|
-
* ```
|
|
967
650
|
*/
|
|
968
651
|
year<T extends DateTime | DateOnly | undefined>(
|
|
969
652
|
source: ExprUnit<T>,
|
|
@@ -979,14 +662,6 @@ export const expr = {
|
|
|
979
662
|
*
|
|
980
663
|
* @param source - DateTime or DateOnly expression
|
|
981
664
|
* @returns Month (1~12)
|
|
982
|
-
*
|
|
983
|
-
* @example
|
|
984
|
-
* ```typescript
|
|
985
|
-
* db.order().select((o) => ({
|
|
986
|
-
* orderMonth: expr.month(o.createdAt),
|
|
987
|
-
* }))
|
|
988
|
-
* // SELECT MONTH(createdAt) AS orderMonth
|
|
989
|
-
* ```
|
|
990
665
|
*/
|
|
991
666
|
month<T extends DateTime | DateOnly | undefined>(
|
|
992
667
|
source: ExprUnit<T>,
|
|
@@ -1002,14 +677,6 @@ export const expr = {
|
|
|
1002
677
|
*
|
|
1003
678
|
* @param source - DateTime or DateOnly expression
|
|
1004
679
|
* @returns Day (1~31)
|
|
1005
|
-
*
|
|
1006
|
-
* @example
|
|
1007
|
-
* ```typescript
|
|
1008
|
-
* db.user().select((u) => ({
|
|
1009
|
-
* birthDay: expr.day(u.birthDate),
|
|
1010
|
-
* }))
|
|
1011
|
-
* // SELECT DAY(birthDate) AS birthDay
|
|
1012
|
-
* ```
|
|
1013
680
|
*/
|
|
1014
681
|
day<T extends DateTime | DateOnly | undefined>(
|
|
1015
682
|
source: ExprUnit<T>,
|
|
@@ -1025,14 +692,6 @@ export const expr = {
|
|
|
1025
692
|
*
|
|
1026
693
|
* @param source - DateTime or Time expression
|
|
1027
694
|
* @returns Hour (0~23)
|
|
1028
|
-
*
|
|
1029
|
-
* @example
|
|
1030
|
-
* ```typescript
|
|
1031
|
-
* db.log().select((l) => ({
|
|
1032
|
-
* logHour: expr.hour(l.createdAt),
|
|
1033
|
-
* }))
|
|
1034
|
-
* // SELECT HOUR(createdAt) AS logHour
|
|
1035
|
-
* ```
|
|
1036
695
|
*/
|
|
1037
696
|
hour<T extends DateTime | Time | undefined>(
|
|
1038
697
|
source: ExprUnit<T>,
|
|
@@ -1048,14 +707,6 @@ export const expr = {
|
|
|
1048
707
|
*
|
|
1049
708
|
* @param source - DateTime or Time expression
|
|
1050
709
|
* @returns Minute (0~59)
|
|
1051
|
-
*
|
|
1052
|
-
* @example
|
|
1053
|
-
* ```typescript
|
|
1054
|
-
* db.log().select((l) => ({
|
|
1055
|
-
* logMinute: expr.minute(l.createdAt),
|
|
1056
|
-
* }))
|
|
1057
|
-
* // SELECT MINUTE(createdAt) AS logMinute
|
|
1058
|
-
* ```
|
|
1059
710
|
*/
|
|
1060
711
|
minute<T extends DateTime | Time | undefined>(
|
|
1061
712
|
source: ExprUnit<T>,
|
|
@@ -1071,14 +722,6 @@ export const expr = {
|
|
|
1071
722
|
*
|
|
1072
723
|
* @param source - DateTime or Time expression
|
|
1073
724
|
* @returns Second (0~59)
|
|
1074
|
-
*
|
|
1075
|
-
* @example
|
|
1076
|
-
* ```typescript
|
|
1077
|
-
* db.log().select((l) => ({
|
|
1078
|
-
* logSecond: expr.second(l.createdAt),
|
|
1079
|
-
* }))
|
|
1080
|
-
* // SELECT SECOND(createdAt) AS logSecond
|
|
1081
|
-
* ```
|
|
1082
725
|
*/
|
|
1083
726
|
second<T extends DateTime | Time | undefined>(
|
|
1084
727
|
source: ExprUnit<T>,
|
|
@@ -1096,14 +739,6 @@ export const expr = {
|
|
|
1096
739
|
*
|
|
1097
740
|
* @param source - DateOnly expression
|
|
1098
741
|
* @returns ISO week number
|
|
1099
|
-
*
|
|
1100
|
-
* @example
|
|
1101
|
-
* ```typescript
|
|
1102
|
-
* db.order().select((o) => ({
|
|
1103
|
-
* weekNum: expr.isoWeek(o.orderDate),
|
|
1104
|
-
* }))
|
|
1105
|
-
* // SELECT WEEK(orderDate, 3) AS weekNum (MySQL)
|
|
1106
|
-
* ```
|
|
1107
742
|
*/
|
|
1108
743
|
isoWeek<T extends DateOnly | undefined>(
|
|
1109
744
|
source: ExprUnit<T>,
|
|
@@ -1121,14 +756,6 @@ export const expr = {
|
|
|
1121
756
|
*
|
|
1122
757
|
* @param source - DateOnly expression
|
|
1123
758
|
* @returns Week start date (Monday)
|
|
1124
|
-
*
|
|
1125
|
-
* @example
|
|
1126
|
-
* ```typescript
|
|
1127
|
-
* db.order().select((o) => ({
|
|
1128
|
-
* weekStart: expr.isoWeekStartDate(o.orderDate),
|
|
1129
|
-
* }))
|
|
1130
|
-
* // 2024-01-10 (Wed) → 2024-01-08 (Mon)
|
|
1131
|
-
* ```
|
|
1132
759
|
*/
|
|
1133
760
|
isoWeekStartDate<T extends DateOnly | undefined>(source: ExprUnit<T>): ExprUnit<T> {
|
|
1134
761
|
return new ExprUnit("DateOnly", {
|
|
@@ -1144,14 +771,6 @@ export const expr = {
|
|
|
1144
771
|
*
|
|
1145
772
|
* @param source - DateOnly expression
|
|
1146
773
|
* @returns Year-month string (YYYYMM)
|
|
1147
|
-
*
|
|
1148
|
-
* @example
|
|
1149
|
-
* ```typescript
|
|
1150
|
-
* db.order().select((o) => ({
|
|
1151
|
-
* yearMonth: expr.isoYearMonth(o.orderDate),
|
|
1152
|
-
* }))
|
|
1153
|
-
* // 2024-01-15 → "202401"
|
|
1154
|
-
* ```
|
|
1155
774
|
*/
|
|
1156
775
|
isoYearMonth<T extends DateOnly | undefined>(
|
|
1157
776
|
source: ExprUnit<T>,
|
|
@@ -1169,14 +788,6 @@ export const expr = {
|
|
|
1169
788
|
* @param from - Start date
|
|
1170
789
|
* @param to - End date
|
|
1171
790
|
* @returns Difference value (to - from)
|
|
1172
|
-
*
|
|
1173
|
-
* @example
|
|
1174
|
-
* ```typescript
|
|
1175
|
-
* db.user().select((u) => ({
|
|
1176
|
-
* age: expr.dateDiff("year", u.birthDate, expr.val("DateOnly", DateOnly.today())),
|
|
1177
|
-
* }))
|
|
1178
|
-
* // SELECT DATEDIFF(year, birthDate, '2024-01-15') AS age
|
|
1179
|
-
* ```
|
|
1180
791
|
*/
|
|
1181
792
|
dateDiff<T extends DateTime | DateOnly | Time | undefined>(
|
|
1182
793
|
unit: DateUnit,
|
|
@@ -1198,14 +809,6 @@ export const expr = {
|
|
|
1198
809
|
* @param source - Original date
|
|
1199
810
|
* @param value - Value to add (negative allowed)
|
|
1200
811
|
* @returns Calculated date
|
|
1201
|
-
*
|
|
1202
|
-
* @example
|
|
1203
|
-
* ```typescript
|
|
1204
|
-
* db.subscription().select((s) => ({
|
|
1205
|
-
* expiresAt: expr.dateAdd("month", s.startDate, 12),
|
|
1206
|
-
* }))
|
|
1207
|
-
* // SELECT DATEADD(month, 12, startDate) AS expiresAt
|
|
1208
|
-
* ```
|
|
1209
812
|
*/
|
|
1210
813
|
dateAdd<T extends DateTime | DateOnly | Time | undefined>(
|
|
1211
814
|
unit: DateUnit,
|
|
@@ -1228,15 +831,6 @@ export const expr = {
|
|
|
1228
831
|
* @param source - Date expression
|
|
1229
832
|
* @param format - Format string (e.g., "%Y-%m-%d")
|
|
1230
833
|
* @returns Formatted string expression
|
|
1231
|
-
*
|
|
1232
|
-
* @example
|
|
1233
|
-
* ```typescript
|
|
1234
|
-
* db.order().select((o) => ({
|
|
1235
|
-
* orderDate: expr.formatDate(o.createdAt, "%Y-%m-%d"),
|
|
1236
|
-
* }))
|
|
1237
|
-
* // SELECT DATE_FORMAT(createdAt, '%Y-%m-%d') AS orderDate (MySQL)
|
|
1238
|
-
* // 2024-01-15 10:30:00 → "2024-01-15"
|
|
1239
|
-
* ```
|
|
1240
834
|
*/
|
|
1241
835
|
formatDate<T extends DateTime | DateOnly | Time | undefined>(
|
|
1242
836
|
source: ExprUnit<T>,
|
|
@@ -1260,14 +854,6 @@ export const expr = {
|
|
|
1260
854
|
*
|
|
1261
855
|
* @param args - Values to inspect (last is default value)
|
|
1262
856
|
* @returns First non-null value
|
|
1263
|
-
*
|
|
1264
|
-
* @example
|
|
1265
|
-
* ```typescript
|
|
1266
|
-
* db.user().select((u) => ({
|
|
1267
|
-
* displayName: expr.coalesce(u.nickname, u.name, "Guest"),
|
|
1268
|
-
* }))
|
|
1269
|
-
* // SELECT COALESCE(nickname, name, 'Guest') AS displayName
|
|
1270
|
-
* ```
|
|
1271
857
|
*/
|
|
1272
858
|
coalesce,
|
|
1273
859
|
|
|
@@ -1279,15 +865,6 @@ export const expr = {
|
|
|
1279
865
|
* @param source - Original value
|
|
1280
866
|
* @param value - Value to compare
|
|
1281
867
|
* @returns NULL or original value
|
|
1282
|
-
*
|
|
1283
|
-
* @example
|
|
1284
|
-
* ```typescript
|
|
1285
|
-
* db.user().select((u) => ({
|
|
1286
|
-
* // Convert empty string to NULL
|
|
1287
|
-
* bio: expr.nullIf(u.bio, ""),
|
|
1288
|
-
* }))
|
|
1289
|
-
* // SELECT NULLIF(bio, '') AS bio
|
|
1290
|
-
* ```
|
|
1291
868
|
*/
|
|
1292
869
|
nullIf<T extends ColumnPrimitive>(
|
|
1293
870
|
source: ExprUnit<T>,
|
|
@@ -1307,14 +884,6 @@ export const expr = {
|
|
|
1307
884
|
*
|
|
1308
885
|
* @param condition - Condition to transform
|
|
1309
886
|
* @returns boolean expression
|
|
1310
|
-
*
|
|
1311
|
-
* @example
|
|
1312
|
-
* ```typescript
|
|
1313
|
-
* db.user().select((u) => ({
|
|
1314
|
-
* isActive: expr.is(expr.eq(u.status, "active")),
|
|
1315
|
-
* }))
|
|
1316
|
-
* // SELECT (status <=> 'active') AS isActive
|
|
1317
|
-
* ```
|
|
1318
887
|
*/
|
|
1319
888
|
is(condition: WhereExprUnit): ExprUnit<boolean> {
|
|
1320
889
|
return new ExprUnit("boolean", {
|
|
@@ -1329,18 +898,6 @@ export const expr = {
|
|
|
1329
898
|
* 메서드 체이닝으로 조건 분기 구성
|
|
1330
899
|
*
|
|
1331
900
|
* @returns SwitchExprBuilder instance
|
|
1332
|
-
*
|
|
1333
|
-
* @example
|
|
1334
|
-
* ```typescript
|
|
1335
|
-
* db.user().select((u) => ({
|
|
1336
|
-
* grade: expr.switch<string>()
|
|
1337
|
-
* .case(expr.gte(u.score, 90), "A")
|
|
1338
|
-
* .case(expr.gte(u.score, 80), "B")
|
|
1339
|
-
* .case(expr.gte(u.score, 70), "C")
|
|
1340
|
-
* .default("F"),
|
|
1341
|
-
* }))
|
|
1342
|
-
* // SELECT CASE WHEN score >= 90 THEN 'A' ... ELSE 'F' END AS grade
|
|
1343
|
-
* ```
|
|
1344
901
|
*/
|
|
1345
902
|
switch<T extends ColumnPrimitive>(): SwitchExprBuilder<T> {
|
|
1346
903
|
return createSwitchBuilder<T>();
|
|
@@ -1353,14 +910,6 @@ export const expr = {
|
|
|
1353
910
|
* @param then - Value when condition is true
|
|
1354
911
|
* @param else_ - Value when condition is false
|
|
1355
912
|
* @returns Conditional value expression
|
|
1356
|
-
*
|
|
1357
|
-
* @example
|
|
1358
|
-
* ```typescript
|
|
1359
|
-
* db.user().select((u) => ({
|
|
1360
|
-
* type: expr.if(expr.gte(u.age, 18), "adult", "minor"),
|
|
1361
|
-
* }))
|
|
1362
|
-
* // SELECT IF(age >= 18, 'adult', 'minor') AS type
|
|
1363
|
-
* ```
|
|
1364
913
|
*/
|
|
1365
914
|
if<T extends ColumnPrimitive>(
|
|
1366
915
|
condition: WhereExprUnit,
|
|
@@ -1404,17 +953,6 @@ export const expr = {
|
|
|
1404
953
|
* @param arg - Column to count (all rows if omitted)
|
|
1405
954
|
* @param distinct - If true, remove duplicates
|
|
1406
955
|
* @returns Row count
|
|
1407
|
-
*
|
|
1408
|
-
* @example
|
|
1409
|
-
* ```typescript
|
|
1410
|
-
* // Total row count
|
|
1411
|
-
* db.user().select(() => ({ total: expr.count() }))
|
|
1412
|
-
*
|
|
1413
|
-
* // Distinct count
|
|
1414
|
-
* db.order().select((o) => ({
|
|
1415
|
-
* uniqueCustomers: expr.count(o.customerId, true),
|
|
1416
|
-
* }))
|
|
1417
|
-
* ```
|
|
1418
956
|
*/
|
|
1419
957
|
count(arg?: ExprUnit<ColumnPrimitive>, distinct?: boolean): ExprUnit<number> {
|
|
1420
958
|
return new ExprUnit("number", {
|
|
@@ -1431,14 +969,6 @@ export const expr = {
|
|
|
1431
969
|
*
|
|
1432
970
|
* @param arg - Number column to sum
|
|
1433
971
|
* @returns Sum (or NULL)
|
|
1434
|
-
*
|
|
1435
|
-
* @example
|
|
1436
|
-
* ```typescript
|
|
1437
|
-
* db.order().groupBy((o) => o.userId).select((o) => ({
|
|
1438
|
-
* userId: o.userId,
|
|
1439
|
-
* totalAmount: expr.sum(o.amount),
|
|
1440
|
-
* }))
|
|
1441
|
-
* ```
|
|
1442
972
|
*/
|
|
1443
973
|
sum(arg: ExprUnit<number | undefined>): ExprUnit<number | undefined> {
|
|
1444
974
|
return new ExprUnit("number", {
|
|
@@ -1454,14 +984,6 @@ export const expr = {
|
|
|
1454
984
|
*
|
|
1455
985
|
* @param arg - Number column to average
|
|
1456
986
|
* @returns Average (or NULL)
|
|
1457
|
-
*
|
|
1458
|
-
* @example
|
|
1459
|
-
* ```typescript
|
|
1460
|
-
* db.product().groupBy((p) => p.categoryId).select((p) => ({
|
|
1461
|
-
* categoryId: p.categoryId,
|
|
1462
|
-
* avgPrice: expr.avg(p.price),
|
|
1463
|
-
* }))
|
|
1464
|
-
* ```
|
|
1465
987
|
*/
|
|
1466
988
|
avg(arg: ExprUnit<number | undefined>): ExprUnit<number | undefined> {
|
|
1467
989
|
return new ExprUnit("number", {
|
|
@@ -1477,14 +999,6 @@ export const expr = {
|
|
|
1477
999
|
*
|
|
1478
1000
|
* @param arg - Column to find maximum of
|
|
1479
1001
|
* @returns Maximum value (or NULL)
|
|
1480
|
-
*
|
|
1481
|
-
* @example
|
|
1482
|
-
* ```typescript
|
|
1483
|
-
* db.order().groupBy((o) => o.userId).select((o) => ({
|
|
1484
|
-
* userId: o.userId,
|
|
1485
|
-
* lastOrderDate: expr.max(o.createdAt),
|
|
1486
|
-
* }))
|
|
1487
|
-
* ```
|
|
1488
1002
|
*/
|
|
1489
1003
|
max<T extends ColumnPrimitive>(arg: ExprUnit<T>): ExprUnit<T | undefined> {
|
|
1490
1004
|
return new ExprUnit(arg.dataType, {
|
|
@@ -1500,14 +1014,6 @@ export const expr = {
|
|
|
1500
1014
|
*
|
|
1501
1015
|
* @param arg - Column to find minimum of
|
|
1502
1016
|
* @returns Minimum value (or NULL)
|
|
1503
|
-
*
|
|
1504
|
-
* @example
|
|
1505
|
-
* ```typescript
|
|
1506
|
-
* db.product().groupBy((p) => p.categoryId).select((p) => ({
|
|
1507
|
-
* categoryId: p.categoryId,
|
|
1508
|
-
* minPrice: expr.min(p.price),
|
|
1509
|
-
* }))
|
|
1510
|
-
* ```
|
|
1511
1017
|
*/
|
|
1512
1018
|
min<T extends ColumnPrimitive>(arg: ExprUnit<T>): ExprUnit<T | undefined> {
|
|
1513
1019
|
return new ExprUnit(arg.dataType, {
|
|
@@ -1525,14 +1031,6 @@ export const expr = {
|
|
|
1525
1031
|
*
|
|
1526
1032
|
* @param args - Values to compare
|
|
1527
1033
|
* @returns Greatest value
|
|
1528
|
-
*
|
|
1529
|
-
* @example
|
|
1530
|
-
* ```typescript
|
|
1531
|
-
* db.product().select((p) => ({
|
|
1532
|
-
* effectivePrice: expr.greatest(p.price, p.minPrice),
|
|
1533
|
-
* }))
|
|
1534
|
-
* // SELECT GREATEST(price, minPrice) AS effectivePrice
|
|
1535
|
-
* ```
|
|
1536
1034
|
*/
|
|
1537
1035
|
greatest<T extends ColumnPrimitive>(...args: ExprInput<T>[]): ExprUnit<T> {
|
|
1538
1036
|
return new ExprUnit(findDataType(args), {
|
|
@@ -1546,14 +1044,6 @@ export const expr = {
|
|
|
1546
1044
|
*
|
|
1547
1045
|
* @param args - Values to compare
|
|
1548
1046
|
* @returns Least value
|
|
1549
|
-
*
|
|
1550
|
-
* @example
|
|
1551
|
-
* ```typescript
|
|
1552
|
-
* db.product().select((p) => ({
|
|
1553
|
-
* effectivePrice: expr.least(p.price, p.maxDiscount),
|
|
1554
|
-
* }))
|
|
1555
|
-
* // SELECT LEAST(price, maxDiscount) AS effectivePrice
|
|
1556
|
-
* ```
|
|
1557
1047
|
*/
|
|
1558
1048
|
least<T extends ColumnPrimitive>(...args: ExprInput<T>[]): ExprUnit<T> {
|
|
1559
1049
|
return new ExprUnit(findDataType(args), {
|
|
@@ -1566,14 +1056,6 @@ export const expr = {
|
|
|
1566
1056
|
* 행 번호 (ROW_NUMBER 없이 모든 행에 순번 부여)
|
|
1567
1057
|
*
|
|
1568
1058
|
* @returns Row number (starting from 1)
|
|
1569
|
-
*
|
|
1570
|
-
* @example
|
|
1571
|
-
* ```typescript
|
|
1572
|
-
* db.user().select((u) => ({
|
|
1573
|
-
* rowNum: expr.rowNum(),
|
|
1574
|
-
* name: u.name,
|
|
1575
|
-
* }))
|
|
1576
|
-
* ```
|
|
1577
1059
|
*/
|
|
1578
1060
|
rowNum(): ExprUnit<number> {
|
|
1579
1061
|
return new ExprUnit("number", {
|
|
@@ -1587,12 +1069,6 @@ export const expr = {
|
|
|
1587
1069
|
* 0과 1 사이의 난수 반환. 주로 ORDER BY에서 무작위 정렬에 사용
|
|
1588
1070
|
*
|
|
1589
1071
|
* @returns Random number between 0 and 1
|
|
1590
|
-
*
|
|
1591
|
-
* @example
|
|
1592
|
-
* ```typescript
|
|
1593
|
-
* // Random sorting
|
|
1594
|
-
* db.user().orderBy(() => expr.random()).limit(10)
|
|
1595
|
-
* ```
|
|
1596
1072
|
*/
|
|
1597
1073
|
random(): ExprUnit<number> {
|
|
1598
1074
|
return new ExprUnit("number", {
|
|
@@ -1606,14 +1082,6 @@ export const expr = {
|
|
|
1606
1082
|
* @param source - Expression to transform
|
|
1607
1083
|
* @param targetType - Target data type
|
|
1608
1084
|
* @returns Transformed expression
|
|
1609
|
-
*
|
|
1610
|
-
* @example
|
|
1611
|
-
* ```typescript
|
|
1612
|
-
* db.order().select((o) => ({
|
|
1613
|
-
* idStr: expr.cast(o.id, { type: "varchar", length: 20 }),
|
|
1614
|
-
* }))
|
|
1615
|
-
* // SELECT CAST(id AS VARCHAR(20)) AS idStr
|
|
1616
|
-
* ```
|
|
1617
1085
|
*/
|
|
1618
1086
|
cast<T extends ColumnPrimitive, TDataType extends DataType>(
|
|
1619
1087
|
source: ExprUnit<T>,
|
|
@@ -1634,20 +1102,6 @@ export const expr = {
|
|
|
1634
1102
|
* @param dataType - Data type of the returned value
|
|
1635
1103
|
* @param queryable - Queryable that returns a scalar value
|
|
1636
1104
|
* @returns Subquery result expression
|
|
1637
|
-
*
|
|
1638
|
-
* @example
|
|
1639
|
-
* ```typescript
|
|
1640
|
-
* db.user().select((u) => ({
|
|
1641
|
-
* id: u.id,
|
|
1642
|
-
* postCount: expr.subquery(
|
|
1643
|
-
* "number",
|
|
1644
|
-
* db.post()
|
|
1645
|
-
* .where((p) => [expr.eq(p.userId, u.id)])
|
|
1646
|
-
* .select(() => ({ cnt: expr.count() }))
|
|
1647
|
-
* ),
|
|
1648
|
-
* }))
|
|
1649
|
-
* // SELECT id, (SELECT COUNT(*) FROM Post WHERE userId = User.id) AS postCount
|
|
1650
|
-
* ```
|
|
1651
1105
|
*/
|
|
1652
1106
|
subquery<TStr extends ColumnPrimitiveStr>(
|
|
1653
1107
|
dataType: TStr,
|
|
@@ -1670,18 +1124,6 @@ export const expr = {
|
|
|
1670
1124
|
*
|
|
1671
1125
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1672
1126
|
* @returns Row number (starting from 1)
|
|
1673
|
-
*
|
|
1674
|
-
* @example
|
|
1675
|
-
* ```typescript
|
|
1676
|
-
* db.order().select((o) => ({
|
|
1677
|
-
* ...o,
|
|
1678
|
-
* rowNum: expr.rowNumber({
|
|
1679
|
-
* partitionBy: [o.userId],
|
|
1680
|
-
* orderBy: [[o.createdAt, "DESC"]],
|
|
1681
|
-
* }),
|
|
1682
|
-
* }))
|
|
1683
|
-
* // SELECT *, ROW_NUMBER() OVER (PARTITION BY userId ORDER BY createdAt DESC)
|
|
1684
|
-
* ```
|
|
1685
1127
|
*/
|
|
1686
1128
|
rowNumber(spec: WinSpecInput): ExprUnit<number> {
|
|
1687
1129
|
return new ExprUnit("number", {
|
|
@@ -1696,16 +1138,6 @@ export const expr = {
|
|
|
1696
1138
|
*
|
|
1697
1139
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1698
1140
|
* @returns Rank (skips after ties: 1, 1, 3)
|
|
1699
|
-
*
|
|
1700
|
-
* @example
|
|
1701
|
-
* ```typescript
|
|
1702
|
-
* db.student().select((s) => ({
|
|
1703
|
-
* name: s.name,
|
|
1704
|
-
* rank: expr.rank({
|
|
1705
|
-
* orderBy: [[s.score, "DESC"]],
|
|
1706
|
-
* }),
|
|
1707
|
-
* }))
|
|
1708
|
-
* ```
|
|
1709
1141
|
*/
|
|
1710
1142
|
rank(spec: WinSpecInput): ExprUnit<number> {
|
|
1711
1143
|
return new ExprUnit("number", {
|
|
@@ -1720,16 +1152,6 @@ export const expr = {
|
|
|
1720
1152
|
*
|
|
1721
1153
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1722
1154
|
* @returns Dense rank (consecutive after ties: 1, 1, 2)
|
|
1723
|
-
*
|
|
1724
|
-
* @example
|
|
1725
|
-
* ```typescript
|
|
1726
|
-
* db.student().select((s) => ({
|
|
1727
|
-
* name: s.name,
|
|
1728
|
-
* denseRank: expr.denseRank({
|
|
1729
|
-
* orderBy: [[s.score, "DESC"]],
|
|
1730
|
-
* }),
|
|
1731
|
-
* }))
|
|
1732
|
-
* ```
|
|
1733
1155
|
*/
|
|
1734
1156
|
denseRank(spec: WinSpecInput): ExprUnit<number> {
|
|
1735
1157
|
return new ExprUnit("number", {
|
|
@@ -1745,17 +1167,6 @@ export const expr = {
|
|
|
1745
1167
|
* @param n - Number of groups to split into
|
|
1746
1168
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1747
1169
|
* @returns Group number (1 ~ n)
|
|
1748
|
-
*
|
|
1749
|
-
* @example
|
|
1750
|
-
* ```typescript
|
|
1751
|
-
* // Quartile split to find top 25%
|
|
1752
|
-
* db.user().select((u) => ({
|
|
1753
|
-
* name: u.name,
|
|
1754
|
-
* quartile: expr.ntile(4, {
|
|
1755
|
-
* orderBy: [[u.score, "DESC"]],
|
|
1756
|
-
* }),
|
|
1757
|
-
* }))
|
|
1758
|
-
* ```
|
|
1759
1170
|
*/
|
|
1760
1171
|
ntile(n: number, spec: WinSpecInput): ExprUnit<number> {
|
|
1761
1172
|
return new ExprUnit("number", {
|
|
@@ -1772,18 +1183,6 @@ export const expr = {
|
|
|
1772
1183
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1773
1184
|
* @param options - offset (default 1), default (default value when no previous row)
|
|
1774
1185
|
* @returns Previous row's value (or default value/NULL)
|
|
1775
|
-
*
|
|
1776
|
-
* @example
|
|
1777
|
-
* ```typescript
|
|
1778
|
-
* db.stock().select((s) => ({
|
|
1779
|
-
* date: s.date,
|
|
1780
|
-
* price: s.price,
|
|
1781
|
-
* prevPrice: expr.lag(s.price, {
|
|
1782
|
-
* partitionBy: [s.symbol],
|
|
1783
|
-
* orderBy: [[s.date, "ASC"]],
|
|
1784
|
-
* }),
|
|
1785
|
-
* }))
|
|
1786
|
-
* ```
|
|
1787
1186
|
*/
|
|
1788
1187
|
lag<T extends ColumnPrimitive>(
|
|
1789
1188
|
column: ExprUnit<T>,
|
|
@@ -1809,18 +1208,6 @@ export const expr = {
|
|
|
1809
1208
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1810
1209
|
* @param options - offset (default 1), default (default value when no following row)
|
|
1811
1210
|
* @returns Following row's value (or default value/NULL)
|
|
1812
|
-
*
|
|
1813
|
-
* @example
|
|
1814
|
-
* ```typescript
|
|
1815
|
-
* db.stock().select((s) => ({
|
|
1816
|
-
* date: s.date,
|
|
1817
|
-
* price: s.price,
|
|
1818
|
-
* nextPrice: expr.lead(s.price, {
|
|
1819
|
-
* partitionBy: [s.symbol],
|
|
1820
|
-
* orderBy: [[s.date, "ASC"]],
|
|
1821
|
-
* }),
|
|
1822
|
-
* }))
|
|
1823
|
-
* ```
|
|
1824
1211
|
*/
|
|
1825
1212
|
lead<T extends ColumnPrimitive>(
|
|
1826
1213
|
column: ExprUnit<T>,
|
|
@@ -1845,17 +1232,6 @@ export const expr = {
|
|
|
1845
1232
|
* @param column - Column to reference
|
|
1846
1233
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1847
1234
|
* @returns First value
|
|
1848
|
-
*
|
|
1849
|
-
* @example
|
|
1850
|
-
* ```typescript
|
|
1851
|
-
* db.order().select((o) => ({
|
|
1852
|
-
* ...o,
|
|
1853
|
-
* firstOrderAmount: expr.firstValue(o.amount, {
|
|
1854
|
-
* partitionBy: [o.userId],
|
|
1855
|
-
* orderBy: [[o.createdAt, "ASC"]],
|
|
1856
|
-
* }),
|
|
1857
|
-
* }))
|
|
1858
|
-
* ```
|
|
1859
1235
|
*/
|
|
1860
1236
|
firstValue<T extends ColumnPrimitive>(
|
|
1861
1237
|
column: ExprUnit<T>,
|
|
@@ -1874,17 +1250,6 @@ export const expr = {
|
|
|
1874
1250
|
* @param column - Column to reference
|
|
1875
1251
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1876
1252
|
* @returns Last value
|
|
1877
|
-
*
|
|
1878
|
-
* @example
|
|
1879
|
-
* ```typescript
|
|
1880
|
-
* db.order().select((o) => ({
|
|
1881
|
-
* ...o,
|
|
1882
|
-
* lastOrderAmount: expr.lastValue(o.amount, {
|
|
1883
|
-
* partitionBy: [o.userId],
|
|
1884
|
-
* orderBy: [[o.createdAt, "ASC"]],
|
|
1885
|
-
* }),
|
|
1886
|
-
* }))
|
|
1887
|
-
* ```
|
|
1888
1253
|
*/
|
|
1889
1254
|
lastValue<T extends ColumnPrimitive>(
|
|
1890
1255
|
column: ExprUnit<T>,
|
|
@@ -1903,18 +1268,6 @@ export const expr = {
|
|
|
1903
1268
|
* @param column - Column to sum
|
|
1904
1269
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1905
1270
|
* @returns Sum within window
|
|
1906
|
-
*
|
|
1907
|
-
* @example
|
|
1908
|
-
* ```typescript
|
|
1909
|
-
* // Running total
|
|
1910
|
-
* db.order().select((o) => ({
|
|
1911
|
-
* ...o,
|
|
1912
|
-
* runningTotal: expr.sumOver(o.amount, {
|
|
1913
|
-
* partitionBy: [o.userId],
|
|
1914
|
-
* orderBy: [[o.createdAt, "ASC"]],
|
|
1915
|
-
* }),
|
|
1916
|
-
* }))
|
|
1917
|
-
* ```
|
|
1918
1271
|
*/
|
|
1919
1272
|
sumOver(column: ExprUnit<number | undefined>, spec: WinSpecInput): ExprUnit<number | undefined> {
|
|
1920
1273
|
return new ExprUnit("number", {
|
|
@@ -1930,18 +1283,6 @@ export const expr = {
|
|
|
1930
1283
|
* @param column - Column to average
|
|
1931
1284
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1932
1285
|
* @returns Average within window
|
|
1933
|
-
*
|
|
1934
|
-
* @example
|
|
1935
|
-
* ```typescript
|
|
1936
|
-
* // Moving average
|
|
1937
|
-
* db.stock().select((s) => ({
|
|
1938
|
-
* ...s,
|
|
1939
|
-
* movingAvg: expr.avgOver(s.price, {
|
|
1940
|
-
* partitionBy: [s.symbol],
|
|
1941
|
-
* orderBy: [[s.date, "ASC"]],
|
|
1942
|
-
* }),
|
|
1943
|
-
* }))
|
|
1944
|
-
* ```
|
|
1945
1286
|
*/
|
|
1946
1287
|
avgOver(column: ExprUnit<number | undefined>, spec: WinSpecInput): ExprUnit<number | undefined> {
|
|
1947
1288
|
return new ExprUnit("number", {
|
|
@@ -1957,16 +1298,6 @@ export const expr = {
|
|
|
1957
1298
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1958
1299
|
* @param column - Column to count (all rows if omitted)
|
|
1959
1300
|
* @returns Row count within window
|
|
1960
|
-
*
|
|
1961
|
-
* @example
|
|
1962
|
-
* ```typescript
|
|
1963
|
-
* db.order().select((o) => ({
|
|
1964
|
-
* ...o,
|
|
1965
|
-
* totalOrdersPerUser: expr.countOver({
|
|
1966
|
-
* partitionBy: [o.userId],
|
|
1967
|
-
* }),
|
|
1968
|
-
* }))
|
|
1969
|
-
* ```
|
|
1970
1301
|
*/
|
|
1971
1302
|
countOver(spec: WinSpecInput, column?: ExprUnit<ColumnPrimitive>): ExprUnit<number> {
|
|
1972
1303
|
return new ExprUnit("number", {
|
|
@@ -1982,16 +1313,6 @@ export const expr = {
|
|
|
1982
1313
|
* @param column - Column to find minimum of
|
|
1983
1314
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
1984
1315
|
* @returns Minimum value within window
|
|
1985
|
-
*
|
|
1986
|
-
* @example
|
|
1987
|
-
* ```typescript
|
|
1988
|
-
* db.stock().select((s) => ({
|
|
1989
|
-
* ...s,
|
|
1990
|
-
* minPriceInPeriod: expr.minOver(s.price, {
|
|
1991
|
-
* partitionBy: [s.symbol],
|
|
1992
|
-
* }),
|
|
1993
|
-
* }))
|
|
1994
|
-
* ```
|
|
1995
1316
|
*/
|
|
1996
1317
|
minOver<T extends ColumnPrimitive>(
|
|
1997
1318
|
column: ExprUnit<T>,
|
|
@@ -2010,16 +1331,6 @@ export const expr = {
|
|
|
2010
1331
|
* @param column - Column to find maximum of
|
|
2011
1332
|
* @param spec - Window spec (partitionBy, orderBy)
|
|
2012
1333
|
* @returns Maximum value within window
|
|
2013
|
-
*
|
|
2014
|
-
* @example
|
|
2015
|
-
* ```typescript
|
|
2016
|
-
* db.stock().select((s) => ({
|
|
2017
|
-
* ...s,
|
|
2018
|
-
* maxPriceInPeriod: expr.maxOver(s.price, {
|
|
2019
|
-
* partitionBy: [s.symbol],
|
|
2020
|
-
* }),
|
|
2021
|
-
* }))
|
|
2022
|
-
* ```
|
|
2023
1334
|
*/
|
|
2024
1335
|
maxOver<T extends ColumnPrimitive>(
|
|
2025
1336
|
column: ExprUnit<T>,
|