kysely 0.16.3 → 0.16.7

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 (150) hide show
  1. package/README.md +122 -9
  2. package/dist/cjs/dialect/mysql/mysql-dialect-config.d.ts +205 -0
  3. package/dist/cjs/dialect/mysql/mysql-dialect-config.js +2 -0
  4. package/dist/cjs/dialect/mysql/mysql-dialect.d.ts +1 -205
  5. package/dist/cjs/dialect/mysql/mysql-dialect.js +2 -1
  6. package/dist/cjs/dialect/mysql/mysql-driver.d.ts +1 -1
  7. package/dist/cjs/dialect/mysql/mysql-driver.js +11 -3
  8. package/dist/cjs/dialect/postgres/postgres-dialect-config.d.ts +103 -0
  9. package/dist/cjs/dialect/postgres/postgres-dialect-config.js +2 -0
  10. package/dist/cjs/dialect/postgres/postgres-dialect.d.ts +1 -103
  11. package/dist/cjs/dialect/postgres/postgres-dialect.js +2 -1
  12. package/dist/cjs/dialect/postgres/postgres-driver.d.ts +1 -1
  13. package/dist/cjs/dialect/postgres/postgres-driver.js +12 -3
  14. package/dist/cjs/dialect/sqlite/sqlite-dialect-config.d.ts +43 -0
  15. package/dist/cjs/dialect/sqlite/sqlite-dialect-config.js +2 -0
  16. package/dist/cjs/dialect/sqlite/sqlite-dialect.d.ts +1 -43
  17. package/dist/cjs/dialect/sqlite/sqlite-dialect.js +2 -1
  18. package/dist/cjs/dialect/sqlite/sqlite-driver.d.ts +1 -1
  19. package/dist/cjs/dialect/sqlite/sqlite-driver.js +11 -3
  20. package/dist/cjs/driver/dummy-driver.d.ts +68 -0
  21. package/dist/cjs/driver/dummy-driver.js +62 -0
  22. package/dist/cjs/driver/runtime-driver.d.ts +1 -1
  23. package/dist/cjs/driver/runtime-driver.js +30 -33
  24. package/dist/cjs/index-nodeless.d.ts +2 -0
  25. package/dist/cjs/index-nodeless.js +2 -0
  26. package/dist/cjs/index.d.ts +3 -0
  27. package/dist/cjs/index.js +3 -0
  28. package/dist/cjs/kysely.d.ts +8 -6
  29. package/dist/cjs/kysely.js +8 -6
  30. package/dist/cjs/migration/migrator.d.ts +2 -2
  31. package/dist/cjs/operation-node/primitive-value-list-node.d.ts +2 -3
  32. package/dist/cjs/operation-node/value-node.d.ts +3 -4
  33. package/dist/cjs/parser/complex-expression-parser.d.ts +1 -0
  34. package/dist/cjs/parser/complex-expression-parser.js +5 -1
  35. package/dist/cjs/parser/default-value-parser.d.ts +2 -3
  36. package/dist/cjs/parser/default-value-parser.js +2 -2
  37. package/dist/cjs/parser/insert-values-parser.js +4 -3
  38. package/dist/cjs/parser/reference-parser.d.ts +2 -3
  39. package/dist/cjs/parser/value-parser.d.ts +2 -3
  40. package/dist/cjs/parser/value-parser.js +6 -7
  41. package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-plugin.d.ts +48 -0
  42. package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-plugin.js +19 -0
  43. package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-transformer.d.ts +9 -0
  44. package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-transformer.js +42 -0
  45. package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.d.ts +0 -0
  46. package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.js +0 -0
  47. package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.d.ts +0 -0
  48. package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.js +0 -0
  49. package/dist/cjs/query-builder/delete-query-builder.d.ts +51 -9
  50. package/dist/cjs/query-builder/delete-query-builder.js +56 -8
  51. package/dist/cjs/query-builder/expression-builder.d.ts +1 -1
  52. package/dist/cjs/query-builder/expression-builder.js +2 -2
  53. package/dist/cjs/query-builder/function-builder.d.ts +29 -5
  54. package/dist/cjs/query-builder/function-builder.js +31 -7
  55. package/dist/cjs/query-builder/insert-query-builder.d.ts +53 -11
  56. package/dist/cjs/query-builder/insert-query-builder.js +58 -10
  57. package/dist/cjs/query-builder/select-query-builder.d.ts +58 -16
  58. package/dist/cjs/query-builder/select-query-builder.js +64 -15
  59. package/dist/cjs/query-builder/update-query-builder.d.ts +52 -9
  60. package/dist/cjs/query-builder/update-query-builder.js +57 -8
  61. package/dist/cjs/query-compiler/default-query-compiler.d.ts +1 -2
  62. package/dist/cjs/query-creator.d.ts +2 -2
  63. package/dist/cjs/query-creator.js +2 -2
  64. package/dist/cjs/schema/alter-table-builder.d.ts +30 -4
  65. package/dist/cjs/schema/alter-table-builder.js +2 -5
  66. package/dist/cjs/schema/column-definition-builder.d.ts +27 -2
  67. package/dist/cjs/schema/create-view-builder.js +1 -1
  68. package/dist/cjs/util/log.d.ts +1 -0
  69. package/dist/cjs/util/log.js +3 -0
  70. package/dist/cjs/util/object-utils.d.ts +5 -5
  71. package/dist/cjs/util/object-utils.js +52 -11
  72. package/dist/cjs/util/type-utils.d.ts +21 -0
  73. package/dist/esm/dialect/mysql/mysql-dialect-config.d.ts +205 -0
  74. package/dist/esm/dialect/mysql/mysql-dialect-config.js +2 -0
  75. package/dist/esm/dialect/mysql/mysql-dialect.d.ts +1 -205
  76. package/dist/esm/dialect/mysql/mysql-dialect.js +2 -1
  77. package/dist/esm/dialect/mysql/mysql-driver.d.ts +1 -1
  78. package/dist/esm/dialect/mysql/mysql-driver.js +12 -4
  79. package/dist/esm/dialect/postgres/postgres-dialect-config.d.ts +103 -0
  80. package/dist/esm/dialect/postgres/postgres-dialect-config.js +2 -0
  81. package/dist/esm/dialect/postgres/postgres-dialect.d.ts +1 -103
  82. package/dist/esm/dialect/postgres/postgres-dialect.js +2 -1
  83. package/dist/esm/dialect/postgres/postgres-driver.d.ts +1 -1
  84. package/dist/esm/dialect/postgres/postgres-driver.js +12 -3
  85. package/dist/esm/dialect/sqlite/sqlite-dialect-config.d.ts +43 -0
  86. package/dist/esm/dialect/sqlite/sqlite-dialect-config.js +2 -0
  87. package/dist/esm/dialect/sqlite/sqlite-dialect.d.ts +1 -43
  88. package/dist/esm/dialect/sqlite/sqlite-dialect.js +2 -1
  89. package/dist/esm/dialect/sqlite/sqlite-driver.d.ts +1 -1
  90. package/dist/esm/dialect/sqlite/sqlite-driver.js +12 -4
  91. package/dist/esm/driver/dummy-driver.d.ts +68 -0
  92. package/dist/esm/driver/dummy-driver.js +59 -0
  93. package/dist/esm/driver/runtime-driver.d.ts +1 -1
  94. package/dist/esm/driver/runtime-driver.js +29 -32
  95. package/dist/esm/index-nodeless.d.ts +2 -0
  96. package/dist/esm/index-nodeless.js +2 -0
  97. package/dist/esm/index.d.ts +3 -0
  98. package/dist/esm/index.js +3 -0
  99. package/dist/esm/kysely.d.ts +8 -6
  100. package/dist/esm/kysely.js +8 -6
  101. package/dist/esm/migration/migrator.d.ts +2 -2
  102. package/dist/esm/operation-node/primitive-value-list-node.d.ts +2 -3
  103. package/dist/esm/operation-node/value-node.d.ts +3 -4
  104. package/dist/esm/parser/complex-expression-parser.d.ts +1 -0
  105. package/dist/esm/parser/complex-expression-parser.js +3 -0
  106. package/dist/esm/parser/default-value-parser.d.ts +2 -3
  107. package/dist/esm/parser/default-value-parser.js +2 -2
  108. package/dist/esm/parser/insert-values-parser.js +5 -4
  109. package/dist/esm/parser/reference-parser.d.ts +2 -3
  110. package/dist/esm/parser/reference-parser.js +1 -1
  111. package/dist/esm/parser/value-parser.d.ts +2 -3
  112. package/dist/esm/parser/value-parser.js +8 -9
  113. package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-plugin.d.ts +48 -0
  114. package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-plugin.js +16 -0
  115. package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-transformer.d.ts +9 -0
  116. package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-transformer.js +39 -0
  117. package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.d.ts +0 -0
  118. package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.js +0 -0
  119. package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.d.ts +0 -0
  120. package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.js +0 -0
  121. package/dist/esm/query-builder/delete-query-builder.d.ts +51 -9
  122. package/dist/esm/query-builder/delete-query-builder.js +56 -8
  123. package/dist/esm/query-builder/expression-builder.d.ts +1 -1
  124. package/dist/esm/query-builder/expression-builder.js +1 -1
  125. package/dist/esm/query-builder/function-builder.d.ts +29 -5
  126. package/dist/esm/query-builder/function-builder.js +31 -7
  127. package/dist/esm/query-builder/insert-query-builder.d.ts +53 -11
  128. package/dist/esm/query-builder/insert-query-builder.js +58 -10
  129. package/dist/esm/query-builder/select-query-builder.d.ts +58 -16
  130. package/dist/esm/query-builder/select-query-builder.js +64 -15
  131. package/dist/esm/query-builder/update-query-builder.d.ts +52 -9
  132. package/dist/esm/query-builder/update-query-builder.js +57 -8
  133. package/dist/esm/query-compiler/default-query-compiler.d.ts +1 -2
  134. package/dist/esm/query-creator.d.ts +2 -2
  135. package/dist/esm/query-creator.js +2 -2
  136. package/dist/esm/schema/alter-table-builder.d.ts +30 -4
  137. package/dist/esm/schema/alter-table-builder.js +2 -5
  138. package/dist/esm/schema/column-definition-builder.d.ts +27 -2
  139. package/dist/esm/schema/create-view-builder.js +1 -1
  140. package/dist/esm/util/log.d.ts +1 -0
  141. package/dist/esm/util/log.js +3 -0
  142. package/dist/esm/util/object-utils.d.ts +5 -5
  143. package/dist/esm/util/object-utils.js +50 -9
  144. package/dist/esm/util/type-utils.d.ts +21 -0
  145. package/package.json +11 -5
  146. package/.prettierignore +0 -1
  147. package/dist/cjs/util/dynamic-import.d.ts +0 -2
  148. package/dist/cjs/util/dynamic-import.js +0 -32
  149. package/dist/esm/util/dynamic-import.d.ts +0 -2
  150. package/dist/esm/util/dynamic-import.js +0 -28
