kysely 0.3.8 → 0.3.9
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/.prettierignore +1 -0
- package/README.md +6 -0
- package/dist/cjs/dialect/postgres/postgres-driver.js +1 -0
- package/dist/cjs/driver/driver-config.d.ts +6 -0
- package/dist/cjs/parser/select-parser.d.ts +2 -2
- package/dist/cjs/parser/table-parser.d.ts +2 -2
- package/dist/cjs/query-builder/query-builder.d.ts +143 -76
- package/dist/cjs/query-builder/query-builder.js +12 -7
- package/dist/cjs/query-builder/type-utils.d.ts +14 -5
- package/dist/esm/dialect/postgres/postgres-driver.js +1 -0
- package/dist/esm/driver/driver-config.d.ts +6 -0
- package/dist/esm/parser/select-parser.d.ts +2 -2
- package/dist/esm/parser/table-parser.d.ts +2 -2
- package/dist/esm/query-builder/query-builder.d.ts +143 -76
- package/dist/esm/query-builder/query-builder.js +12 -7
- package/dist/esm/query-builder/type-utils.d.ts +14 -5
- package/package.json +2 -2
package/.prettierignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
*.md
|
package/README.md
CHANGED
|
@@ -29,6 +29,12 @@ Kysely is still young and some useful methods and modules are not yet implemente
|
|
|
29
29
|
Kysely, please open an issue as soon as you can't find something you want to use.
|
|
30
30
|
|
|
31
31
|
- [API reference](https://koskimas.github.io/kysely/index.html)
|
|
32
|
+
- [select](https://koskimas.github.io/kysely/classes/QueryBuilder.html#select)
|
|
33
|
+
- [where](https://koskimas.github.io/kysely/classes/QueryBuilder.html#where)
|
|
34
|
+
- [join](https://koskimas.github.io/kysely/classes/QueryBuilder.html#innerJoin)
|
|
35
|
+
- [insert](https://koskimas.github.io/kysely/classes/QueryBuilder.html#values)
|
|
36
|
+
- [update](https://koskimas.github.io/kysely/classes/QueryBuilder.html#set)
|
|
37
|
+
- [delete](https://koskimas.github.io/kysely/classes/Kysely.html#deleteFrom)
|
|
32
38
|
- [Migrations](#migrations)
|
|
33
39
|
|
|
34
40
|
# Installation
|
|
@@ -26,6 +26,7 @@ class PostgresDriver extends driver_js_1.Driver {
|
|
|
26
26
|
port: cfg.port,
|
|
27
27
|
user: cfg.user,
|
|
28
28
|
password: cfg.password,
|
|
29
|
+
ssl: cfg.ssl,
|
|
29
30
|
// Pool options.
|
|
30
31
|
connectionTimeoutMillis: cfg.pool.connectionTimeoutMillis,
|
|
31
32
|
idleTimeoutMillis: cfg.pool.idleTimeoutMillis,
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { SecureContextOptions } from 'tls';
|
|
1
3
|
import { DatabaseConnection } from './database-connection.js';
|
|
2
4
|
export interface DriverConfig {
|
|
3
5
|
/**
|
|
@@ -24,6 +26,10 @@ export interface DriverConfig {
|
|
|
24
26
|
* Connection pooling config.
|
|
25
27
|
*/
|
|
26
28
|
pool?: PoolConfig;
|
|
29
|
+
/**
|
|
30
|
+
* Optional SSL configuration.
|
|
31
|
+
*/
|
|
32
|
+
ssl?: SecureContextOptions;
|
|
27
33
|
}
|
|
28
34
|
export interface PoolConfig {
|
|
29
35
|
/**
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { AliasedRawBuilder } from '../raw-builder/raw-builder.js';
|
|
2
2
|
import { AliasedQueryBuilder, QueryBuilder } from '../query-builder/query-builder.js';
|
|
3
3
|
import { SelectionNode } from '../operation-node/selection-node.js';
|
|
4
|
-
import { AliasedQueryBuilderFactory, AliasedRawBuilderFactory, AnyAliasedColumn, AnyAliasedColumnWithTable, AnyAliasedQueryBuilder, AnyColumn, AnyColumnWithTable, InsertResultTypeTag, RowType, ValueType } from '../query-builder/type-utils.js';
|
|
4
|
+
import { AliasedQueryBuilderFactory, AliasedRawBuilderFactory, AnyAliasedColumn, AnyAliasedColumnWithTable, AnyAliasedQueryBuilder, AnyAliasedRawBuilder, AnyColumn, AnyColumnWithTable, InsertResultTypeTag, RowType, ValueType } from '../query-builder/type-utils.js';
|
|
5
5
|
import { DynamicReferenceBuilder } from '../dynamic/dynamic-reference-builder.js';
|
|
6
6
|
/**
|
|
7
7
|
* A selection expression.
|
|
8
8
|
*/
|
|
9
|
-
export declare type SelectExpression<DB, TB extends keyof DB> = AnyAliasedColumnWithTable<DB, TB> | AnyAliasedColumn<DB, TB> | AnyColumnWithTable<DB, TB> | AnyColumn<DB, TB> |
|
|
9
|
+
export declare type SelectExpression<DB, TB extends keyof DB> = AnyAliasedColumnWithTable<DB, TB> | AnyAliasedColumn<DB, TB> | AnyColumnWithTable<DB, TB> | AnyColumn<DB, TB> | AnyAliasedRawBuilder | AliasedRawBuilderFactory<DB, TB> | AnyAliasedQueryBuilder | AliasedQueryBuilderFactory<DB, TB> | DynamicReferenceBuilder<any>;
|
|
10
10
|
export declare type SelectExpressionOrList<DB, TB extends keyof DB> = SelectExpression<DB, TB> | ReadonlyArray<SelectExpression<DB, TB>>;
|
|
11
11
|
/**
|
|
12
12
|
* Given a selection expression returns a query builder type that
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { AliasedQueryBuilder, QueryBuilder } from '../query-builder/query-builder.js';
|
|
2
2
|
import { TableNode } from '../operation-node/table-node.js';
|
|
3
|
-
import { AliasedQueryBuilderFactory, AnyAliasedQueryBuilder } from '../query-builder/type-utils.js';
|
|
3
|
+
import { AliasedQueryBuilderFactory, AnyAliasedQueryBuilder, AnyAliasedRawBuilder } from '../query-builder/type-utils.js';
|
|
4
4
|
import { AliasedRawBuilder } from '../raw-builder/raw-builder.js';
|
|
5
5
|
import { TableExpressionNode } from '../operation-node/operation-node-utils.js';
|
|
6
6
|
export declare type TableExpression<DB, TB extends keyof DB> = TableReference<DB> | AnyAliasedQueryBuilder | AliasedQueryBuilderFactory<DB, TB>;
|
|
7
|
-
export declare type TableReference<DB> = AnyAliasedTable<DB, any, any> | AnyTable<DB> |
|
|
7
|
+
export declare type TableReference<DB> = AnyAliasedTable<DB, any, any> | AnyTable<DB> | AnyAliasedRawBuilder;
|
|
8
8
|
export declare type QueryBuilderWithTable<DB, TB extends keyof DB, O, TE> = QueryBuilder<TableExpressionDatabaseType<DB, TE>, TB | ExtractAliasFromTableExpression<DB, TE>, O>;
|
|
9
9
|
export declare type TableExpressionDatabaseType<DB, TE> = DB extends ExtractDatabaseTypeFromTableExpression<DB, TE> ? DB : DB & ExtractDatabaseTypeFromTableExpression<DB, TE>;
|
|
10
10
|
export declare type ExtractAliasFromTableExpression<DB, TE> = TE extends `${string} as ${infer TA}` ? TA : TE extends keyof DB ? TE : TE extends AliasedQueryBuilder<any, any, any, infer QA> ? QA : TE extends (qb: any) => AliasedQueryBuilder<any, any, any, infer QA> ? QA : TE extends (qb: any) => AliasedRawBuilder<any, infer RA> ? RA : never;
|
|
@@ -44,9 +44,11 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
44
44
|
* Find a row by column value:
|
|
45
45
|
*
|
|
46
46
|
* ```ts
|
|
47
|
-
* db
|
|
48
|
-
* .
|
|
47
|
+
* const person = await db
|
|
48
|
+
* .selectFrom('person')
|
|
49
49
|
* .selectAll()
|
|
50
|
+
* .where('id', '=', 100)
|
|
51
|
+
* .executeTakeFirst()
|
|
50
52
|
* ```
|
|
51
53
|
*
|
|
52
54
|
* The generated SQL (postgresql):
|
|
@@ -60,9 +62,11 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
60
62
|
* you can always use `db.raw('your operator')`.
|
|
61
63
|
*
|
|
62
64
|
* ```ts
|
|
63
|
-
* db
|
|
64
|
-
* .
|
|
65
|
+
* const persons = await db
|
|
66
|
+
* .selectFrom('person')
|
|
65
67
|
* .selectAll()
|
|
68
|
+
* .where('id', '>', 100)
|
|
69
|
+
* .execute()
|
|
66
70
|
* ```
|
|
67
71
|
*
|
|
68
72
|
* The generated SQL (postgresql):
|
|
@@ -72,22 +76,25 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
72
76
|
* ```
|
|
73
77
|
*
|
|
74
78
|
* @example
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
* instances.
|
|
79
|
+
* The `where` methods don't change the type of the query. You can add
|
|
80
|
+
* conditional `where`s easily by doing something like this:
|
|
78
81
|
*
|
|
79
82
|
* ```ts
|
|
80
|
-
* db
|
|
81
|
-
* .
|
|
83
|
+
* let query = db
|
|
84
|
+
* .selectFrom('person')
|
|
82
85
|
* .selectAll()
|
|
83
|
-
* ```
|
|
84
86
|
*
|
|
85
|
-
*
|
|
87
|
+
* if (firstName) {
|
|
88
|
+
* query = query.where('first_name', '=', firstName)
|
|
89
|
+
* }
|
|
86
90
|
*
|
|
87
|
-
*
|
|
88
|
-
* select * from "person" where "id" in ($1, $2, $3)
|
|
91
|
+
* const persons = await query.execute()
|
|
89
92
|
* ```
|
|
90
93
|
*
|
|
94
|
+
* This is true for basically all methods execpt the `select` and
|
|
95
|
+
* `returning` methods, which DO change the return type of the
|
|
96
|
+
* query.
|
|
97
|
+
*
|
|
91
98
|
* @example
|
|
92
99
|
* Both the first and third argument can also be subqueries.
|
|
93
100
|
* A subquery is defined by passing a function and calling
|
|
@@ -95,15 +102,18 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
95
102
|
* function:
|
|
96
103
|
*
|
|
97
104
|
* ```ts
|
|
98
|
-
* db
|
|
105
|
+
* const persons = await db
|
|
106
|
+
* .selectFrom('person')
|
|
107
|
+
* .selectAll()
|
|
99
108
|
* .where(
|
|
100
109
|
* (qb) => qb.subQuery('pet')
|
|
101
|
-
* .select('pet.
|
|
102
|
-
* .whereRef('pet.owner_id', '=', 'person.id')
|
|
103
|
-
*
|
|
104
|
-
*
|
|
110
|
+
* .select('pet.name')
|
|
111
|
+
* .whereRef('pet.owner_id', '=', 'person.id')
|
|
112
|
+
* .limit(1),
|
|
113
|
+
* '=',
|
|
114
|
+
* 'Fluffy'
|
|
105
115
|
* )
|
|
106
|
-
* .
|
|
116
|
+
* .execute()
|
|
107
117
|
* ```
|
|
108
118
|
*
|
|
109
119
|
* The generated SQL (postgresql):
|
|
@@ -112,10 +122,30 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
112
122
|
* select *
|
|
113
123
|
* from "person"
|
|
114
124
|
* where (
|
|
115
|
-
* select "pet"."
|
|
125
|
+
* select "pet"."name"
|
|
116
126
|
* from "pet"
|
|
117
127
|
* where "pet"."owner_id" = "person"."id"
|
|
118
|
-
*
|
|
128
|
+
* limit $1
|
|
129
|
+
* ) = $2
|
|
130
|
+
* ```
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* A `where in` query can be built by using the `in` operator and an array
|
|
134
|
+
* of values. The values in the array can also be subqueries or raw
|
|
135
|
+
* instances.
|
|
136
|
+
*
|
|
137
|
+
* ```ts
|
|
138
|
+
* const persons = await db
|
|
139
|
+
* .selectFrom('person')
|
|
140
|
+
* .selectAll()
|
|
141
|
+
* .where('person.id', 'in', [100, 200, 300])
|
|
142
|
+
* .execute()
|
|
143
|
+
* ```
|
|
144
|
+
*
|
|
145
|
+
* The generated SQL (postgresql):
|
|
146
|
+
*
|
|
147
|
+
* ```sql
|
|
148
|
+
* select * from "person" where "id" in ($1, $2, $3)
|
|
119
149
|
* ```
|
|
120
150
|
*
|
|
121
151
|
* @example
|
|
@@ -123,20 +153,21 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
123
153
|
* as any of the arguments, including the operator:
|
|
124
154
|
*
|
|
125
155
|
* ```ts
|
|
126
|
-
* db
|
|
156
|
+
* const persons = await db
|
|
157
|
+
* .selectFrom('person')
|
|
158
|
+
* .selectAll()
|
|
127
159
|
* .where(
|
|
128
160
|
* db.raw('coalesce(first_name, last_name)'),
|
|
129
161
|
* 'like',
|
|
130
162
|
* '%' + name + '%',
|
|
131
163
|
* )
|
|
132
|
-
* .
|
|
164
|
+
* .execute()
|
|
133
165
|
* ```
|
|
134
166
|
*
|
|
135
167
|
* The generated SQL (postgresql):
|
|
136
168
|
*
|
|
137
169
|
* ```sql
|
|
138
|
-
* select *
|
|
139
|
-
* from "person"
|
|
170
|
+
* select * from "person"
|
|
140
171
|
* where coalesce(first_name, last_name) like $1
|
|
141
172
|
* ```
|
|
142
173
|
*
|
|
@@ -145,12 +176,14 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
145
176
|
* used to create parentheses around other where clauses:
|
|
146
177
|
*
|
|
147
178
|
* ```ts
|
|
148
|
-
* db
|
|
179
|
+
* const persons = await db
|
|
180
|
+
* .selectFrom('person')
|
|
149
181
|
* .selectAll()
|
|
150
182
|
* .where((qb) => qb
|
|
151
183
|
* .where('id', '=', 1)
|
|
152
184
|
* .orWhere('id', '=', 2)
|
|
153
185
|
* )
|
|
186
|
+
* .execute()
|
|
154
187
|
* ```
|
|
155
188
|
*
|
|
156
189
|
* The generated SQL (postgresql):
|
|
@@ -174,10 +207,12 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
174
207
|
* ```ts
|
|
175
208
|
* const { ref } = db.dynamic
|
|
176
209
|
*
|
|
177
|
-
* db
|
|
210
|
+
* const persons = await db
|
|
211
|
+
* .selectFrom('person')
|
|
178
212
|
* .selectAll()
|
|
179
213
|
* .where(ref(columnFromUserInput), '=', 1)
|
|
180
214
|
* .orWhere(db.raw('??', [columnFromUserInput]), '=', 2)
|
|
215
|
+
* .execute()
|
|
181
216
|
* ```
|
|
182
217
|
*/
|
|
183
218
|
where<RE extends ReferenceExpression<DB, TB>>(lhs: RE, op: FilterOperator, rhs: ValueExpressionOrList<DB, TB, RE>): QueryBuilder<DB, TB, O>;
|
|
@@ -209,7 +244,8 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
209
244
|
* Usage in a subquery:
|
|
210
245
|
*
|
|
211
246
|
* ```ts
|
|
212
|
-
* db
|
|
247
|
+
* const persons = await db
|
|
248
|
+
* .selectFrom('person')
|
|
213
249
|
* .selectAll('person')
|
|
214
250
|
* .select((qb) => qb
|
|
215
251
|
* .subQuery('pet')
|
|
@@ -218,6 +254,7 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
218
254
|
* .limit(1)
|
|
219
255
|
* .as('pet_name')
|
|
220
256
|
* )
|
|
257
|
+
* .execute()
|
|
221
258
|
* ```
|
|
222
259
|
*
|
|
223
260
|
* The generated SQL (postgresql):
|
|
@@ -227,8 +264,9 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
227
264
|
* select "name"
|
|
228
265
|
* from "pet"
|
|
229
266
|
* where "pet"."owner_id" = "person"."id"
|
|
230
|
-
*
|
|
231
|
-
*
|
|
267
|
+
* limit $1
|
|
268
|
+
* ) as "pet_name"
|
|
269
|
+
* from "person"
|
|
232
270
|
*/
|
|
233
271
|
whereRef(lhs: ReferenceExpression<DB, TB>, op: FilterOperator, rhs: ReferenceExpression<DB, TB>): QueryBuilder<DB, TB, O>;
|
|
234
272
|
/**
|
|
@@ -241,10 +279,12 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
241
279
|
*
|
|
242
280
|
* @example
|
|
243
281
|
* ```ts
|
|
244
|
-
* db
|
|
282
|
+
* const persons = await db
|
|
283
|
+
* .selectFrom('person')
|
|
245
284
|
* .selectAll()
|
|
246
285
|
* .where('id', '=', 1)
|
|
247
286
|
* .orWhere('id', '=', 2)
|
|
287
|
+
* .execute()
|
|
248
288
|
* ```
|
|
249
289
|
*
|
|
250
290
|
* The generated SQL (postgresql):
|
|
@@ -257,12 +297,14 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
257
297
|
* Grouping with parentheses:
|
|
258
298
|
*
|
|
259
299
|
* ```ts
|
|
260
|
-
* db
|
|
300
|
+
* const persons = await db
|
|
301
|
+
* .selectFrom('person')
|
|
261
302
|
* .selectAll()
|
|
262
303
|
* .where((qb) => qb
|
|
263
304
|
* .where('id', '=', 1)
|
|
264
305
|
* .orWhere('id', '=', 2)
|
|
265
306
|
* )
|
|
307
|
+
* .execute()
|
|
266
308
|
* ```
|
|
267
309
|
*
|
|
268
310
|
* The generated SQL (postgresql):
|
|
@@ -276,12 +318,14 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
276
318
|
* if you are looping through a set of conditions:
|
|
277
319
|
*
|
|
278
320
|
* ```ts
|
|
279
|
-
* db
|
|
321
|
+
* const persons = await db
|
|
322
|
+
* .selectFrom('person')
|
|
280
323
|
* .selectAll()
|
|
281
324
|
* .where((qb) => qb
|
|
282
325
|
* .orWhere('id', '=', 1)
|
|
283
326
|
* .orWhere('id', '=', 2)
|
|
284
327
|
* )
|
|
328
|
+
* .execute()
|
|
285
329
|
* ```
|
|
286
330
|
*
|
|
287
331
|
* The generated SQL (postgresql):
|
|
@@ -308,7 +352,8 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
308
352
|
* The query below selets all persons that own a pet named Catto:
|
|
309
353
|
*
|
|
310
354
|
* ```ts
|
|
311
|
-
* db
|
|
355
|
+
* const persons = await db
|
|
356
|
+
* .selectFrom('person')
|
|
312
357
|
* .selectAll()
|
|
313
358
|
* .whereExists((qb) => qb
|
|
314
359
|
* .subQuery('pet')
|
|
@@ -316,6 +361,7 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
316
361
|
* .whereRef('person.id', '=', 'pet.owner_id')
|
|
317
362
|
* .where('pet.name', '=', 'Catto')
|
|
318
363
|
* )
|
|
364
|
+
* .execute()
|
|
319
365
|
* ```
|
|
320
366
|
*
|
|
321
367
|
* The generated SQL (postgresql):
|
|
@@ -414,14 +460,14 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
414
460
|
/**
|
|
415
461
|
* Adds a select clause to the query.
|
|
416
462
|
*
|
|
417
|
-
* When a column (or any expression) is selected, Kysely
|
|
418
|
-
* type of the query. Kysely is smart enough to parse the
|
|
463
|
+
* When a column (or any expression) is selected, Kysely adds its type to the return
|
|
464
|
+
* type of the query. Kysely is smart enough to parse the column names and types even
|
|
419
465
|
* from aliased columns, subqueries, raw expressions etc.
|
|
420
466
|
*
|
|
421
467
|
* Kysely only allows you to select columns and expressions that exist and would
|
|
422
468
|
* produce valid SQL. However, Kysely is not perfect and there may be cases where
|
|
423
469
|
* the type inference doesn't work and you need to override it. You can always
|
|
424
|
-
* use the {@link Kysely.dynamic | dynamic}
|
|
470
|
+
* use the {@link Kysely.dynamic | dynamic} module and {@link Kysely.raw | raw}
|
|
425
471
|
* to override the types.
|
|
426
472
|
*
|
|
427
473
|
* Select calls are additive. Calling `select('id').select('first_name')` is the
|
|
@@ -434,28 +480,29 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
434
480
|
* Select a single column:
|
|
435
481
|
*
|
|
436
482
|
* ```ts
|
|
437
|
-
* const
|
|
483
|
+
* const persons = await db.selectFrom('person')
|
|
438
484
|
* .select('id')
|
|
485
|
+
* .where('first_name', '=', 'Arnold')
|
|
439
486
|
* .execute()
|
|
440
487
|
*
|
|
441
|
-
*
|
|
488
|
+
* persons[0].id
|
|
442
489
|
* ```
|
|
443
490
|
*
|
|
444
491
|
* The generated SQL (postgresql):
|
|
445
492
|
*
|
|
446
493
|
* ```sql
|
|
447
|
-
* select "id" from "person"
|
|
494
|
+
* select "id" from "person" where "first_name" = $1
|
|
448
495
|
* ```
|
|
449
496
|
*
|
|
450
497
|
* @example
|
|
451
498
|
* Select a single column and specify a table:
|
|
452
499
|
*
|
|
453
500
|
* ```ts
|
|
454
|
-
* const
|
|
501
|
+
* const persons = await db.selectFrom(['person', 'pet'])
|
|
455
502
|
* .select('person.id')
|
|
456
503
|
* .execute()
|
|
457
504
|
*
|
|
458
|
-
*
|
|
505
|
+
* persons[0].id
|
|
459
506
|
* ```
|
|
460
507
|
*
|
|
461
508
|
* The generated SQL (postgresql):
|
|
@@ -468,12 +515,12 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
468
515
|
* Select multiple columns:
|
|
469
516
|
*
|
|
470
517
|
* ```ts
|
|
471
|
-
* const
|
|
518
|
+
* const persons = await db.selectFrom('person')
|
|
472
519
|
* .select(['person.id', 'first_name'])
|
|
473
520
|
* .execute()
|
|
474
521
|
*
|
|
475
|
-
*
|
|
476
|
-
*
|
|
522
|
+
* persons[0].id
|
|
523
|
+
* persons[0].first_name
|
|
477
524
|
* ```
|
|
478
525
|
*
|
|
479
526
|
* The generated SQL (postgresql):
|
|
@@ -483,18 +530,18 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
483
530
|
* ```
|
|
484
531
|
*
|
|
485
532
|
* @example
|
|
486
|
-
*
|
|
533
|
+
* Aliased selections:
|
|
487
534
|
*
|
|
488
535
|
* ```ts
|
|
489
|
-
* const
|
|
536
|
+
* const persons = await db.selectFrom('person')
|
|
490
537
|
* .select([
|
|
491
538
|
* 'person.first_name as fn',
|
|
492
539
|
* 'person.last_name as ln'
|
|
493
540
|
* ])
|
|
494
541
|
* .execute()
|
|
495
542
|
*
|
|
496
|
-
*
|
|
497
|
-
*
|
|
543
|
+
* persons[0].fn
|
|
544
|
+
* persons[0].ln
|
|
498
545
|
* ```
|
|
499
546
|
*
|
|
500
547
|
* The generated SQL (postgresql):
|
|
@@ -512,19 +559,20 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
512
559
|
* method:
|
|
513
560
|
*
|
|
514
561
|
* ```ts
|
|
515
|
-
* const
|
|
562
|
+
* const persons = await db.selectFrom('person')
|
|
516
563
|
* .select([
|
|
517
564
|
* (qb) => qb
|
|
518
565
|
* .subQuery('pet')
|
|
519
566
|
* .whereRef('person.id', '=', 'pet.owner_id')
|
|
520
567
|
* .select('pet.name')
|
|
568
|
+
* .limit(1)
|
|
521
569
|
* .as('pet_name')
|
|
522
570
|
* db.raw<string>("concat(first_name, ' ', last_name)").as('full_name')
|
|
523
571
|
* ])
|
|
524
572
|
* .execute()
|
|
525
573
|
*
|
|
526
|
-
*
|
|
527
|
-
*
|
|
574
|
+
* persons[0].pet_name
|
|
575
|
+
* persons[0].full_name
|
|
528
576
|
* ```
|
|
529
577
|
*
|
|
530
578
|
* The generated SQL (postgresql):
|
|
@@ -535,7 +583,8 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
535
583
|
* select "pet"."name"
|
|
536
584
|
* from "pet"
|
|
537
585
|
* where "person"."id" = "pet"."owner_id"
|
|
538
|
-
*
|
|
586
|
+
* limit $1
|
|
587
|
+
* ) as "pet_name",
|
|
539
588
|
* concat(first_name, ' ', last_name) as full_name
|
|
540
589
|
* from "person"
|
|
541
590
|
* ```
|
|
@@ -549,7 +598,7 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
549
598
|
* the types) sometimes it's not possible or you just prefer to write more
|
|
550
599
|
* dynamic code.
|
|
551
600
|
* <br><br>
|
|
552
|
-
* In this example, we use the `dynamic`
|
|
601
|
+
* In this example, we use the `dynamic` module's methods to add selections
|
|
553
602
|
* dynamically:
|
|
554
603
|
*
|
|
555
604
|
* ```ts
|
|
@@ -562,7 +611,7 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
562
611
|
* // You can use `keyof Person` if any column of an interface is allowed.
|
|
563
612
|
* type PossibleColumns = 'last_name' | 'first_name' | 'birth_date'
|
|
564
613
|
*
|
|
565
|
-
* const
|
|
614
|
+
* const spersons = await db.selectFrom('person')
|
|
566
615
|
* .select([
|
|
567
616
|
* ref<PossibleColumns>(columnFromUserInput)
|
|
568
617
|
* 'id'
|
|
@@ -572,12 +621,12 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
572
621
|
* // The resulting type contains all `PossibleColumns` as optional fields
|
|
573
622
|
* // because we cannot know which field was actually selected before
|
|
574
623
|
* // running the code.
|
|
575
|
-
* const lastName: string | undefined =
|
|
576
|
-
* const firstName: string | undefined =
|
|
577
|
-
* const birthDate: string | undefined =
|
|
624
|
+
* const lastName: string | undefined = persons[0].last_name
|
|
625
|
+
* const firstName: string | undefined = persons[0].first_name
|
|
626
|
+
* const birthDate: string | undefined = persons[0].birth_date
|
|
578
627
|
*
|
|
579
628
|
* // The result type also contains the compile time selection `id`.
|
|
580
|
-
*
|
|
629
|
+
* persons[0].id
|
|
581
630
|
* ```
|
|
582
631
|
*/
|
|
583
632
|
select<S extends SelectExpression<DB, TB>>(selections: ReadonlyArray<S>): QueryBuilderWithSelection<DB, TB, O, S>;
|
|
@@ -585,6 +634,10 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
585
634
|
/**
|
|
586
635
|
* Adds `distinct on` selections to the select clause.
|
|
587
636
|
*
|
|
637
|
+
* Takes the same inputs as the {@link QueryBuilder.select | select} method.
|
|
638
|
+
* See the {@link QueryBuilder.select | select} method's documentation for
|
|
639
|
+
* more examples.
|
|
640
|
+
*
|
|
588
641
|
* @example
|
|
589
642
|
* ```ts
|
|
590
643
|
* await db.selectFrom('person')
|
|
@@ -653,7 +706,8 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
653
706
|
*
|
|
654
707
|
* @example
|
|
655
708
|
* ```ts
|
|
656
|
-
* await db
|
|
709
|
+
* const persons = await db
|
|
710
|
+
* .selectFrom('person')
|
|
657
711
|
* .selectAll()
|
|
658
712
|
* .execute()
|
|
659
713
|
* ```
|
|
@@ -666,7 +720,8 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
666
720
|
*
|
|
667
721
|
* @example
|
|
668
722
|
* ```ts
|
|
669
|
-
* await db
|
|
723
|
+
* const persons = await db
|
|
724
|
+
* .selectFrom('person')
|
|
670
725
|
* .selectAll('person')
|
|
671
726
|
* .execute()
|
|
672
727
|
* ```
|
|
@@ -679,7 +734,8 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
679
734
|
*
|
|
680
735
|
* @example
|
|
681
736
|
* ```ts
|
|
682
|
-
* await db
|
|
737
|
+
* const personsPets = await db
|
|
738
|
+
* .selectFrom(['person', 'pet'])
|
|
683
739
|
* .selectAll(['person', 'pet'])
|
|
684
740
|
* .execute()
|
|
685
741
|
* ```
|
|
@@ -700,10 +756,17 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
700
756
|
* Simple usage by providing a table name and two columns to join:
|
|
701
757
|
*
|
|
702
758
|
* ```ts
|
|
703
|
-
* await db
|
|
759
|
+
* const result = await db
|
|
760
|
+
* .selectFrom('person')
|
|
704
761
|
* .selectAll()
|
|
705
762
|
* .innerJoin('pet', 'pet.owner_id', 'person.id')
|
|
706
763
|
* .execute()
|
|
764
|
+
*
|
|
765
|
+
* // The result has all `Person` columns
|
|
766
|
+
* result[0].first_name
|
|
767
|
+
*
|
|
768
|
+
* // The result has all `Pet` columns
|
|
769
|
+
* result[0].species
|
|
707
770
|
* ```
|
|
708
771
|
*
|
|
709
772
|
* The generated SQL (postgresql):
|
|
@@ -794,7 +857,7 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
794
857
|
* select "owner_id", "name"
|
|
795
858
|
* from "pet"
|
|
796
859
|
* where "name" = $1
|
|
797
|
-
* ) as doggos
|
|
860
|
+
* ) as "doggos"
|
|
798
861
|
* on "doggos"."owner_id" = "person"."id"
|
|
799
862
|
* ```
|
|
800
863
|
*/
|
|
@@ -819,20 +882,24 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
819
882
|
fullJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): QueryBuilderWithTable<DB, TB, O, TE>;
|
|
820
883
|
fullJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): QueryBuilderWithTable<DB, TB, O, TE>;
|
|
821
884
|
/**
|
|
822
|
-
* Sets the values to insert for an
|
|
885
|
+
* Sets the values to insert for an {@link Kysely.insertInto | insert} query.
|
|
823
886
|
*
|
|
824
887
|
* This method takes an object whose keys are column names and values are
|
|
825
|
-
* values to insert. In addition to the column's type, the values can be
|
|
826
|
-
* instances, select queries or the
|
|
888
|
+
* values to insert. In addition to the column's type, the values can be
|
|
889
|
+
* {@link Kysely.raw | raw} instances, select queries or the
|
|
890
|
+
* {@link Kysely.generated} placeholder.
|
|
827
891
|
*
|
|
828
|
-
* You must provide all values defined by the interface
|
|
892
|
+
* You must provide all values defined by the interface for the table you
|
|
829
893
|
* are inserting into. Values that are generated by the database like autoincrementing
|
|
830
|
-
* identifiers can be marked with the {@link Kysely.generated} placeholder
|
|
894
|
+
* identifiers can be marked with the {@link Kysely.generated} placeholder unless you
|
|
895
|
+
* want to insert a specific value instead of the generated default.
|
|
831
896
|
*
|
|
832
897
|
* The return value is the primary key of the inserted row BUT on some databases
|
|
833
898
|
* there is no return value by default. That's the reason for the `number | undefined`
|
|
834
899
|
* type of the return value. On postgres you need to additionally call `returning` to get
|
|
835
|
-
* something out of the query.
|
|
900
|
+
* something out of the query. If you know your database engine always returns the primary
|
|
901
|
+
* key, you can use the {@link QueryBuilder.executeTakeFirstOrThrow | executeTakeFirstOrThrow}
|
|
902
|
+
* method to execute the query.
|
|
836
903
|
*
|
|
837
904
|
* @example
|
|
838
905
|
* Insert a row into `person`:
|
|
@@ -844,7 +911,7 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
844
911
|
* first_name: 'Jennifer',
|
|
845
912
|
* last_name: 'Aniston'
|
|
846
913
|
* })
|
|
847
|
-
* .
|
|
914
|
+
* .executeTakeFirstOrThrow()
|
|
848
915
|
* ```
|
|
849
916
|
*
|
|
850
917
|
* The generated SQL (postgresql):
|
|
@@ -894,9 +961,9 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
894
961
|
* last_name: 'Aniston'
|
|
895
962
|
* })
|
|
896
963
|
* .returning('id')
|
|
897
|
-
* .
|
|
964
|
+
* .executeTakeFirstOrThrow()
|
|
898
965
|
*
|
|
899
|
-
* row
|
|
966
|
+
* row.id
|
|
900
967
|
* ```
|
|
901
968
|
*
|
|
902
969
|
* The generated SQL (postgresql):
|
|
@@ -1016,7 +1083,7 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
1016
1083
|
onConflictUpdate(column: AnyColumn<DB, TB>, updates: MutationObject<DB, TB>): QueryBuilder<DB, TB, O>;
|
|
1017
1084
|
onConflictUpdate(columns: ReadonlyArray<AnyColumn<DB, TB>>, updates: MutationObject<DB, TB>): QueryBuilder<DB, TB, O>;
|
|
1018
1085
|
/**
|
|
1019
|
-
* Sets the values to update for an
|
|
1086
|
+
* Sets the values to update for an {@link Kysely.updateTable | update} query.
|
|
1020
1087
|
*
|
|
1021
1088
|
* This method takes an object whose keys are column names and values are
|
|
1022
1089
|
* values to update. In addition to the column's type, the values can be `raw`
|
|
@@ -1047,7 +1114,7 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
1047
1114
|
* ```
|
|
1048
1115
|
*
|
|
1049
1116
|
* @example
|
|
1050
|
-
* On postgresql you
|
|
1117
|
+
* On postgresql you ca chain `returning` to the query to get
|
|
1051
1118
|
* the updated rows' columns (or any other expression) as the
|
|
1052
1119
|
* return value:
|
|
1053
1120
|
*
|
|
@@ -1060,7 +1127,7 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
1060
1127
|
* })
|
|
1061
1128
|
* .where('id', '=', 1)
|
|
1062
1129
|
* .returning('id')
|
|
1063
|
-
* .
|
|
1130
|
+
* .executeTakeFirstOrThrow()
|
|
1064
1131
|
*
|
|
1065
1132
|
* row.id
|
|
1066
1133
|
* ```
|