kysely 0.18.0 → 0.18.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/dialect/database-introspector.d.ts +13 -0
- package/dist/cjs/dialect/mysql/mysql-dialect.d.ts +9 -1
- package/dist/cjs/dialect/mysql/mysql-dialect.js +11 -5
- package/dist/cjs/dialect/mysql/mysql-driver.d.ts +25 -3
- package/dist/cjs/dialect/mysql/mysql-driver.js +48 -35
- 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.d.ts +9 -1
- package/dist/cjs/dialect/postgres/postgres-dialect.js +11 -5
- package/dist/cjs/dialect/postgres/postgres-driver.d.ts +25 -3
- package/dist/cjs/dialect/postgres/postgres-driver.js +37 -24
- 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-introspector.d.ts +11 -1
- package/dist/cjs/dialect/sqlite/sqlite-introspector.js +11 -4
- package/dist/cjs/index-nodeless.js +5 -1
- package/dist/cjs/index.js +5 -1
- package/dist/cjs/migration/migrator.d.ts +44 -2
- package/dist/cjs/migration/migrator.js +77 -18
- package/dist/cjs/plugin/noop-plugin.d.ts +42 -0
- package/dist/cjs/plugin/noop-plugin.js +12 -0
- package/dist/cjs/query-creator.d.ts +9 -0
- package/dist/cjs/query-creator.js +18 -0
- 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.d.ts +9 -1
- package/dist/esm/dialect/mysql/mysql-dialect.js +11 -5
- package/dist/esm/dialect/mysql/mysql-driver.d.ts +25 -3
- package/dist/esm/dialect/mysql/mysql-driver.js +48 -35
- 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.d.ts +9 -1
- package/dist/esm/dialect/postgres/postgres-dialect.js +11 -5
- package/dist/esm/dialect/postgres/postgres-driver.d.ts +25 -3
- package/dist/esm/dialect/postgres/postgres-driver.js +38 -25
- 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-introspector.d.ts +11 -1
- package/dist/esm/dialect/sqlite/sqlite-introspector.js +12 -5
- package/dist/esm/migration/migrator.d.ts +44 -2
- package/dist/esm/migration/migrator.js +76 -17
- package/dist/esm/plugin/noop-plugin.d.ts +42 -0
- package/dist/esm/plugin/noop-plugin.js +9 -0
- package/dist/esm/query-creator.d.ts +9 -0
- package/dist/esm/query-creator.js +18 -0
- 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 +9 -9
|
@@ -3,8 +3,18 @@ import { ColumnDataType } from '../operation-node/data-type-node.js';
|
|
|
3
3
|
* An interface for getting the database metadata (names of the tables and columns etc.)
|
|
4
4
|
*/
|
|
5
5
|
export interface DatabaseIntrospector {
|
|
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
|
}
|
|
@@ -15,6 +25,9 @@ export interface DatabaseMetadataOptions {
|
|
|
15
25
|
*/
|
|
16
26
|
withInternalKyselyTables: boolean;
|
|
17
27
|
}
|
|
28
|
+
export interface SchemaMetadata {
|
|
29
|
+
readonly name: string;
|
|
30
|
+
}
|
|
18
31
|
export interface DatabaseMetadata {
|
|
19
32
|
/**
|
|
20
33
|
* The tables found in the database.
|
|
@@ -3,6 +3,7 @@ import { Kysely } from '../../kysely.js';
|
|
|
3
3
|
import { QueryCompiler } from '../../query-compiler/query-compiler.js';
|
|
4
4
|
import { Dialect } from '../dialect.js';
|
|
5
5
|
import { DatabaseIntrospector } from '../database-introspector.js';
|
|
6
|
+
import { MysqlPool } from './mysql-driver.js';
|
|
6
7
|
import { DialectAdapter } from '../dialect-adapter.js';
|
|
7
8
|
import { MysqlDialectConfig } from './mysql-dialect-config.js';
|
|
8
9
|
/**
|
|
@@ -11,10 +12,17 @@ import { MysqlDialectConfig } from './mysql-dialect-config.js';
|
|
|
11
12
|
* The {@link MysqlDialectConfig | configuration} passed to the constructor
|
|
12
13
|
* is given as-is to the mysql2 library's [createPool](https://github.com/sidorares/node-mysql2#using-connection-pools)
|
|
13
14
|
* method.
|
|
15
|
+
*
|
|
16
|
+
* You can also provide an existing mysql2 `Pool` instance:
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { createPool } from 'mysql2'
|
|
20
|
+
* new MysqlDialect(createPool(config))
|
|
21
|
+
* ```
|
|
14
22
|
*/
|
|
15
23
|
export declare class MysqlDialect implements Dialect {
|
|
16
24
|
#private;
|
|
17
|
-
constructor(
|
|
25
|
+
constructor(configOrPool: MysqlDialectConfig | MysqlPool);
|
|
18
26
|
/**
|
|
19
27
|
* Creates a driver for the dialect.
|
|
20
28
|
*/
|
|
@@ -5,21 +5,27 @@ const mysql_driver_js_1 = require("./mysql-driver.js");
|
|
|
5
5
|
const mysql_query_compiler_js_1 = require("./mysql-query-compiler.js");
|
|
6
6
|
const mysql_introspector_js_1 = require("./mysql-introspector.js");
|
|
7
7
|
const mysql_adapter_js_1 = require("./mysql-adapter.js");
|
|
8
|
-
const object_utils_js_1 = require("../../util/object-utils.js");
|
|
9
8
|
/**
|
|
10
9
|
* MySQL dialect that uses the [mysql2](https://github.com/sidorares/node-mysql2#readme) library.
|
|
11
10
|
*
|
|
12
11
|
* The {@link MysqlDialectConfig | configuration} passed to the constructor
|
|
13
12
|
* is given as-is to the mysql2 library's [createPool](https://github.com/sidorares/node-mysql2#using-connection-pools)
|
|
14
13
|
* method.
|
|
14
|
+
*
|
|
15
|
+
* You can also provide an existing mysql2 `Pool` instance:
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { createPool } from 'mysql2'
|
|
19
|
+
* new MysqlDialect(createPool(config))
|
|
20
|
+
* ```
|
|
15
21
|
*/
|
|
16
22
|
class MysqlDialect {
|
|
17
|
-
#
|
|
18
|
-
constructor(
|
|
19
|
-
this.#
|
|
23
|
+
#configOrPool;
|
|
24
|
+
constructor(configOrPool) {
|
|
25
|
+
this.#configOrPool = configOrPool;
|
|
20
26
|
}
|
|
21
27
|
createDriver() {
|
|
22
|
-
return new mysql_driver_js_1.MysqlDriver(this.#
|
|
28
|
+
return new mysql_driver_js_1.MysqlDriver(this.#configOrPool);
|
|
23
29
|
}
|
|
24
30
|
createQueryCompiler() {
|
|
25
31
|
return new mysql_query_compiler_js_1.MysqlQueryCompiler();
|
|
@@ -1,9 +1,11 @@
|
|
|
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 { CompiledQuery } from '../../query-compiler/compiled-query.js';
|
|
3
4
|
import { MysqlDialectConfig } from './mysql-dialect-config.js';
|
|
5
|
+
declare const PRIVATE_RELEASE_METHOD: unique symbol;
|
|
4
6
|
export declare class MysqlDriver implements Driver {
|
|
5
7
|
#private;
|
|
6
|
-
constructor(
|
|
8
|
+
constructor(configOrPool: MysqlDialectConfig | MysqlPool);
|
|
7
9
|
/**
|
|
8
10
|
* Initializes the driver.
|
|
9
11
|
*
|
|
@@ -35,9 +37,29 @@ export declare class MysqlDriver implements Driver {
|
|
|
35
37
|
/**
|
|
36
38
|
* Releases a connection back to the pool.
|
|
37
39
|
*/
|
|
38
|
-
releaseConnection(connection:
|
|
40
|
+
releaseConnection(connection: MysqlConnection): Promise<void>;
|
|
39
41
|
/**
|
|
40
42
|
* Destroys the driver and releases all resources.
|
|
41
43
|
*/
|
|
42
44
|
destroy(): Promise<void>;
|
|
43
45
|
}
|
|
46
|
+
export interface MysqlPool {
|
|
47
|
+
getConnection(callback: (error: unknown, connection: MysqlPoolConnection) => void): void;
|
|
48
|
+
end(callback: (error: unknown) => void): void;
|
|
49
|
+
}
|
|
50
|
+
interface MysqlPoolConnection {
|
|
51
|
+
query(sql: string, parameters: ReadonlyArray<unknown>, callback: (error: unknown, result: MysqlQueryResult) => void): void;
|
|
52
|
+
release(): void;
|
|
53
|
+
}
|
|
54
|
+
interface MysqlOkPacket {
|
|
55
|
+
affectedRows: number;
|
|
56
|
+
insertId: number;
|
|
57
|
+
}
|
|
58
|
+
declare type MysqlQueryResult = MysqlOkPacket | Record<string, unknown>[];
|
|
59
|
+
declare class MysqlConnection implements DatabaseConnection {
|
|
60
|
+
#private;
|
|
61
|
+
constructor(rawConnection: MysqlPoolConnection);
|
|
62
|
+
executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>>;
|
|
63
|
+
[PRIVATE_RELEASE_METHOD](): void;
|
|
64
|
+
}
|
|
65
|
+
export {};
|
|
@@ -3,24 +3,30 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.MysqlDriver = 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 MysqlDriver {
|
|
8
9
|
#config;
|
|
9
10
|
#connections = new WeakMap();
|
|
10
11
|
#pool;
|
|
11
|
-
constructor(
|
|
12
|
-
|
|
12
|
+
constructor(configOrPool) {
|
|
13
|
+
if (isMysqlPool(configOrPool)) {
|
|
14
|
+
this.#pool = configOrPool;
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
this.#config = (0, object_utils_js_1.freeze)({ ...configOrPool });
|
|
18
|
+
}
|
|
13
19
|
}
|
|
14
20
|
async init() {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
if (this.#config) {
|
|
22
|
+
// Import the `mysql2` module here instead at the top of the file
|
|
23
|
+
// so that this file can be loaded by node without `mysql2` driver
|
|
24
|
+
// installed.
|
|
25
|
+
const poolFactory = await importMysqlPoolFactory();
|
|
26
|
+
// Use the `mysql2` module's own pool. All drivers should use the
|
|
27
|
+
// pool provided by the database library if possible.
|
|
28
|
+
this.#pool = poolFactory(this.#config);
|
|
29
|
+
}
|
|
24
30
|
}
|
|
25
31
|
async acquireConnection() {
|
|
26
32
|
const rawConnection = await this.#acquireConnection();
|
|
@@ -31,7 +37,7 @@ class MysqlDriver {
|
|
|
31
37
|
// The driver must take care of calling `onCreateConnection` when a new
|
|
32
38
|
// connection is created. The `mysql2` module doesn't provide an async hook
|
|
33
39
|
// for the connection creation. We need to call the method explicitly.
|
|
34
|
-
if (this.#config
|
|
40
|
+
if (this.#config?.onCreateConnection) {
|
|
35
41
|
await this.#config.onCreateConnection(connection);
|
|
36
42
|
}
|
|
37
43
|
}
|
|
@@ -63,8 +69,7 @@ class MysqlDriver {
|
|
|
63
69
|
await connection.executeQuery(compiled_query_js_1.CompiledQuery.raw('rollback'));
|
|
64
70
|
}
|
|
65
71
|
async releaseConnection(connection) {
|
|
66
|
-
|
|
67
|
-
mysqlConnection[PRIVATE_RELEASE_METHOD]();
|
|
72
|
+
connection[PRIVATE_RELEASE_METHOD]();
|
|
68
73
|
}
|
|
69
74
|
async destroy() {
|
|
70
75
|
return new Promise((resolve, reject) => {
|
|
@@ -80,6 +85,12 @@ class MysqlDriver {
|
|
|
80
85
|
}
|
|
81
86
|
}
|
|
82
87
|
exports.MysqlDriver = MysqlDriver;
|
|
88
|
+
function isMysqlPool(obj) {
|
|
89
|
+
return (0, object_utils_js_1.isObject)(obj) && (0, object_utils_js_1.isFunction)(obj.getConnection) && (0, object_utils_js_1.isFunction)(obj.end);
|
|
90
|
+
}
|
|
91
|
+
function isOkPacket(obj) {
|
|
92
|
+
return (0, object_utils_js_1.isObject)(obj) && 'insertId' in obj && 'affectedRows' in obj;
|
|
93
|
+
}
|
|
83
94
|
async function importMysqlPoolFactory() {
|
|
84
95
|
try {
|
|
85
96
|
// The imported module name must be a string literal to make
|
|
@@ -103,29 +114,34 @@ class MysqlConnection {
|
|
|
103
114
|
this.#rawConnection = rawConnection;
|
|
104
115
|
}
|
|
105
116
|
async executeQuery(compiledQuery) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
117
|
+
try {
|
|
118
|
+
const result = await this.#executeQuery(compiledQuery);
|
|
119
|
+
if (isOkPacket(result)) {
|
|
120
|
+
const { insertId, affectedRows } = result;
|
|
121
|
+
return {
|
|
122
|
+
insertId: insertId !== undefined &&
|
|
123
|
+
insertId !== null &&
|
|
124
|
+
insertId.toString() !== '0'
|
|
125
|
+
? BigInt(insertId)
|
|
126
|
+
: undefined,
|
|
127
|
+
numUpdatedOrDeletedRows: affectedRows !== undefined && insertId !== null
|
|
128
|
+
? BigInt(affectedRows)
|
|
129
|
+
: undefined,
|
|
130
|
+
rows: [],
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
else if (Array.isArray(result)) {
|
|
134
|
+
return {
|
|
135
|
+
rows: result,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
109
138
|
return {
|
|
110
|
-
insertId: insertId !== undefined &&
|
|
111
|
-
insertId !== null &&
|
|
112
|
-
insertId.toString() !== '0'
|
|
113
|
-
? BigInt(insertId)
|
|
114
|
-
: undefined,
|
|
115
|
-
numUpdatedOrDeletedRows: affectedRows !== undefined && insertId !== null
|
|
116
|
-
? BigInt(affectedRows)
|
|
117
|
-
: undefined,
|
|
118
139
|
rows: [],
|
|
119
140
|
};
|
|
120
141
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
rows: result,
|
|
124
|
-
};
|
|
142
|
+
catch (err) {
|
|
143
|
+
throw (0, stack_trace_utils_js_1.extendStackTrace)(err, new Error());
|
|
125
144
|
}
|
|
126
|
-
return {
|
|
127
|
-
rows: [],
|
|
128
|
-
};
|
|
129
145
|
}
|
|
130
146
|
#executeQuery(compiledQuery) {
|
|
131
147
|
return new Promise((resolve, reject) => {
|
|
@@ -143,6 +159,3 @@ class MysqlConnection {
|
|
|
143
159
|
this.#rawConnection.release();
|
|
144
160
|
}
|
|
145
161
|
}
|
|
146
|
-
function isOkPacket(obj) {
|
|
147
|
-
return (0, object_utils_js_1.isObject)(obj) && 'insertId' in obj && 'affectedRows' in obj;
|
|
148
|
-
}
|
|
@@ -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 MysqlIntrospector 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
|
}
|
|
@@ -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);
|
|
@@ -2,6 +2,7 @@ import { Driver } from '../../driver/driver.js';
|
|
|
2
2
|
import { Kysely } from '../../kysely.js';
|
|
3
3
|
import { QueryCompiler } from '../../query-compiler/query-compiler.js';
|
|
4
4
|
import { Dialect } from '../dialect.js';
|
|
5
|
+
import { PostgresPool } from './postgres-driver.js';
|
|
5
6
|
import { DatabaseIntrospector } from '../database-introspector.js';
|
|
6
7
|
import { DialectAdapter } from '../dialect-adapter.js';
|
|
7
8
|
import { PostgresDialectConfig } from './postgres-dialect-config.js';
|
|
@@ -14,10 +15,17 @@ import { PostgresDialectConfig } from './postgres-dialect-config.js';
|
|
|
14
15
|
*
|
|
15
16
|
* https://node-postgres.com/api/pool
|
|
16
17
|
* https://node-postgres.com/api/client
|
|
18
|
+
*
|
|
19
|
+
* You can also provide an existing pg `Pool` instance:
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { Pool } from 'pg'
|
|
23
|
+
* new PostgresDialect(new Pool(config))
|
|
24
|
+
* ```
|
|
17
25
|
*/
|
|
18
26
|
export declare class PostgresDialect implements Dialect {
|
|
19
27
|
#private;
|
|
20
|
-
constructor(
|
|
28
|
+
constructor(configOrPool: PostgresDialectConfig | PostgresPool);
|
|
21
29
|
/**
|
|
22
30
|
* Creates a driver for the dialect.
|
|
23
31
|
*/
|
|
@@ -5,7 +5,6 @@ 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
|
*
|
|
@@ -15,14 +14,21 @@ const object_utils_js_1 = require("../../util/object-utils.js");
|
|
|
15
14
|
*
|
|
16
15
|
* https://node-postgres.com/api/pool
|
|
17
16
|
* https://node-postgres.com/api/client
|
|
17
|
+
*
|
|
18
|
+
* You can also provide an existing pg `Pool` instance:
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { Pool } from 'pg'
|
|
22
|
+
* new PostgresDialect(new Pool(config))
|
|
23
|
+
* ```
|
|
18
24
|
*/
|
|
19
25
|
class PostgresDialect {
|
|
20
|
-
#
|
|
21
|
-
constructor(
|
|
22
|
-
this.#
|
|
26
|
+
#configOrPool;
|
|
27
|
+
constructor(configOrPool) {
|
|
28
|
+
this.#configOrPool = configOrPool;
|
|
23
29
|
}
|
|
24
30
|
createDriver() {
|
|
25
|
-
return new postgres_driver_js_1.PostgresDriver(this.#
|
|
31
|
+
return new postgres_driver_js_1.PostgresDriver(this.#configOrPool);
|
|
26
32
|
}
|
|
27
33
|
createQueryCompiler() {
|
|
28
34
|
return new postgres_query_compiler_js_1.PostgresQueryCompiler();
|
|
@@ -1,9 +1,11 @@
|
|
|
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 { CompiledQuery } from '../../query-compiler/compiled-query.js';
|
|
3
4
|
import { PostgresDialectConfig } 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
|
-
constructor(
|
|
8
|
+
constructor(configOrPool: PostgresDialectConfig | PostgresPool);
|
|
7
9
|
/**
|
|
8
10
|
* Initializes the driver.
|
|
9
11
|
*
|
|
@@ -35,9 +37,29 @@ 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
|
+
export interface PostgresPool {
|
|
47
|
+
connect(): Promise<PostgresPoolClient>;
|
|
48
|
+
end(): Promise<void>;
|
|
49
|
+
}
|
|
50
|
+
interface PostgresPoolClient {
|
|
51
|
+
query<R>(sql: string, parameters: ReadonlyArray<unknown>): Promise<PostgresQueryResult<R>>;
|
|
52
|
+
release(): void;
|
|
53
|
+
}
|
|
54
|
+
interface PostgresQueryResult<R> {
|
|
55
|
+
command: 'UPDATE' | 'DELETE' | 'INSERT' | 'SELECT';
|
|
56
|
+
rowCount: number;
|
|
57
|
+
rows: R[];
|
|
58
|
+
}
|
|
59
|
+
declare class PostgresConnection implements DatabaseConnection {
|
|
60
|
+
#private;
|
|
61
|
+
constructor(client: PostgresPoolClient);
|
|
62
|
+
executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>>;
|
|
63
|
+
[PRIVATE_RELEASE_METHOD](): void;
|
|
64
|
+
}
|
|
65
|
+
export {};
|
|
@@ -3,24 +3,30 @@ 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
|
-
constructor(
|
|
12
|
-
|
|
11
|
+
#pool;
|
|
12
|
+
constructor(configOrPool) {
|
|
13
|
+
if (isPostgresPool(configOrPool)) {
|
|
14
|
+
this.#pool = configOrPool;
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
this.#config = (0, object_utils_js_1.freeze)({ ...configOrPool });
|
|
18
|
+
}
|
|
13
19
|
}
|
|
14
20
|
async init() {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
if (this.#config) {
|
|
22
|
+
// Import the `pg` module here instead at the top of the file
|
|
23
|
+
// so that this file can be loaded by node without `pg` driver
|
|
24
|
+
// installed.
|
|
25
|
+
const PoolConstructor = await importPgPool();
|
|
26
|
+
// Use the `pg` module's own pool. All drivers should use the
|
|
27
|
+
// pool provided by the database library if possible.
|
|
28
|
+
this.#pool = new PoolConstructor(this.#config);
|
|
29
|
+
}
|
|
24
30
|
}
|
|
25
31
|
async acquireConnection() {
|
|
26
32
|
const client = await this.#pool.connect();
|
|
@@ -31,7 +37,7 @@ class PostgresDriver {
|
|
|
31
37
|
// The driver must take care of calling `onCreateConnection` when a new
|
|
32
38
|
// connection is created. The `pg` module doesn't provide an async hook
|
|
33
39
|
// for the connection creation. We need to call the method explicitly.
|
|
34
|
-
if (this.#config
|
|
40
|
+
if (this.#config?.onCreateConnection) {
|
|
35
41
|
await this.#config.onCreateConnection(connection);
|
|
36
42
|
}
|
|
37
43
|
}
|
|
@@ -52,18 +58,20 @@ class PostgresDriver {
|
|
|
52
58
|
await connection.executeQuery(compiled_query_js_1.CompiledQuery.raw('rollback'));
|
|
53
59
|
}
|
|
54
60
|
async releaseConnection(connection) {
|
|
55
|
-
|
|
56
|
-
pgConnection[PRIVATE_RELEASE_METHOD]();
|
|
61
|
+
connection[PRIVATE_RELEASE_METHOD]();
|
|
57
62
|
}
|
|
58
63
|
async destroy() {
|
|
59
64
|
if (this.#pool) {
|
|
60
65
|
const pool = this.#pool;
|
|
61
|
-
this.#pool =
|
|
66
|
+
this.#pool = undefined;
|
|
62
67
|
await pool.end();
|
|
63
68
|
}
|
|
64
69
|
}
|
|
65
70
|
}
|
|
66
71
|
exports.PostgresDriver = PostgresDriver;
|
|
72
|
+
function isPostgresPool(obj) {
|
|
73
|
+
return (0, object_utils_js_1.isObject)(obj) && (0, object_utils_js_1.isFunction)(obj.connect) && (0, object_utils_js_1.isFunction)(obj.end);
|
|
74
|
+
}
|
|
67
75
|
async function importPgPool() {
|
|
68
76
|
try {
|
|
69
77
|
// The imported module name must be a string literal to make
|
|
@@ -87,18 +95,23 @@ class PostgresConnection {
|
|
|
87
95
|
this.#client = client;
|
|
88
96
|
}
|
|
89
97
|
async executeQuery(compiledQuery) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
98
|
+
try {
|
|
99
|
+
const result = await this.#client.query(compiledQuery.sql, [
|
|
100
|
+
...compiledQuery.parameters,
|
|
101
|
+
]);
|
|
102
|
+
if (result.command === 'UPDATE' || result.command === 'DELETE') {
|
|
103
|
+
return {
|
|
104
|
+
numUpdatedOrDeletedRows: BigInt(result.rowCount),
|
|
105
|
+
rows: result.rows ?? [],
|
|
106
|
+
};
|
|
107
|
+
}
|
|
94
108
|
return {
|
|
95
|
-
numUpdatedOrDeletedRows: BigInt(result.rowCount),
|
|
96
109
|
rows: result.rows ?? [],
|
|
97
110
|
};
|
|
98
111
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
112
|
+
catch (err) {
|
|
113
|
+
throw (0, stack_trace_utils_js_1.extendStackTrace)(err, new Error());
|
|
114
|
+
}
|
|
102
115
|
}
|
|
103
116
|
[PRIVATE_RELEASE_METHOD]() {
|
|
104
117
|
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,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 SqliteIntrospector 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
|
}
|