kysely 0.26.0 → 0.26.2
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 +299 -28
- 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 +11 -8
- 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 +3 -1
- 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 +9 -4
- 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.js +1 -1
- package/dist/cjs/parser/select-parser.d.ts +5 -5
- 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 +4 -2
- 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 +74 -24
- 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 +1 -1
- 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 +60 -4
- package/dist/cjs/query-builder/on-conflict-builder.d.ts +143 -44
- 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/select-query-builder.d.ts +245 -86
- package/dist/cjs/query-builder/select-query-builder.js +72 -924
- package/dist/cjs/query-builder/update-query-builder.d.ts +106 -25
- package/dist/cjs/query-builder/update-query-builder.js +2 -2
- package/dist/cjs/query-builder/where-interface.d.ts +71 -24
- 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 +74 -2
- package/dist/cjs/query-creator.js +83 -6
- package/dist/cjs/raw-builder/raw-builder.d.ts +88 -49
- 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/util/column-type.d.ts +7 -6
- 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 +10 -10
- 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 +299 -28
- 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 +11 -8
- 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 +3 -1
- 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 +9 -4
- 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.js +1 -1
- package/dist/esm/parser/select-parser.d.ts +5 -5
- 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 +4 -2
- 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 +74 -24
- 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 +1 -1
- 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 +60 -4
- package/dist/esm/query-builder/on-conflict-builder.d.ts +143 -44
- 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/select-query-builder.d.ts +245 -86
- package/dist/esm/query-builder/select-query-builder.js +72 -923
- package/dist/esm/query-builder/update-query-builder.d.ts +106 -25
- package/dist/esm/query-builder/update-query-builder.js +3 -3
- package/dist/esm/query-builder/where-interface.d.ts +71 -24
- 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 +74 -2
- package/dist/esm/query-creator.js +84 -7
- package/dist/esm/raw-builder/raw-builder.d.ts +88 -49
- 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/util/column-type.d.ts +7 -6
- 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 +10 -10
- 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,265 @@ 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
30
|
where(...args) {
|
|
31
|
-
return new
|
|
31
|
+
return new SelectQueryBuilderImpl({
|
|
32
32
|
...this.#props,
|
|
33
|
-
queryNode: query_node_js_1.QueryNode.cloneWithWhere(this.#props.queryNode, (0, binary_operation_parser_js_1.
|
|
33
|
+
queryNode: query_node_js_1.QueryNode.cloneWithWhere(this.#props.queryNode, (0, binary_operation_parser_js_1.parseValueBinaryOperationOrExpression)(args)),
|
|
34
34
|
});
|
|
35
35
|
}
|
|
36
36
|
whereRef(lhs, op, rhs) {
|
|
37
|
-
return new
|
|
37
|
+
return new SelectQueryBuilderImpl({
|
|
38
38
|
...this.#props,
|
|
39
|
-
queryNode: query_node_js_1.QueryNode.cloneWithWhere(this.#props.queryNode, (0, binary_operation_parser_js_1.
|
|
39
|
+
queryNode: query_node_js_1.QueryNode.cloneWithWhere(this.#props.queryNode, (0, binary_operation_parser_js_1.parseReferentialBinaryOperation)(lhs, op, rhs)),
|
|
40
40
|
});
|
|
41
41
|
}
|
|
42
42
|
having(...args) {
|
|
43
|
-
return new
|
|
43
|
+
return new SelectQueryBuilderImpl({
|
|
44
44
|
...this.#props,
|
|
45
|
-
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithHaving(this.#props.queryNode, (0, binary_operation_parser_js_1.
|
|
45
|
+
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithHaving(this.#props.queryNode, (0, binary_operation_parser_js_1.parseValueBinaryOperationOrExpression)(args)),
|
|
46
46
|
});
|
|
47
47
|
}
|
|
48
48
|
havingRef(lhs, op, rhs) {
|
|
49
|
-
return new
|
|
49
|
+
return new SelectQueryBuilderImpl({
|
|
50
50
|
...this.#props,
|
|
51
|
-
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithHaving(this.#props.queryNode, (0, binary_operation_parser_js_1.
|
|
51
|
+
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithHaving(this.#props.queryNode, (0, binary_operation_parser_js_1.parseReferentialBinaryOperation)(lhs, op, rhs)),
|
|
52
52
|
});
|
|
53
53
|
}
|
|
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(({ selectFrom, or, cmpr }) => [
|
|
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
|
-
* cmpr('first_name', '=', 'Jennifer'),
|
|
175
|
-
* cmpr('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
54
|
select(selection) {
|
|
241
|
-
return new
|
|
55
|
+
return new SelectQueryBuilderImpl({
|
|
242
56
|
...this.#props,
|
|
243
57
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithSelections(this.#props.queryNode, (0, select_parser_js_1.parseSelectArg)(selection)),
|
|
244
58
|
});
|
|
245
59
|
}
|
|
246
60
|
distinctOn(selection) {
|
|
247
|
-
return new
|
|
61
|
+
return new SelectQueryBuilderImpl({
|
|
248
62
|
...this.#props,
|
|
249
63
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithDistinctOn(this.#props.queryNode, (0, reference_parser_js_1.parseReferenceExpressionOrList)(selection)),
|
|
250
64
|
});
|
|
251
65
|
}
|
|
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
66
|
modifyFront(modifier) {
|
|
272
|
-
return new
|
|
67
|
+
return new SelectQueryBuilderImpl({
|
|
273
68
|
...this.#props,
|
|
274
69
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithFrontModifier(this.#props.queryNode, select_modifier_node_js_1.SelectModifierNode.createWithExpression(modifier.toOperationNode())),
|
|
275
70
|
});
|
|
276
71
|
}
|
|
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
72
|
modifyEnd(modifier) {
|
|
301
|
-
return new
|
|
73
|
+
return new SelectQueryBuilderImpl({
|
|
302
74
|
...this.#props,
|
|
303
75
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithEndModifier(this.#props.queryNode, select_modifier_node_js_1.SelectModifierNode.createWithExpression(modifier.toOperationNode())),
|
|
304
76
|
});
|
|
305
77
|
}
|
|
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
78
|
distinct() {
|
|
327
|
-
return new
|
|
79
|
+
return new SelectQueryBuilderImpl({
|
|
328
80
|
...this.#props,
|
|
329
81
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithFrontModifier(this.#props.queryNode, select_modifier_node_js_1.SelectModifierNode.create('Distinct')),
|
|
330
82
|
});
|
|
331
83
|
}
|
|
332
|
-
/**
|
|
333
|
-
* Adds the `for update` modifier to a select query on supported databases.
|
|
334
|
-
*/
|
|
335
84
|
forUpdate() {
|
|
336
|
-
return new
|
|
85
|
+
return new SelectQueryBuilderImpl({
|
|
337
86
|
...this.#props,
|
|
338
87
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithEndModifier(this.#props.queryNode, select_modifier_node_js_1.SelectModifierNode.create('ForUpdate')),
|
|
339
88
|
});
|
|
340
89
|
}
|
|
341
|
-
/**
|
|
342
|
-
* Adds the `for share` modifier to a select query on supported databases.
|
|
343
|
-
*/
|
|
344
90
|
forShare() {
|
|
345
|
-
return new
|
|
91
|
+
return new SelectQueryBuilderImpl({
|
|
346
92
|
...this.#props,
|
|
347
93
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithEndModifier(this.#props.queryNode, select_modifier_node_js_1.SelectModifierNode.create('ForShare')),
|
|
348
94
|
});
|
|
349
95
|
}
|
|
350
|
-
/**
|
|
351
|
-
* Adds the `for key share` modifier to a select query on supported databases.
|
|
352
|
-
*/
|
|
353
96
|
forKeyShare() {
|
|
354
|
-
return new
|
|
97
|
+
return new SelectQueryBuilderImpl({
|
|
355
98
|
...this.#props,
|
|
356
99
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithEndModifier(this.#props.queryNode, select_modifier_node_js_1.SelectModifierNode.create('ForKeyShare')),
|
|
357
100
|
});
|
|
358
101
|
}
|
|
359
|
-
/**
|
|
360
|
-
* Adds the `for no key update` modifier to a select query on supported databases.
|
|
361
|
-
*/
|
|
362
102
|
forNoKeyUpdate() {
|
|
363
|
-
return new
|
|
103
|
+
return new SelectQueryBuilderImpl({
|
|
364
104
|
...this.#props,
|
|
365
105
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithEndModifier(this.#props.queryNode, select_modifier_node_js_1.SelectModifierNode.create('ForNoKeyUpdate')),
|
|
366
106
|
});
|
|
367
107
|
}
|
|
368
|
-
/**
|
|
369
|
-
* Adds the `skip locked` modifier to a select query on supported databases.
|
|
370
|
-
*/
|
|
371
108
|
skipLocked() {
|
|
372
|
-
return new
|
|
109
|
+
return new SelectQueryBuilderImpl({
|
|
373
110
|
...this.#props,
|
|
374
111
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithEndModifier(this.#props.queryNode, select_modifier_node_js_1.SelectModifierNode.create('SkipLocked')),
|
|
375
112
|
});
|
|
376
113
|
}
|
|
377
|
-
/**
|
|
378
|
-
* Adds the `nowait` modifier to a select query on supported databases.
|
|
379
|
-
*/
|
|
380
114
|
noWait() {
|
|
381
|
-
return new
|
|
115
|
+
return new SelectQueryBuilderImpl({
|
|
382
116
|
...this.#props,
|
|
383
117
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithEndModifier(this.#props.queryNode, select_modifier_node_js_1.SelectModifierNode.create('NoWait')),
|
|
384
118
|
});
|
|
385
119
|
}
|
|
386
120
|
selectAll(table) {
|
|
387
|
-
return new
|
|
121
|
+
return new SelectQueryBuilderImpl({
|
|
388
122
|
...this.#props,
|
|
389
123
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithSelections(this.#props.queryNode, (0, select_parser_js_1.parseSelectAll)(table)),
|
|
390
124
|
});
|
|
391
125
|
}
|
|
392
126
|
innerJoin(...args) {
|
|
393
|
-
return new
|
|
127
|
+
return new SelectQueryBuilderImpl({
|
|
394
128
|
...this.#props,
|
|
395
129
|
queryNode: query_node_js_1.QueryNode.cloneWithJoin(this.#props.queryNode, (0, join_parser_js_1.parseJoin)('InnerJoin', args)),
|
|
396
130
|
});
|
|
397
131
|
}
|
|
398
132
|
leftJoin(...args) {
|
|
399
|
-
return new
|
|
133
|
+
return new SelectQueryBuilderImpl({
|
|
400
134
|
...this.#props,
|
|
401
135
|
queryNode: query_node_js_1.QueryNode.cloneWithJoin(this.#props.queryNode, (0, join_parser_js_1.parseJoin)('LeftJoin', args)),
|
|
402
136
|
});
|
|
403
137
|
}
|
|
404
138
|
rightJoin(...args) {
|
|
405
|
-
return new
|
|
139
|
+
return new SelectQueryBuilderImpl({
|
|
406
140
|
...this.#props,
|
|
407
141
|
queryNode: query_node_js_1.QueryNode.cloneWithJoin(this.#props.queryNode, (0, join_parser_js_1.parseJoin)('RightJoin', args)),
|
|
408
142
|
});
|
|
409
143
|
}
|
|
410
144
|
fullJoin(...args) {
|
|
411
|
-
return new
|
|
145
|
+
return new SelectQueryBuilderImpl({
|
|
412
146
|
...this.#props,
|
|
413
147
|
queryNode: query_node_js_1.QueryNode.cloneWithJoin(this.#props.queryNode, (0, join_parser_js_1.parseJoin)('FullJoin', args)),
|
|
414
148
|
});
|
|
415
149
|
}
|
|
416
150
|
innerJoinLateral(...args) {
|
|
417
|
-
return new
|
|
151
|
+
return new SelectQueryBuilderImpl({
|
|
418
152
|
...this.#props,
|
|
419
153
|
queryNode: query_node_js_1.QueryNode.cloneWithJoin(this.#props.queryNode, (0, join_parser_js_1.parseJoin)('LateralInnerJoin', args)),
|
|
420
154
|
});
|
|
421
155
|
}
|
|
422
156
|
leftJoinLateral(...args) {
|
|
423
|
-
return new
|
|
157
|
+
return new SelectQueryBuilderImpl({
|
|
424
158
|
...this.#props,
|
|
425
159
|
queryNode: query_node_js_1.QueryNode.cloneWithJoin(this.#props.queryNode, (0, join_parser_js_1.parseJoin)('LateralLeftJoin', args)),
|
|
426
160
|
});
|
|
427
161
|
}
|
|
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({
|
|
162
|
+
orderBy(...args) {
|
|
163
|
+
return new SelectQueryBuilderImpl({
|
|
517
164
|
...this.#props,
|
|
518
|
-
queryNode: select_query_node_js_1.SelectQueryNode.
|
|
165
|
+
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithOrderByItems(this.#props.queryNode, (0, order_by_parser_js_1.parseOrderBy)(args)),
|
|
519
166
|
});
|
|
520
167
|
}
|
|
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
168
|
groupBy(groupBy) {
|
|
620
|
-
return new
|
|
169
|
+
return new SelectQueryBuilderImpl({
|
|
621
170
|
...this.#props,
|
|
622
171
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithGroupByItems(this.#props.queryNode, (0, group_by_parser_js_1.parseGroupBy)(groupBy)),
|
|
623
172
|
});
|
|
624
173
|
}
|
|
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
174
|
limit(limit) {
|
|
650
|
-
return new
|
|
175
|
+
return new SelectQueryBuilderImpl({
|
|
651
176
|
...this.#props,
|
|
652
177
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithLimit(this.#props.queryNode, limit_node_js_1.LimitNode.create(limit)),
|
|
653
178
|
});
|
|
654
179
|
}
|
|
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
180
|
offset(offset) {
|
|
671
|
-
return new
|
|
181
|
+
return new SelectQueryBuilderImpl({
|
|
672
182
|
...this.#props,
|
|
673
183
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithOffset(this.#props.queryNode, offset_node_js_1.OffsetNode.create(offset)),
|
|
674
184
|
});
|
|
675
185
|
}
|
|
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
186
|
union(expression) {
|
|
691
|
-
return new
|
|
187
|
+
return new SelectQueryBuilderImpl({
|
|
692
188
|
...this.#props,
|
|
693
|
-
queryNode: select_query_node_js_1.SelectQueryNode.
|
|
189
|
+
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithSetOperations(this.#props.queryNode, (0, set_operation_parser_js_1.parseSetOperations)('union', expression, false)),
|
|
694
190
|
});
|
|
695
191
|
}
|
|
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
192
|
unionAll(expression) {
|
|
711
|
-
return new
|
|
193
|
+
return new SelectQueryBuilderImpl({
|
|
712
194
|
...this.#props,
|
|
713
|
-
queryNode: select_query_node_js_1.SelectQueryNode.
|
|
195
|
+
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithSetOperations(this.#props.queryNode, (0, set_operation_parser_js_1.parseSetOperations)('union', expression, true)),
|
|
714
196
|
});
|
|
715
197
|
}
|
|
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
198
|
intersect(expression) {
|
|
731
|
-
return new
|
|
199
|
+
return new SelectQueryBuilderImpl({
|
|
732
200
|
...this.#props,
|
|
733
|
-
queryNode: select_query_node_js_1.SelectQueryNode.
|
|
201
|
+
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithSetOperations(this.#props.queryNode, (0, set_operation_parser_js_1.parseSetOperations)('intersect', expression, false)),
|
|
734
202
|
});
|
|
735
203
|
}
|
|
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
204
|
intersectAll(expression) {
|
|
751
|
-
return new
|
|
205
|
+
return new SelectQueryBuilderImpl({
|
|
752
206
|
...this.#props,
|
|
753
|
-
queryNode: select_query_node_js_1.SelectQueryNode.
|
|
207
|
+
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithSetOperations(this.#props.queryNode, (0, set_operation_parser_js_1.parseSetOperations)('intersect', expression, true)),
|
|
754
208
|
});
|
|
755
209
|
}
|
|
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
210
|
except(expression) {
|
|
771
|
-
return new
|
|
211
|
+
return new SelectQueryBuilderImpl({
|
|
772
212
|
...this.#props,
|
|
773
|
-
queryNode: select_query_node_js_1.SelectQueryNode.
|
|
213
|
+
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithSetOperations(this.#props.queryNode, (0, set_operation_parser_js_1.parseSetOperations)('except', expression, false)),
|
|
774
214
|
});
|
|
775
215
|
}
|
|
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
216
|
exceptAll(expression) {
|
|
791
|
-
return new
|
|
217
|
+
return new SelectQueryBuilderImpl({
|
|
792
218
|
...this.#props,
|
|
793
|
-
queryNode: select_query_node_js_1.SelectQueryNode.
|
|
219
|
+
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithSetOperations(this.#props.queryNode, (0, set_operation_parser_js_1.parseSetOperations)('except', expression, true)),
|
|
794
220
|
});
|
|
795
221
|
}
|
|
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
222
|
as(alias) {
|
|
816
|
-
return new
|
|
223
|
+
return new AliasedSelectQueryBuilderImpl(this, alias);
|
|
817
224
|
}
|
|
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
225
|
clearSelect() {
|
|
837
|
-
return new
|
|
226
|
+
return new SelectQueryBuilderImpl({
|
|
838
227
|
...this.#props,
|
|
839
228
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithoutSelections(this.#props.queryNode),
|
|
840
229
|
});
|
|
841
230
|
}
|
|
842
231
|
clearWhere() {
|
|
843
|
-
return new
|
|
232
|
+
return new SelectQueryBuilderImpl({
|
|
844
233
|
...this.#props,
|
|
845
234
|
queryNode: query_node_js_1.QueryNode.cloneWithoutWhere(this.#props.queryNode),
|
|
846
235
|
});
|
|
847
236
|
}
|
|
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
237
|
clearLimit() {
|
|
867
|
-
return new
|
|
238
|
+
return new SelectQueryBuilderImpl({
|
|
868
239
|
...this.#props,
|
|
869
240
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithoutLimit(this.#props.queryNode),
|
|
870
241
|
});
|
|
871
242
|
}
|
|
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
243
|
clearOffset() {
|
|
892
|
-
return new
|
|
244
|
+
return new SelectQueryBuilderImpl({
|
|
893
245
|
...this.#props,
|
|
894
246
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithoutOffset(this.#props.queryNode),
|
|
895
247
|
});
|
|
896
248
|
}
|
|
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
249
|
clearOrderBy() {
|
|
916
|
-
return new
|
|
250
|
+
return new SelectQueryBuilderImpl({
|
|
917
251
|
...this.#props,
|
|
918
252
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithoutOrderBy(this.#props.queryNode),
|
|
919
253
|
});
|
|
920
254
|
}
|
|
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
255
|
$call(func) {
|
|
945
256
|
return func(this);
|
|
946
257
|
}
|
|
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
258
|
$if(condition, func) {
|
|
1013
259
|
if (condition) {
|
|
1014
260
|
return func(this);
|
|
1015
261
|
}
|
|
1016
|
-
return new
|
|
262
|
+
return new SelectQueryBuilderImpl({
|
|
1017
263
|
...this.#props,
|
|
1018
264
|
});
|
|
1019
265
|
}
|
|
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
266
|
$castTo() {
|
|
1027
|
-
return new
|
|
267
|
+
return new SelectQueryBuilderImpl(this.#props);
|
|
1028
268
|
}
|
|
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
269
|
$narrowType() {
|
|
1070
|
-
return new
|
|
270
|
+
return new SelectQueryBuilderImpl(this.#props);
|
|
1071
271
|
}
|
|
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
272
|
$assertType() {
|
|
1112
|
-
return new
|
|
273
|
+
return new SelectQueryBuilderImpl(this.#props);
|
|
274
|
+
}
|
|
275
|
+
$asTuple() {
|
|
276
|
+
return new expression_wrapper_js_1.ExpressionWrapper(this.toOperationNode());
|
|
1113
277
|
}
|
|
1114
|
-
/**
|
|
1115
|
-
* Returns a copy of this SelectQueryBuilder instance with the given plugin installed.
|
|
1116
|
-
*/
|
|
1117
278
|
withPlugin(plugin) {
|
|
1118
|
-
return new
|
|
279
|
+
return new SelectQueryBuilderImpl({
|
|
1119
280
|
...this.#props,
|
|
1120
281
|
executor: this.#props.executor.withPlugin(plugin),
|
|
1121
282
|
});
|
|
@@ -1126,32 +287,15 @@ class SelectQueryBuilder {
|
|
|
1126
287
|
compile() {
|
|
1127
288
|
return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
|
|
1128
289
|
}
|
|
1129
|
-
/**
|
|
1130
|
-
* Executes the query and returns an array of rows.
|
|
1131
|
-
*
|
|
1132
|
-
* Also see the {@link executeTakeFirst} and {@link executeTakeFirstOrThrow} methods.
|
|
1133
|
-
*/
|
|
1134
290
|
async execute() {
|
|
1135
291
|
const compiledQuery = this.compile();
|
|
1136
292
|
const result = await this.#props.executor.executeQuery(compiledQuery, this.#props.queryId);
|
|
1137
293
|
return result.rows;
|
|
1138
294
|
}
|
|
1139
|
-
/**
|
|
1140
|
-
* Executes the query and returns the first result or undefined if
|
|
1141
|
-
* the query returned no result.
|
|
1142
|
-
*/
|
|
1143
295
|
async executeTakeFirst() {
|
|
1144
296
|
const [result] = await this.execute();
|
|
1145
297
|
return result;
|
|
1146
298
|
}
|
|
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
299
|
async executeTakeFirstOrThrow(errorConstructor = no_result_error_js_1.NoResultError) {
|
|
1156
300
|
const result = await this.executeTakeFirst();
|
|
1157
301
|
if (result === undefined) {
|
|
@@ -1170,35 +314,39 @@ class SelectQueryBuilder {
|
|
|
1170
314
|
}
|
|
1171
315
|
}
|
|
1172
316
|
async explain(format, options) {
|
|
1173
|
-
const builder = new
|
|
317
|
+
const builder = new SelectQueryBuilderImpl({
|
|
1174
318
|
...this.#props,
|
|
1175
319
|
queryNode: query_node_js_1.QueryNode.cloneWithExplain(this.#props.queryNode, format, options),
|
|
1176
320
|
});
|
|
1177
321
|
return await builder.execute();
|
|
1178
322
|
}
|
|
1179
323
|
}
|
|
1180
|
-
|
|
1181
|
-
(
|
|
324
|
+
(0, prevent_await_js_1.preventAwait)(SelectQueryBuilderImpl, "don't await SelectQueryBuilder instances directly. To execute the query you need to call `execute` or `executeTakeFirst`.");
|
|
325
|
+
function createSelectQueryBuilder(props) {
|
|
326
|
+
return new SelectQueryBuilderImpl(props);
|
|
327
|
+
}
|
|
328
|
+
exports.createSelectQueryBuilder = createSelectQueryBuilder;
|
|
1182
329
|
/**
|
|
1183
330
|
* {@link SelectQueryBuilder} with an alias. The result of calling {@link SelectQueryBuilder.as}.
|
|
1184
331
|
*/
|
|
1185
|
-
class
|
|
332
|
+
class AliasedSelectQueryBuilderImpl {
|
|
1186
333
|
#queryBuilder;
|
|
1187
334
|
#alias;
|
|
1188
335
|
constructor(queryBuilder, alias) {
|
|
1189
336
|
this.#queryBuilder = queryBuilder;
|
|
1190
337
|
this.#alias = alias;
|
|
1191
338
|
}
|
|
1192
|
-
/** @private */
|
|
1193
339
|
get expression() {
|
|
1194
340
|
return this.#queryBuilder;
|
|
1195
341
|
}
|
|
1196
|
-
/** @private */
|
|
1197
342
|
get alias() {
|
|
1198
343
|
return this.#alias;
|
|
1199
344
|
}
|
|
345
|
+
get selectQueryBuilder() {
|
|
346
|
+
return this.#queryBuilder;
|
|
347
|
+
}
|
|
1200
348
|
toOperationNode() {
|
|
1201
349
|
return alias_node_js_1.AliasNode.create(this.#queryBuilder.toOperationNode(), identifier_node_js_1.IdentifierNode.create(this.#alias));
|
|
1202
350
|
}
|
|
1203
351
|
}
|
|
1204
|
-
|
|
352
|
+
(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.");
|