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