kysely 0.24.2 → 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 {};
@@ -272,6 +272,50 @@ export class DeleteQueryBuilder {
272
272
  castTo() {
273
273
  return this.$castTo();
274
274
  }
275
+ /**
276
+ * Narrows (parts of) the output type of the query.
277
+ *
278
+ * Kysely tries to be as type-safe as possible, but in some cases we have to make
279
+ * compromises for better maintainability and compilation performance. At present,
280
+ * Kysely doesn't narrow the output type of the query when using {@link where} and {@link returning} or {@link returningAll}.
281
+ *
282
+ * This utility method is very useful for these situations, as it removes unncessary
283
+ * runtime assertion/guard code. Its input type is limited to the output type
284
+ * of the query, so you can't add a column that doesn't exist, or change a column's
285
+ * type to something that doesn't exist in its union type.
286
+ *
287
+ * ### Examples
288
+ *
289
+ * Turn this code:
290
+ *
291
+ * ```ts
292
+ * const person = await db.deleteFrom('person')
293
+ * .where('id', '=', id)
294
+ * .where('nullable_column', 'is not', null)
295
+ * .returningAll()
296
+ * .executeTakeFirstOrThrow()
297
+ *
298
+ * if (person.nullable_column) {
299
+ * functionThatExpectsPersonWithNonNullValue(person)
300
+ * }
301
+ * ```
302
+ *
303
+ * Into this:
304
+ *
305
+ * ```ts
306
+ * const person = await db.deleteFrom('person')
307
+ * .where('id', '=', id)
308
+ * .where('nullable_column', 'is not', null)
309
+ * .returningAll()
310
+ * .$narrowType<{ nullable_column: string }>()
311
+ * .executeTakeFirstOrThrow()
312
+ *
313
+ * functionThatExpectsPersonWithNonNullValue(person)
314
+ * ```
315
+ */
316
+ $narrowType() {
317
+ return new DeleteQueryBuilder(this.#props);
318
+ }
275
319
  /**
276
320
  * Asserts that query's output row type equals the given type `T`.
277
321
  *
@@ -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 {};
@@ -2,46 +2,42 @@
2
2
  import { ExpressionWrapper } from '../expression/expression-wrapper.js';
3
3
  import { AggregateFunctionNode } from '../operation-node/aggregate-function-node.js';
4
4
  import { FunctionNode } from '../operation-node/function-node.js';
5
- import { parseSimpleReferenceExpression, parseReferenceExpressionOrList, } from '../parser/reference-parser.js';
5
+ import { parseReferenceExpressionOrList, } from '../parser/reference-parser.js';
6
6
  import { parseSelectAll } from '../parser/select-parser.js';
7
7
  import { AggregateFunctionBuilder } from './aggregate-function-builder.js';
8
8
  export function createFunctionModule() {
9
9
  const fn = (name, args) => {
10
10
  return new ExpressionWrapper(FunctionNode.create(name, parseReferenceExpressionOrList(args)));
11
11
  };
12
+ const agg = (name, args) => {
13
+ return new AggregateFunctionBuilder({
14
+ aggregateFunctionNode: AggregateFunctionNode.create(name, args ? parseReferenceExpressionOrList(args) : undefined),
15
+ });
16
+ };
12
17
  return Object.assign(fn, {
18
+ agg,
13
19
  avg(column) {
14
- return new AggregateFunctionBuilder({
15
- aggregateFunctionNode: AggregateFunctionNode.create('avg', parseSimpleReferenceExpression(column)),
16
- });
20
+ return agg('avg', [column]);
17
21
  },
18
22
  coalesce(value, ...otherValues) {
19
23
  return fn('coalesce', [value, ...otherValues]);
20
24
  },
21
25
  count(column) {
22
- return new AggregateFunctionBuilder({
23
- aggregateFunctionNode: AggregateFunctionNode.create('count', parseSimpleReferenceExpression(column)),
24
- });
26
+ return agg('count', [column]);
25
27
  },
26
28
  countAll(table) {
27
29
  return new AggregateFunctionBuilder({
28
- aggregateFunctionNode: AggregateFunctionNode.create('count', parseSelectAll(table)[0]),
30
+ aggregateFunctionNode: AggregateFunctionNode.create('count', parseSelectAll(table)),
29
31
  });
30
32
  },
31
33
  max(column) {
32
- return new AggregateFunctionBuilder({
33
- aggregateFunctionNode: AggregateFunctionNode.create('max', parseSimpleReferenceExpression(column)),
34
- });
34
+ return agg('max', [column]);
35
35
  },
36
36
  min(column) {
37
- return new AggregateFunctionBuilder({
38
- aggregateFunctionNode: AggregateFunctionNode.create('min', parseSimpleReferenceExpression(column)),
39
- });
37
+ return agg('min', [column]);
40
38
  },
41
39
  sum(column) {
42
- return new AggregateFunctionBuilder({
43
- aggregateFunctionNode: AggregateFunctionNode.create('sum', parseSimpleReferenceExpression(column)),
44
- });
40
+ return agg('sum', [column]);
45
41
  },
46
42
  });
47
43
  }
@@ -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
  *
@@ -391,7 +391,12 @@ export class InsertQueryBuilder {
391
391
  * @deprecated Use `$if` instead
392
392
  */
393
393
  if(condition, func) {
394
- return this.$if(condition, func);
394
+ if (condition) {
395
+ return func(this);
396
+ }
397
+ return new InsertQueryBuilder({
398
+ ...this.#props,
399
+ });
395
400
  }
396
401
  /**
397
402
  * Change the output type of the query.
@@ -408,6 +413,49 @@ export class InsertQueryBuilder {
408
413
  castTo() {
409
414
  return this.$castTo();
410
415
  }
416
+ /**
417
+ * Narrows (parts of) the output type of the query.
418
+ *
419
+ * Kysely tries to be as type-safe as possible, but in some cases we have to make
420
+ * compromises for better maintainability and compilation performance. At present,
421
+ * Kysely doesn't narrow the output type of the query based on {@link values} input
422
+ * when using {@link returning} or {@link returningAll}.
423
+ *
424
+ * This utility method is very useful for these situations, as it removes unncessary
425
+ * runtime assertion/guard code. Its input type is limited to the output type
426
+ * of the query, so you can't add a column that doesn't exist, or change a column's
427
+ * type to something that doesn't exist in its union type.
428
+ *
429
+ * ### Examples
430
+ *
431
+ * Turn this code:
432
+ *
433
+ * ```ts
434
+ * const person = await db.insertInto('person')
435
+ * .values({ ...inputPerson, nullable_column: 'hell yeah!' })
436
+ * .returningAll()
437
+ * .executeTakeFirstOrThrow()
438
+ *
439
+ * if (nullable_column) {
440
+ * functionThatExpectsPersonWithNonNullValue(person)
441
+ * }
442
+ * ```
443
+ *
444
+ * Into this:
445
+ *
446
+ * ```ts
447
+ * const person = await db.insertInto('person')
448
+ * .values({ ...inputPerson, nullable_column: 'hell yeah!' })
449
+ * .returningAll()
450
+ * .$narrowType<{ nullable_column: string }>()
451
+ * .executeTakeFirstOrThrow()
452
+ *
453
+ * functionThatExpectsPersonWithNonNullValue(person)
454
+ * ```
455
+ */
456
+ $narrowType() {
457
+ return new InsertQueryBuilder(this.#props);
458
+ }
411
459
  /**
412
460
  * Asserts that query's output row type equals the given type `T`.
413
461
  *
@@ -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 {};
@@ -1127,6 +1127,49 @@ export class SelectQueryBuilder {
1127
1127
  castTo() {
1128
1128
  return this.$castTo();
1129
1129
  }
1130
+ /**
1131
+ * Narrows (parts of) the output type of the query.
1132
+ *
1133
+ * Kysely tries to be as type-safe as possible, but in some cases we have to make
1134
+ * compromises for better maintainability and compilation performance. At present,
1135
+ * Kysely doesn't narrow the output type of the query when using {@link where}, {@link having}
1136
+ * or {@link JoinQueryBuilder.on}.
1137
+ *
1138
+ * This utility method is very useful for these situations, as it removes unncessary
1139
+ * runtime assertion/guard code. Its input type is limited to the output type
1140
+ * of the query, so you can't add a column that doesn't exist, or change a column's
1141
+ * type to something that doesn't exist in its union type.
1142
+ *
1143
+ * ### Examples
1144
+ *
1145
+ * Turn this code:
1146
+ *
1147
+ * ```ts
1148
+ * const person = await db.selectFrom('person')
1149
+ * .where('nullable_column', 'is not', null)
1150
+ * .selectAll()
1151
+ * .executeTakeFirstOrThrow()
1152
+ *
1153
+ * if (person.nullable_column) {
1154
+ * functionThatExpectsPersonWithNonNullValue(person)
1155
+ * }
1156
+ * ```
1157
+ *
1158
+ * Into this:
1159
+ *
1160
+ * ```ts
1161
+ * const person = await db.selectFrom('person')
1162
+ * .where('nullable_column', 'is not', null)
1163
+ * .selectAll()
1164
+ * .$narrowType<{ nullable_column: string }>()
1165
+ * .executeTakeFirstOrThrow()
1166
+ *
1167
+ * functionThatExpectsPersonWithNonNullValue(person)
1168
+ * ```
1169
+ */
1170
+ $narrowType() {
1171
+ return new SelectQueryBuilder(this.#props);
1172
+ }
1130
1173
  /**
1131
1174
  * Asserts that query's output row type equals the given type `T`.
1132
1175
  *