kysely 0.26.1 → 0.26.3
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 +6 -6
- package/dist/cjs/dialect/dialect-adapter-base.d.ts +3 -3
- package/dist/cjs/dialect/mysql/mysql-adapter.d.ts +3 -2
- package/dist/cjs/dialect/mysql/mysql-adapter.js +2 -2
- package/dist/cjs/dialect/postgres/postgres-adapter.d.ts +3 -2
- package/dist/cjs/dialect/postgres/postgres-adapter.js +2 -2
- package/dist/cjs/dialect/postgres/postgres-dialect-config.d.ts +2 -2
- package/dist/cjs/dialect/sqlite/sqlite-adapter.d.ts +4 -2
- package/dist/cjs/dialect/sqlite/sqlite-adapter.js +2 -2
- package/dist/cjs/driver/runtime-driver.js +17 -2
- package/dist/cjs/expression/expression-builder.d.ts +300 -27
- package/dist/cjs/expression/expression-builder.js +36 -26
- package/dist/cjs/expression/expression-wrapper.d.ts +177 -8
- package/dist/cjs/expression/expression-wrapper.js +4 -4
- package/dist/cjs/expression/expression.d.ts +64 -1
- package/dist/cjs/helpers/mysql.d.ts +14 -11
- package/dist/cjs/helpers/mysql.js +21 -32
- package/dist/cjs/helpers/postgres.d.ts +8 -5
- package/dist/cjs/helpers/postgres.js +8 -5
- package/dist/cjs/helpers/sqlite.d.ts +164 -0
- package/dist/cjs/helpers/sqlite.js +180 -0
- package/dist/cjs/index.d.ts +5 -3
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/kysely.d.ts +4 -3
- package/dist/cjs/kysely.js +1 -1
- package/dist/cjs/operation-node/common-table-expression-node.d.ts +4 -0
- package/dist/cjs/operation-node/common-table-expression-node.js +6 -0
- package/dist/cjs/operation-node/delete-query-node.d.ts +1 -1
- package/dist/cjs/operation-node/delete-query-node.js +3 -3
- package/dist/cjs/operation-node/operation-node-transformer.d.ts +2 -0
- package/dist/cjs/operation-node/operation-node-transformer.js +8 -0
- package/dist/cjs/operation-node/operation-node-visitor.d.ts +2 -0
- package/dist/cjs/operation-node/operation-node-visitor.js +1 -0
- package/dist/cjs/operation-node/operation-node.d.ts +1 -1
- package/dist/cjs/operation-node/operator-node.d.ts +3 -3
- package/dist/cjs/operation-node/operator-node.js +3 -0
- package/dist/cjs/operation-node/order-by-node.d.ts +2 -2
- package/dist/cjs/operation-node/order-by-node.js +4 -4
- package/dist/cjs/operation-node/over-node.d.ts +1 -1
- package/dist/cjs/operation-node/over-node.js +3 -3
- package/dist/cjs/operation-node/select-query-node.d.ts +5 -4
- package/dist/cjs/operation-node/select-query-node.js +13 -7
- package/dist/cjs/operation-node/tuple-node.d.ts +12 -0
- package/dist/cjs/operation-node/tuple-node.js +18 -0
- package/dist/cjs/parser/binary-operation-parser.d.ts +11 -7
- package/dist/cjs/parser/binary-operation-parser.js +35 -46
- package/dist/cjs/parser/expression-parser.d.ts +10 -5
- package/dist/cjs/parser/join-parser.d.ts +2 -2
- package/dist/cjs/parser/join-parser.js +2 -3
- package/dist/cjs/parser/order-by-parser.d.ts +6 -2
- package/dist/cjs/parser/order-by-parser.js +41 -7
- package/dist/cjs/parser/parse-utils.js +2 -2
- package/dist/cjs/parser/reference-parser.d.ts +2 -2
- package/dist/cjs/parser/reference-parser.js +1 -1
- package/dist/cjs/parser/returning-parser.d.ts +2 -1
- package/dist/cjs/parser/select-parser.d.ts +20 -9
- package/dist/cjs/parser/set-operation-parser.d.ts +3 -1
- package/dist/cjs/parser/set-operation-parser.js +13 -4
- package/dist/cjs/parser/tuple-parser.d.ts +47 -0
- package/dist/cjs/parser/tuple-parser.js +2 -0
- package/dist/cjs/parser/value-parser.d.ts +5 -3
- package/dist/cjs/parser/value-parser.js +12 -1
- package/dist/cjs/parser/with-parser.d.ts +3 -2
- package/dist/cjs/parser/with-parser.js +16 -4
- package/dist/cjs/plugin/camel-case/camel-case-plugin.d.ts +3 -3
- package/dist/cjs/plugin/camel-case/camel-case-plugin.js +4 -8
- package/dist/cjs/plugin/parse-json-results/parse-json-results-plugin.d.ts +13 -0
- package/dist/cjs/plugin/parse-json-results/parse-json-results-plugin.js +50 -14
- package/dist/cjs/query-builder/aggregate-function-builder.d.ts +5 -5
- package/dist/cjs/query-builder/aggregate-function-builder.js +2 -2
- package/dist/cjs/query-builder/case-builder.js +8 -4
- package/dist/cjs/query-builder/cte-builder.d.ts +20 -0
- package/dist/cjs/query-builder/cte-builder.js +39 -0
- package/dist/cjs/query-builder/delete-query-builder.d.ts +39 -10
- package/dist/cjs/query-builder/delete-query-builder.js +3 -3
- package/dist/cjs/query-builder/function-module.d.ts +41 -6
- package/dist/cjs/query-builder/function-module.js +3 -0
- package/dist/cjs/query-builder/having-interface.d.ts +2 -5
- package/dist/cjs/query-builder/insert-query-builder.d.ts +5 -3
- package/dist/cjs/query-builder/join-builder.d.ts +2 -6
- package/dist/cjs/query-builder/join-builder.js +2 -2
- package/dist/cjs/query-builder/json-path-builder.d.ts +59 -3
- package/dist/cjs/query-builder/on-conflict-builder.d.ts +65 -12
- package/dist/cjs/query-builder/on-conflict-builder.js +4 -4
- package/dist/cjs/query-builder/over-builder.js +1 -1
- package/dist/cjs/query-builder/returning-interface.d.ts +3 -2
- package/dist/cjs/query-builder/select-query-builder-expression.d.ts +20 -0
- package/dist/cjs/query-builder/select-query-builder-expression.js +2 -0
- package/dist/cjs/query-builder/select-query-builder.d.ts +206 -67
- package/dist/cjs/query-builder/select-query-builder.js +75 -924
- package/dist/cjs/query-builder/update-query-builder.d.ts +39 -10
- package/dist/cjs/query-builder/update-query-builder.js +2 -2
- package/dist/cjs/query-builder/where-interface.d.ts +32 -8
- package/dist/cjs/query-compiler/default-query-compiler.d.ts +3 -0
- package/dist/cjs/query-compiler/default-query-compiler.js +39 -7
- package/dist/cjs/query-creator.d.ts +76 -2
- package/dist/cjs/query-creator.js +36 -6
- package/dist/cjs/raw-builder/raw-builder.d.ts +87 -51
- package/dist/cjs/raw-builder/raw-builder.js +18 -45
- package/dist/cjs/raw-builder/sql.js +8 -8
- package/dist/cjs/schema/alter-table-builder.d.ts +4 -0
- package/dist/cjs/schema/alter-table-builder.js +12 -0
- package/dist/cjs/schema/create-index-builder.d.ts +1 -1
- package/dist/cjs/schema/create-index-builder.js +1 -1
- package/dist/cjs/schema/create-view-builder.d.ts +2 -2
- package/dist/cjs/util/column-type.d.ts +7 -6
- package/dist/cjs/util/infer-result.d.ts +2 -1
- package/dist/cjs/util/json-object-args.d.ts +3 -0
- package/dist/cjs/util/json-object-args.js +35 -0
- package/dist/cjs/util/object-utils.d.ts +1 -0
- package/dist/cjs/util/object-utils.js +9 -1
- package/dist/cjs/util/type-utils.d.ts +16 -17
- package/dist/esm/dialect/dialect-adapter-base.d.ts +3 -3
- package/dist/esm/dialect/mysql/mysql-adapter.d.ts +3 -2
- package/dist/esm/dialect/mysql/mysql-adapter.js +2 -2
- package/dist/esm/dialect/postgres/postgres-adapter.d.ts +3 -2
- package/dist/esm/dialect/postgres/postgres-adapter.js +2 -2
- package/dist/esm/dialect/postgres/postgres-dialect-config.d.ts +2 -2
- package/dist/esm/dialect/sqlite/sqlite-adapter.d.ts +4 -2
- package/dist/esm/dialect/sqlite/sqlite-adapter.js +2 -2
- package/dist/esm/driver/runtime-driver.js +17 -2
- package/dist/esm/expression/expression-builder.d.ts +300 -27
- package/dist/esm/expression/expression-builder.js +40 -30
- package/dist/esm/expression/expression-wrapper.d.ts +177 -8
- package/dist/esm/expression/expression-wrapper.js +5 -5
- package/dist/esm/expression/expression.d.ts +64 -1
- package/dist/esm/helpers/mysql.d.ts +14 -11
- package/dist/esm/helpers/mysql.js +21 -32
- package/dist/esm/helpers/postgres.d.ts +8 -5
- package/dist/esm/helpers/postgres.js +8 -5
- package/dist/esm/helpers/sqlite.d.ts +164 -0
- package/dist/esm/helpers/sqlite.js +175 -0
- package/dist/esm/index.d.ts +5 -3
- package/dist/esm/index.js +1 -0
- package/dist/esm/kysely.d.ts +4 -3
- package/dist/esm/kysely.js +1 -1
- package/dist/esm/operation-node/common-table-expression-node.d.ts +4 -0
- package/dist/esm/operation-node/common-table-expression-node.js +6 -0
- package/dist/esm/operation-node/delete-query-node.d.ts +1 -1
- package/dist/esm/operation-node/delete-query-node.js +3 -3
- package/dist/esm/operation-node/operation-node-transformer.d.ts +2 -0
- package/dist/esm/operation-node/operation-node-transformer.js +8 -0
- package/dist/esm/operation-node/operation-node-visitor.d.ts +2 -0
- package/dist/esm/operation-node/operation-node-visitor.js +1 -0
- package/dist/esm/operation-node/operation-node.d.ts +1 -1
- package/dist/esm/operation-node/operator-node.d.ts +3 -3
- package/dist/esm/operation-node/operator-node.js +3 -0
- package/dist/esm/operation-node/order-by-node.d.ts +2 -2
- package/dist/esm/operation-node/order-by-node.js +4 -4
- package/dist/esm/operation-node/over-node.d.ts +1 -1
- package/dist/esm/operation-node/over-node.js +3 -3
- package/dist/esm/operation-node/select-query-node.d.ts +5 -4
- package/dist/esm/operation-node/select-query-node.js +13 -7
- package/dist/esm/operation-node/tuple-node.d.ts +12 -0
- package/dist/esm/operation-node/tuple-node.js +16 -0
- package/dist/esm/parser/binary-operation-parser.d.ts +11 -7
- package/dist/esm/parser/binary-operation-parser.js +36 -47
- package/dist/esm/parser/expression-parser.d.ts +10 -5
- package/dist/esm/parser/join-parser.d.ts +2 -2
- package/dist/esm/parser/join-parser.js +3 -4
- package/dist/esm/parser/order-by-parser.d.ts +6 -2
- package/dist/esm/parser/order-by-parser.js +40 -7
- package/dist/esm/parser/parse-utils.js +3 -3
- package/dist/esm/parser/reference-parser.d.ts +2 -2
- package/dist/esm/parser/reference-parser.js +1 -1
- package/dist/esm/parser/returning-parser.d.ts +2 -1
- package/dist/esm/parser/select-parser.d.ts +20 -9
- package/dist/esm/parser/set-operation-parser.d.ts +3 -1
- package/dist/esm/parser/set-operation-parser.js +11 -2
- package/dist/esm/parser/tuple-parser.d.ts +47 -0
- package/dist/esm/parser/tuple-parser.js +2 -0
- package/dist/esm/parser/value-parser.d.ts +5 -3
- package/dist/esm/parser/value-parser.js +10 -1
- package/dist/esm/parser/with-parser.d.ts +3 -2
- package/dist/esm/parser/with-parser.js +16 -4
- package/dist/esm/plugin/camel-case/camel-case-plugin.d.ts +3 -3
- package/dist/esm/plugin/camel-case/camel-case-plugin.js +5 -9
- package/dist/esm/plugin/parse-json-results/parse-json-results-plugin.d.ts +13 -0
- package/dist/esm/plugin/parse-json-results/parse-json-results-plugin.js +51 -15
- package/dist/esm/query-builder/aggregate-function-builder.d.ts +5 -5
- package/dist/esm/query-builder/aggregate-function-builder.js +3 -3
- package/dist/esm/query-builder/case-builder.js +10 -6
- package/dist/esm/query-builder/cte-builder.d.ts +20 -0
- package/dist/esm/query-builder/cte-builder.js +36 -0
- package/dist/esm/query-builder/delete-query-builder.d.ts +39 -10
- package/dist/esm/query-builder/delete-query-builder.js +4 -4
- package/dist/esm/query-builder/function-module.d.ts +41 -6
- package/dist/esm/query-builder/function-module.js +3 -0
- package/dist/esm/query-builder/having-interface.d.ts +2 -5
- package/dist/esm/query-builder/insert-query-builder.d.ts +5 -3
- package/dist/esm/query-builder/join-builder.d.ts +2 -6
- package/dist/esm/query-builder/join-builder.js +3 -3
- package/dist/esm/query-builder/json-path-builder.d.ts +59 -3
- package/dist/esm/query-builder/on-conflict-builder.d.ts +65 -12
- package/dist/esm/query-builder/on-conflict-builder.js +5 -5
- package/dist/esm/query-builder/over-builder.js +1 -1
- package/dist/esm/query-builder/returning-interface.d.ts +3 -2
- package/dist/esm/query-builder/select-query-builder-expression.d.ts +20 -0
- package/dist/esm/query-builder/select-query-builder-expression.js +2 -0
- package/dist/esm/query-builder/select-query-builder.d.ts +206 -67
- package/dist/esm/query-builder/select-query-builder.js +75 -923
- package/dist/esm/query-builder/update-query-builder.d.ts +39 -10
- package/dist/esm/query-builder/update-query-builder.js +3 -3
- package/dist/esm/query-builder/where-interface.d.ts +32 -8
- package/dist/esm/query-compiler/default-query-compiler.d.ts +3 -0
- package/dist/esm/query-compiler/default-query-compiler.js +39 -7
- package/dist/esm/query-creator.d.ts +76 -2
- package/dist/esm/query-creator.js +37 -7
- package/dist/esm/raw-builder/raw-builder.d.ts +87 -51
- package/dist/esm/raw-builder/raw-builder.js +17 -42
- package/dist/esm/raw-builder/sql.js +9 -9
- package/dist/esm/schema/alter-table-builder.d.ts +4 -0
- package/dist/esm/schema/alter-table-builder.js +12 -0
- package/dist/esm/schema/create-index-builder.d.ts +1 -1
- package/dist/esm/schema/create-index-builder.js +2 -2
- package/dist/esm/schema/create-view-builder.d.ts +2 -2
- package/dist/esm/util/column-type.d.ts +7 -6
- package/dist/esm/util/infer-result.d.ts +2 -1
- package/dist/esm/util/json-object-args.d.ts +3 -0
- package/dist/esm/util/json-object-args.js +32 -0
- package/dist/esm/util/object-utils.d.ts +1 -0
- package/dist/esm/util/object-utils.js +7 -0
- package/dist/esm/util/type-utils.d.ts +16 -17
- package/helpers/sqlite.d.ts +1 -0
- package/helpers/sqlite.js +1 -0
- package/package.json +7 -4
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.createSelectQueryBuilder = void 0;
|
|
4
4
|
const alias_node_js_1 = require("../operation-node/alias-node.js");
|
|
5
5
|
const select_modifier_node_js_1 = require("../operation-node/select-modifier-node.js");
|
|
6
6
|
const join_parser_js_1 = require("../parser/join-parser.js");
|
|
@@ -18,1104 +18,268 @@ const no_result_error_js_1 = require("./no-result-error.js");
|
|
|
18
18
|
const identifier_node_js_1 = require("../operation-node/identifier-node.js");
|
|
19
19
|
const set_operation_parser_js_1 = require("../parser/set-operation-parser.js");
|
|
20
20
|
const binary_operation_parser_js_1 = require("../parser/binary-operation-parser.js");
|
|
21
|
-
|
|
21
|
+
const expression_wrapper_js_1 = require("../expression/expression-wrapper.js");
|
|
22
|
+
class SelectQueryBuilderImpl {
|
|
22
23
|
#props;
|
|
23
24
|
constructor(props) {
|
|
24
25
|
this.#props = (0, object_utils_js_1.freeze)(props);
|
|
25
26
|
}
|
|
26
|
-
/** @private */
|
|
27
27
|
get expressionType() {
|
|
28
28
|
return undefined;
|
|
29
29
|
}
|
|
30
|
+
get isSelectQueryBuilder() {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
30
33
|
where(...args) {
|
|
31
|
-
return new
|
|
34
|
+
return new SelectQueryBuilderImpl({
|
|
32
35
|
...this.#props,
|
|
33
|
-
queryNode: query_node_js_1.QueryNode.cloneWithWhere(this.#props.queryNode, (0, binary_operation_parser_js_1.
|
|
36
|
+
queryNode: query_node_js_1.QueryNode.cloneWithWhere(this.#props.queryNode, (0, binary_operation_parser_js_1.parseValueBinaryOperationOrExpression)(args)),
|
|
34
37
|
});
|
|
35
38
|
}
|
|
36
39
|
whereRef(lhs, op, rhs) {
|
|
37
|
-
return new
|
|
40
|
+
return new SelectQueryBuilderImpl({
|
|
38
41
|
...this.#props,
|
|
39
|
-
queryNode: query_node_js_1.QueryNode.cloneWithWhere(this.#props.queryNode, (0, binary_operation_parser_js_1.
|
|
42
|
+
queryNode: query_node_js_1.QueryNode.cloneWithWhere(this.#props.queryNode, (0, binary_operation_parser_js_1.parseReferentialBinaryOperation)(lhs, op, rhs)),
|
|
40
43
|
});
|
|
41
44
|
}
|
|
42
45
|
having(...args) {
|
|
43
|
-
return new
|
|
46
|
+
return new SelectQueryBuilderImpl({
|
|
44
47
|
...this.#props,
|
|
45
|
-
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithHaving(this.#props.queryNode, (0, binary_operation_parser_js_1.
|
|
48
|
+
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithHaving(this.#props.queryNode, (0, binary_operation_parser_js_1.parseValueBinaryOperationOrExpression)(args)),
|
|
46
49
|
});
|
|
47
50
|
}
|
|
48
51
|
havingRef(lhs, op, rhs) {
|
|
49
|
-
return new
|
|
52
|
+
return new SelectQueryBuilderImpl({
|
|
50
53
|
...this.#props,
|
|
51
|
-
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithHaving(this.#props.queryNode, (0, binary_operation_parser_js_1.
|
|
54
|
+
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithHaving(this.#props.queryNode, (0, binary_operation_parser_js_1.parseReferentialBinaryOperation)(lhs, op, rhs)),
|
|
52
55
|
});
|
|
53
56
|
}
|
|
54
|
-
/**
|
|
55
|
-
* Adds a select statement to the query.
|
|
56
|
-
*
|
|
57
|
-
* When a column (or any expression) is selected, Kysely adds its type to the return
|
|
58
|
-
* type of the query. Kysely is smart enough to parse the selection names and types
|
|
59
|
-
* from aliased columns, subqueries, raw expressions etc.
|
|
60
|
-
*
|
|
61
|
-
* Kysely only allows you to select columns and expressions that exist and would
|
|
62
|
-
* produce valid SQL. However, Kysely is not perfect and there may be cases where
|
|
63
|
-
* the type inference doesn't work and you need to override it. You can always
|
|
64
|
-
* use the {@link Kysely.dynamic | dynamic} module and the {@link sql} tag
|
|
65
|
-
* to override the types.
|
|
66
|
-
*
|
|
67
|
-
* Select calls are additive. Calling `select('id').select('first_name')` is the
|
|
68
|
-
* same as calling `select(['id', 'first_name'])`.
|
|
69
|
-
*
|
|
70
|
-
* To select all columns of the query or specific tables see the
|
|
71
|
-
* {@link selectAll} method.
|
|
72
|
-
*
|
|
73
|
-
* See the {@link $if} method if you are looking for a way to add selections
|
|
74
|
-
* based on a runtime condition.
|
|
75
|
-
*
|
|
76
|
-
* ### Examples
|
|
77
|
-
*
|
|
78
|
-
* <!-- siteExample("select", "A single column", 10) -->
|
|
79
|
-
*
|
|
80
|
-
* Select a single column:
|
|
81
|
-
*
|
|
82
|
-
* ```ts
|
|
83
|
-
* const persons = await db
|
|
84
|
-
* .selectFrom('person')
|
|
85
|
-
* .select('id')
|
|
86
|
-
* .where('first_name', '=', 'Arnold')
|
|
87
|
-
* .execute()
|
|
88
|
-
* ```
|
|
89
|
-
*
|
|
90
|
-
* The generated SQL (PostgreSQL):
|
|
91
|
-
*
|
|
92
|
-
* ```sql
|
|
93
|
-
* select "id" from "person" where "first_name" = $1
|
|
94
|
-
* ```
|
|
95
|
-
*
|
|
96
|
-
* <!-- siteExample("select", "Column with a table", 20) -->
|
|
97
|
-
*
|
|
98
|
-
* Select a single column and specify a table:
|
|
99
|
-
*
|
|
100
|
-
* ```ts
|
|
101
|
-
* const persons = await db
|
|
102
|
-
* .selectFrom(['person', 'pet'])
|
|
103
|
-
* .select('person.id')
|
|
104
|
-
* .execute()
|
|
105
|
-
* ```
|
|
106
|
-
*
|
|
107
|
-
* The generated SQL (PostgreSQL):
|
|
108
|
-
*
|
|
109
|
-
* ```sql
|
|
110
|
-
* select "person"."id" from "person", "pet"
|
|
111
|
-
* ```
|
|
112
|
-
*
|
|
113
|
-
* <!-- siteExample("select", "Multiple columns", 30) -->
|
|
114
|
-
*
|
|
115
|
-
* Select multiple columns:
|
|
116
|
-
*
|
|
117
|
-
* ```ts
|
|
118
|
-
* const persons = await db
|
|
119
|
-
* .selectFrom('person')
|
|
120
|
-
* .select(['person.id', 'first_name'])
|
|
121
|
-
* .execute()
|
|
122
|
-
* ```
|
|
123
|
-
*
|
|
124
|
-
* The generated SQL (PostgreSQL):
|
|
125
|
-
*
|
|
126
|
-
* ```sql
|
|
127
|
-
* select "person"."id", "first_name" from "person"
|
|
128
|
-
* ```
|
|
129
|
-
*
|
|
130
|
-
* <!-- siteExample("select", "Aliases", 40) -->
|
|
131
|
-
*
|
|
132
|
-
* You can provide an alias for the selections by appending `as the_alias` to the selection.
|
|
133
|
-
*
|
|
134
|
-
* ```ts
|
|
135
|
-
* const persons = await db
|
|
136
|
-
* .selectFrom('person')
|
|
137
|
-
* .select([
|
|
138
|
-
* 'first_name as fn',
|
|
139
|
-
* 'person.last_name as ln'
|
|
140
|
-
* ])
|
|
141
|
-
* .execute()
|
|
142
|
-
* ```
|
|
143
|
-
*
|
|
144
|
-
* The generated SQL (PostgreSQL):
|
|
145
|
-
*
|
|
146
|
-
* ```sql
|
|
147
|
-
* select
|
|
148
|
-
* "first_name" as "fn",
|
|
149
|
-
* "person"."last_name" as "ln"
|
|
150
|
-
* from "person"
|
|
151
|
-
* ```
|
|
152
|
-
*
|
|
153
|
-
* <!-- siteExample("select", "Complex selections", 50) -->
|
|
154
|
-
*
|
|
155
|
-
* You can select arbitrary expression including subqueries and raw sql snippets.
|
|
156
|
-
* When you do that, you need to give a name for the selections using the `as` method:
|
|
157
|
-
*
|
|
158
|
-
* ```ts
|
|
159
|
-
* import { sql } from 'kysely'
|
|
160
|
-
*
|
|
161
|
-
* const persons = await db.selectFrom('person')
|
|
162
|
-
* .select(({ eb, selectFrom, or }) => [
|
|
163
|
-
* // Select a correlated subquery
|
|
164
|
-
* selectFrom('pet')
|
|
165
|
-
* .whereRef('person.id', '=', 'pet.owner_id')
|
|
166
|
-
* .select('pet.name')
|
|
167
|
-
* .orderBy('pet.name')
|
|
168
|
-
* .limit(1)
|
|
169
|
-
* .as('first_pet_name'),
|
|
170
|
-
*
|
|
171
|
-
* // Build and select an expression using
|
|
172
|
-
* // the expression builder
|
|
173
|
-
* or([
|
|
174
|
-
* eb('first_name', '=', 'Jennifer'),
|
|
175
|
-
* eb('first_name', '=', 'Arnold')
|
|
176
|
-
* ]).as('is_jennifer_or_arnold'),
|
|
177
|
-
*
|
|
178
|
-
* // Select a raw sql expression
|
|
179
|
-
* sql<string>`concat(first_name, ' ', last_name)`.as('full_name')
|
|
180
|
-
* ])
|
|
181
|
-
* .execute()
|
|
182
|
-
* ```
|
|
183
|
-
*
|
|
184
|
-
* The generated SQL (PostgreSQL):
|
|
185
|
-
*
|
|
186
|
-
* ```sql
|
|
187
|
-
* select
|
|
188
|
-
* (
|
|
189
|
-
* select "pet"."name"
|
|
190
|
-
* from "pet"
|
|
191
|
-
* where "person"."id" = "pet"."owner_id"
|
|
192
|
-
* order by "pet"."name"
|
|
193
|
-
* limit $1
|
|
194
|
-
* ) as "pet_name",
|
|
195
|
-
* ("first_name" = $2 or "first_name" = $3) as "jennifer_or_arnold",
|
|
196
|
-
* concat(first_name, ' ', last_name) as "full_name"
|
|
197
|
-
* from "person"
|
|
198
|
-
* ```
|
|
199
|
-
*
|
|
200
|
-
* In case you use the {@link sql} tag you need to specify the type of the expression
|
|
201
|
-
* (in this example `string`).
|
|
202
|
-
*
|
|
203
|
-
* All the examples above assume you know the column names at compile time.
|
|
204
|
-
* While it's better to build your code like that (that way you also know
|
|
205
|
-
* the types) sometimes it's not possible or you just prefer to write more
|
|
206
|
-
* dynamic code.
|
|
207
|
-
* <br><br>
|
|
208
|
-
* In this example, we use the `dynamic` module's methods to add selections
|
|
209
|
-
* dynamically:
|
|
210
|
-
*
|
|
211
|
-
* ```ts
|
|
212
|
-
* const { ref } = db.dynamic
|
|
213
|
-
*
|
|
214
|
-
* // Some column name provided by the user. Value not known at compile time.
|
|
215
|
-
* const columnFromUserInput = req.query.select;
|
|
216
|
-
*
|
|
217
|
-
* // A type that lists all possible values `columnFromUserInput` can have.
|
|
218
|
-
* // You can use `keyof Person` if any column of an interface is allowed.
|
|
219
|
-
* type PossibleColumns = 'last_name' | 'first_name' | 'birth_date'
|
|
220
|
-
*
|
|
221
|
-
* const spersons = await db
|
|
222
|
-
* .selectFrom('person')
|
|
223
|
-
* .select([
|
|
224
|
-
* ref<PossibleColumns>(columnFromUserInput)
|
|
225
|
-
* 'id'
|
|
226
|
-
* ])
|
|
227
|
-
* .execute()
|
|
228
|
-
*
|
|
229
|
-
* // The resulting type contains all `PossibleColumns` as optional fields
|
|
230
|
-
* // because we cannot know which field was actually selected before
|
|
231
|
-
* // running the code.
|
|
232
|
-
* const lastName: string | undefined = persons[0].last_name
|
|
233
|
-
* const firstName: string | undefined = persons[0].first_name
|
|
234
|
-
* const birthDate: string | undefined = persons[0].birth_date
|
|
235
|
-
*
|
|
236
|
-
* // The result type also contains the compile time selection `id`.
|
|
237
|
-
* persons[0].id
|
|
238
|
-
* ```
|
|
239
|
-
*/
|
|
240
57
|
select(selection) {
|
|
241
|
-
return new
|
|
58
|
+
return new SelectQueryBuilderImpl({
|
|
242
59
|
...this.#props,
|
|
243
60
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithSelections(this.#props.queryNode, (0, select_parser_js_1.parseSelectArg)(selection)),
|
|
244
61
|
});
|
|
245
62
|
}
|
|
246
63
|
distinctOn(selection) {
|
|
247
|
-
return new
|
|
64
|
+
return new SelectQueryBuilderImpl({
|
|
248
65
|
...this.#props,
|
|
249
66
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithDistinctOn(this.#props.queryNode, (0, reference_parser_js_1.parseReferenceExpressionOrList)(selection)),
|
|
250
67
|
});
|
|
251
68
|
}
|
|
252
|
-
/**
|
|
253
|
-
* This can be used to add any additional SQL to the front of the query __after__ the `select` keyword.
|
|
254
|
-
*
|
|
255
|
-
* ### Examples
|
|
256
|
-
*
|
|
257
|
-
* ```ts
|
|
258
|
-
* db.selectFrom('person')
|
|
259
|
-
* .modifyFront(sql`sql_no_cache`)
|
|
260
|
-
* .select('first_name')
|
|
261
|
-
* .execute()
|
|
262
|
-
* ```
|
|
263
|
-
*
|
|
264
|
-
* The generated SQL (MySQL):
|
|
265
|
-
*
|
|
266
|
-
* ```sql
|
|
267
|
-
* select sql_no_cache `first_name`
|
|
268
|
-
* from `person`
|
|
269
|
-
* ```
|
|
270
|
-
*/
|
|
271
69
|
modifyFront(modifier) {
|
|
272
|
-
return new
|
|
70
|
+
return new SelectQueryBuilderImpl({
|
|
273
71
|
...this.#props,
|
|
274
72
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithFrontModifier(this.#props.queryNode, select_modifier_node_js_1.SelectModifierNode.createWithExpression(modifier.toOperationNode())),
|
|
275
73
|
});
|
|
276
74
|
}
|
|
277
|
-
/**
|
|
278
|
-
* This can be used to add any additional SQL to the end of the query.
|
|
279
|
-
*
|
|
280
|
-
* Also see {@link forUpdate}, {@link forShare}, {@link forKeyShare}, {@link forNoKeyUpdate}
|
|
281
|
-
* {@link skipLocked} and {@link noWait}.
|
|
282
|
-
*
|
|
283
|
-
* ### Examples
|
|
284
|
-
*
|
|
285
|
-
* ```ts
|
|
286
|
-
* db.selectFrom('person')
|
|
287
|
-
* .select('first_name')
|
|
288
|
-
* .modifyEnd(sql`for update`)
|
|
289
|
-
* .execute()
|
|
290
|
-
* ```
|
|
291
|
-
*
|
|
292
|
-
* The generated SQL (PostgreSQL):
|
|
293
|
-
*
|
|
294
|
-
* ```sql
|
|
295
|
-
* select "first_name"
|
|
296
|
-
* from "person"
|
|
297
|
-
* for update
|
|
298
|
-
* ```
|
|
299
|
-
*/
|
|
300
75
|
modifyEnd(modifier) {
|
|
301
|
-
return new
|
|
76
|
+
return new SelectQueryBuilderImpl({
|
|
302
77
|
...this.#props,
|
|
303
78
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithEndModifier(this.#props.queryNode, select_modifier_node_js_1.SelectModifierNode.createWithExpression(modifier.toOperationNode())),
|
|
304
79
|
});
|
|
305
80
|
}
|
|
306
|
-
/**
|
|
307
|
-
* Makes the selection distinct.
|
|
308
|
-
*
|
|
309
|
-
* <!-- siteExample("select", "Distinct", 70) -->
|
|
310
|
-
*
|
|
311
|
-
* ### Examples
|
|
312
|
-
*
|
|
313
|
-
* ```ts
|
|
314
|
-
* const persons = await db.selectFrom('person')
|
|
315
|
-
* .select('first_name')
|
|
316
|
-
* .distinct()
|
|
317
|
-
* .execute()
|
|
318
|
-
* ```
|
|
319
|
-
*
|
|
320
|
-
* The generated SQL (PostgreSQL):
|
|
321
|
-
*
|
|
322
|
-
* ```sql
|
|
323
|
-
* select distinct "first_name" from "person"
|
|
324
|
-
* ```
|
|
325
|
-
*/
|
|
326
81
|
distinct() {
|
|
327
|
-
return new
|
|
82
|
+
return new SelectQueryBuilderImpl({
|
|
328
83
|
...this.#props,
|
|
329
84
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithFrontModifier(this.#props.queryNode, select_modifier_node_js_1.SelectModifierNode.create('Distinct')),
|
|
330
85
|
});
|
|
331
86
|
}
|
|
332
|
-
/**
|
|
333
|
-
* Adds the `for update` modifier to a select query on supported databases.
|
|
334
|
-
*/
|
|
335
87
|
forUpdate() {
|
|
336
|
-
return new
|
|
88
|
+
return new SelectQueryBuilderImpl({
|
|
337
89
|
...this.#props,
|
|
338
90
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithEndModifier(this.#props.queryNode, select_modifier_node_js_1.SelectModifierNode.create('ForUpdate')),
|
|
339
91
|
});
|
|
340
92
|
}
|
|
341
|
-
/**
|
|
342
|
-
* Adds the `for share` modifier to a select query on supported databases.
|
|
343
|
-
*/
|
|
344
93
|
forShare() {
|
|
345
|
-
return new
|
|
94
|
+
return new SelectQueryBuilderImpl({
|
|
346
95
|
...this.#props,
|
|
347
96
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithEndModifier(this.#props.queryNode, select_modifier_node_js_1.SelectModifierNode.create('ForShare')),
|
|
348
97
|
});
|
|
349
98
|
}
|
|
350
|
-
/**
|
|
351
|
-
* Adds the `for key share` modifier to a select query on supported databases.
|
|
352
|
-
*/
|
|
353
99
|
forKeyShare() {
|
|
354
|
-
return new
|
|
100
|
+
return new SelectQueryBuilderImpl({
|
|
355
101
|
...this.#props,
|
|
356
102
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithEndModifier(this.#props.queryNode, select_modifier_node_js_1.SelectModifierNode.create('ForKeyShare')),
|
|
357
103
|
});
|
|
358
104
|
}
|
|
359
|
-
/**
|
|
360
|
-
* Adds the `for no key update` modifier to a select query on supported databases.
|
|
361
|
-
*/
|
|
362
105
|
forNoKeyUpdate() {
|
|
363
|
-
return new
|
|
106
|
+
return new SelectQueryBuilderImpl({
|
|
364
107
|
...this.#props,
|
|
365
108
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithEndModifier(this.#props.queryNode, select_modifier_node_js_1.SelectModifierNode.create('ForNoKeyUpdate')),
|
|
366
109
|
});
|
|
367
110
|
}
|
|
368
|
-
/**
|
|
369
|
-
* Adds the `skip locked` modifier to a select query on supported databases.
|
|
370
|
-
*/
|
|
371
111
|
skipLocked() {
|
|
372
|
-
return new
|
|
112
|
+
return new SelectQueryBuilderImpl({
|
|
373
113
|
...this.#props,
|
|
374
114
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithEndModifier(this.#props.queryNode, select_modifier_node_js_1.SelectModifierNode.create('SkipLocked')),
|
|
375
115
|
});
|
|
376
116
|
}
|
|
377
|
-
/**
|
|
378
|
-
* Adds the `nowait` modifier to a select query on supported databases.
|
|
379
|
-
*/
|
|
380
117
|
noWait() {
|
|
381
|
-
return new
|
|
118
|
+
return new SelectQueryBuilderImpl({
|
|
382
119
|
...this.#props,
|
|
383
120
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithEndModifier(this.#props.queryNode, select_modifier_node_js_1.SelectModifierNode.create('NoWait')),
|
|
384
121
|
});
|
|
385
122
|
}
|
|
386
123
|
selectAll(table) {
|
|
387
|
-
return new
|
|
124
|
+
return new SelectQueryBuilderImpl({
|
|
388
125
|
...this.#props,
|
|
389
126
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithSelections(this.#props.queryNode, (0, select_parser_js_1.parseSelectAll)(table)),
|
|
390
127
|
});
|
|
391
128
|
}
|
|
392
129
|
innerJoin(...args) {
|
|
393
|
-
return new
|
|
130
|
+
return new SelectQueryBuilderImpl({
|
|
394
131
|
...this.#props,
|
|
395
132
|
queryNode: query_node_js_1.QueryNode.cloneWithJoin(this.#props.queryNode, (0, join_parser_js_1.parseJoin)('InnerJoin', args)),
|
|
396
133
|
});
|
|
397
134
|
}
|
|
398
135
|
leftJoin(...args) {
|
|
399
|
-
return new
|
|
136
|
+
return new SelectQueryBuilderImpl({
|
|
400
137
|
...this.#props,
|
|
401
138
|
queryNode: query_node_js_1.QueryNode.cloneWithJoin(this.#props.queryNode, (0, join_parser_js_1.parseJoin)('LeftJoin', args)),
|
|
402
139
|
});
|
|
403
140
|
}
|
|
404
141
|
rightJoin(...args) {
|
|
405
|
-
return new
|
|
142
|
+
return new SelectQueryBuilderImpl({
|
|
406
143
|
...this.#props,
|
|
407
144
|
queryNode: query_node_js_1.QueryNode.cloneWithJoin(this.#props.queryNode, (0, join_parser_js_1.parseJoin)('RightJoin', args)),
|
|
408
145
|
});
|
|
409
146
|
}
|
|
410
147
|
fullJoin(...args) {
|
|
411
|
-
return new
|
|
148
|
+
return new SelectQueryBuilderImpl({
|
|
412
149
|
...this.#props,
|
|
413
150
|
queryNode: query_node_js_1.QueryNode.cloneWithJoin(this.#props.queryNode, (0, join_parser_js_1.parseJoin)('FullJoin', args)),
|
|
414
151
|
});
|
|
415
152
|
}
|
|
416
153
|
innerJoinLateral(...args) {
|
|
417
|
-
return new
|
|
154
|
+
return new SelectQueryBuilderImpl({
|
|
418
155
|
...this.#props,
|
|
419
156
|
queryNode: query_node_js_1.QueryNode.cloneWithJoin(this.#props.queryNode, (0, join_parser_js_1.parseJoin)('LateralInnerJoin', args)),
|
|
420
157
|
});
|
|
421
158
|
}
|
|
422
159
|
leftJoinLateral(...args) {
|
|
423
|
-
return new
|
|
160
|
+
return new SelectQueryBuilderImpl({
|
|
424
161
|
...this.#props,
|
|
425
162
|
queryNode: query_node_js_1.QueryNode.cloneWithJoin(this.#props.queryNode, (0, join_parser_js_1.parseJoin)('LateralLeftJoin', args)),
|
|
426
163
|
});
|
|
427
164
|
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
*
|
|
431
|
-
* `orderBy` calls are additive. To order by multiple columns, call `orderBy`
|
|
432
|
-
* multiple times.
|
|
433
|
-
*
|
|
434
|
-
* The first argument is the expression to order by and the second is the
|
|
435
|
-
* order (`asc` or `desc`).
|
|
436
|
-
*
|
|
437
|
-
* ### Examples
|
|
438
|
-
*
|
|
439
|
-
* ```ts
|
|
440
|
-
* await db
|
|
441
|
-
* .selectFrom('person')
|
|
442
|
-
* .select('person.first_name as fn')
|
|
443
|
-
* .orderBy('id')
|
|
444
|
-
* .orderBy('fn', 'desc')
|
|
445
|
-
* .execute()
|
|
446
|
-
* ```
|
|
447
|
-
*
|
|
448
|
-
* The generated SQL (PostgreSQL):
|
|
449
|
-
*
|
|
450
|
-
* ```sql
|
|
451
|
-
* select "person"."first_name" as "fn"
|
|
452
|
-
* from "person"
|
|
453
|
-
* order by "id" asc, "fn" desc
|
|
454
|
-
* ```
|
|
455
|
-
*
|
|
456
|
-
* The order by expression can also be a raw sql expression or a subquery
|
|
457
|
-
* in addition to column references:
|
|
458
|
-
*
|
|
459
|
-
* ```ts
|
|
460
|
-
* import { sql } from 'kysely'
|
|
461
|
-
*
|
|
462
|
-
* await db
|
|
463
|
-
* .selectFrom('person')
|
|
464
|
-
* .selectAll()
|
|
465
|
-
* .orderBy((eb) => eb.selectFrom('pet')
|
|
466
|
-
* .select('pet.name')
|
|
467
|
-
* .whereRef('pet.owner_id', '=', 'person.id')
|
|
468
|
-
* .limit(1)
|
|
469
|
-
* )
|
|
470
|
-
* .orderBy(
|
|
471
|
-
* sql`concat(first_name, last_name)`
|
|
472
|
-
* )
|
|
473
|
-
* .execute()
|
|
474
|
-
* ```
|
|
475
|
-
*
|
|
476
|
-
* The generated SQL (PostgreSQL):
|
|
477
|
-
*
|
|
478
|
-
* ```sql
|
|
479
|
-
* select *
|
|
480
|
-
* from "person"
|
|
481
|
-
* order by
|
|
482
|
-
* ( select "pet"."name"
|
|
483
|
-
* from "pet"
|
|
484
|
-
* where "pet"."owner_id" = "person"."id"
|
|
485
|
-
* limit 1
|
|
486
|
-
* ) asc,
|
|
487
|
-
* concat(first_name, last_name) asc
|
|
488
|
-
* ```
|
|
489
|
-
*
|
|
490
|
-
* `dynamic.ref` can be used to refer to columns not known at
|
|
491
|
-
* compile time:
|
|
492
|
-
*
|
|
493
|
-
* ```ts
|
|
494
|
-
* async function someQuery(orderBy: string) {
|
|
495
|
-
* const { ref } = db.dynamic
|
|
496
|
-
*
|
|
497
|
-
* return await db
|
|
498
|
-
* .selectFrom('person')
|
|
499
|
-
* .select('person.first_name as fn')
|
|
500
|
-
* .orderBy(ref(orderBy))
|
|
501
|
-
* .execute()
|
|
502
|
-
* }
|
|
503
|
-
*
|
|
504
|
-
* someQuery('fn')
|
|
505
|
-
* ```
|
|
506
|
-
*
|
|
507
|
-
* The generated SQL (PostgreSQL):
|
|
508
|
-
*
|
|
509
|
-
* ```sql
|
|
510
|
-
* select "person"."first_name" as "fn"
|
|
511
|
-
* from "person"
|
|
512
|
-
* order by "fn" asc
|
|
513
|
-
* ```
|
|
514
|
-
*/
|
|
515
|
-
orderBy(orderBy, direction) {
|
|
516
|
-
return new SelectQueryBuilder({
|
|
165
|
+
orderBy(...args) {
|
|
166
|
+
return new SelectQueryBuilderImpl({
|
|
517
167
|
...this.#props,
|
|
518
|
-
queryNode: select_query_node_js_1.SelectQueryNode.
|
|
168
|
+
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithOrderByItems(this.#props.queryNode, (0, order_by_parser_js_1.parseOrderBy)(args)),
|
|
519
169
|
});
|
|
520
170
|
}
|
|
521
|
-
/**
|
|
522
|
-
* Adds a `group by` clause to the query.
|
|
523
|
-
*
|
|
524
|
-
* ### Examples
|
|
525
|
-
*
|
|
526
|
-
* ```ts
|
|
527
|
-
* import { sql } from 'kysely'
|
|
528
|
-
*
|
|
529
|
-
* await db
|
|
530
|
-
* .selectFrom('person')
|
|
531
|
-
* .select([
|
|
532
|
-
* 'first_name',
|
|
533
|
-
* sql`max(id)`.as('max_id')
|
|
534
|
-
* ])
|
|
535
|
-
* .groupBy('first_name')
|
|
536
|
-
* .execute()
|
|
537
|
-
* ```
|
|
538
|
-
*
|
|
539
|
-
* The generated SQL (PostgreSQL):
|
|
540
|
-
*
|
|
541
|
-
* ```sql
|
|
542
|
-
* select "first_name", max(id)
|
|
543
|
-
* from "person"
|
|
544
|
-
* group by "first_name"
|
|
545
|
-
* ```
|
|
546
|
-
*
|
|
547
|
-
* `groupBy` also accepts an array:
|
|
548
|
-
*
|
|
549
|
-
* ```ts
|
|
550
|
-
* import { sql } from 'kysely'
|
|
551
|
-
*
|
|
552
|
-
* await db
|
|
553
|
-
* .selectFrom('person')
|
|
554
|
-
* .select([
|
|
555
|
-
* 'first_name',
|
|
556
|
-
* 'last_name',
|
|
557
|
-
* sql`max(id)`.as('max_id')
|
|
558
|
-
* ])
|
|
559
|
-
* .groupBy([
|
|
560
|
-
* 'first_name',
|
|
561
|
-
* 'last_name'
|
|
562
|
-
* ])
|
|
563
|
-
* .execute()
|
|
564
|
-
* ```
|
|
565
|
-
*
|
|
566
|
-
* The generated SQL (PostgreSQL):
|
|
567
|
-
*
|
|
568
|
-
* ```sql
|
|
569
|
-
* select "first_name", "last_name", max(id)
|
|
570
|
-
* from "person"
|
|
571
|
-
* group by "first_name", "last_name"
|
|
572
|
-
* ```
|
|
573
|
-
*
|
|
574
|
-
* The group by expressions can also be subqueries or
|
|
575
|
-
* raw sql expressions:
|
|
576
|
-
*
|
|
577
|
-
* ```ts
|
|
578
|
-
* import { sql } from 'kysely'
|
|
579
|
-
*
|
|
580
|
-
* await db
|
|
581
|
-
* .selectFrom('person')
|
|
582
|
-
* .select([
|
|
583
|
-
* 'first_name',
|
|
584
|
-
* 'last_name',
|
|
585
|
-
* sql`max(id)`.as('max_id')
|
|
586
|
-
* ])
|
|
587
|
-
* .groupBy([
|
|
588
|
-
* sql`concat(first_name, last_name)`,
|
|
589
|
-
* (qb) => qb.selectFrom('pet').select('id').limit(1)
|
|
590
|
-
* ])
|
|
591
|
-
* .execute()
|
|
592
|
-
* ```
|
|
593
|
-
*
|
|
594
|
-
* `dynamic.ref` can be used to refer to columns not known at
|
|
595
|
-
* compile time:
|
|
596
|
-
*
|
|
597
|
-
* ```ts
|
|
598
|
-
* async function someQuery(groupBy: string) {
|
|
599
|
-
* const { ref } = db.dynamic
|
|
600
|
-
*
|
|
601
|
-
* return await db
|
|
602
|
-
* .selectFrom('person')
|
|
603
|
-
* .select('first_name')
|
|
604
|
-
* .groupBy(ref(groupBy))
|
|
605
|
-
* .execute()
|
|
606
|
-
* }
|
|
607
|
-
*
|
|
608
|
-
* someQuery('first_name')
|
|
609
|
-
* ```
|
|
610
|
-
*
|
|
611
|
-
* The generated SQL (PostgreSQL):
|
|
612
|
-
*
|
|
613
|
-
* ```sql
|
|
614
|
-
* select "first_name"
|
|
615
|
-
* from "person"
|
|
616
|
-
* group by "first_name"
|
|
617
|
-
* ```
|
|
618
|
-
*/
|
|
619
171
|
groupBy(groupBy) {
|
|
620
|
-
return new
|
|
172
|
+
return new SelectQueryBuilderImpl({
|
|
621
173
|
...this.#props,
|
|
622
174
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithGroupByItems(this.#props.queryNode, (0, group_by_parser_js_1.parseGroupBy)(groupBy)),
|
|
623
175
|
});
|
|
624
176
|
}
|
|
625
|
-
/**
|
|
626
|
-
* Adds a limit clause to the query.
|
|
627
|
-
*
|
|
628
|
-
* ### Examples
|
|
629
|
-
*
|
|
630
|
-
* Select the first 10 rows of the result:
|
|
631
|
-
*
|
|
632
|
-
* ```ts
|
|
633
|
-
* return await db
|
|
634
|
-
* .selectFrom('person')
|
|
635
|
-
* .select('first_name')
|
|
636
|
-
* .limit(10)
|
|
637
|
-
* ```
|
|
638
|
-
*
|
|
639
|
-
* Select rows from index 10 to index 19 of the result:
|
|
640
|
-
*
|
|
641
|
-
* ```ts
|
|
642
|
-
* return await db
|
|
643
|
-
* .selectFrom('person')
|
|
644
|
-
* .select('first_name')
|
|
645
|
-
* .offset(10)
|
|
646
|
-
* .limit(10)
|
|
647
|
-
* ```
|
|
648
|
-
*/
|
|
649
177
|
limit(limit) {
|
|
650
|
-
return new
|
|
178
|
+
return new SelectQueryBuilderImpl({
|
|
651
179
|
...this.#props,
|
|
652
180
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithLimit(this.#props.queryNode, limit_node_js_1.LimitNode.create(limit)),
|
|
653
181
|
});
|
|
654
182
|
}
|
|
655
|
-
/**
|
|
656
|
-
* Adds an offset clause to the query.
|
|
657
|
-
*
|
|
658
|
-
* ### Examples
|
|
659
|
-
*
|
|
660
|
-
* Select rows from index 10 to index 19 of the result:
|
|
661
|
-
*
|
|
662
|
-
* ```ts
|
|
663
|
-
* return await db
|
|
664
|
-
* .selectFrom('person')
|
|
665
|
-
* .select('first_name')
|
|
666
|
-
* .offset(10)
|
|
667
|
-
* .limit(10)
|
|
668
|
-
* ```
|
|
669
|
-
*/
|
|
670
183
|
offset(offset) {
|
|
671
|
-
return new
|
|
184
|
+
return new SelectQueryBuilderImpl({
|
|
672
185
|
...this.#props,
|
|
673
186
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithOffset(this.#props.queryNode, offset_node_js_1.OffsetNode.create(offset)),
|
|
674
187
|
});
|
|
675
188
|
}
|
|
676
|
-
/**
|
|
677
|
-
* Combines another select query or raw expression to this query using `union`.
|
|
678
|
-
*
|
|
679
|
-
* The output row type of the combined query must match `this` query.
|
|
680
|
-
*
|
|
681
|
-
* ### Examples
|
|
682
|
-
*
|
|
683
|
-
* ```ts
|
|
684
|
-
* db.selectFrom('person')
|
|
685
|
-
* .select(['id', 'first_name as name'])
|
|
686
|
-
* .union(db.selectFrom('pet').select(['id', 'name']))
|
|
687
|
-
* .orderBy('name')
|
|
688
|
-
* ```
|
|
689
|
-
*/
|
|
690
189
|
union(expression) {
|
|
691
|
-
return new
|
|
190
|
+
return new SelectQueryBuilderImpl({
|
|
692
191
|
...this.#props,
|
|
693
|
-
queryNode: select_query_node_js_1.SelectQueryNode.
|
|
192
|
+
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithSetOperations(this.#props.queryNode, (0, set_operation_parser_js_1.parseSetOperations)('union', expression, false)),
|
|
694
193
|
});
|
|
695
194
|
}
|
|
696
|
-
/**
|
|
697
|
-
* Combines another select query or raw expression to this query using `union all`.
|
|
698
|
-
*
|
|
699
|
-
* The output row type of the combined query must match `this` query.
|
|
700
|
-
*
|
|
701
|
-
* ### Examples
|
|
702
|
-
*
|
|
703
|
-
* ```ts
|
|
704
|
-
* db.selectFrom('person')
|
|
705
|
-
* .select(['id', 'first_name as name'])
|
|
706
|
-
* .unionAll(db.selectFrom('pet').select(['id', 'name']))
|
|
707
|
-
* .orderBy('name')
|
|
708
|
-
* ```
|
|
709
|
-
*/
|
|
710
195
|
unionAll(expression) {
|
|
711
|
-
return new
|
|
196
|
+
return new SelectQueryBuilderImpl({
|
|
712
197
|
...this.#props,
|
|
713
|
-
queryNode: select_query_node_js_1.SelectQueryNode.
|
|
198
|
+
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithSetOperations(this.#props.queryNode, (0, set_operation_parser_js_1.parseSetOperations)('union', expression, true)),
|
|
714
199
|
});
|
|
715
200
|
}
|
|
716
|
-
/**
|
|
717
|
-
* Combines another select query or raw expression to this query using `intersect`.
|
|
718
|
-
*
|
|
719
|
-
* The output row type of the combined query must match `this` query.
|
|
720
|
-
*
|
|
721
|
-
* ### Examples
|
|
722
|
-
*
|
|
723
|
-
* ```ts
|
|
724
|
-
* db.selectFrom('person')
|
|
725
|
-
* .select(['id', 'first_name as name'])
|
|
726
|
-
* .intersect(db.selectFrom('pet').select(['id', 'name']))
|
|
727
|
-
* .orderBy('name')
|
|
728
|
-
* ```
|
|
729
|
-
*/
|
|
730
201
|
intersect(expression) {
|
|
731
|
-
return new
|
|
202
|
+
return new SelectQueryBuilderImpl({
|
|
732
203
|
...this.#props,
|
|
733
|
-
queryNode: select_query_node_js_1.SelectQueryNode.
|
|
204
|
+
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithSetOperations(this.#props.queryNode, (0, set_operation_parser_js_1.parseSetOperations)('intersect', expression, false)),
|
|
734
205
|
});
|
|
735
206
|
}
|
|
736
|
-
/**
|
|
737
|
-
* Combines another select query or raw expression to this query using `intersect all`.
|
|
738
|
-
*
|
|
739
|
-
* The output row type of the combined query must match `this` query.
|
|
740
|
-
*
|
|
741
|
-
* ### Examples
|
|
742
|
-
*
|
|
743
|
-
* ```ts
|
|
744
|
-
* db.selectFrom('person')
|
|
745
|
-
* .select(['id', 'first_name as name'])
|
|
746
|
-
* .intersectAll(db.selectFrom('pet').select(['id', 'name']))
|
|
747
|
-
* .orderBy('name')
|
|
748
|
-
* ```
|
|
749
|
-
*/
|
|
750
207
|
intersectAll(expression) {
|
|
751
|
-
return new
|
|
208
|
+
return new SelectQueryBuilderImpl({
|
|
752
209
|
...this.#props,
|
|
753
|
-
queryNode: select_query_node_js_1.SelectQueryNode.
|
|
210
|
+
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithSetOperations(this.#props.queryNode, (0, set_operation_parser_js_1.parseSetOperations)('intersect', expression, true)),
|
|
754
211
|
});
|
|
755
212
|
}
|
|
756
|
-
/**
|
|
757
|
-
* Combines another select query or raw expression to this query using `except`.
|
|
758
|
-
*
|
|
759
|
-
* The output row type of the combined query must match `this` query.
|
|
760
|
-
*
|
|
761
|
-
* ### Examples
|
|
762
|
-
*
|
|
763
|
-
* ```ts
|
|
764
|
-
* db.selectFrom('person')
|
|
765
|
-
* .select(['id', 'first_name as name'])
|
|
766
|
-
* .except(db.selectFrom('pet').select(['id', 'name']))
|
|
767
|
-
* .orderBy('name')
|
|
768
|
-
* ```
|
|
769
|
-
*/
|
|
770
213
|
except(expression) {
|
|
771
|
-
return new
|
|
214
|
+
return new SelectQueryBuilderImpl({
|
|
772
215
|
...this.#props,
|
|
773
|
-
queryNode: select_query_node_js_1.SelectQueryNode.
|
|
216
|
+
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithSetOperations(this.#props.queryNode, (0, set_operation_parser_js_1.parseSetOperations)('except', expression, false)),
|
|
774
217
|
});
|
|
775
218
|
}
|
|
776
|
-
/**
|
|
777
|
-
* Combines another select query or raw expression to this query using `except all`.
|
|
778
|
-
*
|
|
779
|
-
* The output row type of the combined query must match `this` query.
|
|
780
|
-
*
|
|
781
|
-
* ### Examples
|
|
782
|
-
*
|
|
783
|
-
* ```ts
|
|
784
|
-
* db.selectFrom('person')
|
|
785
|
-
* .select(['id', 'first_name as name'])
|
|
786
|
-
* .exceptAll(db.selectFrom('pet').select(['id', 'name']))
|
|
787
|
-
* .orderBy('name')
|
|
788
|
-
* ```
|
|
789
|
-
*/
|
|
790
219
|
exceptAll(expression) {
|
|
791
|
-
return new
|
|
220
|
+
return new SelectQueryBuilderImpl({
|
|
792
221
|
...this.#props,
|
|
793
|
-
queryNode: select_query_node_js_1.SelectQueryNode.
|
|
222
|
+
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithSetOperations(this.#props.queryNode, (0, set_operation_parser_js_1.parseSetOperations)('except', expression, true)),
|
|
794
223
|
});
|
|
795
224
|
}
|
|
796
|
-
/**
|
|
797
|
-
* Gives an alias for the query. This method is only useful for sub queries.
|
|
798
|
-
*
|
|
799
|
-
* ### Examples
|
|
800
|
-
*
|
|
801
|
-
* ```ts
|
|
802
|
-
* const pets = await db.selectFrom('pet')
|
|
803
|
-
* .selectAll('pet')
|
|
804
|
-
* .select(
|
|
805
|
-
* (qb) => qb.selectFrom('person')
|
|
806
|
-
* .select('first_name')
|
|
807
|
-
* .whereRef('pet.owner_id', '=', 'person.id')
|
|
808
|
-
* .as('owner_first_name')
|
|
809
|
-
* )
|
|
810
|
-
* .execute()
|
|
811
|
-
*
|
|
812
|
-
* pets[0].owner_first_name
|
|
813
|
-
* ```
|
|
814
|
-
*/
|
|
815
225
|
as(alias) {
|
|
816
|
-
return new
|
|
226
|
+
return new AliasedSelectQueryBuilderImpl(this, alias);
|
|
817
227
|
}
|
|
818
|
-
/**
|
|
819
|
-
* Clears all select clauses from the query.
|
|
820
|
-
*
|
|
821
|
-
* ### Examples
|
|
822
|
-
*
|
|
823
|
-
* ```ts
|
|
824
|
-
* db.selectFrom('person')
|
|
825
|
-
* .select(['id', 'first_name'])
|
|
826
|
-
* .clearSelect()
|
|
827
|
-
* .select(['id', 'gender'])
|
|
828
|
-
* ```
|
|
829
|
-
*
|
|
830
|
-
* The generated SQL(PostgreSQL):
|
|
831
|
-
*
|
|
832
|
-
* ```sql
|
|
833
|
-
* select "id", "gender" from "person"
|
|
834
|
-
* ```
|
|
835
|
-
*/
|
|
836
228
|
clearSelect() {
|
|
837
|
-
return new
|
|
229
|
+
return new SelectQueryBuilderImpl({
|
|
838
230
|
...this.#props,
|
|
839
231
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithoutSelections(this.#props.queryNode),
|
|
840
232
|
});
|
|
841
233
|
}
|
|
842
234
|
clearWhere() {
|
|
843
|
-
return new
|
|
235
|
+
return new SelectQueryBuilderImpl({
|
|
844
236
|
...this.#props,
|
|
845
237
|
queryNode: query_node_js_1.QueryNode.cloneWithoutWhere(this.#props.queryNode),
|
|
846
238
|
});
|
|
847
239
|
}
|
|
848
|
-
/**
|
|
849
|
-
* Clears limit clause from the query.
|
|
850
|
-
*
|
|
851
|
-
* ### Examples
|
|
852
|
-
*
|
|
853
|
-
* ```ts
|
|
854
|
-
* db.selectFrom('person')
|
|
855
|
-
* .selectAll()
|
|
856
|
-
* .limit(10)
|
|
857
|
-
* .clearLimit()
|
|
858
|
-
* ```
|
|
859
|
-
*
|
|
860
|
-
* The generated SQL(PostgreSQL):
|
|
861
|
-
*
|
|
862
|
-
* ```sql
|
|
863
|
-
* select * from "person"
|
|
864
|
-
* ```
|
|
865
|
-
*/
|
|
866
240
|
clearLimit() {
|
|
867
|
-
return new
|
|
241
|
+
return new SelectQueryBuilderImpl({
|
|
868
242
|
...this.#props,
|
|
869
243
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithoutLimit(this.#props.queryNode),
|
|
870
244
|
});
|
|
871
245
|
}
|
|
872
|
-
/**
|
|
873
|
-
* Clears offset clause from the query.
|
|
874
|
-
*
|
|
875
|
-
* ### Examples
|
|
876
|
-
*
|
|
877
|
-
* ```ts
|
|
878
|
-
* db.selectFrom('person')
|
|
879
|
-
* .selectAll()
|
|
880
|
-
* .limit(10)
|
|
881
|
-
* .offset(20)
|
|
882
|
-
* .clearOffset()
|
|
883
|
-
* ```
|
|
884
|
-
*
|
|
885
|
-
* The generated SQL(PostgreSQL):
|
|
886
|
-
*
|
|
887
|
-
* ```sql
|
|
888
|
-
* select * from "person" limit 10
|
|
889
|
-
* ```
|
|
890
|
-
*/
|
|
891
246
|
clearOffset() {
|
|
892
|
-
return new
|
|
247
|
+
return new SelectQueryBuilderImpl({
|
|
893
248
|
...this.#props,
|
|
894
249
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithoutOffset(this.#props.queryNode),
|
|
895
250
|
});
|
|
896
251
|
}
|
|
897
|
-
/**
|
|
898
|
-
* Clears all `order by` clauses from the query.
|
|
899
|
-
*
|
|
900
|
-
* ### Examples
|
|
901
|
-
*
|
|
902
|
-
* ```ts
|
|
903
|
-
* db.selectFrom('person')
|
|
904
|
-
* .selectAll()
|
|
905
|
-
* .orderBy('id')
|
|
906
|
-
* .clearOrderBy()
|
|
907
|
-
* ```
|
|
908
|
-
*
|
|
909
|
-
* The generated SQL(PostgreSQL):
|
|
910
|
-
*
|
|
911
|
-
* ```sql
|
|
912
|
-
* select * from "person"
|
|
913
|
-
* ```
|
|
914
|
-
*/
|
|
915
252
|
clearOrderBy() {
|
|
916
|
-
return new
|
|
253
|
+
return new SelectQueryBuilderImpl({
|
|
917
254
|
...this.#props,
|
|
918
255
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithoutOrderBy(this.#props.queryNode),
|
|
919
256
|
});
|
|
920
257
|
}
|
|
921
|
-
/**
|
|
922
|
-
* Simply calls the provided function passing `this` as the only argument. `$call` returns
|
|
923
|
-
* what the provided function returns.
|
|
924
|
-
*
|
|
925
|
-
* If you want to conditionally call a method on `this`, see
|
|
926
|
-
* the {@link $if} method.
|
|
927
|
-
*
|
|
928
|
-
* ### Examples
|
|
929
|
-
*
|
|
930
|
-
* The next example uses a helper function `log` to log a query:
|
|
931
|
-
*
|
|
932
|
-
* ```ts
|
|
933
|
-
* function log<T extends Compilable>(qb: T): T {
|
|
934
|
-
* console.log(qb.compile())
|
|
935
|
-
* return qb
|
|
936
|
-
* }
|
|
937
|
-
*
|
|
938
|
-
* db.selectFrom('person')
|
|
939
|
-
* .selectAll()
|
|
940
|
-
* .$call(log)
|
|
941
|
-
* .execute()
|
|
942
|
-
* ```
|
|
943
|
-
*/
|
|
944
258
|
$call(func) {
|
|
945
259
|
return func(this);
|
|
946
260
|
}
|
|
947
|
-
/**
|
|
948
|
-
* Call `func(this)` if `condition` is true.
|
|
949
|
-
*
|
|
950
|
-
* NOTE: This method has an impact on typescript performance and it should only be used
|
|
951
|
-
* when necessary. Remember that you can call most methods like `where` conditionally
|
|
952
|
-
* like this:
|
|
953
|
-
*
|
|
954
|
-
* ```ts
|
|
955
|
-
* let query = db.selectFrom('person').selectAll()
|
|
956
|
-
*
|
|
957
|
-
* if (firstName) {
|
|
958
|
-
* query = query.where('first_name', '=', firstName)
|
|
959
|
-
* }
|
|
960
|
-
*
|
|
961
|
-
* if (lastName) {
|
|
962
|
-
* query = query.where('last_name', '=', lastName)
|
|
963
|
-
* }
|
|
964
|
-
*
|
|
965
|
-
* const result = await query.execute()
|
|
966
|
-
* ```
|
|
967
|
-
*
|
|
968
|
-
* This method is mainly useful with optional selects. Any `select` or `selectAll`
|
|
969
|
-
* method called inside the callback add optional fields to the result type. This is
|
|
970
|
-
* because we can't know if those selections were actually made before running the code.
|
|
971
|
-
*
|
|
972
|
-
* Also see [this recipe](https://github.com/koskimas/kysely/tree/master/site/docs/recipes/conditional-selects.md)
|
|
973
|
-
*
|
|
974
|
-
* ### Examples
|
|
975
|
-
*
|
|
976
|
-
* ```ts
|
|
977
|
-
* async function getPerson(id: number, withLastName: boolean) {
|
|
978
|
-
* return await db
|
|
979
|
-
* .selectFrom('person')
|
|
980
|
-
* .select(['id', 'first_name'])
|
|
981
|
-
* .$if(withLastName, (qb) => qb.select('last_name'))
|
|
982
|
-
* .where('id', '=', id)
|
|
983
|
-
* .executeTakeFirstOrThrow()
|
|
984
|
-
* }
|
|
985
|
-
* ```
|
|
986
|
-
*
|
|
987
|
-
* Any selections added inside the `if` callback will be added as optional fields to the
|
|
988
|
-
* output type since we can't know if the selections were actually made before running
|
|
989
|
-
* the code. In the example above the return type of the `getPerson` function is:
|
|
990
|
-
*
|
|
991
|
-
* ```ts
|
|
992
|
-
* {
|
|
993
|
-
* id: number
|
|
994
|
-
* first_name: string
|
|
995
|
-
* last_name?: string
|
|
996
|
-
* }
|
|
997
|
-
* ```
|
|
998
|
-
*
|
|
999
|
-
* You can also call any other methods inside the callback:
|
|
1000
|
-
*
|
|
1001
|
-
* ```ts
|
|
1002
|
-
* db.selectFrom('person')
|
|
1003
|
-
* .select('person.id')
|
|
1004
|
-
* .$if(filterByFirstName, (qb) => qb.where('first_name', '=', firstName))
|
|
1005
|
-
* .$if(filterByPetCount, (qb) => qb
|
|
1006
|
-
* .innerJoin('pet', 'pet.owner_id', 'person.id')
|
|
1007
|
-
* .having((eb) => eb.fn.count('pet.id'), '>', petCountLimit)
|
|
1008
|
-
* .groupBy('person.id')
|
|
1009
|
-
* )
|
|
1010
|
-
* ```
|
|
1011
|
-
*/
|
|
1012
261
|
$if(condition, func) {
|
|
1013
262
|
if (condition) {
|
|
1014
263
|
return func(this);
|
|
1015
264
|
}
|
|
1016
|
-
return new
|
|
265
|
+
return new SelectQueryBuilderImpl({
|
|
1017
266
|
...this.#props,
|
|
1018
267
|
});
|
|
1019
268
|
}
|
|
1020
|
-
/**
|
|
1021
|
-
* Change the output type of the query.
|
|
1022
|
-
*
|
|
1023
|
-
* You should only use this method as the last resort if the types
|
|
1024
|
-
* don't support your use case.
|
|
1025
|
-
*/
|
|
1026
269
|
$castTo() {
|
|
1027
|
-
return new
|
|
270
|
+
return new SelectQueryBuilderImpl(this.#props);
|
|
1028
271
|
}
|
|
1029
|
-
/**
|
|
1030
|
-
* Narrows (parts of) the output type of the query.
|
|
1031
|
-
*
|
|
1032
|
-
* Kysely tries to be as type-safe as possible, but in some cases we have to make
|
|
1033
|
-
* compromises for better maintainability and compilation performance. At present,
|
|
1034
|
-
* Kysely doesn't narrow the output type of the query when using {@link where}, {@link having}
|
|
1035
|
-
* or {@link JoinQueryBuilder.on}.
|
|
1036
|
-
*
|
|
1037
|
-
* This utility method is very useful for these situations, as it removes unncessary
|
|
1038
|
-
* runtime assertion/guard code. Its input type is limited to the output type
|
|
1039
|
-
* of the query, so you can't add a column that doesn't exist, or change a column's
|
|
1040
|
-
* type to something that doesn't exist in its union type.
|
|
1041
|
-
*
|
|
1042
|
-
* ### Examples
|
|
1043
|
-
*
|
|
1044
|
-
* Turn this code:
|
|
1045
|
-
*
|
|
1046
|
-
* ```ts
|
|
1047
|
-
* const person = await db.selectFrom('person')
|
|
1048
|
-
* .where('nullable_column', 'is not', null)
|
|
1049
|
-
* .selectAll()
|
|
1050
|
-
* .executeTakeFirstOrThrow()
|
|
1051
|
-
*
|
|
1052
|
-
* if (person.nullable_column) {
|
|
1053
|
-
* functionThatExpectsPersonWithNonNullValue(person)
|
|
1054
|
-
* }
|
|
1055
|
-
* ```
|
|
1056
|
-
*
|
|
1057
|
-
* Into this:
|
|
1058
|
-
*
|
|
1059
|
-
* ```ts
|
|
1060
|
-
* const person = await db.selectFrom('person')
|
|
1061
|
-
* .where('nullable_column', 'is not', null)
|
|
1062
|
-
* .selectAll()
|
|
1063
|
-
* .$narrowType<{ nullable_column: string }>()
|
|
1064
|
-
* .executeTakeFirstOrThrow()
|
|
1065
|
-
*
|
|
1066
|
-
* functionThatExpectsPersonWithNonNullValue(person)
|
|
1067
|
-
* ```
|
|
1068
|
-
*/
|
|
1069
272
|
$narrowType() {
|
|
1070
|
-
return new
|
|
273
|
+
return new SelectQueryBuilderImpl(this.#props);
|
|
1071
274
|
}
|
|
1072
|
-
/**
|
|
1073
|
-
* Asserts that query's output row type equals the given type `T`.
|
|
1074
|
-
*
|
|
1075
|
-
* This method can be used to simplify excessively complex types to make typescript happy
|
|
1076
|
-
* and much faster.
|
|
1077
|
-
*
|
|
1078
|
-
* Kysely uses complex type magic to achieve its type safety. This complexity is sometimes too much
|
|
1079
|
-
* for typescript and you get errors like this:
|
|
1080
|
-
*
|
|
1081
|
-
* ```
|
|
1082
|
-
* error TS2589: Type instantiation is excessively deep and possibly infinite.
|
|
1083
|
-
* ```
|
|
1084
|
-
*
|
|
1085
|
-
* In these case you can often use this method to help typescript a little bit. When you use this
|
|
1086
|
-
* method to assert the output type of a query, Kysely can drop the complex output type that
|
|
1087
|
-
* consists of multiple nested helper types and replace it with the simple asserted type.
|
|
1088
|
-
*
|
|
1089
|
-
* Using this method doesn't reduce type safety at all. You have to pass in a type that is
|
|
1090
|
-
* structurally equal to the current type.
|
|
1091
|
-
*
|
|
1092
|
-
* ### Examples
|
|
1093
|
-
*
|
|
1094
|
-
* ```ts
|
|
1095
|
-
* const result = await db
|
|
1096
|
-
* .with('first_and_last', (qb) => qb
|
|
1097
|
-
* .selectFrom('person')
|
|
1098
|
-
* .select(['first_name', 'last_name'])
|
|
1099
|
-
* .$assertType<{ first_name: string, last_name: string }>()
|
|
1100
|
-
* )
|
|
1101
|
-
* .with('age', (qb) => qb
|
|
1102
|
-
* .selectFrom('person')
|
|
1103
|
-
* .select('age')
|
|
1104
|
-
* .$assertType<{ age: number }>()
|
|
1105
|
-
* )
|
|
1106
|
-
* .selectFrom(['first_and_last', 'age'])
|
|
1107
|
-
* .selectAll()
|
|
1108
|
-
* .executeTakeFirstOrThrow()
|
|
1109
|
-
* ```
|
|
1110
|
-
*/
|
|
1111
275
|
$assertType() {
|
|
1112
|
-
return new
|
|
276
|
+
return new SelectQueryBuilderImpl(this.#props);
|
|
277
|
+
}
|
|
278
|
+
$asTuple() {
|
|
279
|
+
return new expression_wrapper_js_1.ExpressionWrapper(this.toOperationNode());
|
|
1113
280
|
}
|
|
1114
|
-
/**
|
|
1115
|
-
* Returns a copy of this SelectQueryBuilder instance with the given plugin installed.
|
|
1116
|
-
*/
|
|
1117
281
|
withPlugin(plugin) {
|
|
1118
|
-
return new
|
|
282
|
+
return new SelectQueryBuilderImpl({
|
|
1119
283
|
...this.#props,
|
|
1120
284
|
executor: this.#props.executor.withPlugin(plugin),
|
|
1121
285
|
});
|
|
@@ -1126,32 +290,15 @@ class SelectQueryBuilder {
|
|
|
1126
290
|
compile() {
|
|
1127
291
|
return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
|
|
1128
292
|
}
|
|
1129
|
-
/**
|
|
1130
|
-
* Executes the query and returns an array of rows.
|
|
1131
|
-
*
|
|
1132
|
-
* Also see the {@link executeTakeFirst} and {@link executeTakeFirstOrThrow} methods.
|
|
1133
|
-
*/
|
|
1134
293
|
async execute() {
|
|
1135
294
|
const compiledQuery = this.compile();
|
|
1136
295
|
const result = await this.#props.executor.executeQuery(compiledQuery, this.#props.queryId);
|
|
1137
296
|
return result.rows;
|
|
1138
297
|
}
|
|
1139
|
-
/**
|
|
1140
|
-
* Executes the query and returns the first result or undefined if
|
|
1141
|
-
* the query returned no result.
|
|
1142
|
-
*/
|
|
1143
298
|
async executeTakeFirst() {
|
|
1144
299
|
const [result] = await this.execute();
|
|
1145
300
|
return result;
|
|
1146
301
|
}
|
|
1147
|
-
/**
|
|
1148
|
-
* Executes the query and returns the first result or throws if
|
|
1149
|
-
* the query returned no result.
|
|
1150
|
-
*
|
|
1151
|
-
* By default an instance of {@link NoResultError} is thrown, but you can
|
|
1152
|
-
* provide a custom error class, or callback to throw a different
|
|
1153
|
-
* error.
|
|
1154
|
-
*/
|
|
1155
302
|
async executeTakeFirstOrThrow(errorConstructor = no_result_error_js_1.NoResultError) {
|
|
1156
303
|
const result = await this.executeTakeFirst();
|
|
1157
304
|
if (result === undefined) {
|
|
@@ -1170,35 +317,39 @@ class SelectQueryBuilder {
|
|
|
1170
317
|
}
|
|
1171
318
|
}
|
|
1172
319
|
async explain(format, options) {
|
|
1173
|
-
const builder = new
|
|
320
|
+
const builder = new SelectQueryBuilderImpl({
|
|
1174
321
|
...this.#props,
|
|
1175
322
|
queryNode: query_node_js_1.QueryNode.cloneWithExplain(this.#props.queryNode, format, options),
|
|
1176
323
|
});
|
|
1177
324
|
return await builder.execute();
|
|
1178
325
|
}
|
|
1179
326
|
}
|
|
1180
|
-
|
|
1181
|
-
(
|
|
327
|
+
(0, prevent_await_js_1.preventAwait)(SelectQueryBuilderImpl, "don't await SelectQueryBuilder instances directly. To execute the query you need to call `execute` or `executeTakeFirst`.");
|
|
328
|
+
function createSelectQueryBuilder(props) {
|
|
329
|
+
return new SelectQueryBuilderImpl(props);
|
|
330
|
+
}
|
|
331
|
+
exports.createSelectQueryBuilder = createSelectQueryBuilder;
|
|
1182
332
|
/**
|
|
1183
333
|
* {@link SelectQueryBuilder} with an alias. The result of calling {@link SelectQueryBuilder.as}.
|
|
1184
334
|
*/
|
|
1185
|
-
class
|
|
335
|
+
class AliasedSelectQueryBuilderImpl {
|
|
1186
336
|
#queryBuilder;
|
|
1187
337
|
#alias;
|
|
1188
338
|
constructor(queryBuilder, alias) {
|
|
1189
339
|
this.#queryBuilder = queryBuilder;
|
|
1190
340
|
this.#alias = alias;
|
|
1191
341
|
}
|
|
1192
|
-
/** @private */
|
|
1193
342
|
get expression() {
|
|
1194
343
|
return this.#queryBuilder;
|
|
1195
344
|
}
|
|
1196
|
-
/** @private */
|
|
1197
345
|
get alias() {
|
|
1198
346
|
return this.#alias;
|
|
1199
347
|
}
|
|
348
|
+
get isAliasedSelectQueryBuilder() {
|
|
349
|
+
return true;
|
|
350
|
+
}
|
|
1200
351
|
toOperationNode() {
|
|
1201
352
|
return alias_node_js_1.AliasNode.create(this.#queryBuilder.toOperationNode(), identifier_node_js_1.IdentifierNode.create(this.#alias));
|
|
1202
353
|
}
|
|
1203
354
|
}
|
|
1204
|
-
|
|
355
|
+
(0, prevent_await_js_1.preventAwait)(AliasedSelectQueryBuilderImpl, "don't await AliasedSelectQueryBuilder instances directly. AliasedSelectQueryBuilder should never be executed directly since it's always a part of another query.");
|