kysely 0.7.7 → 0.8.1
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 +29 -2
- package/dist/cjs/dialect/dialect-adapter.d.ts +19 -12
- package/dist/cjs/dialect/dialect.d.ts +1 -1
- package/dist/cjs/dialect/mysql/mysql-adapter.js +6 -0
- package/dist/cjs/dialect/mysql/mysql-driver.js +5 -5
- package/dist/cjs/dialect/postgres/postgres-driver.js +5 -5
- package/dist/cjs/driver/database-connection.d.ts +5 -0
- package/dist/cjs/driver/driver.d.ts +3 -5
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/kysely.d.ts +31 -1
- package/dist/cjs/kysely.js +33 -1
- package/dist/cjs/operation-node/operator-node.d.ts +1 -1
- package/dist/cjs/parser/data-type-parser.d.ts +1 -1
- package/dist/cjs/parser/default-value-parser.d.ts +1 -1
- package/dist/cjs/parser/filter-parser.d.ts +1 -1
- package/dist/cjs/parser/insert-values-parser.d.ts +2 -2
- package/dist/cjs/parser/join-parser.d.ts +1 -1
- package/dist/cjs/parser/on-conflict-parser.d.ts +1 -1
- package/dist/cjs/parser/order-by-parser.d.ts +1 -1
- package/dist/cjs/parser/reference-parser.d.ts +3 -2
- package/dist/cjs/parser/returning-parser.d.ts +1 -1
- package/dist/cjs/parser/select-parser.d.ts +1 -1
- package/dist/cjs/parser/table-parser.d.ts +17 -6
- package/dist/cjs/parser/update-set-parser.d.ts +2 -2
- package/dist/cjs/parser/value-parser.d.ts +1 -1
- package/dist/cjs/parser/with-parser.d.ts +1 -1
- package/dist/cjs/plugin/camel-case/camel-case-plugin.d.ts +4 -3
- package/dist/cjs/plugin/kysely-plugin.d.ts +9 -5
- package/dist/cjs/plugin/with-schema/with-schema-plugin.d.ts +3 -2
- package/dist/cjs/query-builder/expression-builder.d.ts +33 -0
- package/dist/cjs/query-builder/expression-builder.js +35 -0
- package/dist/cjs/query-builder/function-builder.d.ts +112 -0
- package/dist/cjs/query-builder/function-builder.js +138 -0
- package/dist/cjs/query-builder/query-builder.d.ts +8 -8
- package/dist/cjs/query-compiler/compiled-query.d.ts +1 -1
- package/dist/cjs/query-compiler/default-query-compiler.d.ts +2 -2
- package/dist/cjs/query-compiler/default-query-compiler.js +9 -9
- package/dist/cjs/query-compiler/query-compiler.d.ts +3 -0
- package/dist/cjs/query-creator.d.ts +9 -9
- package/dist/cjs/query-creator.js +11 -10
- package/dist/cjs/raw-builder/raw-builder.d.ts +14 -5
- package/dist/cjs/raw-builder/raw-builder.js +25 -20
- package/dist/cjs/schema/alter-table-builder.d.ts +1 -1
- package/dist/cjs/schema/column-definition-builder.d.ts +1 -1
- package/dist/cjs/util/generated-placeholder.d.ts +2 -2
- package/dist/cjs/util/generated-placeholder.js +2 -2
- package/dist/cjs/util/random-string.js +3 -3
- package/dist/cjs/{query-builder → util}/type-utils.d.ts +11 -3
- package/dist/cjs/{query-builder → util}/type-utils.js +0 -0
- package/dist/esm/dialect/dialect-adapter.d.ts +19 -12
- package/dist/esm/dialect/dialect.d.ts +1 -1
- package/dist/esm/dialect/mysql/mysql-adapter.js +6 -0
- package/dist/esm/dialect/mysql/mysql-driver.js +5 -5
- package/dist/esm/dialect/postgres/postgres-driver.js +5 -5
- package/dist/esm/driver/database-connection.d.ts +5 -0
- package/dist/esm/driver/driver.d.ts +3 -5
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/kysely.d.ts +31 -1
- package/dist/esm/kysely.js +34 -2
- package/dist/esm/operation-node/operator-node.d.ts +1 -1
- package/dist/esm/parser/data-type-parser.d.ts +1 -1
- package/dist/esm/parser/default-value-parser.d.ts +1 -1
- package/dist/esm/parser/filter-parser.d.ts +1 -1
- package/dist/esm/parser/insert-values-parser.d.ts +2 -2
- package/dist/esm/parser/join-parser.d.ts +1 -1
- package/dist/esm/parser/on-conflict-parser.d.ts +1 -1
- package/dist/esm/parser/order-by-parser.d.ts +1 -1
- package/dist/esm/parser/reference-parser.d.ts +3 -2
- package/dist/esm/parser/returning-parser.d.ts +1 -1
- package/dist/esm/parser/select-parser.d.ts +1 -1
- package/dist/esm/parser/table-parser.d.ts +17 -6
- package/dist/esm/parser/update-set-parser.d.ts +2 -2
- package/dist/esm/parser/value-parser.d.ts +1 -1
- package/dist/esm/parser/with-parser.d.ts +1 -1
- package/dist/esm/plugin/camel-case/camel-case-plugin.d.ts +4 -3
- package/dist/esm/plugin/kysely-plugin.d.ts +9 -5
- package/dist/esm/plugin/with-schema/with-schema-plugin.d.ts +3 -2
- package/dist/esm/query-builder/expression-builder.d.ts +33 -0
- package/dist/esm/query-builder/expression-builder.js +35 -0
- package/dist/esm/query-builder/function-builder.d.ts +112 -0
- package/dist/esm/query-builder/function-builder.js +134 -0
- package/dist/esm/query-builder/query-builder.d.ts +8 -8
- package/dist/esm/query-compiler/compiled-query.d.ts +1 -1
- package/dist/esm/query-compiler/default-query-compiler.d.ts +2 -2
- package/dist/esm/query-compiler/default-query-compiler.js +9 -9
- package/dist/esm/query-compiler/query-compiler.d.ts +3 -0
- package/dist/esm/query-creator.d.ts +9 -9
- package/dist/esm/query-creator.js +11 -10
- package/dist/esm/raw-builder/raw-builder.d.ts +14 -5
- package/dist/esm/raw-builder/raw-builder.js +25 -20
- package/dist/esm/schema/alter-table-builder.d.ts +1 -1
- package/dist/esm/schema/column-definition-builder.d.ts +1 -1
- package/dist/esm/util/generated-placeholder.d.ts +2 -2
- package/dist/esm/util/generated-placeholder.js +1 -1
- package/dist/esm/util/random-string.js +3 -3
- package/dist/esm/{query-builder → util}/type-utils.d.ts +11 -3
- package/dist/esm/{query-builder → util}/type-utils.js +0 -0
- package/package.json +8 -8
|
@@ -1,9 +1,42 @@
|
|
|
1
1
|
import { TableExpression, QueryBuilderWithTable } from '../parser/table-parser.js';
|
|
2
2
|
import { NoopQueryExecutor } from '../query-executor/noop-query-executor.js';
|
|
3
3
|
import { ParseContext } from '../parser/parse-context.js';
|
|
4
|
+
import { FunctionBuilder } from './function-builder.js';
|
|
4
5
|
export declare class ExpressionBuilder<DB, TB extends keyof DB> {
|
|
5
6
|
#private;
|
|
6
7
|
constructor(props: ExpressionBuilderProps);
|
|
8
|
+
/**
|
|
9
|
+
* Returns a {@link FunctionBuilder} that can be used to write type safe function
|
|
10
|
+
* calls.
|
|
11
|
+
*
|
|
12
|
+
* The difference between this and {@link Kysely.fn} is that this one is more
|
|
13
|
+
* type safe. You can only refer to columns visible to the part of the query
|
|
14
|
+
* you are building. {@link Kysely.fn} allows you to refer to columns in any
|
|
15
|
+
* table of the database even if it doesn't produce valid SQL.
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* await db.selectFrom('person')
|
|
19
|
+
* .innerJoin('pet', 'pet.owner_id', 'person.id')
|
|
20
|
+
* .select([
|
|
21
|
+
* 'person.id',
|
|
22
|
+
* (qb) => qb.fn.count('pet.id').as('pet_count')
|
|
23
|
+
* ])
|
|
24
|
+
* .groupBy('person.id')
|
|
25
|
+
* .having(count('pet.id'), '>', 10)
|
|
26
|
+
* .execute()
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* The generated SQL (postgresql):
|
|
30
|
+
*
|
|
31
|
+
* ```sql
|
|
32
|
+
* select "person"."id", count("pet"."id") as "pet_count"
|
|
33
|
+
* from "person"
|
|
34
|
+
* inner join "pet" on "pet"."owner_id" = "person"."id"
|
|
35
|
+
* group by "person"."id"
|
|
36
|
+
* having count("pet"."id") > $1
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
get fn(): FunctionBuilder<DB, TB>;
|
|
7
40
|
/**
|
|
8
41
|
* Creates a subquery.
|
|
9
42
|
*
|
|
@@ -7,11 +7,46 @@ 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
9
|
const object_utils_js_1 = require("../util/object-utils.js");
|
|
10
|
+
const function_builder_js_1 = require("./function-builder.js");
|
|
10
11
|
class ExpressionBuilder {
|
|
11
12
|
#props;
|
|
12
13
|
constructor(props) {
|
|
13
14
|
this.#props = (0, object_utils_js_1.freeze)(props);
|
|
14
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Returns a {@link FunctionBuilder} that can be used to write type safe function
|
|
18
|
+
* calls.
|
|
19
|
+
*
|
|
20
|
+
* The difference between this and {@link Kysely.fn} is that this one is more
|
|
21
|
+
* type safe. You can only refer to columns visible to the part of the query
|
|
22
|
+
* you are building. {@link Kysely.fn} allows you to refer to columns in any
|
|
23
|
+
* table of the database even if it doesn't produce valid SQL.
|
|
24
|
+
*
|
|
25
|
+
* ```ts
|
|
26
|
+
* await db.selectFrom('person')
|
|
27
|
+
* .innerJoin('pet', 'pet.owner_id', 'person.id')
|
|
28
|
+
* .select([
|
|
29
|
+
* 'person.id',
|
|
30
|
+
* (qb) => qb.fn.count('pet.id').as('pet_count')
|
|
31
|
+
* ])
|
|
32
|
+
* .groupBy('person.id')
|
|
33
|
+
* .having(count('pet.id'), '>', 10)
|
|
34
|
+
* .execute()
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* The generated SQL (postgresql):
|
|
38
|
+
*
|
|
39
|
+
* ```sql
|
|
40
|
+
* select "person"."id", count("pet"."id") as "pet_count"
|
|
41
|
+
* from "person"
|
|
42
|
+
* inner join "pet" on "pet"."owner_id" = "person"."id"
|
|
43
|
+
* group by "person"."id"
|
|
44
|
+
* having count("pet"."id") > $1
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
get fn() {
|
|
48
|
+
return new function_builder_js_1.FunctionBuilder({ executor: this.#props.executor });
|
|
49
|
+
}
|
|
15
50
|
subQuery(table) {
|
|
16
51
|
return new query_builder_js_1.QueryBuilder({
|
|
17
52
|
queryId: (0, query_id_js_1.createQueryId)(),
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { StringReference, ExtractTypeFromReferenceExpression } from '../parser/reference-parser.js';
|
|
2
|
+
import { QueryExecutor } from '../query-executor/query-executor.js';
|
|
3
|
+
import { RawBuilder } from '../raw-builder/raw-builder.js';
|
|
4
|
+
/**
|
|
5
|
+
* Helpers for type safe SQL function calls.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const { count } = db.fn
|
|
10
|
+
*
|
|
11
|
+
* await db.selectFrom('person')
|
|
12
|
+
* .innerJoin('pet', 'pet.owner_id', 'person.id')
|
|
13
|
+
* .select([
|
|
14
|
+
* 'person.id',
|
|
15
|
+
* count('pet.id').as('pet_count')
|
|
16
|
+
* ])
|
|
17
|
+
* .groupBy('person.id')
|
|
18
|
+
* .having(count('pet.id'), '>', 10)
|
|
19
|
+
* .execute()
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* The generated SQL (postgresql):
|
|
23
|
+
*
|
|
24
|
+
* ```sql
|
|
25
|
+
* select "person"."id", count("pet"."id") as "pet_count"
|
|
26
|
+
* from "person"
|
|
27
|
+
* inner join "pet" on "pet"."owner_id" = "person"."id"
|
|
28
|
+
* group by "person"."id"
|
|
29
|
+
* having count("pet"."id") > $1
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare class FunctionBuilder<DB, TB extends keyof DB> {
|
|
33
|
+
#private;
|
|
34
|
+
constructor(props: FunctionBuilderProps);
|
|
35
|
+
/**
|
|
36
|
+
* Calls the `max` function for the column given as the argument.
|
|
37
|
+
*/
|
|
38
|
+
min<C extends StringReference<DB, TB>>(column: C): RawBuilder<ExtractTypeFromReferenceExpression<DB, TB, C>>;
|
|
39
|
+
/**
|
|
40
|
+
* Calls the `min` function for the column given as the argument.
|
|
41
|
+
*/
|
|
42
|
+
max<C extends StringReference<DB, TB>>(column: C): RawBuilder<ExtractTypeFromReferenceExpression<DB, TB, C>>;
|
|
43
|
+
/**
|
|
44
|
+
* Calls the `avg` function for the column given as the argument.
|
|
45
|
+
*
|
|
46
|
+
* If this is used in a `select` statement the type of the selected expression
|
|
47
|
+
* will be `number | string` by default. This is because Kysely can't know the
|
|
48
|
+
* type the db driver outputs. Sometimes the output can be larger than the
|
|
49
|
+
* largest javascript number and a string is returned instead. Most drivers
|
|
50
|
+
* allow you to configure the output type of large numbers and Kysely can't
|
|
51
|
+
* know if you've done so.
|
|
52
|
+
*
|
|
53
|
+
* You can specify the output type of the expression by providing
|
|
54
|
+
* the type as the first type argument:
|
|
55
|
+
*
|
|
56
|
+
* ```ts
|
|
57
|
+
* const { avg } = db.fn
|
|
58
|
+
*
|
|
59
|
+
* db.selectFrom('toy')
|
|
60
|
+
* .select(avg<number>('price'))
|
|
61
|
+
* .execute()
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
avg<O extends number | string, C extends StringReference<DB, TB> = any>(column: C): RawBuilder<O>;
|
|
65
|
+
/**
|
|
66
|
+
* Calls the `sum` function for the column given as the argument.
|
|
67
|
+
*
|
|
68
|
+
* If this is used in a `select` statement the type of the selected expression
|
|
69
|
+
* will be `number | string` by default. This is because Kysely can't know the
|
|
70
|
+
* type the db driver outputs. Sometimes the output can be larger than the
|
|
71
|
+
* largest javascript number and a string is returned instead. Most drivers
|
|
72
|
+
* allow you to configure the output type of large numbers and Kysely can't
|
|
73
|
+
* know if you've done so.
|
|
74
|
+
*
|
|
75
|
+
* You can specify the output type of the expression by providing
|
|
76
|
+
* the type as the first type argument:
|
|
77
|
+
*
|
|
78
|
+
* ```ts
|
|
79
|
+
* const { sum } = db.fn
|
|
80
|
+
*
|
|
81
|
+
* db.selectFrom('toy')
|
|
82
|
+
* .select(sum<number>('price'))
|
|
83
|
+
* .execute()
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
sum<O extends number | string | BigInt, C extends StringReference<DB, TB> = any>(column: C): RawBuilder<O>;
|
|
87
|
+
/**
|
|
88
|
+
* Calls the `count` function for the column given as the argument.
|
|
89
|
+
*
|
|
90
|
+
* If this is used in a `select` statement the type of the selected expression
|
|
91
|
+
* will be `number | string` by default. This is because Kysely can't know the
|
|
92
|
+
* type the db driver outputs. Sometimes the output can be larger than the
|
|
93
|
+
* largest javascript number and a string is returned instead. Most drivers
|
|
94
|
+
* allow you to configure the output type of large numbers and Kysely can't
|
|
95
|
+
* know if you've done so.
|
|
96
|
+
*
|
|
97
|
+
* You can specify the output type of the expression by providing
|
|
98
|
+
* the type as the first type argument:
|
|
99
|
+
*
|
|
100
|
+
* ```ts
|
|
101
|
+
* const { count } = db.fn
|
|
102
|
+
*
|
|
103
|
+
* db.selectFrom('toy')
|
|
104
|
+
* .select(count<number>('price'))
|
|
105
|
+
* .execute()
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
count<O extends number | string | BigInt, C extends StringReference<DB, TB> = any>(column: C): RawBuilder<O>;
|
|
109
|
+
}
|
|
110
|
+
export interface FunctionBuilderProps {
|
|
111
|
+
readonly executor: QueryExecutor;
|
|
112
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FunctionBuilder = void 0;
|
|
4
|
+
const raw_builder_js_1 = require("../raw-builder/raw-builder.js");
|
|
5
|
+
const object_utils_js_1 = require("../util/object-utils.js");
|
|
6
|
+
const query_id_js_1 = require("../util/query-id.js");
|
|
7
|
+
/**
|
|
8
|
+
* Helpers for type safe SQL function calls.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* const { count } = db.fn
|
|
13
|
+
*
|
|
14
|
+
* await db.selectFrom('person')
|
|
15
|
+
* .innerJoin('pet', 'pet.owner_id', 'person.id')
|
|
16
|
+
* .select([
|
|
17
|
+
* 'person.id',
|
|
18
|
+
* count('pet.id').as('pet_count')
|
|
19
|
+
* ])
|
|
20
|
+
* .groupBy('person.id')
|
|
21
|
+
* .having(count('pet.id'), '>', 10)
|
|
22
|
+
* .execute()
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* The generated SQL (postgresql):
|
|
26
|
+
*
|
|
27
|
+
* ```sql
|
|
28
|
+
* select "person"."id", count("pet"."id") as "pet_count"
|
|
29
|
+
* from "person"
|
|
30
|
+
* inner join "pet" on "pet"."owner_id" = "person"."id"
|
|
31
|
+
* group by "person"."id"
|
|
32
|
+
* having count("pet"."id") > $1
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
class FunctionBuilder {
|
|
36
|
+
#props;
|
|
37
|
+
constructor(props) {
|
|
38
|
+
this.#props = (0, object_utils_js_1.freeze)(props);
|
|
39
|
+
this.min = this.min.bind(this);
|
|
40
|
+
this.max = this.max.bind(this);
|
|
41
|
+
this.avg = this.avg.bind(this);
|
|
42
|
+
this.sum = this.sum.bind(this);
|
|
43
|
+
this.count = this.count.bind(this);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Calls the `max` function for the column given as the argument.
|
|
47
|
+
*/
|
|
48
|
+
min(column) {
|
|
49
|
+
return this.#oneArgFunction('min', column);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Calls the `min` function for the column given as the argument.
|
|
53
|
+
*/
|
|
54
|
+
max(column) {
|
|
55
|
+
return this.#oneArgFunction('max', column);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Calls the `avg` function for the column given as the argument.
|
|
59
|
+
*
|
|
60
|
+
* If this is used in a `select` statement the type of the selected expression
|
|
61
|
+
* will be `number | string` by default. This is because Kysely can't know the
|
|
62
|
+
* type the db driver outputs. Sometimes the output can be larger than the
|
|
63
|
+
* largest javascript number and a string is returned instead. Most drivers
|
|
64
|
+
* allow you to configure the output type of large numbers and Kysely can't
|
|
65
|
+
* know if you've done so.
|
|
66
|
+
*
|
|
67
|
+
* You can specify the output type of the expression by providing
|
|
68
|
+
* the type as the first type argument:
|
|
69
|
+
*
|
|
70
|
+
* ```ts
|
|
71
|
+
* const { avg } = db.fn
|
|
72
|
+
*
|
|
73
|
+
* db.selectFrom('toy')
|
|
74
|
+
* .select(avg<number>('price'))
|
|
75
|
+
* .execute()
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
avg(column) {
|
|
79
|
+
return this.#oneArgFunction('avg', column);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Calls the `sum` function for the column given as the argument.
|
|
83
|
+
*
|
|
84
|
+
* If this is used in a `select` statement the type of the selected expression
|
|
85
|
+
* will be `number | string` by default. This is because Kysely can't know the
|
|
86
|
+
* type the db driver outputs. Sometimes the output can be larger than the
|
|
87
|
+
* largest javascript number and a string is returned instead. Most drivers
|
|
88
|
+
* allow you to configure the output type of large numbers and Kysely can't
|
|
89
|
+
* know if you've done so.
|
|
90
|
+
*
|
|
91
|
+
* You can specify the output type of the expression by providing
|
|
92
|
+
* the type as the first type argument:
|
|
93
|
+
*
|
|
94
|
+
* ```ts
|
|
95
|
+
* const { sum } = db.fn
|
|
96
|
+
*
|
|
97
|
+
* db.selectFrom('toy')
|
|
98
|
+
* .select(sum<number>('price'))
|
|
99
|
+
* .execute()
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
sum(column) {
|
|
103
|
+
return this.#oneArgFunction('sum', column);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Calls the `count` function for the column given as the argument.
|
|
107
|
+
*
|
|
108
|
+
* If this is used in a `select` statement the type of the selected expression
|
|
109
|
+
* will be `number | string` by default. This is because Kysely can't know the
|
|
110
|
+
* type the db driver outputs. Sometimes the output can be larger than the
|
|
111
|
+
* largest javascript number and a string is returned instead. Most drivers
|
|
112
|
+
* allow you to configure the output type of large numbers and Kysely can't
|
|
113
|
+
* know if you've done so.
|
|
114
|
+
*
|
|
115
|
+
* You can specify the output type of the expression by providing
|
|
116
|
+
* the type as the first type argument:
|
|
117
|
+
*
|
|
118
|
+
* ```ts
|
|
119
|
+
* const { count } = db.fn
|
|
120
|
+
*
|
|
121
|
+
* db.selectFrom('toy')
|
|
122
|
+
* .select(count<number>('price'))
|
|
123
|
+
* .execute()
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
count(column) {
|
|
127
|
+
return this.#oneArgFunction('count', column);
|
|
128
|
+
}
|
|
129
|
+
#oneArgFunction(fn, column) {
|
|
130
|
+
return new raw_builder_js_1.RawBuilder({
|
|
131
|
+
queryId: (0, query_id_js_1.createQueryId)(),
|
|
132
|
+
executor: this.#props.executor,
|
|
133
|
+
sql: `${fn}(??)`,
|
|
134
|
+
parameters: [column],
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
exports.FunctionBuilder = FunctionBuilder;
|
|
@@ -2,7 +2,7 @@ import { AliasNode } from '../operation-node/alias-node.js';
|
|
|
2
2
|
import { OperationNodeSource } from '../operation-node/operation-node-source.js';
|
|
3
3
|
import { CompiledQuery } from '../query-compiler/compiled-query.js';
|
|
4
4
|
import { JoinCallbackExpression, JoinReferenceExpression } from '../parser/join-parser.js';
|
|
5
|
-
import { TableExpression, QueryBuilderWithTable } from '../parser/table-parser.js';
|
|
5
|
+
import { TableExpression, QueryBuilderWithTable, QueryBuilderWithLeftJoin, QueryBuilderWithRightJoin, QueryBuilderWithFullJoin } from '../parser/table-parser.js';
|
|
6
6
|
import { SelectExpression, QueryBuilderWithSelection, SelectAllQueryBuilder } from '../parser/select-parser.js';
|
|
7
7
|
import { ExistsExpression, FilterOperator } from '../parser/filter-parser.js';
|
|
8
8
|
import { InsertObject } from '../parser/insert-values-parser.js';
|
|
@@ -10,7 +10,7 @@ import { QueryBuilderWithReturning } from '../parser/returning-parser.js';
|
|
|
10
10
|
import { ReferenceExpression } from '../parser/reference-parser.js';
|
|
11
11
|
import { ValueExpressionOrList } from '../parser/value-parser.js';
|
|
12
12
|
import { QueryNode } from '../operation-node/query-node.js';
|
|
13
|
-
import { AnyColumn, ManyResultRowType, NonEmptySingleResultRowType, SingleResultRowType } from '
|
|
13
|
+
import { AnyColumn, ManyResultRowType, NonEmptySingleResultRowType, SingleResultRowType } from '../util/type-utils.js';
|
|
14
14
|
import { OrderByDirectionExpression, OrderByExpression } from '../parser/order-by-parser.js';
|
|
15
15
|
import { MutationObject } from '../parser/update-set-parser.js';
|
|
16
16
|
import { Compilable } from '../util/compilable.js';
|
|
@@ -869,20 +869,20 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
869
869
|
* Just like {@link QueryBuilder.innerJoin | innerJoin} but adds a left join
|
|
870
870
|
* instead of an inner join.
|
|
871
871
|
*/
|
|
872
|
-
leftJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2):
|
|
873
|
-
leftJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN):
|
|
872
|
+
leftJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): QueryBuilderWithLeftJoin<DB, TB, O, TE>;
|
|
873
|
+
leftJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): QueryBuilderWithLeftJoin<DB, TB, O, TE>;
|
|
874
874
|
/**
|
|
875
875
|
* Just like {@link QueryBuilder.innerJoin | innerJoin} but adds a right join
|
|
876
876
|
* instead of an inner join.
|
|
877
877
|
*/
|
|
878
|
-
rightJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2):
|
|
879
|
-
rightJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN):
|
|
878
|
+
rightJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): QueryBuilderWithRightJoin<DB, TB, O, TE>;
|
|
879
|
+
rightJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): QueryBuilderWithRightJoin<DB, TB, O, TE>;
|
|
880
880
|
/**
|
|
881
881
|
* Just like {@link QueryBuilder.innerJoin | innerJoin} but adds a full join
|
|
882
882
|
* instead of an inner join.
|
|
883
883
|
*/
|
|
884
|
-
fullJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2):
|
|
885
|
-
fullJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN):
|
|
884
|
+
fullJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): QueryBuilderWithFullJoin<DB, TB, O, TE>;
|
|
885
|
+
fullJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): QueryBuilderWithFullJoin<DB, TB, O, TE>;
|
|
886
886
|
/**
|
|
887
887
|
* Sets the values to insert for an {@link Kysely.insertInto | insert} query.
|
|
888
888
|
*
|
|
@@ -62,7 +62,7 @@ import { ModifyColumnNode } from '../operation-node/modify-column-node.js';
|
|
|
62
62
|
import { OnDuplicateKeyNode } from '../operation-node/on-duplicate-key-node.js';
|
|
63
63
|
export declare class DefaultQueryCompiler extends OperationNodeVisitor implements QueryCompiler {
|
|
64
64
|
#private;
|
|
65
|
-
protected get
|
|
65
|
+
protected get numParameters(): number;
|
|
66
66
|
compileQuery(node: RootOperationNode): CompiledQuery;
|
|
67
67
|
protected getSql(): string;
|
|
68
68
|
protected visitSelectQuery(node: SelectQueryNode): void;
|
|
@@ -129,6 +129,6 @@ export declare class DefaultQueryCompiler extends OperationNodeVisitor implement
|
|
|
129
129
|
protected getLeftIdentifierWrapper(): string;
|
|
130
130
|
protected getRightIdentifierWrapper(): string;
|
|
131
131
|
protected getCurrentParameterPlaceholder(): string;
|
|
132
|
-
protected
|
|
132
|
+
protected addParameter(parameter: any): void;
|
|
133
133
|
protected appendImmediateValue(value: PrimitiveValue): void;
|
|
134
134
|
}
|
|
@@ -8,17 +8,17 @@ const raw_node_js_1 = require("../operation-node/raw-node.js");
|
|
|
8
8
|
const object_utils_js_1 = require("../util/object-utils.js");
|
|
9
9
|
class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisitor {
|
|
10
10
|
#sqlFragments = [];
|
|
11
|
-
#
|
|
12
|
-
get
|
|
13
|
-
return this.#
|
|
11
|
+
#parameters = [];
|
|
12
|
+
get numParameters() {
|
|
13
|
+
return this.#parameters.length;
|
|
14
14
|
}
|
|
15
15
|
compileQuery(node) {
|
|
16
16
|
this.#sqlFragments = [];
|
|
17
|
-
this.#
|
|
17
|
+
this.#parameters = [];
|
|
18
18
|
this.visitNode(node);
|
|
19
19
|
return (0, object_utils_js_1.freeze)({
|
|
20
20
|
sql: this.getSql(),
|
|
21
|
-
|
|
21
|
+
parameters: this.#parameters,
|
|
22
22
|
});
|
|
23
23
|
}
|
|
24
24
|
getSql() {
|
|
@@ -640,7 +640,7 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
|
|
|
640
640
|
this.#sqlFragments.push(str);
|
|
641
641
|
}
|
|
642
642
|
appendValue(value) {
|
|
643
|
-
this.
|
|
643
|
+
this.addParameter(value);
|
|
644
644
|
this.append(this.getCurrentParameterPlaceholder());
|
|
645
645
|
}
|
|
646
646
|
getLeftIdentifierWrapper() {
|
|
@@ -650,10 +650,10 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
|
|
|
650
650
|
return '"';
|
|
651
651
|
}
|
|
652
652
|
getCurrentParameterPlaceholder() {
|
|
653
|
-
return '$' + this.
|
|
653
|
+
return '$' + this.numParameters;
|
|
654
654
|
}
|
|
655
|
-
|
|
656
|
-
this.#
|
|
655
|
+
addParameter(parameter) {
|
|
656
|
+
this.#parameters.push(parameter);
|
|
657
657
|
}
|
|
658
658
|
appendImmediateValue(value) {
|
|
659
659
|
if ((0, object_utils_js_1.isString)(value)) {
|
|
@@ -9,6 +9,9 @@ import { QueryNode } from '../operation-node/query-node.js';
|
|
|
9
9
|
import { RawNode } from '../operation-node/raw-node.js';
|
|
10
10
|
import { CompiledQuery } from './compiled-query.js';
|
|
11
11
|
export declare type RootOperationNode = QueryNode | CreateTableNode | CreateIndexNode | AlterTableNode | DropTableNode | DropIndexNode | CreateSchemaNode | DropSchemaNode | RawNode;
|
|
12
|
+
/**
|
|
13
|
+
* a `QueryCompiler` compiles a query expressed as a tree of `OperationNodes` into SQL.
|
|
14
|
+
*/
|
|
12
15
|
export interface QueryCompiler {
|
|
13
16
|
compileQuery(node: RootOperationNode): CompiledQuery;
|
|
14
17
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { QueryBuilder } from './query-builder/query-builder.js';
|
|
2
2
|
import { QueryBuilderWithTable, TableExpression, TableReference } from './parser/table-parser.js';
|
|
3
|
-
import { InsertResultTypeTag, DeleteResultTypeTag, UpdateResultTypeTag } from './
|
|
3
|
+
import { InsertResultTypeTag, DeleteResultTypeTag, UpdateResultTypeTag } from './util/type-utils.js';
|
|
4
4
|
import { QueryExecutor } from './query-executor/query-executor.js';
|
|
5
5
|
import { RawBuilder } from './raw-builder/raw-builder.js';
|
|
6
6
|
import { CommonTableExpression, QueryCreatorWithCommonTableExpression } from './parser/with-parser.js';
|
|
@@ -276,11 +276,11 @@ export declare class QueryCreator<DB> {
|
|
|
276
276
|
* Raw builder instances can be passed to pretty much anywhere: `select`, `where`,
|
|
277
277
|
* `*Join`, `groupBy`, `orderBy` etc. Just try it. If the method accepts it, it works.
|
|
278
278
|
*
|
|
279
|
-
* @param sql - The raw SQL. Special strings `?` and `??` can be used
|
|
280
|
-
*
|
|
279
|
+
* @param sql - The raw SQL. Special strings `?` and `??` can be used as parameter
|
|
280
|
+
* placeholders. `?` for values and `??` for identifiers such as column names
|
|
281
281
|
* or `column.table` references.
|
|
282
282
|
*
|
|
283
|
-
* @param params - The parameters that will be bound to the `?` and `??`
|
|
283
|
+
* @param params - The parameters that will be bound to the `?` and `??` placeholders in
|
|
284
284
|
* the sql string.
|
|
285
285
|
*
|
|
286
286
|
* @example
|
|
@@ -318,7 +318,7 @@ export declare class QueryCreator<DB> {
|
|
|
318
318
|
* the output type. In this case you need to use the `castTo` method on the query
|
|
319
319
|
* to specify a return type for the query.
|
|
320
320
|
*
|
|
321
|
-
* We could've also used `??`
|
|
321
|
+
* We could've also used a `??` placeholder to provide `first_name` and `last_name` like
|
|
322
322
|
* this:
|
|
323
323
|
*
|
|
324
324
|
* ```ts
|
|
@@ -333,9 +333,9 @@ export declare class QueryCreator<DB> {
|
|
|
333
333
|
*
|
|
334
334
|
* But it's often cleaner to just write the column names in the SQL. Again remember to
|
|
335
335
|
* never concatenate column names or any other untrusted user input to the SQL string or you
|
|
336
|
-
* are going to create an injection vulnerability. All user input should go to the
|
|
336
|
+
* are going to create an injection vulnerability. All user input should go to the parameters
|
|
337
337
|
* array, never to the SQL string directly. But if the column names or values are trusted
|
|
338
|
-
* and known at compile time, there is no reason to use
|
|
338
|
+
* and known at compile time, there is no reason to use parameters.
|
|
339
339
|
*
|
|
340
340
|
* @example
|
|
341
341
|
* Example of using `raw` in `where`:
|
|
@@ -361,7 +361,7 @@ export declare class QueryCreator<DB> {
|
|
|
361
361
|
*
|
|
362
362
|
* The function in the above example returns people that are older than the given number of
|
|
363
363
|
* years. The number of years in this example is an untrusted user input, and therefore we use
|
|
364
|
-
* a `?`
|
|
364
|
+
* a `?` placeholder for it.
|
|
365
365
|
*
|
|
366
366
|
* @example
|
|
367
367
|
* Example of creating a completely raw query from scratch:
|
|
@@ -375,7 +375,7 @@ export declare class QueryCreator<DB> {
|
|
|
375
375
|
* The result of `execute()` method is always an array. In this case the type of
|
|
376
376
|
* the `persons` variable is `Person[]`.
|
|
377
377
|
*/
|
|
378
|
-
raw<T = unknown>(sql: string,
|
|
378
|
+
raw<T = unknown>(sql: string, parameters?: any[]): RawBuilder<T>;
|
|
379
379
|
}
|
|
380
380
|
export interface QueryCreatorProps {
|
|
381
381
|
readonly executor: QueryExecutor;
|
|
@@ -218,11 +218,11 @@ class QueryCreator {
|
|
|
218
218
|
* Raw builder instances can be passed to pretty much anywhere: `select`, `where`,
|
|
219
219
|
* `*Join`, `groupBy`, `orderBy` etc. Just try it. If the method accepts it, it works.
|
|
220
220
|
*
|
|
221
|
-
* @param sql - The raw SQL. Special strings `?` and `??` can be used
|
|
222
|
-
*
|
|
221
|
+
* @param sql - The raw SQL. Special strings `?` and `??` can be used as parameter
|
|
222
|
+
* placeholders. `?` for values and `??` for identifiers such as column names
|
|
223
223
|
* or `column.table` references.
|
|
224
224
|
*
|
|
225
|
-
* @param params - The parameters that will be bound to the `?` and `??`
|
|
225
|
+
* @param params - The parameters that will be bound to the `?` and `??` placeholders in
|
|
226
226
|
* the sql string.
|
|
227
227
|
*
|
|
228
228
|
* @example
|
|
@@ -260,7 +260,7 @@ class QueryCreator {
|
|
|
260
260
|
* the output type. In this case you need to use the `castTo` method on the query
|
|
261
261
|
* to specify a return type for the query.
|
|
262
262
|
*
|
|
263
|
-
* We could've also used `??`
|
|
263
|
+
* We could've also used a `??` placeholder to provide `first_name` and `last_name` like
|
|
264
264
|
* this:
|
|
265
265
|
*
|
|
266
266
|
* ```ts
|
|
@@ -275,9 +275,9 @@ class QueryCreator {
|
|
|
275
275
|
*
|
|
276
276
|
* But it's often cleaner to just write the column names in the SQL. Again remember to
|
|
277
277
|
* never concatenate column names or any other untrusted user input to the SQL string or you
|
|
278
|
-
* are going to create an injection vulnerability. All user input should go to the
|
|
278
|
+
* are going to create an injection vulnerability. All user input should go to the parameters
|
|
279
279
|
* array, never to the SQL string directly. But if the column names or values are trusted
|
|
280
|
-
* and known at compile time, there is no reason to use
|
|
280
|
+
* and known at compile time, there is no reason to use parameters.
|
|
281
281
|
*
|
|
282
282
|
* @example
|
|
283
283
|
* Example of using `raw` in `where`:
|
|
@@ -303,7 +303,7 @@ class QueryCreator {
|
|
|
303
303
|
*
|
|
304
304
|
* The function in the above example returns people that are older than the given number of
|
|
305
305
|
* years. The number of years in this example is an untrusted user input, and therefore we use
|
|
306
|
-
* a `?`
|
|
306
|
+
* a `?` placeholder for it.
|
|
307
307
|
*
|
|
308
308
|
* @example
|
|
309
309
|
* Example of creating a completely raw query from scratch:
|
|
@@ -317,11 +317,12 @@ class QueryCreator {
|
|
|
317
317
|
* The result of `execute()` method is always an array. In this case the type of
|
|
318
318
|
* the `persons` variable is `Person[]`.
|
|
319
319
|
*/
|
|
320
|
-
raw(sql,
|
|
320
|
+
raw(sql, parameters) {
|
|
321
321
|
return new raw_builder_js_1.RawBuilder({
|
|
322
|
-
|
|
323
|
-
params,
|
|
322
|
+
queryId: (0, query_id_js_1.createQueryId)(),
|
|
324
323
|
executor: this.#props.executor,
|
|
324
|
+
sql,
|
|
325
|
+
parameters,
|
|
325
326
|
});
|
|
326
327
|
}
|
|
327
328
|
}
|
|
@@ -4,10 +4,18 @@ import { OperationNodeSource } from '../operation-node/operation-node-source.js'
|
|
|
4
4
|
import { RawNode } from '../operation-node/raw-node.js';
|
|
5
5
|
import { CompiledQuery } from '../query-compiler/compiled-query.js';
|
|
6
6
|
import { QueryExecutor } from '../query-executor/query-executor.js';
|
|
7
|
+
import { QueryId } from '../util/query-id.js';
|
|
7
8
|
export declare class RawBuilder<O = unknown> implements OperationNodeSource {
|
|
8
9
|
#private;
|
|
9
|
-
constructor(
|
|
10
|
+
constructor(props: RawBuilderProps);
|
|
10
11
|
as<A extends string>(alias: A): AliasedRawBuilder<O, A>;
|
|
12
|
+
/**
|
|
13
|
+
* Change the output type of the raw expression.
|
|
14
|
+
*
|
|
15
|
+
* This doesn't produce any SQL. This methods simply returns a copy
|
|
16
|
+
* of this `RawBuilder` with a new output type.
|
|
17
|
+
*/
|
|
18
|
+
castTo<T>(): RawBuilder<T>;
|
|
11
19
|
toOperationNode(): RawNode;
|
|
12
20
|
compile(): CompiledQuery;
|
|
13
21
|
execute(): Promise<QueryResult<O>>;
|
|
@@ -29,8 +37,9 @@ export declare class AliasedRawBuilder<O = unknown, A extends string = never> im
|
|
|
29
37
|
toOperationNode(): AliasNode;
|
|
30
38
|
constructor(rawBuilder: RawBuilder<O>, alias: A);
|
|
31
39
|
}
|
|
32
|
-
export interface
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
40
|
+
export interface RawBuilderProps {
|
|
41
|
+
readonly queryId: QueryId;
|
|
42
|
+
readonly executor: QueryExecutor;
|
|
43
|
+
readonly sql: string;
|
|
44
|
+
readonly parameters?: any;
|
|
36
45
|
}
|