kysely 0.9.2 → 0.9.6
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/dist/cjs/dialect/mysql/mysql-dialect.d.ts +11 -0
- package/dist/cjs/dialect/mysql/mysql-dialect.js +7 -0
- package/dist/cjs/dialect/postgres/postgres-dialect.d.ts +13 -1
- package/dist/cjs/dialect/postgres/postgres-dialect.js +10 -0
- package/dist/cjs/dialect/postgres/postgres-driver.js +2 -2
- package/dist/cjs/dialect/postgres/postgres-query-compiler.js +1 -1
- package/dist/cjs/driver/runtime-driver.d.ts +3 -2
- package/dist/cjs/driver/runtime-driver.js +57 -4
- package/dist/cjs/index.d.ts +2 -1
- package/dist/cjs/kysely.d.ts +31 -9
- package/dist/cjs/kysely.js +25 -9
- package/dist/cjs/migration/migration.d.ts +32 -0
- package/dist/cjs/operation-node/operation-node-transformer.d.ts +2 -0
- package/dist/cjs/operation-node/operation-node-transformer.js +9 -0
- package/dist/cjs/operation-node/operation-node-visitor.d.ts +61 -59
- package/dist/cjs/operation-node/operation-node-visitor.js +1 -316
- package/dist/cjs/operation-node/operation-node.d.ts +1 -1
- package/dist/cjs/operation-node/operator-node.d.ts +1 -1
- package/dist/cjs/operation-node/operator-node.js +1 -0
- package/dist/cjs/operation-node/select-query-node.d.ts +3 -0
- package/dist/cjs/operation-node/select-query-node.js +8 -0
- package/dist/cjs/operation-node/union-node.d.ts +16 -0
- package/dist/cjs/operation-node/union-node.js +19 -0
- package/dist/cjs/parser/insert-values-parser.d.ts +1 -1
- package/dist/cjs/parser/join-parser.d.ts +4 -4
- package/dist/cjs/parser/union-parser.d.ts +5 -0
- package/dist/cjs/parser/union-parser.js +13 -0
- package/dist/cjs/parser/value-parser.d.ts +1 -1
- package/dist/cjs/plugin/camel-case/camel-case-plugin.js +5 -116
- package/dist/cjs/plugin/camel-case/camel-case-transformer.d.ts +8 -0
- package/dist/cjs/plugin/camel-case/camel-case-transformer.js +19 -0
- package/dist/cjs/plugin/camel-case/camel-case.d.ts +15 -0
- package/dist/cjs/plugin/camel-case/camel-case.js +109 -0
- package/dist/cjs/plugin/kysely-plugin.d.ts +1 -1
- package/dist/cjs/query-builder/query-builder.d.ts +65 -1
- package/dist/cjs/query-builder/query-builder.js +83 -0
- package/dist/cjs/query-compiler/default-query-compiler.d.ts +5 -0
- package/dist/cjs/query-compiler/default-query-compiler.js +18 -1
- package/dist/cjs/query-executor/default-query-executor.d.ts +1 -0
- package/dist/cjs/query-executor/default-query-executor.js +6 -0
- package/dist/cjs/query-executor/noop-query-executor.d.ts +1 -0
- package/dist/cjs/query-executor/noop-query-executor.js +3 -0
- package/dist/cjs/query-executor/query-executor.d.ts +9 -4
- package/dist/cjs/util/log.d.ts +19 -0
- package/dist/cjs/util/log.js +37 -0
- package/dist/cjs/util/random-string.js +6 -3
- package/dist/cjs/util/type-utils.d.ts +4 -4
- package/dist/esm/dialect/mysql/mysql-dialect.d.ts +11 -0
- package/dist/esm/dialect/mysql/mysql-dialect.js +7 -0
- package/dist/esm/dialect/mysql/mysql-driver.js +1 -1
- package/dist/esm/dialect/postgres/postgres-dialect.d.ts +13 -1
- package/dist/esm/dialect/postgres/postgres-dialect.js +10 -0
- package/dist/esm/dialect/postgres/postgres-driver.js +2 -2
- package/dist/esm/dialect/postgres/postgres-query-compiler.js +1 -1
- package/dist/esm/driver/runtime-driver.d.ts +3 -2
- package/dist/esm/driver/runtime-driver.js +57 -4
- package/dist/esm/index.d.ts +2 -1
- package/dist/esm/kysely.d.ts +31 -9
- package/dist/esm/kysely.js +25 -9
- package/dist/esm/migration/migration.d.ts +32 -0
- package/dist/esm/operation-node/operation-node-transformer.d.ts +2 -0
- package/dist/esm/operation-node/operation-node-transformer.js +9 -0
- package/dist/esm/operation-node/operation-node-visitor.d.ts +61 -59
- package/dist/esm/operation-node/operation-node-visitor.js +1 -316
- package/dist/esm/operation-node/operation-node.d.ts +1 -1
- package/dist/esm/operation-node/operator-node.d.ts +1 -1
- package/dist/esm/operation-node/operator-node.js +1 -0
- package/dist/esm/operation-node/select-query-node.d.ts +3 -0
- package/dist/esm/operation-node/select-query-node.js +8 -0
- package/dist/esm/operation-node/union-node.d.ts +16 -0
- package/dist/esm/operation-node/union-node.js +16 -0
- package/dist/esm/parser/insert-values-parser.d.ts +1 -1
- package/dist/esm/parser/join-parser.d.ts +4 -4
- package/dist/esm/parser/union-parser.d.ts +5 -0
- package/dist/esm/parser/union-parser.js +9 -0
- package/dist/esm/parser/value-parser.d.ts +1 -1
- package/dist/esm/plugin/camel-case/camel-case-plugin.js +4 -115
- package/dist/esm/plugin/camel-case/camel-case-transformer.d.ts +8 -0
- package/dist/esm/plugin/camel-case/camel-case-transformer.js +15 -0
- package/dist/esm/plugin/camel-case/camel-case.d.ts +15 -0
- package/dist/esm/plugin/camel-case/camel-case.js +104 -0
- package/dist/esm/plugin/kysely-plugin.d.ts +1 -1
- package/dist/esm/query-builder/query-builder.d.ts +65 -1
- package/dist/esm/query-builder/query-builder.js +83 -0
- package/dist/esm/query-compiler/default-query-compiler.d.ts +5 -0
- package/dist/esm/query-compiler/default-query-compiler.js +18 -1
- package/dist/esm/query-executor/default-query-executor.d.ts +1 -0
- package/dist/esm/query-executor/default-query-executor.js +6 -0
- package/dist/esm/query-executor/noop-query-executor.d.ts +1 -0
- package/dist/esm/query-executor/noop-query-executor.js +3 -0
- package/dist/esm/query-executor/query-executor.d.ts +9 -4
- package/dist/esm/util/log.d.ts +19 -0
- package/dist/esm/util/log.js +33 -0
- package/dist/esm/util/random-string.js +6 -3
- package/dist/esm/util/type-utils.d.ts +4 -4
- package/package.json +1 -1
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createCamelCaseMapper = exports.createSnakeCaseMapper = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a function that transforms camel case strings to snake case.
|
|
6
|
+
*/
|
|
7
|
+
function createSnakeCaseMapper({ upperCase = false, underscoreBeforeDigits = false, underscoreBetweenUppercaseLetters = false, } = {}) {
|
|
8
|
+
return memoize((str) => {
|
|
9
|
+
if (str.length === 0) {
|
|
10
|
+
return str;
|
|
11
|
+
}
|
|
12
|
+
const upper = str.toUpperCase();
|
|
13
|
+
const lower = str.toLowerCase();
|
|
14
|
+
let out = lower[0];
|
|
15
|
+
for (let i = 1, l = str.length; i < l; ++i) {
|
|
16
|
+
const char = str[i];
|
|
17
|
+
const prevChar = str[i - 1];
|
|
18
|
+
const upperChar = upper[i];
|
|
19
|
+
const prevUpperChar = upper[i - 1];
|
|
20
|
+
const lowerChar = lower[i];
|
|
21
|
+
const prevLowerChar = lower[i - 1];
|
|
22
|
+
// If underScoreBeforeDigits is true then, well, insert an underscore
|
|
23
|
+
// before digits :). Only the first digit gets an underscore if
|
|
24
|
+
// there are multiple.
|
|
25
|
+
if (underscoreBeforeDigits && isDigit(char) && !isDigit(prevChar)) {
|
|
26
|
+
out += '_' + char;
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
// Test if `char` is an upper-case character and that the character
|
|
30
|
+
// actually has different upper and lower case versions.
|
|
31
|
+
if (char === upperChar && upperChar !== lowerChar) {
|
|
32
|
+
const prevCharacterIsUppercase = prevChar === prevUpperChar && prevUpperChar !== prevLowerChar;
|
|
33
|
+
// If underscoreBetweenUppercaseLetters is true, we always place an underscore
|
|
34
|
+
// before consecutive uppercase letters (e.g. "fooBAR" becomes "foo_b_a_r").
|
|
35
|
+
// Otherwise, we don't (e.g. "fooBAR" becomes "foo_bar").
|
|
36
|
+
if (underscoreBetweenUppercaseLetters || !prevCharacterIsUppercase) {
|
|
37
|
+
out += '_' + lowerChar;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
out += lowerChar;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
out += char;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (upperCase) {
|
|
48
|
+
return out.toUpperCase();
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
exports.createSnakeCaseMapper = createSnakeCaseMapper;
|
|
56
|
+
/**
|
|
57
|
+
* Creates a function that transforms snake case strings to camel case.
|
|
58
|
+
*/
|
|
59
|
+
function createCamelCaseMapper({ upperCase = false, } = {}) {
|
|
60
|
+
return memoize((str) => {
|
|
61
|
+
if (str.length === 0) {
|
|
62
|
+
return str;
|
|
63
|
+
}
|
|
64
|
+
if (upperCase && isAllUpperCaseSnakeCase(str)) {
|
|
65
|
+
// Only convert to lower case if the string is all upper
|
|
66
|
+
// case snake_case. This allows camelCase strings to go
|
|
67
|
+
// through without changing.
|
|
68
|
+
str = str.toLowerCase();
|
|
69
|
+
}
|
|
70
|
+
let out = str[0];
|
|
71
|
+
for (let i = 1, l = str.length; i < l; ++i) {
|
|
72
|
+
const char = str[i];
|
|
73
|
+
const prevChar = str[i - 1];
|
|
74
|
+
if (char !== '_') {
|
|
75
|
+
if (prevChar === '_') {
|
|
76
|
+
out += char.toUpperCase();
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
out += char;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return out;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
exports.createCamelCaseMapper = createCamelCaseMapper;
|
|
87
|
+
function isAllUpperCaseSnakeCase(str) {
|
|
88
|
+
for (let i = 1, l = str.length; i < l; ++i) {
|
|
89
|
+
const char = str[i];
|
|
90
|
+
if (char !== '_' && char !== char.toUpperCase()) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
function isDigit(char) {
|
|
97
|
+
return char >= '0' && char <= '9';
|
|
98
|
+
}
|
|
99
|
+
function memoize(func) {
|
|
100
|
+
const cache = new Map();
|
|
101
|
+
return (str) => {
|
|
102
|
+
let mapped = cache.get(str);
|
|
103
|
+
if (!mapped) {
|
|
104
|
+
mapped = func(str);
|
|
105
|
+
cache.set(str, mapped);
|
|
106
|
+
}
|
|
107
|
+
return mapped;
|
|
108
|
+
};
|
|
109
|
+
}
|
|
@@ -21,7 +21,7 @@ export interface KyselyPlugin {
|
|
|
21
21
|
* return args.node
|
|
22
22
|
* },
|
|
23
23
|
*
|
|
24
|
-
* transformResult(args: PluginTransformResultArgs): QueryResult<
|
|
24
|
+
* transformResult(args: PluginTransformResultArgs): QueryResult<UnknownRow> {
|
|
25
25
|
* const data = this.data.get(args.queryId)
|
|
26
26
|
* return args.result
|
|
27
27
|
* }
|
|
@@ -18,6 +18,7 @@ import { QueryExecutor } from '../query-executor/query-executor.js';
|
|
|
18
18
|
import { QueryId } from '../util/query-id.js';
|
|
19
19
|
import { OnConflictConstraintTarget } from '../parser/on-conflict-parser.js';
|
|
20
20
|
import { ParseContext } from '../parser/parse-context.js';
|
|
21
|
+
import { UnionExpression } from '../parser/union-parser.js';
|
|
21
22
|
/**
|
|
22
23
|
* The main query builder class.
|
|
23
24
|
*
|
|
@@ -998,7 +999,7 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
998
999
|
* ```
|
|
999
1000
|
*/
|
|
1000
1001
|
values(row: InsertObject<DB, TB>): QueryBuilder<DB, TB, O>;
|
|
1001
|
-
values(row: InsertObject<DB, TB
|
|
1002
|
+
values(row: ReadonlyArray<InsertObject<DB, TB>>): QueryBuilder<DB, TB, O>;
|
|
1002
1003
|
/**
|
|
1003
1004
|
* Ignores the insert if the column or constraint given as the first
|
|
1004
1005
|
* argument conflicts with the current rows in the database. The
|
|
@@ -1553,6 +1554,69 @@ export declare class QueryBuilder<DB, TB extends keyof DB, O = {}> implements Op
|
|
|
1553
1554
|
* ```
|
|
1554
1555
|
*/
|
|
1555
1556
|
offset(offset: number): QueryBuilder<DB, TB, O>;
|
|
1557
|
+
/**
|
|
1558
|
+
* Combines another select query or raw expression to this query using `union`.
|
|
1559
|
+
*
|
|
1560
|
+
* The output row type of the combined query must match `this` query.
|
|
1561
|
+
*
|
|
1562
|
+
* @xample
|
|
1563
|
+
* ```ts
|
|
1564
|
+
* db.selectFrom('person')
|
|
1565
|
+
* .select(['id', 'first_name as name'])
|
|
1566
|
+
* .union(db.selectFrom('pet').select(['id', 'name']))
|
|
1567
|
+
* .orderBy('name')
|
|
1568
|
+
* ```
|
|
1569
|
+
*/
|
|
1570
|
+
union(expression: UnionExpression<DB, O>): QueryBuilder<DB, TB, O>;
|
|
1571
|
+
/**
|
|
1572
|
+
* Combines another select query or raw expression to this query using `union all`.
|
|
1573
|
+
*
|
|
1574
|
+
* The output row type of the combined query must match `this` query.
|
|
1575
|
+
*
|
|
1576
|
+
* @xample
|
|
1577
|
+
* ```ts
|
|
1578
|
+
* db.selectFrom('person')
|
|
1579
|
+
* .select(['id', 'first_name as name'])
|
|
1580
|
+
* .unionAll(db.selectFrom('pet').select(['id', 'name']))
|
|
1581
|
+
* .orderBy('name')
|
|
1582
|
+
* ```
|
|
1583
|
+
*/
|
|
1584
|
+
unionAll(expression: UnionExpression<DB, O>): QueryBuilder<DB, TB, O>;
|
|
1585
|
+
/**
|
|
1586
|
+
* Simply calls the given function passing `this` as the only argument.
|
|
1587
|
+
*
|
|
1588
|
+
* This method can be useful when adding optional method calls:
|
|
1589
|
+
*
|
|
1590
|
+
* @example
|
|
1591
|
+
* ```ts
|
|
1592
|
+
* db.selectFrom('person')
|
|
1593
|
+
* .selectAll()
|
|
1594
|
+
* .call((qb) => {
|
|
1595
|
+
* if (something) {
|
|
1596
|
+
* return qb.where('something', '=', something)
|
|
1597
|
+
* } else {
|
|
1598
|
+
* return qb.where('somethingElse', '=', somethingElse)
|
|
1599
|
+
* }
|
|
1600
|
+
* })
|
|
1601
|
+
* .execute()
|
|
1602
|
+
* ```
|
|
1603
|
+
*
|
|
1604
|
+
* The next example uses a helper funtion `log` to log a query:
|
|
1605
|
+
*
|
|
1606
|
+
* @example
|
|
1607
|
+
* ```ts
|
|
1608
|
+
* function log<T extends AnyQueryBuilder>(qb: T): T {
|
|
1609
|
+
* console.log(qb.compile())
|
|
1610
|
+
* return qb
|
|
1611
|
+
* }
|
|
1612
|
+
*
|
|
1613
|
+
* db.selectFrom('person')
|
|
1614
|
+
* .selectAll()
|
|
1615
|
+
* .call(log)
|
|
1616
|
+
* .execute()
|
|
1617
|
+
* ```
|
|
1618
|
+
*/
|
|
1619
|
+
call<T>(func: (qb: this) => T): T;
|
|
1556
1620
|
/**
|
|
1557
1621
|
* Gives an alias for the query. This method is only useful for sub queries.
|
|
1558
1622
|
*
|
|
@@ -20,6 +20,7 @@ const object_utils_js_1 = require("../util/object-utils.js");
|
|
|
20
20
|
const delete_query_node_js_1 = require("../operation-node/delete-query-node.js");
|
|
21
21
|
const on_duplicate_key_node_js_1 = require("../operation-node/on-duplicate-key-node.js");
|
|
22
22
|
const group_by_parser_js_1 = require("../parser/group-by-parser.js");
|
|
23
|
+
const union_parser_js_1 = require("../parser/union-parser.js");
|
|
23
24
|
/**
|
|
24
25
|
* The main query builder class.
|
|
25
26
|
*
|
|
@@ -777,6 +778,83 @@ class QueryBuilder {
|
|
|
777
778
|
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithOffset(this.#props.queryNode, offset_node_js_1.OffsetNode.create(offset)),
|
|
778
779
|
});
|
|
779
780
|
}
|
|
781
|
+
/**
|
|
782
|
+
* Combines another select query or raw expression to this query using `union`.
|
|
783
|
+
*
|
|
784
|
+
* The output row type of the combined query must match `this` query.
|
|
785
|
+
*
|
|
786
|
+
* @xample
|
|
787
|
+
* ```ts
|
|
788
|
+
* db.selectFrom('person')
|
|
789
|
+
* .select(['id', 'first_name as name'])
|
|
790
|
+
* .union(db.selectFrom('pet').select(['id', 'name']))
|
|
791
|
+
* .orderBy('name')
|
|
792
|
+
* ```
|
|
793
|
+
*/
|
|
794
|
+
union(expression) {
|
|
795
|
+
assertCanHaveUnion(this.#props.queryNode);
|
|
796
|
+
return new QueryBuilder({
|
|
797
|
+
...this.#props,
|
|
798
|
+
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithUnion(this.#props.queryNode, (0, union_parser_js_1.parseUnion)(expression, false)),
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
/**
|
|
802
|
+
* Combines another select query or raw expression to this query using `union all`.
|
|
803
|
+
*
|
|
804
|
+
* The output row type of the combined query must match `this` query.
|
|
805
|
+
*
|
|
806
|
+
* @xample
|
|
807
|
+
* ```ts
|
|
808
|
+
* db.selectFrom('person')
|
|
809
|
+
* .select(['id', 'first_name as name'])
|
|
810
|
+
* .unionAll(db.selectFrom('pet').select(['id', 'name']))
|
|
811
|
+
* .orderBy('name')
|
|
812
|
+
* ```
|
|
813
|
+
*/
|
|
814
|
+
unionAll(expression) {
|
|
815
|
+
assertCanHaveUnion(this.#props.queryNode);
|
|
816
|
+
return new QueryBuilder({
|
|
817
|
+
...this.#props,
|
|
818
|
+
queryNode: select_query_node_js_1.SelectQueryNode.cloneWithUnion(this.#props.queryNode, (0, union_parser_js_1.parseUnion)(expression, true)),
|
|
819
|
+
});
|
|
820
|
+
}
|
|
821
|
+
/**
|
|
822
|
+
* Simply calls the given function passing `this` as the only argument.
|
|
823
|
+
*
|
|
824
|
+
* This method can be useful when adding optional method calls:
|
|
825
|
+
*
|
|
826
|
+
* @example
|
|
827
|
+
* ```ts
|
|
828
|
+
* db.selectFrom('person')
|
|
829
|
+
* .selectAll()
|
|
830
|
+
* .call((qb) => {
|
|
831
|
+
* if (something) {
|
|
832
|
+
* return qb.where('something', '=', something)
|
|
833
|
+
* } else {
|
|
834
|
+
* return qb.where('somethingElse', '=', somethingElse)
|
|
835
|
+
* }
|
|
836
|
+
* })
|
|
837
|
+
* .execute()
|
|
838
|
+
* ```
|
|
839
|
+
*
|
|
840
|
+
* The next example uses a helper funtion `log` to log a query:
|
|
841
|
+
*
|
|
842
|
+
* @example
|
|
843
|
+
* ```ts
|
|
844
|
+
* function log<T extends AnyQueryBuilder>(qb: T): T {
|
|
845
|
+
* console.log(qb.compile())
|
|
846
|
+
* return qb
|
|
847
|
+
* }
|
|
848
|
+
*
|
|
849
|
+
* db.selectFrom('person')
|
|
850
|
+
* .selectAll()
|
|
851
|
+
* .call(log)
|
|
852
|
+
* .execute()
|
|
853
|
+
* ```
|
|
854
|
+
*/
|
|
855
|
+
call(func) {
|
|
856
|
+
return func(this);
|
|
857
|
+
}
|
|
780
858
|
/**
|
|
781
859
|
* Gives an alias for the query. This method is only useful for sub queries.
|
|
782
860
|
*
|
|
@@ -985,3 +1063,8 @@ function assertCanHaveOffset(node) {
|
|
|
985
1063
|
throw new Error('only a select query can have an offset');
|
|
986
1064
|
}
|
|
987
1065
|
}
|
|
1066
|
+
function assertCanHaveUnion(node) {
|
|
1067
|
+
if (!select_query_node_js_1.SelectQueryNode.is(node)) {
|
|
1068
|
+
throw new Error('only a select query can have an union');
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
@@ -60,6 +60,8 @@ import { ForeignKeyConstraintNode } from '../operation-node/foreign-key-constrai
|
|
|
60
60
|
import { ColumnDefinitionNode } from '../operation-node/column-definition-node.js';
|
|
61
61
|
import { ModifyColumnNode } from '../operation-node/modify-column-node.js';
|
|
62
62
|
import { OnDuplicateKeyNode } from '../operation-node/on-duplicate-key-node.js';
|
|
63
|
+
import { ColumnNode } from '../operation-node/column-node.js';
|
|
64
|
+
import { UnionNode } from '../operation-node/union-node.js';
|
|
63
65
|
export declare class DefaultQueryCompiler extends OperationNodeVisitor implements QueryCompiler {
|
|
64
66
|
#private;
|
|
65
67
|
protected get numParameters(): number;
|
|
@@ -67,6 +69,8 @@ export declare class DefaultQueryCompiler extends OperationNodeVisitor implement
|
|
|
67
69
|
protected getSql(): string;
|
|
68
70
|
protected visitSelectQuery(node: SelectQueryNode): void;
|
|
69
71
|
protected visitFrom(node: FromNode): void;
|
|
72
|
+
protected visitSelection(node: SelectionNode): void;
|
|
73
|
+
protected visitColumn(node: ColumnNode): void;
|
|
70
74
|
protected compileDistinctOn(selections: ReadonlyArray<SelectionNode>): void;
|
|
71
75
|
protected compileList(nodes: ReadonlyArray<OperationNode>, separator?: string): void;
|
|
72
76
|
protected visitWhere(node: WhereNode): void;
|
|
@@ -125,6 +129,7 @@ export declare class DefaultQueryCompiler extends OperationNodeVisitor implement
|
|
|
125
129
|
protected visitModifyColumn(node: ModifyColumnNode): void;
|
|
126
130
|
protected visitAddConstraint(node: AddConstraintNode): void;
|
|
127
131
|
protected visitDropConstraint(node: DropConstraintNode): void;
|
|
132
|
+
protected visitUnion(node: UnionNode): void;
|
|
128
133
|
protected append(str: string): void;
|
|
129
134
|
protected appendValue(parameter: unknown): void;
|
|
130
135
|
protected getLeftIdentifierWrapper(): string;
|
|
@@ -17,7 +17,7 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
|
|
|
17
17
|
this.visitNode(node);
|
|
18
18
|
return (0, object_utils_js_1.freeze)({
|
|
19
19
|
sql: this.getSql(),
|
|
20
|
-
parameters: this.#parameters,
|
|
20
|
+
parameters: [...this.#parameters],
|
|
21
21
|
});
|
|
22
22
|
}
|
|
23
23
|
getSql() {
|
|
@@ -63,6 +63,10 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
|
|
|
63
63
|
this.append(' ');
|
|
64
64
|
this.visitNode(node.having);
|
|
65
65
|
}
|
|
66
|
+
if (node.union) {
|
|
67
|
+
this.append(' ');
|
|
68
|
+
this.compileList(node.union, ' ');
|
|
69
|
+
}
|
|
66
70
|
if (node.orderBy) {
|
|
67
71
|
this.append(' ');
|
|
68
72
|
this.visitNode(node.orderBy);
|
|
@@ -89,6 +93,12 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
|
|
|
89
93
|
this.append('from ');
|
|
90
94
|
this.compileList(node.froms);
|
|
91
95
|
}
|
|
96
|
+
visitSelection(node) {
|
|
97
|
+
this.visitNode(node.selection);
|
|
98
|
+
}
|
|
99
|
+
visitColumn(node) {
|
|
100
|
+
this.visitNode(node.column);
|
|
101
|
+
}
|
|
92
102
|
compileDistinctOn(selections) {
|
|
93
103
|
this.append('distinct on (');
|
|
94
104
|
this.compileList(selections);
|
|
@@ -630,6 +640,13 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
|
|
|
630
640
|
this.append('drop constraint ');
|
|
631
641
|
this.visitNode(node.constraintName);
|
|
632
642
|
}
|
|
643
|
+
visitUnion(node) {
|
|
644
|
+
this.append('union ');
|
|
645
|
+
if (node.all) {
|
|
646
|
+
this.append('all ');
|
|
647
|
+
}
|
|
648
|
+
this.visitNode(node.union);
|
|
649
|
+
}
|
|
633
650
|
append(str) {
|
|
634
651
|
this.#sql += str;
|
|
635
652
|
}
|
|
@@ -9,6 +9,7 @@ export declare class DefaultQueryExecutor extends QueryExecutor {
|
|
|
9
9
|
constructor(compiler: QueryCompiler, connectionProvider: ConnectionProvider, plugins?: KyselyPlugin[]);
|
|
10
10
|
compileQuery(node: RootOperationNode): CompiledQuery;
|
|
11
11
|
protected executeQueryImpl<R>(compiledQuery: CompiledQuery): Promise<QueryResult<R>>;
|
|
12
|
+
withPlugin(plugin: KyselyPlugin): DefaultQueryExecutor;
|
|
12
13
|
withPluginAtFront(plugin: KyselyPlugin): DefaultQueryExecutor;
|
|
13
14
|
withConnectionProvider(connectionProvider: ConnectionProvider): QueryExecutor;
|
|
14
15
|
withoutPlugins(): QueryExecutor;
|
|
@@ -18,6 +18,12 @@ class DefaultQueryExecutor extends query_executor_js_1.QueryExecutor {
|
|
|
18
18
|
return connection.executeQuery(compiledQuery);
|
|
19
19
|
});
|
|
20
20
|
}
|
|
21
|
+
withPlugin(plugin) {
|
|
22
|
+
return new DefaultQueryExecutor(this.#compiler, this.#connectionProvider, [
|
|
23
|
+
...this.plugins,
|
|
24
|
+
plugin,
|
|
25
|
+
]);
|
|
26
|
+
}
|
|
21
27
|
withPluginAtFront(plugin) {
|
|
22
28
|
return new DefaultQueryExecutor(this.#compiler, this.#connectionProvider, [
|
|
23
29
|
plugin,
|
|
@@ -11,6 +11,7 @@ export declare class NoopQueryExecutor extends QueryExecutor {
|
|
|
11
11
|
compileQuery(): CompiledQuery;
|
|
12
12
|
protected executeQueryImpl<R>(): Promise<QueryResult<R>>;
|
|
13
13
|
withConnectionProvider(): QueryExecutor;
|
|
14
|
+
withPlugin(plugin: KyselyPlugin): NoopQueryExecutor;
|
|
14
15
|
withPluginAtFront(plugin: KyselyPlugin): NoopQueryExecutor;
|
|
15
16
|
withoutPlugins(): QueryExecutor;
|
|
16
17
|
}
|
|
@@ -17,6 +17,9 @@ class NoopQueryExecutor extends query_executor_js_1.QueryExecutor {
|
|
|
17
17
|
withConnectionProvider() {
|
|
18
18
|
throw new Error('this query cannot have a connection provider');
|
|
19
19
|
}
|
|
20
|
+
withPlugin(plugin) {
|
|
21
|
+
return new NoopQueryExecutor([...this.plugins, plugin]);
|
|
22
|
+
}
|
|
20
23
|
withPluginAtFront(plugin) {
|
|
21
24
|
return new NoopQueryExecutor([plugin, ...this.plugins]);
|
|
22
25
|
}
|
|
@@ -35,15 +35,20 @@ export declare abstract class QueryExecutor {
|
|
|
35
35
|
* Executes a compiled query.
|
|
36
36
|
*/
|
|
37
37
|
protected abstract executeQueryImpl<R>(compiledQuery: CompiledQuery, queryId: QueryId): Promise<QueryResult<R>>;
|
|
38
|
+
/**
|
|
39
|
+
* Returns a copy of this executor with a new connection provider.
|
|
40
|
+
*/
|
|
41
|
+
abstract withConnectionProvider(connectionProvider: ConnectionProvider): QueryExecutor;
|
|
38
42
|
/**
|
|
39
43
|
* Returns a copy of this executor with a plugin added as the
|
|
40
|
-
*
|
|
44
|
+
* last plugin.
|
|
41
45
|
*/
|
|
42
|
-
abstract
|
|
46
|
+
abstract withPlugin(plugin: KyselyPlugin): QueryExecutor;
|
|
43
47
|
/**
|
|
44
|
-
* Returns a copy of this executor with a
|
|
48
|
+
* Returns a copy of this executor with a plugin added as the
|
|
49
|
+
* first plugin.
|
|
45
50
|
*/
|
|
46
|
-
abstract
|
|
51
|
+
abstract withPluginAtFront(plugin: KyselyPlugin): QueryExecutor;
|
|
47
52
|
/**
|
|
48
53
|
* Returns a copy of this executor without any plugins.
|
|
49
54
|
*/
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ArrayItemType } from './type-utils.js';
|
|
2
|
+
export declare const LOG_LEVELS: readonly ["query", "error"];
|
|
3
|
+
export declare type LogLevel = ArrayItemType<typeof LOG_LEVELS>;
|
|
4
|
+
export declare class Log {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(levels: ReadonlyArray<LogLevel>);
|
|
7
|
+
query(getEvent: () => QueryLogEvent): void;
|
|
8
|
+
error(getEvent: () => ErrorLogEvent): void;
|
|
9
|
+
}
|
|
10
|
+
export declare type LogEvent = QueryLogEvent | ErrorLogEvent;
|
|
11
|
+
export interface QueryLogEvent {
|
|
12
|
+
readonly level: 'query';
|
|
13
|
+
readonly sql: string;
|
|
14
|
+
readonly queryDurationMillis: number;
|
|
15
|
+
}
|
|
16
|
+
export interface ErrorLogEvent {
|
|
17
|
+
readonly level: 'error';
|
|
18
|
+
readonly error: unknown;
|
|
19
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Log = exports.LOG_LEVELS = void 0;
|
|
4
|
+
const object_utils_js_1 = require("./object-utils.js");
|
|
5
|
+
exports.LOG_LEVELS = ['query', 'error'];
|
|
6
|
+
class Log {
|
|
7
|
+
#levels;
|
|
8
|
+
constructor(levels) {
|
|
9
|
+
this.#levels = (0, object_utils_js_1.freeze)({
|
|
10
|
+
query: levels.includes('query'),
|
|
11
|
+
error: levels.includes('error'),
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
query(getEvent) {
|
|
15
|
+
if (this.#levels.query) {
|
|
16
|
+
this.#defaultQuery(getEvent());
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
error(getEvent) {
|
|
20
|
+
if (this.#levels.error) {
|
|
21
|
+
this.#defaultError(getEvent());
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
#defaultQuery(event) {
|
|
25
|
+
console.log(`kysely:query: ${event.sql}`);
|
|
26
|
+
console.log(`kysely:query: duration: ${event.queryDurationMillis.toFixed(1)}ms`);
|
|
27
|
+
}
|
|
28
|
+
#defaultError(event) {
|
|
29
|
+
if (event.error instanceof Error) {
|
|
30
|
+
console.error(`kysely:error: ${event.error.stack ?? event.error.message}`);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
console.error(`kysely:error: ${event}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.Log = Log;
|
|
@@ -3,10 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.randomString = void 0;
|
|
4
4
|
const CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
5
5
|
function randomString(length) {
|
|
6
|
-
let chars =
|
|
6
|
+
let chars = '';
|
|
7
7
|
for (let i = 0; i < length; ++i) {
|
|
8
|
-
chars
|
|
8
|
+
chars += randomChar();
|
|
9
9
|
}
|
|
10
|
-
return chars
|
|
10
|
+
return chars;
|
|
11
11
|
}
|
|
12
12
|
exports.randomString = randomString;
|
|
13
|
+
function randomChar() {
|
|
14
|
+
return CHARS[Math.floor(Math.random() * CHARS.length)];
|
|
15
|
+
}
|
|
@@ -136,19 +136,19 @@ export declare type RawBuilderFactory<DB, TB extends keyof DB> = (qb: Expression
|
|
|
136
136
|
export declare type AliasedRawBuilderFactory<DB, TB extends keyof DB> = (qb: ExpressionBuilder<DB, TB>) => AnyAliasedRawBuilder;
|
|
137
137
|
export interface InsertResultTypeTag {
|
|
138
138
|
/** @internal */
|
|
139
|
-
__isInsertResultTypeTag__: true;
|
|
139
|
+
readonly __isInsertResultTypeTag__: true;
|
|
140
140
|
}
|
|
141
141
|
export interface DeleteResultTypeTag {
|
|
142
142
|
/** @internal */
|
|
143
|
-
__isDeleteResultTypeTag__: true;
|
|
143
|
+
readonly __isDeleteResultTypeTag__: true;
|
|
144
144
|
}
|
|
145
145
|
export interface UpdateResultTypeTag {
|
|
146
146
|
/** @internal */
|
|
147
|
-
__isUpdateResultTypeTag__: true;
|
|
147
|
+
readonly __isUpdateResultTypeTag__: true;
|
|
148
148
|
}
|
|
149
149
|
export interface GeneratedPlaceholder {
|
|
150
150
|
/** @internal */
|
|
151
|
-
__isGeneratedPlaceholder__: true;
|
|
151
|
+
readonly __isGeneratedPlaceholder__: true;
|
|
152
152
|
}
|
|
153
153
|
export declare type ManyResultRowType<O> = O extends InsertResultTypeTag ? number | undefined : O extends DeleteResultTypeTag ? number : O extends UpdateResultTypeTag ? number : O;
|
|
154
154
|
export declare type SingleResultRowType<O> = O extends InsertResultTypeTag ? number | undefined : O extends DeleteResultTypeTag ? number : O extends UpdateResultTypeTag ? number : O | undefined;
|
|
@@ -5,6 +5,13 @@ import { Dialect } from '../dialect.js';
|
|
|
5
5
|
import { DatabaseIntrospector } from '../../introspection/database-introspector.js';
|
|
6
6
|
import { DialectAdapter } from '../dialect-adapter.js';
|
|
7
7
|
import { DatabaseConnection } from '../../driver/database-connection.js';
|
|
8
|
+
/**
|
|
9
|
+
* MySQL dialect that uses the [mysql2](https://github.com/sidorares/node-mysql2#readme) library.
|
|
10
|
+
*
|
|
11
|
+
* The {@link MysqlDialectConfig | configuration} passed to the constructor
|
|
12
|
+
* is given as-is to the mysql2 library's [createPool](https://github.com/sidorares/node-mysql2#using-connection-pools)
|
|
13
|
+
* method.
|
|
14
|
+
*/
|
|
8
15
|
export declare class MysqlDialect implements Dialect {
|
|
9
16
|
#private;
|
|
10
17
|
constructor(config: MysqlDialectConfig);
|
|
@@ -17,6 +24,8 @@ export declare class MysqlDialect implements Dialect {
|
|
|
17
24
|
* Config for the mysql dialect.
|
|
18
25
|
*
|
|
19
26
|
* This interface is equal to `mysql2` library's pool config.
|
|
27
|
+
*
|
|
28
|
+
* https://github.com/sidorares/node-mysql2#using-connection-pools
|
|
20
29
|
*/
|
|
21
30
|
export interface MysqlDialectConfig {
|
|
22
31
|
/**
|
|
@@ -176,6 +185,8 @@ export interface MysqlDialectConfig {
|
|
|
176
185
|
keepAliveInitialDelay?: number;
|
|
177
186
|
/**
|
|
178
187
|
* Called once for each created connection.
|
|
188
|
+
*
|
|
189
|
+
* This is a Kysely specific feature and does not come from the `pg` module.
|
|
179
190
|
*/
|
|
180
191
|
onCreateConnection?: (connection: DatabaseConnection) => Promise<void>;
|
|
181
192
|
}
|
|
@@ -2,6 +2,13 @@ import { MysqlDriver } from './mysql-driver.js';
|
|
|
2
2
|
import { MysqlQueryCompiler } from './mysql-query-compiler.js';
|
|
3
3
|
import { MysqlIntrospector } from './mysql-introspector.js';
|
|
4
4
|
import { MysqlAdapter } from './mysql-adapter.js';
|
|
5
|
+
/**
|
|
6
|
+
* MySQL dialect that uses the [mysql2](https://github.com/sidorares/node-mysql2#readme) library.
|
|
7
|
+
*
|
|
8
|
+
* The {@link MysqlDialectConfig | configuration} passed to the constructor
|
|
9
|
+
* is given as-is to the mysql2 library's [createPool](https://github.com/sidorares/node-mysql2#using-connection-pools)
|
|
10
|
+
* method.
|
|
11
|
+
*/
|
|
5
12
|
export class MysqlDialect {
|
|
6
13
|
#config;
|
|
7
14
|
constructor(config) {
|
|
@@ -8,6 +8,16 @@ import { Dialect } from '../dialect.js';
|
|
|
8
8
|
import { DatabaseIntrospector } from '../../introspection/database-introspector.js';
|
|
9
9
|
import { DialectAdapter } from '../dialect-adapter.js';
|
|
10
10
|
import { DatabaseConnection } from '../../driver/database-connection.js';
|
|
11
|
+
/**
|
|
12
|
+
* PostgreSQL dialect that uses the [pg](https://node-postgres.com/) library.
|
|
13
|
+
*
|
|
14
|
+
* The {@link PostgresDialectConfig | configuration} passed to the constructor
|
|
15
|
+
* is given as-is to the pg library's [Pool](https://node-postgres.com/api/pool)
|
|
16
|
+
* constructor. See the following two links for more documentation:
|
|
17
|
+
*
|
|
18
|
+
* https://node-postgres.com/api/pool
|
|
19
|
+
* https://node-postgres.com/api/client
|
|
20
|
+
*/
|
|
11
21
|
export declare class PostgresDialect implements Dialect {
|
|
12
22
|
#private;
|
|
13
23
|
constructor(config: PostgresDialectConfig);
|
|
@@ -19,7 +29,7 @@ export declare class PostgresDialect implements Dialect {
|
|
|
19
29
|
/**
|
|
20
30
|
* Config for the postgres dialect.
|
|
21
31
|
*
|
|
22
|
-
* This interface is equal to `pg` library's
|
|
32
|
+
* This interface is equal to `pg` library's `Pool` config:
|
|
23
33
|
*
|
|
24
34
|
* https://node-postgres.com/api/pool
|
|
25
35
|
* https://node-postgres.com/api/client
|
|
@@ -105,6 +115,8 @@ export interface PostgresDialectConfig {
|
|
|
105
115
|
application_name?: string;
|
|
106
116
|
/**
|
|
107
117
|
* Called once for each created connection.
|
|
118
|
+
*
|
|
119
|
+
* This is a Kysely specific feature and does not come from the `pg` module.
|
|
108
120
|
*/
|
|
109
121
|
onCreateConnection?: (connection: DatabaseConnection) => Promise<void>;
|
|
110
122
|
keepAlive?: boolean;
|
|
@@ -2,6 +2,16 @@ import { PostgresDriver } from './postgres-driver.js';
|
|
|
2
2
|
import { PostgresIntrospector } from './postgres-introspector.js';
|
|
3
3
|
import { PostgresQueryCompiler } from './postgres-query-compiler.js';
|
|
4
4
|
import { PostgresAdapter } from './postgres-adapter.js';
|
|
5
|
+
/**
|
|
6
|
+
* PostgreSQL dialect that uses the [pg](https://node-postgres.com/) library.
|
|
7
|
+
*
|
|
8
|
+
* The {@link PostgresDialectConfig | configuration} passed to the constructor
|
|
9
|
+
* is given as-is to the pg library's [Pool](https://node-postgres.com/api/pool)
|
|
10
|
+
* constructor. See the following two links for more documentation:
|
|
11
|
+
*
|
|
12
|
+
* https://node-postgres.com/api/pool
|
|
13
|
+
* https://node-postgres.com/api/client
|
|
14
|
+
*/
|
|
5
15
|
export class PostgresDialect {
|
|
6
16
|
#config;
|
|
7
17
|
constructor(config) {
|
|
@@ -13,10 +13,10 @@ export class PostgresDriver {
|
|
|
13
13
|
// installed. As you can see, there IS an import from `pg` at the
|
|
14
14
|
// top level too, but that's only for types. It doesn't get compiled
|
|
15
15
|
// into javascript. You can check the built javascript code.
|
|
16
|
-
const
|
|
16
|
+
const PoolConstructor = await importPgPool();
|
|
17
17
|
// Use the `pg` module's own pool. All drivers should use the
|
|
18
18
|
// pool provided by the database library if possible.
|
|
19
|
-
this.#pool = new
|
|
19
|
+
this.#pool = new PoolConstructor(this.#config);
|
|
20
20
|
}
|
|
21
21
|
async acquireConnection() {
|
|
22
22
|
const client = await this.#pool.connect();
|