kysely 0.23.3 → 0.23.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -25
- package/dist/cjs/dialect/database-introspector.d.ts +4 -2
- package/dist/cjs/dialect/dialect-adapter-base.d.ts +32 -6
- package/dist/cjs/dialect/dialect-adapter.d.ts +50 -8
- package/dist/cjs/dialect/mysql/mysql-adapter.d.ts +32 -6
- package/dist/cjs/dialect/mysql/mysql-introspector.d.ts +1 -1
- package/dist/cjs/dialect/mysql/mysql-introspector.js +21 -15
- package/dist/cjs/dialect/postgres/postgres-adapter.d.ts +32 -6
- package/dist/cjs/dialect/postgres/postgres-introspector.d.ts +1 -1
- package/dist/cjs/dialect/postgres/postgres-introspector.js +5 -3
- package/dist/cjs/dialect/sqlite/sqlite-adapter.d.ts +32 -6
- package/dist/cjs/dialect/sqlite/sqlite-introspector.d.ts +1 -1
- package/dist/cjs/dialect/sqlite/sqlite-introspector.js +8 -7
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/migration/migrator.d.ts +8 -3
- package/dist/cjs/migration/migrator.js +15 -5
- package/dist/cjs/operation-node/aggregate-function-node.d.ts +3 -2
- package/dist/cjs/operation-node/aggregate-function-node.js +2 -2
- package/dist/cjs/operation-node/create-index-node.d.ts +1 -0
- package/dist/cjs/operation-node/delete-query-node.d.ts +4 -1
- package/dist/cjs/operation-node/delete-query-node.js +11 -2
- package/dist/cjs/operation-node/operation-node-transformer.d.ts +2 -0
- package/dist/cjs/operation-node/operation-node-transformer.js +10 -1
- package/dist/cjs/operation-node/operation-node-visitor.d.ts +2 -0
- package/dist/cjs/operation-node/operation-node-visitor.js +1 -0
- package/dist/cjs/operation-node/operation-node.d.ts +1 -1
- package/dist/cjs/operation-node/operator-node.d.ts +1 -1
- package/dist/cjs/operation-node/operator-node.js +1 -0
- package/dist/cjs/operation-node/using-node.d.ts +13 -0
- package/dist/cjs/operation-node/using-node.js +24 -0
- package/dist/cjs/parser/expression-parser.js +1 -1
- package/dist/cjs/parser/table-parser.d.ts +1 -0
- package/dist/cjs/parser/update-set-parser.d.ts +1 -1
- package/dist/cjs/plugin/camel-case/camel-case-plugin.d.ts +7 -0
- package/dist/cjs/plugin/camel-case/camel-case-plugin.js +10 -4
- package/dist/cjs/query-builder/delete-query-builder.d.ts +103 -11
- package/dist/cjs/query-builder/delete-query-builder.js +40 -9
- package/dist/cjs/query-builder/function-module.d.ts +273 -39
- package/dist/cjs/query-builder/function-module.js +156 -34
- package/dist/cjs/query-builder/insert-query-builder.d.ts +25 -10
- package/dist/cjs/query-builder/insert-query-builder.js +32 -9
- package/dist/cjs/query-builder/select-query-builder.d.ts +51 -34
- package/dist/cjs/query-builder/select-query-builder.js +53 -29
- package/dist/cjs/query-builder/update-query-builder.d.ts +26 -10
- package/dist/cjs/query-builder/update-query-builder.js +33 -9
- package/dist/cjs/query-compiler/default-query-compiler.d.ts +2 -0
- package/dist/cjs/query-compiler/default-query-compiler.js +12 -1
- package/dist/cjs/query-creator.d.ts +29 -0
- package/dist/cjs/query-creator.js +2 -21
- package/dist/cjs/raw-builder/raw-builder.d.ts +4 -0
- package/dist/cjs/raw-builder/raw-builder.js +8 -2
- package/dist/cjs/schema/alter-table-builder.d.ts +5 -1
- package/dist/cjs/schema/alter-table-builder.js +8 -2
- package/dist/cjs/schema/create-index-builder.d.ts +6 -0
- package/dist/cjs/schema/create-index-builder.js +13 -0
- package/dist/cjs/schema/create-table-builder.d.ts +6 -2
- package/dist/cjs/schema/create-table-builder.js +9 -3
- package/dist/cjs/util/type-utils.d.ts +20 -1
- package/dist/esm/dialect/database-introspector.d.ts +4 -2
- package/dist/esm/dialect/dialect-adapter-base.d.ts +32 -6
- package/dist/esm/dialect/dialect-adapter.d.ts +50 -8
- package/dist/esm/dialect/mysql/mysql-adapter.d.ts +32 -6
- package/dist/esm/dialect/mysql/mysql-introspector.d.ts +1 -1
- package/dist/esm/dialect/mysql/mysql-introspector.js +21 -15
- package/dist/esm/dialect/postgres/postgres-adapter.d.ts +32 -6
- package/dist/esm/dialect/postgres/postgres-introspector.d.ts +1 -1
- package/dist/esm/dialect/postgres/postgres-introspector.js +5 -3
- package/dist/esm/dialect/sqlite/sqlite-adapter.d.ts +32 -6
- package/dist/esm/dialect/sqlite/sqlite-introspector.d.ts +1 -1
- package/dist/esm/dialect/sqlite/sqlite-introspector.js +8 -7
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/migration/migrator.d.ts +8 -3
- package/dist/esm/migration/migrator.js +15 -5
- package/dist/esm/operation-node/aggregate-function-node.d.ts +3 -2
- package/dist/esm/operation-node/aggregate-function-node.js +2 -2
- package/dist/esm/operation-node/create-index-node.d.ts +1 -0
- package/dist/esm/operation-node/delete-query-node.d.ts +4 -1
- package/dist/esm/operation-node/delete-query-node.js +11 -2
- package/dist/esm/operation-node/operation-node-transformer.d.ts +2 -0
- package/dist/esm/operation-node/operation-node-transformer.js +10 -1
- package/dist/esm/operation-node/operation-node-visitor.d.ts +2 -0
- package/dist/esm/operation-node/operation-node-visitor.js +1 -0
- package/dist/esm/operation-node/operation-node.d.ts +1 -1
- package/dist/esm/operation-node/operator-node.d.ts +1 -1
- package/dist/esm/operation-node/operator-node.js +1 -0
- package/dist/esm/operation-node/using-node.d.ts +13 -0
- package/dist/esm/operation-node/using-node.js +22 -0
- package/dist/esm/parser/expression-parser.js +2 -2
- package/dist/esm/parser/table-parser.d.ts +1 -0
- package/dist/esm/parser/update-set-parser.d.ts +1 -1
- package/dist/esm/plugin/camel-case/camel-case-plugin.d.ts +7 -0
- package/dist/esm/plugin/camel-case/camel-case-plugin.js +10 -4
- package/dist/esm/query-builder/delete-query-builder.d.ts +103 -11
- package/dist/esm/query-builder/delete-query-builder.js +40 -9
- package/dist/esm/query-builder/function-module.d.ts +273 -39
- package/dist/esm/query-builder/function-module.js +156 -34
- package/dist/esm/query-builder/insert-query-builder.d.ts +25 -10
- package/dist/esm/query-builder/insert-query-builder.js +33 -10
- package/dist/esm/query-builder/select-query-builder.d.ts +51 -34
- package/dist/esm/query-builder/select-query-builder.js +53 -29
- package/dist/esm/query-builder/update-query-builder.d.ts +26 -10
- package/dist/esm/query-builder/update-query-builder.js +33 -9
- package/dist/esm/query-compiler/default-query-compiler.d.ts +2 -0
- package/dist/esm/query-compiler/default-query-compiler.js +12 -1
- package/dist/esm/query-creator.d.ts +29 -0
- package/dist/esm/query-creator.js +2 -21
- package/dist/esm/raw-builder/raw-builder.d.ts +4 -0
- package/dist/esm/raw-builder/raw-builder.js +8 -2
- package/dist/esm/schema/alter-table-builder.d.ts +5 -1
- package/dist/esm/schema/alter-table-builder.js +8 -2
- package/dist/esm/schema/create-index-builder.d.ts +6 -0
- package/dist/esm/schema/create-index-builder.js +13 -0
- package/dist/esm/schema/create-table-builder.d.ts +6 -2
- package/dist/esm/schema/create-table-builder.js +9 -3
- package/dist/esm/util/type-utils.d.ts +20 -1
- package/package.json +1 -1
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { AggregateFunctionNode } from '../operation-node/aggregate-function-node.js';
|
|
3
3
|
import { parseCoalesce, } from '../parser/coalesce-parser.js';
|
|
4
4
|
import { parseSimpleReferenceExpression, } from '../parser/reference-parser.js';
|
|
5
|
+
import { parseSelectAll } from '../parser/select-parser.js';
|
|
5
6
|
import { RawBuilder } from '../raw-builder/raw-builder.js';
|
|
6
7
|
import { createQueryId } from '../util/query-id.js';
|
|
7
8
|
import { AggregateFunctionBuilder } from './aggregate-function-builder.js';
|
|
@@ -42,6 +43,7 @@ export class FunctionModule {
|
|
|
42
43
|
this.avg = this.avg.bind(this);
|
|
43
44
|
this.coalesce = this.coalesce.bind(this);
|
|
44
45
|
this.count = this.count.bind(this);
|
|
46
|
+
this.countAll = this.countAll.bind(this);
|
|
45
47
|
this.max = this.max.bind(this);
|
|
46
48
|
this.min = this.min.bind(this);
|
|
47
49
|
this.sum = this.sum.bind(this);
|
|
@@ -49,8 +51,38 @@ export class FunctionModule {
|
|
|
49
51
|
/**
|
|
50
52
|
* Calls the `avg` function for the column given as the argument.
|
|
51
53
|
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
+
* This sql function calculates the average value for a given column.
|
|
55
|
+
*
|
|
56
|
+
* For additional functionality such as distinct, filtering and window functions,
|
|
57
|
+
* refer to {@link AggregateFunctionBuilder}. An instance of this builder is
|
|
58
|
+
* returned when calling this function.
|
|
59
|
+
*
|
|
60
|
+
* ### Examples
|
|
61
|
+
*
|
|
62
|
+
* ```ts
|
|
63
|
+
* const { avg } = db.fn
|
|
64
|
+
*
|
|
65
|
+
* db.selectFrom('toy')
|
|
66
|
+
* .select(avg('price').as('avg_price'))
|
|
67
|
+
* .execute()
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* The generated SQL (PostgreSQL):
|
|
71
|
+
*
|
|
72
|
+
* ```sql
|
|
73
|
+
* select avg("price") as "avg_price" from "toy"
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* You can limit column range to only columns participating in current query:
|
|
77
|
+
*
|
|
78
|
+
* ```ts
|
|
79
|
+
* db.selectFrom('toy')
|
|
80
|
+
* .select(qb => qb.fn.avg('price').as('avg_price'))
|
|
81
|
+
* .execute()
|
|
82
|
+
* ```
|
|
83
|
+
*
|
|
84
|
+
* If this function is used in a `select` statement, the type of the selected
|
|
85
|
+
* expression will be `number | string` by default. This is because Kysely can't know the
|
|
54
86
|
* type the db driver outputs. Sometimes the output can be larger than the largest
|
|
55
87
|
* javascript number and a string is returned instead. Most drivers allow you
|
|
56
88
|
* to configure the output type of large numbers and Kysely can't know if you've
|
|
@@ -69,7 +101,7 @@ export class FunctionModule {
|
|
|
69
101
|
*
|
|
70
102
|
* Sometimes a null is returned, e.g. when row count is 0, and no `group by`
|
|
71
103
|
* was used. It is highly recommended to include null in the output type union
|
|
72
|
-
* and handle null values in post-execute code, or wrap the function with a
|
|
104
|
+
* and handle null values in post-execute code, or wrap the function with a {@link coalesce}
|
|
73
105
|
* function.
|
|
74
106
|
*
|
|
75
107
|
* ```ts
|
|
@@ -88,36 +120,66 @@ export class FunctionModule {
|
|
|
88
120
|
/**
|
|
89
121
|
* Calls the `coalesce` function for given arguments.
|
|
90
122
|
*
|
|
91
|
-
* This function returns the first non-null value from left to right, commonly
|
|
123
|
+
* This sql function returns the first non-null value from left to right, commonly
|
|
92
124
|
* used to provide a default scalar for nullable columns or functions.
|
|
93
125
|
*
|
|
126
|
+
* If this function is used in a `select` statement, the type of the selected
|
|
127
|
+
* expression is inferred in the same manner that the sql function computes.
|
|
128
|
+
* A union of arguments' types - if a non-nullable argument exists, it stops
|
|
129
|
+
* there (ignoring any further arguments' types) and exludes null from the final
|
|
130
|
+
* union type.
|
|
131
|
+
*
|
|
132
|
+
* `(string | null, number | null)` is inferred as `string | number | null`.
|
|
133
|
+
*
|
|
134
|
+
* `(string | null, number, Date | null)` is inferred as `string | number`.
|
|
135
|
+
*
|
|
136
|
+
* `(number, string | null)` is inferred as `number`.
|
|
137
|
+
*
|
|
138
|
+
* ### Examples
|
|
139
|
+
*
|
|
94
140
|
* ```ts
|
|
95
|
-
* const { coalesce
|
|
141
|
+
* const { coalesce } = db.fn
|
|
96
142
|
*
|
|
97
|
-
* db.selectFrom('
|
|
98
|
-
* .select(coalesce(
|
|
99
|
-
* .where('
|
|
143
|
+
* db.selectFrom('participant')
|
|
144
|
+
* .select(coalesce('nickname', sql<string>`'<anonymous>'`).as('nickname'))
|
|
145
|
+
* .where('room_id', '=', roomId)
|
|
100
146
|
* .execute()
|
|
101
147
|
* ```
|
|
102
148
|
*
|
|
103
|
-
* The generated SQL (
|
|
149
|
+
* The generated SQL (PostgreSQL):
|
|
104
150
|
*
|
|
105
151
|
* ```sql
|
|
106
|
-
* select coalesce(
|
|
152
|
+
* select coalesce("nickname", '<anonymous>') as "nickname"
|
|
153
|
+
* from "participant" where "room_id" = $1
|
|
107
154
|
* ```
|
|
108
155
|
*
|
|
109
|
-
*
|
|
110
|
-
* is inferred in the same manner that the function computes. A union of arguments'
|
|
111
|
-
* types - if a non-nullable argument exists, it stops there (ignoring any further
|
|
112
|
-
* arguments' types) and exludes null from the final union type.
|
|
156
|
+
* You can limit column range to only columns participating in current query:
|
|
113
157
|
*
|
|
114
|
-
*
|
|
158
|
+
* ```ts
|
|
159
|
+
* db.selectFrom('participant')
|
|
160
|
+
* .select(qb =>
|
|
161
|
+
* qb.fn.coalesce('nickname', sql<string>`'<anonymous>'`).as('nickname')
|
|
162
|
+
* )
|
|
163
|
+
* .where('room_id', '=', roomId)
|
|
164
|
+
* .execute()
|
|
165
|
+
* ```
|
|
115
166
|
*
|
|
116
|
-
*
|
|
167
|
+
* You can combine this function with other helpers in this module:
|
|
117
168
|
*
|
|
118
|
-
*
|
|
169
|
+
* ```ts
|
|
170
|
+
* const { avg, coalesce } = db.fn
|
|
119
171
|
*
|
|
120
|
-
*
|
|
172
|
+
* db.selectFrom('person')
|
|
173
|
+
* .select(coalesce(avg<number | null>('age'), sql<number>`0`).as('avg_age'))
|
|
174
|
+
* .where('first_name', '=', 'Jennifer')
|
|
175
|
+
* .execute()
|
|
176
|
+
* ```
|
|
177
|
+
*
|
|
178
|
+
* The generated SQL (PostgreSQL):
|
|
179
|
+
*
|
|
180
|
+
* ```sql
|
|
181
|
+
* select coalesce(avg("age"), 0) as "avg_age" from "person" where "first_name" = $1
|
|
182
|
+
* ```
|
|
121
183
|
*/
|
|
122
184
|
coalesce(value, ...otherValues) {
|
|
123
185
|
return new RawBuilder({
|
|
@@ -128,11 +190,36 @@ export class FunctionModule {
|
|
|
128
190
|
/**
|
|
129
191
|
* Calls the `count` function for the column given as the argument.
|
|
130
192
|
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
193
|
+
* When called with a column as argument, this sql function counts the number of rows where there
|
|
194
|
+
* is a non-null value in that column.
|
|
195
|
+
*
|
|
196
|
+
* For counting all rows nulls included (`count(*)`), see {@link countAll}.
|
|
197
|
+
*
|
|
198
|
+
* For additional functionality such as distinct, filtering and window functions,
|
|
199
|
+
* refer to {@link AggregateFunctionBuilder}. An instance of this builder is
|
|
200
|
+
* returned when calling this function.
|
|
201
|
+
*
|
|
202
|
+
* ### Examples
|
|
203
|
+
*
|
|
204
|
+
* ```ts
|
|
205
|
+
* const { count } = db.fn
|
|
206
|
+
*
|
|
207
|
+
* db.selectFrom('toy')
|
|
208
|
+
* .select(count('id').as('num_toys'))
|
|
209
|
+
* .execute()
|
|
210
|
+
* ```
|
|
211
|
+
*
|
|
212
|
+
* The generated SQL (PostgreSQL):
|
|
213
|
+
*
|
|
214
|
+
* ```sql
|
|
215
|
+
* select count("id") as "num_toys" from "toy"
|
|
216
|
+
* ```
|
|
217
|
+
*
|
|
218
|
+
* If this function is used in a `select` statement, the type of the selected
|
|
219
|
+
* expression will be `number | string | bigint` by default. This is because
|
|
220
|
+
* Kysely can't know the type the db driver outputs. Sometimes the output can
|
|
221
|
+
* be larger than the largest javascript number and a string is returned instead.
|
|
222
|
+
* Most drivers allow you to configure the output type of large numbers and Kysely
|
|
136
223
|
* can't know if you've done so.
|
|
137
224
|
*
|
|
138
225
|
* You can specify the output type of the expression by providing
|
|
@@ -146,11 +233,11 @@ export class FunctionModule {
|
|
|
146
233
|
* .execute()
|
|
147
234
|
* ```
|
|
148
235
|
*
|
|
149
|
-
* You can limit column range:
|
|
236
|
+
* You can limit column range to only columns participating in current query:
|
|
150
237
|
*
|
|
151
238
|
* ```ts
|
|
152
239
|
* db.selectFrom('toy')
|
|
153
|
-
* .select(qb => qb.fn.count
|
|
240
|
+
* .select(qb => qb.fn.count('id').as('num_toys'))
|
|
154
241
|
* .execute()
|
|
155
242
|
* ```
|
|
156
243
|
*/
|
|
@@ -159,6 +246,11 @@ export class FunctionModule {
|
|
|
159
246
|
aggregateFunctionNode: AggregateFunctionNode.create('count', parseSimpleReferenceExpression(column)),
|
|
160
247
|
});
|
|
161
248
|
}
|
|
249
|
+
countAll(table) {
|
|
250
|
+
return new AggregateFunctionBuilder({
|
|
251
|
+
aggregateFunctionNode: AggregateFunctionNode.create('count', parseSelectAll(table)[0].selection),
|
|
252
|
+
});
|
|
253
|
+
}
|
|
162
254
|
max(column) {
|
|
163
255
|
return new AggregateFunctionBuilder({
|
|
164
256
|
aggregateFunctionNode: AggregateFunctionNode.create('max', parseSimpleReferenceExpression(column)),
|
|
@@ -172,15 +264,45 @@ export class FunctionModule {
|
|
|
172
264
|
/**
|
|
173
265
|
* Calls the `sum` function for the column given as the argument.
|
|
174
266
|
*
|
|
175
|
-
*
|
|
176
|
-
* will be `number | string | bigint` by default. This is because Kysely
|
|
177
|
-
* can't know the type the db driver outputs. Sometimes the output can be larger
|
|
178
|
-
* than the largest javascript number and a string is returned instead. Most
|
|
179
|
-
* drivers allow you to configure the output type of large numbers and Kysely
|
|
180
|
-
* can't know if you've done so.
|
|
267
|
+
* This sql function sums the values of a given column.
|
|
181
268
|
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
269
|
+
* For additional functionality such as distinct, filtering and window functions,
|
|
270
|
+
* refer to {@link AggregateFunctionBuilder}. An instance of this builder is
|
|
271
|
+
* returned when calling this function.
|
|
272
|
+
*
|
|
273
|
+
* ### Examples
|
|
274
|
+
*
|
|
275
|
+
* ```ts
|
|
276
|
+
* const { sum } = db.fn
|
|
277
|
+
*
|
|
278
|
+
* db.selectFrom('toy')
|
|
279
|
+
* .select(sum('price').as('total_price'))
|
|
280
|
+
* .execute()
|
|
281
|
+
* ```
|
|
282
|
+
*
|
|
283
|
+
* The generated SQL (PostgreSQL):
|
|
284
|
+
*
|
|
285
|
+
* ```sql
|
|
286
|
+
* select sum("price") as "total_price" from "toy"
|
|
287
|
+
* ```
|
|
288
|
+
*
|
|
289
|
+
* You can limit column range to only columns participating in current query:
|
|
290
|
+
*
|
|
291
|
+
* ```ts
|
|
292
|
+
* db.selectFrom('toy')
|
|
293
|
+
* .select(qb => qb.fn.sum('price').as('total_price'))
|
|
294
|
+
* .execute()
|
|
295
|
+
* ```
|
|
296
|
+
*
|
|
297
|
+
* If this function is used in a `select` statement, the type of the selected
|
|
298
|
+
* expression will be `number | string` by default. This is because Kysely can't know the
|
|
299
|
+
* type the db driver outputs. Sometimes the output can be larger than the largest
|
|
300
|
+
* javascript number and a string is returned instead. Most drivers allow you
|
|
301
|
+
* to configure the output type of large numbers and Kysely can't know if you've
|
|
302
|
+
* done so.
|
|
303
|
+
*
|
|
304
|
+
* You can specify the output type of the expression by providing the type as
|
|
305
|
+
* the first type argument:
|
|
184
306
|
*
|
|
185
307
|
* ```ts
|
|
186
308
|
* const { sum } = db.fn
|
|
@@ -192,7 +314,7 @@ export class FunctionModule {
|
|
|
192
314
|
*
|
|
193
315
|
* Sometimes a null is returned, e.g. when row count is 0, and no `group by`
|
|
194
316
|
* was used. It is highly recommended to include null in the output type union
|
|
195
|
-
* and handle null values in post-execute code, or wrap the function with a
|
|
317
|
+
* and handle null values in post-execute code, or wrap the function with a {@link coalesce}
|
|
196
318
|
* function.
|
|
197
319
|
*
|
|
198
320
|
* ```ts
|
|
@@ -3,7 +3,7 @@ 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 { MergePartial,
|
|
6
|
+
import { MergePartial, SimplifyResult, SimplifySingleResult } from '../util/type-utils.js';
|
|
7
7
|
import { UpdateObject } from '../parser/update-set-parser.js';
|
|
8
8
|
import { Compilable } from '../util/compilable.js';
|
|
9
9
|
import { QueryExecutor } from '../query-executor/query-executor.js';
|
|
@@ -259,7 +259,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
259
259
|
* .doUpdateSet({ species: 'hamster' })
|
|
260
260
|
* )
|
|
261
261
|
* .execute()
|
|
262
|
-
* .execute()
|
|
263
262
|
* ```
|
|
264
263
|
*
|
|
265
264
|
* The generated SQL (PostgreSQL):
|
|
@@ -456,7 +455,7 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
456
455
|
* Simply calls the given function passing `this` as the only argument.
|
|
457
456
|
*
|
|
458
457
|
* If you want to conditionally call a method on `this`, see
|
|
459
|
-
* the {@link if} method.
|
|
458
|
+
* the {@link $if} method.
|
|
460
459
|
*
|
|
461
460
|
* ### Examples
|
|
462
461
|
*
|
|
@@ -470,10 +469,14 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
470
469
|
*
|
|
471
470
|
* db.updateTable('person')
|
|
472
471
|
* .set(values)
|
|
473
|
-
*
|
|
472
|
+
* .$call(log)
|
|
474
473
|
* .execute()
|
|
475
474
|
* ```
|
|
476
475
|
*/
|
|
476
|
+
$call<T>(func: (qb: this) => T): T;
|
|
477
|
+
/**
|
|
478
|
+
* @deprecated Use `$call` instead
|
|
479
|
+
*/
|
|
477
480
|
call<T>(func: (qb: this) => T): T;
|
|
478
481
|
/**
|
|
479
482
|
* Call `func(this)` if `condition` is true.
|
|
@@ -493,7 +496,7 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
493
496
|
* .insertInto('person')
|
|
494
497
|
* .values(values)
|
|
495
498
|
* .returning(['id', 'first_name'])
|
|
496
|
-
*
|
|
499
|
+
* .$if(returnLastName, (qb) => qb.returning('last_name'))
|
|
497
500
|
* .executeTakeFirstOrThrow()
|
|
498
501
|
* }
|
|
499
502
|
* ```
|
|
@@ -510,6 +513,10 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
510
513
|
* }
|
|
511
514
|
* ```
|
|
512
515
|
*/
|
|
516
|
+
$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>>;
|
|
517
|
+
/**
|
|
518
|
+
* @deprecated Use `$if` instead
|
|
519
|
+
*/
|
|
513
520
|
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>>;
|
|
514
521
|
/**
|
|
515
522
|
* Change the output type of the query.
|
|
@@ -517,6 +524,10 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
517
524
|
* You should only use this method as the last resort if the types
|
|
518
525
|
* don't support your use case.
|
|
519
526
|
*/
|
|
527
|
+
$castTo<T>(): InsertQueryBuilder<DB, TB, T>;
|
|
528
|
+
/**
|
|
529
|
+
* @deprecated Use `$castTo` instead.
|
|
530
|
+
*/
|
|
520
531
|
castTo<T>(): InsertQueryBuilder<DB, TB, T>;
|
|
521
532
|
/**
|
|
522
533
|
* Asserts that query's output row type equals the given type `T`.
|
|
@@ -546,19 +557,23 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
546
557
|
* .insertInto('person')
|
|
547
558
|
* .values(person)
|
|
548
559
|
* .returning('id')
|
|
549
|
-
*
|
|
560
|
+
* .$assertType<{ id: string }>()
|
|
550
561
|
* )
|
|
551
562
|
* .with('new_pet', (qb) => qb
|
|
552
563
|
* .insertInto('pet')
|
|
553
564
|
* .values({ owner_id: (eb) => eb.selectFrom('new_person').select('id'), ...pet })
|
|
554
565
|
* .returning(['name as pet_name', 'species'])
|
|
555
|
-
*
|
|
566
|
+
* .$assertType<{ pet_name: string, species: Species }>()
|
|
556
567
|
* )
|
|
557
568
|
* .selectFrom(['new_person', 'new_pet'])
|
|
558
569
|
* .selectAll()
|
|
559
570
|
* .executeTakeFirstOrThrow()
|
|
560
571
|
* ```
|
|
561
572
|
*/
|
|
573
|
+
$assertType<T extends O>(): O extends T ? InsertQueryBuilder<DB, TB, T> : KyselyTypeError<`$assertType() call failed: The type passed in is not equal to the output type of the query.`>;
|
|
574
|
+
/**
|
|
575
|
+
* @deprecated Use `$assertType` instead.
|
|
576
|
+
*/
|
|
562
577
|
assertType<T extends O>(): O extends T ? InsertQueryBuilder<DB, TB, T> : KyselyTypeError<`assertType() call failed: The type passed in is not equal to the output type of the query.`>;
|
|
563
578
|
/**
|
|
564
579
|
* Returns a copy of this InsertQueryBuilder instance with the given plugin installed.
|
|
@@ -571,12 +586,12 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
571
586
|
*
|
|
572
587
|
* Also see the {@link executeTakeFirst} and {@link executeTakeFirstOrThrow} methods.
|
|
573
588
|
*/
|
|
574
|
-
execute(): Promise<O[]>;
|
|
589
|
+
execute(): Promise<SimplifyResult<O>[]>;
|
|
575
590
|
/**
|
|
576
591
|
* Executes the query and returns the first result or undefined if
|
|
577
592
|
* the query returned no result.
|
|
578
593
|
*/
|
|
579
|
-
executeTakeFirst(): Promise<
|
|
594
|
+
executeTakeFirst(): Promise<SimplifySingleResult<O>>;
|
|
580
595
|
/**
|
|
581
596
|
* Executes the query and returns the first result or throws if
|
|
582
597
|
* the query returned no result.
|
|
@@ -585,7 +600,7 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
585
600
|
* provide a custom error class as the only argument to throw a different
|
|
586
601
|
* error.
|
|
587
602
|
*/
|
|
588
|
-
executeTakeFirstOrThrow(errorConstructor?: NoResultErrorConstructor): Promise<O
|
|
603
|
+
executeTakeFirstOrThrow(errorConstructor?: NoResultErrorConstructor): Promise<SimplifyResult<O>>;
|
|
589
604
|
/**
|
|
590
605
|
* Executes query with `explain` statement before `insert` keyword.
|
|
591
606
|
*
|
|
@@ -3,7 +3,7 @@ import { parseSelectExpressionOrList, parseSelectAll, } from '../parser/select-p
|
|
|
3
3
|
import { parseInsertObjectOrList, } from '../parser/insert-values-parser.js';
|
|
4
4
|
import { InsertQueryNode } from '../operation-node/insert-query-node.js';
|
|
5
5
|
import { QueryNode } from '../operation-node/query-node.js';
|
|
6
|
-
import { parseUpdateObject
|
|
6
|
+
import { parseUpdateObject } from '../parser/update-set-parser.js';
|
|
7
7
|
import { preventAwait } from '../util/prevent-await.js';
|
|
8
8
|
import { freeze } from '../util/object-utils.js';
|
|
9
9
|
import { OnDuplicateKeyNode } from '../operation-node/on-duplicate-key-node.js';
|
|
@@ -159,7 +159,6 @@ export class InsertQueryBuilder {
|
|
|
159
159
|
* .doUpdateSet({ species: 'hamster' })
|
|
160
160
|
* )
|
|
161
161
|
* .execute()
|
|
162
|
-
* .execute()
|
|
163
162
|
* ```
|
|
164
163
|
*
|
|
165
164
|
* The generated SQL (PostgreSQL):
|
|
@@ -318,7 +317,7 @@ export class InsertQueryBuilder {
|
|
|
318
317
|
* Simply calls the given function passing `this` as the only argument.
|
|
319
318
|
*
|
|
320
319
|
* If you want to conditionally call a method on `this`, see
|
|
321
|
-
* the {@link if} method.
|
|
320
|
+
* the {@link $if} method.
|
|
322
321
|
*
|
|
323
322
|
* ### Examples
|
|
324
323
|
*
|
|
@@ -332,13 +331,19 @@ export class InsertQueryBuilder {
|
|
|
332
331
|
*
|
|
333
332
|
* db.updateTable('person')
|
|
334
333
|
* .set(values)
|
|
335
|
-
*
|
|
334
|
+
* .$call(log)
|
|
336
335
|
* .execute()
|
|
337
336
|
* ```
|
|
338
337
|
*/
|
|
339
|
-
call(func) {
|
|
338
|
+
$call(func) {
|
|
340
339
|
return func(this);
|
|
341
340
|
}
|
|
341
|
+
/**
|
|
342
|
+
* @deprecated Use `$call` instead
|
|
343
|
+
*/
|
|
344
|
+
call(func) {
|
|
345
|
+
return this.$call(func);
|
|
346
|
+
}
|
|
342
347
|
/**
|
|
343
348
|
* Call `func(this)` if `condition` is true.
|
|
344
349
|
*
|
|
@@ -357,7 +362,7 @@ export class InsertQueryBuilder {
|
|
|
357
362
|
* .insertInto('person')
|
|
358
363
|
* .values(values)
|
|
359
364
|
* .returning(['id', 'first_name'])
|
|
360
|
-
*
|
|
365
|
+
* .$if(returnLastName, (qb) => qb.returning('last_name'))
|
|
361
366
|
* .executeTakeFirstOrThrow()
|
|
362
367
|
* }
|
|
363
368
|
* ```
|
|
@@ -374,7 +379,7 @@ export class InsertQueryBuilder {
|
|
|
374
379
|
* }
|
|
375
380
|
* ```
|
|
376
381
|
*/
|
|
377
|
-
if(condition, func) {
|
|
382
|
+
$if(condition, func) {
|
|
378
383
|
if (condition) {
|
|
379
384
|
return func(this);
|
|
380
385
|
}
|
|
@@ -382,15 +387,27 @@ export class InsertQueryBuilder {
|
|
|
382
387
|
...this.#props,
|
|
383
388
|
});
|
|
384
389
|
}
|
|
390
|
+
/**
|
|
391
|
+
* @deprecated Use `$if` instead
|
|
392
|
+
*/
|
|
393
|
+
if(condition, func) {
|
|
394
|
+
return this.$if(condition, func);
|
|
395
|
+
}
|
|
385
396
|
/**
|
|
386
397
|
* Change the output type of the query.
|
|
387
398
|
*
|
|
388
399
|
* You should only use this method as the last resort if the types
|
|
389
400
|
* don't support your use case.
|
|
390
401
|
*/
|
|
391
|
-
castTo() {
|
|
402
|
+
$castTo() {
|
|
392
403
|
return new InsertQueryBuilder(this.#props);
|
|
393
404
|
}
|
|
405
|
+
/**
|
|
406
|
+
* @deprecated Use `$castTo` instead.
|
|
407
|
+
*/
|
|
408
|
+
castTo() {
|
|
409
|
+
return this.$castTo();
|
|
410
|
+
}
|
|
394
411
|
/**
|
|
395
412
|
* Asserts that query's output row type equals the given type `T`.
|
|
396
413
|
*
|
|
@@ -419,19 +436,25 @@ export class InsertQueryBuilder {
|
|
|
419
436
|
* .insertInto('person')
|
|
420
437
|
* .values(person)
|
|
421
438
|
* .returning('id')
|
|
422
|
-
*
|
|
439
|
+
* .$assertType<{ id: string }>()
|
|
423
440
|
* )
|
|
424
441
|
* .with('new_pet', (qb) => qb
|
|
425
442
|
* .insertInto('pet')
|
|
426
443
|
* .values({ owner_id: (eb) => eb.selectFrom('new_person').select('id'), ...pet })
|
|
427
444
|
* .returning(['name as pet_name', 'species'])
|
|
428
|
-
*
|
|
445
|
+
* .$assertType<{ pet_name: string, species: Species }>()
|
|
429
446
|
* )
|
|
430
447
|
* .selectFrom(['new_person', 'new_pet'])
|
|
431
448
|
* .selectAll()
|
|
432
449
|
* .executeTakeFirstOrThrow()
|
|
433
450
|
* ```
|
|
434
451
|
*/
|
|
452
|
+
$assertType() {
|
|
453
|
+
return new InsertQueryBuilder(this.#props);
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* @deprecated Use `$assertType` instead.
|
|
457
|
+
*/
|
|
435
458
|
assertType() {
|
|
436
459
|
return new InsertQueryBuilder(this.#props);
|
|
437
460
|
}
|