kysely 0.24.1 → 0.25.0

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.
Files changed (132) hide show
  1. package/README.md +30 -13
  2. package/dist/cjs/dialect/mysql/mysql-dialect-config.d.ts +2 -1
  3. package/dist/cjs/dialect/postgres/postgres-introspector.js +4 -1
  4. package/dist/cjs/driver/runtime-driver.js +6 -6
  5. package/dist/cjs/dynamic/dynamic.d.ts +1 -1
  6. package/dist/cjs/dynamic/dynamic.js +1 -1
  7. package/dist/cjs/expression/expression-builder.d.ts +56 -1
  8. package/dist/cjs/expression/expression-builder.js +10 -0
  9. package/dist/cjs/helpers/mysql.d.ts +3 -3
  10. package/dist/cjs/helpers/mysql.js +2 -2
  11. package/dist/cjs/helpers/postgres.d.ts +1 -1
  12. package/dist/cjs/index.d.ts +4 -0
  13. package/dist/cjs/index.js +4 -0
  14. package/dist/cjs/kysely.d.ts +19 -2
  15. package/dist/cjs/kysely.js +28 -1
  16. package/dist/cjs/operation-node/aggregate-function-node.d.ts +3 -7
  17. package/dist/cjs/operation-node/aggregate-function-node.js +1 -1
  18. package/dist/cjs/operation-node/alter-column-node.d.ts +1 -2
  19. package/dist/cjs/operation-node/alter-table-node.d.ts +1 -1
  20. package/dist/cjs/operation-node/alter-table-node.js +1 -2
  21. package/dist/cjs/operation-node/case-node.d.ts +19 -0
  22. package/dist/cjs/operation-node/case-node.js +42 -0
  23. package/dist/cjs/operation-node/create-index-node.d.ts +2 -1
  24. package/dist/cjs/operation-node/create-index-node.js +6 -0
  25. package/dist/cjs/operation-node/data-type-node.d.ts +1 -1
  26. package/dist/cjs/operation-node/operation-node-transformer.d.ts +4 -0
  27. package/dist/cjs/operation-node/operation-node-transformer.js +20 -2
  28. package/dist/cjs/operation-node/operation-node-visitor.d.ts +4 -0
  29. package/dist/cjs/operation-node/operation-node-visitor.js +2 -0
  30. package/dist/cjs/operation-node/operation-node.d.ts +1 -1
  31. package/dist/cjs/operation-node/when-node.d.ts +14 -0
  32. package/dist/cjs/operation-node/when-node.js +24 -0
  33. package/dist/cjs/parser/binary-operation-parser.d.ts +1 -0
  34. package/dist/cjs/parser/binary-operation-parser.js +18 -2
  35. package/dist/cjs/parser/expression-parser.d.ts +2 -1
  36. package/dist/cjs/parser/order-by-parser.d.ts +1 -0
  37. package/dist/cjs/parser/order-by-parser.js +5 -1
  38. package/dist/cjs/parser/reference-parser.d.ts +6 -2
  39. package/dist/cjs/parser/reference-parser.js +16 -1
  40. package/dist/cjs/parser/select-parser.d.ts +4 -4
  41. package/dist/cjs/parser/table-parser.d.ts +5 -4
  42. package/dist/cjs/parser/with-parser.d.ts +4 -3
  43. package/dist/cjs/query-builder/case-builder.d.ts +111 -0
  44. package/dist/cjs/query-builder/case-builder.js +75 -0
  45. package/dist/cjs/query-builder/delete-query-builder.d.ts +55 -13
  46. package/dist/cjs/query-builder/delete-query-builder.js +44 -0
  47. package/dist/cjs/query-builder/function-module.d.ts +36 -5
  48. package/dist/cjs/query-builder/function-module.js +12 -16
  49. package/dist/cjs/query-builder/insert-query-builder.d.ts +44 -3
  50. package/dist/cjs/query-builder/insert-query-builder.js +49 -1
  51. package/dist/cjs/query-builder/select-query-builder.d.ts +54 -13
  52. package/dist/cjs/query-builder/select-query-builder.js +43 -0
  53. package/dist/cjs/query-builder/update-query-builder.d.ts +65 -20
  54. package/dist/cjs/query-builder/update-query-builder.js +47 -0
  55. package/dist/cjs/query-compiler/default-query-compiler.d.ts +4 -0
  56. package/dist/cjs/query-compiler/default-query-compiler.js +30 -3
  57. package/dist/cjs/schema/alter-column-builder.d.ts +2 -2
  58. package/dist/cjs/schema/alter-column-builder.js +2 -2
  59. package/dist/cjs/schema/create-index-builder.d.ts +49 -7
  60. package/dist/cjs/schema/create-index-builder.js +51 -13
  61. package/dist/cjs/schema/schema.js +1 -1
  62. package/dist/cjs/util/column-type.d.ts +3 -2
  63. package/dist/cjs/util/log.d.ts +3 -3
  64. package/dist/cjs/util/log.js +4 -4
  65. package/dist/cjs/util/object-utils.d.ts +2 -1
  66. package/dist/cjs/util/type-utils.d.ts +43 -29
  67. package/dist/esm/dialect/mysql/mysql-dialect-config.d.ts +2 -1
  68. package/dist/esm/dialect/postgres/postgres-introspector.js +4 -1
  69. package/dist/esm/driver/runtime-driver.js +6 -6
  70. package/dist/esm/dynamic/dynamic.d.ts +1 -1
  71. package/dist/esm/dynamic/dynamic.js +1 -1
  72. package/dist/esm/expression/expression-builder.d.ts +56 -1
  73. package/dist/esm/expression/expression-builder.js +11 -1
  74. package/dist/esm/helpers/mysql.d.ts +3 -3
  75. package/dist/esm/helpers/mysql.js +2 -2
  76. package/dist/esm/helpers/postgres.d.ts +1 -1
  77. package/dist/esm/index.d.ts +4 -0
  78. package/dist/esm/index.js +4 -0
  79. package/dist/esm/kysely.d.ts +19 -2
  80. package/dist/esm/kysely.js +29 -2
  81. package/dist/esm/operation-node/aggregate-function-node.d.ts +3 -7
  82. package/dist/esm/operation-node/aggregate-function-node.js +1 -1
  83. package/dist/esm/operation-node/alter-column-node.d.ts +1 -2
  84. package/dist/esm/operation-node/alter-table-node.d.ts +1 -1
  85. package/dist/esm/operation-node/alter-table-node.js +1 -2
  86. package/dist/esm/operation-node/case-node.d.ts +19 -0
  87. package/dist/esm/operation-node/case-node.js +40 -0
  88. package/dist/esm/operation-node/create-index-node.d.ts +2 -1
  89. package/dist/esm/operation-node/create-index-node.js +6 -0
  90. package/dist/esm/operation-node/data-type-node.d.ts +1 -1
  91. package/dist/esm/operation-node/operation-node-transformer.d.ts +4 -0
  92. package/dist/esm/operation-node/operation-node-transformer.js +20 -2
  93. package/dist/esm/operation-node/operation-node-visitor.d.ts +4 -0
  94. package/dist/esm/operation-node/operation-node-visitor.js +2 -0
  95. package/dist/esm/operation-node/operation-node.d.ts +1 -1
  96. package/dist/esm/operation-node/when-node.d.ts +14 -0
  97. package/dist/esm/operation-node/when-node.js +22 -0
  98. package/dist/esm/parser/binary-operation-parser.d.ts +1 -0
  99. package/dist/esm/parser/binary-operation-parser.js +16 -1
  100. package/dist/esm/parser/expression-parser.d.ts +2 -1
  101. package/dist/esm/parser/order-by-parser.d.ts +1 -0
  102. package/dist/esm/parser/order-by-parser.js +3 -0
  103. package/dist/esm/parser/reference-parser.d.ts +6 -2
  104. package/dist/esm/parser/reference-parser.js +14 -0
  105. package/dist/esm/parser/select-parser.d.ts +4 -4
  106. package/dist/esm/parser/table-parser.d.ts +5 -4
  107. package/dist/esm/parser/with-parser.d.ts +4 -3
  108. package/dist/esm/query-builder/case-builder.d.ts +111 -0
  109. package/dist/esm/query-builder/case-builder.js +69 -0
  110. package/dist/esm/query-builder/delete-query-builder.d.ts +55 -13
  111. package/dist/esm/query-builder/delete-query-builder.js +44 -0
  112. package/dist/esm/query-builder/function-module.d.ts +36 -5
  113. package/dist/esm/query-builder/function-module.js +13 -17
  114. package/dist/esm/query-builder/insert-query-builder.d.ts +44 -3
  115. package/dist/esm/query-builder/insert-query-builder.js +49 -1
  116. package/dist/esm/query-builder/select-query-builder.d.ts +54 -13
  117. package/dist/esm/query-builder/select-query-builder.js +43 -0
  118. package/dist/esm/query-builder/update-query-builder.d.ts +65 -20
  119. package/dist/esm/query-builder/update-query-builder.js +47 -0
  120. package/dist/esm/query-compiler/default-query-compiler.d.ts +4 -0
  121. package/dist/esm/query-compiler/default-query-compiler.js +30 -3
  122. package/dist/esm/schema/alter-column-builder.d.ts +2 -2
  123. package/dist/esm/schema/alter-column-builder.js +2 -2
  124. package/dist/esm/schema/create-index-builder.d.ts +49 -7
  125. package/dist/esm/schema/create-index-builder.js +52 -14
  126. package/dist/esm/schema/schema.js +1 -1
  127. package/dist/esm/util/column-type.d.ts +3 -2
  128. package/dist/esm/util/log.d.ts +3 -3
  129. package/dist/esm/util/log.js +4 -4
  130. package/dist/esm/util/object-utils.d.ts +2 -1
  131. package/dist/esm/util/type-utils.d.ts +43 -29
  132. package/package.json +1 -1
