kysely 0.28.17 → 0.28.19
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 +1 -1
- package/dist/cjs/dialect/mssql/mssql-adapter.d.ts +3 -2
- package/dist/cjs/dialect/mssql/mssql-adapter.js +13 -7
- package/dist/cjs/dialect/mssql/mssql-driver.d.ts +3 -0
- package/dist/cjs/dialect/mysql/mysql-dialect-config.d.ts +10 -0
- package/dist/cjs/dialect/mysql/mysql-driver.d.ts +3 -0
- package/dist/cjs/dialect/postgres/postgres-adapter.d.ts +1 -1
- package/dist/cjs/dialect/postgres/postgres-adapter.js +13 -7
- package/dist/cjs/dialect/postgres/postgres-dialect-config.d.ts +12 -2
- package/dist/cjs/dialect/postgres/postgres-driver.d.ts +6 -1
- package/dist/cjs/dialect/postgres/postgres-driver.js +35 -3
- package/dist/cjs/dialect/postgres/postgres-introspector.js +18 -12
- package/dist/cjs/dialect/sqlite/sqlite-dialect-config.d.ts +10 -0
- package/dist/cjs/dialect/sqlite/sqlite-driver.d.ts +3 -0
- package/dist/cjs/driver/aborted-transaction-error.d.ts +11 -0
- package/dist/cjs/driver/aborted-transaction-error.js +20 -0
- package/dist/cjs/driver/driver.d.ts +3 -0
- package/dist/cjs/driver/dummy-driver.d.ts +3 -0
- package/dist/cjs/driver/runtime-driver.d.ts +3 -0
- package/dist/cjs/helpers/mssql.d.ts +3 -1
- package/dist/cjs/helpers/mssql.js +4 -2
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/kysely.d.ts +55 -3
- package/dist/cjs/kysely.js +136 -54
- package/dist/cjs/migration/migrator.js +22 -19
- package/dist/cjs/query-builder/delete-query-builder.d.ts +2 -2
- package/dist/cjs/query-builder/join-builder.d.ts +2 -2
- package/dist/cjs/query-builder/on-conflict-builder.d.ts +5 -2
- package/dist/cjs/query-builder/select-query-builder.d.ts +28 -6
- package/dist/cjs/query-builder/update-query-builder.d.ts +1 -1
- package/dist/cjs/query-compiler/default-query-compiler.js +4 -4
- package/dist/cjs/schema/create-type-builder.d.ts +1 -1
- package/dist/cjs/schema/create-type-builder.js +1 -1
- package/dist/cjs/util/column-type.d.ts +1 -1
- package/dist/esm/dialect/mssql/mssql-adapter.d.ts +3 -2
- package/dist/esm/dialect/mssql/mssql-adapter.js +13 -7
- package/dist/esm/dialect/mssql/mssql-driver.d.ts +3 -0
- package/dist/esm/dialect/mysql/mysql-dialect-config.d.ts +10 -0
- package/dist/esm/dialect/mysql/mysql-driver.d.ts +3 -0
- package/dist/esm/dialect/postgres/postgres-adapter.d.ts +1 -1
- package/dist/esm/dialect/postgres/postgres-adapter.js +13 -7
- package/dist/esm/dialect/postgres/postgres-dialect-config.d.ts +12 -2
- package/dist/esm/dialect/postgres/postgres-driver.d.ts +6 -1
- package/dist/esm/dialect/postgres/postgres-driver.js +35 -3
- package/dist/esm/dialect/postgres/postgres-introspector.js +18 -12
- package/dist/esm/dialect/sqlite/sqlite-dialect-config.d.ts +10 -0
- package/dist/esm/dialect/sqlite/sqlite-driver.d.ts +3 -0
- package/dist/esm/driver/aborted-transaction-error.d.ts +11 -0
- package/dist/esm/driver/aborted-transaction-error.js +16 -0
- package/dist/esm/driver/driver.d.ts +3 -0
- package/dist/esm/driver/dummy-driver.d.ts +3 -0
- package/dist/esm/driver/runtime-driver.d.ts +3 -0
- package/dist/esm/helpers/mssql.d.ts +3 -1
- package/dist/esm/helpers/mssql.js +4 -2
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/kysely.d.ts +55 -3
- package/dist/esm/kysely.js +136 -54
- package/dist/esm/migration/migrator.js +22 -19
- package/dist/esm/query-builder/delete-query-builder.d.ts +2 -2
- package/dist/esm/query-builder/join-builder.d.ts +2 -2
- package/dist/esm/query-builder/on-conflict-builder.d.ts +5 -2
- package/dist/esm/query-builder/select-query-builder.d.ts +28 -6
- package/dist/esm/query-builder/update-query-builder.d.ts +1 -1
- package/dist/esm/query-compiler/default-query-compiler.js +4 -4
- package/dist/esm/schema/create-type-builder.d.ts +1 -1
- package/dist/esm/schema/create-type-builder.js +1 -1
- package/dist/esm/util/column-type.d.ts +1 -1
- package/package.json +36 -34
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
[](https://stand-with-ukraine.pp.ua)
|
|
2
2
|
|
|
3
3
|
[](https://github.com/kysely-org/kysely/releases/latest)
|
|
4
|
-
[](https://socket.dev/npm/package/kysely/overview/0.28.19)
|
|
5
5
|
[](https://github.com/kysely-org/kysely)
|
|
6
6
|
[](https://github.com/kysely-org/kysely/blob/master/LICENSE)
|
|
7
7
|
[](https://github.com/kysely-org/kysely/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc)
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Kysely } from '../../kysely.js';
|
|
2
2
|
import { DialectAdapterBase } from '../dialect-adapter-base.js';
|
|
3
|
+
import type { MigrationLockOptions } from '../dialect-adapter.js';
|
|
3
4
|
export declare class MssqlAdapter extends DialectAdapterBase {
|
|
4
5
|
/**
|
|
5
6
|
* Whether or not this dialect supports `if not exists` in creation of tables/schemas/views/etc.
|
|
@@ -70,7 +71,7 @@ export declare class MssqlAdapter extends DialectAdapterBase {
|
|
|
70
71
|
* `db` is a single connection (session) that will be used to execute the
|
|
71
72
|
* migrations.
|
|
72
73
|
*/
|
|
73
|
-
acquireMigrationLock(db: Kysely<any
|
|
74
|
+
acquireMigrationLock(db: Kysely<any>, _opt: MigrationLockOptions): Promise<void>;
|
|
74
75
|
/**
|
|
75
76
|
* Releases the migration lock. See {@link acquireMigrationLock}.
|
|
76
77
|
*
|
|
@@ -79,5 +80,5 @@ export declare class MssqlAdapter extends DialectAdapterBase {
|
|
|
79
80
|
* is a single connection (session) that was used to execute the migrations
|
|
80
81
|
* and the `acquireMigrationLock` call.
|
|
81
82
|
*/
|
|
82
|
-
releaseMigrationLock(): Promise<void>;
|
|
83
|
+
releaseMigrationLock(db: Kysely<any>, _opt: MigrationLockOptions): Promise<void>;
|
|
83
84
|
}
|
|
@@ -4,6 +4,7 @@ exports.MssqlAdapter = void 0;
|
|
|
4
4
|
const migrator_js_1 = require("../../migration/migrator.js");
|
|
5
5
|
const sql_js_1 = require("../../raw-builder/sql.js");
|
|
6
6
|
const dialect_adapter_base_js_1 = require("../dialect-adapter-base.js");
|
|
7
|
+
const LOCK_PRINCIPAL = 'dbo';
|
|
7
8
|
class MssqlAdapter extends dialect_adapter_base_js_1.DialectAdapterBase {
|
|
8
9
|
get supportsCreateIfNotExists() {
|
|
9
10
|
return false;
|
|
@@ -14,15 +15,20 @@ class MssqlAdapter extends dialect_adapter_base_js_1.DialectAdapterBase {
|
|
|
14
15
|
get supportsOutput() {
|
|
15
16
|
return true;
|
|
16
17
|
}
|
|
17
|
-
async acquireMigrationLock(db) {
|
|
18
|
-
// Acquire a
|
|
18
|
+
async acquireMigrationLock(db, _opt) {
|
|
19
|
+
// Acquire a session-level exclusive lock on the migrations table. A
|
|
20
|
+
// session-level lock (as opposed to the default transaction-level lock)
|
|
21
|
+
// stays held after the migration transaction commits and until we release
|
|
22
|
+
// it explicitly in `releaseMigrationLock` OR the session ends. This way we
|
|
23
|
+
// know the lock is either released by us after successful or failed
|
|
24
|
+
// migrations OR it's released by SQL Server if the connection dies.
|
|
19
25
|
// https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-getapplock-transact-sql?view=sql-server-ver16
|
|
20
|
-
await (0, sql_js_1.sql) `exec sp_getapplock @DbPrincipal = ${sql_js_1.sql.lit(
|
|
26
|
+
await (0, sql_js_1.sql) `exec sp_getapplock @DbPrincipal = ${sql_js_1.sql.lit(LOCK_PRINCIPAL)}, @Resource = ${sql_js_1.sql.lit(migrator_js_1.DEFAULT_MIGRATION_TABLE)}, @LockMode = ${sql_js_1.sql.lit('Exclusive')}, @LockOwner = ${sql_js_1.sql.lit('Session')}`.execute(db);
|
|
21
27
|
}
|
|
22
|
-
async releaseMigrationLock() {
|
|
23
|
-
//
|
|
24
|
-
//
|
|
25
|
-
|
|
28
|
+
async releaseMigrationLock(db, _opt) {
|
|
29
|
+
// Release the session-level lock acquired in `acquireMigrationLock`.
|
|
30
|
+
// https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-releaseapplock-transact-sql?view=sql-server-ver16
|
|
31
|
+
await (0, sql_js_1.sql) `exec sp_releaseapplock @DbPrincipal = ${sql_js_1.sql.lit(LOCK_PRINCIPAL)}, @Resource = ${sql_js_1.sql.lit(migrator_js_1.DEFAULT_MIGRATION_TABLE)}, @LockOwner = ${sql_js_1.sql.lit('Session')}`.execute(db);
|
|
26
32
|
}
|
|
27
33
|
}
|
|
28
34
|
exports.MssqlAdapter = MssqlAdapter;
|
|
@@ -25,6 +25,9 @@ export declare class MssqlDriver implements Driver {
|
|
|
25
25
|
beginTransaction(connection: MssqlConnection, settings: TransactionSettings): Promise<void>;
|
|
26
26
|
/**
|
|
27
27
|
* Commits a transaction.
|
|
28
|
+
*
|
|
29
|
+
* Throws {@link AbortedTransactionError} if the database confirms that
|
|
30
|
+
* the transaction was rolled back instead of committed.
|
|
28
31
|
*/
|
|
29
32
|
commitTransaction(connection: MssqlConnection): Promise<void>;
|
|
30
33
|
/**
|
|
@@ -10,6 +10,16 @@ export interface MysqlDialectConfig {
|
|
|
10
10
|
*
|
|
11
11
|
* If a function is provided, it's called once when the first query is executed.
|
|
12
12
|
*
|
|
13
|
+
* Once the driver has initialized, {@link Kysely.destroy} calls
|
|
14
|
+
* `pool.end(callback)`, whether the pool is supplied directly or returned by a
|
|
15
|
+
* function. If initialization hasn't started, `db.destroy()` does nothing,
|
|
16
|
+
* even if the supplied pool has been used outside Kysely.
|
|
17
|
+
*
|
|
18
|
+
* If Kysely is not the sole consumer of the pool, or the pool's lifecycle is
|
|
19
|
+
* managed elsewhere, skip `db.destroy()`. Let the application call
|
|
20
|
+
* `pool.end(callback)` once all consumers, including Kysely, have finished. No
|
|
21
|
+
* separate cleanup of the `Kysely` instance is needed.
|
|
22
|
+
*
|
|
13
23
|
* https://github.com/sidorares/node-mysql2#using-connection-pools
|
|
14
24
|
*/
|
|
15
25
|
pool: MysqlPool | (() => Promise<MysqlPool>);
|
|
@@ -24,6 +24,9 @@ export declare class MysqlDriver implements Driver {
|
|
|
24
24
|
beginTransaction(connection: DatabaseConnection, settings: TransactionSettings): Promise<void>;
|
|
25
25
|
/**
|
|
26
26
|
* Commits a transaction.
|
|
27
|
+
*
|
|
28
|
+
* Throws {@link AbortedTransactionError} if the database confirms that
|
|
29
|
+
* the transaction was rolled back instead of committed.
|
|
27
30
|
*/
|
|
28
31
|
commitTransaction(connection: DatabaseConnection): Promise<void>;
|
|
29
32
|
/**
|
|
@@ -76,5 +76,5 @@ export declare class PostgresAdapter extends DialectAdapterBase {
|
|
|
76
76
|
* is a single connection (session) that was used to execute the migrations
|
|
77
77
|
* and the `acquireMigrationLock` call.
|
|
78
78
|
*/
|
|
79
|
-
releaseMigrationLock(
|
|
79
|
+
releaseMigrationLock(db: Kysely<any>, _opt: MigrationLockOptions): Promise<void>;
|
|
80
80
|
}
|
|
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.PostgresAdapter = void 0;
|
|
4
4
|
const sql_js_1 = require("../../raw-builder/sql.js");
|
|
5
5
|
const dialect_adapter_base_js_1 = require("../dialect-adapter-base.js");
|
|
6
|
-
// Random id for our transaction lock.
|
|
7
6
|
const LOCK_ID = BigInt('3853314791062309107');
|
|
7
|
+
const LOCK_TIMEOUT_MILLISECONDS = 60 * 60 * 1_000;
|
|
8
8
|
class PostgresAdapter extends dialect_adapter_base_js_1.DialectAdapterBase {
|
|
9
9
|
get supportsTransactionalDdl() {
|
|
10
10
|
return true;
|
|
@@ -13,13 +13,19 @@ class PostgresAdapter extends dialect_adapter_base_js_1.DialectAdapterBase {
|
|
|
13
13
|
return true;
|
|
14
14
|
}
|
|
15
15
|
async acquireMigrationLock(db, _opt) {
|
|
16
|
-
//
|
|
17
|
-
|
|
16
|
+
// in 1 RTT, acquire a session-level advisory lock with a timeout.
|
|
17
|
+
//
|
|
18
|
+
// if ever this runs inside a transaction, niche edge case we support - the
|
|
19
|
+
// user is responsible to set a different timeout value or disable (`0` value).
|
|
20
|
+
await (0, sql_js_1.sql) `
|
|
21
|
+
with set_timeout as (
|
|
22
|
+
select set_config('lock_timeout', '${sql_js_1.sql.lit(LOCK_TIMEOUT_MILLISECONDS)}', true) as config_val
|
|
23
|
+
)
|
|
24
|
+
select pg_advisory_lock(${sql_js_1.sql.lit(LOCK_ID)})
|
|
25
|
+
from set_timeout`.execute(db);
|
|
18
26
|
}
|
|
19
|
-
async releaseMigrationLock(
|
|
20
|
-
|
|
21
|
-
// end of the transaction and since `supportsTransactionalDdl` true, we know
|
|
22
|
-
// the `db` instance passed to acquireMigrationLock is actually a transaction.
|
|
27
|
+
async releaseMigrationLock(db, _opt) {
|
|
28
|
+
await (0, sql_js_1.sql) `select pg_advisory_unlock(${sql_js_1.sql.lit(LOCK_ID)})`.execute(db);
|
|
23
29
|
}
|
|
24
30
|
}
|
|
25
31
|
exports.PostgresAdapter = PostgresAdapter;
|
|
@@ -8,6 +8,16 @@ export interface PostgresDialectConfig {
|
|
|
8
8
|
*
|
|
9
9
|
* If a function is provided, it's called once when the first query is executed.
|
|
10
10
|
*
|
|
11
|
+
* Once the driver has initialized, {@link Kysely.destroy} calls `pool.end()`,
|
|
12
|
+
* whether the pool is supplied directly or returned by a function. If
|
|
13
|
+
* initialization hasn't started, `db.destroy()` does nothing, even if the
|
|
14
|
+
* supplied pool has been used outside Kysely.
|
|
15
|
+
*
|
|
16
|
+
* If Kysely is not the sole consumer of the pool, or the pool's lifecycle is
|
|
17
|
+
* managed elsewhere, skip `db.destroy()`. Let the application call `pool.end()`
|
|
18
|
+
* once all consumers, including Kysely, have finished. No separate cleanup of
|
|
19
|
+
* the `Kysely` instance is needed.
|
|
20
|
+
*
|
|
11
21
|
* https://node-postgres.com/apis/pool
|
|
12
22
|
*/
|
|
13
23
|
pool: PostgresPool | (() => Promise<PostgresPool>);
|
|
@@ -51,7 +61,7 @@ export interface PostgresPool {
|
|
|
51
61
|
export interface PostgresPoolClient {
|
|
52
62
|
query<R>(sql: string, parameters: ReadonlyArray<unknown>): Promise<PostgresQueryResult<R>>;
|
|
53
63
|
query<R>(cursor: PostgresCursor<R>): PostgresCursor<R>;
|
|
54
|
-
release(): void;
|
|
64
|
+
release(destroy?: boolean): void;
|
|
55
65
|
}
|
|
56
66
|
export interface PostgresCursor<T> {
|
|
57
67
|
read(rowsCount: number): Promise<T[]>;
|
|
@@ -59,7 +69,7 @@ export interface PostgresCursor<T> {
|
|
|
59
69
|
}
|
|
60
70
|
export type PostgresCursorConstructor = new <T>(sql: string, parameters: unknown[]) => PostgresCursor<T>;
|
|
61
71
|
export interface PostgresQueryResult<R> {
|
|
62
|
-
command: 'UPDATE' | 'DELETE' | 'INSERT' | 'SELECT' | 'MERGE';
|
|
72
|
+
command: 'UPDATE' | 'DELETE' | 'INSERT' | 'SELECT' | 'MERGE' | 'COMMIT' | 'ROLLBACK';
|
|
63
73
|
rowCount: number;
|
|
64
74
|
rows: R[];
|
|
65
75
|
}
|
|
@@ -4,6 +4,7 @@ import { CompiledQuery } from '../../query-compiler/compiled-query.js';
|
|
|
4
4
|
import type { QueryCompiler } from '../../query-compiler/query-compiler.js';
|
|
5
5
|
import type { PostgresCursorConstructor, PostgresDialectConfig, PostgresPoolClient } from './postgres-dialect-config.js';
|
|
6
6
|
declare const PRIVATE_RELEASE_METHOD: unique symbol;
|
|
7
|
+
declare const PRIVATE_MARK_FOR_DISCARD_METHOD: unique symbol;
|
|
7
8
|
export declare class PostgresDriver implements Driver {
|
|
8
9
|
#private;
|
|
9
10
|
constructor(config: PostgresDialectConfig);
|
|
@@ -24,12 +25,15 @@ export declare class PostgresDriver implements Driver {
|
|
|
24
25
|
beginTransaction(connection: DatabaseConnection, settings: TransactionSettings): Promise<void>;
|
|
25
26
|
/**
|
|
26
27
|
* Commits a transaction.
|
|
28
|
+
*
|
|
29
|
+
* Throws {@link AbortedTransactionError} if the database confirms that
|
|
30
|
+
* the transaction was rolled back instead of committed.
|
|
27
31
|
*/
|
|
28
32
|
commitTransaction(connection: DatabaseConnection): Promise<void>;
|
|
29
33
|
/**
|
|
30
34
|
* Rolls back a transaction.
|
|
31
35
|
*/
|
|
32
|
-
rollbackTransaction(connection:
|
|
36
|
+
rollbackTransaction(connection: PostgresConnection): Promise<void>;
|
|
33
37
|
savepoint(connection: DatabaseConnection, savepointName: string, compileQuery: QueryCompiler['compileQuery']): Promise<void>;
|
|
34
38
|
rollbackToSavepoint(connection: DatabaseConnection, savepointName: string, compileQuery: QueryCompiler['compileQuery']): Promise<void>;
|
|
35
39
|
releaseSavepoint(connection: DatabaseConnection, savepointName: string, compileQuery: QueryCompiler['compileQuery']): Promise<void>;
|
|
@@ -51,5 +55,6 @@ declare class PostgresConnection implements DatabaseConnection {
|
|
|
51
55
|
executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>>;
|
|
52
56
|
streamQuery<O>(compiledQuery: CompiledQuery, chunkSize: number): AsyncIterableIterator<QueryResult<O>>;
|
|
53
57
|
[PRIVATE_RELEASE_METHOD](): void;
|
|
58
|
+
[PRIVATE_MARK_FOR_DISCARD_METHOD](): void;
|
|
54
59
|
}
|
|
55
60
|
export {};
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PostgresDriver = void 0;
|
|
4
|
+
const aborted_transaction_error_js_1 = require("../../driver/aborted-transaction-error.js");
|
|
4
5
|
const savepoint_parser_js_1 = require("../../parser/savepoint-parser.js");
|
|
5
6
|
const compiled_query_js_1 = require("../../query-compiler/compiled-query.js");
|
|
6
7
|
const object_utils_js_1 = require("../../util/object-utils.js");
|
|
7
8
|
const query_id_js_1 = require("../../util/query-id.js");
|
|
8
9
|
const stack_trace_utils_js_1 = require("../../util/stack-trace-utils.js");
|
|
9
10
|
const PRIVATE_RELEASE_METHOD = Symbol();
|
|
11
|
+
const PRIVATE_MARK_FOR_DISCARD_METHOD = Symbol();
|
|
10
12
|
class PostgresDriver {
|
|
11
13
|
#config;
|
|
12
14
|
#connections = new WeakMap();
|
|
@@ -21,6 +23,11 @@ class PostgresDriver {
|
|
|
21
23
|
}
|
|
22
24
|
async acquireConnection() {
|
|
23
25
|
const client = await this.#pool.connect();
|
|
26
|
+
// `destroy` was called while the connection was being acquired.
|
|
27
|
+
if (!this.#pool) {
|
|
28
|
+
client.release();
|
|
29
|
+
throw new Error('driver has already been destroyed');
|
|
30
|
+
}
|
|
24
31
|
let connection = this.#connections.get(client);
|
|
25
32
|
if (!connection) {
|
|
26
33
|
connection = new PostgresConnection(client, {
|
|
@@ -55,10 +62,21 @@ class PostgresDriver {
|
|
|
55
62
|
}
|
|
56
63
|
}
|
|
57
64
|
async commitTransaction(connection) {
|
|
58
|
-
await connection.executeQuery(compiled_query_js_1.CompiledQuery.raw('commit'));
|
|
65
|
+
const { rows } = await connection.executeQuery(compiled_query_js_1.CompiledQuery.raw('commit'));
|
|
66
|
+
// PostgreSQL completes COMMIT with a ROLLBACK tag if an earlier
|
|
67
|
+
// statement error left the transaction aborted.
|
|
68
|
+
if (rows[0]?.command === 'ROLLBACK') {
|
|
69
|
+
throw new aborted_transaction_error_js_1.AbortedTransactionError();
|
|
70
|
+
}
|
|
59
71
|
}
|
|
60
72
|
async rollbackTransaction(connection) {
|
|
61
|
-
|
|
73
|
+
try {
|
|
74
|
+
await connection.executeQuery(compiled_query_js_1.CompiledQuery.raw('rollback'));
|
|
75
|
+
}
|
|
76
|
+
catch (err) {
|
|
77
|
+
connection[PRIVATE_MARK_FOR_DISCARD_METHOD]();
|
|
78
|
+
throw err;
|
|
79
|
+
}
|
|
62
80
|
}
|
|
63
81
|
async savepoint(connection, savepointName, compileQuery) {
|
|
64
82
|
await connection.executeQuery(compileQuery((0, savepoint_parser_js_1.parseSavepointCommand)('savepoint', savepointName), (0, query_id_js_1.createQueryId)()));
|
|
@@ -84,13 +102,18 @@ exports.PostgresDriver = PostgresDriver;
|
|
|
84
102
|
class PostgresConnection {
|
|
85
103
|
#client;
|
|
86
104
|
#options;
|
|
105
|
+
#discarded;
|
|
87
106
|
constructor(client, options) {
|
|
88
107
|
this.#client = client;
|
|
89
108
|
this.#options = options;
|
|
90
109
|
}
|
|
91
110
|
async executeQuery(compiledQuery) {
|
|
111
|
+
this.#assertNotDiscarded();
|
|
92
112
|
try {
|
|
93
113
|
const { command, rowCount, rows } = await this.#client.query(compiledQuery.sql, [...compiledQuery.parameters]);
|
|
114
|
+
if (command === 'ROLLBACK' || command === 'COMMIT') {
|
|
115
|
+
return { rows: [{ command }] };
|
|
116
|
+
}
|
|
94
117
|
return {
|
|
95
118
|
numAffectedRows: command === 'INSERT' ||
|
|
96
119
|
command === 'UPDATE' ||
|
|
@@ -112,6 +135,7 @@ class PostgresConnection {
|
|
|
112
135
|
if (!Number.isInteger(chunkSize) || chunkSize <= 0) {
|
|
113
136
|
throw new Error('chunkSize must be a positive integer');
|
|
114
137
|
}
|
|
138
|
+
this.#assertNotDiscarded();
|
|
115
139
|
const cursor = this.#client.query(new this.#options.cursor(compiledQuery.sql, compiledQuery.parameters.slice()));
|
|
116
140
|
try {
|
|
117
141
|
while (true) {
|
|
@@ -129,6 +153,14 @@ class PostgresConnection {
|
|
|
129
153
|
}
|
|
130
154
|
}
|
|
131
155
|
[PRIVATE_RELEASE_METHOD]() {
|
|
132
|
-
this.#client.release();
|
|
156
|
+
this.#client.release(Boolean(this.#discarded));
|
|
157
|
+
}
|
|
158
|
+
[PRIVATE_MARK_FOR_DISCARD_METHOD]() {
|
|
159
|
+
this.#discarded = true;
|
|
160
|
+
}
|
|
161
|
+
#assertNotDiscarded() {
|
|
162
|
+
if (this.#discarded) {
|
|
163
|
+
throw new Error('Postgres connection has been discarded');
|
|
164
|
+
}
|
|
133
165
|
}
|
|
134
166
|
}
|
|
@@ -73,20 +73,26 @@ class PostgresIntrospector {
|
|
|
73
73
|
};
|
|
74
74
|
}
|
|
75
75
|
#parseTableMetadata(columns) {
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
76
|
+
const tables = [];
|
|
77
|
+
const schemas = new Map();
|
|
78
|
+
for (const column of columns) {
|
|
79
|
+
let schema = schemas.get(column.schema);
|
|
80
|
+
if (!schema) {
|
|
81
|
+
schema = new Map();
|
|
82
|
+
schemas.set(column.schema, schema);
|
|
83
|
+
}
|
|
84
|
+
let table = schema.get(column.table);
|
|
85
|
+
if (!table) {
|
|
86
|
+
table = (0, object_utils_js_1.freeze)({
|
|
83
87
|
columns: [],
|
|
84
88
|
isView: column.table_type === 'v',
|
|
85
|
-
name: table,
|
|
86
|
-
schema,
|
|
87
|
-
})
|
|
89
|
+
name: column.table,
|
|
90
|
+
schema: column.schema,
|
|
91
|
+
});
|
|
92
|
+
schema.set(column.table, table);
|
|
93
|
+
tables.push(table);
|
|
88
94
|
}
|
|
89
|
-
|
|
95
|
+
table.columns.push((0, object_utils_js_1.freeze)({
|
|
90
96
|
comment: column.column_description ?? undefined,
|
|
91
97
|
dataType: column.type,
|
|
92
98
|
dataTypeSchema: column.type_schema,
|
|
@@ -96,7 +102,7 @@ class PostgresIntrospector {
|
|
|
96
102
|
name: column.column,
|
|
97
103
|
}));
|
|
98
104
|
}
|
|
99
|
-
return
|
|
105
|
+
return tables;
|
|
100
106
|
}
|
|
101
107
|
}
|
|
102
108
|
exports.PostgresIntrospector = PostgresIntrospector;
|
|
@@ -8,6 +8,16 @@ export interface SqliteDialectConfig {
|
|
|
8
8
|
*
|
|
9
9
|
* If a function is provided, it's called once when the first query is executed.
|
|
10
10
|
*
|
|
11
|
+
* Once the driver has initialized, {@link Kysely.destroy} calls
|
|
12
|
+
* `database.close()`, whether the database is supplied directly or returned by
|
|
13
|
+
* a function. If initialization hasn't started, `db.destroy()` does nothing,
|
|
14
|
+
* even if the supplied database has been used outside Kysely.
|
|
15
|
+
*
|
|
16
|
+
* If Kysely is not the sole consumer of the database, or the database's
|
|
17
|
+
* lifecycle is managed elsewhere, skip `db.destroy()`. Let the application
|
|
18
|
+
* call `database.close()` once all consumers, including Kysely, have finished.
|
|
19
|
+
* No separate cleanup of the `Kysely` instance is needed.
|
|
20
|
+
*
|
|
11
21
|
* https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#new-databasepath-options
|
|
12
22
|
*/
|
|
13
23
|
database: SqliteDatabase | (() => Promise<SqliteDatabase>);
|
|
@@ -22,6 +22,9 @@ export declare class SqliteDriver implements Driver {
|
|
|
22
22
|
beginTransaction(connection: DatabaseConnection): Promise<void>;
|
|
23
23
|
/**
|
|
24
24
|
* Commits a transaction.
|
|
25
|
+
*
|
|
26
|
+
* Throws {@link AbortedTransactionError} if the database confirms that
|
|
27
|
+
* the transaction was rolled back instead of committed.
|
|
25
28
|
*/
|
|
26
29
|
commitTransaction(connection: DatabaseConnection): Promise<void>;
|
|
27
30
|
/**
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
declare const ABORTED_TRANSACTION_ERROR: unique symbol;
|
|
2
|
+
/**
|
|
3
|
+
* Thrown when the database rolls back a transaction instead of committing it.
|
|
4
|
+
*/
|
|
5
|
+
export declare class AbortedTransactionError extends Error {
|
|
6
|
+
name: string;
|
|
7
|
+
readonly [ABORTED_TRANSACTION_ERROR] = true;
|
|
8
|
+
constructor();
|
|
9
|
+
}
|
|
10
|
+
export declare function isAbortedTransactionError(error: unknown): error is AbortedTransactionError;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AbortedTransactionError = void 0;
|
|
4
|
+
exports.isAbortedTransactionError = isAbortedTransactionError;
|
|
5
|
+
const object_utils_js_1 = require("../util/object-utils.js");
|
|
6
|
+
const ABORTED_TRANSACTION_ERROR = Symbol.for('kysely.AbortedTransactionError');
|
|
7
|
+
/**
|
|
8
|
+
* Thrown when the database rolls back a transaction instead of committing it.
|
|
9
|
+
*/
|
|
10
|
+
class AbortedTransactionError extends Error {
|
|
11
|
+
name = 'AbortedTransactionError';
|
|
12
|
+
[ABORTED_TRANSACTION_ERROR] = true;
|
|
13
|
+
constructor() {
|
|
14
|
+
super('Transaction was rolled back instead of committed');
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.AbortedTransactionError = AbortedTransactionError;
|
|
18
|
+
function isAbortedTransactionError(error) {
|
|
19
|
+
return (0, object_utils_js_1.isObject)(error) && ABORTED_TRANSACTION_ERROR in error;
|
|
20
|
+
}
|
|
@@ -23,6 +23,9 @@ export interface Driver {
|
|
|
23
23
|
beginTransaction(connection: DatabaseConnection, settings: TransactionSettings): Promise<void>;
|
|
24
24
|
/**
|
|
25
25
|
* Commits a transaction.
|
|
26
|
+
*
|
|
27
|
+
* Throws {@link AbortedTransactionError} if the database confirms that
|
|
28
|
+
* the transaction was rolled back instead of committed.
|
|
26
29
|
*/
|
|
27
30
|
commitTransaction(connection: DatabaseConnection): Promise<void>;
|
|
28
31
|
/**
|
|
@@ -55,6 +55,9 @@ export declare class DummyDriver implements Driver {
|
|
|
55
55
|
beginTransaction(): Promise<void>;
|
|
56
56
|
/**
|
|
57
57
|
* Commits a transaction.
|
|
58
|
+
*
|
|
59
|
+
* Throws {@link AbortedTransactionError} if the database confirms that
|
|
60
|
+
* the transaction was rolled back instead of committed.
|
|
58
61
|
*/
|
|
59
62
|
commitTransaction(): Promise<void>;
|
|
60
63
|
/**
|
|
@@ -31,6 +31,9 @@ export declare class RuntimeDriver implements Driver {
|
|
|
31
31
|
beginTransaction(connection: DatabaseConnection, settings: TransactionSettings): Promise<void>;
|
|
32
32
|
/**
|
|
33
33
|
* Commits a transaction.
|
|
34
|
+
*
|
|
35
|
+
* Throws {@link AbortedTransactionError} if the database confirms that
|
|
36
|
+
* the transaction was rolled back instead of committed.
|
|
34
37
|
*/
|
|
35
38
|
commitTransaction(connection: DatabaseConnection): Promise<void>;
|
|
36
39
|
/**
|
|
@@ -202,7 +202,9 @@ export declare function jsonObjectFrom<O>(expr: Expression<O>): RawBuilder<Simpl
|
|
|
202
202
|
*
|
|
203
203
|
* ```sql
|
|
204
204
|
* select "id", json_query(
|
|
205
|
-
* '{"
|
|
205
|
+
* '{"'+string_escape(N'first', 'json')+'":"'+string_escape("first_name", 'json')+
|
|
206
|
+
* '","'+string_escape(N'last', 'json')+'":"'+string_escape("last_name", 'json')+
|
|
207
|
+
* '","'+string_escape(N'full', 'json')+'":"'+string_escape(concat("first_name", ' ', "last_name"), 'json')+'"}'
|
|
206
208
|
* ) as "name"
|
|
207
209
|
* from "person"
|
|
208
210
|
* ```
|
|
@@ -209,11 +209,13 @@ function jsonObjectFrom(expr) {
|
|
|
209
209
|
*
|
|
210
210
|
* ```sql
|
|
211
211
|
* select "id", json_query(
|
|
212
|
-
* '{"
|
|
212
|
+
* '{"'+string_escape(N'first', 'json')+'":"'+string_escape("first_name", 'json')+
|
|
213
|
+
* '","'+string_escape(N'last', 'json')+'":"'+string_escape("last_name", 'json')+
|
|
214
|
+
* '","'+string_escape(N'full', 'json')+'":"'+string_escape(concat("first_name", ' ', "last_name"), 'json')+'"}'
|
|
213
215
|
* ) as "name"
|
|
214
216
|
* from "person"
|
|
215
217
|
* ```
|
|
216
218
|
*/
|
|
217
219
|
function jsonBuildObject(obj) {
|
|
218
|
-
return (0, sql_js_1.sql) `json_query('{${sql_js_1.sql.join(Object.keys(obj).map((k) => (0, sql_js_1.sql) `"${sql_js_1.sql.
|
|
220
|
+
return (0, sql_js_1.sql) `json_query('{${sql_js_1.sql.join(Object.keys(obj).map((k) => (0, sql_js_1.sql) `"'+string_escape(N${sql_js_1.sql.lit(k)}, 'json')+'":"'+string_escape(${obj[k]}, 'json')+'"`), (0, sql_js_1.sql) `,`)}}')`;
|
|
219
221
|
}
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -57,6 +57,7 @@ export * from './dynamic/dynamic.js';
|
|
|
57
57
|
export * from './dynamic/dynamic-reference-builder.js';
|
|
58
58
|
export * from './dynamic/dynamic-table-builder.js';
|
|
59
59
|
export * from './driver/driver.js';
|
|
60
|
+
export * from './driver/aborted-transaction-error.js';
|
|
60
61
|
export * from './driver/database-connection.js';
|
|
61
62
|
export * from './driver/connection-provider.js';
|
|
62
63
|
export * from './driver/default-connection-provider.js';
|
package/dist/cjs/index.js
CHANGED
|
@@ -75,6 +75,7 @@ __exportStar(require("./dynamic/dynamic.js"), exports);
|
|
|
75
75
|
__exportStar(require("./dynamic/dynamic-reference-builder.js"), exports);
|
|
76
76
|
__exportStar(require("./dynamic/dynamic-table-builder.js"), exports);
|
|
77
77
|
__exportStar(require("./driver/driver.js"), exports);
|
|
78
|
+
__exportStar(require("./driver/aborted-transaction-error.js"), exports);
|
|
78
79
|
__exportStar(require("./driver/database-connection.js"), exports);
|
|
79
80
|
__exportStar(require("./driver/connection-provider.js"), exports);
|
|
80
81
|
__exportStar(require("./driver/default-connection-provider.js"), exports);
|
package/dist/cjs/kysely.d.ts
CHANGED
|
@@ -29,7 +29,8 @@ declare global {
|
|
|
29
29
|
* The main Kysely class.
|
|
30
30
|
*
|
|
31
31
|
* You should create one instance of `Kysely` per database using the {@link Kysely}
|
|
32
|
-
* constructor.
|
|
32
|
+
* constructor. The dialect manages database connections and may use a shared
|
|
33
|
+
* pool or database instance.
|
|
33
34
|
*
|
|
34
35
|
* ### Examples
|
|
35
36
|
*
|
|
@@ -384,7 +385,16 @@ export declare class Kysely<DB> extends QueryCreator<DB> implements QueryExecuto
|
|
|
384
385
|
/**
|
|
385
386
|
* Releases all resources and disconnects from the database.
|
|
386
387
|
*
|
|
387
|
-
*
|
|
388
|
+
* Call this when you are done using the `Kysely` instance and Kysely is
|
|
389
|
+
* responsible for closing its connections, pool, or database instance.
|
|
390
|
+
*
|
|
391
|
+
* If the pool or database instance is shared with other consumers or its
|
|
392
|
+
* lifecycle is managed elsewhere, skip this call. Let the application close it
|
|
393
|
+
* once all consumers, including Kysely, have finished. No separate cleanup of
|
|
394
|
+
* the `Kysely` instance is needed.
|
|
395
|
+
*
|
|
396
|
+
* The driver is initialized lazily. If initialization hasn't started, this
|
|
397
|
+
* method does nothing.
|
|
388
398
|
*/
|
|
389
399
|
destroy(): Promise<void>;
|
|
390
400
|
/**
|
|
@@ -511,7 +521,16 @@ export declare class Transaction<DB> extends Kysely<DB> {
|
|
|
511
521
|
/**
|
|
512
522
|
* Releases all resources and disconnects from the database.
|
|
513
523
|
*
|
|
514
|
-
*
|
|
524
|
+
* Call this when you are done using the `Kysely` instance and Kysely is
|
|
525
|
+
* responsible for closing its connections, pool, or database instance.
|
|
526
|
+
*
|
|
527
|
+
* If the pool or database instance is shared with other consumers or its
|
|
528
|
+
* lifecycle is managed elsewhere, skip this call. Let the application close it
|
|
529
|
+
* once all consumers, including Kysely, have finished. No separate cleanup of
|
|
530
|
+
* the `Kysely` instance is needed.
|
|
531
|
+
*
|
|
532
|
+
* The driver is initialized lazily. If initialization hasn't started, this
|
|
533
|
+
* method does nothing.
|
|
515
534
|
*/
|
|
516
535
|
destroy(): Promise<void>;
|
|
517
536
|
/**
|
|
@@ -646,6 +665,13 @@ export declare class ControlledTransaction<DB, S extends string[] = []> extends
|
|
|
646
665
|
/**
|
|
647
666
|
* Commits the transaction.
|
|
648
667
|
*
|
|
668
|
+
* If the database rolls back instead of committing, marks the transaction
|
|
669
|
+
* as rolled back, releases the connection and throws {@link AbortedTransactionError}.
|
|
670
|
+
*
|
|
671
|
+
* Waits for ongoing queries to finish. Streams must be exhausted or closed
|
|
672
|
+
* before awaiting this command. When consuming a stream with `for await`,
|
|
673
|
+
* exit the loop before committing.
|
|
674
|
+
*
|
|
649
675
|
* See {@link rollback}.
|
|
650
676
|
*
|
|
651
677
|
* ### Examples
|
|
@@ -671,6 +697,16 @@ export declare class ControlledTransaction<DB, S extends string[] = []> extends
|
|
|
671
697
|
/**
|
|
672
698
|
* Rolls back the transaction.
|
|
673
699
|
*
|
|
700
|
+
* If a commit already reported that the database rolled back the transaction,
|
|
701
|
+
* succeeds without sending another rollback command.
|
|
702
|
+
*
|
|
703
|
+
* The connection is released even if the rollback fails, so the transaction
|
|
704
|
+
* cannot be used afterwards in either case.
|
|
705
|
+
*
|
|
706
|
+
* Waits for ongoing queries to finish. Streams must be exhausted or closed
|
|
707
|
+
* before awaiting this command. When consuming a stream with `for await`,
|
|
708
|
+
* exit the loop before rolling back.
|
|
709
|
+
*
|
|
674
710
|
* See {@link commit} and {@link rollbackToSavepoint}.
|
|
675
711
|
*
|
|
676
712
|
* ### Examples
|
|
@@ -696,6 +732,10 @@ export declare class ControlledTransaction<DB, S extends string[] = []> extends
|
|
|
696
732
|
/**
|
|
697
733
|
* Creates a savepoint with a given name.
|
|
698
734
|
*
|
|
735
|
+
* Waits for ongoing queries to finish. Streams must be exhausted or closed
|
|
736
|
+
* before awaiting this command. When consuming a stream with `for await`,
|
|
737
|
+
* exit the loop before creating a savepoint.
|
|
738
|
+
*
|
|
699
739
|
* See {@link rollbackToSavepoint} and {@link releaseSavepoint}.
|
|
700
740
|
*
|
|
701
741
|
* For a type-safe experience, you should use the returned instance from now on.
|
|
@@ -726,6 +766,10 @@ export declare class ControlledTransaction<DB, S extends string[] = []> extends
|
|
|
726
766
|
/**
|
|
727
767
|
* Rolls back to a savepoint with a given name.
|
|
728
768
|
*
|
|
769
|
+
* Waits for ongoing queries to finish. Streams must be exhausted or closed
|
|
770
|
+
* before awaiting this command. When consuming a stream with `for await`,
|
|
771
|
+
* exit the loop before rolling back to a savepoint.
|
|
772
|
+
*
|
|
729
773
|
* See {@link savepoint} and {@link releaseSavepoint}.
|
|
730
774
|
*
|
|
731
775
|
* You must use the same instance returned by {@link savepoint}, or
|
|
@@ -757,6 +801,10 @@ export declare class ControlledTransaction<DB, S extends string[] = []> extends
|
|
|
757
801
|
/**
|
|
758
802
|
* Releases a savepoint with a given name.
|
|
759
803
|
*
|
|
804
|
+
* Waits for ongoing queries to finish. Streams must be exhausted or closed
|
|
805
|
+
* before awaiting this command. When consuming a stream with `for await`,
|
|
806
|
+
* exit the loop before releasing a savepoint.
|
|
807
|
+
*
|
|
760
808
|
* See {@link savepoint} and {@link rollbackToSavepoint}.
|
|
761
809
|
*
|
|
762
810
|
* You must use the same instance returned by {@link savepoint}, or
|
|
@@ -834,8 +882,12 @@ export declare class ControlledTransaction<DB, S extends string[] = []> extends
|
|
|
834
882
|
*/
|
|
835
883
|
withTables<T extends Record<string, Record<string, any>>>(): ControlledTransaction<DrainOuterGeneric<DB & T>, S>;
|
|
836
884
|
}
|
|
885
|
+
interface ControlledTransactionState {
|
|
886
|
+
status: 'active' | 'committed' | 'rolled back' | 'aborted';
|
|
887
|
+
}
|
|
837
888
|
interface ControlledTransactionProps extends KyselyProps {
|
|
838
889
|
readonly connection: ControlledConnection;
|
|
890
|
+
readonly transactionState?: ControlledTransactionState;
|
|
839
891
|
}
|
|
840
892
|
export declare class Command<T> {
|
|
841
893
|
#private;
|