kysely 0.16.4 → 0.16.8
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 +118 -5
- package/dist/cjs/dialect/mysql/mysql-dialect.js +2 -1
- package/dist/cjs/dialect/mysql/mysql-driver.js +1 -1
- package/dist/cjs/dialect/postgres/postgres-dialect.js +2 -1
- package/dist/cjs/dialect/postgres/postgres-driver.js +1 -1
- package/dist/cjs/dialect/sqlite/sqlite-dialect.js +2 -1
- package/dist/cjs/dialect/sqlite/sqlite-driver.js +1 -1
- package/dist/cjs/driver/dummy-driver.d.ts +68 -0
- package/dist/cjs/driver/dummy-driver.js +62 -0
- package/dist/cjs/driver/runtime-driver.d.ts +1 -1
- package/dist/cjs/driver/runtime-driver.js +30 -33
- package/dist/cjs/index-nodeless.d.ts +2 -0
- package/dist/cjs/index-nodeless.js +2 -0
- package/dist/cjs/kysely.d.ts +8 -6
- package/dist/cjs/kysely.js +8 -6
- package/dist/cjs/migration/migrator.d.ts +2 -2
- package/dist/cjs/operation-node/primitive-value-list-node.d.ts +2 -3
- package/dist/cjs/operation-node/value-node.d.ts +3 -4
- package/dist/cjs/parser/complex-expression-parser.d.ts +1 -0
- package/dist/cjs/parser/complex-expression-parser.js +5 -1
- package/dist/cjs/parser/default-value-parser.d.ts +2 -3
- package/dist/cjs/parser/default-value-parser.js +2 -2
- package/dist/cjs/parser/insert-values-parser.js +4 -3
- package/dist/cjs/parser/reference-parser.d.ts +2 -3
- package/dist/cjs/parser/value-parser.d.ts +2 -3
- package/dist/cjs/parser/value-parser.js +6 -7
- package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-plugin.d.ts +48 -0
- package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-plugin.js +19 -0
- package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-transformer.d.ts +9 -0
- package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-transformer.js +42 -0
- package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.d.ts +0 -0
- package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.js +0 -0
- package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.d.ts +0 -0
- package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.js +0 -0
- package/dist/cjs/query-builder/delete-query-builder.d.ts +51 -9
- package/dist/cjs/query-builder/delete-query-builder.js +56 -8
- package/dist/cjs/query-builder/expression-builder.d.ts +1 -1
- package/dist/cjs/query-builder/expression-builder.js +2 -2
- package/dist/cjs/query-builder/function-builder.d.ts +29 -5
- package/dist/cjs/query-builder/function-builder.js +31 -7
- package/dist/cjs/query-builder/insert-query-builder.d.ts +53 -11
- package/dist/cjs/query-builder/insert-query-builder.js +58 -10
- package/dist/cjs/query-builder/select-query-builder.d.ts +58 -16
- package/dist/cjs/query-builder/select-query-builder.js +64 -15
- package/dist/cjs/query-builder/update-query-builder.d.ts +52 -9
- package/dist/cjs/query-builder/update-query-builder.js +57 -8
- package/dist/cjs/query-compiler/default-query-compiler.d.ts +1 -2
- package/dist/cjs/query-creator.d.ts +2 -2
- package/dist/cjs/query-creator.js +2 -2
- package/dist/cjs/query-executor/query-executor.js +3 -2
- package/dist/cjs/raw-builder/raw-builder.d.ts +2 -2
- package/dist/cjs/raw-builder/raw-builder.js +8 -1
- package/dist/cjs/schema/alter-table-builder.d.ts +30 -4
- package/dist/cjs/schema/alter-table-builder.js +2 -5
- package/dist/cjs/schema/column-definition-builder.d.ts +27 -2
- package/dist/cjs/schema/create-view-builder.js +1 -1
- package/dist/cjs/util/log.d.ts +1 -0
- package/dist/cjs/util/log.js +3 -0
- package/dist/cjs/util/object-utils.d.ts +5 -5
- package/dist/cjs/util/object-utils.js +52 -11
- package/dist/cjs/util/type-utils.d.ts +21 -0
- package/dist/esm/dialect/mysql/mysql-dialect.js +2 -1
- package/dist/esm/dialect/mysql/mysql-driver.js +2 -2
- package/dist/esm/dialect/postgres/postgres-dialect.js +2 -1
- package/dist/esm/dialect/postgres/postgres-driver.js +2 -2
- package/dist/esm/dialect/sqlite/sqlite-dialect.js +2 -1
- package/dist/esm/dialect/sqlite/sqlite-driver.js +2 -2
- package/dist/esm/driver/dummy-driver.d.ts +68 -0
- package/dist/esm/driver/dummy-driver.js +59 -0
- package/dist/esm/driver/runtime-driver.d.ts +1 -1
- package/dist/esm/driver/runtime-driver.js +29 -32
- package/dist/esm/index-nodeless.d.ts +2 -0
- package/dist/esm/index-nodeless.js +2 -0
- package/dist/esm/kysely.d.ts +8 -6
- package/dist/esm/kysely.js +8 -6
- package/dist/esm/migration/migrator.d.ts +2 -2
- package/dist/esm/operation-node/primitive-value-list-node.d.ts +2 -3
- package/dist/esm/operation-node/value-node.d.ts +3 -4
- package/dist/esm/parser/complex-expression-parser.d.ts +1 -0
- package/dist/esm/parser/complex-expression-parser.js +3 -0
- package/dist/esm/parser/default-value-parser.d.ts +2 -3
- package/dist/esm/parser/default-value-parser.js +2 -2
- package/dist/esm/parser/insert-values-parser.js +5 -4
- package/dist/esm/parser/reference-parser.d.ts +2 -3
- package/dist/esm/parser/reference-parser.js +1 -1
- package/dist/esm/parser/value-parser.d.ts +2 -3
- package/dist/esm/parser/value-parser.js +8 -9
- package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-plugin.d.ts +48 -0
- package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-plugin.js +16 -0
- package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-transformer.d.ts +9 -0
- package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-transformer.js +39 -0
- package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.d.ts +0 -0
- package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.js +0 -0
- package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.d.ts +0 -0
- package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.js +0 -0
- package/dist/esm/query-builder/delete-query-builder.d.ts +51 -9
- package/dist/esm/query-builder/delete-query-builder.js +56 -8
- package/dist/esm/query-builder/expression-builder.d.ts +1 -1
- package/dist/esm/query-builder/expression-builder.js +1 -1
- package/dist/esm/query-builder/function-builder.d.ts +29 -5
- package/dist/esm/query-builder/function-builder.js +31 -7
- package/dist/esm/query-builder/insert-query-builder.d.ts +53 -11
- package/dist/esm/query-builder/insert-query-builder.js +58 -10
- package/dist/esm/query-builder/select-query-builder.d.ts +58 -16
- package/dist/esm/query-builder/select-query-builder.js +64 -15
- package/dist/esm/query-builder/update-query-builder.d.ts +52 -9
- package/dist/esm/query-builder/update-query-builder.js +57 -8
- package/dist/esm/query-compiler/default-query-compiler.d.ts +1 -2
- package/dist/esm/query-creator.d.ts +2 -2
- package/dist/esm/query-creator.js +2 -2
- package/dist/esm/query-executor/query-executor.js +3 -2
- package/dist/esm/raw-builder/raw-builder.d.ts +2 -2
- package/dist/esm/raw-builder/raw-builder.js +8 -1
- package/dist/esm/schema/alter-table-builder.d.ts +30 -4
- package/dist/esm/schema/alter-table-builder.js +2 -5
- package/dist/esm/schema/column-definition-builder.d.ts +27 -2
- package/dist/esm/schema/create-view-builder.js +1 -1
- package/dist/esm/util/log.d.ts +1 -0
- package/dist/esm/util/log.js +3 -0
- package/dist/esm/util/object-utils.d.ts +5 -5
- package/dist/esm/util/object-utils.js +50 -9
- package/dist/esm/util/type-utils.d.ts +21 -0
- package/package.json +11 -5
|
@@ -3,12 +3,13 @@ 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 { SingleResultType } from '../util/type-utils.js';
|
|
6
|
+
import { MergePartial, SingleResultType } from '../util/type-utils.js';
|
|
7
7
|
import { MutationObject } from '../parser/update-set-parser.js';
|
|
8
8
|
import { Compilable } from '../util/compilable.js';
|
|
9
9
|
import { QueryExecutor } from '../query-executor/query-executor.js';
|
|
10
10
|
import { QueryId } from '../util/query-id.js';
|
|
11
11
|
import { ParseContext } from '../parser/parse-context.js';
|
|
12
|
+
import { InsertResult } from './insert-result.js';
|
|
12
13
|
import { KyselyPlugin } from '../plugin/kysely-plugin.js';
|
|
13
14
|
import { ReturningRow } from '../parser/returning-parser.js';
|
|
14
15
|
import { NoResultErrorConstructor } from './no-result-error.js';
|
|
@@ -448,24 +449,62 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
448
449
|
/**
|
|
449
450
|
* Simply calls the given function passing `this` as the only argument.
|
|
450
451
|
*
|
|
451
|
-
*
|
|
452
|
+
* If you want to conditionally call a method on `this`, see
|
|
453
|
+
* the {@link if} method.
|
|
452
454
|
*
|
|
453
455
|
* ### Examples
|
|
454
456
|
*
|
|
457
|
+
* The next example uses a helper funtion `log` to log a query:
|
|
458
|
+
*
|
|
455
459
|
* ```ts
|
|
456
|
-
*
|
|
457
|
-
* .
|
|
458
|
-
*
|
|
459
|
-
*
|
|
460
|
-
*
|
|
461
|
-
*
|
|
462
|
-
*
|
|
463
|
-
*
|
|
464
|
-
* })
|
|
460
|
+
* function log<T extends Compilable>(qb: T): T {
|
|
461
|
+
* console.log(qb.compile())
|
|
462
|
+
* return qb
|
|
463
|
+
* }
|
|
464
|
+
*
|
|
465
|
+
* db.updateTable('person')
|
|
466
|
+
* .set(values)
|
|
467
|
+
* .call(log)
|
|
465
468
|
* .execute()
|
|
466
469
|
* ```
|
|
467
470
|
*/
|
|
468
471
|
call<T>(func: (qb: this) => T): T;
|
|
472
|
+
/**
|
|
473
|
+
* Call `func(this)` if `condition` is true.
|
|
474
|
+
*
|
|
475
|
+
* This method is especially handy with optional selects. Any `returning` or `returningAll`
|
|
476
|
+
* method calls add columns as optional fields to the output type when called inside
|
|
477
|
+
* the `func` callback. This is because we can't know if those selections were actually
|
|
478
|
+
* made before running the code.
|
|
479
|
+
*
|
|
480
|
+
* You can also call any other methods inside the callback.
|
|
481
|
+
*
|
|
482
|
+
* ### Examples
|
|
483
|
+
*
|
|
484
|
+
* ```ts
|
|
485
|
+
* async function insertPerson(values: InsertablePerson, returnLastName: boolean) {
|
|
486
|
+
* return await db
|
|
487
|
+
* .insertInto('person')
|
|
488
|
+
* .values(values)
|
|
489
|
+
* .returning(['id', 'first_name'])
|
|
490
|
+
* .if(returnLastName, (qb) => qb.returning('last_name'))
|
|
491
|
+
* .executeTakeFirstOrThrow()
|
|
492
|
+
* }
|
|
493
|
+
* ```
|
|
494
|
+
*
|
|
495
|
+
* Any selections added inside the `if` callback will be added as optional fields to the
|
|
496
|
+
* output type since we can't know if the selections were actually made before running
|
|
497
|
+
* the code. In the example above the return type of the `insertPerson` function is:
|
|
498
|
+
*
|
|
499
|
+
* ```ts
|
|
500
|
+
* {
|
|
501
|
+
* id: number
|
|
502
|
+
* first_name: string
|
|
503
|
+
* last_name?: string
|
|
504
|
+
* }
|
|
505
|
+
* ```
|
|
506
|
+
*/
|
|
507
|
+
if<O2>(condition: boolean, func: (qb: this) => InsertQueryBuilder<DB, TB, O2>): InsertQueryBuilder<DB, TB, O2 extends InsertResult ? InsertResult : O extends InsertResult ? Partial<O2> : MergePartial<O, O2>>;
|
|
469
508
|
/**
|
|
470
509
|
* Change the output type of the query.
|
|
471
510
|
*
|
|
@@ -473,6 +512,9 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
473
512
|
* don't support your use case.
|
|
474
513
|
*/
|
|
475
514
|
castTo<T>(): InsertQueryBuilder<DB, TB, T>;
|
|
515
|
+
/**
|
|
516
|
+
* Returns a copy of this InsertQueryBuilder instance with the given plugin installed.
|
|
517
|
+
*/
|
|
476
518
|
withPlugin(plugin: KyselyPlugin): InsertQueryBuilder<DB, TB, O>;
|
|
477
519
|
toOperationNode(): InsertQueryNode;
|
|
478
520
|
compile(): CompiledQuery;
|
|
@@ -317,26 +317,71 @@ class InsertQueryBuilder {
|
|
|
317
317
|
/**
|
|
318
318
|
* Simply calls the given function passing `this` as the only argument.
|
|
319
319
|
*
|
|
320
|
-
*
|
|
320
|
+
* If you want to conditionally call a method on `this`, see
|
|
321
|
+
* the {@link if} method.
|
|
321
322
|
*
|
|
322
323
|
* ### Examples
|
|
323
324
|
*
|
|
325
|
+
* The next example uses a helper funtion `log` to log a query:
|
|
326
|
+
*
|
|
324
327
|
* ```ts
|
|
325
|
-
*
|
|
326
|
-
* .
|
|
327
|
-
*
|
|
328
|
-
*
|
|
329
|
-
*
|
|
330
|
-
*
|
|
331
|
-
*
|
|
332
|
-
*
|
|
333
|
-
* })
|
|
328
|
+
* function log<T extends Compilable>(qb: T): T {
|
|
329
|
+
* console.log(qb.compile())
|
|
330
|
+
* return qb
|
|
331
|
+
* }
|
|
332
|
+
*
|
|
333
|
+
* db.updateTable('person')
|
|
334
|
+
* .set(values)
|
|
335
|
+
* .call(log)
|
|
334
336
|
* .execute()
|
|
335
337
|
* ```
|
|
336
338
|
*/
|
|
337
339
|
call(func) {
|
|
338
340
|
return func(this);
|
|
339
341
|
}
|
|
342
|
+
/**
|
|
343
|
+
* Call `func(this)` if `condition` is true.
|
|
344
|
+
*
|
|
345
|
+
* This method is especially handy with optional selects. Any `returning` or `returningAll`
|
|
346
|
+
* method calls add columns as optional fields to the output type when called inside
|
|
347
|
+
* the `func` callback. This is because we can't know if those selections were actually
|
|
348
|
+
* made before running the code.
|
|
349
|
+
*
|
|
350
|
+
* You can also call any other methods inside the callback.
|
|
351
|
+
*
|
|
352
|
+
* ### Examples
|
|
353
|
+
*
|
|
354
|
+
* ```ts
|
|
355
|
+
* async function insertPerson(values: InsertablePerson, returnLastName: boolean) {
|
|
356
|
+
* return await db
|
|
357
|
+
* .insertInto('person')
|
|
358
|
+
* .values(values)
|
|
359
|
+
* .returning(['id', 'first_name'])
|
|
360
|
+
* .if(returnLastName, (qb) => qb.returning('last_name'))
|
|
361
|
+
* .executeTakeFirstOrThrow()
|
|
362
|
+
* }
|
|
363
|
+
* ```
|
|
364
|
+
*
|
|
365
|
+
* Any selections added inside the `if` callback will be added as optional fields to the
|
|
366
|
+
* output type since we can't know if the selections were actually made before running
|
|
367
|
+
* the code. In the example above the return type of the `insertPerson` function is:
|
|
368
|
+
*
|
|
369
|
+
* ```ts
|
|
370
|
+
* {
|
|
371
|
+
* id: number
|
|
372
|
+
* first_name: string
|
|
373
|
+
* last_name?: string
|
|
374
|
+
* }
|
|
375
|
+
* ```
|
|
376
|
+
*/
|
|
377
|
+
if(condition, func) {
|
|
378
|
+
if (condition) {
|
|
379
|
+
return func(this);
|
|
380
|
+
}
|
|
381
|
+
return new InsertQueryBuilder({
|
|
382
|
+
...this.#props,
|
|
383
|
+
});
|
|
384
|
+
}
|
|
340
385
|
/**
|
|
341
386
|
* Change the output type of the query.
|
|
342
387
|
*
|
|
@@ -346,6 +391,9 @@ class InsertQueryBuilder {
|
|
|
346
391
|
castTo() {
|
|
347
392
|
return new InsertQueryBuilder(this.#props);
|
|
348
393
|
}
|
|
394
|
+
/**
|
|
395
|
+
* Returns a copy of this InsertQueryBuilder instance with the given plugin installed.
|
|
396
|
+
*/
|
|
349
397
|
withPlugin(plugin) {
|
|
350
398
|
return new InsertQueryBuilder({
|
|
351
399
|
...this.#props,
|
|
@@ -7,7 +7,7 @@ import { SelectExpression, QueryBuilderWithSelection, SelectAllQueryBuilder } fr
|
|
|
7
7
|
import { ExistsExpression, FilterOperator, FilterValueExpressionOrList, WhereGrouper, HavingGrouper } from '../parser/filter-parser.js';
|
|
8
8
|
import { ReferenceExpression } from '../parser/reference-parser.js';
|
|
9
9
|
import { SelectQueryNode } from '../operation-node/select-query-node.js';
|
|
10
|
-
import { AnyRawBuilder, SingleResultType } from '../util/type-utils.js';
|
|
10
|
+
import { AnyRawBuilder, MergePartial, SingleResultType } from '../util/type-utils.js';
|
|
11
11
|
import { OrderByDirectionExpression, OrderByExpression } from '../parser/order-by-parser.js';
|
|
12
12
|
import { Compilable } from '../util/compilable.js';
|
|
13
13
|
import { QueryExecutor } from '../query-executor/query-executor.js';
|
|
@@ -1116,27 +1116,15 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
1116
1116
|
/**
|
|
1117
1117
|
* Simply calls the given function passing `this` as the only argument.
|
|
1118
1118
|
*
|
|
1119
|
-
*
|
|
1119
|
+
* If you want to conditionally call a method on `this`, see
|
|
1120
|
+
* the {@link if} method.
|
|
1120
1121
|
*
|
|
1121
1122
|
* ### Examples
|
|
1122
1123
|
*
|
|
1123
|
-
* ```ts
|
|
1124
|
-
* db.selectFrom('person')
|
|
1125
|
-
* .selectAll()
|
|
1126
|
-
* .call((qb) => {
|
|
1127
|
-
* if (something) {
|
|
1128
|
-
* return qb.where('something', '=', something)
|
|
1129
|
-
* } else {
|
|
1130
|
-
* return qb.where('somethingElse', '=', somethingElse)
|
|
1131
|
-
* }
|
|
1132
|
-
* })
|
|
1133
|
-
* .execute()
|
|
1134
|
-
* ```
|
|
1135
|
-
*
|
|
1136
1124
|
* The next example uses a helper funtion `log` to log a query:
|
|
1137
1125
|
*
|
|
1138
1126
|
* ```ts
|
|
1139
|
-
* function log<T extends
|
|
1127
|
+
* function log<T extends Compilable>(qb: T): T {
|
|
1140
1128
|
* console.log(qb.compile())
|
|
1141
1129
|
* return qb
|
|
1142
1130
|
* }
|
|
@@ -1148,6 +1136,57 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
1148
1136
|
* ```
|
|
1149
1137
|
*/
|
|
1150
1138
|
call<T>(func: (qb: this) => T): T;
|
|
1139
|
+
/**
|
|
1140
|
+
* Call `func(this)` if `condition` is true.
|
|
1141
|
+
*
|
|
1142
|
+
* This method is especially handy with optional selects. Any `select` or `selectAll`
|
|
1143
|
+
* method calls add columns as optional fields to the output type when called inside
|
|
1144
|
+
* the `func` callback. This is because we can't know if those selections were actually
|
|
1145
|
+
* made before running the code.
|
|
1146
|
+
*
|
|
1147
|
+
* Also see [this recipe](https://github.com/koskimas/kysely/tree/master/recipes/conditional-selects.md)
|
|
1148
|
+
*
|
|
1149
|
+
* ### Examples
|
|
1150
|
+
*
|
|
1151
|
+
* ```ts
|
|
1152
|
+
* async function getPerson(id: number, withLastName: boolean) {
|
|
1153
|
+
* return await db
|
|
1154
|
+
* .selectFrom('person')
|
|
1155
|
+
* .select(['id', 'first_name'])
|
|
1156
|
+
* .if(withLastName, (qb) => qb.select('last_name'))
|
|
1157
|
+
* .where('id', '=', id)
|
|
1158
|
+
* .executeTakeFirstOrThrow()
|
|
1159
|
+
* }
|
|
1160
|
+
* ```
|
|
1161
|
+
*
|
|
1162
|
+
* Any selections added inside the `if` callback will be added as optional fields to the
|
|
1163
|
+
* output type since we can't know if the selections were actually made before running
|
|
1164
|
+
* the code. In the example above the return type of the `getPerson` function is:
|
|
1165
|
+
*
|
|
1166
|
+
* ```ts
|
|
1167
|
+
* {
|
|
1168
|
+
* id: number
|
|
1169
|
+
* first_name: string
|
|
1170
|
+
* last_name?: string
|
|
1171
|
+
* }
|
|
1172
|
+
* ```
|
|
1173
|
+
*
|
|
1174
|
+
* You can also call any other methods inside the callback:
|
|
1175
|
+
*
|
|
1176
|
+
* ```ts
|
|
1177
|
+
* const { count } = db.fn
|
|
1178
|
+
*
|
|
1179
|
+
* db.selectFrom('person')
|
|
1180
|
+
* .select('person.id')
|
|
1181
|
+
* .if(filterByFirstName, (qb) => qb.where('first_name', '=', firstName))
|
|
1182
|
+
* .if(filterByPetCount, (qb) => qb
|
|
1183
|
+
* .innerJoin('pet', 'pet.owner_id', 'person.id')
|
|
1184
|
+
* .having(count('pet.id'), '>', petCountLimit)
|
|
1185
|
+
* .groupBy('person.id')
|
|
1186
|
+
* )
|
|
1187
|
+
* ```
|
|
1188
|
+
*/
|
|
1189
|
+
if<O2 extends O>(condition: boolean, func: (qb: this) => SelectQueryBuilder<DB, TB, O2>): SelectQueryBuilder<DB, TB, MergePartial<O, O2>>;
|
|
1151
1190
|
/**
|
|
1152
1191
|
* Gives an alias for the query. This method is only useful for sub queries.
|
|
1153
1192
|
*
|
|
@@ -1173,6 +1212,9 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
1173
1212
|
* don't support your use case.
|
|
1174
1213
|
*/
|
|
1175
1214
|
castTo<T>(): SelectQueryBuilder<DB, TB, T>;
|
|
1215
|
+
/**
|
|
1216
|
+
* Returns a copy of this SelectQueryBuilder instance with the given plugin installed.
|
|
1217
|
+
*/
|
|
1176
1218
|
withPlugin(plugin: KyselyPlugin): SelectQueryBuilder<DB, TB, O>;
|
|
1177
1219
|
toOperationNode(): SelectQueryNode;
|
|
1178
1220
|
compile(): CompiledQuery;
|
|
@@ -427,27 +427,15 @@ class SelectQueryBuilder {
|
|
|
427
427
|
/**
|
|
428
428
|
* Simply calls the given function passing `this` as the only argument.
|
|
429
429
|
*
|
|
430
|
-
*
|
|
430
|
+
* If you want to conditionally call a method on `this`, see
|
|
431
|
+
* the {@link if} method.
|
|
431
432
|
*
|
|
432
433
|
* ### Examples
|
|
433
434
|
*
|
|
434
|
-
* ```ts
|
|
435
|
-
* db.selectFrom('person')
|
|
436
|
-
* .selectAll()
|
|
437
|
-
* .call((qb) => {
|
|
438
|
-
* if (something) {
|
|
439
|
-
* return qb.where('something', '=', something)
|
|
440
|
-
* } else {
|
|
441
|
-
* return qb.where('somethingElse', '=', somethingElse)
|
|
442
|
-
* }
|
|
443
|
-
* })
|
|
444
|
-
* .execute()
|
|
445
|
-
* ```
|
|
446
|
-
*
|
|
447
435
|
* The next example uses a helper funtion `log` to log a query:
|
|
448
436
|
*
|
|
449
437
|
* ```ts
|
|
450
|
-
* function log<T extends
|
|
438
|
+
* function log<T extends Compilable>(qb: T): T {
|
|
451
439
|
* console.log(qb.compile())
|
|
452
440
|
* return qb
|
|
453
441
|
* }
|
|
@@ -461,6 +449,64 @@ class SelectQueryBuilder {
|
|
|
461
449
|
call(func) {
|
|
462
450
|
return func(this);
|
|
463
451
|
}
|
|
452
|
+
/**
|
|
453
|
+
* Call `func(this)` if `condition` is true.
|
|
454
|
+
*
|
|
455
|
+
* This method is especially handy with optional selects. Any `select` or `selectAll`
|
|
456
|
+
* method calls add columns as optional fields to the output type when called inside
|
|
457
|
+
* the `func` callback. This is because we can't know if those selections were actually
|
|
458
|
+
* made before running the code.
|
|
459
|
+
*
|
|
460
|
+
* Also see [this recipe](https://github.com/koskimas/kysely/tree/master/recipes/conditional-selects.md)
|
|
461
|
+
*
|
|
462
|
+
* ### Examples
|
|
463
|
+
*
|
|
464
|
+
* ```ts
|
|
465
|
+
* async function getPerson(id: number, withLastName: boolean) {
|
|
466
|
+
* return await db
|
|
467
|
+
* .selectFrom('person')
|
|
468
|
+
* .select(['id', 'first_name'])
|
|
469
|
+
* .if(withLastName, (qb) => qb.select('last_name'))
|
|
470
|
+
* .where('id', '=', id)
|
|
471
|
+
* .executeTakeFirstOrThrow()
|
|
472
|
+
* }
|
|
473
|
+
* ```
|
|
474
|
+
*
|
|
475
|
+
* Any selections added inside the `if` callback will be added as optional fields to the
|
|
476
|
+
* output type since we can't know if the selections were actually made before running
|
|
477
|
+
* the code. In the example above the return type of the `getPerson` function is:
|
|
478
|
+
*
|
|
479
|
+
* ```ts
|
|
480
|
+
* {
|
|
481
|
+
* id: number
|
|
482
|
+
* first_name: string
|
|
483
|
+
* last_name?: string
|
|
484
|
+
* }
|
|
485
|
+
* ```
|
|
486
|
+
*
|
|
487
|
+
* You can also call any other methods inside the callback:
|
|
488
|
+
*
|
|
489
|
+
* ```ts
|
|
490
|
+
* const { count } = db.fn
|
|
491
|
+
*
|
|
492
|
+
* db.selectFrom('person')
|
|
493
|
+
* .select('person.id')
|
|
494
|
+
* .if(filterByFirstName, (qb) => qb.where('first_name', '=', firstName))
|
|
495
|
+
* .if(filterByPetCount, (qb) => qb
|
|
496
|
+
* .innerJoin('pet', 'pet.owner_id', 'person.id')
|
|
497
|
+
* .having(count('pet.id'), '>', petCountLimit)
|
|
498
|
+
* .groupBy('person.id')
|
|
499
|
+
* )
|
|
500
|
+
* ```
|
|
501
|
+
*/
|
|
502
|
+
if(condition, func) {
|
|
503
|
+
if (condition) {
|
|
504
|
+
return func(this);
|
|
505
|
+
}
|
|
506
|
+
return new SelectQueryBuilder({
|
|
507
|
+
...this.#props,
|
|
508
|
+
});
|
|
509
|
+
}
|
|
464
510
|
/**
|
|
465
511
|
* Gives an alias for the query. This method is only useful for sub queries.
|
|
466
512
|
*
|
|
@@ -490,6 +536,9 @@ class SelectQueryBuilder {
|
|
|
490
536
|
castTo() {
|
|
491
537
|
return new SelectQueryBuilder(this.#props);
|
|
492
538
|
}
|
|
539
|
+
/**
|
|
540
|
+
* Returns a copy of this SelectQueryBuilder instance with the given plugin installed.
|
|
541
|
+
*/
|
|
493
542
|
withPlugin(plugin) {
|
|
494
543
|
return new SelectQueryBuilder({
|
|
495
544
|
...this.#props,
|
|
@@ -6,13 +6,14 @@ import { SelectExpression } from '../parser/select-parser.js';
|
|
|
6
6
|
import { ExistsExpression, FilterOperator, FilterValueExpressionOrList, WhereGrouper } from '../parser/filter-parser.js';
|
|
7
7
|
import { ReturningRow } from '../parser/returning-parser.js';
|
|
8
8
|
import { ReferenceExpression } from '../parser/reference-parser.js';
|
|
9
|
-
import { AnyRawBuilder, SingleResultType } from '../util/type-utils.js';
|
|
9
|
+
import { AnyRawBuilder, MergePartial, SingleResultType } from '../util/type-utils.js';
|
|
10
10
|
import { UpdateQueryNode } from '../operation-node/update-query-node.js';
|
|
11
11
|
import { MutationObject } from '../parser/update-set-parser.js';
|
|
12
12
|
import { Compilable } from '../util/compilable.js';
|
|
13
13
|
import { QueryExecutor } from '../query-executor/query-executor.js';
|
|
14
14
|
import { QueryId } from '../util/query-id.js';
|
|
15
15
|
import { ParseContext } from '../parser/parse-context.js';
|
|
16
|
+
import { UpdateResult } from './update-result.js';
|
|
16
17
|
import { KyselyPlugin } from '../plugin/kysely-plugin.js';
|
|
17
18
|
import { WhereInterface } from './where-interface.js';
|
|
18
19
|
import { JoinInterface } from './join-interface.js';
|
|
@@ -676,24 +677,63 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
676
677
|
/**
|
|
677
678
|
* Simply calls the given function passing `this` as the only argument.
|
|
678
679
|
*
|
|
679
|
-
*
|
|
680
|
+
* If you want to conditionally call a method on `this`, see
|
|
681
|
+
* the {@link if} method.
|
|
680
682
|
*
|
|
681
683
|
* ### Examples
|
|
682
684
|
*
|
|
685
|
+
* The next example uses a helper funtion `log` to log a query:
|
|
686
|
+
*
|
|
683
687
|
* ```ts
|
|
688
|
+
* function log<T extends Compilable>(qb: T): T {
|
|
689
|
+
* console.log(qb.compile())
|
|
690
|
+
* return qb
|
|
691
|
+
* }
|
|
692
|
+
*
|
|
684
693
|
* db.updateTable('person')
|
|
685
694
|
* .set(values)
|
|
686
|
-
* .call(
|
|
687
|
-
* if (something) {
|
|
688
|
-
* return qb.where('something', '=', something)
|
|
689
|
-
* } else {
|
|
690
|
-
* return qb.where('somethingElse', '=', somethingElse)
|
|
691
|
-
* }
|
|
692
|
-
* })
|
|
695
|
+
* .call(log)
|
|
693
696
|
* .execute()
|
|
694
697
|
* ```
|
|
695
698
|
*/
|
|
696
699
|
call<T>(func: (qb: this) => T): T;
|
|
700
|
+
/**
|
|
701
|
+
* Call `func(this)` if `condition` is true.
|
|
702
|
+
*
|
|
703
|
+
* This method is especially handy with optional selects. Any `returning` or `returningAll`
|
|
704
|
+
* method calls add columns as optional fields to the output type when called inside
|
|
705
|
+
* the `func` callback. This is because we can't know if those selections were actually
|
|
706
|
+
* made before running the code.
|
|
707
|
+
*
|
|
708
|
+
* You can also call any other methods inside the callback.
|
|
709
|
+
*
|
|
710
|
+
* ### Examples
|
|
711
|
+
*
|
|
712
|
+
* ```ts
|
|
713
|
+
* async function updatePerson(id: number, updates: UpdateablePerson, returnLastName: boolean) {
|
|
714
|
+
* return await db
|
|
715
|
+
* .updateTable('person')
|
|
716
|
+
* .set(updates)
|
|
717
|
+
* .where('id', '=', id)
|
|
718
|
+
* .returning(['id', 'first_name'])
|
|
719
|
+
* .if(returnLastName, (qb) => qb.returning('last_name'))
|
|
720
|
+
* .executeTakeFirstOrThrow()
|
|
721
|
+
* }
|
|
722
|
+
* ```
|
|
723
|
+
*
|
|
724
|
+
* Any selections added inside the `if` callback will be added as optional fields to the
|
|
725
|
+
* output type since we can't know if the selections were actually made before running
|
|
726
|
+
* the code. In the example above the return type of the `updatePerson` function is:
|
|
727
|
+
*
|
|
728
|
+
* ```ts
|
|
729
|
+
* {
|
|
730
|
+
* id: number
|
|
731
|
+
* first_name: string
|
|
732
|
+
* last_name?: string
|
|
733
|
+
* }
|
|
734
|
+
* ```
|
|
735
|
+
*/
|
|
736
|
+
if<O2>(condition: boolean, func: (qb: this) => UpdateQueryBuilder<DB, TB, O2>): UpdateQueryBuilder<DB, TB, O2 extends UpdateResult ? UpdateResult : O extends UpdateResult ? Partial<O2> : MergePartial<O, O2>>;
|
|
697
737
|
/**
|
|
698
738
|
* Change the output type of the query.
|
|
699
739
|
*
|
|
@@ -701,6 +741,9 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
701
741
|
* don't support your use case.
|
|
702
742
|
*/
|
|
703
743
|
castTo<T>(): UpdateQueryBuilder<DB, TB, T>;
|
|
744
|
+
/**
|
|
745
|
+
* Returns a copy of this UpdateQueryBuilder instance with the given plugin installed.
|
|
746
|
+
*/
|
|
704
747
|
withPlugin(plugin: KyselyPlugin): UpdateQueryBuilder<DB, TB, O>;
|
|
705
748
|
toOperationNode(): UpdateQueryNode;
|
|
706
749
|
compile(): CompiledQuery;
|
|
@@ -194,26 +194,72 @@ class UpdateQueryBuilder {
|
|
|
194
194
|
/**
|
|
195
195
|
* Simply calls the given function passing `this` as the only argument.
|
|
196
196
|
*
|
|
197
|
-
*
|
|
197
|
+
* If you want to conditionally call a method on `this`, see
|
|
198
|
+
* the {@link if} method.
|
|
198
199
|
*
|
|
199
200
|
* ### Examples
|
|
200
201
|
*
|
|
202
|
+
* The next example uses a helper funtion `log` to log a query:
|
|
203
|
+
*
|
|
201
204
|
* ```ts
|
|
205
|
+
* function log<T extends Compilable>(qb: T): T {
|
|
206
|
+
* console.log(qb.compile())
|
|
207
|
+
* return qb
|
|
208
|
+
* }
|
|
209
|
+
*
|
|
202
210
|
* db.updateTable('person')
|
|
203
211
|
* .set(values)
|
|
204
|
-
* .call(
|
|
205
|
-
* if (something) {
|
|
206
|
-
* return qb.where('something', '=', something)
|
|
207
|
-
* } else {
|
|
208
|
-
* return qb.where('somethingElse', '=', somethingElse)
|
|
209
|
-
* }
|
|
210
|
-
* })
|
|
212
|
+
* .call(log)
|
|
211
213
|
* .execute()
|
|
212
214
|
* ```
|
|
213
215
|
*/
|
|
214
216
|
call(func) {
|
|
215
217
|
return func(this);
|
|
216
218
|
}
|
|
219
|
+
/**
|
|
220
|
+
* Call `func(this)` if `condition` is true.
|
|
221
|
+
*
|
|
222
|
+
* This method is especially handy with optional selects. Any `returning` or `returningAll`
|
|
223
|
+
* method calls add columns as optional fields to the output type when called inside
|
|
224
|
+
* the `func` callback. This is because we can't know if those selections were actually
|
|
225
|
+
* made before running the code.
|
|
226
|
+
*
|
|
227
|
+
* You can also call any other methods inside the callback.
|
|
228
|
+
*
|
|
229
|
+
* ### Examples
|
|
230
|
+
*
|
|
231
|
+
* ```ts
|
|
232
|
+
* async function updatePerson(id: number, updates: UpdateablePerson, returnLastName: boolean) {
|
|
233
|
+
* return await db
|
|
234
|
+
* .updateTable('person')
|
|
235
|
+
* .set(updates)
|
|
236
|
+
* .where('id', '=', id)
|
|
237
|
+
* .returning(['id', 'first_name'])
|
|
238
|
+
* .if(returnLastName, (qb) => qb.returning('last_name'))
|
|
239
|
+
* .executeTakeFirstOrThrow()
|
|
240
|
+
* }
|
|
241
|
+
* ```
|
|
242
|
+
*
|
|
243
|
+
* Any selections added inside the `if` callback will be added as optional fields to the
|
|
244
|
+
* output type since we can't know if the selections were actually made before running
|
|
245
|
+
* the code. In the example above the return type of the `updatePerson` function is:
|
|
246
|
+
*
|
|
247
|
+
* ```ts
|
|
248
|
+
* {
|
|
249
|
+
* id: number
|
|
250
|
+
* first_name: string
|
|
251
|
+
* last_name?: string
|
|
252
|
+
* }
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
255
|
+
if(condition, func) {
|
|
256
|
+
if (condition) {
|
|
257
|
+
return func(this);
|
|
258
|
+
}
|
|
259
|
+
return new UpdateQueryBuilder({
|
|
260
|
+
...this.#props,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
217
263
|
/**
|
|
218
264
|
* Change the output type of the query.
|
|
219
265
|
*
|
|
@@ -223,6 +269,9 @@ class UpdateQueryBuilder {
|
|
|
223
269
|
castTo() {
|
|
224
270
|
return new UpdateQueryBuilder(this.#props);
|
|
225
271
|
}
|
|
272
|
+
/**
|
|
273
|
+
* Returns a copy of this UpdateQueryBuilder instance with the given plugin installed.
|
|
274
|
+
*/
|
|
226
275
|
withPlugin(plugin) {
|
|
227
276
|
return new UpdateQueryBuilder({
|
|
228
277
|
...this.#props,
|
|
@@ -44,7 +44,6 @@ import { ValueNode } from '../operation-node/value-node.js';
|
|
|
44
44
|
import { WhereNode } from '../operation-node/where-node.js';
|
|
45
45
|
import { CommonTableExpressionNode } from '../operation-node/common-table-expression-node.js';
|
|
46
46
|
import { WithNode } from '../operation-node/with-node.js';
|
|
47
|
-
import { PrimitiveValue } from '../util/object-utils.js';
|
|
48
47
|
import { CompiledQuery } from './compiled-query.js';
|
|
49
48
|
import { RootOperationNode, QueryCompiler } from './query-compiler.js';
|
|
50
49
|
import { HavingNode } from '../operation-node/having-node.js';
|
|
@@ -150,5 +149,5 @@ export declare class DefaultQueryCompiler extends OperationNodeVisitor implement
|
|
|
150
149
|
protected getRightIdentifierWrapper(): string;
|
|
151
150
|
protected getCurrentParameterPlaceholder(): string;
|
|
152
151
|
protected addParameter(parameter: unknown): void;
|
|
153
|
-
protected appendImmediateValue(value:
|
|
152
|
+
protected appendImmediateValue(value: unknown): void;
|
|
154
153
|
}
|
|
@@ -162,7 +162,7 @@ export declare class QueryCreator<DB> {
|
|
|
162
162
|
/**
|
|
163
163
|
* Creates a delete query.
|
|
164
164
|
*
|
|
165
|
-
* See the {@link
|
|
165
|
+
* See the {@link DeleteQueryBuilder.where} method for examples on how to specify
|
|
166
166
|
* a where clause for the delete operation.
|
|
167
167
|
*
|
|
168
168
|
* The return value of the query is an instance of {@link DeleteResult}.
|
|
@@ -182,7 +182,7 @@ export declare class QueryCreator<DB> {
|
|
|
182
182
|
/**
|
|
183
183
|
* Creates an update query.
|
|
184
184
|
*
|
|
185
|
-
* See the {@link
|
|
185
|
+
* See the {@link UpdateQueryBuilder.where} method for examples on how to specify
|
|
186
186
|
* a where clause for the update operation.
|
|
187
187
|
*
|
|
188
188
|
* See the {@link UpdateQueryBuilder.set} method for examples on how to
|
|
@@ -78,7 +78,7 @@ class QueryCreator {
|
|
|
78
78
|
/**
|
|
79
79
|
* Creates a delete query.
|
|
80
80
|
*
|
|
81
|
-
* See the {@link
|
|
81
|
+
* See the {@link DeleteQueryBuilder.where} method for examples on how to specify
|
|
82
82
|
* a where clause for the delete operation.
|
|
83
83
|
*
|
|
84
84
|
* The return value of the query is an instance of {@link DeleteResult}.
|
|
@@ -105,7 +105,7 @@ class QueryCreator {
|
|
|
105
105
|
/**
|
|
106
106
|
* Creates an update query.
|
|
107
107
|
*
|
|
108
|
-
* See the {@link
|
|
108
|
+
* See the {@link UpdateQueryBuilder.where} method for examples on how to specify
|
|
109
109
|
* a where clause for the update operation.
|
|
110
110
|
*
|
|
111
111
|
* See the {@link UpdateQueryBuilder.set} method for examples on how to
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.QueryExecutor = void 0;
|
|
4
4
|
const object_utils_js_1 = require("../util/object-utils.js");
|
|
5
|
+
const NO_PLUGINS = (0, object_utils_js_1.freeze)([]);
|
|
5
6
|
/**
|
|
6
7
|
* This class abstracts away the details of how to compile a query into SQL
|
|
7
8
|
* and execute it. Instead of passing around all those details, {@link SelectQueryBuilder}
|
|
@@ -10,8 +11,8 @@ const object_utils_js_1 = require("../util/object-utils.js");
|
|
|
10
11
|
*/
|
|
11
12
|
class QueryExecutor {
|
|
12
13
|
#plugins;
|
|
13
|
-
constructor(plugins
|
|
14
|
-
this.#plugins =
|
|
14
|
+
constructor(plugins) {
|
|
15
|
+
this.#plugins = plugins ?? NO_PLUGINS;
|
|
15
16
|
}
|
|
16
17
|
get plugins() {
|
|
17
18
|
return this.#plugins;
|
|
@@ -40,8 +40,8 @@ export declare class AliasedRawBuilder<O = unknown, A extends string = never> im
|
|
|
40
40
|
constructor(rawBuilder: RawBuilder<O>, alias: A);
|
|
41
41
|
}
|
|
42
42
|
export interface RawBuilderProps {
|
|
43
|
-
readonly queryId
|
|
44
|
-
readonly executor
|
|
43
|
+
readonly queryId?: QueryId;
|
|
44
|
+
readonly executor?: QueryExecutor;
|
|
45
45
|
readonly sql: string;
|
|
46
46
|
readonly parameters?: ReadonlyArray<unknown>;
|
|
47
47
|
}
|