kysely 0.18.1 → 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.
- package/README.md +22 -17
- package/dist/cjs/dialect/mysql/mysql-dialect-config.d.ts +24 -207
- package/dist/cjs/dialect/mysql/mysql-dialect.d.ts +22 -7
- package/dist/cjs/dialect/mysql/mysql-dialect.js +25 -9
- package/dist/cjs/dialect/mysql/mysql-driver.d.ts +2 -15
- package/dist/cjs/dialect/mysql/mysql-driver.js +4 -35
- package/dist/cjs/dialect/postgres/postgres-dialect-config.d.ts +25 -89
- package/dist/cjs/dialect/postgres/postgres-dialect.d.ts +21 -9
- package/dist/cjs/dialect/postgres/postgres-dialect.js +24 -11
- package/dist/cjs/dialect/postgres/postgres-driver.d.ts +2 -15
- package/dist/cjs/dialect/postgres/postgres-driver.js +5 -36
- 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/index.d.ts +153 -2
- package/dist/cjs/index.js +142 -2
- package/dist/cjs/migration/file-migration-provider.d.ts +21 -5
- package/dist/cjs/migration/file-migration-provider.js +14 -13
- package/dist/cjs/plugin/noop-plugin.d.ts +1 -1
- package/dist/esm/dialect/mysql/mysql-dialect-config.d.ts +24 -207
- package/dist/esm/dialect/mysql/mysql-dialect.d.ts +22 -7
- package/dist/esm/dialect/mysql/mysql-dialect.js +25 -9
- package/dist/esm/dialect/mysql/mysql-driver.d.ts +2 -15
- package/dist/esm/dialect/mysql/mysql-driver.js +4 -35
- package/dist/esm/dialect/postgres/postgres-dialect-config.d.ts +25 -89
- package/dist/esm/dialect/postgres/postgres-dialect.d.ts +21 -9
- package/dist/esm/dialect/postgres/postgres-dialect.js +24 -11
- package/dist/esm/dialect/postgres/postgres-driver.d.ts +2 -15
- package/dist/esm/dialect/postgres/postgres-driver.js +6 -37
- 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/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/plugin/noop-plugin.d.ts +1 -1
- package/package.json +2 -7
- package/dist/cjs/index-nodeless.d.ts +0 -152
- package/dist/cjs/index-nodeless.js +0 -157
- 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>
|
|
@@ -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>[];
|
|
@@ -3,26 +3,41 @@ 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';
|
|
7
6
|
import { DialectAdapter } from '../dialect-adapter.js';
|
|
8
7
|
import { MysqlDialectConfig } from './mysql-dialect-config.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
|
-
* 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}.
|
|
15
12
|
*
|
|
16
|
-
*
|
|
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:
|
|
17
26
|
*
|
|
18
27
|
* ```ts
|
|
19
28
|
* import { createPool } from 'mysql2'
|
|
20
|
-
*
|
|
29
|
+
*
|
|
30
|
+
* new MysqlDialect({
|
|
31
|
+
* pool: async () => createPool({
|
|
32
|
+
* database: 'some_db',
|
|
33
|
+
* host: 'localhost',
|
|
34
|
+
* })
|
|
35
|
+
* })
|
|
21
36
|
* ```
|
|
22
37
|
*/
|
|
23
38
|
export declare class MysqlDialect implements Dialect {
|
|
24
39
|
#private;
|
|
25
|
-
constructor(
|
|
40
|
+
constructor(config: MysqlDialectConfig);
|
|
26
41
|
/**
|
|
27
42
|
* Creates a driver for the dialect.
|
|
28
43
|
*/
|
|
@@ -8,24 +8,40 @@ const mysql_adapter_js_1 = require("./mysql-adapter.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
|
-
* 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}.
|
|
14
12
|
*
|
|
15
|
-
*
|
|
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:
|
|
16
26
|
*
|
|
17
27
|
* ```ts
|
|
18
28
|
* import { createPool } from 'mysql2'
|
|
19
|
-
*
|
|
29
|
+
*
|
|
30
|
+
* new MysqlDialect({
|
|
31
|
+
* pool: async () => createPool({
|
|
32
|
+
* database: 'some_db',
|
|
33
|
+
* host: 'localhost',
|
|
34
|
+
* })
|
|
35
|
+
* })
|
|
20
36
|
* ```
|
|
21
37
|
*/
|
|
22
38
|
class MysqlDialect {
|
|
23
|
-
#
|
|
24
|
-
constructor(
|
|
25
|
-
this.#
|
|
39
|
+
#config;
|
|
40
|
+
constructor(config) {
|
|
41
|
+
this.#config = config;
|
|
26
42
|
}
|
|
27
43
|
createDriver() {
|
|
28
|
-
return new mysql_driver_js_1.MysqlDriver(this.#
|
|
44
|
+
return new mysql_driver_js_1.MysqlDriver(this.#config);
|
|
29
45
|
}
|
|
30
46
|
createQueryCompiler() {
|
|
31
47
|
return new mysql_query_compiler_js_1.MysqlQueryCompiler();
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { DatabaseConnection, QueryResult } from '../../driver/database-connection.js';
|
|
2
2
|
import { Driver, TransactionSettings } from '../../driver/driver.js';
|
|
3
3
|
import { CompiledQuery } from '../../query-compiler/compiled-query.js';
|
|
4
|
-
import { MysqlDialectConfig } from './mysql-dialect-config.js';
|
|
4
|
+
import { MysqlDialectConfig, MysqlPoolConnection } from './mysql-dialect-config.js';
|
|
5
5
|
declare const PRIVATE_RELEASE_METHOD: unique symbol;
|
|
6
6
|
export declare class MysqlDriver implements Driver {
|
|
7
7
|
#private;
|
|
8
|
-
constructor(configOrPool: MysqlDialectConfig
|
|
8
|
+
constructor(configOrPool: MysqlDialectConfig);
|
|
9
9
|
/**
|
|
10
10
|
* Initializes the driver.
|
|
11
11
|
*
|
|
@@ -43,19 +43,6 @@ export declare class MysqlDriver implements Driver {
|
|
|
43
43
|
*/
|
|
44
44
|
destroy(): Promise<void>;
|
|
45
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
46
|
declare class MysqlConnection implements DatabaseConnection {
|
|
60
47
|
#private;
|
|
61
48
|
constructor(rawConnection: MysqlPoolConnection);
|
|
@@ -10,23 +10,12 @@ class MysqlDriver {
|
|
|
10
10
|
#connections = new WeakMap();
|
|
11
11
|
#pool;
|
|
12
12
|
constructor(configOrPool) {
|
|
13
|
-
|
|
14
|
-
this.#pool = configOrPool;
|
|
15
|
-
}
|
|
16
|
-
else {
|
|
17
|
-
this.#config = (0, object_utils_js_1.freeze)({ ...configOrPool });
|
|
18
|
-
}
|
|
13
|
+
this.#config = (0, object_utils_js_1.freeze)({ ...configOrPool });
|
|
19
14
|
}
|
|
20
15
|
async init() {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
}
|
|
16
|
+
this.#pool = (0, object_utils_js_1.isFunction)(this.#config.pool)
|
|
17
|
+
? await this.#config.pool()
|
|
18
|
+
: this.#config.pool;
|
|
30
19
|
}
|
|
31
20
|
async acquireConnection() {
|
|
32
21
|
const rawConnection = await this.#acquireConnection();
|
|
@@ -85,29 +74,9 @@ class MysqlDriver {
|
|
|
85
74
|
}
|
|
86
75
|
}
|
|
87
76
|
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
77
|
function isOkPacket(obj) {
|
|
92
78
|
return (0, object_utils_js_1.isObject)(obj) && 'insertId' in obj && 'affectedRows' in obj;
|
|
93
79
|
}
|
|
94
|
-
async function importMysqlPoolFactory() {
|
|
95
|
-
try {
|
|
96
|
-
// The imported module name must be a string literal to make
|
|
97
|
-
// some bundlers work. So don't move this code behind a helper
|
|
98
|
-
// for example.
|
|
99
|
-
const mysqlModule = await Promise.resolve().then(() => require('mysql2'));
|
|
100
|
-
if ((0, object_utils_js_1.isFunction)(mysqlModule.createPool)) {
|
|
101
|
-
return mysqlModule.createPool;
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
return mysqlModule.default.createPool;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
catch (error) {
|
|
108
|
-
throw new Error('MySQL client not installed. Please run `npm install mysql2`');
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
80
|
class MysqlConnection {
|
|
112
81
|
#rawConnection;
|
|
113
82
|
constructor(rawConnection) {
|
|
@@ -1,103 +1,39 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import * as stream from 'stream';
|
|
3
|
-
import { ConnectionOptions } from 'tls';
|
|
4
1
|
import { DatabaseConnection } from '../../driver/database-connection.js';
|
|
5
2
|
/**
|
|
6
3
|
* Config for the PostgreSQL dialect.
|
|
7
|
-
*
|
|
8
|
-
* This interface is equal to `pg` library's `Pool` config:
|
|
9
|
-
*
|
|
10
|
-
* https://node-postgres.com/api/pool
|
|
11
|
-
* https://node-postgres.com/api/client
|
|
12
4
|
*/
|
|
13
5
|
export interface PostgresDialectConfig {
|
|
14
6
|
/**
|
|
15
|
-
*
|
|
16
|
-
*/
|
|
17
|
-
user?: string;
|
|
18
|
-
/**
|
|
19
|
-
* default process.env.PGDATABASE || process.env.USER
|
|
20
|
-
*/
|
|
21
|
-
database?: string;
|
|
22
|
-
/**
|
|
23
|
-
* default process.env.PGPASSWORD
|
|
24
|
-
*/
|
|
25
|
-
password?: string | (() => string | Promise<string>);
|
|
26
|
-
/**
|
|
27
|
-
* default process.env.PGPORT
|
|
28
|
-
*/
|
|
29
|
-
port?: number;
|
|
30
|
-
/**
|
|
31
|
-
* default process.env.PGHOST
|
|
32
|
-
*/
|
|
33
|
-
host?: string;
|
|
34
|
-
/**
|
|
35
|
-
* e.g. postgres:*user:password@host:5432/database
|
|
36
|
-
*/
|
|
37
|
-
connectionString?: string;
|
|
38
|
-
/**
|
|
39
|
-
* passed directly to node.TLSSocket, supports all tls.connect options
|
|
40
|
-
*/
|
|
41
|
-
ssl?: boolean | ConnectionOptions;
|
|
42
|
-
/**
|
|
43
|
-
* Number of milliseconds to wait before timing out when connecting a new client
|
|
44
|
-
* by default this is 0 which means no timeout.
|
|
45
|
-
*/
|
|
46
|
-
connectionTimeoutMillis?: number;
|
|
47
|
-
/**
|
|
48
|
-
* Maximum number of clients the pool should contain. by default this is set to 10.
|
|
49
|
-
*/
|
|
50
|
-
max?: number;
|
|
51
|
-
/**
|
|
52
|
-
* Minimum number of clients the pool should contain
|
|
53
|
-
*/
|
|
54
|
-
min?: number;
|
|
55
|
-
/**
|
|
56
|
-
* Number of milliseconds a client must sit idle in the pool and not be checked out
|
|
57
|
-
* before it is disconnected from the backend and discarded.
|
|
7
|
+
* A postgres Pool instance or a function that returns one.
|
|
58
8
|
*
|
|
59
|
-
*
|
|
60
|
-
*/
|
|
61
|
-
idleTimeoutMillis?: number;
|
|
62
|
-
/**
|
|
63
|
-
* Default behavior is the pool will keep clients open & connected to the backend
|
|
64
|
-
* until idleTimeoutMillis expire for each client and node will maintain a ref
|
|
65
|
-
* to the socket on the client, keeping the event loop alive until all clients are closed
|
|
66
|
-
* after being idle or the pool is manually shutdown with `pool.end()`.
|
|
9
|
+
* If a function is provided, it's called once when the first query is executed.
|
|
67
10
|
*
|
|
68
|
-
*
|
|
69
|
-
* as soon as all clients in the pool are idle, even if their socket is still open
|
|
70
|
-
* to the PostgreSQL server. This can be handy in scripts & tests
|
|
71
|
-
* where you don't want to wait for your clients to go idle before your process exits.
|
|
72
|
-
*/
|
|
73
|
-
allowExitOnIdle?: boolean;
|
|
74
|
-
log?: (...messages: any[]) => void;
|
|
75
|
-
/**
|
|
76
|
-
* Number of milliseconds before a statement in query will time out, default is no timeout.
|
|
11
|
+
* https://node-postgres.com/api/pool
|
|
77
12
|
*/
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Number of milliseconds before terminating any session with an open idle transaction.
|
|
81
|
-
* Default is no timeout.
|
|
82
|
-
*/
|
|
83
|
-
idle_in_transaction_session_timeout?: number;
|
|
84
|
-
/**
|
|
85
|
-
* Number of milliseconds before a query call will timeout, default is no timeout.
|
|
86
|
-
*/
|
|
87
|
-
query_timeout?: number;
|
|
88
|
-
/**
|
|
89
|
-
* The name of the application that created this Client instance.
|
|
90
|
-
*/
|
|
91
|
-
application_name?: string;
|
|
13
|
+
pool: PostgresPool | (() => Promise<PostgresPool>);
|
|
92
14
|
/**
|
|
93
15
|
* Called once for each created connection.
|
|
94
|
-
*
|
|
95
|
-
* This is a Kysely specific feature and does not come from the `pg` module.
|
|
96
16
|
*/
|
|
97
17
|
onCreateConnection?: (connection: DatabaseConnection) => Promise<void>;
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* This interface is the subset of pg driver's `Pool` class that
|
|
21
|
+
* kysely needs.
|
|
22
|
+
*
|
|
23
|
+
* We don't use the type from `pg` here to not have a dependency to it.
|
|
24
|
+
*
|
|
25
|
+
* https://node-postgres.com/api/pool
|
|
26
|
+
*/
|
|
27
|
+
export interface PostgresPool {
|
|
28
|
+
connect(): Promise<PostgresPoolClient>;
|
|
29
|
+
end(): Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
export interface PostgresPoolClient {
|
|
32
|
+
query<R>(sql: string, parameters: ReadonlyArray<unknown>): Promise<PostgresQueryResult<R>>;
|
|
33
|
+
release(): void;
|
|
34
|
+
}
|
|
35
|
+
export interface PostgresQueryResult<R> {
|
|
36
|
+
command: 'UPDATE' | 'DELETE' | 'INSERT' | 'SELECT';
|
|
37
|
+
rowCount: number;
|
|
38
|
+
rows: R[];
|
|
103
39
|
}
|