kysely 0.18.0 → 0.19.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/README.md +22 -17
- package/dist/cjs/dialect/database-introspector.d.ts +13 -0
- package/dist/cjs/dialect/mysql/mysql-dialect-config.d.ts +24 -207
- package/dist/cjs/dialect/mysql/mysql-dialect.d.ts +26 -3
- package/dist/cjs/dialect/mysql/mysql-dialect.js +27 -5
- package/dist/cjs/dialect/mysql/mysql-driver.d.ts +13 -4
- package/dist/cjs/dialect/mysql/mysql-driver.js +33 -51
- package/dist/cjs/dialect/mysql/mysql-introspector.d.ts +11 -1
- package/dist/cjs/dialect/mysql/mysql-introspector.js +29 -7
- package/dist/cjs/dialect/postgres/postgres-dialect-config.d.ts +25 -89
- package/dist/cjs/dialect/postgres/postgres-dialect.d.ts +25 -5
- package/dist/cjs/dialect/postgres/postgres-dialect.js +26 -7
- package/dist/cjs/dialect/postgres/postgres-driver.d.ts +12 -3
- package/dist/cjs/dialect/postgres/postgres-driver.js +21 -39
- package/dist/cjs/dialect/postgres/postgres-introspector.d.ts +11 -1
- package/dist/cjs/dialect/postgres/postgres-introspector.js +15 -4
- package/dist/cjs/dialect/sqlite/sqlite-dialect-config.d.ts +25 -28
- package/dist/cjs/dialect/sqlite/sqlite-dialect.d.ts +19 -3
- package/dist/cjs/dialect/sqlite/sqlite-dialect.js +19 -3
- package/dist/cjs/dialect/sqlite/sqlite-driver.js +3 -37
- package/dist/cjs/dialect/sqlite/sqlite-introspector.d.ts +11 -1
- package/dist/cjs/dialect/sqlite/sqlite-introspector.js +11 -4
- package/dist/cjs/index.d.ts +153 -2
- package/dist/cjs/index.js +147 -3
- package/dist/cjs/migration/file-migration-provider.d.ts +21 -5
- package/dist/cjs/migration/file-migration-provider.js +14 -13
- package/dist/cjs/migration/migrator.d.ts +44 -2
- package/dist/cjs/migration/migrator.js +77 -18
- package/dist/cjs/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/alter-table-builder.d.ts +2 -5
- package/dist/cjs/schema/alter-table-builder.js +3 -7
- package/dist/cjs/schema/schema.d.ts +9 -0
- package/dist/cjs/schema/schema.js +12 -0
- package/dist/cjs/util/column-type.d.ts +9 -1
- package/dist/cjs/util/stack-trace-utils.d.ts +1 -0
- package/dist/cjs/util/stack-trace-utils.js +17 -0
- package/dist/esm/dialect/database-introspector.d.ts +13 -0
- package/dist/esm/dialect/mysql/mysql-dialect-config.d.ts +24 -207
- package/dist/esm/dialect/mysql/mysql-dialect.d.ts +26 -3
- package/dist/esm/dialect/mysql/mysql-dialect.js +27 -5
- package/dist/esm/dialect/mysql/mysql-driver.d.ts +13 -4
- package/dist/esm/dialect/mysql/mysql-driver.js +33 -51
- package/dist/esm/dialect/mysql/mysql-introspector.d.ts +11 -1
- package/dist/esm/dialect/mysql/mysql-introspector.js +30 -8
- package/dist/esm/dialect/postgres/postgres-dialect-config.d.ts +25 -89
- package/dist/esm/dialect/postgres/postgres-dialect.d.ts +25 -5
- package/dist/esm/dialect/postgres/postgres-dialect.js +26 -7
- package/dist/esm/dialect/postgres/postgres-driver.d.ts +12 -3
- package/dist/esm/dialect/postgres/postgres-driver.js +21 -39
- package/dist/esm/dialect/postgres/postgres-introspector.d.ts +11 -1
- package/dist/esm/dialect/postgres/postgres-introspector.js +16 -5
- package/dist/esm/dialect/sqlite/sqlite-dialect-config.d.ts +25 -28
- package/dist/esm/dialect/sqlite/sqlite-dialect.d.ts +19 -3
- package/dist/esm/dialect/sqlite/sqlite-dialect.js +19 -3
- package/dist/esm/dialect/sqlite/sqlite-driver.js +4 -38
- package/dist/esm/dialect/sqlite/sqlite-introspector.d.ts +11 -1
- package/dist/esm/dialect/sqlite/sqlite-introspector.js +12 -5
- package/dist/esm/index.d.ts +153 -2
- package/dist/esm/index.js +142 -2
- package/dist/esm/migration/file-migration-provider.d.ts +21 -5
- package/dist/esm/migration/file-migration-provider.js +14 -13
- package/dist/esm/migration/migrator.d.ts +44 -2
- package/dist/esm/migration/migrator.js +76 -17
- package/dist/esm/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/alter-table-builder.d.ts +2 -5
- package/dist/esm/schema/alter-table-builder.js +4 -8
- package/dist/esm/schema/schema.d.ts +9 -0
- package/dist/esm/schema/schema.js +12 -0
- package/dist/esm/util/column-type.d.ts +9 -1
- package/dist/esm/util/stack-trace-utils.d.ts +1 -0
- package/dist/esm/util/stack-trace-utils.js +14 -0
- package/package.json +10 -15
- package/dist/cjs/index-nodeless.d.ts +0 -152
- package/dist/cjs/index-nodeless.js +0 -153
- package/dist/esm/index-nodeless.d.ts +0 -152
- package/dist/esm/index-nodeless.js +0 -142
package/README.md
CHANGED
|
@@ -70,6 +70,7 @@ All you need to do is define an interface for each table in the database and pas
|
|
|
70
70
|
interfaces to the `Kysely` constructor:
|
|
71
71
|
|
|
72
72
|
```ts
|
|
73
|
+
import { Pool } from 'pg'
|
|
73
74
|
import {
|
|
74
75
|
Kysely,
|
|
75
76
|
PostgresDialect,
|
|
@@ -125,9 +126,11 @@ interface Database {
|
|
|
125
126
|
const db = new Kysely<Database>({
|
|
126
127
|
// Use MysqlDialect for MySQL and SqliteDialect for SQLite.
|
|
127
128
|
dialect: new PostgresDialect({
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
129
|
+
pool: new Pool({
|
|
130
|
+
host: 'localhost',
|
|
131
|
+
database: 'kysely_test'
|
|
132
|
+
})
|
|
133
|
+
})
|
|
131
134
|
})
|
|
132
135
|
|
|
133
136
|
async function demo() {
|
|
@@ -319,6 +322,9 @@ need to depend on a typescript compiler, which most production environments don'
|
|
|
319
322
|
have. You will probably want to add a simple migration script to your projects like this:
|
|
320
323
|
|
|
321
324
|
```ts
|
|
325
|
+
import * as path from 'path'
|
|
326
|
+
import { Pool } from 'pg'
|
|
327
|
+
import { promises: fs } from 'fs'
|
|
322
328
|
import {
|
|
323
329
|
Kysely,
|
|
324
330
|
Migrator,
|
|
@@ -329,17 +335,20 @@ import {
|
|
|
329
335
|
async function migrateToLatest() {
|
|
330
336
|
const db = new Kysely<Database>({
|
|
331
337
|
dialect: new PostgresDialect({
|
|
332
|
-
|
|
333
|
-
|
|
338
|
+
pool: new Pool({
|
|
339
|
+
host: 'localhost',
|
|
340
|
+
database: 'kysely_test',
|
|
341
|
+
})
|
|
334
342
|
}),
|
|
335
343
|
})
|
|
336
344
|
|
|
337
345
|
const migrator = new Migrator({
|
|
338
346
|
db,
|
|
339
|
-
provider: new FileMigrationProvider(
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
347
|
+
provider: new FileMigrationProvider({
|
|
348
|
+
fs,
|
|
349
|
+
path,
|
|
350
|
+
migrationFolder: 'some/path/to/migrations',
|
|
351
|
+
})
|
|
343
352
|
})
|
|
344
353
|
|
|
345
354
|
const { error, results } = await migrator.migrateToLatest()
|
|
@@ -376,9 +385,7 @@ Kysely doesn't include drivers for deno, but you can still use Kysely as a query
|
|
|
376
385
|
or implement your own driver:
|
|
377
386
|
|
|
378
387
|
```ts
|
|
379
|
-
// We use jsdeliver to get Kysely from npm.
|
|
380
|
-
// import the `dist/esm/index-nodeless.js` file instead of the
|
|
381
|
-
// root which has node dependencies.
|
|
388
|
+
// We use jsdeliver to get Kysely from npm.
|
|
382
389
|
import {
|
|
383
390
|
DummyDriver,
|
|
384
391
|
Generated,
|
|
@@ -386,7 +393,7 @@ import {
|
|
|
386
393
|
PostgresAdapter,
|
|
387
394
|
PostgresIntrospector,
|
|
388
395
|
PostgresQueryCompiler,
|
|
389
|
-
} from 'https://cdn.jsdelivr.net/npm/kysely/dist/esm/index
|
|
396
|
+
} from 'https://cdn.jsdelivr.net/npm/kysely/dist/esm/index.js'
|
|
390
397
|
|
|
391
398
|
interface Person {
|
|
392
399
|
id: Generated<number>
|
|
@@ -425,9 +432,7 @@ console.log(sql.sql)
|
|
|
425
432
|
|
|
426
433
|
# Browser
|
|
427
434
|
|
|
428
|
-
Kysely also runs in the browser
|
|
429
|
-
module instead of the root that has node dependencies. You can also import the
|
|
430
|
-
`dist/cjs/index-nodeless.js` file if you are using commonjs modules instead of ESM.
|
|
435
|
+
Kysely also runs in the browser:
|
|
431
436
|
|
|
432
437
|
```ts
|
|
433
438
|
import {
|
|
@@ -437,7 +442,7 @@ import {
|
|
|
437
442
|
SqliteAdapter,
|
|
438
443
|
SqliteIntrospector,
|
|
439
444
|
SqliteQueryCompiler,
|
|
440
|
-
} from 'kysely
|
|
445
|
+
} from 'kysely'
|
|
441
446
|
|
|
442
447
|
interface Person {
|
|
443
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
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
13
|
+
* https://github.com/sidorares/node-mysql2#using-connection-pools
|
|
111
14
|
*/
|
|
112
|
-
|
|
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
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
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
|
|
12
|
-
*
|
|
13
|
-
*
|
|
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
|
|
13
|
-
*
|
|
14
|
-
*
|
|
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 =
|
|
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 {
|
|
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(
|
|
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:
|
|
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(
|
|
12
|
-
this.#config = (0, object_utils_js_1.freeze)({ ...
|
|
12
|
+
constructor(configOrPool) {
|
|
13
|
+
this.#config = (0, object_utils_js_1.freeze)({ ...configOrPool });
|
|
13
14
|
}
|
|
14
15
|
async init() {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
// top level too, but that's only for types. It doesn't get compiled
|
|
19
|
-
// into javascript. You can check the built javascript code.
|
|
20
|
-
const 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
|
|
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
|
-
|
|
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
|
-
|
|
84
|
-
|
|
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
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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
|
-
|
|
122
|
-
|
|
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
|
}
|