kysely 0.23.4 → 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 +3 -1
- package/dist/cjs/dialect/database-introspector.d.ts +4 -2
- package/dist/cjs/dialect/mysql/mysql-introspector.d.ts +1 -1
- package/dist/cjs/dialect/mysql/mysql-introspector.js +19 -13
- package/dist/cjs/dialect/postgres/postgres-introspector.d.ts +1 -1
- package/dist/cjs/dialect/postgres/postgres-introspector.js +3 -1
- package/dist/cjs/dialect/sqlite/sqlite-introspector.d.ts +1 -1
- package/dist/cjs/dialect/sqlite/sqlite-introspector.js +6 -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/delete-query-node.d.ts +1 -1
- package/dist/cjs/operation-node/delete-query-node.js +2 -2
- package/dist/cjs/operation-node/operation-node-transformer.js +2 -2
- package/dist/cjs/parser/expression-parser.js +1 -1
- package/dist/cjs/parser/table-parser.d.ts +1 -0
- 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 +5 -5
- package/dist/cjs/query-builder/delete-query-builder.js +1 -1
- 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 +4 -5
- package/dist/cjs/query-builder/insert-query-builder.js +0 -1
- package/dist/cjs/query-builder/select-query-builder.d.ts +8 -7
- package/dist/cjs/query-builder/update-query-builder.d.ts +5 -5
- package/dist/cjs/query-builder/update-query-builder.js +1 -1
- package/dist/cjs/query-compiler/default-query-compiler.js +1 -1
- package/dist/cjs/query-creator.d.ts +29 -0
- package/dist/cjs/query-creator.js +2 -21
- package/dist/cjs/util/type-utils.d.ts +5 -4
- package/dist/esm/dialect/database-introspector.d.ts +4 -2
- package/dist/esm/dialect/mysql/mysql-introspector.d.ts +1 -1
- package/dist/esm/dialect/mysql/mysql-introspector.js +19 -13
- package/dist/esm/dialect/postgres/postgres-introspector.d.ts +1 -1
- package/dist/esm/dialect/postgres/postgres-introspector.js +3 -1
- package/dist/esm/dialect/sqlite/sqlite-introspector.d.ts +1 -1
- package/dist/esm/dialect/sqlite/sqlite-introspector.js +6 -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/delete-query-node.d.ts +1 -1
- package/dist/esm/operation-node/delete-query-node.js +2 -2
- package/dist/esm/operation-node/operation-node-transformer.js +2 -2
- package/dist/esm/parser/expression-parser.js +2 -2
- package/dist/esm/parser/table-parser.d.ts +1 -0
- 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 +5 -5
- package/dist/esm/query-builder/delete-query-builder.js +1 -1
- 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 +4 -5
- package/dist/esm/query-builder/insert-query-builder.js +0 -1
- package/dist/esm/query-builder/select-query-builder.d.ts +8 -7
- package/dist/esm/query-builder/update-query-builder.d.ts +5 -5
- package/dist/esm/query-builder/update-query-builder.js +1 -1
- package/dist/esm/query-compiler/default-query-compiler.js +1 -1
- package/dist/esm/query-creator.d.ts +29 -0
- package/dist/esm/query-creator.js +2 -21
- package/dist/esm/util/type-utils.d.ts +5 -4
- 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):
|
|
@@ -587,12 +586,12 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
587
586
|
*
|
|
588
587
|
* Also see the {@link executeTakeFirst} and {@link executeTakeFirstOrThrow} methods.
|
|
589
588
|
*/
|
|
590
|
-
execute(): Promise<O[]>;
|
|
589
|
+
execute(): Promise<SimplifyResult<O>[]>;
|
|
591
590
|
/**
|
|
592
591
|
* Executes the query and returns the first result or undefined if
|
|
593
592
|
* the query returned no result.
|
|
594
593
|
*/
|
|
595
|
-
executeTakeFirst(): Promise<
|
|
594
|
+
executeTakeFirst(): Promise<SimplifySingleResult<O>>;
|
|
596
595
|
/**
|
|
597
596
|
* Executes the query and returns the first result or throws if
|
|
598
597
|
* the query returned no result.
|
|
@@ -601,7 +600,7 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
601
600
|
* provide a custom error class as the only argument to throw a different
|
|
602
601
|
* error.
|
|
603
602
|
*/
|
|
604
|
-
executeTakeFirstOrThrow(errorConstructor?: NoResultErrorConstructor): Promise<O
|
|
603
|
+
executeTakeFirstOrThrow(errorConstructor?: NoResultErrorConstructor): Promise<SimplifyResult<O>>;
|
|
605
604
|
/**
|
|
606
605
|
* Executes query with `explain` statement before `insert` keyword.
|
|
607
606
|
*
|
|
@@ -5,7 +5,7 @@ import { TableExpression } from '../parser/table-parser.js';
|
|
|
5
5
|
import { SelectExpression, QueryBuilderWithSelection, SelectAllQueryBuilder } from '../parser/select-parser.js';
|
|
6
6
|
import { ReferenceExpression } from '../parser/reference-parser.js';
|
|
7
7
|
import { SelectQueryNode } from '../operation-node/select-query-node.js';
|
|
8
|
-
import { MergePartial, Nullable,
|
|
8
|
+
import { MergePartial, Nullable, Simplify, SimplifySingleResult } from '../util/type-utils.js';
|
|
9
9
|
import { OrderByDirectionExpression, OrderByExpression } from '../parser/order-by-parser.js';
|
|
10
10
|
import { Compilable } from '../util/compilable.js';
|
|
11
11
|
import { QueryExecutor } from '../query-executor/query-executor.js';
|
|
@@ -483,7 +483,7 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
483
483
|
* to override the types.
|
|
484
484
|
*
|
|
485
485
|
* Select calls are additive. Calling `select('id').select('first_name')` is the
|
|
486
|
-
* same as calling `select(['id', 'first_name'])
|
|
486
|
+
* same as calling `select(['id', 'first_name'])`.
|
|
487
487
|
*
|
|
488
488
|
* To select all columns of the query or specific tables see the
|
|
489
489
|
* {@link selectAll} method.
|
|
@@ -892,7 +892,7 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
892
892
|
* ```ts
|
|
893
893
|
* await db.selectFrom('person')
|
|
894
894
|
* .innerJoin(
|
|
895
|
-
*
|
|
895
|
+
* db.selectFrom('pet')
|
|
896
896
|
* .select(['owner_id', 'name'])
|
|
897
897
|
* .where('name', '=', 'Doggo')
|
|
898
898
|
* .as('doggos'),
|
|
@@ -950,6 +950,7 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
950
950
|
* )
|
|
951
951
|
* .select(['first_name', 'p.name'])
|
|
952
952
|
* .orderBy('first_name')
|
|
953
|
+
* ```
|
|
953
954
|
*/
|
|
954
955
|
innerJoinLateral<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): SelectQueryBuilderWithInnerJoin<DB, TB, O, TE>;
|
|
955
956
|
innerJoinLateral<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): SelectQueryBuilderWithInnerJoin<DB, TB, O, TE>;
|
|
@@ -1564,18 +1565,18 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
1564
1565
|
* ```
|
|
1565
1566
|
*/
|
|
1566
1567
|
toOperationNode(): SelectQueryNode;
|
|
1567
|
-
compile(): CompiledQuery<O
|
|
1568
|
+
compile(): CompiledQuery<Simplify<O>>;
|
|
1568
1569
|
/**
|
|
1569
1570
|
* Executes the query and returns an array of rows.
|
|
1570
1571
|
*
|
|
1571
1572
|
* Also see the {@link executeTakeFirst} and {@link executeTakeFirstOrThrow} methods.
|
|
1572
1573
|
*/
|
|
1573
|
-
execute(): Promise<O[]>;
|
|
1574
|
+
execute(): Promise<Simplify<O>[]>;
|
|
1574
1575
|
/**
|
|
1575
1576
|
* Executes the query and returns the first result or undefined if
|
|
1576
1577
|
* the query returned no result.
|
|
1577
1578
|
*/
|
|
1578
|
-
executeTakeFirst(): Promise<
|
|
1579
|
+
executeTakeFirst(): Promise<SimplifySingleResult<O>>;
|
|
1579
1580
|
/**
|
|
1580
1581
|
* Executes the query and returns the first result or throws if
|
|
1581
1582
|
* the query returned no result.
|
|
@@ -1584,7 +1585,7 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
1584
1585
|
* provide a custom error class as the only argument to throw a different
|
|
1585
1586
|
* error.
|
|
1586
1587
|
*/
|
|
1587
|
-
executeTakeFirstOrThrow(errorConstructor?: NoResultErrorConstructor): Promise<O
|
|
1588
|
+
executeTakeFirstOrThrow(errorConstructor?: NoResultErrorConstructor): Promise<Simplify<O>>;
|
|
1588
1589
|
/**
|
|
1589
1590
|
* Executes the query and streams the rows.
|
|
1590
1591
|
*
|
|
@@ -5,7 +5,7 @@ import { TableExpression, From, FromTables } from '../parser/table-parser.js';
|
|
|
5
5
|
import { SelectExpression } from '../parser/select-parser.js';
|
|
6
6
|
import { ReturningRow } from '../parser/returning-parser.js';
|
|
7
7
|
import { ReferenceExpression } from '../parser/reference-parser.js';
|
|
8
|
-
import { MergePartial, Nullable,
|
|
8
|
+
import { MergePartial, Nullable, SimplifyResult, SimplifySingleResult } from '../util/type-utils.js';
|
|
9
9
|
import { UpdateQueryNode } from '../operation-node/update-query-node.js';
|
|
10
10
|
import { UpdateObject } from '../parser/update-set-parser.js';
|
|
11
11
|
import { Compilable } from '../util/compilable.js';
|
|
@@ -869,18 +869,18 @@ export declare class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyo
|
|
|
869
869
|
*/
|
|
870
870
|
withPlugin(plugin: KyselyPlugin): UpdateQueryBuilder<DB, UT, TB, O>;
|
|
871
871
|
toOperationNode(): UpdateQueryNode;
|
|
872
|
-
compile(): CompiledQuery<O
|
|
872
|
+
compile(): CompiledQuery<SimplifyResult<O>>;
|
|
873
873
|
/**
|
|
874
874
|
* Executes the query and returns an array of rows.
|
|
875
875
|
*
|
|
876
876
|
* Also see the {@link executeTakeFirst} and {@link executeTakeFirstOrThrow} methods.
|
|
877
877
|
*/
|
|
878
|
-
execute(): Promise<O[]>;
|
|
878
|
+
execute(): Promise<SimplifyResult<O>[]>;
|
|
879
879
|
/**
|
|
880
880
|
* Executes the query and returns the first result or undefined if
|
|
881
881
|
* the query returned no result.
|
|
882
882
|
*/
|
|
883
|
-
executeTakeFirst(): Promise<
|
|
883
|
+
executeTakeFirst(): Promise<SimplifySingleResult<O>>;
|
|
884
884
|
/**
|
|
885
885
|
* Executes the query and returns the first result or throws if
|
|
886
886
|
* the query returned no result.
|
|
@@ -889,7 +889,7 @@ export declare class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyo
|
|
|
889
889
|
* provide a custom error class as the only argument to throw a different
|
|
890
890
|
* error.
|
|
891
891
|
*/
|
|
892
|
-
executeTakeFirstOrThrow(errorConstructor?: NoResultErrorConstructor): Promise<O
|
|
892
|
+
executeTakeFirstOrThrow(errorConstructor?: NoResultErrorConstructor): Promise<SimplifyResult<O>>;
|
|
893
893
|
/**
|
|
894
894
|
* Executes query with `explain` statement before `update` keyword.
|
|
895
895
|
*
|
|
@@ -384,7 +384,7 @@ export class UpdateQueryBuilder {
|
|
|
384
384
|
return [
|
|
385
385
|
new UpdateResult(
|
|
386
386
|
// TODO: remove numUpdatedOrDeletedRows.
|
|
387
|
-
|
|
387
|
+
result.numAffectedRows ?? result.numUpdatedOrDeletedRows ?? 0n),
|
|
388
388
|
];
|
|
389
389
|
}
|
|
390
390
|
/**
|
|
@@ -862,7 +862,7 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
862
862
|
if (node.distinct) {
|
|
863
863
|
this.append('distinct ');
|
|
864
864
|
}
|
|
865
|
-
this.visitNode(node.
|
|
865
|
+
this.visitNode(node.aggregated);
|
|
866
866
|
this.append(')');
|
|
867
867
|
if (node.filter) {
|
|
868
868
|
this.append(' filter(');
|
|
@@ -198,6 +198,8 @@ export declare class QueryCreator<DB> {
|
|
|
198
198
|
*
|
|
199
199
|
* ### Examples
|
|
200
200
|
*
|
|
201
|
+
* Deleting person with id 1:
|
|
202
|
+
*
|
|
201
203
|
* ```ts
|
|
202
204
|
* const result = await db
|
|
203
205
|
* .deleteFrom('person')
|
|
@@ -206,7 +208,34 @@ export declare class QueryCreator<DB> {
|
|
|
206
208
|
*
|
|
207
209
|
* console.log(result.numDeletedRows)
|
|
208
210
|
* ```
|
|
211
|
+
*
|
|
212
|
+
* The generated SQL (PostgreSQL):
|
|
213
|
+
*
|
|
214
|
+
* ```sql
|
|
215
|
+
* delete from "person" where "person"."id" = $1
|
|
216
|
+
* ```
|
|
217
|
+
*
|
|
218
|
+
* Some databases such as MySQL support deleting from multiple tables:
|
|
219
|
+
*
|
|
220
|
+
* ```ts
|
|
221
|
+
* const result = await db
|
|
222
|
+
* .deleteFrom(['person', 'pet'])
|
|
223
|
+
* .using('person')
|
|
224
|
+
* .innerJoin('pet', 'pet.owner_id', '=', 'person.id')
|
|
225
|
+
* .where('person.id', '=', 1)
|
|
226
|
+
* .executeTakeFirst()
|
|
227
|
+
* ```
|
|
228
|
+
*
|
|
229
|
+
* The generated SQL (MySQL):
|
|
230
|
+
*
|
|
231
|
+
* ```sql
|
|
232
|
+
* delete from `person`, `pet`
|
|
233
|
+
* using `person`
|
|
234
|
+
* inner join `pet` on `pet`.`owner_id` = `person`.`id`
|
|
235
|
+
* where `person`.`id` = ?
|
|
236
|
+
* ```
|
|
209
237
|
*/
|
|
238
|
+
deleteFrom<TR extends TableReference<DB>>(tables: TR[]): DeleteQueryBuilder<From<DB, TR>, FromTables<DB, never, TR>, DeleteResult>;
|
|
210
239
|
deleteFrom<TR extends TableReference<DB>>(table: TR): DeleteQueryBuilder<From<DB, TR>, FromTables<DB, never, TR>, DeleteResult>;
|
|
211
240
|
/**
|
|
212
241
|
* Creates an update query.
|
|
@@ -103,30 +103,11 @@ export class QueryCreator {
|
|
|
103
103
|
queryNode: InsertQueryNode.create(parseTable(table), this.#props.withNode, true),
|
|
104
104
|
});
|
|
105
105
|
}
|
|
106
|
-
|
|
107
|
-
* Creates a delete query.
|
|
108
|
-
*
|
|
109
|
-
* See the {@link DeleteQueryBuilder.where} method for examples on how to specify
|
|
110
|
-
* a where clause for the delete operation.
|
|
111
|
-
*
|
|
112
|
-
* The return value of the query is an instance of {@link DeleteResult}.
|
|
113
|
-
*
|
|
114
|
-
* ### Examples
|
|
115
|
-
*
|
|
116
|
-
* ```ts
|
|
117
|
-
* const result = await db
|
|
118
|
-
* .deleteFrom('person')
|
|
119
|
-
* .where('person.id', '=', 1)
|
|
120
|
-
* .executeTakeFirst()
|
|
121
|
-
*
|
|
122
|
-
* console.log(result.numDeletedRows)
|
|
123
|
-
* ```
|
|
124
|
-
*/
|
|
125
|
-
deleteFrom(table) {
|
|
106
|
+
deleteFrom(tables) {
|
|
126
107
|
return new DeleteQueryBuilder({
|
|
127
108
|
queryId: createQueryId(),
|
|
128
109
|
executor: this.#props.executor,
|
|
129
|
-
queryNode: DeleteQueryNode.create(
|
|
110
|
+
queryNode: DeleteQueryNode.create(parseTableExpressionOrList(tables), this.#props.withNode),
|
|
130
111
|
});
|
|
131
112
|
}
|
|
132
113
|
/**
|
|
@@ -100,10 +100,11 @@ export declare type ArrayItemType<T> = T extends ReadonlyArray<infer I> ? I : ne
|
|
|
100
100
|
* Any select query builder.
|
|
101
101
|
*/
|
|
102
102
|
export declare type AnySelectQueryBuilder = SelectQueryBuilder<any, any, any>;
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
103
|
+
export declare type SimplifySingleResult<O> = O extends InsertResult ? O : O extends DeleteResult ? O : O extends UpdateResult ? O : Simplify<O> | undefined;
|
|
104
|
+
export declare type SimplifyResult<O> = O extends InsertResult ? O : O extends DeleteResult ? O : O extends UpdateResult ? O : Simplify<O>;
|
|
105
|
+
export declare type Simplify<T> = {
|
|
106
|
+
[K in keyof T]: T[K];
|
|
107
|
+
} & {};
|
|
107
108
|
/**
|
|
108
109
|
* Represents a database row whose column names and their types are unknown.
|
|
109
110
|
*/
|