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.
Files changed (114) hide show
  1. package/README.md +24 -17
  2. package/dist/cjs/dialect/database-introspector.d.ts +13 -0
  3. package/dist/cjs/dialect/mysql/mysql-dialect-config.d.ts +24 -207
  4. package/dist/cjs/dialect/mysql/mysql-dialect.d.ts +26 -3
  5. package/dist/cjs/dialect/mysql/mysql-dialect.js +27 -5
  6. package/dist/cjs/dialect/mysql/mysql-driver.d.ts +13 -4
  7. package/dist/cjs/dialect/mysql/mysql-driver.js +33 -51
  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-config.d.ts +25 -89
  11. package/dist/cjs/dialect/postgres/postgres-dialect.d.ts +25 -5
  12. package/dist/cjs/dialect/postgres/postgres-dialect.js +26 -7
  13. package/dist/cjs/dialect/postgres/postgres-driver.d.ts +12 -3
  14. package/dist/cjs/dialect/postgres/postgres-driver.js +21 -39
  15. package/dist/cjs/dialect/postgres/postgres-introspector.d.ts +11 -1
  16. package/dist/cjs/dialect/postgres/postgres-introspector.js +15 -4
  17. package/dist/cjs/dialect/sqlite/sqlite-dialect-config.d.ts +25 -28
  18. package/dist/cjs/dialect/sqlite/sqlite-dialect.d.ts +19 -3
  19. package/dist/cjs/dialect/sqlite/sqlite-dialect.js +19 -3
  20. package/dist/cjs/dialect/sqlite/sqlite-driver.js +3 -37
  21. package/dist/cjs/dialect/sqlite/sqlite-introspector.d.ts +11 -1
  22. package/dist/cjs/dialect/sqlite/sqlite-introspector.js +11 -4
  23. package/dist/cjs/index.d.ts +153 -2
  24. package/dist/cjs/index.js +147 -3
  25. package/dist/cjs/migration/file-migration-provider.d.ts +21 -5
  26. package/dist/cjs/migration/file-migration-provider.js +14 -13
  27. package/dist/cjs/migration/migrator.d.ts +44 -2
  28. package/dist/cjs/migration/migrator.js +77 -18
  29. package/dist/cjs/operation-node/alias-node.d.ts +2 -2
  30. package/dist/cjs/operation-node/alias-node.js +1 -2
  31. package/dist/cjs/operation-node/operation-node-transformer.js +1 -0
  32. package/dist/cjs/operation-node/update-query-node.d.ts +3 -0
  33. package/dist/cjs/operation-node/update-query-node.js +11 -2
  34. package/dist/cjs/parser/reference-parser.js +2 -1
  35. package/dist/cjs/parser/table-parser.js +2 -1
  36. package/dist/cjs/parser/update-set-parser.d.ts +3 -3
  37. package/dist/cjs/parser/with-parser.d.ts +2 -2
  38. package/dist/cjs/plugin/noop-plugin.d.ts +42 -0
  39. package/dist/cjs/plugin/noop-plugin.js +12 -0
  40. package/dist/cjs/query-builder/insert-query-builder.d.ts +1 -1
  41. package/dist/cjs/query-builder/on-conflict-builder.d.ts +1 -1
  42. package/dist/cjs/query-builder/select-query-builder.d.ts +3 -1
  43. package/dist/cjs/query-builder/select-query-builder.js +5 -5
  44. package/dist/cjs/query-builder/update-query-builder.d.ts +57 -28
  45. package/dist/cjs/query-builder/update-query-builder.js +7 -0
  46. package/dist/cjs/query-compiler/default-query-compiler.js +4 -0
  47. package/dist/cjs/query-creator.d.ts +10 -1
  48. package/dist/cjs/query-creator.js +18 -0
  49. package/dist/cjs/raw-builder/raw-builder.d.ts +32 -2
  50. package/dist/cjs/raw-builder/raw-builder.js +5 -26
  51. package/dist/cjs/schema/schema.d.ts +9 -0
  52. package/dist/cjs/schema/schema.js +12 -0
  53. package/dist/cjs/util/column-type.d.ts +9 -1
  54. package/dist/cjs/util/stack-trace-utils.d.ts +1 -0
  55. package/dist/cjs/util/stack-trace-utils.js +17 -0
  56. package/dist/esm/dialect/database-introspector.d.ts +13 -0
  57. package/dist/esm/dialect/mysql/mysql-dialect-config.d.ts +24 -207
  58. package/dist/esm/dialect/mysql/mysql-dialect.d.ts +26 -3
  59. package/dist/esm/dialect/mysql/mysql-dialect.js +27 -5
  60. package/dist/esm/dialect/mysql/mysql-driver.d.ts +13 -4
  61. package/dist/esm/dialect/mysql/mysql-driver.js +33 -51
  62. package/dist/esm/dialect/mysql/mysql-introspector.d.ts +11 -1
  63. package/dist/esm/dialect/mysql/mysql-introspector.js +30 -8
  64. package/dist/esm/dialect/postgres/postgres-dialect-config.d.ts +25 -89
  65. package/dist/esm/dialect/postgres/postgres-dialect.d.ts +25 -5
  66. package/dist/esm/dialect/postgres/postgres-dialect.js +26 -7
  67. package/dist/esm/dialect/postgres/postgres-driver.d.ts +12 -3
  68. package/dist/esm/dialect/postgres/postgres-driver.js +21 -39
  69. package/dist/esm/dialect/postgres/postgres-introspector.d.ts +11 -1
  70. package/dist/esm/dialect/postgres/postgres-introspector.js +16 -5
  71. package/dist/esm/dialect/sqlite/sqlite-dialect-config.d.ts +25 -28
  72. package/dist/esm/dialect/sqlite/sqlite-dialect.d.ts +19 -3
  73. package/dist/esm/dialect/sqlite/sqlite-dialect.js +19 -3
  74. package/dist/esm/dialect/sqlite/sqlite-driver.js +4 -38
  75. package/dist/esm/dialect/sqlite/sqlite-introspector.d.ts +11 -1
  76. package/dist/esm/dialect/sqlite/sqlite-introspector.js +12 -5
  77. package/dist/esm/index.d.ts +153 -2
  78. package/dist/esm/index.js +142 -2
  79. package/dist/esm/migration/file-migration-provider.d.ts +21 -5
  80. package/dist/esm/migration/file-migration-provider.js +14 -13
  81. package/dist/esm/migration/migrator.d.ts +44 -2
  82. package/dist/esm/migration/migrator.js +76 -17
  83. package/dist/esm/operation-node/alias-node.d.ts +2 -2
  84. package/dist/esm/operation-node/alias-node.js +1 -2
  85. package/dist/esm/operation-node/operation-node-transformer.js +1 -0
  86. package/dist/esm/operation-node/update-query-node.d.ts +3 -0
  87. package/dist/esm/operation-node/update-query-node.js +11 -2
  88. package/dist/esm/parser/reference-parser.js +2 -1
  89. package/dist/esm/parser/table-parser.js +2 -1
  90. package/dist/esm/parser/update-set-parser.d.ts +3 -3
  91. package/dist/esm/parser/with-parser.d.ts +2 -2
  92. package/dist/esm/plugin/noop-plugin.d.ts +42 -0
  93. package/dist/esm/plugin/noop-plugin.js +9 -0
  94. package/dist/esm/query-builder/insert-query-builder.d.ts +1 -1
  95. package/dist/esm/query-builder/on-conflict-builder.d.ts +1 -1
  96. package/dist/esm/query-builder/select-query-builder.d.ts +3 -1
  97. package/dist/esm/query-builder/select-query-builder.js +5 -5
  98. package/dist/esm/query-builder/update-query-builder.d.ts +57 -28
  99. package/dist/esm/query-builder/update-query-builder.js +7 -0
  100. package/dist/esm/query-compiler/default-query-compiler.js +4 -0
  101. package/dist/esm/query-creator.d.ts +10 -1
  102. package/dist/esm/query-creator.js +18 -0
  103. package/dist/esm/raw-builder/raw-builder.d.ts +32 -2
  104. package/dist/esm/raw-builder/raw-builder.js +5 -26
  105. package/dist/esm/schema/schema.d.ts +9 -0
  106. package/dist/esm/schema/schema.js +12 -0
  107. package/dist/esm/util/column-type.d.ts +9 -1
  108. package/dist/esm/util/stack-trace-utils.d.ts +1 -0
  109. package/dist/esm/util/stack-trace-utils.js +14 -0
  110. package/package.json +10 -15
  111. package/dist/cjs/index-nodeless.d.ts +0 -152
  112. package/dist/cjs/index-nodeless.js +0 -153
  113. package/dist/esm/index-nodeless.d.ts +0 -152
  114. package/dist/esm/index-nodeless.js +0 -142
