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
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
 
@@ -68,6 +70,7 @@ All you need to do is define an interface for each table in the database and pas
68
70
  interfaces to the `Kysely` constructor:
69
71
 
70
72
  ```ts
73
+ import { Pool } from 'pg'
71
74
  import {
72
75
  Kysely,
73
76
  PostgresDialect,
@@ -123,9 +126,11 @@ interface Database {
123
126
  const db = new Kysely<Database>({
124
127
  // Use MysqlDialect for MySQL and SqliteDialect for SQLite.
125
128
  dialect: new PostgresDialect({
126
- host: 'localhost',
127
- database: 'kysely_test',
128
- }),
129
+ pool: new Pool({
130
+ host: 'localhost',
131
+ database: 'kysely_test'
132
+ })
133
+ })
129
134
  })
130
135
 
131
136
  async function demo() {
@@ -317,6 +322,9 @@ need to depend on a typescript compiler, which most production environments don'
317
322
  have. You will probably want to add a simple migration script to your projects like this:
318
323
 
319
324
  ```ts
325
+ import * as path from 'path'
326
+ import { Pool } from 'pg'
327
+ import { promises: fs } from 'fs'
320
328
  import {
321
329
  Kysely,
322
330
  Migrator,
@@ -327,17 +335,20 @@ import {
327
335
  async function migrateToLatest() {
328
336
  const db = new Kysely<Database>({
329
337
  dialect: new PostgresDialect({
330
- host: 'localhost',
331
- database: 'kysely_test',
338
+ pool: new Pool({
339
+ host: 'localhost',
340
+ database: 'kysely_test',
341
+ })
332
342
  }),
333
343
  })
334
344
 
335
345
  const migrator = new Migrator({
336
346
  db,
337
- provider: new FileMigrationProvider(
338
- // Path to the folder that contains all your migrations.
339
- 'some/path/to/migrations'
340
- )
347
+ provider: new FileMigrationProvider({
348
+ fs,
349
+ path,
350
+ migrationFolder: 'some/path/to/migrations',
351
+ })
341
352
  })
342
353
 
343
354
  const { error, results } = await migrator.migrateToLatest()
@@ -374,9 +385,7 @@ Kysely doesn't include drivers for deno, but you can still use Kysely as a query
374
385
  or implement your own driver:
375
386
 
376
387
  ```ts
377
- // We use jsdeliver to get Kysely from npm. We also need to
378
- // import the `dist/esm/index-nodeless.js` file instead of the
379
- // root which has node dependencies.
388
+ // We use jsdeliver to get Kysely from npm.
380
389
  import {
381
390
  DummyDriver,
382
391
  Generated,
@@ -384,7 +393,7 @@ import {
384
393
  PostgresAdapter,
385
394
  PostgresIntrospector,
386
395
  PostgresQueryCompiler,
387
- } from 'https://cdn.jsdelivr.net/npm/kysely/dist/esm/index-nodeless.js'
396
+ } from 'https://cdn.jsdelivr.net/npm/kysely/dist/esm/index.js'
388
397
 
389
398
  interface Person {
390
399
  id: Generated<number>
@@ -423,9 +432,7 @@ console.log(sql.sql)
423
432
 
424
433
  # Browser
425
434
 
426
- Kysely also runs in the browser as long as you import the `dist/esm/index-nodeless.js`
427
- module instead of the root that has node dependencies. You can also import the
428
- `dist/cjs/index-nodeless.js` file if you are using commonjs modules instead of ESM.
435
+ Kysely also runs in the browser:
429
436
 
430
437
  ```ts
431
438
  import {
@@ -435,7 +442,7 @@ import {
435
442
  SqliteAdapter,
436
443
  SqliteIntrospector,
437
444
  SqliteQueryCompiler,
438
- } from 'kysely/dist/esm/index-nodeless.js'
445
+ } from 'kysely'
439
446
 
440
447
  interface Person {
441
448
  id: Generated<number>
@@ -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.
@@ -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;
@@ -5,18 +5,40 @@ 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
- * The {@link MysqlDialectConfig | configuration} passed to the constructor
13
- * is given as-is to the mysql2 library's [createPool](https://github.com/sidorares/node-mysql2#using-connection-pools)
14
- * 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
+ * ```
15
37
  */
16
38
  class MysqlDialect {
17
39
  #config;
18
40
  constructor(config) {
19
- this.#config = (0, object_utils_js_1.freeze)({ ...config });
41
+ this.#config = config;
20
42
  }
21
43
  createDriver() {
22
44
  return new mysql_driver_js_1.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 {};
@@ -3,24 +3,19 @@ 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
+ this.#config = (0, object_utils_js_1.freeze)({ ...configOrPool });
13
14
  }
14
15
  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);
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 rawConnection = await this.#acquireConnection();
@@ -31,7 +26,7 @@ class MysqlDriver {
31
26
  // The driver must take care of calling `onCreateConnection` when a new
32
27
  // connection is created. The `mysql2` module doesn't provide an async hook
33
28
  // for the connection creation. We need to call the method explicitly.
34
- if (this.#config.onCreateConnection) {
29
+ if (this.#config?.onCreateConnection) {
35
30
  await this.#config.onCreateConnection(connection);
36
31
  }
37
32
  }
@@ -63,8 +58,7 @@ class MysqlDriver {
63
58
  await connection.executeQuery(compiled_query_js_1.CompiledQuery.raw('rollback'));
64
59
  }
65
60
  async releaseConnection(connection) {
66
- const mysqlConnection = connection;
67
- mysqlConnection[PRIVATE_RELEASE_METHOD]();
61
+ connection[PRIVATE_RELEASE_METHOD]();
68
62
  }
69
63
  async destroy() {
70
64
  return new Promise((resolve, reject) => {
@@ -80,22 +74,8 @@ class MysqlDriver {
80
74
  }
81
75
  }
82
76
  exports.MysqlDriver = MysqlDriver;
83
- async function importMysqlPoolFactory() {
84
- try {
85
- // The imported module name must be a string literal to make
86
- // some bundlers work. So don't move this code behind a helper
87
- // for example.
88
- const mysqlModule = await Promise.resolve().then(() => require('mysql2'));
89
- if ((0, object_utils_js_1.isFunction)(mysqlModule.createPool)) {
90
- return mysqlModule.createPool;
91
- }
92
- else {
93
- return mysqlModule.default.createPool;
94
- }
95
- }
96
- catch (error) {
97
- throw new Error('MySQL client not installed. Please run `npm install mysql2`');
98
- }
77
+ function isOkPacket(obj) {
78
+ return (0, object_utils_js_1.isObject)(obj) && 'insertId' in obj && 'affectedRows' in obj;
99
79
  }
100
80
  class MysqlConnection {
101
81
  #rawConnection;
@@ -103,29 +83,34 @@ class MysqlConnection {
103
83
  this.#rawConnection = rawConnection;
104
84
  }
105
85
  async executeQuery(compiledQuery) {
106
- const result = await this.#executeQuery(compiledQuery);
107
- if (isOkPacket(result)) {
108
- const { insertId, affectedRows } = result;
86
+ try {
87
+ const result = await this.#executeQuery(compiledQuery);
88
+ if (isOkPacket(result)) {
89
+ const { insertId, affectedRows } = result;
90
+ return {
91
+ insertId: insertId !== undefined &&
92
+ insertId !== null &&
93
+ insertId.toString() !== '0'
94
+ ? BigInt(insertId)
95
+ : undefined,
96
+ numUpdatedOrDeletedRows: affectedRows !== undefined && insertId !== null
97
+ ? BigInt(affectedRows)
98
+ : undefined,
99
+ rows: [],
100
+ };
101
+ }
102
+ else if (Array.isArray(result)) {
103
+ return {
104
+ rows: result,
105
+ };
106
+ }
109
107
  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
108
  rows: [],
119
109
  };
120
110
  }
121
- else if (Array.isArray(result)) {
122
- return {
123
- rows: result,
124
- };
111
+ catch (err) {
112
+ throw (0, stack_trace_utils_js_1.extendStackTrace)(err, new Error());
125
113
  }
126
- return {
127
- rows: [],
128
- };
129
114
  }
130
115
  #executeQuery(compiledQuery) {
131
116
  return new Promise((resolve, reject) => {
@@ -143,6 +128,3 @@ class MysqlConnection {
143
128
  this.#rawConnection.release();
144
129
  }
145
130
  }
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
  }