kysely 0.21.5 → 0.22.0
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 +3 -2
- package/dist/cjs/dialect/sqlite/sqlite-query-compiler.d.ts +2 -0
- package/dist/cjs/dialect/sqlite/sqlite-query-compiler.js +4 -0
- package/dist/cjs/index.d.ts +10 -4
- package/dist/cjs/index.js +8 -2
- package/dist/cjs/kysely.d.ts +3 -3
- package/dist/cjs/kysely.js +3 -3
- package/dist/cjs/migration/file-migration-provider.js +7 -2
- package/dist/cjs/operation-node/aggregate-function-node.d.ts +22 -0
- package/dist/cjs/operation-node/aggregate-function-node.js +31 -0
- package/dist/cjs/operation-node/alias-node.d.ts +2 -1
- package/dist/cjs/operation-node/default-insert-value-node.d.ts +11 -0
- package/dist/cjs/operation-node/{union-node.js → default-insert-value-node.js} +5 -7
- package/dist/cjs/operation-node/group-by-node.d.ts +1 -1
- package/dist/cjs/operation-node/group-by-node.js +3 -3
- package/dist/cjs/operation-node/insert-query-node.d.ts +2 -1
- package/dist/cjs/operation-node/operation-node-transformer.d.ts +12 -2
- package/dist/cjs/operation-node/operation-node-transformer.js +43 -5
- package/dist/cjs/operation-node/operation-node-utils.d.ts +2 -1
- package/dist/cjs/operation-node/operation-node-visitor.d.ts +12 -2
- package/dist/cjs/operation-node/operation-node-visitor.js +6 -1
- package/dist/cjs/operation-node/operation-node.d.ts +1 -1
- package/dist/cjs/operation-node/over-node.d.ts +19 -0
- package/dist/cjs/operation-node/over-node.js +35 -0
- package/dist/cjs/operation-node/partition-by-item-node.d.ts +14 -0
- package/dist/cjs/operation-node/partition-by-item-node.js +18 -0
- package/dist/cjs/operation-node/partition-by-node.d.ts +14 -0
- package/dist/cjs/operation-node/partition-by-node.js +24 -0
- package/dist/cjs/operation-node/select-query-node.d.ts +3 -3
- package/dist/cjs/operation-node/select-query-node.js +4 -4
- package/dist/cjs/operation-node/set-operation-node.d.ts +18 -0
- package/dist/cjs/operation-node/set-operation-node.js +20 -0
- package/dist/cjs/operation-node/value-list-node.d.ts +3 -1
- package/dist/cjs/parser/complex-expression-parser.d.ts +6 -4
- package/dist/cjs/parser/group-by-parser.d.ts +1 -1
- package/dist/cjs/parser/group-by-parser.js +2 -2
- package/dist/cjs/parser/insert-values-parser.js +13 -4
- package/dist/cjs/parser/parse-utils.d.ts +2 -0
- package/dist/cjs/parser/parse-utils.js +9 -1
- package/dist/cjs/parser/partition-by-parser.d.ts +6 -0
- package/dist/cjs/parser/partition-by-parser.js +9 -0
- package/dist/cjs/parser/reference-parser.d.ts +2 -1
- package/dist/cjs/parser/select-parser.d.ts +4 -3
- package/dist/cjs/parser/set-operation-parser.d.ts +5 -0
- package/dist/cjs/parser/set-operation-parser.js +8 -0
- package/dist/cjs/parser/value-parser.d.ts +2 -1
- package/dist/cjs/query-builder/aggregate-function-builder.d.ts +117 -0
- package/dist/cjs/query-builder/aggregate-function-builder.js +146 -0
- package/dist/cjs/query-builder/expression-builder.d.ts +3 -3
- package/dist/cjs/query-builder/expression-builder.js +3 -3
- package/dist/cjs/query-builder/{function-builder.d.ts → function-module.d.ts} +49 -42
- package/dist/cjs/query-builder/{function-builder.js → function-module.js} +70 -59
- package/dist/cjs/query-builder/over-builder.d.ts +59 -0
- package/dist/cjs/query-builder/over-builder.js +50 -0
- package/dist/cjs/query-builder/select-query-builder.d.ts +63 -3
- package/dist/cjs/query-builder/select-query-builder.js +83 -3
- package/dist/cjs/query-compiler/default-query-compiler.d.ts +12 -2
- package/dist/cjs/query-compiler/default-query-compiler.js +44 -14
- package/dist/cjs/raw-builder/sql.d.ts +13 -5
- package/dist/cjs/raw-builder/sql.js +6 -0
- package/dist/cjs/util/object-utils.d.ts +1 -0
- package/dist/cjs/util/object-utils.js +5 -1
- package/dist/cjs/util/type-utils.d.ts +5 -1
- package/dist/esm/dialect/sqlite/sqlite-query-compiler.d.ts +2 -0
- package/dist/esm/dialect/sqlite/sqlite-query-compiler.js +4 -0
- package/dist/esm/index.d.ts +10 -4
- package/dist/esm/index.js +8 -2
- package/dist/esm/kysely.d.ts +3 -3
- package/dist/esm/kysely.js +3 -3
- package/dist/esm/migration/file-migration-provider.js +7 -2
- package/dist/esm/operation-node/aggregate-function-node.d.ts +22 -0
- package/dist/esm/operation-node/aggregate-function-node.js +29 -0
- package/dist/esm/operation-node/alias-node.d.ts +2 -1
- package/dist/esm/operation-node/default-insert-value-node.d.ts +11 -0
- package/dist/esm/operation-node/default-insert-value-node.js +15 -0
- package/dist/esm/operation-node/group-by-node.d.ts +1 -1
- package/dist/esm/operation-node/group-by-node.js +3 -3
- package/dist/esm/operation-node/insert-query-node.d.ts +2 -1
- package/dist/esm/operation-node/operation-node-transformer.d.ts +12 -2
- package/dist/esm/operation-node/operation-node-transformer.js +43 -5
- package/dist/esm/operation-node/operation-node-utils.d.ts +2 -1
- package/dist/esm/operation-node/operation-node-visitor.d.ts +12 -2
- package/dist/esm/operation-node/operation-node-visitor.js +6 -1
- package/dist/esm/operation-node/operation-node.d.ts +1 -1
- package/dist/esm/operation-node/over-node.d.ts +19 -0
- package/dist/esm/operation-node/over-node.js +33 -0
- package/dist/esm/operation-node/partition-by-item-node.d.ts +14 -0
- package/dist/esm/operation-node/partition-by-item-node.js +16 -0
- package/dist/esm/operation-node/partition-by-node.d.ts +14 -0
- package/dist/esm/operation-node/partition-by-node.js +22 -0
- package/dist/esm/operation-node/select-query-node.d.ts +3 -3
- package/dist/esm/operation-node/select-query-node.js +4 -4
- package/dist/esm/operation-node/set-operation-node.d.ts +18 -0
- package/dist/esm/operation-node/set-operation-node.js +18 -0
- package/dist/esm/operation-node/value-list-node.d.ts +3 -1
- package/dist/esm/parser/complex-expression-parser.d.ts +6 -4
- package/dist/esm/parser/group-by-parser.d.ts +1 -1
- package/dist/esm/parser/group-by-parser.js +2 -2
- package/dist/esm/parser/insert-values-parser.js +14 -5
- package/dist/esm/parser/parse-utils.d.ts +2 -0
- package/dist/esm/parser/parse-utils.js +7 -0
- package/dist/esm/parser/partition-by-parser.d.ts +6 -0
- package/dist/esm/parser/partition-by-parser.js +6 -0
- package/dist/esm/parser/reference-parser.d.ts +2 -1
- package/dist/esm/parser/select-parser.d.ts +4 -3
- package/dist/esm/parser/set-operation-parser.d.ts +5 -0
- package/dist/esm/parser/set-operation-parser.js +5 -0
- package/dist/esm/parser/value-parser.d.ts +2 -1
- package/dist/esm/query-builder/aggregate-function-builder.d.ts +117 -0
- package/dist/esm/query-builder/aggregate-function-builder.js +142 -0
- package/dist/esm/query-builder/expression-builder.d.ts +3 -3
- package/dist/esm/query-builder/expression-builder.js +3 -3
- package/dist/esm/query-builder/{function-builder.d.ts → function-module.d.ts} +49 -42
- package/dist/esm/query-builder/{function-builder.js → function-module.js} +69 -58
- package/dist/esm/query-builder/over-builder.d.ts +59 -0
- package/dist/esm/query-builder/over-builder.js +47 -0
- package/dist/esm/query-builder/select-query-builder.d.ts +63 -3
- package/dist/esm/query-builder/select-query-builder.js +83 -3
- package/dist/esm/query-compiler/default-query-compiler.d.ts +12 -2
- package/dist/esm/query-compiler/default-query-compiler.js +44 -14
- package/dist/esm/raw-builder/sql.d.ts +13 -5
- package/dist/esm/raw-builder/sql.js +6 -0
- package/dist/esm/util/object-utils.d.ts +1 -0
- package/dist/esm/util/object-utils.js +3 -0
- package/dist/esm/util/type-utils.d.ts +5 -1
- package/package.json +4 -1
- package/dist/cjs/operation-node/union-node.d.ts +0 -16
- package/dist/cjs/parser/union-parser.d.ts +0 -5
- package/dist/cjs/parser/union-parser.js +0 -8
- package/dist/esm/operation-node/union-node.d.ts +0 -16
- package/dist/esm/operation-node/union-node.js +0 -17
- package/dist/esm/parser/union-parser.d.ts +0 -5
- package/dist/esm/parser/union-parser.js +0 -5
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AliasedAggregateFunctionBuilder = exports.AggregateFunctionBuilder = void 0;
|
|
4
|
+
const object_utils_js_1 = require("../util/object-utils.js");
|
|
5
|
+
const aggregate_function_node_js_1 = require("../operation-node/aggregate-function-node.js");
|
|
6
|
+
const alias_node_js_1 = require("../operation-node/alias-node.js");
|
|
7
|
+
const identifier_node_js_1 = require("../operation-node/identifier-node.js");
|
|
8
|
+
const prevent_await_js_1 = require("../util/prevent-await.js");
|
|
9
|
+
const parse_utils_js_1 = require("../parser/parse-utils.js");
|
|
10
|
+
class AggregateFunctionBuilder {
|
|
11
|
+
#props;
|
|
12
|
+
constructor(props) {
|
|
13
|
+
this.#props = (0, object_utils_js_1.freeze)(props);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Returns an aliased version of the function.
|
|
17
|
+
*
|
|
18
|
+
* In addition to slapping `as "the_alias"` to the end of the SQL,
|
|
19
|
+
* this method also provides strict typing:
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* const result = await db
|
|
23
|
+
* .selectFrom('person')
|
|
24
|
+
* .select(
|
|
25
|
+
* eb => eb.fn.count<number>('id').as('person_count')
|
|
26
|
+
* )
|
|
27
|
+
* .executeTakeFirstOrThrow()
|
|
28
|
+
*
|
|
29
|
+
* // `person_count: number` field exists in the result type.
|
|
30
|
+
* console.log(result.person_count)
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* The generated SQL (PostgreSQL):
|
|
34
|
+
*
|
|
35
|
+
* ```sql
|
|
36
|
+
* select count("id") as "person_count"
|
|
37
|
+
* from "person"
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
as(alias) {
|
|
41
|
+
return new AliasedAggregateFunctionBuilder(this, alias);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Adds a distinct clause inside the function.
|
|
45
|
+
*
|
|
46
|
+
* ```ts
|
|
47
|
+
* const result = await db
|
|
48
|
+
* .selectFrom('person')
|
|
49
|
+
* .select(
|
|
50
|
+
* eb => eb.fn.count<number>('first_name').distinct().as('first_name_count')
|
|
51
|
+
* )
|
|
52
|
+
* .executeTakeFirstOrThrow()
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* The generated SQL (PostgreSQL):
|
|
56
|
+
*
|
|
57
|
+
* ```sql
|
|
58
|
+
* select count(distinct "first_name") as "first_name_count"
|
|
59
|
+
* from "person"
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
distinct() {
|
|
63
|
+
return new AggregateFunctionBuilder({
|
|
64
|
+
...this.#props,
|
|
65
|
+
aggregateFunctionNode: aggregate_function_node_js_1.AggregateFunctionNode.cloneWithDistinct(this.#props.aggregateFunctionNode),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Adds an over clause (window functions) after the function.
|
|
70
|
+
*
|
|
71
|
+
* ```ts
|
|
72
|
+
* const result = await db
|
|
73
|
+
* .selectFrom('person')
|
|
74
|
+
* .select(
|
|
75
|
+
* eb => eb.fn.avg<number>('age').over().as('average_age')
|
|
76
|
+
* )
|
|
77
|
+
* .execute()
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* The generated SQL (PostgreSQL):
|
|
81
|
+
*
|
|
82
|
+
* ```sql
|
|
83
|
+
* select avg("age") over() as "average_age"
|
|
84
|
+
* from "person"
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* Also supports passing a callback that returns an over builder,
|
|
88
|
+
* allowing to add partition by and sort by clauses inside over.
|
|
89
|
+
*
|
|
90
|
+
* ```ts
|
|
91
|
+
* const result = await db
|
|
92
|
+
* .selectFrom('person')
|
|
93
|
+
* .select(
|
|
94
|
+
* eb => eb.fn.avg<number>('age').over(
|
|
95
|
+
* ob => ob.partitionBy('last_name').orderBy('first_name', 'asc')
|
|
96
|
+
* ).as('average_age')
|
|
97
|
+
* )
|
|
98
|
+
* .execute()
|
|
99
|
+
* ```
|
|
100
|
+
*
|
|
101
|
+
* The generated SQL (PostgreSQL):
|
|
102
|
+
*
|
|
103
|
+
* ```sql
|
|
104
|
+
* select avg("age") over(partition by "last_name" order by "first_name" asc) as "average_age"
|
|
105
|
+
* from "person"
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
over(over) {
|
|
109
|
+
const builder = (0, parse_utils_js_1.createOverBuilder)();
|
|
110
|
+
return new AggregateFunctionBuilder({
|
|
111
|
+
...this.#props,
|
|
112
|
+
aggregateFunctionNode: aggregate_function_node_js_1.AggregateFunctionNode.cloneWithOver(this.#props.aggregateFunctionNode, (over ? over(builder) : builder).toOperationNode()),
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
toOperationNode() {
|
|
116
|
+
return this.#props.aggregateFunctionNode;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
exports.AggregateFunctionBuilder = AggregateFunctionBuilder;
|
|
120
|
+
(0, prevent_await_js_1.preventAwait)(AggregateFunctionBuilder, "don't await AggregateFunctionBuilder instances. They are never executed directly and are always just a part of a query.");
|
|
121
|
+
/**
|
|
122
|
+
* {@link AggregateFunctionBuilder} with an alias. The result of calling {@link AggregateFunctionBuilder.as}.
|
|
123
|
+
*/
|
|
124
|
+
class AliasedAggregateFunctionBuilder {
|
|
125
|
+
#aggregateFunctionBuilder;
|
|
126
|
+
#alias;
|
|
127
|
+
constructor(aggregateFunctionBuilder, alias) {
|
|
128
|
+
this.#aggregateFunctionBuilder = aggregateFunctionBuilder;
|
|
129
|
+
this.#alias = alias;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* @private
|
|
133
|
+
*
|
|
134
|
+
* This needs to be here just so that the typings work. Without this
|
|
135
|
+
* the generated .d.ts file contains no reference to the type param A
|
|
136
|
+
* which causes this type to be equal to AliasedAggregateFunctionBuilder with any A
|
|
137
|
+
* as long as O is the same.
|
|
138
|
+
*/
|
|
139
|
+
get alias() {
|
|
140
|
+
return this.#alias;
|
|
141
|
+
}
|
|
142
|
+
toOperationNode() {
|
|
143
|
+
return alias_node_js_1.AliasNode.create(this.#aggregateFunctionBuilder.toOperationNode(), identifier_node_js_1.IdentifierNode.create(this.#alias));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
exports.AliasedAggregateFunctionBuilder = AliasedAggregateFunctionBuilder;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { SelectQueryBuilder } from './select-query-builder.js';
|
|
2
2
|
import { TableExpression, From, FromTables } from '../parser/table-parser.js';
|
|
3
|
-
import {
|
|
3
|
+
import { FunctionModule } from './function-module.js';
|
|
4
4
|
import { ExtractTypeFromReferenceExpression, StringReference } from '../parser/reference-parser.js';
|
|
5
5
|
import { RawBuilder } from '../raw-builder/raw-builder.js';
|
|
6
6
|
import { QueryExecutor } from '../query-executor/query-executor.js';
|
|
@@ -8,7 +8,7 @@ export declare class ExpressionBuilder<DB, TB extends keyof DB> {
|
|
|
8
8
|
#private;
|
|
9
9
|
constructor(props: ExpressionBuilderProps);
|
|
10
10
|
/**
|
|
11
|
-
* Returns a {@link
|
|
11
|
+
* Returns a {@link FunctionModule} that can be used to write type safe function
|
|
12
12
|
* calls.
|
|
13
13
|
*
|
|
14
14
|
* The difference between this and {@link Kysely.fn} is that this one is more
|
|
@@ -38,7 +38,7 @@ export declare class ExpressionBuilder<DB, TB extends keyof DB> {
|
|
|
38
38
|
* having count("pet"."id") > $1
|
|
39
39
|
* ```
|
|
40
40
|
*/
|
|
41
|
-
get fn():
|
|
41
|
+
get fn(): FunctionModule<DB, TB>;
|
|
42
42
|
/**
|
|
43
43
|
* Creates a subquery.
|
|
44
44
|
*
|
|
@@ -6,7 +6,7 @@ const select_query_node_js_1 = require("../operation-node/select-query-node.js")
|
|
|
6
6
|
const table_parser_js_1 = require("../parser/table-parser.js");
|
|
7
7
|
const with_schema_plugin_js_1 = require("../plugin/with-schema/with-schema-plugin.js");
|
|
8
8
|
const query_id_js_1 = require("../util/query-id.js");
|
|
9
|
-
const
|
|
9
|
+
const function_module_js_1 = require("./function-module.js");
|
|
10
10
|
const reference_parser_js_1 = require("../parser/reference-parser.js");
|
|
11
11
|
const raw_builder_js_1 = require("../raw-builder/raw-builder.js");
|
|
12
12
|
const object_utils_js_1 = require("../util/object-utils.js");
|
|
@@ -17,7 +17,7 @@ class ExpressionBuilder {
|
|
|
17
17
|
this.#props = (0, object_utils_js_1.freeze)(props);
|
|
18
18
|
}
|
|
19
19
|
/**
|
|
20
|
-
* Returns a {@link
|
|
20
|
+
* Returns a {@link FunctionModule} that can be used to write type safe function
|
|
21
21
|
* calls.
|
|
22
22
|
*
|
|
23
23
|
* The difference between this and {@link Kysely.fn} is that this one is more
|
|
@@ -48,7 +48,7 @@ class ExpressionBuilder {
|
|
|
48
48
|
* ```
|
|
49
49
|
*/
|
|
50
50
|
get fn() {
|
|
51
|
-
return new
|
|
51
|
+
return new function_module_js_1.FunctionModule();
|
|
52
52
|
}
|
|
53
53
|
selectFrom(table) {
|
|
54
54
|
return new select_query_builder_js_1.SelectQueryBuilder({
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { StringReference, ExtractTypeFromReferenceExpression } from '../parser/reference-parser.js';
|
|
2
|
-
import {
|
|
2
|
+
import { AggregateFunctionBuilder } from './aggregate-function-builder.js';
|
|
3
3
|
/**
|
|
4
4
|
* Helpers for type safe SQL function calls.
|
|
5
5
|
*
|
|
@@ -32,38 +32,8 @@ import { RawBuilder } from '../raw-builder/raw-builder.js';
|
|
|
32
32
|
* having count("pet"."id") > $1
|
|
33
33
|
* ```
|
|
34
34
|
*/
|
|
35
|
-
export declare class
|
|
36
|
-
#private;
|
|
35
|
+
export declare class FunctionModule<DB, TB extends keyof DB> {
|
|
37
36
|
constructor();
|
|
38
|
-
/**
|
|
39
|
-
* Calls the `max` function for the column given as the argument.
|
|
40
|
-
*
|
|
41
|
-
* ### Examples
|
|
42
|
-
*
|
|
43
|
-
* ```ts
|
|
44
|
-
* const { max } = db.fn
|
|
45
|
-
*
|
|
46
|
-
* db.selectFrom('toy')
|
|
47
|
-
* .select(max('price').as('max_price'))
|
|
48
|
-
* .execute()
|
|
49
|
-
* ```
|
|
50
|
-
*/
|
|
51
|
-
max<C extends StringReference<DB, TB>>(column: C): RawBuilder<ExtractTypeFromReferenceExpression<DB, TB, C>>;
|
|
52
|
-
/**
|
|
53
|
-
* Calls the `min` function for the column given as the argument.
|
|
54
|
-
*
|
|
55
|
-
* ### Examples
|
|
56
|
-
*
|
|
57
|
-
* ```ts
|
|
58
|
-
* const { min } = db.fn
|
|
59
|
-
*
|
|
60
|
-
* db.selectFrom('toy')
|
|
61
|
-
* .select(min('price').as('min_price'))
|
|
62
|
-
* .execute()
|
|
63
|
-
* ```
|
|
64
|
-
*
|
|
65
|
-
*/
|
|
66
|
-
min<C extends StringReference<DB, TB>>(column: C): RawBuilder<ExtractTypeFromReferenceExpression<DB, TB, C>>;
|
|
67
37
|
/**
|
|
68
38
|
* Calls the `avg` function for the column given as the argument.
|
|
69
39
|
*
|
|
@@ -85,9 +55,9 @@ export declare class FunctionBuilder<DB, TB extends keyof DB> {
|
|
|
85
55
|
* .execute()
|
|
86
56
|
* ```
|
|
87
57
|
*/
|
|
88
|
-
avg<O extends number | string, C extends StringReference<DB, TB> = StringReference<DB, TB>>(column: C):
|
|
58
|
+
avg<O extends number | string, C extends StringReference<DB, TB> = StringReference<DB, TB>>(column: C): AggregateFunctionBuilder<DB, TB, O>;
|
|
89
59
|
/**
|
|
90
|
-
* Calls the `
|
|
60
|
+
* Calls the `count` function for the column given as the argument.
|
|
91
61
|
*
|
|
92
62
|
* If this is used in a `select` statement the type of the selected expression
|
|
93
63
|
* will be `number | string | bigint` by default. This is because Kysely can't
|
|
@@ -100,19 +70,56 @@ export declare class FunctionBuilder<DB, TB extends keyof DB> {
|
|
|
100
70
|
* the type as the first type argument:
|
|
101
71
|
*
|
|
102
72
|
* ```ts
|
|
103
|
-
* const {
|
|
73
|
+
* const { count } = db.fn
|
|
104
74
|
*
|
|
105
75
|
* db.selectFrom('toy')
|
|
106
|
-
* .select(
|
|
76
|
+
* .select(count<number>('id').as('num_toys'))
|
|
77
|
+
* .execute()
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* You can limit column range:
|
|
81
|
+
*
|
|
82
|
+
* ```ts
|
|
83
|
+
* db.selectFrom('toy')
|
|
84
|
+
* .select(qb => qb.fn.count<number>('id').as('num_toys'))
|
|
107
85
|
* .execute()
|
|
108
86
|
* ```
|
|
109
87
|
*/
|
|
110
|
-
|
|
88
|
+
count<O extends number | string | bigint, C extends StringReference<DB, TB> = StringReference<DB, TB>>(column: C): AggregateFunctionBuilder<DB, TB, O>;
|
|
111
89
|
/**
|
|
112
|
-
* Calls the `
|
|
90
|
+
* Calls the `max` function for the column given as the argument.
|
|
91
|
+
*
|
|
92
|
+
* ### Examples
|
|
93
|
+
*
|
|
94
|
+
* ```ts
|
|
95
|
+
* const { max } = db.fn
|
|
96
|
+
*
|
|
97
|
+
* db.selectFrom('toy')
|
|
98
|
+
* .select(max('price').as('max_price'))
|
|
99
|
+
* .execute()
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
max<C extends StringReference<DB, TB>>(column: C): AggregateFunctionBuilder<DB, TB, ExtractTypeFromReferenceExpression<DB, TB, C>>;
|
|
103
|
+
/**
|
|
104
|
+
* Calls the `min` function for the column given as the argument.
|
|
105
|
+
*
|
|
106
|
+
* ### Examples
|
|
107
|
+
*
|
|
108
|
+
* ```ts
|
|
109
|
+
* const { min } = db.fn
|
|
110
|
+
*
|
|
111
|
+
* db.selectFrom('toy')
|
|
112
|
+
* .select(min('price').as('min_price'))
|
|
113
|
+
* .execute()
|
|
114
|
+
* ```
|
|
115
|
+
*
|
|
116
|
+
*/
|
|
117
|
+
min<C extends StringReference<DB, TB>>(column: C): AggregateFunctionBuilder<DB, TB, ExtractTypeFromReferenceExpression<DB, TB, C>>;
|
|
118
|
+
/**
|
|
119
|
+
* Calls the `sum` function for the column given as the argument.
|
|
113
120
|
*
|
|
114
121
|
* If this is used in a `select` statement the type of the selected expression
|
|
115
|
-
* will be `number | string |bigint` by default. This is because Kysely can't
|
|
122
|
+
* will be `number | string | bigint` by default. This is because Kysely can't
|
|
116
123
|
* know the type the db driver outputs. Sometimes the output can be larger than
|
|
117
124
|
* the largest javascript number and a string is returned instead. Most drivers
|
|
118
125
|
* allow you to configure the output type of large numbers and Kysely can't
|
|
@@ -122,12 +129,12 @@ export declare class FunctionBuilder<DB, TB extends keyof DB> {
|
|
|
122
129
|
* the type as the first type argument:
|
|
123
130
|
*
|
|
124
131
|
* ```ts
|
|
125
|
-
* const {
|
|
132
|
+
* const { sum } = db.fn
|
|
126
133
|
*
|
|
127
134
|
* db.selectFrom('toy')
|
|
128
|
-
* .select(
|
|
135
|
+
* .select(sum<number>('price').as('total_price'))
|
|
129
136
|
* .execute()
|
|
130
137
|
* ```
|
|
131
138
|
*/
|
|
132
|
-
|
|
139
|
+
sum<O extends number | string | bigint, C extends StringReference<DB, TB> = StringReference<DB, TB>>(column: C): AggregateFunctionBuilder<DB, TB, O>;
|
|
133
140
|
}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const
|
|
3
|
+
exports.FunctionModule = void 0;
|
|
4
|
+
const aggregate_function_node_js_1 = require("../operation-node/aggregate-function-node.js");
|
|
5
5
|
const reference_parser_js_1 = require("../parser/reference-parser.js");
|
|
6
|
-
const
|
|
7
|
-
const query_id_js_1 = require("../util/query-id.js");
|
|
6
|
+
const aggregate_function_builder_js_1 = require("./aggregate-function-builder.js");
|
|
8
7
|
/**
|
|
9
8
|
* Helpers for type safe SQL function calls.
|
|
10
9
|
*
|
|
@@ -37,46 +36,13 @@ const query_id_js_1 = require("../util/query-id.js");
|
|
|
37
36
|
* having count("pet"."id") > $1
|
|
38
37
|
* ```
|
|
39
38
|
*/
|
|
40
|
-
class
|
|
39
|
+
class FunctionModule {
|
|
41
40
|
constructor() {
|
|
42
|
-
this.min = this.min.bind(this);
|
|
43
|
-
this.max = this.max.bind(this);
|
|
44
41
|
this.avg = this.avg.bind(this);
|
|
45
|
-
this.sum = this.sum.bind(this);
|
|
46
42
|
this.count = this.count.bind(this);
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
*
|
|
51
|
-
* ### Examples
|
|
52
|
-
*
|
|
53
|
-
* ```ts
|
|
54
|
-
* const { max } = db.fn
|
|
55
|
-
*
|
|
56
|
-
* db.selectFrom('toy')
|
|
57
|
-
* .select(max('price').as('max_price'))
|
|
58
|
-
* .execute()
|
|
59
|
-
* ```
|
|
60
|
-
*/
|
|
61
|
-
max(column) {
|
|
62
|
-
return this.#oneArgFunction('max', column);
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Calls the `min` function for the column given as the argument.
|
|
66
|
-
*
|
|
67
|
-
* ### Examples
|
|
68
|
-
*
|
|
69
|
-
* ```ts
|
|
70
|
-
* const { min } = db.fn
|
|
71
|
-
*
|
|
72
|
-
* db.selectFrom('toy')
|
|
73
|
-
* .select(min('price').as('min_price'))
|
|
74
|
-
* .execute()
|
|
75
|
-
* ```
|
|
76
|
-
*
|
|
77
|
-
*/
|
|
78
|
-
min(column) {
|
|
79
|
-
return this.#oneArgFunction('min', column);
|
|
43
|
+
this.max = this.max.bind(this);
|
|
44
|
+
this.min = this.min.bind(this);
|
|
45
|
+
this.sum = this.sum.bind(this);
|
|
80
46
|
}
|
|
81
47
|
/**
|
|
82
48
|
* Calls the `avg` function for the column given as the argument.
|
|
@@ -100,10 +66,12 @@ class FunctionBuilder {
|
|
|
100
66
|
* ```
|
|
101
67
|
*/
|
|
102
68
|
avg(column) {
|
|
103
|
-
return
|
|
69
|
+
return new aggregate_function_builder_js_1.AggregateFunctionBuilder({
|
|
70
|
+
aggregateFunctionNode: aggregate_function_node_js_1.AggregateFunctionNode.create('avg', (0, reference_parser_js_1.parseStringReference)(column)),
|
|
71
|
+
});
|
|
104
72
|
}
|
|
105
73
|
/**
|
|
106
|
-
* Calls the `
|
|
74
|
+
* Calls the `count` function for the column given as the argument.
|
|
107
75
|
*
|
|
108
76
|
* If this is used in a `select` statement the type of the selected expression
|
|
109
77
|
* will be `number | string | bigint` by default. This is because Kysely can't
|
|
@@ -116,21 +84,68 @@ class FunctionBuilder {
|
|
|
116
84
|
* the type as the first type argument:
|
|
117
85
|
*
|
|
118
86
|
* ```ts
|
|
119
|
-
* const {
|
|
87
|
+
* const { count } = db.fn
|
|
120
88
|
*
|
|
121
89
|
* db.selectFrom('toy')
|
|
122
|
-
* .select(
|
|
90
|
+
* .select(count<number>('id').as('num_toys'))
|
|
91
|
+
* .execute()
|
|
92
|
+
* ```
|
|
93
|
+
*
|
|
94
|
+
* You can limit column range:
|
|
95
|
+
*
|
|
96
|
+
* ```ts
|
|
97
|
+
* db.selectFrom('toy')
|
|
98
|
+
* .select(qb => qb.fn.count<number>('id').as('num_toys'))
|
|
123
99
|
* .execute()
|
|
124
100
|
* ```
|
|
125
101
|
*/
|
|
126
|
-
|
|
127
|
-
return
|
|
102
|
+
count(column) {
|
|
103
|
+
return new aggregate_function_builder_js_1.AggregateFunctionBuilder({
|
|
104
|
+
aggregateFunctionNode: aggregate_function_node_js_1.AggregateFunctionNode.create('count', (0, reference_parser_js_1.parseStringReference)(column)),
|
|
105
|
+
});
|
|
128
106
|
}
|
|
129
107
|
/**
|
|
130
|
-
* Calls the `
|
|
108
|
+
* Calls the `max` function for the column given as the argument.
|
|
109
|
+
*
|
|
110
|
+
* ### Examples
|
|
111
|
+
*
|
|
112
|
+
* ```ts
|
|
113
|
+
* const { max } = db.fn
|
|
114
|
+
*
|
|
115
|
+
* db.selectFrom('toy')
|
|
116
|
+
* .select(max('price').as('max_price'))
|
|
117
|
+
* .execute()
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
max(column) {
|
|
121
|
+
return new aggregate_function_builder_js_1.AggregateFunctionBuilder({
|
|
122
|
+
aggregateFunctionNode: aggregate_function_node_js_1.AggregateFunctionNode.create('max', (0, reference_parser_js_1.parseStringReference)(column)),
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Calls the `min` function for the column given as the argument.
|
|
127
|
+
*
|
|
128
|
+
* ### Examples
|
|
129
|
+
*
|
|
130
|
+
* ```ts
|
|
131
|
+
* const { min } = db.fn
|
|
132
|
+
*
|
|
133
|
+
* db.selectFrom('toy')
|
|
134
|
+
* .select(min('price').as('min_price'))
|
|
135
|
+
* .execute()
|
|
136
|
+
* ```
|
|
137
|
+
*
|
|
138
|
+
*/
|
|
139
|
+
min(column) {
|
|
140
|
+
return new aggregate_function_builder_js_1.AggregateFunctionBuilder({
|
|
141
|
+
aggregateFunctionNode: aggregate_function_node_js_1.AggregateFunctionNode.create('min', (0, reference_parser_js_1.parseStringReference)(column)),
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Calls the `sum` function for the column given as the argument.
|
|
131
146
|
*
|
|
132
147
|
* If this is used in a `select` statement the type of the selected expression
|
|
133
|
-
* will be `number | string |bigint` by default. This is because Kysely can't
|
|
148
|
+
* will be `number | string | bigint` by default. This is because Kysely can't
|
|
134
149
|
* know the type the db driver outputs. Sometimes the output can be larger than
|
|
135
150
|
* the largest javascript number and a string is returned instead. Most drivers
|
|
136
151
|
* allow you to configure the output type of large numbers and Kysely can't
|
|
@@ -140,21 +155,17 @@ class FunctionBuilder {
|
|
|
140
155
|
* the type as the first type argument:
|
|
141
156
|
*
|
|
142
157
|
* ```ts
|
|
143
|
-
* const {
|
|
158
|
+
* const { sum } = db.fn
|
|
144
159
|
*
|
|
145
160
|
* db.selectFrom('toy')
|
|
146
|
-
* .select(
|
|
161
|
+
* .select(sum<number>('price').as('total_price'))
|
|
147
162
|
* .execute()
|
|
148
163
|
* ```
|
|
149
164
|
*/
|
|
150
|
-
|
|
151
|
-
return
|
|
152
|
-
|
|
153
|
-
#oneArgFunction(fn, column) {
|
|
154
|
-
return new raw_builder_js_1.RawBuilder({
|
|
155
|
-
queryId: (0, query_id_js_1.createQueryId)(),
|
|
156
|
-
rawNode: raw_node_js_1.RawNode.create([`${fn}(`, ')'], [(0, reference_parser_js_1.parseStringReference)(column)]),
|
|
165
|
+
sum(column) {
|
|
166
|
+
return new aggregate_function_builder_js_1.AggregateFunctionBuilder({
|
|
167
|
+
aggregateFunctionNode: aggregate_function_node_js_1.AggregateFunctionNode.create('sum', (0, reference_parser_js_1.parseStringReference)(column)),
|
|
157
168
|
});
|
|
158
169
|
}
|
|
159
170
|
}
|
|
160
|
-
exports.
|
|
171
|
+
exports.FunctionModule = FunctionModule;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { DynamicReferenceBuilder } from '../dynamic/dynamic-reference-builder.js';
|
|
2
|
+
import { OperationNodeSource } from '../operation-node/operation-node-source.js';
|
|
3
|
+
import { OverNode } from '../operation-node/over-node.js';
|
|
4
|
+
import { OrderByDirectionExpression } from '../parser/order-by-parser.js';
|
|
5
|
+
import { PartitionByExpression } from '../parser/partition-by-parser.js';
|
|
6
|
+
import { StringReference } from '../parser/reference-parser.js';
|
|
7
|
+
export declare class OverBuilder<DB, TB extends keyof DB> implements OperationNodeSource {
|
|
8
|
+
#private;
|
|
9
|
+
constructor(props: OverBuilderProps);
|
|
10
|
+
/**
|
|
11
|
+
* Adds an order by clause item inside the over function.
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* const result = await db
|
|
15
|
+
* .selectFrom('person')
|
|
16
|
+
* .select(
|
|
17
|
+
* eb => eb.fn.avg<number>('age').over(
|
|
18
|
+
* ob => ob.orderBy('first_name', 'asc').orderBy('last_name', 'asc')
|
|
19
|
+
* ).as('average_age')
|
|
20
|
+
* )
|
|
21
|
+
* .execute()
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* The generated SQL (PostgreSQL):
|
|
25
|
+
*
|
|
26
|
+
* ```sql
|
|
27
|
+
* select avg("age") over(order by "first_name" asc, "last_name" asc) as "average_age"
|
|
28
|
+
* from "person"
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
orderBy(orderBy: StringReference<DB, TB> | DynamicReferenceBuilder<any>, direction?: OrderByDirectionExpression): OverBuilder<DB, TB>;
|
|
32
|
+
/**
|
|
33
|
+
* Adds partition by clause item/s inside the over function.
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* const result = await db
|
|
37
|
+
* .selectFrom('person')
|
|
38
|
+
* .select(
|
|
39
|
+
* eb => eb.fn.avg<number>('age').over(
|
|
40
|
+
* ob => ob.partitionBy(['last_name', 'first_name'])
|
|
41
|
+
* ).as('average_age')
|
|
42
|
+
* )
|
|
43
|
+
* .execute()
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* The generated SQL (PostgreSQL):
|
|
47
|
+
*
|
|
48
|
+
* ```sql
|
|
49
|
+
* select avg("age") over(partition by "last_name", "first_name") as "average_age"
|
|
50
|
+
* from "person"
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
partitionBy(partitionBy: ReadonlyArray<PartitionByExpression<DB, TB>>): OverBuilder<DB, TB>;
|
|
54
|
+
partitionBy(partitionBy: PartitionByExpression<DB, TB>): OverBuilder<DB, TB>;
|
|
55
|
+
toOperationNode(): OverNode;
|
|
56
|
+
}
|
|
57
|
+
export interface OverBuilderProps {
|
|
58
|
+
readonly overNode: OverNode;
|
|
59
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OverBuilder = void 0;
|
|
4
|
+
const over_node_js_1 = require("../operation-node/over-node.js");
|
|
5
|
+
const order_by_parser_js_1 = require("../parser/order-by-parser.js");
|
|
6
|
+
const partition_by_parser_js_1 = require("../parser/partition-by-parser.js");
|
|
7
|
+
const object_utils_js_1 = require("../util/object-utils.js");
|
|
8
|
+
const prevent_await_js_1 = require("../util/prevent-await.js");
|
|
9
|
+
class OverBuilder {
|
|
10
|
+
#props;
|
|
11
|
+
constructor(props) {
|
|
12
|
+
this.#props = (0, object_utils_js_1.freeze)(props);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Adds an order by clause item inside the over function.
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* const result = await db
|
|
19
|
+
* .selectFrom('person')
|
|
20
|
+
* .select(
|
|
21
|
+
* eb => eb.fn.avg<number>('age').over(
|
|
22
|
+
* ob => ob.orderBy('first_name', 'asc').orderBy('last_name', 'asc')
|
|
23
|
+
* ).as('average_age')
|
|
24
|
+
* )
|
|
25
|
+
* .execute()
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* The generated SQL (PostgreSQL):
|
|
29
|
+
*
|
|
30
|
+
* ```sql
|
|
31
|
+
* select avg("age") over(order by "first_name" asc, "last_name" asc) as "average_age"
|
|
32
|
+
* from "person"
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
orderBy(orderBy, direction) {
|
|
36
|
+
return new OverBuilder({
|
|
37
|
+
overNode: over_node_js_1.OverNode.cloneWithOrderByItem(this.#props.overNode, (0, order_by_parser_js_1.parseOrderBy)(orderBy, direction)),
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
partitionBy(partitionBy) {
|
|
41
|
+
return new OverBuilder({
|
|
42
|
+
overNode: over_node_js_1.OverNode.cloneWithPartitionByItems(this.#props.overNode, (0, partition_by_parser_js_1.parsePartitionBy)(partitionBy)),
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
toOperationNode() {
|
|
46
|
+
return this.#props.overNode;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.OverBuilder = OverBuilder;
|
|
50
|
+
(0, prevent_await_js_1.preventAwait)(OverBuilder, "don't await OverBuilder instances. They are never executed directly and are always just a part of a query.");
|