@@ -101,25 +101,70 @@ class DeleteQueryBuilder {
101
101
  /**
102
102
  * Simply calls the given function passing `this` as the only argument.
103
103
  *
104
- * This method can be useful when adding optional method calls:
104
+ * If you want to conditionally call a method on `this`, see
105
+ * the {@link if} method.
105
106
  *
106
107
  * ### Examples
107
108
  *
109
+ * The next example uses a helper funtion `log` to log a query:
110
+ *
108
111
  * ```ts
112
+ * function log<T extends Compilable>(qb: T): T {
113
+ * console.log(qb.compile())
114
+ * return qb
115
+ * }
116
+ *
109
117
  * db.deleteFrom('person')
110
- * .call((qb) => {
111
- * if (something) {
112
- * return qb.where('something', '=', something)
113
- * } else {
114
- * return qb.where('somethingElse', '=', somethingElse)
115
- * }
116
- * })
118
+ * .call(log)
117
119
  * .execute()
118
120
  * ```
119
121
  */
120
122
  call(func) {
121
123
  return func(this);
122
124
  }
125
+ /**
126
+ * Call `func(this)` if `condition` is true.
127
+ *
128
+ * This method is especially handy with optional selects. Any `returning` or `returningAll`
129
+ * method calls add columns as optional fields to the output type when called inside
130
+ * the `func` callback. This is because we can't know if those selections were actually
131
+ * made before running the code.
132
+ *
133
+ * You can also call any other methods inside the callback.
134
+ *
135
+ * ### Examples
136
+ *
137
+ * ```ts
138
+ * async function deletePerson(id: number, returnLastName: boolean) {
139
+ * return await db
140
+ * .deleteFrom('person')
141
+ * .where('id', '=', id)
142
+ * .returning(['id', 'first_name'])
143
+ * .if(returnLastName, (qb) => qb.returning('last_name'))
144
+ * .executeTakeFirstOrThrow()
145
+ * }
146
+ * ```
147
+ *
148
+ * Any selections added inside the `if` callback will be added as optional fields to the
149
+ * output type since we can't know if the selections were actually made before running
150
+ * the code. In the example above the return type of the `deletePerson` function is:
151
+ *
152
+ * ```ts
153
+ * {
154
+ * id: number
155
+ * first_name: string
156
+ * last_name?: string
157
+ * }
158
+ * ```
159
+ */
160
+ if(condition, func) {
161
+ if (condition) {
162
+ return func(this);
163
+ }
164
+ return new DeleteQueryBuilder({
165
+ ...this.#props,
166
+ });
167
+ }
123
168
  /**
124
169
  * Change the output type of the query.
125
170
  *
@@ -129,6 +174,9 @@ class DeleteQueryBuilder {
129
174
  castTo() {
130
175
  return new DeleteQueryBuilder(this.#props);
131
176
  }
177
+ /**
178
+ * Returns a copy of this DeleteQueryBuilder instance with the given plugin installed.
179
+ */
132
180
  withPlugin(plugin) {
133
181
  return new DeleteQueryBuilder({
134
182
  ...this.#props,
@@ -3,8 +3,8 @@ import { TableExpression, TableExpressionDatabase, TableExpressionTables } from
3
3
  import { NoopQueryExecutor } from '../query-executor/noop-query-executor.js';
4
4
  import { ParseContext } from '../parser/parse-context.js';
5
5
  import { FunctionBuilder } from './function-builder.js';
6
- import { RawBuilder } from '../index-nodeless.js';
7
6
  import { ExtractTypeFromReferenceExpression, StringReference } from '../parser/reference-parser.js';
7
+ import { RawBuilder } from '../raw-builder/raw-builder.js';
8
8
  export declare class ExpressionBuilder<DB, TB extends keyof DB> {
9
9
  #private;
10
10
  constructor(props: ExpressionBuilderProps);
@@ -8,7 +8,7 @@ const with_schema_plugin_js_1 = require("../plugin/with-schema/with-schema-plugi
8
8
  const query_id_js_1 = require("../util/query-id.js");
9
9
  const object_utils_js_1 = require("../util/object-utils.js");
10
10
  const function_builder_js_1 = require("./function-builder.js");
11
- const index_nodeless_js_1 = require("../index-nodeless.js");
11
+ const raw_builder_js_1 = require("../raw-builder/raw-builder.js");
12
12
  class ExpressionBuilder {
13
13
  #props;
14
14
  constructor(props) {
@@ -69,7 +69,7 @@ class ExpressionBuilder {
69
69
  * See {@link QueryCreator.raw}.
70
70
  */
71
71
  raw(sql, parameters) {
72
- return new index_nodeless_js_1.RawBuilder({
72
+ return new raw_builder_js_1.RawBuilder({
73
73
  queryId: (0, query_id_js_1.createQueryId)(),
74
74
  executor: this.#props.executor,
75
75
  sql,
@@ -4,6 +4,9 @@ import { RawBuilder } from '../raw-builder/raw-builder.js';
4
4
  /**
5
5
  * Helpers for type safe SQL function calls.
6
6
  *
7
+ * You can always use {@link Kysely.raw} to call functions and build arbitrary
8
+ * expressions. This module simply has shortcuts for most common function calls.
9
+ *
7
10
  * ### Examples
8
11
  *
9
12
  * ```ts
@@ -35,12 +38,33 @@ export declare class FunctionBuilder<DB, TB extends keyof DB> {
35
38
  constructor(props: FunctionBuilderProps);
36
39
  /**
37
40
  * Calls the `max` function for the column given as the argument.
41
+ *
42
+ * ### Examples
43
+ *
44
+ * ```ts
45
+ * const { max } = db.fn
46
+ *
47
+ * db.selectFrom('toy')
48
+ * .select(max('price').as('max_price'))
49
+ * .execute()
50
+ * ```
38
51
  */
39
- min<C extends StringReference<DB, TB>>(column: C): RawBuilder<ExtractTypeFromReferenceExpression<DB, TB, C>>;
52
+ max<C extends StringReference<DB, TB>>(column: C): RawBuilder<ExtractTypeFromReferenceExpression<DB, TB, C>>;
40
53
  /**
41
54
  * Calls the `min` function for the column given as the argument.
55
+ *
56
+ * ### Examples
57
+ *
58
+ * ```ts
59
+ * const { min } = db.fn
60
+ *
61
+ * db.selectFrom('toy')
62
+ * .select(min('price').as('min_price'))
63
+ * .execute()
64
+ * ```
65
+ *
42
66
  */
43
- max<C extends StringReference<DB, TB>>(column: C): RawBuilder<ExtractTypeFromReferenceExpression<DB, TB, C>>;
67
+ min<C extends StringReference<DB, TB>>(column: C): RawBuilder<ExtractTypeFromReferenceExpression<DB, TB, C>>;
44
68
  /**
45
69
  * Calls the `avg` function for the column given as the argument.
46
70
  *
@@ -58,7 +82,7 @@ export declare class FunctionBuilder<DB, TB extends keyof DB> {
58
82
  * const { avg } = db.fn
59
83
  *
60
84
  * db.selectFrom('toy')
61
- * .select(avg<number>('price'))
85
+ * .select(avg<number>('price').as('avg_price'))
62
86
  * .execute()
63
87
  * ```
64
88
  */
@@ -80,7 +104,7 @@ export declare class FunctionBuilder<DB, TB extends keyof DB> {
80
104
  * const { sum } = db.fn
81
105
  *
82
106
  * db.selectFrom('toy')
83
- * .select(sum<number>('price'))
107
+ * .select(sum<number>('price').as('total_price'))
84
108
  * .execute()
85
109
  * ```
86
110
  */
@@ -102,7 +126,7 @@ export declare class FunctionBuilder<DB, TB extends keyof DB> {
102
126
  * const { count } = db.fn
103
127
  *
104
128
  * db.selectFrom('toy')
105
- * .select(count<number>('price'))
129
+ * .select(count<number>('id').as('num_toys'))
106
130
  * .execute()
107
131
  * ```
108
132
  */
@@ -7,6 +7,9 @@ const query_id_js_1 = require("../util/query-id.js");
7
7
  /**
8
8
  * Helpers for type safe SQL function calls.
9
9
  *
10
+ * You can always use {@link Kysely.raw} to call functions and build arbitrary
11
+ * expressions. This module simply has shortcuts for most common function calls.
12
+ *
10
13
  * ### Examples
11
14
  *
12
15
  * ```ts
@@ -45,15 +48,36 @@ class FunctionBuilder {
45
48
  }
46
49
  /**
47
50
  * Calls the `max` function for the column given as the argument.
51
+ *
52
+ * ### Examples
53
+ *
54
+ * ```ts
55
+ * const { max } = db.fn
56
+ *
57
+ * db.selectFrom('toy')
58
+ * .select(max('price').as('max_price'))
59
+ * .execute()
60
+ * ```
48
61
  */
49
- min(column) {
50
- return this.#oneArgFunction('min', column);
62
+ max(column) {
63
+ return this.#oneArgFunction('max', column);
51
64
  }
52
65
  /**
53
66
  * Calls the `min` function for the column given as the argument.
67
+ *
68
+ * ### Examples
69
+ *
70
+ * ```ts
71
+ * const { min } = db.fn
72
+ *
73
+ * db.selectFrom('toy')
74
+ * .select(min('price').as('min_price'))
75
+ * .execute()
76
+ * ```
77
+ *
54
78
  */
55
- max(column) {
56
- return this.#oneArgFunction('max', column);
79
+ min(column) {
80
+ return this.#oneArgFunction('min', column);
57
81
  }
58
82
  /**
59
83
  * Calls the `avg` function for the column given as the argument.
@@ -72,7 +96,7 @@ class FunctionBuilder {
72
96
  * const { avg } = db.fn
73
97
  *
74
98
  * db.selectFrom('toy')
75
- * .select(avg<number>('price'))
99
+ * .select(avg<number>('price').as('avg_price'))
76
100
  * .execute()
77
101
  * ```
78
102
  */
@@ -96,7 +120,7 @@ class FunctionBuilder {
96
120
  * const { sum } = db.fn
97
121
  *
98
122
  * db.selectFrom('toy')
99
- * .select(sum<number>('price'))
123
+ * .select(sum<number>('price').as('total_price'))
100
124
  * .execute()
101
125
  * ```
102
126
  */
@@ -120,7 +144,7 @@ class FunctionBuilder {
120
144
  * const { count } = db.fn
121
145
  *
122
146
  * db.selectFrom('toy')
123
- * .select(count<number>('price'))
147
+ * .select(count<number>('id').as('num_toys'))
124
148
  * .execute()
125
149
  * ```
126
150
  */
@@ -3,12 +3,13 @@ import { CompiledQuery } from '../query-compiler/compiled-query.js';
3
3
  import { SelectExpression } from '../parser/select-parser.js';
4
4
  import { InsertObject } from '../parser/insert-values-parser.js';
5
5
  import { InsertQueryNode } from '../operation-node/insert-query-node.js';
6
- import { SingleResultType } from '../util/type-utils.js';
6
+ import { MergePartial, SingleResultType } from '../util/type-utils.js';
7
7
  import { MutationObject } from '../parser/update-set-parser.js';
8
8
  import { Compilable } from '../util/compilable.js';
9
9
  import { QueryExecutor } from '../query-executor/query-executor.js';
10
10
  import { QueryId } from '../util/query-id.js';
11
11
  import { ParseContext } from '../parser/parse-context.js';
12
+ import { InsertResult } from './insert-result.js';
12
13
  import { KyselyPlugin } from '../plugin/kysely-plugin.js';
13
14
  import { ReturningRow } from '../parser/returning-parser.js';
14
15
  import { NoResultErrorConstructor } from './no-result-error.js';
@@ -448,24 +449,62 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
448
449
  /**
449
450
  * Simply calls the given function passing `this` as the only argument.
450
451
  *
451
- * This method can be useful when adding optional method calls:
452
+ * If you want to conditionally call a method on `this`, see
453
+ * the {@link if} method.
452
454
  *
453
455
  * ### Examples
454
456
  *
457
+ * The next example uses a helper funtion `log` to log a query:
458
+ *
455
459
  * ```ts
456
- * db.insertInto('person')
457
- * .selectAll()
458
- * .call((qb) => {
459
- * if (something) {
460
- * return qb.onConflict((oc) => oc.column('something').doNothing())
461
- * } else {
462
- * return qb.onConflict((oc) => oc.column('something_else').doNothing())
463
- * }
464
- * })
460
+ * function log<T extends Compilable>(qb: T): T {
461
+ * console.log(qb.compile())
462
+ * return qb
463
+ * }
464
+ *
465
+ * db.updateTable('person')
466
+ * .set(values)
467
+ * .call(log)
465
468
  * .execute()
466
469
  * ```
467
470
  */
468
471
  call<T>(func: (qb: this) => T): T;
472
+ /**
473
+ * Call `func(this)` if `condition` is true.
474
+ *
475
+ * This method is especially handy with optional selects. Any `returning` or `returningAll`
476
+ * method calls add columns as optional fields to the output type when called inside
477
+ * the `func` callback. This is because we can't know if those selections were actually
478
+ * made before running the code.
479
+ *
480
+ * You can also call any other methods inside the callback.
481
+ *
482
+ * ### Examples
483
+ *
484
+ * ```ts
485
+ * async function insertPerson(values: InsertablePerson, returnLastName: boolean) {
486
+ * return await db
487
+ * .insertInto('person')
488
+ * .values(values)
489
+ * .returning(['id', 'first_name'])
490
+ * .if(returnLastName, (qb) => qb.returning('last_name'))
491
+ * .executeTakeFirstOrThrow()
492
+ * }
493
+ * ```
494
+ *
495
+ * Any selections added inside the `if` callback will be added as optional fields to the
496
+ * output type since we can't know if the selections were actually made before running
497
+ * the code. In the example above the return type of the `insertPerson` function is:
498
+ *
499
+ * ```ts
500
+ * {
501
+ * id: number
502
+ * first_name: string
503
+ * last_name?: string
504
+ * }
505
+ * ```
506
+ */
507
+ 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>>;
469
508
  /**
470
509
  * Change the output type of the query.
471
510
  *
@@ -473,6 +512,9 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
473
512
  * don't support your use case.
474
513
  */
475
514
  castTo<T>(): InsertQueryBuilder<DB, TB, T>;
515
+ /**
516
+ * Returns a copy of this InsertQueryBuilder instance with the given plugin installed.
517
+ */
476
518
  withPlugin(plugin: KyselyPlugin): InsertQueryBuilder<DB, TB, O>;
477
519
  toOperationNode(): InsertQueryNode;
478
520
  compile(): CompiledQuery;
@@ -317,26 +317,71 @@ class InsertQueryBuilder {
317
317
  /**
318
318
  * Simply calls the given function passing `this` as the only argument.
319
319
  *
320
- * This method can be useful when adding optional method calls:
320
+ * If you want to conditionally call a method on `this`, see
321
+ * the {@link if} method.
321
322
  *
322
323
  * ### Examples
323
324
  *
325
+ * The next example uses a helper funtion `log` to log a query:
326
+ *
324
327
  * ```ts
325
- * db.insertInto('person')
326
- * .selectAll()
327
- * .call((qb) => {
328
- * if (something) {
329
- * return qb.onConflict((oc) => oc.column('something').doNothing())
330
- * } else {
331
- * return qb.onConflict((oc) => oc.column('something_else').doNothing())
332
- * }
333
- * })
328
+ * function log<T extends Compilable>(qb: T): T {
329
+ * console.log(qb.compile())
330
+ * return qb
331
+ * }
332
+ *
333
+ * db.updateTable('person')
334
+ * .set(values)
335
+ * .call(log)
334
336
  * .execute()
335
337
  * ```
336
338
  */
337
339
  call(func) {
338
340
  return func(this);
339
341
  }
342
+ /**
343
+ * Call `func(this)` if `condition` is true.
344
+ *
345
+ * This method is especially handy with optional selects. Any `returning` or `returningAll`
346
+ * method calls add columns as optional fields to the output type when called inside
347
+ * the `func` callback. This is because we can't know if those selections were actually
348
+ * made before running the code.
349
+ *
350
+ * You can also call any other methods inside the callback.
351
+ *
352
+ * ### Examples
353
+ *
354
+ * ```ts
355
+ * async function insertPerson(values: InsertablePerson, returnLastName: boolean) {
356
+ * return await db
357
+ * .insertInto('person')
358
+ * .values(values)
359
+ * .returning(['id', 'first_name'])
360
+ * .if(returnLastName, (qb) => qb.returning('last_name'))
361
+ * .executeTakeFirstOrThrow()
362
+ * }
363
+ * ```
364
+ *
365
+ * Any selections added inside the `if` callback will be added as optional fields to the
366
+ * output type since we can't know if the selections were actually made before running
367
+ * the code. In the example above the return type of the `insertPerson` function is:
368
+ *
369
+ * ```ts
370
+ * {
371
+ * id: number
372
+ * first_name: string
373
+ * last_name?: string
374
+ * }
375
+ * ```
376
+ */
377
+ if(condition, func) {
378
+ if (condition) {
379
+ return func(this);
380
+ }
381
+ return new InsertQueryBuilder({
382
+ ...this.#props,
383
+ });
384
+ }
340
385
  /**
341
386
  * Change the output type of the query.
342
387
  *
@@ -346,6 +391,9 @@ class InsertQueryBuilder {
346
391
  castTo() {
347
392
  return new InsertQueryBuilder(this.#props);
348
393
  }
394
+ /**
395
+ * Returns a copy of this InsertQueryBuilder instance with the given plugin installed.
396
+ */
349
397
  withPlugin(plugin) {
350
398
  return new InsertQueryBuilder({
351
399
  ...this.#props,
@@ -7,7 +7,7 @@ import { SelectExpression, QueryBuilderWithSelection, SelectAllQueryBuilder } fr
7
7
  import { ExistsExpression, FilterOperator, FilterValueExpressionOrList, WhereGrouper, HavingGrouper } from '../parser/filter-parser.js';
8
8
  import { ReferenceExpression } from '../parser/reference-parser.js';
9
9
  import { SelectQueryNode } from '../operation-node/select-query-node.js';
10
- import { AnyRawBuilder, SingleResultType } from '../util/type-utils.js';
10
+ import { AnyRawBuilder, MergePartial, SingleResultType } from '../util/type-utils.js';
11
11
  import { OrderByDirectionExpression, OrderByExpression } from '../parser/order-by-parser.js';
12
12
  import { Compilable } from '../util/compilable.js';
13
13
  import { QueryExecutor } from '../query-executor/query-executor.js';
@@ -1116,27 +1116,15 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
1116
1116
  /**
1117
1117
  * Simply calls the given function passing `this` as the only argument.
1118
1118
  *
1119
- * This method can be useful when adding optional method calls:
1119
+ * If you want to conditionally call a method on `this`, see
1120
+ * the {@link if} method.
1120
1121
  *
1121
1122
  * ### Examples
1122
1123
  *
1123
- * ```ts
1124
- * db.selectFrom('person')
1125
- * .selectAll()
1126
- * .call((qb) => {
1127
- * if (something) {
1128
- * return qb.where('something', '=', something)
1129
- * } else {
1130
- * return qb.where('somethingElse', '=', somethingElse)
1131
- * }
1132
- * })
1133
- * .execute()
1134
- * ```
1135
- *
1136
1124
  * The next example uses a helper funtion `log` to log a query:
1137
1125
  *
1138
1126
  * ```ts
1139
- * function log<T extends AnyQueryBuilder>(qb: T): T {
1127
+ * function log<T extends Compilable>(qb: T): T {
1140
1128
  * console.log(qb.compile())
1141
1129
  * return qb
1142
1130
  * }
@@ -1148,6 +1136,57 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
1148
1136
  * ```
1149
1137
  */
1150
1138
  call<T>(func: (qb: this) => T): T;
1139
+ /**
1140
+ * Call `func(this)` if `condition` is true.
1141
+ *
1142
+ * This method is especially handy with optional selects. Any `select` or `selectAll`
1143
+ * method calls add columns as optional fields to the output type when called inside
1144
+ * the `func` callback. This is because we can't know if those selections were actually
1145
+ * made before running the code.
1146
+ *
1147
+ * Also see [this recipe](https://github.com/koskimas/kysely/tree/master/recipes/conditional-selects.md)
1148
+ *
1149
+ * ### Examples
1150
+ *
1151
+ * ```ts
1152
+ * async function getPerson(id: number, withLastName: boolean) {
1153
+ * return await db
1154
+ * .selectFrom('person')
1155
+ * .select(['id', 'first_name'])
1156
+ * .if(withLastName, (qb) => qb.select('last_name'))
1157
+ * .where('id', '=', id)
1158
+ * .executeTakeFirstOrThrow()
1159
+ * }
1160
+ * ```
1161
+ *
1162
+ * Any selections added inside the `if` callback will be added as optional fields to the
1163
+ * output type since we can't know if the selections were actually made before running
1164
+ * the code. In the example above the return type of the `getPerson` function is:
1165
+ *
1166
+ * ```ts
1167
+ * {
1168
+ * id: number
1169
+ * first_name: string
1170
+ * last_name?: string
1171
+ * }
1172
+ * ```
1173
+ *
1174
+ * You can also call any other methods inside the callback:
1175
+ *
1176
+ * ```ts
1177
+ * const { count } = db.fn
1178
+ *
1179
+ * db.selectFrom('person')
1180
+ * .select('person.id')
1181
+ * .if(filterByFirstName, (qb) => qb.where('first_name', '=', firstName))
1182
+ * .if(filterByPetCount, (qb) => qb
1183
+ * .innerJoin('pet', 'pet.owner_id', 'person.id')
1184
+ * .having(count('pet.id'), '>', petCountLimit)
1185
+ * .groupBy('person.id')
1186
+ * )
1187
+ * ```
1188
+ */
1189
+ if<O2 extends O>(condition: boolean, func: (qb: this) => SelectQueryBuilder<DB, TB, O2>): SelectQueryBuilder<DB, TB, MergePartial<O, O2>>;
1151
1190
  /**
1152
1191
  * Gives an alias for the query. This method is only useful for sub queries.
1153
1192
  *
@@ -1173,6 +1212,9 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
1173
1212
  * don't support your use case.
1174
1213
  */
1175
1214
  castTo<T>(): SelectQueryBuilder<DB, TB, T>;
1215
+ /**
1216
+ * Returns a copy of this SelectQueryBuilder instance with the given plugin installed.
1217
+ */
1176
1218
  withPlugin(plugin: KyselyPlugin): SelectQueryBuilder<DB, TB, O>;
1177
1219
  toOperationNode(): SelectQueryNode;
1178
1220
  compile(): CompiledQuery;
@@ -427,27 +427,15 @@ class SelectQueryBuilder {
427
427
  /**
428
428
  * Simply calls the given function passing `this` as the only argument.
429
429
  *
430
- * This method can be useful when adding optional method calls:
430
+ * If you want to conditionally call a method on `this`, see
431
+ * the {@link if} method.
431
432
  *
432
433
  * ### Examples
433
434
  *
434
- * ```ts
435
- * db.selectFrom('person')
436
- * .selectAll()
437
- * .call((qb) => {
438
- * if (something) {
439
- * return qb.where('something', '=', something)
440
- * } else {
441
- * return qb.where('somethingElse', '=', somethingElse)
442
- * }
443
- * })
444
- * .execute()
445
- * ```
446
- *
447
435
  * The next example uses a helper funtion `log` to log a query:
448
436
  *
449
437
  * ```ts
450
- * function log<T extends AnyQueryBuilder>(qb: T): T {
438
+ * function log<T extends Compilable>(qb: T): T {
451
439
  * console.log(qb.compile())
452
440
  * return qb
453
441
  * }
@@ -461,6 +449,64 @@ class SelectQueryBuilder {
461
449
  call(func) {
462
450
  return func(this);
463
451
  }
452
+ /**
453
+ * Call `func(this)` if `condition` is true.
454
+ *
455
+ * This method is especially handy with optional selects. Any `select` or `selectAll`
456
+ * method calls add columns as optional fields to the output type when called inside
457
+ * the `func` callback. This is because we can't know if those selections were actually
458
+ * made before running the code.
459
+ *
460
+ * Also see [this recipe](https://github.com/koskimas/kysely/tree/master/recipes/conditional-selects.md)
461
+ *
462
+ * ### Examples
463
+ *
464
+ * ```ts
465
+ * async function getPerson(id: number, withLastName: boolean) {
466
+ * return await db
467
+ * .selectFrom('person')
468
+ * .select(['id', 'first_name'])
469
+ * .if(withLastName, (qb) => qb.select('last_name'))
470
+ * .where('id', '=', id)
471
+ * .executeTakeFirstOrThrow()
472
+ * }
473
+ * ```
474
+ *
475
+ * Any selections added inside the `if` callback will be added as optional fields to the
476
+ * output type since we can't know if the selections were actually made before running
477
+ * the code. In the example above the return type of the `getPerson` function is:
478
+ *
479
+ * ```ts
480
+ * {
481
+ * id: number
482
+ * first_name: string
483
+ * last_name?: string
484
+ * }
485
+ * ```
486
+ *
487
+ * You can also call any other methods inside the callback:
488
+ *
489
+ * ```ts
490
+ * const { count } = db.fn
491
+ *
492
+ * db.selectFrom('person')
493
+ * .select('person.id')
494
+ * .if(filterByFirstName, (qb) => qb.where('first_name', '=', firstName))
495
+ * .if(filterByPetCount, (qb) => qb
496
+ * .innerJoin('pet', 'pet.owner_id', 'person.id')
497
+ * .having(count('pet.id'), '>', petCountLimit)
498
+ * .groupBy('person.id')
499
+ * )
500
+ * ```
501
+ */
502
+ if(condition, func) {
503
+ if (condition) {
504
+ return func(this);
505
+ }
506
+ return new SelectQueryBuilder({
507
+ ...this.#props,
508
+ });
509
+ }
464
510
  /**
465
511
  * Gives an alias for the query. This method is only useful for sub queries.
466
512
  *
@@ -490,6 +536,9 @@ class SelectQueryBuilder {
490
536
  castTo() {
491
537
  return new SelectQueryBuilder(this.#props);
492
538
  }
539
+ /**
540
+ * Returns a copy of this SelectQueryBuilder instance with the given plugin installed.
541
+ */
493
542
  withPlugin(plugin) {
494
543
  return new SelectQueryBuilder({
495
544
  ...this.#props,