kysely 0.17.2 → 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.
Files changed (94) hide show
  1. package/README.md +2 -0
  2. package/dist/cjs/dialect/database-introspector.d.ts +13 -0
  3. package/dist/cjs/dialect/mysql/mysql-dialect-config.d.ts +19 -19
  4. package/dist/cjs/dialect/mysql/mysql-dialect.d.ts +9 -1
  5. package/dist/cjs/dialect/mysql/mysql-dialect.js +11 -5
  6. package/dist/cjs/dialect/mysql/mysql-driver.d.ts +25 -3
  7. package/dist/cjs/dialect/mysql/mysql-driver.js +48 -35
  8. package/dist/cjs/dialect/mysql/mysql-introspector.d.ts +11 -1
  9. package/dist/cjs/dialect/mysql/mysql-introspector.js +29 -7
  10. package/dist/cjs/dialect/postgres/postgres-dialect.d.ts +9 -1
  11. package/dist/cjs/dialect/postgres/postgres-dialect.js +11 -5
  12. package/dist/cjs/dialect/postgres/postgres-driver.d.ts +25 -3
  13. package/dist/cjs/dialect/postgres/postgres-driver.js +37 -24
  14. package/dist/cjs/dialect/postgres/postgres-introspector.d.ts +11 -1
  15. package/dist/cjs/dialect/postgres/postgres-introspector.js +15 -4
  16. package/dist/cjs/dialect/sqlite/sqlite-introspector.d.ts +11 -1
  17. package/dist/cjs/dialect/sqlite/sqlite-introspector.js +11 -4
  18. package/dist/cjs/index-nodeless.js +5 -1
  19. package/dist/cjs/index.js +5 -1
  20. package/dist/cjs/migration/migrator.d.ts +44 -2
  21. package/dist/cjs/migration/migrator.js +77 -18
  22. package/dist/cjs/operation-node/alias-node.d.ts +2 -2
  23. package/dist/cjs/operation-node/alias-node.js +1 -2
  24. package/dist/cjs/operation-node/operation-node-transformer.js +1 -0
  25. package/dist/cjs/operation-node/update-query-node.d.ts +3 -0
  26. package/dist/cjs/operation-node/update-query-node.js +11 -2
  27. package/dist/cjs/parser/reference-parser.js +2 -1
  28. package/dist/cjs/parser/table-parser.js +2 -1
  29. package/dist/cjs/parser/update-set-parser.d.ts +3 -3
  30. package/dist/cjs/parser/with-parser.d.ts +2 -2
  31. package/dist/cjs/plugin/noop-plugin.d.ts +42 -0
  32. package/dist/cjs/plugin/noop-plugin.js +12 -0
  33. package/dist/cjs/query-builder/insert-query-builder.d.ts +1 -1
  34. package/dist/cjs/query-builder/on-conflict-builder.d.ts +1 -1
  35. package/dist/cjs/query-builder/select-query-builder.d.ts +3 -1
  36. package/dist/cjs/query-builder/select-query-builder.js +5 -5
  37. package/dist/cjs/query-builder/update-query-builder.d.ts +57 -28
  38. package/dist/cjs/query-builder/update-query-builder.js +7 -0
  39. package/dist/cjs/query-compiler/default-query-compiler.js +4 -0
  40. package/dist/cjs/query-creator.d.ts +10 -1
  41. package/dist/cjs/query-creator.js +18 -0
  42. package/dist/cjs/raw-builder/raw-builder.d.ts +32 -2
  43. package/dist/cjs/raw-builder/raw-builder.js +5 -26
  44. package/dist/cjs/schema/schema.d.ts +9 -0
  45. package/dist/cjs/schema/schema.js +12 -0
  46. package/dist/cjs/util/column-type.d.ts +9 -1
  47. package/dist/cjs/util/stack-trace-utils.d.ts +1 -0
  48. package/dist/cjs/util/stack-trace-utils.js +17 -0
  49. package/dist/esm/dialect/database-introspector.d.ts +13 -0
  50. package/dist/esm/dialect/mysql/mysql-dialect-config.d.ts +19 -19
  51. package/dist/esm/dialect/mysql/mysql-dialect.d.ts +9 -1
  52. package/dist/esm/dialect/mysql/mysql-dialect.js +11 -5
  53. package/dist/esm/dialect/mysql/mysql-driver.d.ts +25 -3
  54. package/dist/esm/dialect/mysql/mysql-driver.js +48 -35
  55. package/dist/esm/dialect/mysql/mysql-introspector.d.ts +11 -1
  56. package/dist/esm/dialect/mysql/mysql-introspector.js +30 -8
  57. package/dist/esm/dialect/postgres/postgres-dialect.d.ts +9 -1
  58. package/dist/esm/dialect/postgres/postgres-dialect.js +11 -5
  59. package/dist/esm/dialect/postgres/postgres-driver.d.ts +25 -3
  60. package/dist/esm/dialect/postgres/postgres-driver.js +38 -25
  61. package/dist/esm/dialect/postgres/postgres-introspector.d.ts +11 -1
  62. package/dist/esm/dialect/postgres/postgres-introspector.js +16 -5
  63. package/dist/esm/dialect/sqlite/sqlite-introspector.d.ts +11 -1
  64. package/dist/esm/dialect/sqlite/sqlite-introspector.js +12 -5
  65. package/dist/esm/migration/migrator.d.ts +44 -2
  66. package/dist/esm/migration/migrator.js +76 -17
  67. package/dist/esm/operation-node/alias-node.d.ts +2 -2
  68. package/dist/esm/operation-node/alias-node.js +1 -2
  69. package/dist/esm/operation-node/operation-node-transformer.js +1 -0
  70. package/dist/esm/operation-node/update-query-node.d.ts +3 -0
  71. package/dist/esm/operation-node/update-query-node.js +11 -2
  72. package/dist/esm/parser/reference-parser.js +2 -1
  73. package/dist/esm/parser/table-parser.js +2 -1
  74. package/dist/esm/parser/update-set-parser.d.ts +3 -3
  75. package/dist/esm/parser/with-parser.d.ts +2 -2
  76. package/dist/esm/plugin/noop-plugin.d.ts +42 -0
  77. package/dist/esm/plugin/noop-plugin.js +9 -0
  78. package/dist/esm/query-builder/insert-query-builder.d.ts +1 -1
  79. package/dist/esm/query-builder/on-conflict-builder.d.ts +1 -1
  80. package/dist/esm/query-builder/select-query-builder.d.ts +3 -1
  81. package/dist/esm/query-builder/select-query-builder.js +5 -5
  82. package/dist/esm/query-builder/update-query-builder.d.ts +57 -28
  83. package/dist/esm/query-builder/update-query-builder.js +7 -0
  84. package/dist/esm/query-compiler/default-query-compiler.js +4 -0
  85. package/dist/esm/query-creator.d.ts +10 -1
  86. package/dist/esm/query-creator.js +18 -0
  87. package/dist/esm/raw-builder/raw-builder.d.ts +32 -2
  88. package/dist/esm/raw-builder/raw-builder.js +5 -26
  89. package/dist/esm/schema/schema.d.ts +9 -0
  90. package/dist/esm/schema/schema.js +12 -0
  91. package/dist/esm/util/column-type.d.ts +9 -1
  92. package/dist/esm/util/stack-trace-utils.d.ts +1 -0
  93. package/dist/esm/util/stack-trace-utils.js +14 -0
  94. package/package.json +9 -9