@@ -6,7 +6,7 @@ import { SelectExpression, SelectArg } from '../parser/select-parser.js';
6
6
  import { ReturningAllRow, ReturningRow } from '../parser/returning-parser.js';
7
7
  import { ReferenceExpression } from '../parser/reference-parser.js';
8
8
  import { QueryNode } from '../operation-node/query-node.js';
9
- import { MergePartial, Nullable, SimplifyResult, SimplifySingleResult } from '../util/type-utils.js';
9
+ import { DrainOuterGeneric, NarrowPartial, Nullable, ShallowRecord, SimplifyResult, SimplifySingleResult } from '../util/type-utils.js';
10
10
  import { Compilable } from '../util/compilable.js';
11
11
  import { QueryExecutor } from '../query-executor/query-executor.js';
12
12
  import { QueryId } from '../util/query-id.js';
@@ -784,11 +784,11 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
784
784
  * }
785
785
  * ```
786
786
  */
787
- $if<O2>(condition: boolean, func: (qb: this) => DeleteQueryBuilder<DB, TB, O2>): DeleteQueryBuilder<DB, TB, O2 extends DeleteResult ? DeleteResult : O extends DeleteResult ? Partial<O2> : MergePartial<O, O2>>;
787
+ $if<O2>(condition: boolean, func: (qb: this) => DeleteQueryBuilder<DB, TB, O2>): O2 extends DeleteResult ? DeleteQueryBuilder<DB, TB, DeleteResult> : O2 extends O & infer E ? DeleteQueryBuilder<DB, TB, O & Partial<E>> : DeleteQueryBuilder<DB, TB, Partial<O2>>;
788
788
  /**
789
789
  * @deprecated Use `$if` instead
790
790
  */
791
- if<O2>(condition: boolean, func: (qb: this) => DeleteQueryBuilder<DB, TB, O2>): DeleteQueryBuilder<DB, TB, O2 extends DeleteResult ? DeleteResult : O extends DeleteResult ? Partial<O2> : MergePartial<O, O2>>;
791
+ if<O2>(condition: boolean, func: (qb: this) => DeleteQueryBuilder<DB, TB, O2>): O2 extends DeleteResult ? DeleteQueryBuilder<DB, TB, DeleteResult> : O2 extends O & infer E ? DeleteQueryBuilder<DB, TB, O & Partial<E>> : DeleteQueryBuilder<DB, TB, Partial<O2>>;
792
792
  /**
793
793
  * Change the output type of the query.
794
794
  *
@@ -800,6 +800,48 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
800
800
  * @deprecated Use `$castTo` instead.
801
801
  */
802
802
  castTo<T>(): DeleteQueryBuilder<DB, TB, T>;
803
+ /**
804
+ * Narrows (parts of) the output type of the query.
805
+ *
806
+ * Kysely tries to be as type-safe as possible, but in some cases we have to make
807
+ * compromises for better maintainability and compilation performance. At present,
808
+ * Kysely doesn't narrow the output type of the query when using {@link where} and {@link returning} or {@link returningAll}.
809
+ *
810
+ * This utility method is very useful for these situations, as it removes unncessary
811
+ * runtime assertion/guard code. Its input type is limited to the output type
812
+ * of the query, so you can't add a column that doesn't exist, or change a column's
813
+ * type to something that doesn't exist in its union type.
814
+ *
815
+ * ### Examples
816
+ *
817
+ * Turn this code:
818
+ *
819
+ * ```ts
820
+ * const person = await db.deleteFrom('person')
821
+ * .where('id', '=', id)
822
+ * .where('nullable_column', 'is not', null)
823
+ * .returningAll()
824
+ * .executeTakeFirstOrThrow()
825
+ *
826
+ * if (person.nullable_column) {
827
+ * functionThatExpectsPersonWithNonNullValue(person)
828
+ * }
829
+ * ```
830
+ *
831
+ * Into this:
832
+ *
833
+ * ```ts
834
+ * const person = await db.deleteFrom('person')
835
+ * .where('id', '=', id)
836
+ * .where('nullable_column', 'is not', null)
837
+ * .returningAll()
838
+ * .$narrowType<{ nullable_column: string }>()
839
+ * .executeTakeFirstOrThrow()
840
+ *
841
+ * functionThatExpectsPersonWithNonNullValue(person)
842
+ * ```
843
+ */
844
+ $narrowType<T>(): DeleteQueryBuilder<DB, TB, NarrowPartial<O, T>>;
803
845
  /**
804
846
  * Asserts that query's output row type equals the given type `T`.
805
847
  *
@@ -942,23 +984,23 @@ export interface DeleteQueryBuilderProps {
942
984
  readonly executor: QueryExecutor;
943
985
  }
944
986
  export declare type DeleteQueryBuilderWithInnerJoin<DB, TB extends keyof DB, O, TE extends TableExpression<DB, TB>> = TE extends `${infer T} as ${infer A}` ? T extends keyof DB ? InnerJoinedBuilder<DB, TB, O, A, DB[T]> : never : TE extends keyof DB ? DeleteQueryBuilder<DB, TB | TE, O> : TE extends AliasedExpression<infer QO, infer QA> ? InnerJoinedBuilder<DB, TB, O, QA, QO> : TE extends (qb: any) => AliasedExpression<infer QO, infer QA> ? InnerJoinedBuilder<DB, TB, O, QA, QO> : never;
945
- declare type InnerJoinedBuilder<DB, TB extends keyof DB, O, A extends string, R> = A extends keyof DB ? DeleteQueryBuilder<InnerJoinedDB<DB, A, R>, TB | A, O> : DeleteQueryBuilder<DB & Record<A, R>, TB | A, O>;
946
- declare type InnerJoinedDB<DB, A extends string, R> = {
987
+ declare type InnerJoinedBuilder<DB, TB extends keyof DB, O, A extends string, R> = A extends keyof DB ? DeleteQueryBuilder<InnerJoinedDB<DB, A, R>, TB | A, O> : DeleteQueryBuilder<DB & ShallowRecord<A, R>, TB | A, O>;
988
+ declare type InnerJoinedDB<DB, A extends string, R> = DrainOuterGeneric<{
947
989
  [C in keyof DB | A]: C extends A ? R : C extends keyof DB ? DB[C] : never;
948
- };
990
+ }>;
949
991
  export declare type DeleteQueryBuilderWithLeftJoin<DB, TB extends keyof DB, O, TE extends TableExpression<DB, TB>> = TE extends `${infer T} as ${infer A}` ? T extends keyof DB ? LeftJoinedBuilder<DB, TB, O, A, DB[T]> : never : TE extends keyof DB ? LeftJoinedBuilder<DB, TB, O, TE, DB[TE]> : TE extends AliasedExpression<infer QO, infer QA> ? LeftJoinedBuilder<DB, TB, O, QA, QO> : TE extends (qb: any) => AliasedExpression<infer QO, infer QA> ? LeftJoinedBuilder<DB, TB, O, QA, QO> : never;
950
- declare type LeftJoinedBuilder<DB, TB extends keyof DB, O, A extends keyof any, R> = A extends keyof DB ? DeleteQueryBuilder<LeftJoinedDB<DB, A, R>, TB | A, O> : DeleteQueryBuilder<DB & Record<A, Nullable<R>>, TB | A, O>;
951
- declare type LeftJoinedDB<DB, A extends keyof any, R> = {
992
+ declare type LeftJoinedBuilder<DB, TB extends keyof DB, O, A extends keyof any, R> = A extends keyof DB ? DeleteQueryBuilder<LeftJoinedDB<DB, A, R>, TB | A, O> : DeleteQueryBuilder<DB & ShallowRecord<A, Nullable<R>>, TB | A, O>;
993
+ declare type LeftJoinedDB<DB, A extends keyof any, R> = DrainOuterGeneric<{
952
994
  [C in keyof DB | A]: C extends A ? Nullable<R> : C extends keyof DB ? DB[C] : never;
953
- };
995
+ }>;
954
996
  export declare type DeleteQueryBuilderWithRightJoin<DB, TB extends keyof DB, O, TE extends TableExpression<DB, TB>> = TE extends `${infer T} as ${infer A}` ? T extends keyof DB ? RightJoinedBuilder<DB, TB, O, A, DB[T]> : never : TE extends keyof DB ? RightJoinedBuilder<DB, TB, O, TE, DB[TE]> : TE extends AliasedExpression<infer QO, infer QA> ? RightJoinedBuilder<DB, TB, O, QA, QO> : TE extends (qb: any) => AliasedExpression<infer QO, infer QA> ? RightJoinedBuilder<DB, TB, O, QA, QO> : never;
955
997
  declare type RightJoinedBuilder<DB, TB extends keyof DB, O, A extends keyof any, R> = DeleteQueryBuilder<RightJoinedDB<DB, TB, A, R>, TB | A, O>;
956
- declare type RightJoinedDB<DB, TB extends keyof DB, A extends keyof any, R> = {
998
+ declare type RightJoinedDB<DB, TB extends keyof DB, A extends keyof any, R> = DrainOuterGeneric<{
957
999
  [C in keyof DB | A]: C extends A ? R : C extends TB ? Nullable<DB[C]> : C extends keyof DB ? DB[C] : never;
958
- };
1000
+ }>;
959
1001
  export declare type DeleteQueryBuilderWithFullJoin<DB, TB extends keyof DB, O, TE extends TableExpression<DB, TB>> = TE extends `${infer T} as ${infer A}` ? T extends keyof DB ? OuterJoinedBuilder<DB, TB, O, A, DB[T]> : never : TE extends keyof DB ? OuterJoinedBuilder<DB, TB, O, TE, DB[TE]> : TE extends AliasedExpression<infer QO, infer QA> ? OuterJoinedBuilder<DB, TB, O, QA, QO> : TE extends (qb: any) => AliasedExpression<infer QO, infer QA> ? OuterJoinedBuilder<DB, TB, O, QA, QO> : never;
960
1002
  declare type OuterJoinedBuilder<DB, TB extends keyof DB, O, A extends keyof any, R> = DeleteQueryBuilder<OuterJoinedBuilderDB<DB, TB, A, R>, TB | A, O>;
961
- declare type OuterJoinedBuilderDB<DB, TB extends keyof DB, A extends keyof any, R> = {
1003
+ declare type OuterJoinedBuilderDB<DB, TB extends keyof DB, A extends keyof any, R> = DrainOuterGeneric<{
962
1004
  [C in keyof DB | A]: C extends A ? Nullable<R> : C extends TB ? Nullable<DB[C]> : C extends keyof DB ? DB[C] : never;
963
- };
1005
+ }>;
964
1006
  export {};
@@ -274,6 +274,50 @@ class DeleteQueryBuilder {
274
274
  castTo() {
275
275
  return this.$castTo();
276
276
  }
277
+ /**
278
+ * Narrows (parts of) the output type of the query.
279
+ *
280
+ * Kysely tries to be as type-safe as possible, but in some cases we have to make
281
+ * compromises for better maintainability and compilation performance. At present,
282
+ * Kysely doesn't narrow the output type of the query when using {@link where} and {@link returning} or {@link returningAll}.
283
+ *
284
+ * This utility method is very useful for these situations, as it removes unncessary
285
+ * runtime assertion/guard code. Its input type is limited to the output type
286
+ * of the query, so you can't add a column that doesn't exist, or change a column's
287
+ * type to something that doesn't exist in its union type.
288
+ *
289
+ * ### Examples
290
+ *
291
+ * Turn this code:
292
+ *
293
+ * ```ts
294
+ * const person = await db.deleteFrom('person')
295
+ * .where('id', '=', id)
296
+ * .where('nullable_column', 'is not', null)
297
+ * .returningAll()
298
+ * .executeTakeFirstOrThrow()
299
+ *
300
+ * if (person.nullable_column) {
301
+ * functionThatExpectsPersonWithNonNullValue(person)
302
+ * }
303
+ * ```
304
+ *
305
+ * Into this:
306
+ *
307
+ * ```ts
308
+ * const person = await db.deleteFrom('person')
309
+ * .where('id', '=', id)
310
+ * .where('nullable_column', 'is not', null)
311
+ * .returningAll()
312
+ * .$narrowType<{ nullable_column: string }>()
313
+ * .executeTakeFirstOrThrow()
314
+ *
315
+ * functionThatExpectsPersonWithNonNullValue(person)
316
+ * ```
317
+ */
318
+ $narrowType() {
319
+ return new DeleteQueryBuilder(this.#props);
320
+ }
277
321
  /**
278
322
  * Asserts that query's output row type equals the given type `T`.
279
323
  *
@@ -2,7 +2,7 @@ import { DynamicReferenceBuilder } from '../dynamic/dynamic-reference-builder.js
2
2
  import { ExpressionWrapper } from '../expression/expression-wrapper.js';
3
3
  import { CoalesceReferenceExpressionList } from '../parser/coalesce-parser.js';
4
4
  import { ExtractTypeFromReferenceExpression, SimpleReferenceExpression, ReferenceExpression, StringReference } from '../parser/reference-parser.js';
5
- import { Equals, IsNever } from '../util/type-utils.js';
5
+ import { Equals, IsAny } from '../util/type-utils.js';
6
6
  import { AggregateFunctionBuilder } from './aggregate-function-builder.js';
7
7
  /**
8
8
  * Helpers for type safe SQL function calls.
@@ -40,6 +40,8 @@ export interface FunctionModule<DB, TB extends keyof DB> {
40
40
  /**
41
41
  * Creates a function call.
42
42
  *
43
+ * To create an aggregate function call, use {@link FunctionModule.agg}.
44
+ *
43
45
  * ### Examples
44
46
  *
45
47
  * ```ts