@@ -2,223 +2,40 @@ import { DatabaseConnection } from '../../driver/database-connection.js';
2
2
  /**
3
3
  * Config for the MySQL dialect.
4
4
  *
5
- * This interface is equal to `mysql2` library's pool config.
6
- *
7
5
  * https://github.com/sidorares/node-mysql2#using-connection-pools
8
6
  */
9
7
  export interface MysqlDialectConfig {
10
8
  /**
11
- * The MySQL user to authenticate as
12
- */
13
- user?: string;
14
- /**
15
- * The password of that MySQL user
16
- */
17
- password?: string;
18
- /**
19
- * Name of the database to use for this connection
20
- */
21
- database?: string;
22
- /**
23
- * The charset for the connection. This is called 'collation' in the SQL-level of MySQL (like utf8_general_ci).
24
- * If a SQL-level charset is specified (like utf8mb4) then the default collation for that charset is used.
25
- * (Default: 'UTF8_GENERAL_CI')
26
- */
27
- charset?: string;
28
- /**
29
- * The hostname of the database you are connecting to. (Default: localhost)
30
- */
31
- host?: string;
32
- /**
33
- * The port number to connect to. (Default: 3306)
34
- */
35
- port?: number;
36
- /**
37
- * The source IP address to use for TCP connection
38
- */
39
- localAddress?: string;
40
- /**
41
- * The path to a unix domain socket to connect to. When used host and port are ignored
42
- */
43
- socketPath?: string;
44
- /**
45
- * The timezone used to store local dates. (Default: 'local')
46
- */
47
- timezone?: string | 'local';
48
- /**
49
- * The milliseconds before a timeout occurs during the initial connection to the MySQL server. (Default: 10 seconds)
50
- */
51
- connectTimeout?: number;
52
- /**
53
- * Stringify objects instead of converting to values. (Default: 'false')
54
- */
55
- stringifyObjects?: boolean;
56
- /**
57
- * Allow connecting to MySQL instances that ask for the old (insecure) authentication method. (Default: false)
58
- */
59
- insecureAuth?: boolean;
60
- /**
61
- * Determines if column values should be converted to native JavaScript types. It is not recommended (and may go away / change in the future)
62
- * to disable type casting, but you can currently do so on either the connection or query level. (Default: true)
63
- *
64
- * You can also specify a function (field: any, next: () => void) => {} to do the type casting yourself.
65
- *
66
- * WARNING: YOU MUST INVOKE the parser using one of these three field functions in your custom typeCast callback. They can only be called once.
67
- *
68
- * field.string()
69
- * field.buffer()
70
- * field.geometry()
71
- *
72
- * are aliases for
73
- *
74
- * parser.parseLengthCodedString()
75
- * parser.parseLengthCodedBuffer()
76
- * parser.parseGeometryValue()
77
- *
78
- * You can find which field function you need to use by looking at: RowDataPacket.prototype._typeCast
79
- */
80
- typeCast?: boolean | ((field: any, next: () => void) => any);
81
- /**
82
- * A custom query format function
83
- */
84
- queryFormat?: (query: string, values: any) => void;
85
- /**
86
- * When dealing with big numbers (BIGINT and DECIMAL columns) in the database, you should enable this option
87
- * (Default: false)
88
- */
89
- supportBigNumbers?: boolean;
90
- /**
91
- * Enabling both supportBigNumbers and bigNumberStrings forces big numbers (BIGINT and DECIMAL columns) to be
92
- * always returned as JavaScript String objects (Default: false). Enabling supportBigNumbers but leaving
93
- * bigNumberStrings disabled will return big numbers as String objects only when they cannot be accurately
94
- * represented with [JavaScript Number objects] (http://ecma262-5.com/ELS5_HTML.htm#Section_8.5)
95
- * (which happens when they exceed the [-2^53, +2^53] range), otherwise they will be returned as Number objects.
96
- * This option is ignored if supportBigNumbers is disabled.
97
- */
98
- bigNumberStrings?: boolean;
99
- /**
100
- * Force date types (TIMESTAMP, DATETIME, DATE) to be returned as strings rather then inflated into JavaScript Date
101
- * objects. Can be true/false or an array of type names to keep as strings.
9
+ * A mysql2 Pool instance or a function that returns one.
102
10
  *
103
- * (Default: false)
104
- */
105
- dateStrings?: boolean | Array<'TIMESTAMP' | 'DATETIME' | 'DATE'>;
106
- /**
107
- * This will print all incoming and outgoing packets on stdout.
108
- * You can also restrict debugging to packet types by passing an array of types (strings) to debug;
11
+ * If a function is provided, it's called once when the first query is executed.
109
12
  *
110
- * (Default: false)
13
+ * https://github.com/sidorares/node-mysql2#using-connection-pools
111
14
  */
112
- debug?: any;
113
- /**
114
- * Generates stack traces on Error to include call site of library entrance ('long stack traces'). Slight
115
- * performance penalty for most calls. (Default: true)
116
- */
117
- trace?: boolean;
118
- /**
119
- * Allow multiple MySQL statements per query. Be careful with this, it exposes you to SQL injection attacks. (Default: false)
120
- */
121
- multipleStatements?: boolean;
122
- /**
123
- * List of connection flags to use other than the default ones. It is also possible to blacklist default ones
124
- */
125
- flags?: Array<string>;
126
- /**
127
- * object with ssl parameters or a string containing name of ssl profile
128
- */
129
- ssl?: string | MysqlSslOptions;
130
- /**
131
- * Return each row as an array, not as an object.
132
- * This is useful when you have duplicate column names.
133
- * This can also be set in the `QueryOption` object to be applied per-query.
134
- */
135
- rowsAsArray?: boolean;
136
- /**
137
- * The milliseconds before a timeout occurs during the connection acquisition. This is slightly different from connectTimeout,
138
- * because acquiring a pool connection does not always involve making a connection. (Default: 10 seconds)
139
- */
140
- acquireTimeout?: number;
141
- /**
142
- * Determines the pool's action when no connections are available and the limit has been reached. If true, the pool will queue
143
- * the connection request and call it when one becomes available. If false, the pool will immediately call back with an error.
144
- * (Default: true)
145
- */
146
- waitForConnections?: boolean;
147
- /**
148
- * The maximum number of connections to create at once. (Default: 10)
149
- */
150
- connectionLimit?: number;
151
- /**
152
- * The maximum number of connection requests the pool will queue before returning an error from getConnection. If set to 0, there
153
- * is no limit to the number of queued connection requests. (Default: 0)
154
- */
155
- queueLimit?: number;
156
- /**
157
- * Enable keep-alive on the socket. It's disabled by default, but the
158
- * user can enable it and supply an initial delay.
159
- */
160
- enableKeepAlive?: true;
161
- /**
162
- * If keep-alive is enabled users can supply an initial delay.
163
- */
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
- };
15
+ pool: MysqlPool | (() => Promise<MysqlPool>);
184
16
  /**
185
17
  * Called once for each created connection.
186
- *
187
- * This is a Kysely specific feature and does not come from the `mysql2` module.
188
18
  */
