kysely 0.10.1 → 0.11.3
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 +7 -3
- package/dist/cjs/dialect/dialect-adapter.d.ts +3 -2
- package/dist/cjs/dialect/mysql/mysql-adapter.js +1 -1
- package/dist/cjs/dialect/mysql/mysql-dialect.d.ts +3 -3
- package/dist/cjs/dialect/mysql/mysql-driver.js +25 -32
- package/dist/cjs/dialect/mysql/mysql-query-compiler.d.ts +0 -2
- package/dist/cjs/dialect/mysql/mysql-query-compiler.js +0 -6
- package/dist/cjs/dialect/postgres/postgres-dialect.d.ts +2 -2
- package/dist/cjs/dialect/postgres/postgres-driver.js +8 -19
- package/dist/cjs/dialect/postgres/postgres-query-compiler.d.ts +3 -0
- package/dist/cjs/dialect/postgres/postgres-query-compiler.js +9 -1
- package/dist/cjs/dialect/sqlite/sqlite-adapter.d.ts +7 -0
- package/dist/cjs/dialect/sqlite/sqlite-adapter.js +18 -0
- package/dist/cjs/dialect/sqlite/sqlite-dialect.d.ts +64 -0
- package/dist/cjs/dialect/sqlite/sqlite-dialect.js +33 -0
- package/dist/cjs/dialect/sqlite/sqlite-driver.d.ts +14 -0
- package/dist/cjs/dialect/sqlite/sqlite-driver.js +116 -0
- package/dist/cjs/dialect/sqlite/sqlite-introspector.d.ts +7 -0
- package/dist/cjs/dialect/sqlite/sqlite-introspector.js +44 -0
- package/dist/cjs/dialect/sqlite/sqlite-query-compiler.d.ts +7 -0
- package/dist/cjs/dialect/sqlite/sqlite-query-compiler.js +19 -0
- package/dist/cjs/driver/database-connection.d.ts +2 -2
- package/dist/cjs/driver/runtime-driver.js +1 -1
- package/dist/cjs/index.d.ts +46 -30
- package/dist/cjs/index.js +45 -29
- package/dist/cjs/kysely.d.ts +28 -10
- package/dist/cjs/kysely.js +6 -4
- package/dist/cjs/migration/migration.d.ts +1 -1
- package/dist/cjs/migration/migration.js +3 -3
- package/dist/cjs/operation-node/alias-node.d.ts +1 -1
- package/dist/cjs/operation-node/create-view-node.d.ts +1 -0
- package/dist/cjs/operation-node/having-node.d.ts +4 -8
- package/dist/cjs/operation-node/having-node.js +3 -3
- package/dist/cjs/operation-node/join-node.d.ts +6 -9
- package/dist/cjs/operation-node/join-node.js +10 -7
- package/dist/cjs/operation-node/on-node.d.ts +14 -0
- package/dist/cjs/operation-node/on-node.js +28 -0
- package/dist/cjs/operation-node/operation-node-transformer.d.ts +2 -0
- package/dist/cjs/operation-node/operation-node-transformer.js +8 -0
- package/dist/cjs/operation-node/operation-node-utils.d.ts +1 -1
- package/dist/cjs/operation-node/operation-node-visitor.d.ts +3 -0
- package/dist/cjs/operation-node/operation-node-visitor.js +4 -0
- package/dist/cjs/operation-node/operation-node.d.ts +1 -1
- package/dist/cjs/operation-node/query-node.d.ts +3 -3
- package/dist/cjs/operation-node/select-query-node.d.ts +4 -4
- package/dist/cjs/operation-node/where-node.d.ts +4 -8
- package/dist/cjs/operation-node/where-node.js +3 -3
- package/dist/cjs/parser/filter-parser.d.ts +1 -0
- package/dist/cjs/parser/filter-parser.js +49 -52
- package/dist/cjs/parser/returning-parser.d.ts +4 -2
- package/dist/cjs/parser/select-parser.d.ts +2 -2
- package/dist/cjs/plugin/immediate-value.ts/immediate-value-plugin.d.ts +1 -1
- package/dist/cjs/query-builder/delete-result.d.ts +5 -0
- package/dist/cjs/query-builder/delete-result.js +13 -0
- package/dist/cjs/query-builder/expression-builder.d.ts +2 -2
- package/dist/cjs/query-builder/expression-builder.js +1 -1
- package/dist/cjs/query-builder/function-builder.d.ts +5 -5
- package/dist/cjs/query-builder/function-builder.js +3 -3
- package/dist/cjs/query-builder/insert-result.d.ts +27 -0
- package/dist/cjs/query-builder/insert-result.js +35 -0
- package/dist/cjs/query-builder/join-builder.d.ts +3 -0
- package/dist/cjs/query-builder/query-builder.d.ts +95 -81
- package/dist/cjs/query-builder/query-builder.js +38 -41
- package/dist/cjs/query-builder/update-result.d.ts +5 -0
- package/dist/cjs/query-builder/update-result.js +13 -0
- package/dist/cjs/query-compiler/compiled-query.d.ts +5 -0
- package/dist/cjs/query-compiler/compiled-query.js +12 -0
- package/dist/cjs/query-compiler/default-query-compiler.d.ts +2 -0
- package/dist/cjs/query-compiler/default-query-compiler.js +24 -13
- package/dist/cjs/query-creator.d.ts +36 -26
- package/dist/cjs/query-creator.js +25 -17
- package/dist/cjs/schema/create-view-builder.d.ts +4 -0
- package/dist/cjs/schema/create-view-builder.js +11 -0
- package/dist/cjs/util/dynamic-import.d.ts +2 -0
- package/dist/cjs/util/dynamic-import.js +32 -0
- package/dist/cjs/util/log.d.ts +10 -8
- package/dist/cjs/util/log.js +29 -9
- package/dist/cjs/util/type-utils.d.ts +4 -15
- package/dist/esm/dialect/dialect-adapter.d.ts +3 -2
- package/dist/esm/dialect/mysql/mysql-adapter.js +1 -1
- package/dist/esm/dialect/mysql/mysql-dialect.d.ts +3 -3
- package/dist/esm/dialect/mysql/mysql-driver.js +26 -33
- package/dist/esm/dialect/mysql/mysql-query-compiler.d.ts +0 -2
- package/dist/esm/dialect/mysql/mysql-query-compiler.js +0 -6
- package/dist/esm/dialect/postgres/postgres-dialect.d.ts +2 -2
- package/dist/esm/dialect/postgres/postgres-driver.js +8 -19
- package/dist/esm/dialect/postgres/postgres-query-compiler.d.ts +3 -0
- package/dist/esm/dialect/postgres/postgres-query-compiler.js +9 -1
- package/dist/esm/dialect/sqlite/sqlite-adapter.d.ts +7 -0
- package/dist/esm/dialect/sqlite/sqlite-adapter.js +14 -0
- package/dist/esm/dialect/sqlite/sqlite-dialect.d.ts +64 -0
- package/dist/esm/dialect/sqlite/sqlite-dialect.js +29 -0
- package/dist/esm/dialect/sqlite/sqlite-driver.d.ts +14 -0
- package/dist/esm/dialect/sqlite/sqlite-driver.js +112 -0
- package/dist/esm/dialect/sqlite/sqlite-introspector.d.ts +7 -0
- package/dist/esm/dialect/sqlite/sqlite-introspector.js +40 -0
- package/dist/esm/dialect/sqlite/sqlite-query-compiler.d.ts +7 -0
- package/dist/esm/dialect/sqlite/sqlite-query-compiler.js +15 -0
- package/dist/esm/driver/database-connection.d.ts +2 -2
- package/dist/esm/driver/runtime-driver.js +1 -1
- package/dist/esm/index.d.ts +46 -30
- package/dist/esm/index.js +45 -29
- package/dist/esm/kysely.d.ts +28 -10
- package/dist/esm/kysely.js +6 -4
- package/dist/esm/migration/migration.d.ts +1 -1
- package/dist/esm/migration/migration.js +3 -3
- package/dist/esm/operation-node/alias-node.d.ts +1 -1
- package/dist/esm/operation-node/create-view-node.d.ts +1 -0
- package/dist/esm/operation-node/having-node.d.ts +4 -8
- package/dist/esm/operation-node/having-node.js +3 -3
- package/dist/esm/operation-node/join-node.d.ts +6 -9
- package/dist/esm/operation-node/join-node.js +10 -7
- package/dist/esm/operation-node/on-node.d.ts +14 -0
- package/dist/esm/operation-node/on-node.js +25 -0
- package/dist/esm/operation-node/operation-node-transformer.d.ts +2 -0
- package/dist/esm/operation-node/operation-node-transformer.js +8 -0
- package/dist/esm/operation-node/operation-node-utils.d.ts +1 -1
- package/dist/esm/operation-node/operation-node-visitor.d.ts +3 -0
- package/dist/esm/operation-node/operation-node-visitor.js +4 -0
- package/dist/esm/operation-node/operation-node.d.ts +1 -1
- package/dist/esm/operation-node/query-node.d.ts +3 -3
- package/dist/esm/operation-node/select-query-node.d.ts +4 -4
- package/dist/esm/operation-node/where-node.d.ts +4 -8
- package/dist/esm/operation-node/where-node.js +3 -3
- package/dist/esm/parser/filter-parser.d.ts +1 -0
- package/dist/esm/parser/filter-parser.js +48 -52
- package/dist/esm/parser/returning-parser.d.ts +4 -2
- package/dist/esm/parser/select-parser.d.ts +2 -2
- package/dist/esm/plugin/immediate-value.ts/immediate-value-plugin.d.ts +1 -1
- package/dist/esm/query-builder/delete-result.d.ts +5 -0
- package/dist/esm/query-builder/delete-result.js +9 -0
- package/dist/esm/query-builder/expression-builder.d.ts +2 -2
- package/dist/esm/query-builder/expression-builder.js +1 -1
- package/dist/esm/query-builder/function-builder.d.ts +5 -5
- package/dist/esm/query-builder/function-builder.js +3 -3
- package/dist/esm/query-builder/insert-result.d.ts +27 -0
- package/dist/esm/query-builder/insert-result.js +31 -0
- package/dist/esm/query-builder/join-builder.d.ts +3 -0
- package/dist/esm/query-builder/query-builder.d.ts +95 -81
- package/dist/esm/query-builder/query-builder.js +38 -41
- package/dist/esm/query-builder/update-result.d.ts +5 -0
- package/dist/esm/query-builder/update-result.js +9 -0
- package/dist/esm/query-compiler/compiled-query.d.ts +5 -0
- package/dist/esm/query-compiler/compiled-query.js +11 -1
- package/dist/esm/query-compiler/default-query-compiler.d.ts +2 -0
- package/dist/esm/query-compiler/default-query-compiler.js +24 -13
- package/dist/esm/query-creator.d.ts +36 -26
- package/dist/esm/query-creator.js +25 -17
- package/dist/esm/schema/create-view-builder.d.ts +4 -0
- package/dist/esm/schema/create-view-builder.js +11 -0
- package/dist/esm/util/dynamic-import.d.ts +2 -0
- package/dist/esm/util/dynamic-import.js +27 -0
- package/dist/esm/util/log.d.ts +10 -8
- package/dist/esm/util/log.js +30 -10
- package/dist/esm/util/type-utils.d.ts +4 -15
- package/package.json +3 -1
|
@@ -29,20 +29,17 @@ class QueryCreator {
|
|
|
29
29
|
/**
|
|
30
30
|
* Creates an insert query.
|
|
31
31
|
*
|
|
32
|
-
* The return value of this query is
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* If you are running a database engine like `Mysql` that always returns the primary
|
|
36
|
-
* key, you can safely use `!` or the {@link QueryBuilder.castTo | castTo} method
|
|
37
|
-
* to cast away the `undefined` from the type.
|
|
32
|
+
* The return value of this query is an instance of {@link InsertResult}. {@link InsertResult}
|
|
33
|
+
* has the {@link InsertResult.insertId | insertId} field that holds the auto incremented id of
|
|
34
|
+
* the inserted row if the db returned one.
|
|
38
35
|
*
|
|
39
36
|
* See the {@link QueryBuilder.values | values} method for more info and examples. Also see
|
|
40
37
|
* the {@link QueryBuilder.returning | returning} method for a way to return columns
|
|
41
|
-
* on supported databases like
|
|
38
|
+
* on supported databases like PostgreSQL.
|
|
42
39
|
*
|
|
43
40
|
* @example
|
|
44
41
|
* ```ts
|
|
45
|
-
* const
|
|
42
|
+
* const result = await db
|
|
46
43
|
* .insertInto('person')
|
|
47
44
|
* .values({
|
|
48
45
|
* id: db.generated,
|
|
@@ -50,10 +47,12 @@ class QueryCreator {
|
|
|
50
47
|
* last_name: 'Aniston'
|
|
51
48
|
* })
|
|
52
49
|
* .executeTakeFirst()
|
|
50
|
+
*
|
|
51
|
+
* console.log(result.insertId)
|
|
53
52
|
* ```
|
|
54
53
|
*
|
|
55
54
|
* @example
|
|
56
|
-
* Some databases like
|
|
55
|
+
* Some databases like PostgreSQL support the `returning` method:
|
|
57
56
|
*
|
|
58
57
|
* ```ts
|
|
59
58
|
* const { id } = await db
|
|
@@ -79,14 +78,18 @@ class QueryCreator {
|
|
|
79
78
|
* Creates a delete query.
|
|
80
79
|
*
|
|
81
80
|
* See the {@link QueryBuilder.where} method for examples on how to specify
|
|
82
|
-
* a where clause for the delete operation
|
|
81
|
+
* a where clause for the delete operation.
|
|
82
|
+
*
|
|
83
|
+
* The return value of the query is an instance of {@link DeleteResult}.
|
|
83
84
|
*
|
|
84
85
|
* @example
|
|
85
86
|
* ```ts
|
|
86
|
-
* const
|
|
87
|
+
* const result = await db
|
|
87
88
|
* .deleteFrom('person')
|
|
88
89
|
* .where('person.id', '=', 1)
|
|
89
90
|
* .executeTakeFirst()
|
|
91
|
+
*
|
|
92
|
+
* console.log(result.numDeletedRows)
|
|
90
93
|
* ```
|
|
91
94
|
*/
|
|
92
95
|
deleteFrom(table) {
|
|
@@ -106,13 +109,17 @@ class QueryCreator {
|
|
|
106
109
|
* See the {@link QueryBuilder.set} method for examples on how to
|
|
107
110
|
* specify the updates.
|
|
108
111
|
*
|
|
112
|
+
* The return value of the query is an {@link UpdateResult}.
|
|
113
|
+
*
|
|
109
114
|
* @example
|
|
110
115
|
* ```ts
|
|
111
|
-
* const
|
|
116
|
+
* const result = await db
|
|
112
117
|
* .updateTable('person')
|
|
113
118
|
* .set({ first_name: 'Jennifer' })
|
|
114
119
|
* .where('person.id', '=', 1)
|
|
115
120
|
* .executeTakeFirst()
|
|
121
|
+
*
|
|
122
|
+
* console.log(result.numUpdatedRows)
|
|
116
123
|
* ```
|
|
117
124
|
*/
|
|
118
125
|
updateTable(table) {
|
|
@@ -170,7 +177,7 @@ class QueryCreator {
|
|
|
170
177
|
* .execute()
|
|
171
178
|
* ```
|
|
172
179
|
*
|
|
173
|
-
* The generated SQL (
|
|
180
|
+
* The generated SQL (PostgreSQL):
|
|
174
181
|
*
|
|
175
182
|
* ```sql
|
|
176
183
|
* select * from "mammals"."pet"
|
|
@@ -190,7 +197,7 @@ class QueryCreator {
|
|
|
190
197
|
* .execute()
|
|
191
198
|
* ```
|
|
192
199
|
*
|
|
193
|
-
* The generated SQL (
|
|
200
|
+
* The generated SQL (PostgreSQL):
|
|
194
201
|
*
|
|
195
202
|
* ```sql
|
|
196
203
|
* select "p"."name" from "mammals"."pet" as "p"
|
|
@@ -240,7 +247,7 @@ class QueryCreator {
|
|
|
240
247
|
* console.log(person.name)
|
|
241
248
|
* ```
|
|
242
249
|
*
|
|
243
|
-
* The generated SQL (
|
|
250
|
+
* The generated SQL (PostgreSQL):
|
|
244
251
|
*
|
|
245
252
|
* ```sql
|
|
246
253
|
* select concat(first_name, ' ', last_name) as "name"
|
|
@@ -298,7 +305,7 @@ class QueryCreator {
|
|
|
298
305
|
* }
|
|
299
306
|
* ```
|
|
300
307
|
*
|
|
301
|
-
* The generated SQL (
|
|
308
|
+
* The generated SQL (PostgreSQL):
|
|
302
309
|
*
|
|
303
310
|
* ```sql
|
|
304
311
|
* select * from "person" where now() - birth_date > interval $1 year
|
|
@@ -312,7 +319,8 @@ class QueryCreator {
|
|
|
312
319
|
* Example of creating a completely raw query from scratch:
|
|
313
320
|
*
|
|
314
321
|
* ```ts
|
|
315
|
-
* const
|
|
322
|
+
* const result = await db.raw<Person>('select p.* from person p').execute()
|
|
323
|
+
* const persons = result.rows
|
|
316
324
|
* ```
|
|
317
325
|
*
|
|
318
326
|
* For a raw query, you need to specify the type of the returned __row__. In
|
|
@@ -10,6 +10,10 @@ export declare class CreateViewBuilder implements OperationNodeSource, Compilabl
|
|
|
10
10
|
#private;
|
|
11
11
|
constructor(props: CreateViewBuilderProps);
|
|
12
12
|
materialized(): CreateViewBuilder;
|
|
13
|
+
/**
|
|
14
|
+
* Only implemented on some dialects like SQLite. On most dialects, use {@link orReplace}.
|
|
15
|
+
*/
|
|
16
|
+
ifNotExists(): CreateViewBuilder;
|
|
13
17
|
orReplace(): CreateViewBuilder;
|
|
14
18
|
columns(columns: string[]): CreateViewBuilder;
|
|
15
19
|
as(query: AnyQueryBuilder | AnyRawBuilder): CreateViewBuilder;
|
|
@@ -31,6 +31,17 @@ class CreateViewBuilder {
|
|
|
31
31
|
}),
|
|
32
32
|
});
|
|
33
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Only implemented on some dialects like SQLite. On most dialects, use {@link orReplace}.
|
|
36
|
+
*/
|
|
37
|
+
ifNotExists() {
|
|
38
|
+
return new CreateViewBuilder({
|
|
39
|
+
...this.#props,
|
|
40
|
+
createViewNode: create_view_node_js_1.CreateViewNode.cloneWith(this.#props.createViewNode, {
|
|
41
|
+
ifNotExists: true,
|
|
42
|
+
}),
|
|
43
|
+
});
|
|
44
|
+
}
|
|
34
45
|
orReplace() {
|
|
35
46
|
return new CreateViewBuilder({
|
|
36
47
|
...this.#props,
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.importNamedFunction = exports.importFunction = void 0;
|
|
4
|
+
const object_utils_js_1 = require("./object-utils.js");
|
|
5
|
+
async function importFunction(modulePath) {
|
|
6
|
+
const module = await Promise.resolve().then(() => require(modulePath));
|
|
7
|
+
const errorMessage = `module "${modulePath}" doesn't export a function`;
|
|
8
|
+
if ((0, object_utils_js_1.isFunction)(module)) {
|
|
9
|
+
return module;
|
|
10
|
+
}
|
|
11
|
+
else if ((0, object_utils_js_1.isObject)(module) && (0, object_utils_js_1.isFunction)(module.default)) {
|
|
12
|
+
return module.default;
|
|
13
|
+
}
|
|
14
|
+
throw new Error(errorMessage);
|
|
15
|
+
}
|
|
16
|
+
exports.importFunction = importFunction;
|
|
17
|
+
async function importNamedFunction(modulePath, exportName) {
|
|
18
|
+
const module = await Promise.resolve().then(() => require(modulePath));
|
|
19
|
+
const errorMessage = `no function export named "${exportName}" found in module "${modulePath}"`;
|
|
20
|
+
if (!(0, object_utils_js_1.isObject)(module)) {
|
|
21
|
+
throw new Error(errorMessage);
|
|
22
|
+
}
|
|
23
|
+
if ((0, object_utils_js_1.isFunction)(module[exportName])) {
|
|
24
|
+
return module[exportName];
|
|
25
|
+
}
|
|
26
|
+
else if ((0, object_utils_js_1.isObject)(module.default) &&
|
|
27
|
+
(0, object_utils_js_1.isFunction)(module.default[exportName])) {
|
|
28
|
+
return module.default[exportName];
|
|
29
|
+
}
|
|
30
|
+
throw new Error(errorMessage);
|
|
31
|
+
}
|
|
32
|
+
exports.importNamedFunction = importNamedFunction;
|
package/dist/cjs/util/log.d.ts
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
|
+
import { CompiledQuery } from '../index.js';
|
|
1
2
|
import { ArrayItemType } from './type-utils.js';
|
|
2
3
|
export declare const LOG_LEVELS: readonly ["query", "error"];
|
|
3
4
|
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
5
|
export interface QueryLogEvent {
|
|
12
6
|
readonly level: 'query';
|
|
13
|
-
readonly
|
|
7
|
+
readonly query: CompiledQuery;
|
|
14
8
|
readonly queryDurationMillis: number;
|
|
15
9
|
}
|
|
16
10
|
export interface ErrorLogEvent {
|
|
17
11
|
readonly level: 'error';
|
|
18
12
|
readonly error: unknown;
|
|
19
13
|
}
|
|
14
|
+
export declare type LogEvent = QueryLogEvent | ErrorLogEvent;
|
|
15
|
+
export declare type LogConfig = ReadonlyArray<LogLevel> | ((event: LogEvent) => void);
|
|
16
|
+
export declare class Log {
|
|
17
|
+
#private;
|
|
18
|
+
constructor(config: LogConfig);
|
|
19
|
+
query(getEvent: () => QueryLogEvent): void;
|
|
20
|
+
error(getEvent: () => ErrorLogEvent): void;
|
|
21
|
+
}
|
package/dist/cjs/util/log.js
CHANGED
|
@@ -2,27 +2,47 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Log = exports.LOG_LEVELS = void 0;
|
|
4
4
|
const object_utils_js_1 = require("./object-utils.js");
|
|
5
|
-
exports.LOG_LEVELS = ['query', 'error'];
|
|
5
|
+
exports.LOG_LEVELS = (0, object_utils_js_1.freeze)(['query', 'error']);
|
|
6
6
|
class Log {
|
|
7
7
|
#levels;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
#logger;
|
|
9
|
+
constructor(config) {
|
|
10
|
+
if ((0, object_utils_js_1.isFunction)(config)) {
|
|
11
|
+
this.#logger = config;
|
|
12
|
+
this.#levels = (0, object_utils_js_1.freeze)({
|
|
13
|
+
query: true,
|
|
14
|
+
error: true,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
this.#levels = (0, object_utils_js_1.freeze)({
|
|
19
|
+
query: config.includes('query'),
|
|
20
|
+
error: config.includes('error'),
|
|
21
|
+
});
|
|
22
|
+
}
|
|
13
23
|
}
|
|
14
24
|
query(getEvent) {
|
|
15
25
|
if (this.#levels.query) {
|
|
16
|
-
this.#
|
|
26
|
+
if (this.#logger) {
|
|
27
|
+
this.#logger(getEvent());
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
this.#defaultQuery(getEvent());
|
|
31
|
+
}
|
|
17
32
|
}
|
|
18
33
|
}
|
|
19
34
|
error(getEvent) {
|
|
20
35
|
if (this.#levels.error) {
|
|
21
|
-
this.#
|
|
36
|
+
if (this.#logger) {
|
|
37
|
+
this.#logger(getEvent());
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
this.#defaultError(getEvent());
|
|
41
|
+
}
|
|
22
42
|
}
|
|
23
43
|
}
|
|
24
44
|
#defaultQuery(event) {
|
|
25
|
-
console.log(`kysely:query: ${event.sql}`);
|
|
45
|
+
console.log(`kysely:query: ${event.query.sql}`);
|
|
26
46
|
console.log(`kysely:query: duration: ${event.queryDurationMillis.toFixed(1)}ms`);
|
|
27
47
|
}
|
|
28
48
|
#defaultError(event) {
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { AliasedRawBuilder, RawBuilder } from '../raw-builder/raw-builder.js';
|
|
2
2
|
import { AliasedQueryBuilder, QueryBuilder } from '../query-builder/query-builder.js';
|
|
3
3
|
import { ExpressionBuilder } from '../query-builder/expression-builder.js';
|
|
4
|
+
import { InsertResult } from '../query-builder/insert-result.js';
|
|
5
|
+
import { DeleteResult } from '../query-builder/delete-result.js';
|
|
6
|
+
import { UpdateResult } from '../query-builder/update-result.js';
|
|
4
7
|
/**
|
|
5
8
|
* Given an object type, extracts the union of all value types.
|
|
6
9
|
*/
|
|
@@ -134,25 +137,11 @@ export declare type QueryBuilderFactory<DB, TB extends keyof DB> = (qb: Expressi
|
|
|
134
137
|
export declare type AliasedQueryBuilderFactory<DB, TB extends keyof DB> = (qb: ExpressionBuilder<DB, TB>) => AnyAliasedQueryBuilder;
|
|
135
138
|
export declare type RawBuilderFactory<DB, TB extends keyof DB> = (qb: ExpressionBuilder<DB, TB>) => AnyRawBuilder;
|
|
136
139
|
export declare type AliasedRawBuilderFactory<DB, TB extends keyof DB> = (qb: ExpressionBuilder<DB, TB>) => AnyAliasedRawBuilder;
|
|
137
|
-
export interface InsertResultTypeTag {
|
|
138
|
-
/** @internal */
|
|
139
|
-
readonly __isInsertResultTypeTag__: true;
|
|
140
|
-
}
|
|
141
|
-
export interface DeleteResultTypeTag {
|
|
142
|
-
/** @internal */
|
|
143
|
-
readonly __isDeleteResultTypeTag__: true;
|
|
144
|
-
}
|
|
145
|
-
export interface UpdateResultTypeTag {
|
|
146
|
-
/** @internal */
|
|
147
|
-
readonly __isUpdateResultTypeTag__: true;
|
|
148
|
-
}
|
|
149
140
|
export interface GeneratedPlaceholder {
|
|
150
141
|
/** @internal */
|
|
151
142
|
readonly __isGeneratedPlaceholder__: true;
|
|
152
143
|
}
|
|
153
|
-
export declare type
|
|
154
|
-
export declare type SingleResultRowType<O> = O extends InsertResultTypeTag ? number | undefined : O extends DeleteResultTypeTag ? number : O extends UpdateResultTypeTag ? number : O | undefined;
|
|
155
|
-
export declare type NonEmptySingleResultRowType<O> = O extends InsertResultTypeTag ? number : O extends DeleteResultTypeTag ? number : O extends UpdateResultTypeTag ? number : O;
|
|
144
|
+
export declare type SingleResultType<O> = O extends InsertResult ? O : O extends DeleteResult ? O : O extends UpdateResult ? O : O | undefined;
|
|
156
145
|
export declare type UnknownRow = Record<string, unknown>;
|
|
157
146
|
export declare type Nullable<T> = {
|
|
158
147
|
[P in keyof T]: T[P] | null;
|
|
@@ -24,7 +24,7 @@ export interface DialectAdapter {
|
|
|
24
24
|
* it's not possible for two migration processes to run in parallel.
|
|
25
25
|
*
|
|
26
26
|
* Most dialects have explicit locks that can be used, like advisory locks
|
|
27
|
-
* in
|
|
27
|
+
* in PostgreSQL and the get_lock function in MySQL.
|
|
28
28
|
*
|
|
29
29
|
* If the dialect doesn't have explicit locks the {@link MIGRATION_LOCK_TABLE}
|
|
30
30
|
* created by Kysely can be used instead. {@link MIGRATION_LOCK_TABLE}
|
|
@@ -44,7 +44,8 @@ export interface DialectAdapter {
|
|
|
44
44
|
*
|
|
45
45
|
* If `supportsTransactionalDdl` is `true` then the `db` passed to this method
|
|
46
46
|
* is a transaction inside which the migrations were executed. Otherwise `db`
|
|
47
|
-
* is a single connection (session) that was used to execute the migrations
|
|
47
|
+
* is a single connection (session) that was used to execute the migrations
|
|
48
|
+
* and the `acquireMigrationLock` call.
|
|
48
49
|
*/
|
|
49
50
|
releaseMigrationLock(db: Kysely<any>): Promise<void>;
|
|
50
51
|
}
|
|
@@ -13,7 +13,7 @@ export class MysqlAdapter {
|
|
|
13
13
|
// released when the connection is destroyed (session ends) or when the lock
|
|
14
14
|
// is released using `release_lock`. This way we know that the lock is either
|
|
15
15
|
// released by us after successfull or failed migrations OR it's released by
|
|
16
|
-
//
|
|
16
|
+
// MySQL if the process gets killed for some reason.
|
|
17
17
|
await db
|
|
18
18
|
.raw(`select get_lock('${LOCK_ID}', ${LOCK_TIMEOUT_SECONDS})`)
|
|
19
19
|
.execute();
|
|
@@ -21,7 +21,7 @@ export declare class MysqlDialect implements Dialect {
|
|
|
21
21
|
createIntrospector(db: Kysely<any>): DatabaseIntrospector;
|
|
22
22
|
}
|
|
23
23
|
/**
|
|
24
|
-
* Config for the
|
|
24
|
+
* Config for the MySQL dialect.
|
|
25
25
|
*
|
|
26
26
|
* This interface is equal to `mysql2` library's pool config.
|
|
27
27
|
*
|
|
@@ -137,7 +137,7 @@ export interface MysqlDialectConfig {
|
|
|
137
137
|
*/
|
|
138
138
|
trace?: boolean;
|
|
139
139
|
/**
|
|
140
|
-
* Allow multiple
|
|
140
|
+
* Allow multiple MySQL statements per query. Be careful with this, it exposes you to SQL injection attacks. (Default: false)
|
|
141
141
|
*/
|
|
142
142
|
multipleStatements?: boolean;
|
|
143
143
|
/**
|
|
@@ -186,7 +186,7 @@ export interface MysqlDialectConfig {
|
|
|
186
186
|
/**
|
|
187
187
|
* Called once for each created connection.
|
|
188
188
|
*
|
|
189
|
-
* This is a Kysely specific feature and does not come from the `
|
|
189
|
+
* This is a Kysely specific feature and does not come from the `mysql2` module.
|
|
190
190
|
*/
|
|
191
191
|
onCreateConnection?: (connection: DatabaseConnection) => Promise<void>;
|
|
192
192
|
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CompiledQuery } from '../../query-compiler/compiled-query.js';
|
|
2
|
+
import { importNamedFunction } from '../../util/dynamic-import.js';
|
|
3
|
+
import { isObject } from '../../util/object-utils.js';
|
|
2
4
|
const PRIVATE_RELEASE_METHOD = Symbol();
|
|
3
5
|
export class MysqlDriver {
|
|
4
6
|
#config;
|
|
@@ -47,19 +49,16 @@ export class MysqlDriver {
|
|
|
47
49
|
}
|
|
48
50
|
async beginTransaction(connection, settings) {
|
|
49
51
|
if (settings.isolationLevel) {
|
|
50
|
-
// On
|
|
51
|
-
await connection.executeQuery({
|
|
52
|
-
sql: `set transaction isolation level ${settings.isolationLevel}`,
|
|
53
|
-
parameters: [],
|
|
54
|
-
});
|
|
52
|
+
// On MySQL this sets the isolation level of the next transaction.
|
|
53
|
+
await connection.executeQuery(CompiledQuery.raw(`set transaction isolation level ${settings.isolationLevel}`));
|
|
55
54
|
}
|
|
56
|
-
await connection.executeQuery(
|
|
55
|
+
await connection.executeQuery(CompiledQuery.raw('begin'));
|
|
57
56
|
}
|
|
58
57
|
async commitTransaction(connection) {
|
|
59
|
-
await connection.executeQuery(
|
|
58
|
+
await connection.executeQuery(CompiledQuery.raw('commit'));
|
|
60
59
|
}
|
|
61
60
|
async rollbackTransaction(connection) {
|
|
62
|
-
await connection.executeQuery(
|
|
61
|
+
await connection.executeQuery(CompiledQuery.raw('rollback'));
|
|
63
62
|
}
|
|
64
63
|
async releaseConnection(connection) {
|
|
65
64
|
const mysqlConnection = connection;
|
|
@@ -80,19 +79,10 @@ export class MysqlDriver {
|
|
|
80
79
|
}
|
|
81
80
|
async function importMysqlPoolFactory() {
|
|
82
81
|
try {
|
|
83
|
-
|
|
84
|
-
// this hacky crap here.
|
|
85
|
-
const mysql = (await import('mysql2'));
|
|
86
|
-
if (isFunction(mysql.createPool)) {
|
|
87
|
-
return mysql.createPool;
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
// With esm the imported module doesn't match the typings.
|
|
91
|
-
return mysql.default.createPool;
|
|
92
|
-
}
|
|
82
|
+
return await importNamedFunction('mysql2', 'createPool');
|
|
93
83
|
}
|
|
94
84
|
catch (error) {
|
|
95
|
-
throw new Error('
|
|
85
|
+
throw new Error('MySQL client not installed. Please run `npm install mysql2`');
|
|
96
86
|
}
|
|
97
87
|
}
|
|
98
88
|
class MysqlConnection {
|
|
@@ -102,19 +92,19 @@ class MysqlConnection {
|
|
|
102
92
|
}
|
|
103
93
|
async executeQuery(compiledQuery) {
|
|
104
94
|
const result = await this.#executeQuery(compiledQuery);
|
|
105
|
-
if (
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
95
|
+
if (isOkPacket(result)) {
|
|
96
|
+
const { insertId, affectedRows } = result;
|
|
97
|
+
return {
|
|
98
|
+
insertId: insertId !== undefined &&
|
|
99
|
+
insertId !== null &&
|
|
100
|
+
insertId.toString() !== '0'
|
|
101
|
+
? BigInt(insertId)
|
|
102
|
+
: undefined,
|
|
103
|
+
numUpdatedOrDeletedRows: affectedRows !== undefined && insertId !== null
|
|
104
|
+
? BigInt(affectedRows)
|
|
105
|
+
: undefined,
|
|
106
|
+
rows: [],
|
|
107
|
+
};
|
|
118
108
|
}
|
|
119
109
|
else if (Array.isArray(result)) {
|
|
120
110
|
return {
|
|
@@ -141,3 +131,6 @@ class MysqlConnection {
|
|
|
141
131
|
this.#rawConnection.release();
|
|
142
132
|
}
|
|
143
133
|
}
|
|
134
|
+
function isOkPacket(obj) {
|
|
135
|
+
return isObject(obj) && 'insertId' in obj && 'affectedRows' in obj;
|
|
136
|
+
}
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
|
|
2
2
|
export declare class MysqlQueryCompiler extends DefaultQueryCompiler {
|
|
3
|
-
protected visitReturning(): void;
|
|
4
|
-
protected getSql(): string;
|
|
5
3
|
protected getCurrentParameterPlaceholder(): string;
|
|
6
4
|
protected getLeftIdentifierWrapper(): string;
|
|
7
5
|
protected getRightIdentifierWrapper(): string;
|
|
@@ -1,11 +1,5 @@
|
|
|
1
1
|
import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
|
|
2
2
|
export class MysqlQueryCompiler extends DefaultQueryCompiler {
|
|
3
|
-
visitReturning() {
|
|
4
|
-
// Do nothing.
|
|
5
|
-
}
|
|
6
|
-
getSql() {
|
|
7
|
-
return super.getSql().trimEnd();
|
|
8
|
-
}
|
|
9
3
|
getCurrentParameterPlaceholder() {
|
|
10
4
|
return '?';
|
|
11
5
|
}
|
|
@@ -27,7 +27,7 @@ export declare class PostgresDialect implements Dialect {
|
|
|
27
27
|
createIntrospector(db: Kysely<any>): DatabaseIntrospector;
|
|
28
28
|
}
|
|
29
29
|
/**
|
|
30
|
-
* Config for the
|
|
30
|
+
* Config for the PostgreSQL dialect.
|
|
31
31
|
*
|
|
32
32
|
* This interface is equal to `pg` library's `Pool` config:
|
|
33
33
|
*
|
|
@@ -91,7 +91,7 @@ export interface PostgresDialectConfig {
|
|
|
91
91
|
*
|
|
92
92
|
* Setting `allowExitOnIdle: true` in the config will allow the node event loop to exit
|
|
93
93
|
* as soon as all clients in the pool are idle, even if their socket is still open
|
|
94
|
-
* to the
|
|
94
|
+
* to the PostgreSQL server. This can be handy in scripts & tests
|
|
95
95
|
* where you don't want to wait for your clients to go idle before your process exits.
|
|
96
96
|
*/
|
|
97
97
|
allowExitOnIdle?: boolean;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CompiledQuery } from '../../query-compiler/compiled-query.js';
|
|
2
|
+
import { importNamedFunction } from '../../util/dynamic-import.js';
|
|
2
3
|
const PRIVATE_RELEASE_METHOD = Symbol();
|
|
3
4
|
export class PostgresDriver {
|
|
4
5
|
#config;
|
|
@@ -35,20 +36,17 @@ export class PostgresDriver {
|
|
|
35
36
|
}
|
|
36
37
|
async beginTransaction(connection, settings) {
|
|
37
38
|
if (settings.isolationLevel) {
|
|
38
|
-
await connection.executeQuery({
|
|
39
|
-
sql: `start transaction isolation level ${settings.isolationLevel}`,
|
|
40
|
-
parameters: [],
|
|
41
|
-
});
|
|
39
|
+
await connection.executeQuery(CompiledQuery.raw(`start transaction isolation level ${settings.isolationLevel}`));
|
|
42
40
|
}
|
|
43
41
|
else {
|
|
44
|
-
await connection.executeQuery(
|
|
42
|
+
await connection.executeQuery(CompiledQuery.raw('begin'));
|
|
45
43
|
}
|
|
46
44
|
}
|
|
47
45
|
async commitTransaction(connection) {
|
|
48
|
-
await connection.executeQuery(
|
|
46
|
+
await connection.executeQuery(CompiledQuery.raw('commit'));
|
|
49
47
|
}
|
|
50
48
|
async rollbackTransaction(connection) {
|
|
51
|
-
await connection.executeQuery(
|
|
49
|
+
await connection.executeQuery(CompiledQuery.raw('rollback'));
|
|
52
50
|
}
|
|
53
51
|
async releaseConnection(connection) {
|
|
54
52
|
const pgConnection = connection;
|
|
@@ -64,16 +62,7 @@ export class PostgresDriver {
|
|
|
64
62
|
}
|
|
65
63
|
async function importPgPool() {
|
|
66
64
|
try {
|
|
67
|
-
|
|
68
|
-
// this hacky crap here.
|
|
69
|
-
const pg = (await import('pg'));
|
|
70
|
-
if (isFunction(pg.Pool)) {
|
|
71
|
-
return pg.Pool;
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
// With esm the imported module doesn't match the typings.
|
|
75
|
-
return pg.default.Pool;
|
|
76
|
-
}
|
|
65
|
+
return await importNamedFunction('pg', 'Pool');
|
|
77
66
|
}
|
|
78
67
|
catch (error) {
|
|
79
68
|
throw new Error('Postgres client not installed. Please run `npm install pg`');
|
|
@@ -90,7 +79,7 @@ class PostgresConnection {
|
|
|
90
79
|
]);
|
|
91
80
|
if (result.command === 'UPDATE' || result.command === 'DELETE') {
|
|
92
81
|
return {
|
|
93
|
-
numUpdatedOrDeletedRows: result.rowCount,
|
|
82
|
+
numUpdatedOrDeletedRows: BigInt(result.rowCount),
|
|
94
83
|
rows: result.rows ?? [],
|
|
95
84
|
};
|
|
96
85
|
}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
|
|
2
2
|
export declare class PostgresQueryCompiler extends DefaultQueryCompiler {
|
|
3
|
+
protected getCurrentParameterPlaceholder(): string;
|
|
4
|
+
protected getLeftIdentifierWrapper(): string;
|
|
5
|
+
protected getRightIdentifierWrapper(): string;
|
|
3
6
|
}
|
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
|
|
2
|
-
// The default query compiler is for PostgreSQL.
|
|
3
2
|
export class PostgresQueryCompiler extends DefaultQueryCompiler {
|
|
3
|
+
getCurrentParameterPlaceholder() {
|
|
4
|
+
return '$' + this.numParameters;
|
|
5
|
+
}
|
|
6
|
+
getLeftIdentifierWrapper() {
|
|
7
|
+
return '"';
|
|
8
|
+
}
|
|
9
|
+
getRightIdentifierWrapper() {
|
|
10
|
+
return '"';
|
|
11
|
+
}
|
|
4
12
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { DialectAdapter } from '../dialect-adapter.js';
|
|
2
|
+
export declare class SqliteAdapter implements DialectAdapter {
|
|
3
|
+
get supportsTransactionalDdl(): boolean;
|
|
4
|
+
get supportsReturning(): boolean;
|
|
5
|
+
acquireMigrationLock(): Promise<void>;
|
|
6
|
+
releaseMigrationLock(): Promise<void>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export class SqliteAdapter {
|
|
2
|
+
get supportsTransactionalDdl() {
|
|
3
|
+
return false;
|
|
4
|
+
}
|
|
5
|
+
get supportsReturning() {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
async acquireMigrationLock() {
|
|
9
|
+
// Nothing to do here.
|
|
10
|
+
}
|
|
11
|
+
async releaseMigrationLock() {
|
|
12
|
+
// Nothing to do here.
|
|
13
|
+
}
|
|
14
|
+
}
|