kysely 0.17.3 → 0.19.0
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 +24 -17
- package/dist/cjs/dialect/database-introspector.d.ts +13 -0
- package/dist/cjs/dialect/mysql/mysql-dialect-config.d.ts +24 -207
- package/dist/cjs/dialect/mysql/mysql-dialect.d.ts +26 -3
- package/dist/cjs/dialect/mysql/mysql-dialect.js +27 -5
- package/dist/cjs/dialect/mysql/mysql-driver.d.ts +13 -4
- package/dist/cjs/dialect/mysql/mysql-driver.js +33 -51
- package/dist/cjs/dialect/mysql/mysql-introspector.d.ts +11 -1
- package/dist/cjs/dialect/mysql/mysql-introspector.js +29 -7
- package/dist/cjs/dialect/postgres/postgres-dialect-config.d.ts +25 -89
- package/dist/cjs/dialect/postgres/postgres-dialect.d.ts +25 -5
- package/dist/cjs/dialect/postgres/postgres-dialect.js +26 -7
- package/dist/cjs/dialect/postgres/postgres-driver.d.ts +12 -3
- package/dist/cjs/dialect/postgres/postgres-driver.js +21 -39
- package/dist/cjs/dialect/postgres/postgres-introspector.d.ts +11 -1
- package/dist/cjs/dialect/postgres/postgres-introspector.js +15 -4
- package/dist/cjs/dialect/sqlite/sqlite-dialect-config.d.ts +25 -28
- package/dist/cjs/dialect/sqlite/sqlite-dialect.d.ts +19 -3
- package/dist/cjs/dialect/sqlite/sqlite-dialect.js +19 -3
- package/dist/cjs/dialect/sqlite/sqlite-driver.js +3 -37
- package/dist/cjs/dialect/sqlite/sqlite-introspector.d.ts +11 -1
- package/dist/cjs/dialect/sqlite/sqlite-introspector.js +11 -4
- package/dist/cjs/index.d.ts +153 -2
- package/dist/cjs/index.js +147 -3
- package/dist/cjs/migration/file-migration-provider.d.ts +21 -5
- package/dist/cjs/migration/file-migration-provider.js +14 -13
- package/dist/cjs/migration/migrator.d.ts +44 -2
- package/dist/cjs/migration/migrator.js +77 -18
- package/dist/cjs/operation-node/alias-node.d.ts +2 -2
- package/dist/cjs/operation-node/alias-node.js +1 -2
- package/dist/cjs/operation-node/operation-node-transformer.js +1 -0
- package/dist/cjs/operation-node/update-query-node.d.ts +3 -0
- package/dist/cjs/operation-node/update-query-node.js +11 -2
- package/dist/cjs/parser/reference-parser.js +2 -1
- package/dist/cjs/parser/table-parser.js +2 -1
- package/dist/cjs/parser/update-set-parser.d.ts +3 -3
- package/dist/cjs/parser/with-parser.d.ts +2 -2
- package/dist/cjs/plugin/noop-plugin.d.ts +42 -0
- package/dist/cjs/plugin/noop-plugin.js +12 -0
- package/dist/cjs/query-builder/insert-query-builder.d.ts +1 -1
- package/dist/cjs/query-builder/on-conflict-builder.d.ts +1 -1
- package/dist/cjs/query-builder/select-query-builder.d.ts +3 -1
- package/dist/cjs/query-builder/select-query-builder.js +5 -5
- package/dist/cjs/query-builder/update-query-builder.d.ts +57 -28
- package/dist/cjs/query-builder/update-query-builder.js +7 -0
- package/dist/cjs/query-compiler/default-query-compiler.js +4 -0
- package/dist/cjs/query-creator.d.ts +10 -1
- package/dist/cjs/query-creator.js +18 -0
- package/dist/cjs/raw-builder/raw-builder.d.ts +32 -2
- package/dist/cjs/raw-builder/raw-builder.js +5 -26
- package/dist/cjs/schema/schema.d.ts +9 -0
- package/dist/cjs/schema/schema.js +12 -0
- package/dist/cjs/util/column-type.d.ts +9 -1
- package/dist/cjs/util/stack-trace-utils.d.ts +1 -0
- package/dist/cjs/util/stack-trace-utils.js +17 -0
- package/dist/esm/dialect/database-introspector.d.ts +13 -0
- package/dist/esm/dialect/mysql/mysql-dialect-config.d.ts +24 -207
- package/dist/esm/dialect/mysql/mysql-dialect.d.ts +26 -3
- package/dist/esm/dialect/mysql/mysql-dialect.js +27 -5
- package/dist/esm/dialect/mysql/mysql-driver.d.ts +13 -4
- package/dist/esm/dialect/mysql/mysql-driver.js +33 -51
- package/dist/esm/dialect/mysql/mysql-introspector.d.ts +11 -1
- package/dist/esm/dialect/mysql/mysql-introspector.js +30 -8
- package/dist/esm/dialect/postgres/postgres-dialect-config.d.ts +25 -89
- package/dist/esm/dialect/postgres/postgres-dialect.d.ts +25 -5
- package/dist/esm/dialect/postgres/postgres-dialect.js +26 -7
- package/dist/esm/dialect/postgres/postgres-driver.d.ts +12 -3
- package/dist/esm/dialect/postgres/postgres-driver.js +21 -39
- package/dist/esm/dialect/postgres/postgres-introspector.d.ts +11 -1
- package/dist/esm/dialect/postgres/postgres-introspector.js +16 -5
- package/dist/esm/dialect/sqlite/sqlite-dialect-config.d.ts +25 -28
- package/dist/esm/dialect/sqlite/sqlite-dialect.d.ts +19 -3
- package/dist/esm/dialect/sqlite/sqlite-dialect.js +19 -3
- package/dist/esm/dialect/sqlite/sqlite-driver.js +4 -38
- package/dist/esm/dialect/sqlite/sqlite-introspector.d.ts +11 -1
- package/dist/esm/dialect/sqlite/sqlite-introspector.js +12 -5
- package/dist/esm/index.d.ts +153 -2
- package/dist/esm/index.js +142 -2
- package/dist/esm/migration/file-migration-provider.d.ts +21 -5
- package/dist/esm/migration/file-migration-provider.js +14 -13
- package/dist/esm/migration/migrator.d.ts +44 -2
- package/dist/esm/migration/migrator.js +76 -17
- package/dist/esm/operation-node/alias-node.d.ts +2 -2
- package/dist/esm/operation-node/alias-node.js +1 -2
- package/dist/esm/operation-node/operation-node-transformer.js +1 -0
- package/dist/esm/operation-node/update-query-node.d.ts +3 -0
- package/dist/esm/operation-node/update-query-node.js +11 -2
- package/dist/esm/parser/reference-parser.js +2 -1
- package/dist/esm/parser/table-parser.js +2 -1
- package/dist/esm/parser/update-set-parser.d.ts +3 -3
- package/dist/esm/parser/with-parser.d.ts +2 -2
- package/dist/esm/plugin/noop-plugin.d.ts +42 -0
- package/dist/esm/plugin/noop-plugin.js +9 -0
- package/dist/esm/query-builder/insert-query-builder.d.ts +1 -1
- package/dist/esm/query-builder/on-conflict-builder.d.ts +1 -1
- package/dist/esm/query-builder/select-query-builder.d.ts +3 -1
- package/dist/esm/query-builder/select-query-builder.js +5 -5
- package/dist/esm/query-builder/update-query-builder.d.ts +57 -28
- package/dist/esm/query-builder/update-query-builder.js +7 -0
- package/dist/esm/query-compiler/default-query-compiler.js +4 -0
- package/dist/esm/query-creator.d.ts +10 -1
- package/dist/esm/query-creator.js +18 -0
- package/dist/esm/raw-builder/raw-builder.d.ts +32 -2
- package/dist/esm/raw-builder/raw-builder.js +5 -26
- package/dist/esm/schema/schema.d.ts +9 -0
- package/dist/esm/schema/schema.js +12 -0
- package/dist/esm/util/column-type.d.ts +9 -1
- package/dist/esm/util/stack-trace-utils.d.ts +1 -0
- package/dist/esm/util/stack-trace-utils.js +14 -0
- package/package.json +10 -15
- package/dist/cjs/index-nodeless.d.ts +0 -152
- package/dist/cjs/index-nodeless.js +0 -153
- package/dist/esm/index-nodeless.d.ts +0 -152
- package/dist/esm/index-nodeless.js +0 -142
|
@@ -3,26 +3,47 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.MysqlIntrospector = void 0;
|
|
4
4
|
const migrator_js_1 = require("../../migration/migrator.js");
|
|
5
5
|
const object_utils_js_1 = require("../../util/object-utils.js");
|
|
6
|
-
const sql_js_1 = require("../../raw-builder/sql.js");
|
|
7
6
|
class MysqlIntrospector {
|
|
8
7
|
#db;
|
|
9
8
|
constructor(db) {
|
|
10
9
|
this.#db = db;
|
|
11
10
|
}
|
|
12
|
-
async
|
|
11
|
+
async getSchemas() {
|
|
12
|
+
let rawSchemas = await this.#db
|
|
13
|
+
.selectFrom('information_schema.schemata')
|
|
14
|
+
.select('schema_name')
|
|
15
|
+
.castTo()
|
|
16
|
+
.execute();
|
|
17
|
+
return rawSchemas.map((it) => ({ name: it.SCHEMA_NAME }));
|
|
18
|
+
}
|
|
19
|
+
async getTables(options = { withInternalKyselyTables: false }) {
|
|
13
20
|
let query = this.#db
|
|
14
21
|
.selectFrom('information_schema.columns')
|
|
15
|
-
.
|
|
16
|
-
|
|
22
|
+
.select([
|
|
23
|
+
'column_name',
|
|
24
|
+
'table_name',
|
|
25
|
+
'table_schema',
|
|
26
|
+
'is_nullable',
|
|
27
|
+
'data_type',
|
|
28
|
+
])
|
|
29
|
+
.where('table_schema', 'not in', [
|
|
30
|
+
'information_schema',
|
|
31
|
+
'performance_schema',
|
|
32
|
+
'mysql',
|
|
33
|
+
'sys',
|
|
34
|
+
])
|
|
17
35
|
.castTo();
|
|
18
36
|
if (!options.withInternalKyselyTables) {
|
|
19
37
|
query = query
|
|
20
|
-
.where('table_name', '!=', migrator_js_1.
|
|
21
|
-
.where('table_name', '!=', migrator_js_1.
|
|
38
|
+
.where('table_name', '!=', migrator_js_1.DEFAULT_MIGRATION_TABLE)
|
|
39
|
+
.where('table_name', '!=', migrator_js_1.DEFAULT_MIGRATION_LOCK_TABLE);
|
|
22
40
|
}
|
|
23
41
|
const rawColumns = await query.execute();
|
|
42
|
+
return this.#parseTableMetadata(rawColumns);
|
|
43
|
+
}
|
|
44
|
+
async getMetadata(options) {
|
|
24
45
|
return {
|
|
25
|
-
tables: this
|
|
46
|
+
tables: await this.getTables(options),
|
|
26
47
|
};
|
|
27
48
|
}
|
|
28
49
|
#parseTableMetadata(columns) {
|
|
@@ -31,6 +52,7 @@ class MysqlIntrospector {
|
|
|
31
52
|
if (!table) {
|
|
32
53
|
table = (0, object_utils_js_1.freeze)({
|
|
33
54
|
name: it.TABLE_NAME,
|
|
55
|
+
schema: it.TABLE_SCHEMA,
|
|
34
56
|
columns: [],
|
|
35
57
|
});
|
|
36
58
|
tables.push(table);
|
|
@@ -1,103 +1,39 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import * as stream from 'stream';
|
|
3
|
-
import { ConnectionOptions } from 'tls';
|
|
4
1
|
import { DatabaseConnection } from '../../driver/database-connection.js';
|
|
5
2
|
/**
|
|
6
3
|
* Config for the PostgreSQL dialect.
|
|
7
|
-
*
|
|
8
|
-
* This interface is equal to `pg` library's `Pool` config:
|
|
9
|
-
*
|
|
10
|
-
* https://node-postgres.com/api/pool
|
|
11
|
-
* https://node-postgres.com/api/client
|
|
12
4
|
*/
|
|
13
5
|
export interface PostgresDialectConfig {
|
|
14
6
|
/**
|
|
15
|
-
*
|
|
16
|
-
*/
|
|
17
|
-
user?: string;
|
|
18
|
-
/**
|
|
19
|
-
* default process.env.PGDATABASE || process.env.USER
|
|
20
|
-
*/
|
|
21
|
-
database?: string;
|
|
22
|
-
/**
|
|
23
|
-
* default process.env.PGPASSWORD
|
|
24
|
-
*/
|
|
25
|
-
password?: string | (() => string | Promise<string>);
|
|
26
|
-
/**
|
|
27
|
-
* default process.env.PGPORT
|
|
28
|
-
*/
|
|
29
|
-
port?: number;
|
|
30
|
-
/**
|
|
31
|
-
* default process.env.PGHOST
|
|
32
|
-
*/
|
|
33
|
-
host?: string;
|
|
34
|
-
/**
|
|
35
|
-
* e.g. postgres:*user:password@host:5432/database
|
|
36
|
-
*/
|
|
37
|
-
connectionString?: string;
|
|
38
|
-
/**
|
|
39
|
-
* passed directly to node.TLSSocket, supports all tls.connect options
|
|
40
|
-
*/
|
|
41
|
-
ssl?: boolean | ConnectionOptions;
|
|
42
|
-
/**
|
|
43
|
-
* Number of milliseconds to wait before timing out when connecting a new client
|
|
44
|
-
* by default this is 0 which means no timeout.
|
|
45
|
-
*/
|
|
46
|
-
connectionTimeoutMillis?: number;
|
|
47
|
-
/**
|
|
48
|
-
* Maximum number of clients the pool should contain. by default this is set to 10.
|
|
49
|
-
*/
|
|
50
|
-
max?: number;
|
|
51
|
-
/**
|
|
52
|
-
* Minimum number of clients the pool should contain
|
|
53
|
-
*/
|
|
54
|
-
min?: number;
|
|
55
|
-
/**
|
|
56
|
-
* Number of milliseconds a client must sit idle in the pool and not be checked out
|
|
57
|
-
* before it is disconnected from the backend and discarded.
|
|
7
|
+
* A postgres Pool instance or a function that returns one.
|
|
58
8
|
*
|
|
59
|
-
*
|
|
60
|
-
*/
|
|
61
|
-
idleTimeoutMillis?: number;
|
|
62
|
-
/**
|
|
63
|
-
* Default behavior is the pool will keep clients open & connected to the backend
|
|
64
|
-
* until idleTimeoutMillis expire for each client and node will maintain a ref
|
|
65
|
-
* to the socket on the client, keeping the event loop alive until all clients are closed
|
|
66
|
-
* after being idle or the pool is manually shutdown with `pool.end()`.
|
|
9
|
+
* If a function is provided, it's called once when the first query is executed.
|
|
67
10
|
*
|
|
68
|
-
*
|
|
69
|
-
* as soon as all clients in the pool are idle, even if their socket is still open
|
|
70
|
-
* to the PostgreSQL server. This can be handy in scripts & tests
|
|
71
|
-
* where you don't want to wait for your clients to go idle before your process exits.
|
|
72
|
-
*/
|
|
73
|
-
allowExitOnIdle?: boolean;
|
|
74
|
-
log?: (...messages: any[]) => void;
|
|
75
|
-
/**
|
|
76
|
-
* Number of milliseconds before a statement in query will time out, default is no timeout.
|
|
11
|
+
* https://node-postgres.com/api/pool
|
|
77
12
|
*/
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Number of milliseconds before terminating any session with an open idle transaction.
|
|
81
|
-
* Default is no timeout.
|
|
82
|
-
*/
|
|
83
|
-
idle_in_transaction_session_timeout?: number;
|
|
84
|
-
/**
|
|
85
|
-
* Number of milliseconds before a query call will timeout, default is no timeout.
|
|
86
|
-
*/
|
|
87
|
-
query_timeout?: number;
|
|
88
|
-
/**
|
|
89
|
-
* The name of the application that created this Client instance.
|
|
90
|
-
*/
|
|
91
|
-
application_name?: string;
|
|
13
|
+
pool: PostgresPool | (() => Promise<PostgresPool>);
|
|
92
14
|
/**
|
|
93
15
|
* Called once for each created connection.
|
|
94
|
-
*
|
|
95
|
-
* This is a Kysely specific feature and does not come from the `pg` module.
|
|
96
16
|
*/
|
|
97
17
|
onCreateConnection?: (connection: DatabaseConnection) => Promise<void>;
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* This interface is the subset of pg driver's `Pool` class that
|
|
21
|
+
* kysely needs.
|
|
22
|
+
*
|
|
23
|
+
* We don't use the type from `pg` here to not have a dependency to it.
|
|
24
|
+
*
|
|
25
|
+
* https://node-postgres.com/api/pool
|
|
26
|
+
*/
|
|
27
|
+
export interface PostgresPool {
|
|
28
|
+
connect(): Promise<PostgresPoolClient>;
|
|
29
|
+
end(): Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
export interface PostgresPoolClient {
|
|
32
|
+
query<R>(sql: string, parameters: ReadonlyArray<unknown>): Promise<PostgresQueryResult<R>>;
|
|
33
|
+
release(): void;
|
|
34
|
+
}
|
|
35
|
+
export interface PostgresQueryResult<R> {
|
|
36
|
+
command: 'UPDATE' | 'DELETE' | 'INSERT' | 'SELECT';
|
|
37
|
+
rowCount: number;
|
|
38
|
+
rows: R[];
|
|
103
39
|
}
|
|
@@ -8,12 +8,32 @@ import { PostgresDialectConfig } from './postgres-dialect-config.js';
|
|
|
8
8
|
/**
|
|
9
9
|
* PostgreSQL dialect that uses the [pg](https://node-postgres.com/) library.
|
|
10
10
|
*
|
|
11
|
-
* The {@link PostgresDialectConfig
|
|
12
|
-
* is given as-is to the pg library's [Pool](https://node-postgres.com/api/pool)
|
|
13
|
-
* constructor. See the following two links for more documentation:
|
|
11
|
+
* The constructor takes an instance of {@link PostgresDialectConfig}.
|
|
14
12
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { Pool } from 'pg'
|
|
15
|
+
*
|
|
16
|
+
* new PostgresDialect({
|
|
17
|
+
* pool: new Pool({
|
|
18
|
+
* database: 'some_db',
|
|
19
|
+
* host: 'localhost',
|
|
20
|
+
* })
|
|
21
|
+
* })
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* If you want the pool to only be created once it's first used, `pool`
|
|
25
|
+
* can be a function:
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { Pool } from 'pg'
|
|
29
|
+
*
|
|
30
|
+
* new PostgresDialect({
|
|
31
|
+
* pool: async () => new Pool({
|
|
32
|
+
* database: 'some_db',
|
|
33
|
+
* host: 'localhost',
|
|
34
|
+
* })
|
|
35
|
+
* })
|
|
36
|
+
* ```
|
|
17
37
|
*/
|
|
18
38
|
export declare class PostgresDialect implements Dialect {
|
|
19
39
|
#private;
|
|
@@ -5,21 +5,40 @@ const postgres_driver_js_1 = require("./postgres-driver.js");
|
|
|
5
5
|
const postgres_introspector_js_1 = require("./postgres-introspector.js");
|
|
6
6
|
const postgres_query_compiler_js_1 = require("./postgres-query-compiler.js");
|
|
7
7
|
const postgres_adapter_js_1 = require("./postgres-adapter.js");
|
|
8
|
-
const object_utils_js_1 = require("../../util/object-utils.js");
|
|
9
8
|
/**
|
|
10
9
|
* PostgreSQL dialect that uses the [pg](https://node-postgres.com/) library.
|
|
11
10
|
*
|
|
12
|
-
* The {@link PostgresDialectConfig
|
|
13
|
-
* is given as-is to the pg library's [Pool](https://node-postgres.com/api/pool)
|
|
14
|
-
* constructor. See the following two links for more documentation:
|
|
11
|
+
* The constructor takes an instance of {@link PostgresDialectConfig}.
|
|
15
12
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { Pool } from 'pg'
|
|
15
|
+
*
|
|
16
|
+
* new PostgresDialect({
|
|
17
|
+
* pool: new Pool({
|
|
18
|
+
* database: 'some_db',
|
|
19
|
+
* host: 'localhost',
|
|
20
|
+
* })
|
|
21
|
+
* })
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* If you want the pool to only be created once it's first used, `pool`
|
|
25
|
+
* can be a function:
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { Pool } from 'pg'
|
|
29
|
+
*
|
|
30
|
+
* new PostgresDialect({
|
|
31
|
+
* pool: async () => new Pool({
|
|
32
|
+
* database: 'some_db',
|
|
33
|
+
* host: 'localhost',
|
|
34
|
+
* })
|
|
35
|
+
* })
|
|
36
|
+
* ```
|
|
18
37
|
*/
|
|
19
38
|
class PostgresDialect {
|
|
20
39
|
#config;
|
|
21
40
|
constructor(config) {
|
|
22
|
-
this.#config =
|
|
41
|
+
this.#config = config;
|
|
23
42
|
}
|
|
24
43
|
createDriver() {
|
|
25
44
|
return new postgres_driver_js_1.PostgresDriver(this.#config);
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { DatabaseConnection } from '../../driver/database-connection.js';
|
|
1
|
+
import { DatabaseConnection, QueryResult } from '../../driver/database-connection.js';
|
|
2
2
|
import { Driver, TransactionSettings } from '../../driver/driver.js';
|
|
3
|
-
import {
|
|
3
|
+
import { CompiledQuery } from '../../query-compiler/compiled-query.js';
|
|
4
|
+
import { PostgresDialectConfig, PostgresPoolClient } from './postgres-dialect-config.js';
|
|
5
|
+
declare const PRIVATE_RELEASE_METHOD: unique symbol;
|
|
4
6
|
export declare class PostgresDriver implements Driver {
|
|
5
7
|
#private;
|
|
6
8
|
constructor(config: PostgresDialectConfig);
|
|
@@ -35,9 +37,16 @@ export declare class PostgresDriver implements Driver {
|
|
|
35
37
|
/**
|
|
36
38
|
* Releases a connection back to the pool.
|
|
37
39
|
*/
|
|
38
|
-
releaseConnection(connection:
|
|
40
|
+
releaseConnection(connection: PostgresConnection): Promise<void>;
|
|
39
41
|
/**
|
|
40
42
|
* Destroys the driver and releases all resources.
|
|
41
43
|
*/
|
|
42
44
|
destroy(): Promise<void>;
|
|
43
45
|
}
|
|
46
|
+
declare class PostgresConnection implements DatabaseConnection {
|
|
47
|
+
#private;
|
|
48
|
+
constructor(client: PostgresPoolClient);
|
|
49
|
+
executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>>;
|
|
50
|
+
[PRIVATE_RELEASE_METHOD](): void;
|
|
51
|
+
}
|
|
52
|
+
export {};
|
|
@@ -3,24 +3,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.PostgresDriver = void 0;
|
|
4
4
|
const compiled_query_js_1 = require("../../query-compiler/compiled-query.js");
|
|
5
5
|
const object_utils_js_1 = require("../../util/object-utils.js");
|
|
6
|
+
const stack_trace_utils_js_1 = require("../../util/stack-trace-utils.js");
|
|
6
7
|
const PRIVATE_RELEASE_METHOD = Symbol();
|
|
7
8
|
class PostgresDriver {
|
|
8
9
|
#config;
|
|
9
10
|
#connections = new WeakMap();
|
|
10
|
-
#pool
|
|
11
|
+
#pool;
|
|
11
12
|
constructor(config) {
|
|
12
13
|
this.#config = (0, object_utils_js_1.freeze)({ ...config });
|
|
13
14
|
}
|
|
14
15
|
async init() {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
// top level too, but that's only for types. It doesn't get compiled
|
|
19
|
-
// into javascript. You can check the built javascript code.
|
|
20
|
-
const PoolConstructor = await importPgPool();
|
|
21
|
-
// Use the `pg` module's own pool. All drivers should use the
|
|
22
|
-
// pool provided by the database library if possible.
|
|
23
|
-
this.#pool = new PoolConstructor(this.#config);
|
|
16
|
+
this.#pool = (0, object_utils_js_1.isFunction)(this.#config.pool)
|
|
17
|
+
? await this.#config.pool()
|
|
18
|
+
: this.#config.pool;
|
|
24
19
|
}
|
|
25
20
|
async acquireConnection() {
|
|
26
21
|
const client = await this.#pool.connect();
|
|
@@ -31,7 +26,7 @@ class PostgresDriver {
|
|
|
31
26
|
// The driver must take care of calling `onCreateConnection` when a new
|
|
32
27
|
// connection is created. The `pg` module doesn't provide an async hook
|
|
33
28
|
// for the connection creation. We need to call the method explicitly.
|
|
34
|
-
if (this.#config
|
|
29
|
+
if (this.#config?.onCreateConnection) {
|
|
35
30
|
await this.#config.onCreateConnection(connection);
|
|
36
31
|
}
|
|
37
32
|
}
|
|
@@ -52,53 +47,40 @@ class PostgresDriver {
|
|
|
52
47
|
await connection.executeQuery(compiled_query_js_1.CompiledQuery.raw('rollback'));
|
|
53
48
|
}
|
|
54
49
|
async releaseConnection(connection) {
|
|
55
|
-
|
|
56
|
-
pgConnection[PRIVATE_RELEASE_METHOD]();
|
|
50
|
+
connection[PRIVATE_RELEASE_METHOD]();
|
|
57
51
|
}
|
|
58
52
|
async destroy() {
|
|
59
53
|
if (this.#pool) {
|
|
60
54
|
const pool = this.#pool;
|
|
61
|
-
this.#pool =
|
|
55
|
+
this.#pool = undefined;
|
|
62
56
|
await pool.end();
|
|
63
57
|
}
|
|
64
58
|
}
|
|
65
59
|
}
|
|
66
60
|
exports.PostgresDriver = PostgresDriver;
|
|
67
|
-
async function importPgPool() {
|
|
68
|
-
try {
|
|
69
|
-
// The imported module name must be a string literal to make
|
|
70
|
-
// some bundlers work. So don't move this code behind a helper
|
|
71
|
-
// for example.
|
|
72
|
-
const pgModule = await Promise.resolve().then(() => require('pg'));
|
|
73
|
-
if ((0, object_utils_js_1.isFunction)(pgModule.Pool)) {
|
|
74
|
-
return pgModule.Pool;
|
|
75
|
-
}
|
|
76
|
-
else {
|
|
77
|
-
return pgModule.default.Pool;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
catch (error) {
|
|
81
|
-
throw new Error('Postgres client not installed. Please run `npm install pg`');
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
61
|
class PostgresConnection {
|
|
85
62
|
#client;
|
|
86
63
|
constructor(client) {
|
|
87
64
|
this.#client = client;
|
|
88
65
|
}
|
|
89
66
|
async executeQuery(compiledQuery) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
67
|
+
try {
|
|
68
|
+
const result = await this.#client.query(compiledQuery.sql, [
|
|
69
|
+
...compiledQuery.parameters,
|
|
70
|
+
]);
|
|
71
|
+
if (result.command === 'UPDATE' || result.command === 'DELETE') {
|
|
72
|
+
return {
|
|
73
|
+
numUpdatedOrDeletedRows: BigInt(result.rowCount),
|
|
74
|
+
rows: result.rows ?? [],
|
|
75
|
+
};
|
|
76
|
+
}
|
|
94
77
|
return {
|
|
95
|
-
numUpdatedOrDeletedRows: BigInt(result.rowCount),
|
|
96
78
|
rows: result.rows ?? [],
|
|
97
79
|
};
|
|
98
80
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
throw (0, stack_trace_utils_js_1.extendStackTrace)(err, new Error());
|
|
83
|
+
}
|
|
102
84
|
}
|
|
103
85
|
[PRIVATE_RELEASE_METHOD]() {
|
|
104
86
|
this.#client.release();
|
|
@@ -1,10 +1,20 @@
|
|
|
1
|
-
import { DatabaseIntrospector, DatabaseMetadata, DatabaseMetadataOptions } from '../database-introspector.js';
|
|
1
|
+
import { DatabaseIntrospector, DatabaseMetadata, DatabaseMetadataOptions, SchemaMetadata, TableMetadata } from '../database-introspector.js';
|
|
2
2
|
import { Kysely } from '../../kysely.js';
|
|
3
3
|
export declare class PostgresIntrospector implements DatabaseIntrospector {
|
|
4
4
|
#private;
|
|
5
5
|
constructor(db: Kysely<any>);
|
|
6
|
+
/**
|
|
7
|
+
* Get schema metadata.
|
|
8
|
+
*/
|
|
9
|
+
getSchemas(): Promise<SchemaMetadata[]>;
|
|
10
|
+
/**
|
|
11
|
+
* Get table metadata.
|
|
12
|
+
*/
|
|
13
|
+
getTables(options?: DatabaseMetadataOptions): Promise<TableMetadata[]>;
|
|
6
14
|
/**
|
|
7
15
|
* Get the database metadata such as table and column names.
|
|
16
|
+
*
|
|
17
|
+
* @deprecated Use getTables() instead.
|
|
8
18
|
*/
|
|
9
19
|
getMetadata(options?: DatabaseMetadataOptions): Promise<DatabaseMetadata>;
|
|
10
20
|
}
|
|
@@ -8,7 +8,15 @@ class PostgresIntrospector {
|
|
|
8
8
|
constructor(db) {
|
|
9
9
|
this.#db = db;
|
|
10
10
|
}
|
|
11
|
-
async
|
|
11
|
+
async getSchemas() {
|
|
12
|
+
let rawSchemas = await this.#db
|
|
13
|
+
.selectFrom('pg_catalog.pg_namespace')
|
|
14
|
+
.select('nspname')
|
|
15
|
+
.castTo()
|
|
16
|
+
.execute();
|
|
17
|
+
return rawSchemas.map((it) => ({ name: it.nspname }));
|
|
18
|
+
}
|
|
19
|
+
async getTables(options = { withInternalKyselyTables: false }) {
|
|
12
20
|
let query = this.#db
|
|
13
21
|
.selectFrom('pg_catalog.pg_attribute as a')
|
|
14
22
|
.innerJoin('pg_catalog.pg_class as c', 'a.attrelid', 'c.oid')
|
|
@@ -28,12 +36,15 @@ class PostgresIntrospector {
|
|
|
28
36
|
.castTo();
|
|
29
37
|
if (!options.withInternalKyselyTables) {
|
|
30
38
|
query = query
|
|
31
|
-
.where('t.tablename', '!=', migrator_js_1.
|
|
32
|
-
.where('t.tablename', '!=', migrator_js_1.
|
|
39
|
+
.where('t.tablename', '!=', migrator_js_1.DEFAULT_MIGRATION_TABLE)
|
|
40
|
+
.where('t.tablename', '!=', migrator_js_1.DEFAULT_MIGRATION_LOCK_TABLE);
|
|
33
41
|
}
|
|
34
42
|
const rawColumns = await query.execute();
|
|
43
|
+
return this.#parseTableMetadata(rawColumns);
|
|
44
|
+
}
|
|
45
|
+
async getMetadata(options) {
|
|
35
46
|
return {
|
|
36
|
-
tables: this
|
|
47
|
+
tables: await this.getTables(options),
|
|
37
48
|
};
|
|
38
49
|
}
|
|
39
50
|
#parseTableMetadata(columns) {
|
|
@@ -1,39 +1,16 @@
|
|
|
1
1
|
import { DatabaseConnection } from '../../driver/database-connection.js';
|
|
2
2
|
/**
|
|
3
3
|
* Config for the SQLite dialect.
|
|
4
|
-
*
|
|
5
|
-
* This interface is equal to `better-sqlite3` library's `Databse` class's constructor parameters.
|
|
6
|
-
*
|
|
7
|
-
* https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#new-databasepath-options
|
|
8
4
|
*/
|
|
9
5
|
export interface SqliteDialectConfig {
|
|
10
6
|
/**
|
|
11
|
-
*
|
|
7
|
+
* An sqlite Database instance or a function that returns one.
|
|
12
8
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
|
|
16
|
-
databasePath: string;
|
|
17
|
-
/**
|
|
18
|
-
* Open the database connection in readonly mode (default: false)
|
|
19
|
-
*/
|
|
20
|
-
readonly?: boolean;
|
|
21
|
-
/**
|
|
22
|
-
* If the database does not exist, an Error will be thrown instead of creating a new file.
|
|
23
|
-
* This option is ignored for in-memory, temporary, or readonly database connections
|
|
24
|
-
* (default: false)
|
|
25
|
-
*/
|
|
26
|
-
fileMustExist?: boolean;
|
|
27
|
-
/**
|
|
28
|
-
* The number of milliseconds to wait when executing queries on a locked database,
|
|
29
|
-
* before throwing a SQLITE_BUSY error (default: 5000)
|
|
30
|
-
*/
|
|
31
|
-
timeout?: number;
|
|
32
|
-
/**
|
|
33
|
-
* Provide a function that gets called with every SQL string executed by the database connection
|
|
34
|
-
* (default: null)
|
|
9
|
+
* If a function is provided, it's called once when the first query is executed.
|
|
10
|
+
*
|
|
11
|
+
* https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#new-databasepath-options
|
|
35
12
|
*/
|
|
36
|
-
|
|
13
|
+
database: SqliteDatabase | (() => Promise<SqliteDatabase>);
|
|
37
14
|
/**
|
|
38
15
|
* Called once when the first query is executed.
|
|
39
16
|
*
|
|
@@ -41,3 +18,23 @@ export interface SqliteDialectConfig {
|
|
|
41
18
|
*/
|
|
42
19
|
onCreateConnection?: (connection: DatabaseConnection) => Promise<void>;
|
|
43
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* This interface is the subset of better-sqlite3 driver's `Database` class that
|
|
23
|
+
* kysely needs.
|
|
24
|
+
*
|
|
25
|
+
* We don't use the type from `better-sqlite3` here to not have a dependency to it.
|
|
26
|
+
*
|
|
27
|
+
* https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#new-databasepath-options
|
|
28
|
+
*/
|
|
29
|
+
export interface SqliteDatabase {
|
|
30
|
+
close(): void;
|
|
31
|
+
prepare(sql: string): SqliteStatement;
|
|
32
|
+
}
|
|
33
|
+
export interface SqliteStatement {
|
|
34
|
+
readonly reader: boolean;
|
|
35
|
+
all(parameters: ReadonlyArray<unknown>): unknown[];
|
|
36
|
+
run(parameters: ReadonlyArray<unknown>): {
|
|
37
|
+
changes: number | bigint;
|
|
38
|
+
lastInsertRowid: number | bigint;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -8,9 +8,25 @@ import { SqliteDialectConfig } from './sqlite-dialect-config.js';
|
|
|
8
8
|
/**
|
|
9
9
|
* SQLite dialect that uses the [better-sqlite3](https://github.com/JoshuaWise/better-sqlite3) library.
|
|
10
10
|
*
|
|
11
|
-
* The
|
|
12
|
-
*
|
|
13
|
-
*
|
|
11
|
+
* The constructor takes an instance of {@link SqliteDialectConfig}.
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import Database from 'better-sqlite3'
|
|
15
|
+
*
|
|
16
|
+
* new SqliteDialect({
|
|
17
|
+
* database: new Database('db.sqlite')
|
|
18
|
+
* })
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* If you want the pool to only be created once it's first used, `database`
|
|
22
|
+
* can be a function:
|
|
23
|
+
*
|
|
24
|
+
* ```ts
|
|
25
|
+
* import Database from 'better-sqlite3'
|
|
26
|
+
*
|
|
27
|
+
* new SqliteDialect({
|
|
28
|
+
* database: async () => new Database('db.sqlite')
|
|
29
|
+
* })
|
|
14
30
|
*/
|
|
15
31
|
export declare class SqliteDialect implements Dialect {
|
|
16
32
|
#private;
|
|
@@ -9,9 +9,25 @@ const object_utils_js_1 = require("../../util/object-utils.js");
|
|
|
9
9
|
/**
|
|
10
10
|
* SQLite dialect that uses the [better-sqlite3](https://github.com/JoshuaWise/better-sqlite3) library.
|
|
11
11
|
*
|
|
12
|
-
* The
|
|
13
|
-
*
|
|
14
|
-
*
|
|
12
|
+
* The constructor takes an instance of {@link SqliteDialectConfig}.
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* import Database from 'better-sqlite3'
|
|
16
|
+
*
|
|
17
|
+
* new SqliteDialect({
|
|
18
|
+
* database: new Database('db.sqlite')
|
|
19
|
+
* })
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* If you want the pool to only be created once it's first used, `database`
|
|
23
|
+
* can be a function:
|
|
24
|
+
*
|
|
25
|
+
* ```ts
|
|
26
|
+
* import Database from 'better-sqlite3'
|
|
27
|
+
*
|
|
28
|
+
* new SqliteDialect({
|
|
29
|
+
* database: async () => new Database('db.sqlite')
|
|
30
|
+
* })
|
|
15
31
|
*/
|
|
16
32
|
class SqliteDialect {
|
|
17
33
|
#config;
|
|
@@ -12,26 +12,9 @@ class SqliteDriver {
|
|
|
12
12
|
this.#config = (0, object_utils_js_1.freeze)({ ...config });
|
|
13
13
|
}
|
|
14
14
|
async init() {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
// the top level too, but that's only for types. It doesn't get compiled
|
|
19
|
-
// into javascript. You can check the built javascript code.
|
|
20
|
-
const DatabaseConstructor = await importBetterSqlite3Database();
|
|
21
|
-
const options = {};
|
|
22
|
-
if ((0, object_utils_js_1.isBoolean)(this.#config.readonly)) {
|
|
23
|
-
options.readonly = this.#config.readonly;
|
|
24
|
-
}
|
|
25
|
-
if ((0, object_utils_js_1.isBoolean)(this.#config.fileMustExist)) {
|
|
26
|
-
options.fileMustExist = this.#config.fileMustExist;
|
|
27
|
-
}
|
|
28
|
-
if ((0, object_utils_js_1.isNumber)(this.#config.timeout)) {
|
|
29
|
-
options.timeout = this.#config.timeout;
|
|
30
|
-
}
|
|
31
|
-
if ((0, object_utils_js_1.isFunction)(this.#config.verbose)) {
|
|
32
|
-
options.verbose = this.#config.verbose;
|
|
33
|
-
}
|
|
34
|
-
this.#db = new DatabaseConstructor(this.#config.databasePath, options);
|
|
15
|
+
this.#db = (0, object_utils_js_1.isFunction)(this.#config.database)
|
|
16
|
+
? await this.#config.database()
|
|
17
|
+
: this.#config.database;
|
|
35
18
|
this.#connection = new SqliteConnection(this.#db);
|
|
36
19
|
if (this.#config.onCreateConnection) {
|
|
37
20
|
await this.#config.onCreateConnection(this.#connection);
|
|
@@ -60,23 +43,6 @@ class SqliteDriver {
|
|
|
60
43
|
}
|
|
61
44
|
}
|
|
62
45
|
exports.SqliteDriver = SqliteDriver;
|
|
63
|
-
async function importBetterSqlite3Database() {
|
|
64
|
-
try {
|
|
65
|
-
// The imported module name must be a string literal to make
|
|
66
|
-
// some bundlers work. So don't move this code behind a helper
|
|
67
|
-
// for example.
|
|
68
|
-
const sqliteModule = await Promise.resolve().then(() => require('better-sqlite3'));
|
|
69
|
-
if ((0, object_utils_js_1.isFunction)(sqliteModule)) {
|
|
70
|
-
return sqliteModule;
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
return sqliteModule.default;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
catch (error) {
|
|
77
|
-
throw new Error('SQLite client not installed. Please run `npm install better-sqlite3`');
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
46
|
class SqliteConnection {
|
|
81
47
|
#db;
|
|
82
48
|
constructor(db) {
|