189
19
  onCreateConnection?: (connection: DatabaseConnection) => Promise<void>;
190
20
  }
191
- export interface MysqlSslOptions {
192
- /**
193
- * A string or buffer holding the PFX or PKCS12 encoded private key, certificate and CA certificates
194
- */
195
- pfx?: string;
196
- /**
197
- * A string holding the PEM encoded private key
198
- */
199
- key?: string;
200
- /**
201
- * A string of passphrase for the private key or pfx
202
- */
203
- passphrase?: string;
204
- /**
205
- * A string holding the PEM encoded certificate
206
- */
207
- cert?: string;
208
- /**
209
- * Either a string or list of strings of PEM encoded CA certificates to trust.
210
- */
211
- ca?: string | string[];
212
- /**
213
- * Either a string or list of strings of PEM encoded CRLs (Certificate Revocation List)
214
- */
215
- crl?: string | string[];
216
- /**
217
- * A string describing the ciphers to use or exclude
218
- */
219
- ciphers?: string;
220
- /**
221
- * You can also connect to a MySQL server without properly providing the appropriate CA to trust. You should not do this.
222
- */
223
- rejectUnauthorized?: boolean;
21
+ /**
22
+ * This interface is the subset of mysql2 driver's `Pool` class that
23
+ * kysely needs.
24
+ *
25
+ * We don't use the type from `mysql2` here to not have a dependency to it.
26
+ *
27
+ * https://github.com/sidorares/node-mysql2#using-connection-pools
28
+ */
29
+ export interface MysqlPool {
30
+ getConnection(callback: (error: unknown, connection: MysqlPoolConnection) => void): void;
31
+ end(callback: (error: unknown) => void): void;
32
+ }
33
+ export interface MysqlPoolConnection {
34
+ query(sql: string, parameters: ReadonlyArray<unknown>, callback: (error: unknown, result: MysqlQueryResult) => void): void;
35
+ release(): void;
36
+ }
37
+ export interface MysqlOkPacket {
38
+ affectedRows: number;
39
+ insertId: number;
224
40
  }
41
+ export declare type MysqlQueryResult = MysqlOkPacket | Record<string, unknown>[];
@@ -8,9 +8,32 @@ import { MysqlDialectConfig } from './mysql-dialect-config.js';
8
8
  /**
9
9
  * MySQL dialect that uses the [mysql2](https://github.com/sidorares/node-mysql2#readme) library.
10
10
  *
11
- * The {@link MysqlDialectConfig | configuration} passed to the constructor
12
- * is given as-is to the mysql2 library's [createPool](https://github.com/sidorares/node-mysql2#using-connection-pools)
13
- * method.
11
+ * The constructor takes an instance of {@link MysqlDialectConfig}.
12
+ *
13
+ * ```ts
14
+ * import { createPool } from 'mysql2'
15
+ *
16
+ * new MysqlDialect({
17
+ * pool: createPool({
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 { createPool } from 'mysql2'
29
+ *
30
+ * new MysqlDialect({
31
+ * pool: async () => createPool({
32
+ * database: 'some_db',
33
+ * host: 'localhost',
34
+ * })
35
+ * })
36
+ * ```
14
37
  */
15
38
  export declare class MysqlDialect implements Dialect {
16
39
  #private;
@@ -3,18 +3,40 @@ import { MysqlDriver } from './mysql-driver.js';
3
3
  import { MysqlQueryCompiler } from './mysql-query-compiler.js';
4
4
  import { MysqlIntrospector } from './mysql-introspector.js';
5
5
  import { MysqlAdapter } from './mysql-adapter.js';
6
- import { freeze } from '../../util/object-utils.js';
7
6
  /**
8
7
  * MySQL dialect that uses the [mysql2](https://github.com/sidorares/node-mysql2#readme) library.
9
8
  *
10
- * The {@link MysqlDialectConfig | configuration} passed to the constructor
11
- * is given as-is to the mysql2 library's [createPool](https://github.com/sidorares/node-mysql2#using-connection-pools)
12
- * method.
9
+ * The constructor takes an instance of {@link MysqlDialectConfig}.
10
+ *
11
+ * ```ts
12
+ * import { createPool } from 'mysql2'
13
+ *
14
+ * new MysqlDialect({
15
+ * pool: createPool({
16
+ * database: 'some_db',
17
+ * host: 'localhost',
18
+ * })
19
+ * })
20
+ * ```
21
+ *
22
+ * If you want the pool to only be created once it's first used, `pool`
23
+ * can be a function:
24
+ *
25
+ * ```ts
26
+ * import { createPool } from 'mysql2'
27
+ *
28
+ * new MysqlDialect({
29
+ * pool: async () => createPool({
30
+ * database: 'some_db',
31
+ * host: 'localhost',
32
+ * })
33
+ * })
34
+ * ```
13
35
  */
14
36
  export class MysqlDialect {
15
37
  #config;
16
38
  constructor(config) {
17
- this.#config = freeze({ ...config });
39
+ this.#config = config;
18
40
  }
19
41
  createDriver() {
20
42
  return new MysqlDriver(this.#config);
@@ -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 { MysqlDialectConfig } from './mysql-dialect-config.js';
3
+ import { CompiledQuery } from '../../query-compiler/compiled-query.js';
4
+ import { MysqlDialectConfig, MysqlPoolConnection } 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);
7
9
  /**
8
10
  * Initializes the driver.
9
11
  *
@@ -35,9 +37,16 @@ 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
+ declare class MysqlConnection implements DatabaseConnection {
47
+ #private;
48
+ constructor(rawConnection: MysqlPoolConnection);
49
+ executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>>;
50
+ [PRIVATE_RELEASE_METHOD](): void;
51
+ }
52
+ export {};
@@ -1,24 +1,19 @@
1
1
  /// <reference types="./mysql-driver.d.ts" />
2
2
  import { CompiledQuery } from '../../query-compiler/compiled-query.js';
3
3
  import { isFunction, isObject, freeze } from '../../util/object-utils.js';
4
+ import { extendStackTrace } from '../../util/stack-trace-utils.js';
4
5
  const PRIVATE_RELEASE_METHOD = Symbol();
5
6
  export class MysqlDriver {
6
7
  #config;
7
8
  #connections = new WeakMap();
8
9
  #pool;
9
- constructor(config) {
10
- this.#config = freeze({ ...config });
10
+ constructor(configOrPool) {
11
+ this.#config = freeze({ ...configOrPool });
11
12
  }
12
13
  async init() {
13
- // Import the `mysql2` module here instead at the top of the file
14
- // so that this file can be loaded by node without `mysql2` driver
15
- // installed. As you can see, there IS an import from `mysql2` at the
16
- // top level too, but that's only for types. It doesn't get compiled
17
- // into javascript. You can check the built javascript code.
18
- const poolFactory = await importMysqlPoolFactory();
19
- // Use the `mysql2` module's own pool. All drivers should use the
20
- // pool provided by the database library if possible.
21
- this.#pool = poolFactory(this.#config);
14
+ this.#pool = isFunction(this.#config.pool)
15
+ ? await this.#config.pool()
16
+ : this.#config.pool;
22
17
  }
23
18
  async acquireConnection() {
24
19
  const rawConnection = await this.#acquireConnection();
@@ -29,7 +24,7 @@ export class MysqlDriver {
29
24
  // The driver must take care of calling `onCreateConnection` when a new
30
25
  // connection is created. The `mysql2` module doesn't provide an async hook
31
26
  // for the connection creation. We need to call the method explicitly.
32
- if (this.#config.onCreateConnection) {
27
+ if (this.#config?.onCreateConnection) {
33
28
  await this.#config.onCreateConnection(connection);
34
29
  }
35
30
  }
@@ -61,8 +56,7 @@ export class MysqlDriver {
61
56
  await connection.executeQuery(CompiledQuery.raw('rollback'));
62
57
  }
63
58
  async releaseConnection(connection) {
64
- const mysqlConnection = connection;
65
- mysqlConnection[PRIVATE_RELEASE_METHOD]();
59
+ connection[PRIVATE_RELEASE_METHOD]();
66
60
  }
67
61
  async destroy() {
68
62
  return new Promise((resolve, reject) => {
@@ -77,22 +71,8 @@ export class MysqlDriver {
77
71
  });
78
72
  }
79
73
  }
80
- async function importMysqlPoolFactory() {
81
- try {
82
- // The imported module name must be a string literal to make
83
- // some bundlers work. So don't move this code behind a helper
84
- // for example.
85
- const mysqlModule = await import('mysql2');
86
- if (isFunction(mysqlModule.createPool)) {
87
- return mysqlModule.createPool;
88
- }
89
- else {
90
- return mysqlModule.default.createPool;
91
- }
92
- }
93
- catch (error) {
94
- throw new Error('MySQL client not installed. Please run `npm install mysql2`');
95
- }
74
+ function isOkPacket(obj) {
75
+ return isObject(obj) && 'insertId' in obj && 'affectedRows' in obj;
96
76
  }
97
77
  class MysqlConnection {
98
78
  #rawConnection;
@@ -100,29 +80,34 @@ class MysqlConnection {
100
80
  this.#rawConnection = rawConnection;
101
81
  }
102
82
  async executeQuery(compiledQuery) {
103
- const result = await this.#executeQuery(compiledQuery);
104
- if (isOkPacket(result)) {
105
- const { insertId, affectedRows } = result;
83
+ try {
84
+ const result = await this.#executeQuery(compiledQuery);
85
+ if (isOkPacket(result)) {
86
+ const { insertId, affectedRows } = result;
87
+ return {
88
+ insertId: insertId !== undefined &&
89
+ insertId !== null &&
90
+ insertId.toString() !== '0'
91
+ ? BigInt(insertId)
92
+ : undefined,
93
+ numUpdatedOrDeletedRows: affectedRows !== undefined && insertId !== null
94
+ ? BigInt(affectedRows)
95
+ : undefined,
96
+ rows: [],
97
+ };
98
+ }
99
+ else if (Array.isArray(result)) {
100
+ return {
101
+ rows: result,
102
+ };
103
+ }
106
104
  return {
107
- insertId: insertId !== undefined &&
108
- insertId !== null &&
109
- insertId.toString() !== '0'
110
- ? BigInt(insertId)
111
- : undefined,
112
- numUpdatedOrDeletedRows: affectedRows !== undefined && insertId !== null
113
- ? BigInt(affectedRows)
114
- : undefined,
115
105
  rows: [],
116
106
  };
117
107
  }
118
- else if (Array.isArray(result)) {
119
- return {
120
- rows: result,
121
- };
108
+ catch (err) {
109
+ throw extendStackTrace(err, new Error());
122
110
  }
123
- return {
124
- rows: [],
125
- };
126
111
  }
127
112
  #executeQuery(compiledQuery) {
128
113
  return new Promise((resolve, reject) => {
@@ -140,6 +125,3 @@ class MysqlConnection {
140
125
  this.#rawConnection.release();
141
126
  }
142
127
  }
143
- function isOkPacket(obj) {
144
- return isObject(obj) && 'insertId' in obj && 'affectedRows' in obj;
145
- }
@@ -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
  }
@@ -1,26 +1,47 @@
1
1
  /// <reference types="./mysql-introspector.d.ts" />
2
- import { MIGRATION_LOCK_TABLE, MIGRATION_TABLE, } from '../../migration/migrator.js';
2
+ import { DEFAULT_MIGRATION_LOCK_TABLE, DEFAULT_MIGRATION_TABLE, } from '../../migration/migrator.js';
3
3
  import { freeze } from '../../util/object-utils.js';
4
- import { sql } from '../../raw-builder/sql.js';
5
4
  export class MysqlIntrospector {
6
5
  #db;
7
6
  constructor(db) {
8
7
  this.#db = db;
9
8
  }
10
- async getMetadata(options = { withInternalKyselyTables: false }) {
9
+ async getSchemas() {
10
+ let rawSchemas = await this.#db
11
+ .selectFrom('information_schema.schemata')
12
+ .select('schema_name')
13
+ .castTo()
14
+ .execute();
15
+ return rawSchemas.map((it) => ({ name: it.SCHEMA_NAME }));
16
+ }
17
+ async getTables(options = { withInternalKyselyTables: false }) {
11
18
  let query = this.#db
12
19
  .selectFrom('information_schema.columns')
13
- .selectAll()
14
- .where('table_schema', '=', sql `database()`)
20
+ .select([
21
+ 'column_name',
22
+ 'table_name',
23
+ 'table_schema',
24
+ 'is_nullable',
25
+ 'data_type',
26
+ ])
27
+ .where('table_schema', 'not in', [
28
+ 'information_schema',
29
+ 'performance_schema',
30
+ 'mysql',
31
+ 'sys',
32
+ ])
15
33
  .castTo();
16
34
  if (!options.withInternalKyselyTables) {
17
35
  query = query
18
- .where('table_name', '!=', MIGRATION_TABLE)
19
- .where('table_name', '!=', MIGRATION_LOCK_TABLE);
36
+ .where('table_name', '!=', DEFAULT_MIGRATION_TABLE)
37
+ .where('table_name', '!=', DEFAULT_MIGRATION_LOCK_TABLE);
20
38
  }
21
39
  const rawColumns = await query.execute();
40
+ return this.#parseTableMetadata(rawColumns);
41
+ }
42
+ async getMetadata(options) {
22
43
  return {
23
- tables: this.#parseTableMetadata(rawColumns),
44
+ tables: await this.getTables(options),
24
45
  };
25
46
  }
26
47
  #parseTableMetadata(columns) {
@@ -29,6 +50,7 @@ export class MysqlIntrospector {
29
50
  if (!table) {
30
51
  table = freeze({
31
52
  name: it.TABLE_NAME,
53
+ schema: it.TABLE_SCHEMA,
32
54
  columns: [],
33
55
  });
34
56
  tables.push(table);