package/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Stand With Ukraine](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner2-direct.svg)](https://stand-with-ukraine.pp.ua)
2
+
1
3
  [![Discord](https://img.shields.io/discord/890118421587578920)](https://discord.gg/C9aJ49mCra)
2
4
  [![Tests](https://github.com/koskimas/kysely/actions/workflows/test.yml/badge.svg)](https://github.com/koskimas/kysely)
3
5
 
@@ -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.
@@ -162,6 +162,25 @@ export interface MysqlDialectConfig {
162
162
  * If keep-alive is enabled users can supply an initial delay.
163
163
  */
164
164
  keepAliveInitialDelay?: number;
165
+ charsetNumber?: number;
166
+ compress?: boolean;
167
+ authSwitchHandler?: (data: any, callback: () => void) => any;
168
+ connectAttributes?: {
169
+ [param: string]: any;
170
+ };
171
+ decimalNumbers?: boolean;
172
+ isServer?: boolean;
173
+ maxPreparedStatements?: number;
174
+ namedPlaceholders?: boolean;
175
+ nestTables?: boolean | string;
176
+ passwordSha1?: string;
177
+ pool?: any;
178
+ stream?: any;
179
+ uri?: string;
180
+ Promise?: any;
181
+ authPlugins?: {
182
+ [key: string]: any;
183
+ };
165
184
  /**
166
185
  * Called once for each created connection.
167
186
  *
@@ -202,23 +221,4 @@ export interface MysqlSslOptions {
202
221
  * You can also connect to a MySQL server without properly providing the appropriate CA to trust. You should not do this.
203
222
  */
204
223
  rejectUnauthorized?: boolean;
205
- charsetNumber?: number;
206
- compress?: boolean;
207
- authSwitchHandler?: (data: any, callback: () => void) => any;
208
- connectAttributes?: {
209
- [param: string]: any;
210
- };
211
- decimalNumbers?: boolean;
212
- isServer?: boolean;
213
- maxPreparedStatements?: number;
214
- namedPlaceholders?: boolean;
215
- nestTables?: boolean | string;
216
- passwordSha1?: string;
217
- pool?: any;
218
- stream?: any;
219
- uri?: string;
220
- Promise?: any;
221
- authPlugins?: {
222
- [key: string]: any;
223
- };
224
224
  }
@@ -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(config: MysqlDialectConfig);
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
- #config;
18
- constructor(config) {
19
- this.#config = (0, object_utils_js_1.freeze)({ ...config });
23
+ #configOrPool;
24
+ constructor(configOrPool) {
25
+ this.#configOrPool = configOrPool;
20
26
  }
21
27
  createDriver() {
22
- return new mysql_driver_js_1.MysqlDriver(this.#config);
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(config: MysqlDialectConfig);
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: DatabaseConnection): Promise<void>;
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(config) {
12
- this.#config = (0, object_utils_js_1.freeze)({ ...config });
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
- // Import the `mysql2` module here instead at the top of the file
16
- // so that this file can be loaded by node without `mysql2` driver
17
- // installed. As you can see, there IS an import from `mysql2` at the
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 poolFactory = await importMysqlPoolFactory();
21
- // Use the `mysql2` module's own pool. All drivers should use the
22
- // pool provided by the database library if possible.
23
- this.#pool = poolFactory(this.#config);
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.onCreateConnection) {
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
- const mysqlConnection = connection;
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
- const result = await this.#executeQuery(compiledQuery);
107
- if (isOkPacket(result)) {
108
- const { insertId, affectedRows } = result;
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
- else if (Array.isArray(result)) {
122
- return {
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 getMetadata(options = { withInternalKyselyTables: false }) {
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
- .selectAll()
16
- .where('table_schema', '=', (0, sql_js_1.sql) `database()`)
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.MIGRATION_TABLE)
21
- .where('table_name', '!=', migrator_js_1.MIGRATION_LOCK_TABLE);
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.#parseTableMetadata(rawColumns),
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(config: PostgresDialectConfig);
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
- #config;
21
- constructor(config) {
22
- this.#config = (0, object_utils_js_1.freeze)({ ...config });
26
+ #configOrPool;
27
+ constructor(configOrPool) {
28
+ this.#configOrPool = configOrPool;
23
29
  }
24
30
  createDriver() {
25
- return new postgres_driver_js_1.PostgresDriver(this.#config);
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(config: PostgresDialectConfig);
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: DatabaseConnection): Promise<void>;
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 = null;
11
- constructor(config) {
12
- this.#config = (0, object_utils_js_1.freeze)({ ...config });
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
- // Import the `pg` module here instead at the top of the file
16
- // so that this file can be loaded by node without `pg` driver
17
- // installed. As you can see, there IS an import from `pg` at the
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);
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.onCreateConnection) {
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
- const pgConnection = connection;
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 = null;
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
- const result = await this.#client.query(compiledQuery.sql, [
91
- ...compiledQuery.parameters,
92
- ]);
93
- if (result.command === 'UPDATE' || result.command === 'DELETE') {
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
- return {
100
- rows: result.rows ?? [],
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
  }