@@ -65,6 +67,35 @@ export interface FunctionModule<DB, TB extends keyof DB> {
65
67
  * ```
66
68
  */
67
69
  <T>(name: string, args: ReadonlyArray<ReferenceExpression<DB, TB>>): ExpressionWrapper<T>;
70
+ /**
71
+ * Creates an aggregate function call.
72
+ *
73
+ * This is a specialized version of the `fn` method, that returns an {@link AggregateFunctionBuilder}
74
+ * instance. A builder that allows you to chain additional methods such as `distinct`,
75
+ * `filterWhere` and `over`.
76
+ *
77
+ * See {@link avg}, {@link count}, {@link countAll}, {@link max}, {@link min}, {@link sum}
78
+ * shortcuts of common aggregate functions.
79
+ *
80
+ * ### Examples
81
+ *
82
+ * ```ts
83
+ * db.selectFrom('person')
84
+ * .select(({ fn }) => [
85
+ * fn.agg<number>('rank').over().as('rank'),
86
+ * fn.agg<string>('group_concat', ['first_name']).distinct().as('first_names')
87
+ * ])
88
+ * ```
89
+ *
90
+ * The generated SQL (MySQL):
91
+ *
92
+ * ```sql
93
+ * select rank() over() as "rank",
94
+ * group_concat(distinct "first_name") as "first_names"
95
+ * from "person"
96
+ * ```
97
+ */
98
+ agg<O>(name: string, args?: ReadonlyArray<ReferenceExpression<DB, TB>>): AggregateFunctionBuilder<DB, TB, O>;
68
99
  /**
69
100
  * Calls the `avg` function for the column given as the argument.
70
101
  *
@@ -377,7 +408,7 @@ export interface FunctionModule<DB, TB extends keyof DB> {
377
408
  * .execute()
378
409
  * ```
379
410
  */
380
- max<O extends number | string | bigint | null = never, C extends StringReference<DB, TB> = StringReference<DB, TB>>(column: OutputBoundStringReference<DB, TB, C, O>): StringReferenceBoundAggregateFunctionBuilder<DB, TB, C, O>;
411
+ max<O extends number | string | bigint | null = any, C extends StringReference<DB, TB> = StringReference<DB, TB>>(column: OutputBoundStringReference<DB, TB, C, O>): StringReferenceBoundAggregateFunctionBuilder<DB, TB, C, O>;
381
412
  max<O extends number | string | bigint | null = number | string | bigint>(column: DynamicReferenceBuilder): AggregateFunctionBuilder<DB, TB, O>;
382
413
  /**
383
414
  * Calls the `min` function for the column given as the argument.
@@ -429,7 +460,7 @@ export interface FunctionModule<DB, TB extends keyof DB> {
429
460
  * .execute()
430
461
  * ```
431
462
  */
432
- min<O extends number | string | bigint | null = never, C extends StringReference<DB, TB> = StringReference<DB, TB>>(column: OutputBoundStringReference<DB, TB, C, O>): StringReferenceBoundAggregateFunctionBuilder<DB, TB, C, O>;
463
+ min<O extends number | string | bigint | null = any, C extends StringReference<DB, TB> = StringReference<DB, TB>>(column: OutputBoundStringReference<DB, TB, C, O>): StringReferenceBoundAggregateFunctionBuilder<DB, TB, C, O>;
433
464
  min<O extends number | string | bigint | null = number | string | bigint>(column: DynamicReferenceBuilder): AggregateFunctionBuilder<DB, TB, O>;
434
465
  /**
435
466
  * Calls the `sum` function for the column given as the argument.
@@ -498,6 +529,6 @@ export interface FunctionModule<DB, TB extends keyof DB> {
498
529
  sum<O extends number | string | bigint | null = number | string | bigint, C extends SimpleReferenceExpression<DB, TB> = SimpleReferenceExpression<DB, TB>>(column: C): AggregateFunctionBuilder<DB, TB, O>;
499
530
  }
500
531
  export declare function createFunctionModule<DB, TB extends keyof DB>(): FunctionModule<DB, TB>;
501
- declare type OutputBoundStringReference<DB, TB extends keyof DB, C extends StringReference<DB, TB>, O> = IsNever<O> extends true ? C : Equals<ExtractTypeFromReferenceExpression<DB, TB, C> | null, O | null> extends true ? C : never;
502
- declare type StringReferenceBoundAggregateFunctionBuilder<DB, TB extends keyof DB, C extends StringReference<DB, TB>, O> = AggregateFunctionBuilder<DB, TB, ExtractTypeFromReferenceExpression<DB, TB, C> | (null extends O ? null : never)>;
532
+ declare type OutputBoundStringReference<DB, TB extends keyof DB, C extends StringReference<DB, TB>, O> = IsAny<O> extends true ? C : Equals<ExtractTypeFromReferenceExpression<DB, TB, C> | null, O | null> extends true ? C : never;
533
+ declare type StringReferenceBoundAggregateFunctionBuilder<DB, TB extends keyof DB, C extends StringReference<DB, TB>, O> = AggregateFunctionBuilder<DB, TB, ExtractTypeFromReferenceExpression<DB, TB, C> | (IsAny<O> extends true ? never : null extends O ? null : never)>;
503
534
  export {};
@@ -11,39 +11,35 @@ function createFunctionModule() {
11
11
  const fn = (name, args) => {
12
12
  return new expression_wrapper_js_1.ExpressionWrapper(function_node_js_1.FunctionNode.create(name, (0, reference_parser_js_1.parseReferenceExpressionOrList)(args)));
13
13
  };
14
+ const agg = (name, args) => {
15
+ return new aggregate_function_builder_js_1.AggregateFunctionBuilder({
16
+ aggregateFunctionNode: aggregate_function_node_js_1.AggregateFunctionNode.create(name, args ? (0, reference_parser_js_1.parseReferenceExpressionOrList)(args) : undefined),
17
+ });
18
+ };
14
19
  return Object.assign(fn, {
20
+ agg,
15
21
  avg(column) {
16
- return new aggregate_function_builder_js_1.AggregateFunctionBuilder({
17
- aggregateFunctionNode: aggregate_function_node_js_1.AggregateFunctionNode.create('avg', (0, reference_parser_js_1.parseSimpleReferenceExpression)(column)),
18
- });
22
+ return agg('avg', [column]);
19
23
  },
20
24
  coalesce(value, ...otherValues) {
21
25
  return fn('coalesce', [value, ...otherValues]);
22
26
  },
23
27
  count(column) {
24
- return new aggregate_function_builder_js_1.AggregateFunctionBuilder({
25
- aggregateFunctionNode: aggregate_function_node_js_1.AggregateFunctionNode.create('count', (0, reference_parser_js_1.parseSimpleReferenceExpression)(column)),
26
- });
28
+ return agg('count', [column]);
27
29
  },
28
30
  countAll(table) {
29
31
  return new aggregate_function_builder_js_1.AggregateFunctionBuilder({
30
- aggregateFunctionNode: aggregate_function_node_js_1.AggregateFunctionNode.create('count', (0, select_parser_js_1.parseSelectAll)(table)[0]),
32
+ aggregateFunctionNode: aggregate_function_node_js_1.AggregateFunctionNode.create('count', (0, select_parser_js_1.parseSelectAll)(table)),
31
33
  });
32
34
  },
33
35
  max(column) {
34
- return new aggregate_function_builder_js_1.AggregateFunctionBuilder({
35
- aggregateFunctionNode: aggregate_function_node_js_1.AggregateFunctionNode.create('max', (0, reference_parser_js_1.parseSimpleReferenceExpression)(column)),
36
- });
36
+ return agg('max', [column]);
37
37
  },
38
38
  min(column) {
39
- return new aggregate_function_builder_js_1.AggregateFunctionBuilder({
40
- aggregateFunctionNode: aggregate_function_node_js_1.AggregateFunctionNode.create('min', (0, reference_parser_js_1.parseSimpleReferenceExpression)(column)),
41
- });
39
+ return agg('min', [column]);
42
40
  },
43
41
  sum(column) {
44
- return new aggregate_function_builder_js_1.AggregateFunctionBuilder({
45
- aggregateFunctionNode: aggregate_function_node_js_1.AggregateFunctionNode.create('sum', (0, reference_parser_js_1.parseSimpleReferenceExpression)(column)),
46
- });
42
+ return agg('sum', [column]);
47
43
  },
48
44
  });
49
45
  }
@@ -4,7 +4,7 @@ import { SelectExpression, SelectArg } from '../parser/select-parser.js';
4
4
  import { InsertObjectOrList, InsertObjectOrListFactory } from '../parser/insert-values-parser.js';
5
5
  import { InsertQueryNode } from '../operation-node/insert-query-node.js';
6
6
  import { QueryNode } from '../operation-node/query-node.js';
7
- import { MergePartial, SimplifyResult, SimplifySingleResult } from '../util/type-utils.js';
7
+ import { NarrowPartial, SimplifyResult, SimplifySingleResult } from '../util/type-utils.js';
8
8
  import { UpdateExpression } from '../parser/update-set-parser.js';
9
9
  import { Compilable } from '../util/compilable.js';
10
10
  import { QueryExecutor } from '../query-executor/query-executor.js';
@@ -516,11 +516,11 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
516
516
  * }
517
517
  * ```
518
518
  */
519
- $if<O2>(condition: boolean, func: (qb: this) => InsertQueryBuilder<DB, TB, O2>): InsertQueryBuilder<DB, TB, O2 extends InsertResult ? InsertResult : O extends InsertResult ? Partial<O2> : MergePartial<O, O2>>;
519
+ $if<O2>(condition: boolean, func: (qb: this) => InsertQueryBuilder<DB, TB, O2>): O2 extends InsertResult ? InsertQueryBuilder<DB, TB, InsertResult> : O2 extends O & infer E ? InsertQueryBuilder<DB, TB, O & Partial<E>> : InsertQueryBuilder<DB, TB, Partial<O2>>;
520
520
  /**
521
521
  * @deprecated Use `$if` instead
522
522
  */
523
- if<O2>(condition: boolean, func: (qb: this) => InsertQueryBuilder<DB, TB, O2>): InsertQueryBuilder<DB, TB, O2 extends InsertResult ? InsertResult : O extends InsertResult ? Partial<O2> : MergePartial<O, O2>>;
523
+ if<O2>(condition: boolean, func: (qb: this) => InsertQueryBuilder<DB, TB, O2>): O2 extends InsertResult ? InsertQueryBuilder<DB, TB, InsertResult> : O2 extends O & infer E ? InsertQueryBuilder<DB, TB, O & Partial<E>> : InsertQueryBuilder<DB, TB, Partial<O2>>;
524
524
  /**
525
525
  * Change the output type of the query.
526
526
  *
@@ -532,6 +532,47 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
532
532
  * @deprecated Use `$castTo` instead.
533
533
  */
534
534
  castTo<T>(): InsertQueryBuilder<DB, TB, T>;
535
+ /**
536
+ * Narrows (parts of) the output type of the query.
537
+ *
538
+ * Kysely tries to be as type-safe as possible, but in some cases we have to make
539
+ * compromises for better maintainability and compilation performance. At present,
540
+ * Kysely doesn't narrow the output type of the query based on {@link values} input
541
+ * when using {@link returning} or {@link returningAll}.
542
+ *
543
+ * This utility method is very useful for these situations, as it removes unncessary
544
+ * runtime assertion/guard code. Its input type is limited to the output type
545
+ * of the query, so you can't add a column that doesn't exist, or change a column's
546
+ * type to something that doesn't exist in its union type.
547
+ *
548
+ * ### Examples
549
+ *
550
+ * Turn this code:
551
+ *
552
+ * ```ts
553
+ * const person = await db.insertInto('person')
554
+ * .values({ ...inputPerson, nullable_column: 'hell yeah!' })
555
+ * .returningAll()
556
+ * .executeTakeFirstOrThrow()
557
+ *
558
+ * if (nullable_column) {
559
+ * functionThatExpectsPersonWithNonNullValue(person)
560
+ * }
561
+ * ```
562
+ *
563
+ * Into this:
564
+ *
565
+ * ```ts
566
+ * const person = await db.insertInto('person')
567
+ * .values({ ...inputPerson, nullable_column: 'hell yeah!' })
568
+ * .returningAll()
569
+ * .$narrowType<{ nullable_column: string }>()
570
+ * .executeTakeFirstOrThrow()
571
+ *
572
+ * functionThatExpectsPersonWithNonNullValue(person)
573
+ * ```
574
+ */
575
+ $narrowType<T>(): InsertQueryBuilder<DB, TB, NarrowPartial<O, T>>;
535
576
  /**
536
577
  * Asserts that query's output row type equals the given type `T`.
537
578
  *
@@ -393,7 +393,12 @@ class InsertQueryBuilder {
393
393
  * @deprecated Use `$if` instead
394
394
  */
395
395
  if(condition, func) {
396
- return this.$if(condition, func);
396
+ if (condition) {
397
+ return func(this);
398
+ }
399
+ return new InsertQueryBuilder({
400
+ ...this.#props,
401
+ });
397
402
  }
398
403
  /**
399
404
  * Change the output type of the query.
@@ -410,6 +415,49 @@ class InsertQueryBuilder {
410
415
  castTo() {
411
416
  return this.$castTo();
412
417
  }
418
+ /**
419
+ * Narrows (parts of) the output type of the query.
420
+ *
421
+ * Kysely tries to be as type-safe as possible, but in some cases we have to make
422
+ * compromises for better maintainability and compilation performance. At present,
423
+ * Kysely doesn't narrow the output type of the query based on {@link values} input
424
+ * when using {@link returning} or {@link returningAll}.
425
+ *
426
+ * This utility method is very useful for these situations, as it removes unncessary
427
+ * runtime assertion/guard code. Its input type is limited to the output type
428
+ * of the query, so you can't add a column that doesn't exist, or change a column's
429
+ * type to something that doesn't exist in its union type.
430
+ *
431
+ * ### Examples
432
+ *
433
+ * Turn this code:
434
+ *
435
+ * ```ts
436
+ * const person = await db.insertInto('person')
437
+ * .values({ ...inputPerson, nullable_column: 'hell yeah!' })
438
+ * .returningAll()
439
+ * .executeTakeFirstOrThrow()
440
+ *
441
+ * if (nullable_column) {
442
+ * functionThatExpectsPersonWithNonNullValue(person)
443
+ * }
444
+ * ```
445
+ *
446
+ * Into this:
447
+ *
448
+ * ```ts
449
+ * const person = await db.insertInto('person')
450
+ * .values({ ...inputPerson, nullable_column: 'hell yeah!' })
451
+ * .returningAll()
452
+ * .$narrowType<{ nullable_column: string }>()
453
+ * .executeTakeFirstOrThrow()
454
+ *
455
+ * functionThatExpectsPersonWithNonNullValue(person)
456
+ * ```
457
+ */
458
+ $narrowType() {
459
+ return new InsertQueryBuilder(this.#props);
460
+ }
413
461
  /**
414
462
  * Asserts that query's output row type equals the given type `T`.
415
463
  *
@@ -6,7 +6,7 @@ import { SelectExpression, Selection, SelectArg, AllSelection } from '../parser/
6
6
  import { ReferenceExpression } from '../parser/reference-parser.js';
7
7
  import { SelectQueryNode } from '../operation-node/select-query-node.js';
8
8
  import { QueryNode } from '../operation-node/query-node.js';
9
- import { MergePartial, Nullable, Simplify, SimplifySingleResult } from '../util/type-utils.js';
9
+ import { DrainOuterGeneric, NarrowPartial, Nullable, ShallowRecord, Simplify, SimplifySingleResult } from '../util/type-utils.js';
10
10
  import { OrderByDirectionExpression, OrderByExpression } from '../parser/order-by-parser.js';
11
11
  import { Compilable } from '../util/compilable.js';
12
12
  import { QueryExecutor } from '../query-executor/query-executor.js';
@@ -1397,11 +1397,11 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
1397
1397
  * )
1398
1398
  * ```
1399
1399
  */
1400
- $if<O2 extends O>(condition: boolean, func: (qb: this) => SelectQueryBuilder<DB, TB, O2>): SelectQueryBuilder<DB, TB, MergePartial<O, O2>>;
1400
+ $if<O2>(condition: boolean, func: (qb: this) => SelectQueryBuilder<DB, TB, O & O2>): SelectQueryBuilder<DB, TB, O & Partial<O2>>;
1401
1401
  /**
1402
1402
  * @deprecated Use `$if` instead
1403
1403
  */
1404
- if<O2 extends O>(condition: boolean, func: (qb: this) => SelectQueryBuilder<DB, TB, O2>): SelectQueryBuilder<DB, TB, MergePartial<O, O2>>;
1404
+ if<O2>(condition: boolean, func: (qb: this) => SelectQueryBuilder<DB, TB, O & O2>): SelectQueryBuilder<DB, TB, O & Partial<O2>>;
1405
1405
  /**
1406
1406
  * Change the output type of the query.
1407
1407
  *
@@ -1413,6 +1413,47 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
1413
1413
  * @deprecated Use `$castTo` instead.
1414
1414
  */
1415
1415
  castTo<T>(): SelectQueryBuilder<DB, TB, T>;
1416
+ /**
1417
+ * Narrows (parts of) the output type of the query.
1418
+ *
1419
+ * Kysely tries to be as type-safe as possible, but in some cases we have to make
1420
+ * compromises for better maintainability and compilation performance. At present,
1421
+ * Kysely doesn't narrow the output type of the query when using {@link where}, {@link having}
1422
+ * or {@link JoinQueryBuilder.on}.
1423
+ *
1424
+ * This utility method is very useful for these situations, as it removes unncessary
1425
+ * runtime assertion/guard code. Its input type is limited to the output type
1426
+ * of the query, so you can't add a column that doesn't exist, or change a column's
1427
+ * type to something that doesn't exist in its union type.
1428
+ *
1429
+ * ### Examples
1430
+ *
1431
+ * Turn this code:
1432
+ *
1433
+ * ```ts
1434
+ * const person = await db.selectFrom('person')
1435
+ * .where('nullable_column', 'is not', null)
1436
+ * .selectAll()
1437
+ * .executeTakeFirstOrThrow()
1438
+ *
1439
+ * if (person.nullable_column) {
1440
+ * functionThatExpectsPersonWithNonNullValue(person)
1441
+ * }
1442
+ * ```
1443
+ *
1444
+ * Into this:
1445
+ *
1446
+ * ```ts
1447
+ * const person = await db.selectFrom('person')
1448
+ * .where('nullable_column', 'is not', null)
1449
+ * .selectAll()
1450
+ * .$narrowType<{ nullable_column: string }>()
1451
+ * .executeTakeFirstOrThrow()
1452
+ *
1453
+ * functionThatExpectsPersonWithNonNullValue(person)
1454
+ * ```
1455
+ */
1456
+ $narrowType<T>(): SelectQueryBuilder<DB, TB, NarrowPartial<O, T>>;
1416
1457
  /**
1417
1458
  * Asserts that query's output row type equals the given type `T`.
1418
1459
  *
@@ -1588,23 +1629,23 @@ export declare class AliasedSelectQueryBuilder<DB, TB extends keyof DB, O = unde
1588
1629
  toOperationNode(): AliasNode;
1589
1630
  }
1590
1631
  export declare type SelectQueryBuilderWithInnerJoin<DB, TB extends keyof DB, O, TE extends TableExpression<DB, TB>> = TE extends `${infer T} as ${infer A}` ? T extends keyof DB ? InnerJoinedBuilder<DB, TB, O, A, DB[T]> : never : TE extends keyof DB ? SelectQueryBuilder<DB, TB | TE, O> : TE extends AliasedExpression<infer QO, infer QA> ? InnerJoinedBuilder<DB, TB, O, QA, QO> : TE extends (qb: any) => AliasedExpression<infer QO, infer QA> ? InnerJoinedBuilder<DB, TB, O, QA, QO> : never;
1591
- declare type InnerJoinedBuilder<DB, TB extends keyof DB, O, A extends string, R> = A extends keyof DB ? SelectQueryBuilder<InnerJoinedDB<DB, A, R>, TB | A, O> : SelectQueryBuilder<DB & Record<A, R>, TB | A, O>;
1592
- declare type InnerJoinedDB<DB, A extends string, R> = {
1632
+ declare type InnerJoinedBuilder<DB, TB extends keyof DB, O, A extends string, R> = A extends keyof DB ? SelectQueryBuilder<InnerJoinedDB<DB, A, R>, TB | A, O> : SelectQueryBuilder<DB & ShallowRecord<A, R>, TB | A, O>;
1633
+ declare type InnerJoinedDB<DB, A extends string, R> = DrainOuterGeneric<{
1593
1634
  [C in keyof DB | A]: C extends A ? R : C extends keyof DB ? DB[C] : never;
1594
- };
1635
+ }>;
1595
1636
  export declare type SelectQueryBuilderWithLeftJoin<DB, TB extends keyof DB, O, TE extends TableExpression<DB, TB>> = TE extends `${infer T} as ${infer A}` ? T extends keyof DB ? LeftJoinedBuilder<DB, TB, O, A, DB[T]> : never : TE extends keyof DB ? LeftJoinedBuilder<DB, TB, O, TE, DB[TE]> : TE extends AliasedExpression<infer QO, infer QA> ? LeftJoinedBuilder<DB, TB, O, QA, QO> : TE extends (qb: any) => AliasedExpression<infer QO, infer QA> ? LeftJoinedBuilder<DB, TB, O, QA, QO> : never;
1596
- declare type LeftJoinedBuilder<DB, TB extends keyof DB, O, A extends keyof any, R> = A extends keyof DB ? SelectQueryBuilder<LeftJoinedDB<DB, A, R>, TB | A, O> : SelectQueryBuilder<DB & Record<A, Nullable<R>>, TB | A, O>;
1597
- declare type LeftJoinedDB<DB, A extends keyof any, R> = {
1637
+ declare type LeftJoinedBuilder<DB, TB extends keyof DB, O, A extends keyof any, R> = A extends keyof DB ? SelectQueryBuilder<LeftJoinedDB<DB, A, R>, TB | A, O> : SelectQueryBuilder<DB & ShallowRecord<A, Nullable<R>>, TB | A, O>;
1638
+ declare type LeftJoinedDB<DB, A extends keyof any, R> = DrainOuterGeneric<{
1598
1639
  [C in keyof DB | A]: C extends A ? Nullable<R> : C extends keyof DB ? DB[C] : never;
1599
- };
1640
+ }>;
1600
1641
  export declare type SelectQueryBuilderWithRightJoin<DB, TB extends keyof DB, O, TE extends TableExpression<DB, TB>> = TE extends `${infer T} as ${infer A}` ? T extends keyof DB ? RightJoinedBuilder<DB, TB, O, A, DB[T]> : never : TE extends keyof DB ? RightJoinedBuilder<DB, TB, O, TE, DB[TE]> : TE extends AliasedExpression<infer QO, infer QA> ? RightJoinedBuilder<DB, TB, O, QA, QO> : TE extends (qb: any) => AliasedExpression<infer QO, infer QA> ? RightJoinedBuilder<DB, TB, O, QA, QO> : never;
1601
1642
  declare type RightJoinedBuilder<DB, TB extends keyof DB, O, A extends keyof any, R> = SelectQueryBuilder<RightJoinedDB<DB, TB, A, R>, TB | A, O>;
1602
- declare type RightJoinedDB<DB, TB extends keyof DB, A extends keyof any, R> = {
1643
+ declare type RightJoinedDB<DB, TB extends keyof DB, A extends keyof any, R> = DrainOuterGeneric<{
1603
1644
  [C in keyof DB | A]: C extends A ? R : C extends TB ? Nullable<DB[C]> : C extends keyof DB ? DB[C] : never;
1604
- };
1645
+ }>;
1605
1646
  export declare type SelectQueryBuilderWithFullJoin<DB, TB extends keyof DB, O, TE extends TableExpression<DB, TB>> = TE extends `${infer T} as ${infer A}` ? T extends keyof DB ? OuterJoinedBuilder<DB, TB, O, A, DB[T]> : never : TE extends keyof DB ? OuterJoinedBuilder<DB, TB, O, TE, DB[TE]> : TE extends AliasedExpression<infer QO, infer QA> ? OuterJoinedBuilder<DB, TB, O, QA, QO> : TE extends (qb: any) => AliasedExpression<infer QO, infer QA> ? OuterJoinedBuilder<DB, TB, O, QA, QO> : never;
1606
1647
  declare type OuterJoinedBuilder<DB, TB extends keyof DB, O, A extends keyof any, R> = SelectQueryBuilder<OuterJoinedBuilderDB<DB, TB, A, R>, TB | A, O>;
1607
- declare type OuterJoinedBuilderDB<DB, TB extends keyof DB, A extends keyof any, R> = {
1648
+ declare type OuterJoinedBuilderDB<DB, TB extends keyof DB, A extends keyof any, R> = DrainOuterGeneric<{
1608
1649
  [C in keyof DB | A]: C extends A ? Nullable<R> : C extends TB ? Nullable<DB[C]> : C extends keyof DB ? DB[C] : never;
1609
- };
1650
+ }>;
1610
1651
  export {};
@@ -1129,6 +1129,49 @@ class SelectQueryBuilder {
1129
1129
  castTo() {
1130
1130
  return this.$castTo();
1131
1131
  }
1132
+ /**
1133
+ * Narrows (parts of) the output type of the query.
1134
+ *
1135
+ * Kysely tries to be as type-safe as possible, but in some cases we have to make
1136
+ * compromises for better maintainability and compilation performance. At present,
1137
+ * Kysely doesn't narrow the output type of the query when using {@link where}, {@link having}
1138
+ * or {@link JoinQueryBuilder.on}.
1139
+ *
1140
+ * This utility method is very useful for these situations, as it removes unncessary
1141
+ * runtime assertion/guard code. Its input type is limited to the output type
1142
+ * of the query, so you can't add a column that doesn't exist, or change a column's
1143
+ * type to something that doesn't exist in its union type.
1144
+ *
1145
+ * ### Examples
1146
+ *
1147
+ * Turn this code:
1148
+ *
1149
+ * ```ts
1150
+ * const person = await db.selectFrom('person')
1151
+ * .where('nullable_column', 'is not', null)
1152
+ * .selectAll()
1153
+ * .executeTakeFirstOrThrow()
1154
+ *
1155
+ * if (person.nullable_column) {
1156
+ * functionThatExpectsPersonWithNonNullValue(person)
1157
+ * }
1158
+ * ```
1159
+ *
1160
+ * Into this:
1161
+ *
1162
+ * ```ts
1163
+ * const person = await db.selectFrom('person')
1164
+ * .where('nullable_column', 'is not', null)
1165
+ * .selectAll()
1166
+ * .$narrowType<{ nullable_column: string }>()
1167
+ * .executeTakeFirstOrThrow()
1168
+ *
1169
+ * functionThatExpectsPersonWithNonNullValue(person)
1170
+ * ```
1171
+ */
1172
+ $narrowType() {
1173
+ return new SelectQueryBuilder(this.#props);
1174
+ }
1132
1175
  /**
1133
1176
  * Asserts that query's output row type equals the given type `T`.
1134
1177
  *