kysely 0.16.6 → 0.16.10
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/LICENSE +21 -0
- 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 +28 -0
- package/dist/cjs/driver/dummy-driver.js +31 -3
- package/dist/cjs/index-nodeless.d.ts +1 -0
- package/dist/cjs/index-nodeless.js +1 -0
- package/dist/cjs/kysely.d.ts +8 -6
- package/dist/cjs/kysely.js +10 -8
- 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 +52 -12
- 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 +54 -14
- package/dist/cjs/query-builder/insert-query-builder.js +58 -10
- package/dist/cjs/query-builder/returning-interface.d.ts +1 -3
- package/dist/cjs/query-builder/select-query-builder.d.ts +65 -24
- package/dist/cjs/query-builder/select-query-builder.js +64 -15
- package/dist/cjs/query-builder/update-query-builder.d.ts +53 -12
- package/dist/cjs/query-builder/update-query-builder.js +57 -8
- package/dist/cjs/query-compiler/default-query-compiler.js +8 -7
- 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.js +3 -3
- package/dist/cjs/schema/create-table-builder.js +2 -2
- package/dist/cjs/schema/create-view-builder.js +1 -1
- 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 +28 -0
- package/dist/esm/driver/dummy-driver.js +31 -3
- package/dist/esm/index-nodeless.d.ts +1 -0
- package/dist/esm/index-nodeless.js +1 -0
- package/dist/esm/kysely.d.ts +8 -6
- package/dist/esm/kysely.js +10 -8
- 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 +52 -12
- 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 +54 -14
- package/dist/esm/query-builder/insert-query-builder.js +58 -10
- package/dist/esm/query-builder/returning-interface.d.ts +1 -3
- package/dist/esm/query-builder/select-query-builder.d.ts +65 -24
- package/dist/esm/query-builder/select-query-builder.js +64 -15
- package/dist/esm/query-builder/update-query-builder.d.ts +53 -12
- package/dist/esm/query-builder/update-query-builder.js +57 -8
- package/dist/esm/query-compiler/default-query-compiler.js +8 -7
- 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.js +3 -3
- package/dist/esm/schema/create-table-builder.js +2 -2
- package/dist/esm/schema/create-view-builder.js +1 -1
- 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 +4 -2
|
@@ -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 {
|
|
@@ -563,8 +564,6 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
563
564
|
* Return arbitrary expressions:
|
|
564
565
|
*
|
|
565
566
|
* ```ts
|
|
566
|
-
* const { raw } = db
|
|
567
|
-
*
|
|
568
567
|
* const { id, full_name, first_pet_id } = await db
|
|
569
568
|
* .insertInto('person')
|
|
570
569
|
* .values({
|
|
@@ -573,7 +572,7 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
573
572
|
* })
|
|
574
573
|
* .returning([
|
|
575
574
|
* 'id as id',
|
|
576
|
-
* raw<string>(`concat(first_name, ' ', last_name)`).as('full_name'),
|
|
575
|
+
* db.raw<string>(`concat(first_name, ' ', last_name)`).as('full_name'),
|
|
577
576
|
* (qb) => qb.selectFrom('pets').select('pet.id').limit(1).as('first_pet_id')
|
|
578
577
|
* ])
|
|
579
578
|
* .executeTakeFirst()
|
|
@@ -589,23 +588,61 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
589
588
|
/**
|
|
590
589
|
* Simply calls the given function passing `this` as the only argument.
|
|
591
590
|
*
|
|
592
|
-
*
|
|
591
|
+
* If you want to conditionally call a method on `this`, see
|
|
592
|
+
* the {@link if} method.
|
|
593
593
|
*
|
|
594
594
|
* ### Examples
|
|
595
595
|
*
|
|
596
|
+
* The next example uses a helper funtion `log` to log a query:
|
|
597
|
+
*
|
|
596
598
|
* ```ts
|
|
599
|
+
* function log<T extends Compilable>(qb: T): T {
|
|
600
|
+
* console.log(qb.compile())
|
|
601
|
+
* return qb
|
|
602
|
+
* }
|
|
603
|
+
*
|
|
597
604
|
* db.deleteFrom('person')
|
|
598
|
-
* .call(
|
|
599
|
-
* if (something) {
|
|
600
|
-
* return qb.where('something', '=', something)
|
|
601
|
-
* } else {
|
|
602
|
-
* return qb.where('somethingElse', '=', somethingElse)
|
|
603
|
-
* }
|
|
604
|
-
* })
|
|
605
|
+
* .call(log)
|
|
605
606
|
* .execute()
|
|
606
607
|
* ```
|
|
607
608
|
*/
|
|
608
609
|
call<T>(func: (qb: this) => T): T;
|
|
610
|
+
/**
|
|
611
|
+
* Call `func(this)` if `condition` is true.
|
|
612
|
+
*
|
|
613
|
+
* This method is especially handy with optional selects. Any `returning` or `returningAll`
|
|
614
|
+
* method calls add columns as optional fields to the output type when called inside
|
|
615
|
+
* the `func` callback. This is because we can't know if those selections were actually
|
|
616
|
+
* made before running the code.
|
|
617
|
+
*
|
|
618
|
+
* You can also call any other methods inside the callback.
|
|
619
|
+
*
|
|
620
|
+
* ### Examples
|
|
621
|
+
*
|
|
622
|
+
* ```ts
|
|
623
|
+
* async function deletePerson(id: number, returnLastName: boolean) {
|
|
624
|
+
* return await db
|
|
625
|
+
* .deleteFrom('person')
|
|
626
|
+
* .where('id', '=', id)
|
|
627
|
+
* .returning(['id', 'first_name'])
|
|
628
|
+
* .if(returnLastName, (qb) => qb.returning('last_name'))
|
|
629
|
+
* .executeTakeFirstOrThrow()
|
|
630
|
+
* }
|
|
631
|
+
* ```
|
|
632
|
+
*
|
|
633
|
+
* Any selections added inside the `if` callback will be added as optional fields to the
|
|
634
|
+
* output type since we can't know if the selections were actually made before running
|
|
635
|
+
* the code. In the example above the return type of the `deletePerson` function is:
|
|
636
|
+
*
|
|
637
|
+
* ```ts
|
|
638
|
+
* {
|
|
639
|
+
* id: number
|
|
640
|
+
* first_name: string
|
|
641
|
+
* last_name?: string
|
|
642
|
+
* }
|
|
643
|
+
* ```
|
|
644
|
+
*/
|
|
645
|
+
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
646
|
/**
|
|
610
647
|
* Change the output type of the query.
|
|
611
648
|
*
|
|
@@ -613,6 +650,9 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
613
650
|
* don't support your use case.
|
|
614
651
|
*/
|
|
615
652
|
castTo<T>(): DeleteQueryBuilder<DB, TB, T>;
|
|
653
|
+
/**
|
|
654
|
+
* Returns a copy of this DeleteQueryBuilder instance with the given plugin installed.
|
|
655
|
+
*/
|
|
616
656
|
withPlugin(plugin: KyselyPlugin): DeleteQueryBuilder<DB, TB, O>;
|
|
617
657
|
toOperationNode(): DeleteQueryNode;
|
|
618
658
|
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';
|
|
@@ -422,8 +423,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
422
423
|
* Return arbitrary expressions:
|
|
423
424
|
*
|
|
424
425
|
* ```ts
|
|
425
|
-
* const { raw } = db
|
|
426
|
-
*
|
|
427
426
|
* const { id, full_name, first_pet_id } = await db
|
|
428
427
|
* .insertInto('person')
|
|
429
428
|
* .values({
|
|
@@ -432,7 +431,7 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
432
431
|
* })
|
|
433
432
|
* .returning([
|
|
434
433
|
* 'id as id',
|
|
435
|
-
* raw<string>(`concat(first_name, ' ', last_name)`).as('full_name'),
|
|
434
|
+
* db.raw<string>(`concat(first_name, ' ', last_name)`).as('full_name'),
|
|
436
435
|
* (qb) => qb.selectFrom('pets').select('pet.id').limit(1).as('first_pet_id')
|
|
437
436
|
* ])
|
|
438
437
|
* .executeTakeFirst()
|
|
@@ -448,24 +447,62 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
448
447
|
/**
|
|
449
448
|
* Simply calls the given function passing `this` as the only argument.
|
|
450
449
|
*
|
|
451
|
-
*
|
|
450
|
+
* If you want to conditionally call a method on `this`, see
|
|
451
|
+
* the {@link if} method.
|
|
452
452
|
*
|
|
453
453
|
* ### Examples
|
|
454
454
|
*
|
|
455
|
+
* The next example uses a helper funtion `log` to log a query:
|
|
456
|
+
*
|
|
455
457
|
* ```ts
|
|
456
|
-
*
|
|
457
|
-
* .
|
|
458
|
-
*
|
|
459
|
-
*
|
|
460
|
-
*
|
|
461
|
-
*
|
|
462
|
-
*
|
|
463
|
-
*
|
|
464
|
-
* })
|
|
458
|
+
* function log<T extends Compilable>(qb: T): T {
|
|
459
|
+
* console.log(qb.compile())
|
|
460
|
+
* return qb
|
|
461
|
+
* }
|
|
462
|
+
*
|
|
463
|
+
* db.updateTable('person')
|
|
464
|
+
* .set(values)
|
|
465
|
+
* .call(log)
|
|
465
466
|
* .execute()
|
|
466
467
|
* ```
|
|
467
468
|
*/
|
|
468
469
|
call<T>(func: (qb: this) => T): T;
|
|
470
|
+
/**
|
|
471
|
+
* Call `func(this)` if `condition` is true.
|
|
472
|
+
*
|
|
473
|
+
* This method is especially handy with optional selects. Any `returning` or `returningAll`
|
|
474
|
+
* method calls add columns as optional fields to the output type when called inside
|
|
475
|
+
* the `func` callback. This is because we can't know if those selections were actually
|
|
476
|
+
* made before running the code.
|
|
477
|
+
*
|
|
478
|
+
* You can also call any other methods inside the callback.
|
|
479
|
+
*
|
|
480
|
+
* ### Examples
|
|
481
|
+
*
|
|
482
|
+
* ```ts
|
|
483
|
+
* async function insertPerson(values: InsertablePerson, returnLastName: boolean) {
|
|
484
|
+
* return await db
|
|
485
|
+
* .insertInto('person')
|
|
486
|
+
* .values(values)
|
|
487
|
+
* .returning(['id', 'first_name'])
|
|
488
|
+
* .if(returnLastName, (qb) => qb.returning('last_name'))
|
|
489
|
+
* .executeTakeFirstOrThrow()
|
|
490
|
+
* }
|
|
491
|
+
* ```
|
|
492
|
+
*
|
|
493
|
+
* Any selections added inside the `if` callback will be added as optional fields to the
|
|
494
|
+
* output type since we can't know if the selections were actually made before running
|
|
495
|
+
* the code. In the example above the return type of the `insertPerson` function is:
|
|
496
|
+
*
|
|
497
|
+
* ```ts
|
|
498
|
+
* {
|
|
499
|
+
* id: number
|
|
500
|
+
* first_name: string
|
|
501
|
+
* last_name?: string
|
|
502
|
+
* }
|
|
503
|
+
* ```
|
|
504
|
+
*/
|
|
505
|
+
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
506
|
/**
|
|
470
507
|
* Change the output type of the query.
|
|
471
508
|
*
|
|
@@ -473,6 +510,9 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
473
510
|
* don't support your use case.
|
|
474
511
|
*/
|
|
475
512
|
castTo<T>(): InsertQueryBuilder<DB, TB, T>;
|
|
513
|
+
/**
|
|
514
|
+
* Returns a copy of this InsertQueryBuilder instance with the given plugin installed.
|
|
515
|
+
*/
|
|
476
516
|
withPlugin(plugin: KyselyPlugin): InsertQueryBuilder<DB, TB, O>;
|
|
477
517
|
toOperationNode(): InsertQueryNode;
|
|
478
518
|
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,
|
|
@@ -45,8 +45,6 @@ export interface ReturningInterface<DB, TB extends keyof DB, O> {
|
|
|
45
45
|
* Return arbitrary expressions:
|
|
46
46
|
*
|
|
47
47
|
* ```ts
|
|
48
|
-
* const { raw } = db
|
|
49
|
-
*
|
|
50
48
|
* const { id, full_name, first_pet_id } = await db
|
|
51
49
|
* .insertInto('person')
|
|
52
50
|
* .values({
|
|
@@ -55,7 +53,7 @@ export interface ReturningInterface<DB, TB extends keyof DB, O> {
|
|
|
55
53
|
* })
|
|
56
54
|
* .returning([
|
|
57
55
|
* 'id as id',
|
|
58
|
-
* raw<string>(`concat(first_name, ' ', last_name)`).as('full_name'),
|
|
56
|
+
* db.raw<string>(`concat(first_name, ' ', last_name)`).as('full_name'),
|
|
59
57
|
* (qb) => qb.selectFrom('pets').select('pet.id').limit(1).as('first_pet_id')
|
|
60
58
|
* ])
|
|
61
59
|
* .executeTakeFirst()
|