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
package/README.md
CHANGED
|
@@ -39,14 +39,17 @@ using Kysely and can't find something you'd want to use, please open an issue or
|
|
|
39
39
|
|
|
40
40
|
# Installation
|
|
41
41
|
|
|
42
|
-
Kysely currently works on PostgreSQL and
|
|
42
|
+
Kysely currently works on PostgreSQL, MySQL and SQLite. You can install it using
|
|
43
43
|
|
|
44
44
|
```
|
|
45
|
-
#
|
|
45
|
+
# PostgreSQL
|
|
46
46
|
npm install kysely pg
|
|
47
47
|
|
|
48
|
-
#
|
|
48
|
+
# MySQL
|
|
49
49
|
npm install kysely mysql2
|
|
50
|
+
|
|
51
|
+
# SQLite
|
|
52
|
+
npm install kysely better-sqlite3
|
|
50
53
|
```
|
|
51
54
|
|
|
52
55
|
More dialects will be added soon. Kysely also has a simple interface
|
|
@@ -94,6 +97,7 @@ interface Database {
|
|
|
94
97
|
|
|
95
98
|
// You'd create one of these when you start your app.
|
|
96
99
|
const db = new Kysely<Database>({
|
|
100
|
+
// Use MysqlDialect for MySQL and SqliteDialect for SQLite.
|
|
97
101
|
dialect: new PostgresDialect({
|
|
98
102
|
host: 'localhost',
|
|
99
103
|
database: 'kysely_test',
|
|
@@ -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
|
}
|
|
@@ -16,7 +16,7 @@ class MysqlAdapter {
|
|
|
16
16
|
// released when the connection is destroyed (session ends) or when the lock
|
|
17
17
|
// is released using `release_lock`. This way we know that the lock is either
|
|
18
18
|
// released by us after successfull or failed migrations OR it's released by
|
|
19
|
-
//
|
|
19
|
+
// MySQL if the process gets killed for some reason.
|
|
20
20
|
await db
|
|
21
21
|
.raw(`select get_lock('${LOCK_ID}', ${LOCK_TIMEOUT_SECONDS})`)
|
|
22
22
|
.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,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MysqlDriver = void 0;
|
|
4
|
+
const compiled_query_js_1 = require("../../query-compiler/compiled-query.js");
|
|
5
|
+
const dynamic_import_js_1 = require("../../util/dynamic-import.js");
|
|
4
6
|
const object_utils_js_1 = require("../../util/object-utils.js");
|
|
5
7
|
const PRIVATE_RELEASE_METHOD = Symbol();
|
|
6
8
|
class MysqlDriver {
|
|
@@ -50,19 +52,16 @@ class MysqlDriver {
|
|
|
50
52
|
}
|
|
51
53
|
async beginTransaction(connection, settings) {
|
|
52
54
|
if (settings.isolationLevel) {
|
|
53
|
-
// On
|
|
54
|
-
await connection.executeQuery({
|
|
55
|
-
sql: `set transaction isolation level ${settings.isolationLevel}`,
|
|
56
|
-
parameters: [],
|
|
57
|
-
});
|
|
55
|
+
// On MySQL this sets the isolation level of the next transaction.
|
|
56
|
+
await connection.executeQuery(compiled_query_js_1.CompiledQuery.raw(`set transaction isolation level ${settings.isolationLevel}`));
|
|
58
57
|
}
|
|
59
|
-
await connection.executeQuery(
|
|
58
|
+
await connection.executeQuery(compiled_query_js_1.CompiledQuery.raw('begin'));
|
|
60
59
|
}
|
|
61
60
|
async commitTransaction(connection) {
|
|
62
|
-
await connection.executeQuery(
|
|
61
|
+
await connection.executeQuery(compiled_query_js_1.CompiledQuery.raw('commit'));
|
|
63
62
|
}
|
|
64
63
|
async rollbackTransaction(connection) {
|
|
65
|
-
await connection.executeQuery(
|
|
64
|
+
await connection.executeQuery(compiled_query_js_1.CompiledQuery.raw('rollback'));
|
|
66
65
|
}
|
|
67
66
|
async releaseConnection(connection) {
|
|
68
67
|
const mysqlConnection = connection;
|
|
@@ -84,19 +83,10 @@ class MysqlDriver {
|
|
|
84
83
|
exports.MysqlDriver = MysqlDriver;
|
|
85
84
|
async function importMysqlPoolFactory() {
|
|
86
85
|
try {
|
|
87
|
-
|
|
88
|
-
// this hacky crap here.
|
|
89
|
-
const mysql = (await Promise.resolve().then(() => require('mysql2')));
|
|
90
|
-
if ((0, object_utils_js_1.isFunction)(mysql.createPool)) {
|
|
91
|
-
return mysql.createPool;
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
// With esm the imported module doesn't match the typings.
|
|
95
|
-
return mysql.default.createPool;
|
|
96
|
-
}
|
|
86
|
+
return await (0, dynamic_import_js_1.importNamedFunction)('mysql2', 'createPool');
|
|
97
87
|
}
|
|
98
88
|
catch (error) {
|
|
99
|
-
throw new Error('
|
|
89
|
+
throw new Error('MySQL client not installed. Please run `npm install mysql2`');
|
|
100
90
|
}
|
|
101
91
|
}
|
|
102
92
|
class MysqlConnection {
|
|
@@ -106,19 +96,19 @@ class MysqlConnection {
|
|
|
106
96
|
}
|
|
107
97
|
async executeQuery(compiledQuery) {
|
|
108
98
|
const result = await this.#executeQuery(compiledQuery);
|
|
109
|
-
if ((
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
99
|
+
if (isOkPacket(result)) {
|
|
100
|
+
const { insertId, affectedRows } = result;
|
|
101
|
+
return {
|
|
102
|
+
insertId: insertId !== undefined &&
|
|
103
|
+
insertId !== null &&
|
|
104
|
+
insertId.toString() !== '0'
|
|
105
|
+
? BigInt(insertId)
|
|
106
|
+
: undefined,
|
|
107
|
+
numUpdatedOrDeletedRows: affectedRows !== undefined && insertId !== null
|
|
108
|
+
? BigInt(affectedRows)
|
|
109
|
+
: undefined,
|
|
110
|
+
rows: [],
|
|
111
|
+
};
|
|
122
112
|
}
|
|
123
113
|
else if (Array.isArray(result)) {
|
|
124
114
|
return {
|
|
@@ -145,3 +135,6 @@ class MysqlConnection {
|
|
|
145
135
|
this.#rawConnection.release();
|
|
146
136
|
}
|
|
147
137
|
}
|
|
138
|
+
function isOkPacket(obj) {
|
|
139
|
+
return (0, object_utils_js_1.isObject)(obj) && 'insertId' in obj && 'affectedRows' in obj;
|
|
140
|
+
}
|
|
@@ -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;
|
|
@@ -3,12 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.MysqlQueryCompiler = void 0;
|
|
4
4
|
const default_query_compiler_js_1 = require("../../query-compiler/default-query-compiler.js");
|
|
5
5
|
class MysqlQueryCompiler extends default_query_compiler_js_1.DefaultQueryCompiler {
|
|
6
|
-
visitReturning() {
|
|
7
|
-
// Do nothing.
|
|
8
|
-
}
|
|
9
|
-
getSql() {
|
|
10
|
-
return super.getSql().trimEnd();
|
|
11
|
-
}
|
|
12
6
|
getCurrentParameterPlaceholder() {
|
|
13
7
|
return '?';
|
|
14
8
|
}
|
|
@@ -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,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PostgresDriver = void 0;
|
|
4
|
-
const
|
|
4
|
+
const compiled_query_js_1 = require("../../query-compiler/compiled-query.js");
|
|
5
|
+
const dynamic_import_js_1 = require("../../util/dynamic-import.js");
|
|
5
6
|
const PRIVATE_RELEASE_METHOD = Symbol();
|
|
6
7
|
class PostgresDriver {
|
|
7
8
|
#config;
|
|
@@ -38,20 +39,17 @@ class PostgresDriver {
|
|
|
38
39
|
}
|
|
39
40
|
async beginTransaction(connection, settings) {
|
|
40
41
|
if (settings.isolationLevel) {
|
|
41
|
-
await connection.executeQuery({
|
|
42
|
-
sql: `start transaction isolation level ${settings.isolationLevel}`,
|
|
43
|
-
parameters: [],
|
|
44
|
-
});
|
|
42
|
+
await connection.executeQuery(compiled_query_js_1.CompiledQuery.raw(`start transaction isolation level ${settings.isolationLevel}`));
|
|
45
43
|
}
|
|
46
44
|
else {
|
|
47
|
-
await connection.executeQuery(
|
|
45
|
+
await connection.executeQuery(compiled_query_js_1.CompiledQuery.raw('begin'));
|
|
48
46
|
}
|
|
49
47
|
}
|
|
50
48
|
async commitTransaction(connection) {
|
|
51
|
-
await connection.executeQuery(
|
|
49
|
+
await connection.executeQuery(compiled_query_js_1.CompiledQuery.raw('commit'));
|
|
52
50
|
}
|
|
53
51
|
async rollbackTransaction(connection) {
|
|
54
|
-
await connection.executeQuery(
|
|
52
|
+
await connection.executeQuery(compiled_query_js_1.CompiledQuery.raw('rollback'));
|
|
55
53
|
}
|
|
56
54
|
async releaseConnection(connection) {
|
|
57
55
|
const pgConnection = connection;
|
|
@@ -68,16 +66,7 @@ class PostgresDriver {
|
|
|
68
66
|
exports.PostgresDriver = PostgresDriver;
|
|
69
67
|
async function importPgPool() {
|
|
70
68
|
try {
|
|
71
|
-
|
|
72
|
-
// this hacky crap here.
|
|
73
|
-
const pg = (await Promise.resolve().then(() => require('pg')));
|
|
74
|
-
if ((0, object_utils_js_1.isFunction)(pg.Pool)) {
|
|
75
|
-
return pg.Pool;
|
|
76
|
-
}
|
|
77
|
-
else {
|
|
78
|
-
// With esm the imported module doesn't match the typings.
|
|
79
|
-
return pg.default.Pool;
|
|
80
|
-
}
|
|
69
|
+
return await (0, dynamic_import_js_1.importNamedFunction)('pg', 'Pool');
|
|
81
70
|
}
|
|
82
71
|
catch (error) {
|
|
83
72
|
throw new Error('Postgres client not installed. Please run `npm install pg`');
|
|
@@ -94,7 +83,7 @@ class PostgresConnection {
|
|
|
94
83
|
]);
|
|
95
84
|
if (result.command === 'UPDATE' || result.command === 'DELETE') {
|
|
96
85
|
return {
|
|
97
|
-
numUpdatedOrDeletedRows: result.rowCount,
|
|
86
|
+
numUpdatedOrDeletedRows: BigInt(result.rowCount),
|
|
98
87
|
rows: result.rows ?? [],
|
|
99
88
|
};
|
|
100
89
|
}
|
|
@@ -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
|
}
|
|
@@ -2,7 +2,15 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PostgresQueryCompiler = void 0;
|
|
4
4
|
const default_query_compiler_js_1 = require("../../query-compiler/default-query-compiler.js");
|
|
5
|
-
// The default query compiler is for PostgreSQL.
|
|
6
5
|
class PostgresQueryCompiler extends default_query_compiler_js_1.DefaultQueryCompiler {
|
|
6
|
+
getCurrentParameterPlaceholder() {
|
|
7
|
+
return '$' + this.numParameters;
|
|
8
|
+
}
|
|
9
|
+
getLeftIdentifierWrapper() {
|
|
10
|
+
return '"';
|
|
11
|
+
}
|
|
12
|
+
getRightIdentifierWrapper() {
|
|
13
|
+
return '"';
|
|
14
|
+
}
|
|
7
15
|
}
|
|
8
16
|
exports.PostgresQueryCompiler = PostgresQueryCompiler;
|
|
@@ -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,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SqliteAdapter = void 0;
|
|
4
|
+
class SqliteAdapter {
|
|
5
|
+
get supportsTransactionalDdl() {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
get supportsReturning() {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
async acquireMigrationLock() {
|
|
12
|
+
// Nothing to do here.
|
|
13
|
+
}
|
|
14
|
+
async releaseMigrationLock() {
|
|
15
|
+
// Nothing to do here.
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.SqliteAdapter = SqliteAdapter;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Driver } from '../../driver/driver.js';
|
|
2
|
+
import { Kysely } from '../../kysely.js';
|
|
3
|
+
import { QueryCompiler } from '../../query-compiler/query-compiler.js';
|
|
4
|
+
import { Dialect } from '../dialect.js';
|
|
5
|
+
import { DatabaseIntrospector } from '../../introspection/database-introspector.js';
|
|
6
|
+
import { DialectAdapter } from '../dialect-adapter.js';
|
|
7
|
+
import { DatabaseConnection } from '../../driver/database-connection.js';
|
|
8
|
+
/**
|
|
9
|
+
* SQLite dialect that uses the [better-sqlite3](https://github.com/JoshuaWise/better-sqlite3) library.
|
|
10
|
+
*
|
|
11
|
+
* The {@link SqliteDialectConfig | configuration} is used to construct an instance of
|
|
12
|
+
* [Database](https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#new-databasepath-options)
|
|
13
|
+
* class. The configuration values are passed directly to the `Database` constructor.
|
|
14
|
+
*/
|
|
15
|
+
export declare class SqliteDialect implements Dialect {
|
|
16
|
+
#private;
|
|
17
|
+
constructor(config: SqliteDialectConfig);
|
|
18
|
+
createDriver(): Driver;
|
|
19
|
+
createQueryCompiler(): QueryCompiler;
|
|
20
|
+
createAdapter(): DialectAdapter;
|
|
21
|
+
createIntrospector(db: Kysely<any>): DatabaseIntrospector;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Config for the SQLite dialect.
|
|
25
|
+
*
|
|
26
|
+
* This interface is equal to `better-sqlite3` library's `Databse` class's constructor parameters.
|
|
27
|
+
*
|
|
28
|
+
* https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#new-databasepath-options
|
|
29
|
+
*/
|
|
30
|
+
export interface SqliteDialectConfig {
|
|
31
|
+
/**
|
|
32
|
+
* The database file path.
|
|
33
|
+
*
|
|
34
|
+
* This is passed as the first argument for the [Database](https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#new-databasepath-options)
|
|
35
|
+
* class constructor.
|
|
36
|
+
*/
|
|
37
|
+
databasePath: string;
|
|
38
|
+
/**
|
|
39
|
+
* Open the database connection in readonly mode (default: false)
|
|
40
|
+
*/
|
|
41
|
+
readonly?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* If the database does not exist, an Error will be thrown instead of creating a new file.
|
|
44
|
+
* This option is ignored for in-memory, temporary, or readonly database connections
|
|
45
|
+
* (default: false)
|
|
46
|
+
*/
|
|
47
|
+
fileMustExist?: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* The number of milliseconds to wait when executing queries on a locked database,
|
|
50
|
+
* before throwing a SQLITE_BUSY error (default: 5000)
|
|
51
|
+
*/
|
|
52
|
+
timeout?: number;
|
|
53
|
+
/**
|
|
54
|
+
* Provide a function that gets called with every SQL string executed by the database connection
|
|
55
|
+
* (default: null)
|
|
56
|
+
*/
|
|
57
|
+
verbose?: (sql: string) => void;
|
|
58
|
+
/**
|
|
59
|
+
* Called once when the first query is executed.
|
|
60
|
+
*
|
|
61
|
+
* This is a Kysely specific feature and does not come from the `better-sqlite3` module.
|
|
62
|
+
*/
|
|
63
|
+
onCreateConnection?: (connection: DatabaseConnection) => Promise<void>;
|
|
64
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SqliteDialect = void 0;
|
|
4
|
+
const sqlite_driver_js_1 = require("./sqlite-driver.js");
|
|
5
|
+
const sqlite_query_compiler_js_1 = require("./sqlite-query-compiler.js");
|
|
6
|
+
const sqlite_introspector_js_1 = require("./sqlite-introspector.js");
|
|
7
|
+
const sqlite_adapter_js_1 = require("./sqlite-adapter.js");
|
|
8
|
+
/**
|
|
9
|
+
* SQLite dialect that uses the [better-sqlite3](https://github.com/JoshuaWise/better-sqlite3) library.
|
|
10
|
+
*
|
|
11
|
+
* The {@link SqliteDialectConfig | configuration} is used to construct an instance of
|
|
12
|
+
* [Database](https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#new-databasepath-options)
|
|
13
|
+
* class. The configuration values are passed directly to the `Database` constructor.
|
|
14
|
+
*/
|
|
15
|
+
class SqliteDialect {
|
|
16
|
+
#config;
|
|
17
|
+
constructor(config) {
|
|
18
|
+
this.#config = config;
|
|
19
|
+
}
|
|
20
|
+
createDriver() {
|
|
21
|
+
return new sqlite_driver_js_1.SqliteDriver(this.#config);
|
|
22
|
+
}
|
|
23
|
+
createQueryCompiler() {
|
|
24
|
+
return new sqlite_query_compiler_js_1.SqliteQueryCompiler();
|
|
25
|
+
}
|
|
26
|
+
createAdapter() {
|
|
27
|
+
return new sqlite_adapter_js_1.SqliteAdapter();
|
|
28
|
+
}
|
|
29
|
+
createIntrospector(db) {
|
|
30
|
+
return new sqlite_introspector_js_1.SqliteIntrospector(db);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.SqliteDialect = SqliteDialect;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { DatabaseConnection } from '../../driver/database-connection.js';
|
|
2
|
+
import { Driver } from '../../driver/driver.js';
|
|
3
|
+
import { SqliteDialectConfig } from './sqlite-dialect.js';
|
|
4
|
+
export declare class SqliteDriver implements Driver {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(config: SqliteDialectConfig);
|
|
7
|
+
init(): Promise<void>;
|
|
8
|
+
acquireConnection(): Promise<DatabaseConnection>;
|
|
9
|
+
beginTransaction(connection: DatabaseConnection): Promise<void>;
|
|
10
|
+
commitTransaction(connection: DatabaseConnection): Promise<void>;
|
|
11
|
+
rollbackTransaction(connection: DatabaseConnection): Promise<void>;
|
|
12
|
+
releaseConnection(): Promise<void>;
|
|
13
|
+
destroy(): Promise<void>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SqliteDriver = void 0;
|
|
4
|
+
const compiled_query_js_1 = require("../../query-compiler/compiled-query.js");
|
|
5
|
+
const dynamic_import_js_1 = require("../../util/dynamic-import.js");
|
|
6
|
+
const object_utils_js_1 = require("../../util/object-utils.js");
|
|
7
|
+
class SqliteDriver {
|
|
8
|
+
#config;
|
|
9
|
+
#connectionMutex = new ConnectionMutex();
|
|
10
|
+
#db;
|
|
11
|
+
#connection;
|
|
12
|
+
constructor(config) {
|
|
13
|
+
this.#config = config;
|
|
14
|
+
}
|
|
15
|
+
async init() {
|
|
16
|
+
// Import the `better-sqlite3` module here instead at the top of the file
|
|
17
|
+
// so that this file can be loaded by node without `better-sqlite3` driver
|
|
18
|
+
// installed. As you can see, there IS an import from `better-sqlite3` at
|
|
19
|
+
// the top level too, but that's only for types. It doesn't get compiled
|
|
20
|
+
// into javascript. You can check the built javascript code.
|
|
21
|
+
const DatabaseConstructor = await importBetterSqlite3Database();
|
|
22
|
+
const options = {};
|
|
23
|
+
if ((0, object_utils_js_1.isBoolean)(this.#config.readonly)) {
|
|
24
|
+
options.readonly = this.#config.readonly;
|
|
25
|
+
}
|
|
26
|
+
if ((0, object_utils_js_1.isBoolean)(this.#config.fileMustExist)) {
|
|
27
|
+
options.fileMustExist = this.#config.fileMustExist;
|
|
28
|
+
}
|
|
29
|
+
if ((0, object_utils_js_1.isNumber)(this.#config.timeout)) {
|
|
30
|
+
options.timeout = this.#config.timeout;
|
|
31
|
+
}
|
|
32
|
+
if ((0, object_utils_js_1.isFunction)(this.#config.verbose)) {
|
|
33
|
+
options.verbose = this.#config.verbose;
|
|
34
|
+
}
|
|
35
|
+
this.#db = new DatabaseConstructor(this.#config.databasePath, options);
|
|
36
|
+
this.#connection = new SqliteConnection(this.#db);
|
|
37
|
+
if (this.#config.onCreateConnection) {
|
|
38
|
+
await this.#config.onCreateConnection(this.#connection);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
async acquireConnection() {
|
|
42
|
+
// SQLite only has one single connection. We use a mutex here to wait
|
|
43
|
+
// until the single connection has been released.
|
|
44
|
+
await this.#connectionMutex.lock();
|
|
45
|
+
return this.#connection;
|
|
46
|
+
}
|
|
47
|
+
async beginTransaction(connection) {
|
|
48
|
+
await connection.executeQuery(compiled_query_js_1.CompiledQuery.raw('begin'));
|
|
49
|
+
}
|
|
50
|
+
async commitTransaction(connection) {
|
|
51
|
+
await connection.executeQuery(compiled_query_js_1.CompiledQuery.raw('commit'));
|
|
52
|
+
}
|
|
53
|
+
async rollbackTransaction(connection) {
|
|
54
|
+
await connection.executeQuery(compiled_query_js_1.CompiledQuery.raw('rollback'));
|
|
55
|
+
}
|
|
56
|
+
async releaseConnection() {
|
|
57
|
+
this.#connectionMutex.unlock();
|
|
58
|
+
}
|
|
59
|
+
async destroy() {
|
|
60
|
+
this.#db?.close();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
exports.SqliteDriver = SqliteDriver;
|
|
64
|
+
async function importBetterSqlite3Database() {
|
|
65
|
+
try {
|
|
66
|
+
return await (0, dynamic_import_js_1.importFunction)('better-sqlite3');
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
throw new Error('SQLite client not installed. Please run `npm install better-sqlite3`');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
class SqliteConnection {
|
|
73
|
+
#db;
|
|
74
|
+
constructor(db) {
|
|
75
|
+
this.#db = db;
|
|
76
|
+
}
|
|
77
|
+
executeQuery(compiledQuery) {
|
|
78
|
+
const { sql, parameters } = compiledQuery;
|
|
79
|
+
const stmt = this.#db.prepare(sql);
|
|
80
|
+
if (stmt.reader) {
|
|
81
|
+
return Promise.resolve({
|
|
82
|
+
rows: stmt.all(parameters),
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
const { changes, lastInsertRowid } = stmt.run(parameters);
|
|
87
|
+
return Promise.resolve({
|
|
88
|
+
numUpdatedOrDeletedRows: changes !== undefined && changes !== null
|
|
89
|
+
? BigInt(changes)
|
|
90
|
+
: undefined,
|
|
91
|
+
insertId: lastInsertRowid !== undefined && lastInsertRowid !== null
|
|
92
|
+
? BigInt(lastInsertRowid)
|
|
93
|
+
: undefined,
|
|
94
|
+
rows: [],
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
class ConnectionMutex {
|
|
100
|
+
#promise;
|
|
101
|
+
#resolve;
|
|
102
|
+
async lock() {
|
|
103
|
+
while (this.#promise) {
|
|
104
|
+
await this.#promise;
|
|
105
|
+
}
|
|
106
|
+
this.#promise = new Promise((resolve) => {
|
|
107
|
+
this.#resolve = resolve;
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
unlock() {
|
|
111
|
+
const resolve = this.#resolve;
|
|
112
|
+
this.#promise = undefined;
|
|
113
|
+
this.#resolve = undefined;
|
|
114
|
+
resolve?.();
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { DatabaseIntrospector, DatabaseMetadata, DatabaseMetadataOptions } from '../../introspection/database-introspector.js';
|
|
2
|
+
import { Kysely } from '../../kysely.js';
|
|
3
|
+
export declare class SqliteIntrospector implements DatabaseIntrospector {
|
|
4
|
+
#private;
|
|
5
|
+
constructor(db: Kysely<any>);
|
|
6
|
+
getMetadata(options?: DatabaseMetadataOptions): Promise<DatabaseMetadata>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SqliteIntrospector = void 0;
|
|
4
|
+
const migration_js_1 = require("../../migration/migration.js");
|
|
5
|
+
class SqliteIntrospector {
|
|
6
|
+
#db;
|
|
7
|
+
constructor(db) {
|
|
8
|
+
this.#db = db;
|
|
9
|
+
}
|
|
10
|
+
async getMetadata(options = { withInternalKyselyTables: false }) {
|
|
11
|
+
let query = this.#db
|
|
12
|
+
.selectFrom('sqlite_schema')
|
|
13
|
+
.where('type', '=', 'table')
|
|
14
|
+
.where('name', 'not like', 'sqlite_%')
|
|
15
|
+
.select('name')
|
|
16
|
+
.castTo();
|
|
17
|
+
if (!options.withInternalKyselyTables) {
|
|
18
|
+
query = query
|
|
19
|
+
.where('name', '!=', migration_js_1.MIGRATION_TABLE)
|
|
20
|
+
.where('name', '!=', migration_js_1.MIGRATION_LOCK_TABLE);
|
|
21
|
+
}
|
|
22
|
+
const tables = await query.execute();
|
|
23
|
+
return {
|
|
24
|
+
tables: await Promise.all(tables.map(({ name }) => this.#getTableMetadata(name))),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
async #getTableMetadata(table) {
|
|
28
|
+
const db = this.#db;
|
|
29
|
+
const columns = await db
|
|
30
|
+
.selectFrom(db.raw(`PRAGMA_TABLE_INFO(?)`, [table]).as('table_info'))
|
|
31
|
+
.select(['name', 'type', 'notnull'])
|
|
32
|
+
.castTo()
|
|
33
|
+
.execute();
|
|
34
|
+
return {
|
|
35
|
+
name: table,
|
|
36
|
+
columns: columns.map((col) => ({
|
|
37
|
+
name: col.name,
|
|
38
|
+
dataType: col.type,
|
|
39
|
+
isNullable: !col.notnull,
|
|
40
|
+
})),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.SqliteIntrospector = SqliteIntrospector;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { DefaultQueryCompiler } from '../../query-compiler/default-query-compiler.js';
|
|
2
|
+
export declare class SqliteQueryCompiler extends DefaultQueryCompiler {
|
|
3
|
+
protected getCurrentParameterPlaceholder(): string;
|
|
4
|
+
protected getLeftIdentifierWrapper(): string;
|
|
5
|
+
protected getRightIdentifierWrapper(): string;
|
|
6
|
+
protected getAutoIncrement(): string;
|
|
7
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SqliteQueryCompiler = void 0;
|
|
4
|
+
const default_query_compiler_js_1 = require("../../query-compiler/default-query-compiler.js");
|
|
5
|
+
class SqliteQueryCompiler extends default_query_compiler_js_1.DefaultQueryCompiler {
|
|
6
|
+
getCurrentParameterPlaceholder() {
|
|
7
|
+
return '?';
|
|
8
|
+
}
|
|
9
|
+
getLeftIdentifierWrapper() {
|
|
10
|
+
return '"';
|
|
11
|
+
}
|
|
12
|
+
getRightIdentifierWrapper() {
|
|
13
|
+
return '"';
|
|
14
|
+
}
|
|
15
|
+
getAutoIncrement() {
|
|
16
|
+
return 'autoincrement';
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.SqliteQueryCompiler = SqliteQueryCompiler;
|