kysely 0.11.4 → 0.12.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 (192) hide show
  1. package/README.md +1 -1
  2. package/dist/cjs/dynamic/dynamic-reference-builder.d.ts +1 -0
  3. package/dist/cjs/dynamic/dynamic-reference-builder.js +9 -1
  4. package/dist/cjs/dynamic/dynamic.d.ts +6 -7
  5. package/dist/cjs/dynamic/dynamic.js +6 -7
  6. package/dist/cjs/index.d.ts +13 -3
  7. package/dist/cjs/index.js +11 -1
  8. package/dist/cjs/kysely.d.ts +19 -11
  9. package/dist/cjs/kysely.js +32 -9
  10. package/dist/cjs/migration/migration.d.ts +2 -2
  11. package/dist/cjs/operation-node/create-table-node.d.ts +6 -2
  12. package/dist/cjs/operation-node/create-table-node.js +6 -6
  13. package/dist/cjs/operation-node/create-view-node.d.ts +1 -0
  14. package/dist/cjs/operation-node/insert-query-node.d.ts +4 -4
  15. package/dist/cjs/operation-node/operation-node-transformer.d.ts +2 -0
  16. package/dist/cjs/operation-node/operation-node-transformer.js +11 -1
  17. package/dist/cjs/operation-node/operation-node-visitor.d.ts +2 -0
  18. package/dist/cjs/operation-node/operation-node-visitor.js +1 -0
  19. package/dist/cjs/operation-node/operation-node.d.ts +1 -1
  20. package/dist/cjs/operation-node/query-node.d.ts +0 -2
  21. package/dist/cjs/operation-node/query-node.js +0 -10
  22. package/dist/cjs/operation-node/values-node.d.ts +15 -0
  23. package/dist/cjs/operation-node/values-node.js +18 -0
  24. package/dist/cjs/parser/complex-expression-parser.d.ts +9 -0
  25. package/dist/cjs/parser/complex-expression-parser.js +25 -0
  26. package/dist/cjs/parser/filter-parser.d.ts +13 -4
  27. package/dist/cjs/parser/filter-parser.js +21 -24
  28. package/dist/cjs/parser/insert-values-parser.d.ts +5 -9
  29. package/dist/cjs/parser/insert-values-parser.js +27 -41
  30. package/dist/cjs/parser/order-by-parser.d.ts +1 -1
  31. package/dist/cjs/parser/parse-context.d.ts +8 -8
  32. package/dist/cjs/parser/parse-context.js +10 -10
  33. package/dist/cjs/parser/reference-parser.d.ts +8 -7
  34. package/dist/cjs/parser/reference-parser.js +8 -17
  35. package/dist/cjs/parser/returning-parser.d.ts +1 -5
  36. package/dist/cjs/parser/select-parser.d.ts +12 -14
  37. package/dist/cjs/parser/select-parser.js +5 -9
  38. package/dist/cjs/parser/table-parser.d.ts +9 -11
  39. package/dist/cjs/parser/table-parser.js +2 -8
  40. package/dist/cjs/parser/union-parser.d.ts +2 -2
  41. package/dist/cjs/parser/union-parser.js +1 -6
  42. package/dist/cjs/parser/update-set-parser.d.ts +2 -8
  43. package/dist/cjs/parser/update-set-parser.js +6 -26
  44. package/dist/cjs/parser/value-parser.d.ts +9 -6
  45. package/dist/cjs/parser/value-parser.js +7 -30
  46. package/dist/cjs/parser/with-parser.d.ts +6 -3
  47. package/dist/cjs/plugin/immediate-value.ts/immediate-value-plugin.js +2 -2
  48. package/dist/cjs/query-builder/delete-query-builder.d.ts +102 -0
  49. package/dist/cjs/query-builder/delete-query-builder.js +185 -0
  50. package/dist/cjs/query-builder/expression-builder.d.ts +8 -6
  51. package/dist/cjs/query-builder/expression-builder.js +3 -3
  52. package/dist/cjs/query-builder/function-builder.d.ts +2 -1
  53. package/dist/cjs/query-builder/function-builder.js +2 -1
  54. package/dist/cjs/query-builder/having-interface.d.ts +49 -0
  55. package/dist/cjs/query-builder/having-interface.js +2 -0
  56. package/dist/cjs/query-builder/insert-query-builder.d.ts +439 -0
  57. package/dist/cjs/query-builder/insert-query-builder.js +256 -0
  58. package/dist/cjs/query-builder/insert-result.d.ts +3 -2
  59. package/dist/cjs/query-builder/insert-result.js +3 -2
  60. package/dist/cjs/query-builder/join-builder.d.ts +19 -20
  61. package/dist/cjs/query-builder/join-builder.js +12 -12
  62. package/dist/cjs/query-builder/join-interface.d.ts +133 -0
  63. package/dist/cjs/query-builder/join-interface.js +2 -0
  64. package/dist/cjs/query-builder/no-result-error.d.ts +9 -0
  65. package/dist/cjs/query-builder/no-result-error.js +14 -0
  66. package/dist/cjs/query-builder/returning-interface.d.ts +73 -0
  67. package/dist/cjs/query-builder/returning-interface.js +2 -0
  68. package/dist/cjs/query-builder/select-query-builder.d.ts +711 -0
  69. package/dist/cjs/query-builder/select-query-builder.js +571 -0
  70. package/dist/cjs/query-builder/update-query-builder.d.ts +190 -0
  71. package/dist/cjs/query-builder/update-query-builder.js +279 -0
  72. package/dist/cjs/query-builder/where-interface.d.ts +381 -0
  73. package/dist/cjs/query-builder/where-interface.js +2 -0
  74. package/dist/cjs/query-compiler/default-query-compiler.d.ts +2 -0
  75. package/dist/cjs/query-compiler/default-query-compiler.js +22 -5
  76. package/dist/cjs/query-creator.d.ts +42 -40
  77. package/dist/cjs/query-creator.js +33 -28
  78. package/dist/cjs/query-executor/query-executor.d.ts +1 -1
  79. package/dist/cjs/query-executor/query-executor.js +1 -1
  80. package/dist/cjs/raw-builder/raw-builder.d.ts +2 -0
  81. package/dist/cjs/raw-builder/raw-builder.js +6 -0
  82. package/dist/cjs/schema/alter-table-builder.d.ts +2 -2
  83. package/dist/cjs/schema/column-definition-builder.d.ts +27 -20
  84. package/dist/cjs/schema/create-index-builder.d.ts +2 -1
  85. package/dist/cjs/schema/create-index-builder.js +2 -1
  86. package/dist/cjs/schema/create-table-builder.d.ts +26 -9
  87. package/dist/cjs/schema/create-table-builder.js +42 -9
  88. package/dist/cjs/schema/create-view-builder.d.ts +17 -3
  89. package/dist/cjs/schema/create-view-builder.js +25 -16
  90. package/dist/cjs/schema/schema.d.ts +18 -11
  91. package/dist/cjs/schema/schema.js +18 -11
  92. package/dist/cjs/util/log.d.ts +3 -2
  93. package/dist/cjs/util/log.js +8 -15
  94. package/dist/cjs/util/type-utils.d.ts +7 -7
  95. package/dist/esm/dynamic/dynamic-reference-builder.d.ts +1 -0
  96. package/dist/esm/dynamic/dynamic-reference-builder.js +7 -0
  97. package/dist/esm/dynamic/dynamic.d.ts +6 -7
  98. package/dist/esm/dynamic/dynamic.js +6 -7
  99. package/dist/esm/index.d.ts +13 -3
  100. package/dist/esm/index.js +11 -1
  101. package/dist/esm/kysely.d.ts +19 -11
  102. package/dist/esm/kysely.js +32 -9
  103. package/dist/esm/migration/migration.d.ts +2 -2
  104. package/dist/esm/operation-node/create-table-node.d.ts +6 -2
  105. package/dist/esm/operation-node/create-table-node.js +6 -6
  106. package/dist/esm/operation-node/create-view-node.d.ts +1 -0
  107. package/dist/esm/operation-node/insert-query-node.d.ts +4 -4
  108. package/dist/esm/operation-node/operation-node-transformer.d.ts +2 -0
  109. package/dist/esm/operation-node/operation-node-transformer.js +11 -1
  110. package/dist/esm/operation-node/operation-node-visitor.d.ts +2 -0
  111. package/dist/esm/operation-node/operation-node-visitor.js +1 -0
  112. package/dist/esm/operation-node/operation-node.d.ts +1 -1
  113. package/dist/esm/operation-node/query-node.d.ts +0 -2
  114. package/dist/esm/operation-node/query-node.js +0 -10
  115. package/dist/esm/operation-node/values-node.d.ts +15 -0
  116. package/dist/esm/operation-node/values-node.js +15 -0
  117. package/dist/esm/parser/complex-expression-parser.d.ts +9 -0
  118. package/dist/esm/parser/complex-expression-parser.js +20 -0
  119. package/dist/esm/parser/filter-parser.d.ts +13 -4
  120. package/dist/esm/parser/filter-parser.js +21 -24
  121. package/dist/esm/parser/insert-values-parser.d.ts +5 -9
  122. package/dist/esm/parser/insert-values-parser.js +27 -40
  123. package/dist/esm/parser/order-by-parser.d.ts +1 -1
  124. package/dist/esm/parser/parse-context.d.ts +8 -8
  125. package/dist/esm/parser/parse-context.js +10 -10
  126. package/dist/esm/parser/reference-parser.d.ts +8 -7
  127. package/dist/esm/parser/reference-parser.js +9 -18
  128. package/dist/esm/parser/returning-parser.d.ts +1 -5
  129. package/dist/esm/parser/select-parser.d.ts +12 -14
  130. package/dist/esm/parser/select-parser.js +6 -10
  131. package/dist/esm/parser/table-parser.d.ts +9 -11
  132. package/dist/esm/parser/table-parser.js +3 -9
  133. package/dist/esm/parser/union-parser.d.ts +2 -2
  134. package/dist/esm/parser/union-parser.js +1 -6
  135. package/dist/esm/parser/update-set-parser.d.ts +2 -8
  136. package/dist/esm/parser/update-set-parser.js +5 -24
  137. package/dist/esm/parser/value-parser.d.ts +9 -6
  138. package/dist/esm/parser/value-parser.js +8 -31
  139. package/dist/esm/parser/with-parser.d.ts +6 -3
  140. package/dist/esm/plugin/immediate-value.ts/immediate-value-plugin.js +2 -2
  141. package/dist/esm/query-builder/delete-query-builder.d.ts +102 -0
  142. package/dist/esm/query-builder/delete-query-builder.js +181 -0
  143. package/dist/esm/query-builder/expression-builder.d.ts +8 -6
  144. package/dist/esm/query-builder/expression-builder.js +3 -3
  145. package/dist/esm/query-builder/function-builder.d.ts +2 -1
  146. package/dist/esm/query-builder/function-builder.js +2 -1
  147. package/dist/esm/query-builder/having-interface.d.ts +49 -0
  148. package/dist/esm/query-builder/having-interface.js +1 -0
  149. package/dist/esm/query-builder/insert-query-builder.d.ts +439 -0
  150. package/dist/esm/query-builder/insert-query-builder.js +252 -0
  151. package/dist/esm/query-builder/insert-result.d.ts +3 -2
  152. package/dist/esm/query-builder/insert-result.js +3 -2
  153. package/dist/esm/query-builder/join-builder.d.ts +19 -20
  154. package/dist/esm/query-builder/join-builder.js +12 -12
  155. package/dist/esm/query-builder/join-interface.d.ts +133 -0
  156. package/dist/esm/query-builder/join-interface.js +1 -0
  157. package/dist/esm/query-builder/no-result-error.d.ts +9 -0
  158. package/dist/esm/query-builder/no-result-error.js +10 -0
  159. package/dist/esm/query-builder/returning-interface.d.ts +73 -0
  160. package/dist/esm/query-builder/returning-interface.js +1 -0
  161. package/dist/esm/query-builder/select-query-builder.d.ts +711 -0
  162. package/dist/esm/query-builder/select-query-builder.js +566 -0
  163. package/dist/esm/query-builder/update-query-builder.d.ts +190 -0
  164. package/dist/esm/query-builder/update-query-builder.js +275 -0
  165. package/dist/esm/query-builder/where-interface.d.ts +381 -0
  166. package/dist/esm/query-builder/where-interface.js +1 -0
  167. package/dist/esm/query-compiler/default-query-compiler.d.ts +2 -0
  168. package/dist/esm/query-compiler/default-query-compiler.js +22 -5
  169. package/dist/esm/query-creator.d.ts +42 -40
  170. package/dist/esm/query-creator.js +33 -28
  171. package/dist/esm/query-executor/query-executor.d.ts +1 -1
  172. package/dist/esm/query-executor/query-executor.js +1 -1
  173. package/dist/esm/raw-builder/raw-builder.d.ts +2 -0
  174. package/dist/esm/raw-builder/raw-builder.js +6 -0
  175. package/dist/esm/schema/alter-table-builder.d.ts +2 -2
  176. package/dist/esm/schema/column-definition-builder.d.ts +27 -20
  177. package/dist/esm/schema/create-index-builder.d.ts +2 -1
  178. package/dist/esm/schema/create-index-builder.js +2 -1
  179. package/dist/esm/schema/create-table-builder.d.ts +26 -9
  180. package/dist/esm/schema/create-table-builder.js +43 -10
  181. package/dist/esm/schema/create-view-builder.d.ts +17 -3
  182. package/dist/esm/schema/create-view-builder.js +25 -16
  183. package/dist/esm/schema/schema.d.ts +18 -11
  184. package/dist/esm/schema/schema.js +18 -11
  185. package/dist/esm/util/log.d.ts +3 -2
  186. package/dist/esm/util/log.js +7 -14
  187. package/dist/esm/util/type-utils.d.ts +7 -7
  188. package/package.json +3 -3
  189. package/dist/cjs/query-builder/query-builder.d.ts +0 -1728
  190. package/dist/cjs/query-builder/query-builder.js +0 -1067
  191. package/dist/esm/query-builder/query-builder.d.ts +0 -1728
  192. package/dist/esm/query-builder/query-builder.js +0 -1061
