kysely 0.16.5 → 0.16.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/README.md +139 -14
- 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 +22 -0
- package/dist/cjs/migration/migrator.js +23 -1
- 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/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-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/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 +1 -0
- package/dist/cjs/util/object-utils.js +50 -1
- 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 +22 -0
- package/dist/esm/migration/migrator.js +23 -1
- 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/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-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/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 +1 -0
- package/dist/esm/util/object-utils.js +48 -0
- package/dist/esm/util/type-utils.d.ts +21 -0
- package/package.json +11 -5
|
@@ -32,6 +32,28 @@ class Migrator {
|
|
|
32
32
|
constructor(props) {
|
|
33
33
|
this.#props = (0, object_utils_js_1.freeze)(props);
|
|
34
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Returns a {@link MigrationInfo} object for each migration.
|
|
37
|
+
*
|
|
38
|
+
* The returned array is sorted by migration name.
|
|
39
|
+
*/
|
|
40
|
+
async getMigrations() {
|
|
41
|
+
const executedMigrations = (await this.#doesTableExists(exports.MIGRATION_TABLE))
|
|
42
|
+
? await this.#props.db
|
|
43
|
+
.selectFrom(exports.MIGRATION_TABLE)
|
|
44
|
+
.select(['name', 'timestamp'])
|
|
45
|
+
.execute()
|
|
46
|
+
: [];
|
|
47
|
+
const migrations = await this.#resolveMigrations();
|
|
48
|
+
return migrations.map(({ name, ...migration }) => {
|
|
49
|
+
const executed = executedMigrations.find((it) => it.name === name);
|
|
50
|
+
return {
|
|
51
|
+
name,
|
|
52
|
+
migration,
|
|
53
|
+
executedAt: executed ? new Date(executed.timestamp) : undefined,
|
|
54
|
+
};
|
|
55
|
+
});
|
|
56
|
+
}
|
|
35
57
|
/**
|
|
36
58
|
* Runs all migrations that have not yet been run.
|
|
37
59
|
*
|
|
@@ -215,7 +237,7 @@ class Migrator {
|
|
|
215
237
|
try {
|
|
216
238
|
await this.#props.db
|
|
217
239
|
.insertInto(exports.MIGRATION_LOCK_TABLE)
|
|
218
|
-
.values({ id: exports.MIGRATION_LOCK_ID })
|
|
240
|
+
.values({ id: exports.MIGRATION_LOCK_ID, is_locked: 0 })
|
|
219
241
|
.execute();
|
|
220
242
|
}
|
|
221
243
|
catch (error) {
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { QueryResult } from '../../driver/database-connection.js';
|
|
2
|
+
import { RootOperationNode } from '../../query-compiler/query-compiler.js';
|
|
3
|
+
import { UnknownRow } from '../../util/type-utils.js';
|
|
4
|
+
import { KyselyPlugin, PluginTransformQueryArgs, PluginTransformResultArgs } from '../kysely-plugin.js';
|
|
5
|
+
/**
|
|
6
|
+
* Plugin that removes duplicate joins from queries.
|
|
7
|
+
*
|
|
8
|
+
* See [this recipe](https://github.com/koskimas/kysely/tree/master/recipes/deduplicate-joins.md)
|
|
9
|
+
*/
|
|
10
|
+
export declare class DeduplicateJoinsPlugin implements KyselyPlugin {
|
|
11
|
+
#private;
|
|
12
|
+
/**
|
|
13
|
+
* This is called for each query before it is executed. You can modify the query by
|
|
14
|
+
* transforming its {@link OperationNode} tree provided in {@link PluginTransformQueryArgs.node | args.node}
|
|
15
|
+
* and returning the transformed tree. You'd usually want to use an {@link OperationNodeTransformer}
|
|
16
|
+
* for this.
|
|
17
|
+
*
|
|
18
|
+
* If you need to pass some query-related data between this method and `transformResult` you
|
|
19
|
+
* can use a `WeakMap` with {@link PluginTransformQueryArgs.queryId | args.queryId} as the key:
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* const plugin = {
|
|
23
|
+
* data: new WeakMap<QueryId, SomeData>(),
|
|
24
|
+
*
|
|
25
|
+
* transformQuery(args: PluginTransformQueryArgs): RootOperationNode {
|
|
26
|
+
* this.data.set(args.queryId, something)
|
|
27
|
+
* return args.node
|
|
28
|
+
* },
|
|
29
|
+
*
|
|
30
|
+
* transformResult(args: PluginTransformResultArgs): QueryResult<UnknownRow> {
|
|
31
|
+
* const data = this.data.get(args.queryId)
|
|
32
|
+
* return args.result
|
|
33
|
+
* }
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* You should use a `WeakMap` instead of a `Map` or some other strong references because `transformQuery`
|
|
38
|
+
* is not always matched by a call to `transformResult` which would leave orphaned items in the map
|
|
39
|
+
* and cause a memory leak.
|
|
40
|
+
*/
|
|
41
|
+
transformQuery(args: PluginTransformQueryArgs): RootOperationNode;
|
|
42
|
+
/**
|
|
43
|
+
* This method is called for each query after it has been executed. The result
|
|
44
|
+
* of the query can be accessed through {@link PluginTransformResultArgs.result | args.result}.
|
|
45
|
+
* You can modify the result and return the modifier result.
|
|
46
|
+
*/
|
|
47
|
+
transformResult(args: PluginTransformResultArgs): Promise<QueryResult<UnknownRow>>;
|
|
48
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DeduplicateJoinsPlugin = void 0;
|
|
4
|
+
const deduplicate_joins_transformer_js_1 = require("./deduplicate-joins-transformer.js");
|
|
5
|
+
/**
|
|
6
|
+
* Plugin that removes duplicate joins from queries.
|
|
7
|
+
*
|
|
8
|
+
* See [this recipe](https://github.com/koskimas/kysely/tree/master/recipes/deduplicate-joins.md)
|
|
9
|
+
*/
|
|
10
|
+
class DeduplicateJoinsPlugin {
|
|
11
|
+
#transformer = new deduplicate_joins_transformer_js_1.DeduplicateJoinsTransformer();
|
|
12
|
+
transformQuery(args) {
|
|
13
|
+
return this.#transformer.transformNode(args.node);
|
|
14
|
+
}
|
|
15
|
+
transformResult(args) {
|
|
16
|
+
return Promise.resolve(args.result);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.DeduplicateJoinsPlugin = DeduplicateJoinsPlugin;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { DeleteQueryNode, UpdateQueryNode } from '../../index-nodeless.js';
|
|
2
|
+
import { OperationNodeTransformer } from '../../operation-node/operation-node-transformer.js';
|
|
3
|
+
import { SelectQueryNode } from '../../operation-node/select-query-node.js';
|
|
4
|
+
export declare class DeduplicateJoinsTransformer extends OperationNodeTransformer {
|
|
5
|
+
#private;
|
|
6
|
+
protected transformSelectQuery(node: SelectQueryNode): SelectQueryNode;
|
|
7
|
+
protected transformUpdateQuery(node: UpdateQueryNode): UpdateQueryNode;
|
|
8
|
+
protected transformDeleteQuery(node: DeleteQueryNode): DeleteQueryNode;
|
|
9
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DeduplicateJoinsTransformer = void 0;
|
|
4
|
+
const operation_node_transformer_js_1 = require("../../operation-node/operation-node-transformer.js");
|
|
5
|
+
const object_utils_js_1 = require("../../util/object-utils.js");
|
|
6
|
+
class DeduplicateJoinsTransformer extends operation_node_transformer_js_1.OperationNodeTransformer {
|
|
7
|
+
transformSelectQuery(node) {
|
|
8
|
+
return this.#transformQuery(super.transformSelectQuery(node));
|
|
9
|
+
}
|
|
10
|
+
transformUpdateQuery(node) {
|
|
11
|
+
return this.#transformQuery(super.transformUpdateQuery(node));
|
|
12
|
+
}
|
|
13
|
+
transformDeleteQuery(node) {
|
|
14
|
+
return this.#transformQuery(super.transformDeleteQuery(node));
|
|
15
|
+
}
|
|
16
|
+
#transformQuery(node) {
|
|
17
|
+
if (!node.joins || node.joins.length === 0) {
|
|
18
|
+
return node;
|
|
19
|
+
}
|
|
20
|
+
return (0, object_utils_js_1.freeze)({
|
|
21
|
+
...node,
|
|
22
|
+
joins: this.#deduplicateJoins(node.joins),
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
#deduplicateJoins(joins) {
|
|
26
|
+
const out = [];
|
|
27
|
+
for (let i = 0; i < joins.length; ++i) {
|
|
28
|
+
let foundDuplicate = false;
|
|
29
|
+
for (let j = i + 1; j < joins.length; ++j) {
|
|
30
|
+
if ((0, object_utils_js_1.compare)(joins[i], joins[j])) {
|
|
31
|
+
foundDuplicate = true;
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (!foundDuplicate) {
|
|
36
|
+
out.push(joins[i]);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return (0, object_utils_js_1.freeze)(out);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.DeduplicateJoinsTransformer = DeduplicateJoinsTransformer;
|
|
File without changes
|
|
File without changes
|
package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.d.ts
RENAMED
|
File without changes
|
package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.js
RENAMED
|
File without changes
|
|
@@ -6,7 +6,7 @@ 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 { Compilable } from '../util/compilable.js';
|
|
11
11
|
import { QueryExecutor } from '../query-executor/query-executor.js';
|
|
12
12
|
import { QueryId } from '../util/query-id.js';
|
|
@@ -16,6 +16,7 @@ import { WhereInterface } from './where-interface.js';
|
|
|
16
16
|
import { JoinInterface } from './join-interface.js';
|
|
17
17
|
import { ReturningInterface } from './returning-interface.js';
|
|
18
18
|
import { NoResultErrorConstructor } from './no-result-error.js';
|
|
19
|
+
import { DeleteResult } from './delete-result.js';
|
|
19
20
|
import { DeleteQueryNode } from '../operation-node/delete-query-node.js';
|
|
20
21
|
import { Selectable } from '../util/column-type.js';
|
|
21
22
|
export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements WhereInterface<DB, TB>, JoinInterface<DB, TB>, ReturningInterface<DB, TB, O>, OperationNodeSource, Compilable {
|
|
@@ -589,23 +590,61 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
589
590
|
/**
|
|
590
591
|
* Simply calls the given function passing `this` as the only argument.
|
|
591
592
|
*
|
|
592
|
-
*
|
|
593
|
+
* If you want to conditionally call a method on `this`, see
|
|
594
|
+
* the {@link if} method.
|
|
593
595
|
*
|
|
594
596
|
* ### Examples
|
|
595
597
|
*
|
|
598
|
+
* The next example uses a helper funtion `log` to log a query:
|
|
599
|
+
*
|
|
596
600
|
* ```ts
|
|
601
|
+
* function log<T extends Compilable>(qb: T): T {
|
|
602
|
+
* console.log(qb.compile())
|
|
603
|
+
* return qb
|
|
604
|
+
* }
|
|
605
|
+
*
|
|
597
606
|
* db.deleteFrom('person')
|
|
598
|
-
* .call(
|
|
599
|
-
* if (something) {
|
|
600
|
-
* return qb.where('something', '=', something)
|
|
601
|
-
* } else {
|
|
602
|
-
* return qb.where('somethingElse', '=', somethingElse)
|
|
603
|
-
* }
|
|
604
|
-
* })
|
|
607
|
+
* .call(log)
|
|
605
608
|
* .execute()
|
|
606
609
|
* ```
|
|
607
610
|
*/
|
|
608
611
|
call<T>(func: (qb: this) => T): T;
|
|
612
|
+
/**
|
|
613
|
+
* Call `func(this)` if `condition` is true.
|
|
614
|
+
*
|
|
615
|
+
* This method is especially handy with optional selects. Any `returning` or `returningAll`
|
|
616
|
+
* method calls add columns as optional fields to the output type when called inside
|
|
617
|
+
* the `func` callback. This is because we can't know if those selections were actually
|
|
618
|
+
* made before running the code.
|
|
619
|
+
*
|
|
620
|
+
* You can also call any other methods inside the callback.
|
|
621
|
+
*
|
|
622
|
+
* ### Examples
|
|
623
|
+
*
|
|
624
|
+
* ```ts
|
|
625
|
+
* async function deletePerson(id: number, returnLastName: boolean) {
|
|
626
|
+
* return await db
|
|
627
|
+
* .deleteFrom('person')
|
|
628
|
+
* .where('id', '=', id)
|
|
629
|
+
* .returning(['id', 'first_name'])
|
|
630
|
+
* .if(returnLastName, (qb) => qb.returning('last_name'))
|
|
631
|
+
* .executeTakeFirstOrThrow()
|
|
632
|
+
* }
|
|
633
|
+
* ```
|
|
634
|
+
*
|
|
635
|
+
* Any selections added inside the `if` callback will be added as optional fields to the
|
|
636
|
+
* output type since we can't know if the selections were actually made before running
|
|
637
|
+
* the code. In the example above the return type of the `deletePerson` function is:
|
|
638
|
+
*
|
|
639
|
+
* ```ts
|
|
640
|
+
* {
|
|
641
|
+
* id: number
|
|
642
|
+
* first_name: string
|
|
643
|
+
* last_name?: string
|
|
644
|
+
* }
|
|
645
|
+
* ```
|
|
646
|
+
*/
|
|
647
|
+
if<O2>(condition: boolean, func: (qb: this) => DeleteQueryBuilder<DB, TB, O2>): DeleteQueryBuilder<DB, TB, O2 extends DeleteResult ? DeleteResult : O extends DeleteResult ? Partial<O2> : MergePartial<O, O2>>;
|
|
609
648
|
/**
|
|
610
649
|
* Change the output type of the query.
|
|
611
650
|
*
|
|
@@ -613,6 +652,9 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
613
652
|
* don't support your use case.
|
|
614
653
|
*/
|
|
615
654
|
castTo<T>(): DeleteQueryBuilder<DB, TB, T>;
|
|
655
|
+
/**
|
|
656
|
+
* Returns a copy of this DeleteQueryBuilder instance with the given plugin installed.
|
|
657
|
+
*/
|
|
616
658
|
withPlugin(plugin: KyselyPlugin): DeleteQueryBuilder<DB, TB, O>;
|
|
617
659
|
toOperationNode(): DeleteQueryNode;
|
|
618
660
|
compile(): CompiledQuery;
|
|
@@ -101,25 +101,70 @@ class DeleteQueryBuilder {
|
|
|
101
101
|
/**
|
|
102
102
|
* Simply calls the given function passing `this` as the only argument.
|
|
103
103
|
*
|
|
104
|
-
*
|
|
104
|
+
* If you want to conditionally call a method on `this`, see
|
|
105
|
+
* the {@link if} method.
|
|
105
106
|
*
|
|
106
107
|
* ### Examples
|
|
107
108
|
*
|
|
109
|
+
* The next example uses a helper funtion `log` to log a query:
|
|
110
|
+
*
|
|
108
111
|
* ```ts
|
|
112
|
+
* function log<T extends Compilable>(qb: T): T {
|
|
113
|
+
* console.log(qb.compile())
|
|
114
|
+
* return qb
|
|
115
|
+
* }
|
|
116
|
+
*
|
|
109
117
|
* db.deleteFrom('person')
|
|
110
|
-
* .call(
|
|
111
|
-
* if (something) {
|
|
112
|
-
* return qb.where('something', '=', something)
|
|
113
|
-
* } else {
|
|
114
|
-
* return qb.where('somethingElse', '=', somethingElse)
|
|
115
|
-
* }
|
|
116
|
-
* })
|
|
118
|
+
* .call(log)
|
|
117
119
|
* .execute()
|
|
118
120
|
* ```
|
|
119
121
|
*/
|
|
120
122
|
call(func) {
|
|
121
123
|
return func(this);
|
|
122
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* Call `func(this)` if `condition` is true.
|
|
127
|
+
*
|
|
128
|
+
* This method is especially handy with optional selects. Any `returning` or `returningAll`
|
|
129
|
+
* method calls add columns as optional fields to the output type when called inside
|
|
130
|
+
* the `func` callback. This is because we can't know if those selections were actually
|
|
131
|
+
* made before running the code.
|
|
132
|
+
*
|
|
133
|
+
* You can also call any other methods inside the callback.
|
|
134
|
+
*
|
|
135
|
+
* ### Examples
|
|
136
|
+
*
|
|
137
|
+
* ```ts
|
|
138
|
+
* async function deletePerson(id: number, returnLastName: boolean) {
|
|
139
|
+
* return await db
|
|
140
|
+
* .deleteFrom('person')
|
|
141
|
+
* .where('id', '=', id)
|
|
142
|
+
* .returning(['id', 'first_name'])
|
|
143
|
+
* .if(returnLastName, (qb) => qb.returning('last_name'))
|
|
144
|
+
* .executeTakeFirstOrThrow()
|
|
145
|
+
* }
|
|
146
|
+
* ```
|
|
147
|
+
*
|
|
148
|
+
* Any selections added inside the `if` callback will be added as optional fields to the
|
|
149
|
+
* output type since we can't know if the selections were actually made before running
|
|
150
|
+
* the code. In the example above the return type of the `deletePerson` function is:
|
|
151
|
+
*
|
|
152
|
+
* ```ts
|
|
153
|
+
* {
|
|
154
|
+
* id: number
|
|
155
|
+
* first_name: string
|
|
156
|
+
* last_name?: string
|
|
157
|
+
* }
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
if(condition, func) {
|
|
161
|
+
if (condition) {
|
|
162
|
+
return func(this);
|
|
163
|
+
}
|
|
164
|
+
return new DeleteQueryBuilder({
|
|
165
|
+
...this.#props,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
123
168
|
/**
|
|
124
169
|
* Change the output type of the query.
|
|
125
170
|
*
|
|
@@ -129,6 +174,9 @@ class DeleteQueryBuilder {
|
|
|
129
174
|
castTo() {
|
|
130
175
|
return new DeleteQueryBuilder(this.#props);
|
|
131
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* Returns a copy of this DeleteQueryBuilder instance with the given plugin installed.
|
|
179
|
+
*/
|
|
132
180
|
withPlugin(plugin) {
|
|
133
181
|
return new DeleteQueryBuilder({
|
|
134
182
|
...this.#props,
|
|
@@ -4,6 +4,9 @@ import { RawBuilder } from '../raw-builder/raw-builder.js';
|
|
|
4
4
|
/**
|
|
5
5
|
* Helpers for type safe SQL function calls.
|
|
6
6
|
*
|
|
7
|
+
* You can always use {@link Kysely.raw} to call functions and build arbitrary
|
|
8
|
+
* expressions. This module simply has shortcuts for most common function calls.
|
|
9
|
+
*
|
|
7
10
|
* ### Examples
|
|
8
11
|
*
|
|
9
12
|
* ```ts
|
|
@@ -35,12 +38,33 @@ export declare class FunctionBuilder<DB, TB extends keyof DB> {
|
|
|
35
38
|
constructor(props: FunctionBuilderProps);
|
|
36
39
|
/**
|
|
37
40
|
* Calls the `max` function for the column given as the argument.
|
|
41
|
+
*
|
|
42
|
+
* ### Examples
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* const { max } = db.fn
|
|
46
|
+
*
|
|
47
|
+
* db.selectFrom('toy')
|
|
48
|
+
* .select(max('price').as('max_price'))
|
|
49
|
+
* .execute()
|
|
50
|
+
* ```
|
|
38
51
|
*/
|
|
39
|
-
|
|
52
|
+
max<C extends StringReference<DB, TB>>(column: C): RawBuilder<ExtractTypeFromReferenceExpression<DB, TB, C>>;
|
|
40
53
|
/**
|
|
41
54
|
* Calls the `min` function for the column given as the argument.
|
|
55
|
+
*
|
|
56
|
+
* ### Examples
|
|
57
|
+
*
|
|
58
|
+
* ```ts
|
|
59
|
+
* const { min } = db.fn
|
|
60
|
+
*
|
|
61
|
+
* db.selectFrom('toy')
|
|
62
|
+
* .select(min('price').as('min_price'))
|
|
63
|
+
* .execute()
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
42
66
|
*/
|
|
43
|
-
|
|
67
|
+
min<C extends StringReference<DB, TB>>(column: C): RawBuilder<ExtractTypeFromReferenceExpression<DB, TB, C>>;
|
|
44
68
|
/**
|
|
45
69
|
* Calls the `avg` function for the column given as the argument.
|
|
46
70
|
*
|
|
@@ -58,7 +82,7 @@ export declare class FunctionBuilder<DB, TB extends keyof DB> {
|
|
|
58
82
|
* const { avg } = db.fn
|
|
59
83
|
*
|
|
60
84
|
* db.selectFrom('toy')
|
|
61
|
-
* .select(avg<number>('price'))
|
|
85
|
+
* .select(avg<number>('price').as('avg_price'))
|
|
62
86
|
* .execute()
|
|
63
87
|
* ```
|
|
64
88
|
*/
|
|
@@ -80,7 +104,7 @@ export declare class FunctionBuilder<DB, TB extends keyof DB> {
|
|
|
80
104
|
* const { sum } = db.fn
|
|
81
105
|
*
|
|
82
106
|
* db.selectFrom('toy')
|
|
83
|
-
* .select(sum<number>('price'))
|
|
107
|
+
* .select(sum<number>('price').as('total_price'))
|
|
84
108
|
* .execute()
|
|
85
109
|
* ```
|
|
86
110
|
*/
|
|
@@ -102,7 +126,7 @@ export declare class FunctionBuilder<DB, TB extends keyof DB> {
|
|
|
102
126
|
* const { count } = db.fn
|
|
103
127
|
*
|
|
104
128
|
* db.selectFrom('toy')
|
|
105
|
-
* .select(count<number>('
|
|
129
|
+
* .select(count<number>('id').as('num_toys'))
|
|
106
130
|
* .execute()
|
|
107
131
|
* ```
|
|
108
132
|
*/
|
|
@@ -7,6 +7,9 @@ const query_id_js_1 = require("../util/query-id.js");
|
|
|
7
7
|
/**
|
|
8
8
|
* Helpers for type safe SQL function calls.
|
|
9
9
|
*
|
|
10
|
+
* You can always use {@link Kysely.raw} to call functions and build arbitrary
|
|
11
|
+
* expressions. This module simply has shortcuts for most common function calls.
|
|
12
|
+
*
|
|
10
13
|
* ### Examples
|
|
11
14
|
*
|
|
12
15
|
* ```ts
|
|
@@ -45,15 +48,36 @@ class FunctionBuilder {
|
|
|
45
48
|
}
|
|
46
49
|
/**
|
|
47
50
|
* Calls the `max` function for the column given as the argument.
|
|
51
|
+
*
|
|
52
|
+
* ### Examples
|
|
53
|
+
*
|
|
54
|
+
* ```ts
|
|
55
|
+
* const { max } = db.fn
|
|
56
|
+
*
|
|
57
|
+
* db.selectFrom('toy')
|
|
58
|
+
* .select(max('price').as('max_price'))
|
|
59
|
+
* .execute()
|
|
60
|
+
* ```
|
|
48
61
|
*/
|
|
49
|
-
|
|
50
|
-
return this.#oneArgFunction('
|
|
62
|
+
max(column) {
|
|
63
|
+
return this.#oneArgFunction('max', column);
|
|
51
64
|
}
|
|
52
65
|
/**
|
|
53
66
|
* Calls the `min` function for the column given as the argument.
|
|
67
|
+
*
|
|
68
|
+
* ### Examples
|
|
69
|
+
*
|
|
70
|
+
* ```ts
|
|
71
|
+
* const { min } = db.fn
|
|
72
|
+
*
|
|
73
|
+
* db.selectFrom('toy')
|
|
74
|
+
* .select(min('price').as('min_price'))
|
|
75
|
+
* .execute()
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
54
78
|
*/
|
|
55
|
-
|
|
56
|
-
return this.#oneArgFunction('
|
|
79
|
+
min(column) {
|
|
80
|
+
return this.#oneArgFunction('min', column);
|
|
57
81
|
}
|
|
58
82
|
/**
|
|
59
83
|
* Calls the `avg` function for the column given as the argument.
|
|
@@ -72,7 +96,7 @@ class FunctionBuilder {
|
|
|
72
96
|
* const { avg } = db.fn
|
|
73
97
|
*
|
|
74
98
|
* db.selectFrom('toy')
|
|
75
|
-
* .select(avg<number>('price'))
|
|
99
|
+
* .select(avg<number>('price').as('avg_price'))
|
|
76
100
|
* .execute()
|
|
77
101
|
* ```
|
|
78
102
|
*/
|
|
@@ -96,7 +120,7 @@ class FunctionBuilder {
|
|
|
96
120
|
* const { sum } = db.fn
|
|
97
121
|
*
|
|
98
122
|
* db.selectFrom('toy')
|
|
99
|
-
* .select(sum<number>('price'))
|
|
123
|
+
* .select(sum<number>('price').as('total_price'))
|
|
100
124
|
* .execute()
|
|
101
125
|
* ```
|
|
102
126
|
*/
|
|
@@ -120,7 +144,7 @@ class FunctionBuilder {
|
|
|
120
144
|
* const { count } = db.fn
|
|
121
145
|
*
|
|
122
146
|
* db.selectFrom('toy')
|
|
123
|
-
* .select(count<number>('
|
|
147
|
+
* .select(count<number>('id').as('num_toys'))
|
|
124
148
|
* .execute()
|
|
125
149
|
* ```
|
|
126
150
|
*/
|
|
@@ -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;
|