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
|
@@ -4,6 +4,7 @@ exports.FunctionModule = void 0;
|
|
|
4
4
|
const aggregate_function_node_js_1 = require("../operation-node/aggregate-function-node.js");
|
|
5
5
|
const coalesce_parser_js_1 = require("../parser/coalesce-parser.js");
|
|
6
6
|
const reference_parser_js_1 = require("../parser/reference-parser.js");
|
|
7
|
+
const select_parser_js_1 = require("../parser/select-parser.js");
|
|
7
8
|
const raw_builder_js_1 = require("../raw-builder/raw-builder.js");
|
|
8
9
|
const query_id_js_1 = require("../util/query-id.js");
|
|
9
10
|
const aggregate_function_builder_js_1 = require("./aggregate-function-builder.js");
|
|
@@ -44,6 +45,7 @@ class FunctionModule {
|
|
|
44
45
|
this.avg = this.avg.bind(this);
|
|
45
46
|
this.coalesce = this.coalesce.bind(this);
|
|
46
47
|
this.count = this.count.bind(this);
|
|
48
|
+
this.countAll = this.countAll.bind(this);
|
|
47
49
|
this.max = this.max.bind(this);
|
|
48
50
|
this.min = this.min.bind(this);
|
|
49
51
|
this.sum = this.sum.bind(this);
|
|
@@ -51,8 +53,38 @@ class FunctionModule {
|
|
|
51
53
|
/**
|
|
52
54
|
* Calls the `avg` function for the column given as the argument.
|
|
53
55
|
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
+
* This sql function calculates the average value for a given column.
|
|
57
|
+
*
|
|
58
|
+
* For additional functionality such as distinct, filtering and window functions,
|
|
59
|
+
* refer to {@link AggregateFunctionBuilder}. An instance of this builder is
|
|
60
|
+
* returned when calling this function.
|
|
61
|
+
*
|
|
62
|
+
* ### Examples
|
|
63
|
+
*
|
|
64
|
+
* ```ts
|
|
65
|
+
* const { avg } = db.fn
|
|
66
|
+
*
|
|
67
|
+
* db.selectFrom('toy')
|
|
68
|
+
* .select(avg('price').as('avg_price'))
|
|
69
|
+
* .execute()
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* The generated SQL (PostgreSQL):
|
|
73
|
+
*
|
|
74
|
+
* ```sql
|
|
75
|
+
* select avg("price") as "avg_price" from "toy"
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* You can limit column range to only columns participating in current query:
|
|
79
|
+
*
|
|
80
|
+
* ```ts
|
|
81
|
+
* db.selectFrom('toy')
|
|
82
|
+
* .select(qb => qb.fn.avg('price').as('avg_price'))
|
|
83
|
+
* .execute()
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* If this function is used in a `select` statement, the type of the selected
|
|
87
|
+
* expression will be `number | string` by default. This is because Kysely can't know the
|
|
56
88
|
* type the db driver outputs. Sometimes the output can be larger than the largest
|
|
57
89
|
* javascript number and a string is returned instead. Most drivers allow you
|
|
58
90
|
* to configure the output type of large numbers and Kysely can't know if you've
|
|
@@ -71,7 +103,7 @@ class FunctionModule {
|
|
|
71
103
|
*
|
|
72
104
|
* Sometimes a null is returned, e.g. when row count is 0, and no `group by`
|
|
73
105
|
* was used. It is highly recommended to include null in the output type union
|
|
74
|
-
* and handle null values in post-execute code, or wrap the function with a
|
|
106
|
+
* and handle null values in post-execute code, or wrap the function with a {@link coalesce}
|
|
75
107
|
* function.
|
|
76
108
|
*
|
|
77
109
|
* ```ts
|
|
@@ -90,36 +122,66 @@ class FunctionModule {
|
|
|
90
122
|
/**
|
|
91
123
|
* Calls the `coalesce` function for given arguments.
|
|
92
124
|
*
|
|
93
|
-
* This function returns the first non-null value from left to right, commonly
|
|
125
|
+
* This sql function returns the first non-null value from left to right, commonly
|
|
94
126
|
* used to provide a default scalar for nullable columns or functions.
|
|
95
127
|
*
|
|
128
|
+
* If this function is used in a `select` statement, the type of the selected
|
|
129
|
+
* expression is inferred in the same manner that the sql function computes.
|
|
130
|
+
* A union of arguments' types - if a non-nullable argument exists, it stops
|
|
131
|
+
* there (ignoring any further arguments' types) and exludes null from the final
|
|
132
|
+
* union type.
|
|
133
|
+
*
|
|
134
|
+
* `(string | null, number | null)` is inferred as `string | number | null`.
|
|
135
|
+
*
|
|
136
|
+
* `(string | null, number, Date | null)` is inferred as `string | number`.
|
|
137
|
+
*
|
|
138
|
+
* `(number, string | null)` is inferred as `number`.
|
|
139
|
+
*
|
|
140
|
+
* ### Examples
|
|
141
|
+
*
|
|
96
142
|
* ```ts
|
|
97
|
-
* const { coalesce
|
|
143
|
+
* const { coalesce } = db.fn
|
|
98
144
|
*
|
|
99
|
-
* db.selectFrom('
|
|
100
|
-
* .select(coalesce(
|
|
101
|
-
* .where('
|
|
145
|
+
* db.selectFrom('participant')
|
|
146
|
+
* .select(coalesce('nickname', sql<string>`'<anonymous>'`).as('nickname'))
|
|
147
|
+
* .where('room_id', '=', roomId)
|
|
102
148
|
* .execute()
|
|
103
149
|
* ```
|
|
104
150
|
*
|
|
105
|
-
* The generated SQL (
|
|
151
|
+
* The generated SQL (PostgreSQL):
|
|
106
152
|
*
|
|
107
153
|
* ```sql
|
|
108
|
-
* select coalesce(
|
|
154
|
+
* select coalesce("nickname", '<anonymous>') as "nickname"
|
|
155
|
+
* from "participant" where "room_id" = $1
|
|
109
156
|
* ```
|
|
110
157
|
*
|
|
111
|
-
*
|
|
112
|
-
* is inferred in the same manner that the function computes. A union of arguments'
|
|
113
|
-
* types - if a non-nullable argument exists, it stops there (ignoring any further
|
|
114
|
-
* arguments' types) and exludes null from the final union type.
|
|
158
|
+
* You can limit column range to only columns participating in current query:
|
|
115
159
|
*
|
|
116
|
-
*
|
|
160
|
+
* ```ts
|
|
161
|
+
* db.selectFrom('participant')
|
|
162
|
+
* .select(qb =>
|
|
163
|
+
* qb.fn.coalesce('nickname', sql<string>`'<anonymous>'`).as('nickname')
|
|
164
|
+
* )
|
|
165
|
+
* .where('room_id', '=', roomId)
|
|
166
|
+
* .execute()
|
|
167
|
+
* ```
|
|
117
168
|
*
|
|
118
|
-
*
|
|
169
|
+
* You can combine this function with other helpers in this module:
|
|
119
170
|
*
|
|
120
|
-
*
|
|
171
|
+
* ```ts
|
|
172
|
+
* const { avg, coalesce } = db.fn
|
|
121
173
|
*
|
|
122
|
-
*
|
|
174
|
+
* db.selectFrom('person')
|
|
175
|
+
* .select(coalesce(avg<number | null>('age'), sql<number>`0`).as('avg_age'))
|
|
176
|
+
* .where('first_name', '=', 'Jennifer')
|
|
177
|
+
* .execute()
|
|
178
|
+
* ```
|
|
179
|
+
*
|
|
180
|
+
* The generated SQL (PostgreSQL):
|
|
181
|
+
*
|
|
182
|
+
* ```sql
|
|
183
|
+
* select coalesce(avg("age"), 0) as "avg_age" from "person" where "first_name" = $1
|
|
184
|
+
* ```
|
|
123
185
|
*/
|
|
124
186
|
coalesce(value, ...otherValues) {
|
|
125
187
|
return new raw_builder_js_1.RawBuilder({
|
|
@@ -130,11 +192,36 @@ class FunctionModule {
|
|
|
130
192
|
/**
|
|
131
193
|
* Calls the `count` function for the column given as the argument.
|
|
132
194
|
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
195
|
+
* When called with a column as argument, this sql function counts the number of rows where there
|
|
196
|
+
* is a non-null value in that column.
|
|
197
|
+
*
|
|
198
|
+
* For counting all rows nulls included (`count(*)`), see {@link countAll}.
|
|
199
|
+
*
|
|
200
|
+
* For additional functionality such as distinct, filtering and window functions,
|
|
201
|
+
* refer to {@link AggregateFunctionBuilder}. An instance of this builder is
|
|
202
|
+
* returned when calling this function.
|
|
203
|
+
*
|
|
204
|
+
* ### Examples
|
|
205
|
+
*
|
|
206
|
+
* ```ts
|
|
207
|
+
* const { count } = db.fn
|
|
208
|
+
*
|
|
209
|
+
* db.selectFrom('toy')
|
|
210
|
+
* .select(count('id').as('num_toys'))
|
|
211
|
+
* .execute()
|
|
212
|
+
* ```
|
|
213
|
+
*
|
|
214
|
+
* The generated SQL (PostgreSQL):
|
|
215
|
+
*
|
|
216
|
+
* ```sql
|
|
217
|
+
* select count("id") as "num_toys" from "toy"
|
|
218
|
+
* ```
|
|
219
|
+
*
|
|
220
|
+
* If this function is used in a `select` statement, the type of the selected
|
|
221
|
+
* expression will be `number | string | bigint` by default. This is because
|
|
222
|
+
* Kysely can't know the type the db driver outputs. Sometimes the output can
|
|
223
|
+
* be larger than the largest javascript number and a string is returned instead.
|
|
224
|
+
* Most drivers allow you to configure the output type of large numbers and Kysely
|
|
138
225
|
* can't know if you've done so.
|
|
139
226
|
*
|
|
140
227
|
* You can specify the output type of the expression by providing
|
|
@@ -148,11 +235,11 @@ class FunctionModule {
|
|
|
148
235
|
* .execute()
|
|
149
236
|
* ```
|
|
150
237
|
*
|
|
151
|
-
* You can limit column range:
|
|
238
|
+
* You can limit column range to only columns participating in current query:
|
|
152
239
|
*
|
|
153
240
|
* ```ts
|
|
154
241
|
* db.selectFrom('toy')
|
|
155
|
-
* .select(qb => qb.fn.count
|
|
242
|
+
* .select(qb => qb.fn.count('id').as('num_toys'))
|
|
156
243
|
* .execute()
|
|
157
244
|
* ```
|
|
158
245
|
*/
|
|
@@ -161,6 +248,11 @@ class FunctionModule {
|
|
|
161
248
|
aggregateFunctionNode: aggregate_function_node_js_1.AggregateFunctionNode.create('count', (0, reference_parser_js_1.parseSimpleReferenceExpression)(column)),
|
|
162
249
|
});
|
|
163
250
|
}
|
|
251
|
+
countAll(table) {
|
|
252
|
+
return new aggregate_function_builder_js_1.AggregateFunctionBuilder({
|
|
253
|
+
aggregateFunctionNode: aggregate_function_node_js_1.AggregateFunctionNode.create('count', (0, select_parser_js_1.parseSelectAll)(table)[0].selection),
|
|
254
|
+
});
|
|
255
|
+
}
|
|
164
256
|
max(column) {
|
|
165
257
|
return new aggregate_function_builder_js_1.AggregateFunctionBuilder({
|
|
166
258
|
aggregateFunctionNode: aggregate_function_node_js_1.AggregateFunctionNode.create('max', (0, reference_parser_js_1.parseSimpleReferenceExpression)(column)),
|
|
@@ -174,15 +266,45 @@ class FunctionModule {
|
|
|
174
266
|
/**
|
|
175
267
|
* Calls the `sum` function for the column given as the argument.
|
|
176
268
|
*
|
|
177
|
-
*
|
|
178
|
-
* will be `number | string | bigint` by default. This is because Kysely
|
|
179
|
-
* can't know the type the db driver outputs. Sometimes the output can be larger
|
|
180
|
-
* than the largest javascript number and a string is returned instead. Most
|
|
181
|
-
* drivers allow you to configure the output type of large numbers and Kysely
|
|
182
|
-
* can't know if you've done so.
|
|
269
|
+
* This sql function sums the values of a given column.
|
|
183
270
|
*
|
|
184
|
-
*
|
|
185
|
-
*
|
|
271
|
+
* For additional functionality such as distinct, filtering and window functions,
|
|
272
|
+
* refer to {@link AggregateFunctionBuilder}. An instance of this builder is
|
|
273
|
+
* returned when calling this function.
|
|
274
|
+
*
|
|
275
|
+
* ### Examples
|
|
276
|
+
*
|
|
277
|
+
* ```ts
|
|
278
|
+
* const { sum } = db.fn
|
|
279
|
+
*
|
|
280
|
+
* db.selectFrom('toy')
|
|
281
|
+
* .select(sum('price').as('total_price'))
|
|
282
|
+
* .execute()
|
|
283
|
+
* ```
|
|
284
|
+
*
|
|
285
|
+
* The generated SQL (PostgreSQL):
|
|
286
|
+
*
|
|
287
|
+
* ```sql
|
|
288
|
+
* select sum("price") as "total_price" from "toy"
|
|
289
|
+
* ```
|
|
290
|
+
*
|
|
291
|
+
* You can limit column range to only columns participating in current query:
|
|
292
|
+
*
|
|
293
|
+
* ```ts
|
|
294
|
+
* db.selectFrom('toy')
|
|
295
|
+
* .select(qb => qb.fn.sum('price').as('total_price'))
|
|
296
|
+
* .execute()
|
|
297
|
+
* ```
|
|
298
|
+
*
|
|
299
|
+
* If this function is used in a `select` statement, the type of the selected
|
|
300
|
+
* expression will be `number | string` by default. This is because Kysely can't know the
|
|
301
|
+
* type the db driver outputs. Sometimes the output can be larger than the largest
|
|
302
|
+
* javascript number and a string is returned instead. Most drivers allow you
|
|
303
|
+
* to configure the output type of large numbers and Kysely can't know if you've
|
|
304
|
+
* done so.
|
|
305
|
+
*
|
|
306
|
+
* You can specify the output type of the expression by providing the type as
|
|
307
|
+
* the first type argument:
|
|
186
308
|
*
|
|
187
309
|
* ```ts
|
|
188
310
|
* const { sum } = db.fn
|
|
@@ -194,7 +316,7 @@ class FunctionModule {
|
|
|
194
316
|
*
|
|
195
317
|
* Sometimes a null is returned, e.g. when row count is 0, and no `group by`
|
|
196
318
|
* was used. It is highly recommended to include null in the output type union
|
|
197
|
-
* and handle null values in post-execute code, or wrap the function with a
|
|
319
|
+
* and handle null values in post-execute code, or wrap the function with a {@link coalesce}
|
|
198
320
|
* function.
|
|
199
321
|
*
|
|
200
322
|
* ```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
|
*
|
|
@@ -386,7 +386,7 @@ class UpdateQueryBuilder {
|
|
|
386
386
|
return [
|
|
387
387
|
new update_result_js_1.UpdateResult(
|
|
388
388
|
// TODO: remove numUpdatedOrDeletedRows.
|
|
389
|
-
|
|
389
|
+
result.numAffectedRows ?? result.numUpdatedOrDeletedRows ?? 0n),
|
|
390
390
|
];
|
|
391
391
|
}
|
|
392
392
|
/**
|
|
@@ -864,7 +864,7 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
|
|
|
864
864
|
if (node.distinct) {
|
|
865
865
|
this.append('distinct ');
|
|
866
866
|
}
|
|
867
|
-
this.visitNode(node.
|
|
867
|
+
this.visitNode(node.aggregated);
|
|
868
868
|
this.append(')');
|
|
869
869
|
if (node.filter) {
|
|
870
870
|
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.
|
|
@@ -105,30 +105,11 @@ class QueryCreator {
|
|
|
105
105
|
queryNode: insert_query_node_js_1.InsertQueryNode.create((0, table_parser_js_1.parseTable)(table), this.#props.withNode, true),
|
|
106
106
|
});
|
|
107
107
|
}
|
|
108
|
-
|
|
109
|
-
* Creates a delete query.
|
|
110
|
-
*
|
|
111
|
-
* See the {@link DeleteQueryBuilder.where} method for examples on how to specify
|
|
112
|
-
* a where clause for the delete operation.
|
|
113
|
-
*
|
|
114
|
-
* The return value of the query is an instance of {@link DeleteResult}.
|
|
115
|
-
*
|
|
116
|
-
* ### Examples
|
|
117
|
-
*
|
|
118
|
-
* ```ts
|
|
119
|
-
* const result = await db
|
|
120
|
-
* .deleteFrom('person')
|
|
121
|
-
* .where('person.id', '=', 1)
|
|
122
|
-
* .executeTakeFirst()
|
|
123
|
-
*
|
|
124
|
-
* console.log(result.numDeletedRows)
|
|
125
|
-
* ```
|
|
126
|
-
*/
|
|
127
|
-
deleteFrom(table) {
|
|
108
|
+
deleteFrom(tables) {
|
|
128
109
|
return new delete_query_builder_js_1.DeleteQueryBuilder({
|
|
129
110
|
queryId: (0, query_id_js_1.createQueryId)(),
|
|
130
111
|
executor: this.#props.executor,
|
|
131
|
-
queryNode: delete_query_node_js_1.DeleteQueryNode.create((0, table_parser_js_1.
|
|
112
|
+
queryNode: delete_query_node_js_1.DeleteQueryNode.create((0, table_parser_js_1.parseTableExpressionOrList)(tables), this.#props.withNode),
|
|
132
113
|
});
|
|
133
114
|
}
|
|
134
115
|
/**
|
|
@@ -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
|
*/
|
|
@@ -7,7 +7,7 @@ export interface DatabaseIntrospector {
|
|
|
7
7
|
*/
|
|
8
8
|
getSchemas(): Promise<SchemaMetadata[]>;
|
|
9
9
|
/**
|
|
10
|
-
* Get
|
|
10
|
+
* Get tables and views metadata.
|
|
11
11
|
*/
|
|
12
12
|
getTables(options?: DatabaseMetadataOptions): Promise<TableMetadata[]>;
|
|
13
13
|
/**
|
|
@@ -29,12 +29,14 @@ export interface SchemaMetadata {
|
|
|
29
29
|
}
|
|
30
30
|
export interface DatabaseMetadata {
|
|
31
31
|
/**
|
|
32
|
-
* The tables found in the database.
|
|
32
|
+
* The tables and views found in the database.
|
|
33
|
+
* * The propery isView can be used to tell them apart.
|
|
33
34
|
*/
|
|
34
35
|
readonly tables: TableMetadata[];
|
|
35
36
|
}
|
|
36
37
|
export interface TableMetadata {
|
|
37
38
|
readonly name: string;
|
|
39
|
+
readonly isView: boolean;
|
|
38
40
|
readonly columns: ColumnMetadata[];
|
|
39
41
|
readonly schema?: string;
|
|
40
42
|
}
|
|
@@ -8,7 +8,7 @@ export declare class MysqlIntrospector implements DatabaseIntrospector {
|
|
|
8
8
|
*/
|
|
9
9
|
getSchemas(): Promise<SchemaMetadata[]>;
|
|
10
10
|
/**
|
|
11
|
-
* Get
|
|
11
|
+
* Get tables and views metadata.
|
|
12
12
|
*/
|
|
13
13
|
getTables(options?: DatabaseMetadataOptions): Promise<TableMetadata[]>;
|
|
14
14
|
/**
|
|
@@ -17,24 +17,29 @@ export class MysqlIntrospector {
|
|
|
17
17
|
}
|
|
18
18
|
async getTables(options = { withInternalKyselyTables: false }) {
|
|
19
19
|
let query = this.#db
|
|
20
|
-
.selectFrom('information_schema.columns')
|
|
20
|
+
.selectFrom('information_schema.columns as columns')
|
|
21
|
+
.innerJoin('information_schema.tables as tables', (b) => b
|
|
22
|
+
.onRef('columns.TABLE_CATALOG', '=', 'tables.TABLE_CATALOG')
|
|
23
|
+
.onRef('columns.TABLE_SCHEMA', '=', 'tables.TABLE_SCHEMA')
|
|
24
|
+
.onRef('columns.TABLE_NAME', '=', 'tables.TABLE_NAME'))
|
|
21
25
|
.select([
|
|
22
|
-
'COLUMN_NAME',
|
|
23
|
-
'COLUMN_DEFAULT',
|
|
24
|
-
'TABLE_NAME',
|
|
25
|
-
'TABLE_SCHEMA',
|
|
26
|
-
'
|
|
27
|
-
'
|
|
28
|
-
'
|
|
26
|
+
'columns.COLUMN_NAME',
|
|
27
|
+
'columns.COLUMN_DEFAULT',
|
|
28
|
+
'columns.TABLE_NAME',
|
|
29
|
+
'columns.TABLE_SCHEMA',
|
|
30
|
+
'tables.TABLE_TYPE',
|
|
31
|
+
'columns.IS_NULLABLE',
|
|
32
|
+
'columns.DATA_TYPE',
|
|
33
|
+
'columns.EXTRA',
|
|
29
34
|
])
|
|
30
|
-
.where('
|
|
31
|
-
.orderBy('
|
|
32
|
-
.orderBy('
|
|
35
|
+
.where('columns.TABLE_SCHEMA', '=', sql `database()`)
|
|
36
|
+
.orderBy('columns.TABLE_NAME')
|
|
37
|
+
.orderBy('columns.ORDINAL_POSITION')
|
|
33
38
|
.$castTo();
|
|
34
39
|
if (!options.withInternalKyselyTables) {
|
|
35
40
|
query = query
|
|
36
|
-
.where('
|
|
37
|
-
.where('
|
|
41
|
+
.where('columns.TABLE_NAME', '!=', DEFAULT_MIGRATION_TABLE)
|
|
42
|
+
.where('columns.TABLE_NAME', '!=', DEFAULT_MIGRATION_LOCK_TABLE);
|
|
38
43
|
}
|
|
39
44
|
const rawColumns = await query.execute();
|
|
40
45
|
return this.#parseTableMetadata(rawColumns);
|
|
@@ -50,6 +55,7 @@ export class MysqlIntrospector {
|
|
|
50
55
|
if (!table) {
|
|
51
56
|
table = freeze({
|
|
52
57
|
name: it.TABLE_NAME,
|
|
58
|
+
isView: it.TABLE_TYPE === 'VIEW',
|
|
53
59
|
schema: it.TABLE_SCHEMA,
|
|
54
60
|
columns: [],
|
|
55
61
|
});
|
|
@@ -8,7 +8,7 @@ export declare class PostgresIntrospector implements DatabaseIntrospector {
|
|
|
8
8
|
*/
|
|
9
9
|
getSchemas(): Promise<SchemaMetadata[]>;
|
|
10
10
|
/**
|
|
11
|
-
* Get
|
|
11
|
+
* Get tables and views metadata.
|
|
12
12
|
*/
|
|
13
13
|
getTables(options?: DatabaseMetadataOptions): Promise<TableMetadata[]>;
|
|
14
14
|
/**
|