@@ -1,1728 +0,0 @@
1
- import { AliasNode } from '../operation-node/alias-node.js';
2
- import { OperationNodeSource } from '../operation-node/operation-node-source.js';
3
- import { CompiledQuery } from '../query-compiler/compiled-query.js';
4
- import { JoinCallbackExpression, JoinReferenceExpression } from '../parser/join-parser.js';
5
- import { TableExpression, QueryBuilderWithTable, QueryBuilderWithLeftJoin, QueryBuilderWithRightJoin, QueryBuilderWithFullJoin } from '../parser/table-parser.js';
6
- import { SelectExpression, QueryBuilderWithSelection, SelectAllQueryBuilder } from '../parser/select-parser.js';
7
- import { ExistsExpression, FilterOperator } from '../parser/filter-parser.js';
8
- import { InsertObject } from '../parser/insert-values-parser.js';
9
- import { QueryBuilderWithReturning } from '../parser/returning-parser.js';
10
- import { ReferenceExpression } from '../parser/reference-parser.js';
11
- import { ValueExpressionOrList } from '../parser/value-parser.js';
12
- import { QueryNode } from '../operation-node/query-node.js';
13
- import { AnyColumn, AnyRawBuilder, SingleResultType } from '../util/type-utils.js';
14
- import { OrderByDirectionExpression, OrderByExpression } from '../parser/order-by-parser.js';
15
- import { MutationObject } from '../parser/update-set-parser.js';
16
- import { Compilable } from '../util/compilable.js';
17
- import { QueryExecutor } from '../query-executor/query-executor.js';
18
- import { QueryId } from '../util/query-id.js';
19
- import { OnConflictConstraintTarget } from '../parser/on-conflict-parser.js';
20
- import { ParseContext } from '../parser/parse-context.js';
21
- import { UnionExpression } from '../parser/union-parser.js';
22
- /**
23
- * The main query builder class.
24
- *
25
- * @typeParam DB - A type whose keys are table names/aliases and values are interfaces that
26
- * define the table's columns and their types. This type defines the tables, subqueries
27
- * etc. that are available to the query. This type contains all tables, even the ones
28
- * that have not actually been joined to the query. the `TB` parameter defines the
29
- * table names/aliases that have been joined to the query.
30
- *
31
- * @typeParam TB - The names/aliases of the tables that have been joined to the query
32
- * using `from`, `with` any join method and so on. This type is a union of `DB`
33
- * type's keys. For example `'person' | 'pet'`.
34
- *
35
- * @typePAram O - The query output row type.
36
- */
37
- export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements OperationNodeSource, Compilable {
38
- #private;
39
- constructor(props: QueryBuilderProps);
40
- /**
41
- * Adds a `where` clause to the query.
42
- *
43
- * Also see {@link QueryBuilder.whereExists | whereExists}, {@link QueryBuilder.whereRef | whereRef}
44
- * and {@link QueryBuilder.whereRef | orWhere}.
45
- *
46
- * @example
47
- * Find a row by column value:
48
- *
49
- * ```ts
50
- * const person = await db
51
- * .selectFrom('person')
52
- * .selectAll()
53
- * .where('id', '=', 100)
54
- * .executeTakeFirst()
55
- * ```
56
- *
57
- * The generated SQL (PostgreSQL):
58
- *
59
- * ```sql
60
- * select * from "person" where "id" = $1
61
- * ```
62
- *
63
- * @example
64
- * Operator can be any supported operator or if the typings don't support it
65
- * you can always use `db.raw('your operator')`.
66
- *
67
- * ```ts
68
- * const persons = await db
69
- * .selectFrom('person')
70
- * .selectAll()
71
- * .where('id', '>', 100)
72
- * .execute()
73
- * ```
74
- *
75
- * The generated SQL (PostgreSQL):
76
- *
77
- * ```sql
78
- * select * from "person" where "id" > $1
79
- * ```
80
- *
81
- * @example
82
- * The `where` methods don't change the type of the query. You can add
83
- * conditional `where`s easily by doing something like this:
84
- *
85
- * ```ts
86
- * let query = db
87
- * .selectFrom('person')
88
- * .selectAll()
89
- *
90
- * if (firstName) {
91
- * query = query.where('first_name', '=', firstName)
92
- * }
93
- *
94
- * const persons = await query.execute()
95
- * ```
96
- *
97
- * This is true for basically all methods execpt the `select` and
98
- * `returning` methods, which DO change the return type of the
99
- * query.
100
- *
101
- * @example
102
- * Both the first and third argument can also be subqueries.
103
- * A subquery is defined by passing a function and calling
104
- * the `subQuery` method of the object passed into the
105
- * function:
106
- *
107
- * ```ts
108
- * const persons = await db
109
- * .selectFrom('person')
110
- * .selectAll()
111
- * .where(
112
- * (qb) => qb.subQuery('pet')
113
- * .select('pet.name')
114
- * .whereRef('pet.owner_id', '=', 'person.id')
115
- * .limit(1),
116
- * '=',
117
- * 'Fluffy'
118
- * )
119
- * .execute()
120
- * ```
121
- *
122
- * The generated SQL (PostgreSQL):
123
- *
124
- * ```sql
125
- * select *
126
- * from "person"
127
- * where (
128
- * select "pet"."name"
129
- * from "pet"
130
- * where "pet"."owner_id" = "person"."id"
131
- * limit $1
132
- * ) = $2
133
- * ```
134
- *
135
- * @example
136
- * A `where in` query can be built by using the `in` operator and an array
137
- * of values. The values in the array can also be subqueries or raw
138
- * instances.
139
- *
140
- * ```ts
141
- * const persons = await db
142
- * .selectFrom('person')
143
- * .selectAll()
144
- * .where('person.id', 'in', [100, 200, 300])
145
- * .execute()
146
- * ```
147
- *
148
- * The generated SQL (PostgreSQL):
149
- *
150
- * ```sql
151
- * select * from "person" where "id" in ($1, $2, $3)
152
- * ```
153
- *
154
- * @example
155
- * If everything else fails, you can always pass {@link Kysely.raw | raw}
156
- * as any of the arguments, including the operator:
157
- *
158
- * ```ts
159
- * const persons = await db
160
- * .selectFrom('person')
161
- * .selectAll()
162
- * .where(
163
- * db.raw('coalesce(first_name, last_name)'),
164
- * 'like',
165
- * '%' + name + '%',
166
- * )
167
- * .execute()
168
- * ```
169
- *
170
- * The generated SQL (PostgreSQL):
171
- *
172
- * ```sql
173
- * select * from "person"
174
- * where coalesce(first_name, last_name) like $1
175
- * ```
176
- *
177
- * @example
178
- * If you only pass one function argument to this method, it can be
179
- * used to create parentheses around other where clauses:
180
- *
181
- * ```ts
182
- * const persons = await db
183
- * .selectFrom('person')
184
- * .selectAll()
185
- * .where((qb) => qb
186
- * .where('id', '=', 1)
187
- * .orWhere('id', '=', 2)
188
- * )
189
- * .execute()
190
- * ```
191
- *
192
- * The generated SQL (PostgreSQL):
193
- *
194
- * ```sql
195
- * select * from "person" (where "id" = 1 or "id" = 2)
196
- * ```
197
- *
198
- * @example
199
- * In all examples above the columns were known at compile time
200
- * (except for the `raw` expressions). By default kysely only allows
201
- * you to refer to columns that exist in the database **and** can be
202
- * referred to in the current query and context.
203
- *
204
- * Sometimes you may want to refer to columns that come from the user
205
- * input and thus are not available at compile time.
206
- *
207
- * You have two options, `db.raw` or `db.dynamic`. The example below
208
- * uses both:
209
- *
210
- * ```ts
211
- * const { ref } = db.dynamic
212
- *
213
- * const persons = await db
214
- * .selectFrom('person')
215
- * .selectAll()
216
- * .where(ref(columnFromUserInput), '=', 1)
217
- * .orWhere(db.raw('??', [columnFromUserInput]), '=', 2)
218
- * .execute()
219
- * ```
220
- */
221
- where<RE extends ReferenceExpression<DB, TB>>(lhs: RE, op: FilterOperator, rhs: ValueExpressionOrList<DB, TB, RE>): QueryBuilder<DB, TB, O>;
222
- where(grouper: (qb: QueryBuilder<DB, TB, O>) => QueryBuilder<DB, TB, O>): QueryBuilder<DB, TB, O>;
223
- where(raw: AnyRawBuilder): QueryBuilder<DB, TB, O>;
224
- /**
225
- * Adds a `where` clause where both sides of the operator are references
226
- * to columns.
227
- *
228
- * The normal `where` method treats the right hand side argument as a
229
- * value by default. `whereRef` treats it as a column reference. This method is
230
- * expecially useful with joins and correclated subqueries.
231
- *
232
- * @example
233
- * Usage with a join:
234
- *
235
- * ```ts
236
- * db.selectFrom(['person', 'pet'])
237
- * .selectAll()
238
- * .whereRef('person.first_name', '=', 'pet.name')
239
- * ```
240
- *
241
- * The generated SQL (PostgreSQL):
242
- *
243
- * ```sql
244
- * select * from "person", "pet" where "person"."first_name" = "pet"."name"
245
- * ```
246
- *
247
- * @example
248
- * Usage in a subquery:
249
- *
250
- * ```ts
251
- * const persons = await db
252
- * .selectFrom('person')
253
- * .selectAll('person')
254
- * .select((qb) => qb
255
- * .subQuery('pet')
256
- * .select('name')
257
- * .whereRef('pet.owner_id', '=', 'person.id')
258
- * .limit(1)
259
- * .as('pet_name')
260
- * )
261
- * .execute()
262
- * ```
263
- *
264
- * The generated SQL (PostgreSQL):
265
- *
266
- * ```sql
267
- * select "person".*, (
268
- * select "name"
269
- * from "pet"
270
- * where "pet"."owner_id" = "person"."id"
271
- * limit $1
272
- * ) as "pet_name"
273
- * from "person"
274
- */
275
- whereRef(lhs: ReferenceExpression<DB, TB>, op: FilterOperator, rhs: ReferenceExpression<DB, TB>): QueryBuilder<DB, TB, O>;
276
- /**
277
- * Adds an `or where` clause to the query. Otherwise works just like
278
- * {@link QueryBuilder.where | where}.
279
- *
280
- * It's often necessary to wrap `or where` clauses in parentheses to control
281
- * precendence. You can use the one argument version of the `where` method
282
- * for that. See the examples.
283
- *
284
- * @example
285
- * ```ts
286
- * const persons = await db
287
- * .selectFrom('person')
288
- * .selectAll()
289
- * .where('id', '=', 1)
290
- * .orWhere('id', '=', 2)
291
- * .execute()
292
- * ```
293
- *
294
- * The generated SQL (PostgreSQL):
295
- *
296
- * ```sql
297
- * select * from "person" where "id" = 1 or "id" = 2
298
- * ```
299
- *
300
- * @example
301
- * Grouping with parentheses:
302
- *
303
- * ```ts
304
- * const persons = await db
305
- * .selectFrom('person')
306
- * .selectAll()
307
- * .where((qb) => qb
308
- * .where('id', '=', 1)
309
- * .orWhere('id', '=', 2)
310
- * )
311
- * .execute()
312
- * ```
313
- *
314
- * The generated SQL (PostgreSQL):
315
- *
316
- * ```sql
317
- * select * from "person" (where "id" = 1 or "id" = 2)
318
- * ```
319
- *
320
- * @example
321
- * Even the first `where` can be an `orWhere`. This is useful
322
- * if you are looping through a set of conditions:
323
- *
324
- * ```ts
325
- * const persons = await db
326
- * .selectFrom('person')
327
- * .selectAll()
328
- * .where((qb) => qb
329
- * .orWhere('id', '=', 1)
330
- * .orWhere('id', '=', 2)
331
- * )
332
- * .execute()
333
- * ```
334
- *
335
- * The generated SQL (PostgreSQL):
336
- *
337
- * ```sql
338
- * select * from "person" (where "id" = 1 or "id" = 2)
339
- * ```
340
- */
341
- orWhere<RE extends ReferenceExpression<DB, TB>>(lhs: RE, op: FilterOperator, rhs: ValueExpressionOrList<DB, TB, RE>): QueryBuilder<DB, TB, O>;
342
- orWhere(grouper: (qb: QueryBuilder<DB, TB, O>) => QueryBuilder<DB, TB, O>): QueryBuilder<DB, TB, O>;
343
- orWhere(raw: AnyRawBuilder): QueryBuilder<DB, TB, O>;
344
- /**
345
- * Adds an `or where` clause to the query. Otherwise works just like
346
- * {@link QueryBuilder.whereRef | whereRef}.
347
- *
348
- * Also see {@link QueryBuilder.orWhere | orWhere} and {@link QueryBuilder.where | where}.
349
- */
350
- orWhereRef(lhs: ReferenceExpression<DB, TB>, op: FilterOperator, rhs: ReferenceExpression<DB, TB>): QueryBuilder<DB, TB, O>;
351
- /**
352
- * Adds a `where exists` clause to the query.
353
- *
354
- * You can either use a subquery or a raw instance.
355
- *
356
- * @example
357
- * The query below selets all persons that own a pet named Catto:
358
- *
359
- * ```ts
360
- * const persons = await db
361
- * .selectFrom('person')
362
- * .selectAll()
363
- * .whereExists((qb) => qb
364
- * .subQuery('pet')
365
- * .select('pet.id')
366
- * .whereRef('person.id', '=', 'pet.owner_id')
367
- * .where('pet.name', '=', 'Catto')
368
- * )
369
- * .execute()
370
- * ```
371
- *
372
- * The generated SQL (PostgreSQL):
373
- *
374
- * ```sql
375
- * select * from "person"
376
- * where exists (
377
- * select "pet"."id"
378
- * from "pet"
379
- * where "person"."id" = "pet"."owner_id"
380
- * and "pet"."name" = $1
381
- * )
382
- * ```
383
- *
384
- * @example
385
- * The same query as in the previous example but with using raw:
386
- *
387
- * ```ts
388
- * db.selectFrom('person')
389
- * .selectAll()
390
- * .whereExists(
391
- * db.raw(
392
- * '(select pet.id from pet where person.id = pet.owner_id and pet.name = ?)',
393
- * ['Catto']
394
- * )
395
- * )
396
- * ```
397
- *
398
- * The generated SQL (PostgreSQL):
399
- *
400
- * ```sql
401
- * select * from "person"
402
- * where exists (
403
- * select pet.id
404
- * from pet
405
- * where person.id = pet.owner_id
406
- * and pet.name = $1
407
- * )
408
- * ```
409
- */
410
- whereExists(arg: ExistsExpression<DB, TB>): QueryBuilder<DB, TB, O>;
411
- /**
412
- * Just like {@link QueryBuilder.whereExists | whereExists} but creates a `not exists` clause.
413
- */
414
- whereNotExists(arg: ExistsExpression<DB, TB>): QueryBuilder<DB, TB, O>;
415
- /**
416
- * Just like {@link QueryBuilder.whereExists | whereExists} but creates a `or exists` clause.
417
- */
418
- orWhereExists(arg: ExistsExpression<DB, TB>): QueryBuilder<DB, TB, O>;
419
- /**
420
- * Just like {@link QueryBuilder.whereExists | whereExists} but creates a `or not exists` clause.
421
- */
422
- orWhereNotExists(arg: ExistsExpression<DB, TB>): QueryBuilder<DB, TB, O>;
423
- /**
424
- * Just like {@link QueryBuilder.where | where} but adds a `having` statement
425
- * instead of a `where` statement.
426
- */
427
- having<RE extends ReferenceExpression<DB, TB>>(lhs: RE, op: FilterOperator, rhs: ValueExpressionOrList<DB, TB, RE>): QueryBuilder<DB, TB, O>;
428
- having(grouper: (qb: QueryBuilder<DB, TB, O>) => QueryBuilder<DB, TB, O>): QueryBuilder<DB, TB, O>;
429
- having(raw: AnyRawBuilder): QueryBuilder<DB, TB, O>;
430
- /**
431
- * Just like {@link QueryBuilder.whereRef | whereRef} but adds a `having` statement
432
- * instead of a `where` statement.
433
- */
434
- havingRef(lhs: ReferenceExpression<DB, TB>, op: FilterOperator, rhs: ReferenceExpression<DB, TB>): QueryBuilder<DB, TB, O>;
435
- /**
436
- * Just like {@link QueryBuilder.orWhere | orWhere} but adds a `having` statement
437
- * instead of a `where` statement.
438
- */
439
- orHaving<RE extends ReferenceExpression<DB, TB>>(lhs: RE, op: FilterOperator, rhs: ValueExpressionOrList<DB, TB, RE>): QueryBuilder<DB, TB, O>;
440
- orHaving(grouper: (qb: QueryBuilder<DB, TB, O>) => QueryBuilder<DB, TB, O>): QueryBuilder<DB, TB, O>;
441
- orHaving(raw: AnyRawBuilder): QueryBuilder<DB, TB, O>;
442
- /**
443
- * Just like {@link QueryBuilder.orWhereRef | orWhereRef} but adds a `having` statement
444
- * instead of a `where` statement.
445
- */
446
- orHavingRef(lhs: ReferenceExpression<DB, TB>, op: FilterOperator, rhs: ReferenceExpression<DB, TB>): QueryBuilder<DB, TB, O>;
447
- /**
448
- * Just like {@link QueryBuilder.whereExists | whereExists} but adds a `having` statement
449
- * instead of a `where` statement.
450
- */
451
- havingExists(arg: ExistsExpression<DB, TB>): QueryBuilder<DB, TB, O>;
452
- /**
453
- * Just like {@link QueryBuilder.whereNotExists | whereNotExists} but adds a `having` statement
454
- * instead of a `where` statement.
455
- */
456
- havingNotExist(arg: ExistsExpression<DB, TB>): QueryBuilder<DB, TB, O>;
457
- /**
458
- * Just like {@link QueryBuilder.orWhereExists | orWhereExists} but adds a `having` statement
459
- * instead of a `where` statement.
460
- */
461
- orHavingExists(arg: ExistsExpression<DB, TB>): QueryBuilder<DB, TB, O>;
462
- /**
463
- * Just like {@link QueryBuilder.orWhereNotExists | orWhereNotExists} but adds a `having` statement
464
- * instead of a `where` statement.
465
- */
466
- orHavingNotExists(arg: ExistsExpression<DB, TB>): QueryBuilder<DB, TB, O>;
467
- /**
468
- * Adds a select clause to the query.
469
- *
470
- * When a column (or any expression) is selected, Kysely adds its type to the return
471
- * type of the query. Kysely is smart enough to parse the column names and types even
472
- * from aliased columns, subqueries, raw expressions etc.
473
- *
474
- * Kysely only allows you to select columns and expressions that exist and would
475
- * produce valid SQL. However, Kysely is not perfect and there may be cases where
476
- * the type inference doesn't work and you need to override it. You can always
477
- * use the {@link Kysely.dynamic | dynamic} module and {@link Kysely.raw | raw}
478
- * to override the types.
479
- *
480
- * Select calls are additive. Calling `select('id').select('first_name')` is the
481
- * same as calling `select(['id', 'first_name']).
482
- *
483
- * To select all columns of the query or specific tables see the
484
- * {@link QueryBuilder.selectAll | selectAll} method.
485
- *
486
- * @example
487
- * Select a single column:
488
- *
489
- * ```ts
490
- * const persons = await db.selectFrom('person')
491
- * .select('id')
492
- * .where('first_name', '=', 'Arnold')
493
- * .execute()
494
- *
495
- * persons[0].id
496
- * ```
497
- *
498
- * The generated SQL (PostgreSQL):
499
- *
500
- * ```sql
501
- * select "id" from "person" where "first_name" = $1
502
- * ```
503
- *
504
- * @example
505
- * Select a single column and specify a table:
506
- *
507
- * ```ts
508
- * const persons = await db.selectFrom(['person', 'pet'])
509
- * .select('person.id')
510
- * .execute()
511
- *
512
- * persons[0].id
513
- * ```
514
- *
515
- * The generated SQL (PostgreSQL):
516
- *
517
- * ```sql
518
- * select "person"."id" from "person", "pet"
519
- * ```
520
- *
521
- * @example
522
- * Select multiple columns:
523
- *
524
- * ```ts
525
- * const persons = await db.selectFrom('person')
526
- * .select(['person.id', 'first_name'])
527
- * .execute()
528
- *
529
- * persons[0].id
530
- * persons[0].first_name
531
- * ```
532
- *
533
- * The generated SQL (PostgreSQL):
534
- *
535
- * ```sql
536
- * select "person"."id", "first_name" from "person"
537
- * ```
538
- *
539
- * @example
540
- * Aliased selections:
541
- *
542
- * ```ts
543
- * const persons = await db.selectFrom('person')
544
- * .select([
545
- * 'person.first_name as fn',
546
- * 'person.last_name as ln'
547
- * ])
548
- * .execute()
549
- *
550
- * persons[0].fn
551
- * persons[0].ln
552
- * ```
553
- *
554
- * The generated SQL (PostgreSQL):
555
- *
556
- * ```sql
557
- * select
558
- * "person"."first_name" as "fn",
559
- * "person"."last_name" as "ln"
560
- * from "person"
561
- * ```
562
- *
563
- * @example
564
- * You can also select subqueries and raw expressions. Note that you
565
- * always need to give a name for the selections using the `as`
566
- * method:
567
- *
568
- * ```ts
569
- * const persons = await db.selectFrom('person')
570
- * .select([
571
- * (qb) => qb
572
- * .subQuery('pet')
573
- * .whereRef('person.id', '=', 'pet.owner_id')
574
- * .select('pet.name')
575
- * .limit(1)
576
- * .as('pet_name')
577
- * db.raw<string>("concat(first_name, ' ', last_name)").as('full_name')
578
- * ])
579
- * .execute()
580
- *
581
- * persons[0].pet_name
582
- * persons[0].full_name
583
- * ```
584
- *
585
- * The generated SQL (PostgreSQL):
586
- *
587
- * ```sql
588
- * select
589
- * (
590
- * select "pet"."name"
591
- * from "pet"
592
- * where "person"."id" = "pet"."owner_id"
593
- * limit $1
594
- * ) as "pet_name",
595
- * concat(first_name, ' ', last_name) as full_name
596
- * from "person"
597
- * ```
598
- *
599
- * In case you use `raw` you need to specify the type of the expression
600
- * (in this example `string`).
601
- *
602
- * @example
603
- * All the examples above assume you know the column names at compile time.
604
- * While it's better to build your code like that (that way you also know
605
- * the types) sometimes it's not possible or you just prefer to write more
606
- * dynamic code.
607
- * <br><br>
608
- * In this example, we use the `dynamic` module's methods to add selections
609
- * dynamically:
610
- *
611
- * ```ts
612
- * const { ref } = db.dynamic
613
- *
614
- * // Some column name provided by the user. Value not known at compile time.
615
- * const columnFromUserInput = req.query.select;
616
- *
617
- * // A type that lists all possible values `columnFromUserInput` can have.
618
- * // You can use `keyof Person` if any column of an interface is allowed.
619
- * type PossibleColumns = 'last_name' | 'first_name' | 'birth_date'
620
- *
621
- * const spersons = await db.selectFrom('person')
622
- * .select([
623
- * ref<PossibleColumns>(columnFromUserInput)
624
- * 'id'
625
- * ])
626
- * .execute()
627
- *
628
- * // The resulting type contains all `PossibleColumns` as optional fields
629
- * // because we cannot know which field was actually selected before
630
- * // running the code.
631
- * const lastName: string | undefined = persons[0].last_name
632
- * const firstName: string | undefined = persons[0].first_name
633
- * const birthDate: string | undefined = persons[0].birth_date
634
- *
635
- * // The result type also contains the compile time selection `id`.
636
- * persons[0].id
637
- * ```
638
- */
639
- select<S extends SelectExpression<DB, TB>>(selections: ReadonlyArray<S>): QueryBuilderWithSelection<DB, TB, O, S>;
640
- select<S extends SelectExpression<DB, TB>>(selection: S): QueryBuilderWithSelection<DB, TB, O, S>;
641
- /**
642
- * Adds `distinct on` selections to the select clause.
643
- *
644
- * Takes the same inputs as the {@link QueryBuilder.select | select} method.
645
- * See the {@link QueryBuilder.select | select} method's documentation for
646
- * more examples.
647
- *
648
- * @example
649
- * ```ts
650
- * await db.selectFrom('person')
651
- * .innerJoin('pet', 'pet.owner_id', 'person.id')
652
- * .where('pet.name', '=', 'Doggo')
653
- * .distinctOn('person.id')
654
- * .selectAll('person')
655
- * .execute()
656
- * ```
657
- *
658
- * The generated SQL (PostgreSQL):
659
- *
660
- * ```sql
661
- * select distinct on ("person"."id") "person".*
662
- * from "person"
663
- * inner join "pet" on "pet"."owner_id" = "person"."id"
664
- * where "pet"."name" = $1
665
- * ```
666
- */
667
- distinctOn<S extends SelectExpression<DB, TB>>(selections: ReadonlyArray<S>): QueryBuilder<DB, TB, O>;
668
- distinctOn<S extends SelectExpression<DB, TB>>(selection: S): QueryBuilder<DB, TB, O>;
669
- /**
670
- * Makes the selection distinct.
671
- *
672
- * @example
673
- * ```ts
674
- * await db.selectFrom('person')
675
- * .select('first_name')
676
- * .distinct()
677
- * .execute()
678
- * ```
679
- *
680
- * The generated SQL (PostgreSQL):
681
- *
682
- * ```sql
683
- * select distinct "first_name" from "person"
684
- * ```
685
- */
686
- distinct(): QueryBuilder<DB, TB, O>;
687
- /**
688
- * Adds the `for update` option to a select query on supported databases.
689
- */
690
- forUpdate(): QueryBuilder<DB, TB, O>;
691
- /**
692
- * Adds the `for share` option to a select query on supported databases.
693
- */
694
- forShare(): QueryBuilder<DB, TB, O>;
695
- /**
696
- * Adds the `for key share` option to a select query on supported databases.
697
- */
698
- forKeyShare(): QueryBuilder<DB, TB, O>;
699
- /**
700
- * Adds the `for no key update` option to a select query on supported databases.
701
- */
702
- forNoKeyUpdate(): QueryBuilder<DB, TB, O>;
703
- /**
704
- * Adds the `skip locked` option to a select query on supported databases.
705
- */
706
- skipLocked(): QueryBuilder<DB, TB, O>;
707
- /**
708
- * Adds the `nowait` option to a select query on supported databases.
709
- */
710
- noWait(): QueryBuilder<DB, TB, O>;
711
- /**
712
- * Adds a `select *` or `select table.*` clause to the query.
713
- *
714
- * @example
715
- * ```ts
716
- * const persons = await db
717
- * .selectFrom('person')
718
- * .selectAll()
719
- * .execute()
720
- * ```
721
- *
722
- * The generated SQL (PostgreSQL):
723
- *
724
- * ```sql
725
- * select * from "person"
726
- * ```
727
- *
728
- * @example
729
- * ```ts
730
- * const persons = await db
731
- * .selectFrom('person')
732
- * .selectAll('person')
733
- * .execute()
734
- * ```
735
- *
736
- * The generated SQL (PostgreSQL):
737
- *
738
- * ```sql
739
- * select "person".* from "person"
740
- * ```
741
- *
742
- * @example
743
- * ```ts
744
- * const personsPets = await db
745
- * .selectFrom(['person', 'pet'])
746
- * .selectAll(['person', 'pet'])
747
- * .execute()
748
- * ```
749
- *
750
- * The generated SQL (PostgreSQL):
751
- *
752
- * ```sql
753
- * select "person".*, "pet".* from "person", "pet"
754
- * ```
755
- */
756
- selectAll<T extends TB>(table: ReadonlyArray<T>): SelectAllQueryBuilder<DB, TB, O, T>;
757
- selectAll<T extends TB>(table: T): SelectAllQueryBuilder<DB, TB, O, T>;
758
- selectAll<T extends TB>(): SelectAllQueryBuilder<DB, TB, O, T>;
759
- /**
760
- * Joins another table to the query using an inner join.
761
- *
762
- * @example
763
- * Simple usage by providing a table name and two columns to join:
764
- *
765
- * ```ts
766
- * const result = await db
767
- * .selectFrom('person')
768
- * .innerJoin('pet', 'pet.owner_id', 'person.id')
769
- * // `select` needs to come after the call to `innerJoin` so
770
- * // that you can select from the joined table.
771
- * .select('person.id', 'pet.name')
772
- * .execute()
773
- *
774
- * result[0].id
775
- * result[0].name
776
- * ```
777
- *
778
- * The generated SQL (PostgreSQL):
779
- *
780
- * ```sql
781
- * select "person"."id", "pet"."name"
782
- * from "person"
783
- * inner join "pet"
784
- * on "pet"."owner_id" = "person"."id"
785
- * ```
786
- *
787
- * @example
788
- * You can give an alias for the joined table like this:
789
- *
790
- * ```ts
791
- * await db.selectFrom('person')
792
- * .innerJoin('pet as p', 'p.owner_id', 'person.id')
793
- * .where('p.name', '=', 'Doggo')
794
- * .selectAll()
795
- * .execute()
796
- * ```
797
- *
798
- * The generated SQL (PostgreSQL):
799
- *
800
- * ```sql
801
- * select *
802
- * from "person"
803
- * inner join "pet" as "p"
804
- * on "p"."owner_id" = "person"."id"
805
- * where "p".name" = $1
806
- * ```
807
- *
808
- * @example
809
- * You can provide a function as the second argument to get a join
810
- * builder for creating more complex joins. The join builder has a
811
- * bunch of `on*` methods for building the `on` clause of the join.
812
- * There's basically an equivalent for every `where` method
813
- * (`on`, `onRef`, `onExists` etc.). You can do all the same things
814
- * with the `on` method that you can with the corresponding `where`
815
- * method. See the `where` method documentation for more examples.
816
- *
817
- * ```ts
818
- * await db.selectFrom('person')
819
- * .innerJoin(
820
- * 'pet',
821
- * (join) => join
822
- * .onRef('pet.owner_id', '=', 'person.id')
823
- * .on('pet.name', '=', 'Doggo')
824
- * )
825
- * .selectAll()
826
- * .execute()
827
- * ```
828
- *
829
- * The generated SQL (PostgreSQL):
830
- *
831
- * ```sql
832
- * select *
833
- * from "person"
834
- * inner join "pet"
835
- * on "pet"."owner_id" = "person"."id"
836
- * and "pet"."name" = $1
837
- * ```
838
- *
839
- * @example
840
- * You can join a subquery by providing a function as the first
841
- * argument:
842
- *
843
- * ```ts
844
- * await db.selectFrom('person')
845
- * .innerJoin(
846
- * (qb) => qb.subQuery('pet')
847
- * .select(['owner_id', 'name'])
848
- * .where('name', '=', 'Doggo')
849
- * .as('doggos'),
850
- * 'doggos.owner_id',
851
- * 'person.id',
852
- * )
853
- * .selectAll()
854
- * .execute()
855
- * ```
856
- *
857
- * The generated SQL (PostgreSQL):
858
- *
859
- * ```sql
860
- * select *
861
- * from "person"
862
- * inner join (
863
- * select "owner_id", "name"
864
- * from "pet"
865
- * where "name" = $1
866
- * ) as "doggos"
867
- * on "doggos"."owner_id" = "person"."id"
868
- * ```
869
- */
870
- innerJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): QueryBuilderWithTable<DB, TB, O, TE>;
871
- innerJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): QueryBuilderWithTable<DB, TB, O, TE>;
872
- /**
873
- * Just like {@link QueryBuilder.innerJoin | innerJoin} but adds a left join
874
- * instead of an inner join.
875
- */
876
- leftJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): QueryBuilderWithLeftJoin<DB, TB, O, TE>;
877
- leftJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): QueryBuilderWithLeftJoin<DB, TB, O, TE>;
878
- /**
879
- * Just like {@link QueryBuilder.innerJoin | innerJoin} but adds a right join
880
- * instead of an inner join.
881
- */
882
- rightJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): QueryBuilderWithRightJoin<DB, TB, O, TE>;
883
- rightJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): QueryBuilderWithRightJoin<DB, TB, O, TE>;
884
- /**
885
- * Just like {@link QueryBuilder.innerJoin | innerJoin} but adds a full join
886
- * instead of an inner join.
887
- */
888
- fullJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): QueryBuilderWithFullJoin<DB, TB, O, TE>;
889
- fullJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): QueryBuilderWithFullJoin<DB, TB, O, TE>;
890
- /**
891
- * Sets the values to insert for an {@link Kysely.insertInto | insert} query.
892
- *
893
- * This method takes an object whose keys are column names and values are
894
- * values to insert. In addition to the column's type, the values can be
895
- * {@link Kysely.raw | raw} instances, select queries or the
896
- * {@link Kysely.generated | generated} placeholder.
897
- *
898
- * You must provide all values defined by the interface of the table you
899
- * are inserting into. Values that are generated by the database like auto incrementing
900
- * identifiers can be marked with the {@link Kysely.generated} placeholder unless you
901
- * want to insert a specific value instead of the generated default.
902
- *
903
- * The return value of an `insert` query is an instance of {@link InsertResult}. The
904
- * {@link InsertResult.insertId | insertId} field holds the auto incremented primary
905
- * key if the database returned one.
906
- *
907
- * On PostgreSQL and some other dialects, you need to call `returning` to get
908
- * something out of the query.
909
- *
910
- * @example
911
- * Insert a row into `person`:
912
- * ```ts
913
- * const id = await db
914
- * .insertInto('person')
915
- * .values({
916
- * id: db.generated,
917
- * first_name: 'Jennifer',
918
- * last_name: 'Aniston'
919
- * })
920
- * .executeTakeFirstOrThrow()
921
- * ```
922
- *
923
- * The generated SQL (PostgreSQL):
924
- *
925
- * ```sql
926
- * insert into "person" ("first_name", "last_name") values ($1, $2)
927
- * ```
928
- *
929
- * @example
930
- * On dialects that support it (for example PostgreSQL) you can insert multiple
931
- * rows by providing an array. Note that the return value is once again very
932
- * dialect-specific. Some databases may only return the id of the *last* inserted
933
- * row and some return nothing at all unless you call `returning`.
934
- *
935
- * ```ts
936
- * await db
937
- * .insertInto('person')
938
- * .values([{
939
- * id: db.generated,
940
- * first_name: 'Jennifer',
941
- * last_name: 'Aniston'
942
- * }, {
943
- * id: db.generated,
944
- * first_name: 'Arnold',
945
- * last_name: 'Schwarzenegger',
946
- * }])
947
- * .execute()
948
- * ```
949
- *
950
- * The generated SQL (PostgreSQL):
951
- *
952
- * ```sql
953
- * insert into "person" ("first_name", "last_name") values (($1, $2), ($3, $4))
954
- * ```
955
- *
956
- * @example
957
- * On postgresql you need to chain `returning` to the query to get
958
- * the inserted row's columns (or any other expression) as the
959
- * return value:
960
- *
961
- * ```ts
962
- * const row = await db
963
- * .insertInto('person')
964
- * .values({
965
- * id: db.generated,
966
- * first_name: 'Jennifer',
967
- * last_name: 'Aniston'
968
- * })
969
- * .returning('id')
970
- * .executeTakeFirstOrThrow()
971
- *
972
- * row.id
973
- * ```
974
- *
975
- * The generated SQL (PostgreSQL):
976
- *
977
- * ```sql
978
- * insert into "person" ("first_name", "last_name") values ($1, $2) returning "id"
979
- * ```
980
- *
981
- * @example
982
- * In addition to primitives, the values can also be `raw` expressions or
983
- * select queries:
984
- *
985
- * ```ts
986
- * const result = await db
987
- * .insertInto('person')
988
- * .values({
989
- * id: db.generated,
990
- * first_name: 'Jennifer',
991
- * last_name: db.raw('? || ?', ['Ani', 'ston']),
992
- * age: db.selectFrom('person').select(raw('avg(age)')),
993
- * })
994
- * .executeTakeFirst()
995
- *
996
- * console.log(result.insertId)
997
- * ```
998
- *
999
- * The generated SQL (PostgreSQL):
1000
- *
1001
- * ```sql
1002
- * insert into "person" ("first_name", "last_name", "age")
1003
- * values ($1, $2 || $3, (select avg(age) from "person"))
1004
- * ```
1005
- *
1006
- * @example
1007
- * You can also use the callback version of subqueries or raw expressions:
1008
- *
1009
- * ```ts
1010
- * db.with('jennifer', (db) => db
1011
- * .selectFrom('person')
1012
- * .where('first_name', '=', 'Jennifer')
1013
- * .select(['id', 'first_name', 'gender'])
1014
- * .limit(1)
1015
- * ).insertInto('pet').values({
1016
- * id: db.generated,
1017
- * owner_id: (eb) => eb.subQuery('jennifer').select('id'),
1018
- * name: (eb) => eb.subQuery('jennifer').select('first_name'),
1019
- * species: 'cat',
1020
- * })
1021
- * ```
1022
- */
1023
- values(row: InsertObject<DB, TB>): QueryBuilder<DB, TB, O>;
1024
- values(row: ReadonlyArray<InsertObject<DB, TB>>): QueryBuilder<DB, TB, O>;
1025
- /**
1026
- * Ignores the insert if the column or constraint given as the first
1027
- * argument conflicts with the current rows in the database. The
1028
- * default behavior without this method call is to throw an error.
1029
- *
1030
- * For example if a table has a field `name` that has a unique constraint
1031
- * and you try to insert a row with a `name` that already exists in the
1032
- * database, calling `onConflictDoNothing('name')` will ignore the conflict
1033
- * and do nothing. By default the query would throw.
1034
- *
1035
- * Only some dialects like PostgreSQL and SQLite implement the `on conflict`
1036
- * statement. On MySQL you should use the {@link ignore} method to achieve
1037
- * similar results.
1038
- *
1039
- * Also see the {@link QueryBuilder.onConflictUpdate | onConflictUpdate}
1040
- * method if you want to perform an update in case of a conflict (upsert).
1041
- *
1042
- * @example
1043
- * ```ts
1044
- * await db
1045
- * .insertInto('pet')
1046
- * .values({
1047
- * id: db.generated,
1048
- * name: 'Catto',
1049
- * species: 'cat',
1050
- * })
1051
- * .onConflictDoNothing('name')
1052
- * .execute()
1053
- * ```
1054
- *
1055
- * The generated SQL (PostgreSQL):
1056
- *
1057
- * ```sql
1058
- * insert into "pet" ("name", "species")
1059
- * values ($1, $2)
1060
- * on conflict ("name") do nothing
1061
- * ```
1062
- *
1063
- * @example
1064
- * You can provied the name of the constraint instead of a column name
1065
- *
1066
- * ```ts
1067
- * await db
1068
- * .insertInto('pet')
1069
- * .values({
1070
- * id: db.generated,
1071
- * name: 'Catto',
1072
- * species: 'cat',
1073
- * })
1074
- * .onConflictDoNothing({
1075
- * constraint: 'pet_name_key'
1076
- * })
1077
- * .execute()
1078
- * ```
1079
- *
1080
- * The generated SQL (PostgreSQL):
1081
- *
1082
- * ```sql
1083
- * insert into "pet" ("name", "species")
1084
- * values ($1, $2)
1085
- * on conflict on constraint "pet_name_key"
1086
- * do nothing
1087
- * ```
1088
- */
1089
- onConflictDoNothing(column: AnyColumn<DB, TB>): QueryBuilder<DB, TB, O>;
1090
- onConflictDoNothing(columns: ReadonlyArray<AnyColumn<DB, TB>>): QueryBuilder<DB, TB, O>;
1091
- onConflictDoNothing(constraint: OnConflictConstraintTarget): QueryBuilder<DB, TB, O>;
1092
- /**
1093
- * Changes an `insert into` query to an `insert ignore into` query.
1094
- *
1095
- * If you use the ignore modifier, ignorable errors that occur while executing the
1096
- * insert statement are ignored. For example, without ignore, a row that duplicates
1097
- * an existing unique index or primary key value in the table causes a duplicate-key
1098
- * error and the statement is aborted. With ignore, the row is discarded and no error
1099
- * occurs.
1100
- *
1101
- * This is only supported on some dialects like MySQL. On most dialects you should
1102
- * use the {@link onConflictDoNothing} method.
1103
- *
1104
- * @example
1105
- * ```ts
1106
- * await db.insertInto('person')
1107
- * .ignore()
1108
- * .values(values)
1109
- * .execute()
1110
- * ```
1111
- */
1112
- ignore(): QueryBuilder<DB, TB, O>;
1113
- /**
1114
- * Ignores an insert if the column or constraint given as the first
1115
- * argument conflicts with the current rows in the database and
1116
- * performs an update on the conflicting row instead. This method
1117
- * can be used to implement an upsert operation.
1118
- *
1119
- * For example if a table has a field `name` that has a unique constraint
1120
- * and you try to insert a row with a `name` that already exists in the
1121
- * database, calling `onConfictUpdate('name', { species: 'hamster' })`
1122
- * will set the conflicting row's `species` field to value `'hamster'`.
1123
- * By default the query would throw.
1124
- *
1125
- * The second argument (updates) can be anything the {@link QueryBuilder.set | set}
1126
- * method accepts.
1127
- *
1128
- * The `on conflict do update` statement is only implemented by some dialects
1129
- * like PostgreSQL and SQLite. On MySQL you should use the {@link onDuplicateKeyUpdate}
1130
- * method instead.
1131
- *
1132
- * Also see the {@link QueryBuilder.onConflictDoNothing | onConflictDoNothing}
1133
- * method.
1134
- *
1135
- * @example
1136
- * ```ts
1137
- * await db
1138
- * .insertInto('pet')
1139
- * .values({
1140
- * id: db.generated,
1141
- * name: 'Catto',
1142
- * species: 'cat',
1143
- * })
1144
- * .onConflictUpdate('name', { species: 'hamster' })
1145
- * .execute()
1146
- * ```
1147
- *
1148
- * The generated SQL (PostgreSQL):
1149
- *
1150
- * ```sql
1151
- * insert into "pet" ("name", "species")
1152
- * values ($1, $2)
1153
- * on conflict ("name") do update set "species" = $3
1154
- * ```
1155
- *
1156
- * @example
1157
- * You can provied the name of the constraint instead of a column name
1158
- *
1159
- * ```ts
1160
- * await db
1161
- * .insertInto('pet')
1162
- * .values({
1163
- * id: db.generated,
1164
- * name: 'Catto',
1165
- * species: 'cat',
1166
- * })
1167
- * .onConflictUpdate(
1168
- * { constraint: 'pet_name_key' },
1169
- * { species: 'hamster' }
1170
- * )
1171
- * .execute()
1172
- * ```
1173
- *
1174
- * The generated SQL (PostgreSQL):
1175
- *
1176
- * ```sql
1177
- * insert into "pet" ("name", "species")
1178
- * values ($1, $2)
1179
- * on conflict on constraint "pet_name_key"
1180
- * do update set "species" = $3
1181
- * ```
1182
- */
1183
- onConflictUpdate(column: AnyColumn<DB, TB>, updates: MutationObject<DB, TB>): QueryBuilder<DB, TB, O>;
1184
- onConflictUpdate(columns: ReadonlyArray<AnyColumn<DB, TB>>, updates: MutationObject<DB, TB>): QueryBuilder<DB, TB, O>;
1185
- onConflictUpdate(constraint: OnConflictConstraintTarget, updates: MutationObject<DB, TB>): QueryBuilder<DB, TB, O>;
1186
- /**
1187
- * Adds `on duplicate key update` to an insert query.
1188
- *
1189
- * If you specify `on duplicate key update`, and a row is inserted that would cause
1190
- * a duplicate value in a unique index or primary key, an update of the old row occurs.
1191
- *
1192
- * This is only implemented by some dialects like MySQL. On most dialects you should
1193
- * use {@link onConflictUpdate} instead.
1194
- *
1195
- * @example
1196
- * ```ts
1197
- * await db
1198
- * .insertInto('person')
1199
- * .values(values)
1200
- * .onDuplicateKeyUpdate({ species: 'hamster' })
1201
- * ```
1202
- */
1203
- onDuplicateKeyUpdate(updates: MutationObject<DB, TB>): QueryBuilder<DB, TB, O>;
1204
- /**
1205
- * Sets the values to update for an {@link Kysely.updateTable | update} query.
1206
- *
1207
- * This method takes an object whose keys are column names and values are
1208
- * values to update. In addition to the column's type, the values can be `raw`
1209
- * instances or select queries.
1210
- *
1211
- * The return value of an update query is an instance of {@link UpdateResult}.
1212
- * You can use the {@link QueryBuilder.returning | returning} method on
1213
- * supported databases to get out the updated rows.
1214
- *
1215
- * @example
1216
- * Update a row in `person` table:
1217
- *
1218
- * ```ts
1219
- * const result = await db
1220
- * .updateTable('person')
1221
- * .set({
1222
- * first_name: 'Jennifer',
1223
- * last_name: 'Aniston'
1224
- * })
1225
- * .where('id', '=', 1)
1226
- * .executeTakeFirst()
1227
- *
1228
- * console.log(result.numUpdatedRows)
1229
- * ```
1230
- *
1231
- * The generated SQL (PostgreSQL):
1232
- *
1233
- * ```sql
1234
- * update "person" set "first_name" = $1, "last_name" = $2 where "id" = $3
1235
- * ```
1236
- *
1237
- * @example
1238
- * On postgresql you ca chain `returning` to the query to get
1239
- * the updated rows' columns (or any other expression) as the
1240
- * return value:
1241
- *
1242
- * ```ts
1243
- * const row = await db
1244
- * .updateTable('person')
1245
- * .set({
1246
- * first_name: 'Jennifer',
1247
- * last_name: 'Aniston'
1248
- * })
1249
- * .where('id', '=', 1)
1250
- * .returning('id')
1251
- * .executeTakeFirstOrThrow()
1252
- *
1253
- * row.id
1254
- * ```
1255
- *
1256
- * The generated SQL (PostgreSQL):
1257
- *
1258
- * ```sql
1259
- * update "person" set "first_name" = $1, "last_name" = $2 where "id" = $3 returning "id"
1260
- * ```
1261
- *
1262
- * @example
1263
- * In addition to primitives, the values can also be `raw` expressions or
1264
- * select queries:
1265
- *
1266
- * ```ts
1267
- * const result = await db
1268
- * .updateTable('person')
1269
- * .set({
1270
- * first_name: 'Jennifer',
1271
- * last_name: db.raw('? || ?', ['Ani', 'ston']),
1272
- * age: db.selectFrom('person').select(raw('avg(age)')),
1273
- * })
1274
- * .where('id', '=', 1)
1275
- * .executeTakeFirst()
1276
- *
1277
- * console.log(result.numUpdatedRows)
1278
- * ```
1279
- *
1280
- * The generated SQL (PostgreSQL):
1281
- *
1282
- * ```sql
1283
- * update "person" set
1284
- * "first_name" = $1,
1285
- * "last_name" = $2 || $3,
1286
- * "age" = (select avg(age) from "person")
1287
- * where "id" = $4
1288
- * ```
1289
- */
1290
- set(row: MutationObject<DB, TB>): QueryBuilder<DB, TB, O>;
1291
- /**
1292
- * Allows you to return data from modified rows.
1293
- *
1294
- * On supported databases like PostgreSQL, this method can be chained to
1295
- * `insert`, `update` and `delete` queries to return data.
1296
- *
1297
- * Note that on SQLite you need to give aliases for the expressions to avoid
1298
- * [this bug](https://sqlite.org/forum/forumpost/033daf0b32) in SQLite.
1299
- * For example `.returning('id as id')`.
1300
- *
1301
- * Also see the {@link QueryBuilder.returningAll | returningAll} method.
1302
- *
1303
- * @example
1304
- * Return one column:
1305
- *
1306
- * ```ts
1307
- * const { id } = await db
1308
- * .insertInto('person')
1309
- * .values({
1310
- * id: db.generated,
1311
- * first_name: 'Jennifer',
1312
- * last_name: 'Aniston'
1313
- * })
1314
- * .returning('id')
1315
- * .executeTakeFirst()
1316
- * ```
1317
- *
1318
- * @example
1319
- * Return multiple columns:
1320
- *
1321
- * ```ts
1322
- * const { id, first_name } = await db
1323
- * .insertInto('person')
1324
- * .values({
1325
- * id: db.generated,
1326
- * first_name: 'Jennifer',
1327
- * last_name: 'Aniston'
1328
- * })
1329
- * .returning(['id', 'last_name'])
1330
- * .executeTakeFirst()
1331
- * ```
1332
- *
1333
- * @example
1334
- * Return arbitrary expressions:
1335
- *
1336
- * ```ts
1337
- * const { raw } = db
1338
- *
1339
- * const { id, full_name, first_pet_id } = await db
1340
- * .insertInto('person')
1341
- * .values({
1342
- * id: db.generated,
1343
- * first_name: 'Jennifer',
1344
- * last_name: 'Aniston'
1345
- * })
1346
- * .returning([
1347
- * 'id as id',
1348
- * raw<string>(`concat(first_name, ' ', last_name)`).as('full_name'),
1349
- * (qb) => qb.subQuery('pets').select('pet.id').limit(1).as('first_pet_id')
1350
- * ])
1351
- * .executeTakeFirst()
1352
- * ```
1353
- */
1354
- returning<S extends SelectExpression<DB, TB>>(selections: ReadonlyArray<S>): QueryBuilderWithReturning<DB, TB, O, S>;
1355
- returning<S extends SelectExpression<DB, TB>>(selection: S): QueryBuilderWithReturning<DB, TB, O, S>;
1356
- /**
1357
- * Adds a `returning *` to an insert/update/delete query on databases
1358
- * that support `returning` such as PostgreSQL.
1359
- */
1360
- returningAll(): QueryBuilder<DB, TB, DB[TB]>;
1361
- /**
1362
- * Adds an `order by` clause to the query.
1363
- *
1364
- * `orderBy` calls are additive. To order by multiple columns, call `orderBy`
1365
- * multiple times.
1366
- *
1367
- * The first argument is the expression to order by and the second is the
1368
- * order (`asc` or `desc`).
1369
- *
1370
- * @example
1371
- * ```ts
1372
- * await db
1373
- * .selectFrom('person')
1374
- * .select('person.first_name as fn')
1375
- * .orderBy('id')
1376
- * .orderBy('fn', 'desc')
1377
- * .execute()
1378
- * ```
1379
- *
1380
- * The generated SQL (PostgreSQL):
1381
- *
1382
- * ```sql
1383
- * select "person"."first_name" as "fn"
1384
- * from "person"
1385
- * order by "id" asc, "fn" desc
1386
- * ```
1387
- *
1388
- * @example
1389
- * The order by expression can also be a `raw` expression or a subquery
1390
- * in addition to column references:
1391
- *
1392
- * ```ts
1393
- * await db
1394
- * .selectFrom('person')
1395
- * .selectAll()
1396
- * .orderBy((qb) => qb.subQuery('pet')
1397
- * .select('pet.name')
1398
- * .whereRef('pet.owner_id', '=', 'person.id')
1399
- * .limit(1)
1400
- * )
1401
- * .orderBy(
1402
- * db.raw('concat(first_name, last_name)')
1403
- * )
1404
- * .execute()
1405
- * ```
1406
- *
1407
- * The generated SQL (PostgreSQL):
1408
- *
1409
- * ```sql
1410
- * select *
1411
- * from "person"
1412
- * order by
1413
- * ( select "pet"."name"
1414
- * from "pet"
1415
- * where "pet"."owner_id" = "person"."id"
1416
- * limit 1
1417
- * ) asc,
1418
- * concat(first_name, last_name) asc
1419
- * ```
1420
- *
1421
- * @example
1422
- * `dynamic.ref` can be used to refer to columns not known at
1423
- * compile time:
1424
- *
1425
- * ```ts
1426
- * async function someQuery(orderBy: string) {
1427
- * const { ref } = db.dynamic
1428
- *
1429
- * return await db
1430
- * .selectFrom('person')
1431
- * .select('person.first_name as fn')
1432
- * .orderBy(ref(orderBy))
1433
- * .execute()
1434
- * }
1435
- *
1436
- * someQuery('fn')
1437
- * ```
1438
- *
1439
- * The generated SQL (PostgreSQL):
1440
- *
1441
- * ```sql
1442
- * select "person"."first_name" as "fn"
1443
- * from "person"
1444
- * order by "fn" asc
1445
- * ```
1446
- */
1447
- orderBy(orderBy: OrderByExpression<DB, TB, O>, direction?: OrderByDirectionExpression): QueryBuilder<DB, TB, O>;
1448
- /**
1449
- * Adds a `group by` clause to the query.
1450
- *
1451
- * @example
1452
- * ```ts
1453
- * await db
1454
- * .selectFrom('person')
1455
- * .select([
1456
- * 'first_name',
1457
- * db.raw('max(id)').as('max_id')
1458
- * ])
1459
- * .groupBy('first_name')
1460
- * .execute()
1461
- * ```
1462
- *
1463
- * The generated SQL (PostgreSQL):
1464
- *
1465
- * ```sql
1466
- * select "first_name", max(id)
1467
- * from "person"
1468
- * group by "first_name"
1469
- * ```
1470
- *
1471
- * @example
1472
- * `groupBy` also accepts an array:
1473
- *
1474
- * ```ts
1475
- * const { raw } = db
1476
- *
1477
- * await db
1478
- * .selectFrom('person')
1479
- * .select([
1480
- * 'first_name',
1481
- * 'last_name',
1482
- * raw('max(id)').as('max_id')
1483
- * ])
1484
- * .groupBy([
1485
- * 'first_name',
1486
- * 'last_name'
1487
- * ])
1488
- * .execute()
1489
- * ```
1490
- *
1491
- * The generated SQL (PostgreSQL):
1492
- *
1493
- * ```sql
1494
- * select "first_name", "last_name", max(id)
1495
- * from "person"
1496
- * group by "first_name", "last_name"
1497
- * ```
1498
- *
1499
- * @example
1500
- * The group by expressions can also be subqueries or
1501
- * raw expressions:
1502
- *
1503
- * ```ts
1504
- * const { raw } = db
1505
- *
1506
- * await db
1507
- * .selectFrom('person')
1508
- * .select([
1509
- * 'first_name',
1510
- * 'last_name',
1511
- * raw('max(id)').as('max_id')
1512
- * ])
1513
- * .groupBy([
1514
- * raw('concat(first_name, last_name)'),
1515
- * (qb) => qb.subQuery('pet').select('id').limit(1)
1516
- * ])
1517
- * .execute()
1518
- * ```
1519
- *
1520
- * @example
1521
- * `dynamic.ref` can be used to refer to columns not known at
1522
- * compile time:
1523
- *
1524
- * ```ts
1525
- * async function someQuery(groupBy: string) {
1526
- * const { ref } = db.dynamic
1527
- *
1528
- * return await db
1529
- * .selectFrom('person')
1530
- * .select('first_name')
1531
- * .groupBy(ref(groupBy))
1532
- * .execute()
1533
- * }
1534
- *
1535
- * someQuery('first_name')
1536
- * ```
1537
- *
1538
- * The generated SQL (PostgreSQL):
1539
- *
1540
- * ```sql
1541
- * select "first_name"
1542
- * from "person"
1543
- * group by "first_name"
1544
- * ```
1545
- */
1546
- groupBy(orderBy: ReadonlyArray<ReferenceExpression<DB, TB>>): QueryBuilder<DB, TB, O>;
1547
- groupBy(orderBy: ReferenceExpression<DB, TB>): QueryBuilder<DB, TB, O>;
1548
- /**
1549
- * Adds a limit clause to the query.
1550
- *
1551
- * @example
1552
- * Select the first 10 rows of the result:
1553
- *
1554
- * ```ts
1555
- * return await db
1556
- * .selectFrom('person')
1557
- * .select('first_name')
1558
- * .limit(10)
1559
- * ```
1560
- *
1561
- * @example
1562
- * Select rows from index 10 to index 19 of the result:
1563
- *
1564
- * ```ts
1565
- * return await db
1566
- * .selectFrom('person')
1567
- * .select('first_name')
1568
- * .offset(10)
1569
- * .limit(10)
1570
- * ```
1571
- */
1572
- limit(limit: number): QueryBuilder<DB, TB, O>;
1573
- /**
1574
- * Adds an offset clause to the query.
1575
- *
1576
- * @example
1577
- * Select rows from index 10 to index 19 of the result:
1578
- *
1579
- * ```ts
1580
- * return await db
1581
- * .selectFrom('person')
1582
- * .select('first_name')
1583
- * .offset(10)
1584
- * .limit(10)
1585
- * ```
1586
- */
1587
- offset(offset: number): QueryBuilder<DB, TB, O>;
1588
- /**
1589
- * Combines another select query or raw expression to this query using `union`.
1590
- *
1591
- * The output row type of the combined query must match `this` query.
1592
- *
1593
- * @xample
1594
- * ```ts
1595
- * db.selectFrom('person')
1596
- * .select(['id', 'first_name as name'])
1597
- * .union(db.selectFrom('pet').select(['id', 'name']))
1598
- * .orderBy('name')
1599
- * ```
1600
- */
1601
- union(expression: UnionExpression<DB, O>): QueryBuilder<DB, TB, O>;
1602
- /**
1603
- * Combines another select query or raw expression to this query using `union all`.
1604
- *
1605
- * The output row type of the combined query must match `this` query.
1606
- *
1607
- * @xample
1608
- * ```ts
1609
- * db.selectFrom('person')
1610
- * .select(['id', 'first_name as name'])
1611
- * .unionAll(db.selectFrom('pet').select(['id', 'name']))
1612
- * .orderBy('name')
1613
- * ```
1614
- */
1615
- unionAll(expression: UnionExpression<DB, O>): QueryBuilder<DB, TB, O>;
1616
- /**
1617
- * Simply calls the given function passing `this` as the only argument.
1618
- *
1619
- * This method can be useful when adding optional method calls:
1620
- *
1621
- * @example
1622
- * ```ts
1623
- * db.selectFrom('person')
1624
- * .selectAll()
1625
- * .call((qb) => {
1626
- * if (something) {
1627
- * return qb.where('something', '=', something)
1628
- * } else {
1629
- * return qb.where('somethingElse', '=', somethingElse)
1630
- * }
1631
- * })
1632
- * .execute()
1633
- * ```
1634
- *
1635
- * The next example uses a helper funtion `log` to log a query:
1636
- *
1637
- * @example
1638
- * ```ts
1639
- * function log<T extends AnyQueryBuilder>(qb: T): T {
1640
- * console.log(qb.compile())
1641
- * return qb
1642
- * }
1643
- *
1644
- * db.selectFrom('person')
1645
- * .selectAll()
1646
- * .call(log)
1647
- * .execute()
1648
- * ```
1649
- */
1650
- call<T>(func: (qb: this) => T): T;
1651
- /**
1652
- * Gives an alias for the query. This method is only useful for sub queries.
1653
- *
1654
- * @example
1655
- * ```ts
1656
- * await db.selectFrom('pet')
1657
- * .selectAll('pet')
1658
- * .select(
1659
- * (qb) => qb.subQuery('person')
1660
- * .select('first_name')
1661
- * .whereRef('pet.owner_id', '=', 'person.id')
1662
- * .as('owner_first_name')
1663
- * )
1664
- * .execute()
1665
- * ```
1666
- */
1667
- as<A extends string>(alias: A): AliasedQueryBuilder<DB, TB, O, A>;
1668
- /**
1669
- * Change the output type of the query.
1670
- *
1671
- * You should only use this method as the last resort if the types
1672
- * don't support your use case.
1673
- */
1674
- castTo<T>(): QueryBuilder<DB, TB, T>;
1675
- toOperationNode(): QueryNode;
1676
- compile(): CompiledQuery;
1677
- /**
1678
- * Executes the query and returns an array of rows.
1679
- *
1680
- * Also see the {@link executeTakeFirst} and {@link executeTakeFirstOrThrow} methods.
1681
- */
1682
- execute(): Promise<O[]>;
1683
- /**
1684
- * Executes the query and returns the first result or undefined if
1685
- * the query returned no result.
1686
- */
1687
- executeTakeFirst(): Promise<SingleResultType<O>>;
1688
- /**
1689
- * Executes the query and returns the first result or throws if
1690
- * the query returned no result.
1691
- *
1692
- * By default an instance of {@link NoResultError} is thrown, but you can
1693
- * provide a custom error class as the only argument to throw a different
1694
- * error.
1695
- */
1696
- executeTakeFirstOrThrow(errorConstructor?: NoResultErrorConstructor): Promise<O>;
1697
- }
1698
- export interface QueryBuilderProps {
1699
- readonly queryId: QueryId;
1700
- readonly queryNode: QueryNode;
1701
- readonly executor: QueryExecutor;
1702
- readonly parseContext: ParseContext;
1703
- }
1704
- /**
1705
- * {@link QueryBuilder} with an alias. The result of calling {@link QueryBuilder.as}.
1706
- */
1707
- export declare class AliasedQueryBuilder<DB, TB extends keyof DB, O = undefined, A extends string = never> {
1708
- #private;
1709
- constructor(queryBuilder: QueryBuilder<DB, TB, O>, alias: A);
1710
- /**
1711
- * @private
1712
- *
1713
- * This needs to be here just so that the typings work. Without this
1714
- * the generated .d.ts file contains no reference to the type param A
1715
- * which causes this type to be equal to AliasedQueryBuilder with any A
1716
- * as long as D, TB and O are the same.
1717
- */
1718
- protected get alias(): A;
1719
- toOperationNode(): AliasNode;
1720
- }
1721
- export declare type NoResultErrorConstructor = new (node: QueryNode) => Error;
1722
- export declare class NoResultError extends Error {
1723
- /**
1724
- * The operation node tree of the query that was executed.
1725
- */
1726
- readonly node: QueryNode;
1727
- constructor(node: QueryNode);
1728
- }