kysely 0.16.3 → 0.16.7
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 +122 -9
- package/dist/cjs/dialect/mysql/mysql-dialect-config.d.ts +205 -0
- package/dist/cjs/dialect/mysql/mysql-dialect-config.js +2 -0
- package/dist/cjs/dialect/mysql/mysql-dialect.d.ts +1 -205
- package/dist/cjs/dialect/mysql/mysql-dialect.js +2 -1
- package/dist/cjs/dialect/mysql/mysql-driver.d.ts +1 -1
- package/dist/cjs/dialect/mysql/mysql-driver.js +11 -3
- package/dist/cjs/dialect/postgres/postgres-dialect-config.d.ts +103 -0
- package/dist/cjs/dialect/postgres/postgres-dialect-config.js +2 -0
- package/dist/cjs/dialect/postgres/postgres-dialect.d.ts +1 -103
- package/dist/cjs/dialect/postgres/postgres-dialect.js +2 -1
- package/dist/cjs/dialect/postgres/postgres-driver.d.ts +1 -1
- package/dist/cjs/dialect/postgres/postgres-driver.js +12 -3
- package/dist/cjs/dialect/sqlite/sqlite-dialect-config.d.ts +43 -0
- package/dist/cjs/dialect/sqlite/sqlite-dialect-config.js +2 -0
- package/dist/cjs/dialect/sqlite/sqlite-dialect.d.ts +1 -43
- package/dist/cjs/dialect/sqlite/sqlite-dialect.js +2 -1
- package/dist/cjs/dialect/sqlite/sqlite-driver.d.ts +1 -1
- package/dist/cjs/dialect/sqlite/sqlite-driver.js +11 -3
- package/dist/cjs/driver/dummy-driver.d.ts +68 -0
- package/dist/cjs/driver/dummy-driver.js +62 -0
- package/dist/cjs/driver/runtime-driver.d.ts +1 -1
- package/dist/cjs/driver/runtime-driver.js +30 -33
- package/dist/cjs/index-nodeless.d.ts +2 -0
- package/dist/cjs/index-nodeless.js +2 -0
- package/dist/cjs/index.d.ts +3 -0
- package/dist/cjs/index.js +3 -0
- package/dist/cjs/kysely.d.ts +8 -6
- package/dist/cjs/kysely.js +8 -6
- package/dist/cjs/migration/migrator.d.ts +2 -2
- package/dist/cjs/operation-node/primitive-value-list-node.d.ts +2 -3
- package/dist/cjs/operation-node/value-node.d.ts +3 -4
- package/dist/cjs/parser/complex-expression-parser.d.ts +1 -0
- package/dist/cjs/parser/complex-expression-parser.js +5 -1
- package/dist/cjs/parser/default-value-parser.d.ts +2 -3
- package/dist/cjs/parser/default-value-parser.js +2 -2
- package/dist/cjs/parser/insert-values-parser.js +4 -3
- package/dist/cjs/parser/reference-parser.d.ts +2 -3
- package/dist/cjs/parser/value-parser.d.ts +2 -3
- package/dist/cjs/parser/value-parser.js +6 -7
- package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-plugin.d.ts +48 -0
- package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-plugin.js +19 -0
- package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-transformer.d.ts +9 -0
- package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-transformer.js +42 -0
- package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.d.ts +0 -0
- package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.js +0 -0
- package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.d.ts +0 -0
- package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.js +0 -0
- package/dist/cjs/query-builder/delete-query-builder.d.ts +51 -9
- package/dist/cjs/query-builder/delete-query-builder.js +56 -8
- package/dist/cjs/query-builder/expression-builder.d.ts +1 -1
- package/dist/cjs/query-builder/expression-builder.js +2 -2
- package/dist/cjs/query-builder/function-builder.d.ts +29 -5
- package/dist/cjs/query-builder/function-builder.js +31 -7
- package/dist/cjs/query-builder/insert-query-builder.d.ts +53 -11
- package/dist/cjs/query-builder/insert-query-builder.js +58 -10
- package/dist/cjs/query-builder/select-query-builder.d.ts +58 -16
- package/dist/cjs/query-builder/select-query-builder.js +64 -15
- package/dist/cjs/query-builder/update-query-builder.d.ts +52 -9
- package/dist/cjs/query-builder/update-query-builder.js +57 -8
- package/dist/cjs/query-compiler/default-query-compiler.d.ts +1 -2
- package/dist/cjs/query-creator.d.ts +2 -2
- package/dist/cjs/query-creator.js +2 -2
- package/dist/cjs/schema/alter-table-builder.d.ts +30 -4
- package/dist/cjs/schema/alter-table-builder.js +2 -5
- package/dist/cjs/schema/column-definition-builder.d.ts +27 -2
- package/dist/cjs/schema/create-view-builder.js +1 -1
- package/dist/cjs/util/log.d.ts +1 -0
- package/dist/cjs/util/log.js +3 -0
- package/dist/cjs/util/object-utils.d.ts +5 -5
- package/dist/cjs/util/object-utils.js +52 -11
- package/dist/cjs/util/type-utils.d.ts +21 -0
- package/dist/esm/dialect/mysql/mysql-dialect-config.d.ts +205 -0
- package/dist/esm/dialect/mysql/mysql-dialect-config.js +2 -0
- package/dist/esm/dialect/mysql/mysql-dialect.d.ts +1 -205
- package/dist/esm/dialect/mysql/mysql-dialect.js +2 -1
- package/dist/esm/dialect/mysql/mysql-driver.d.ts +1 -1
- package/dist/esm/dialect/mysql/mysql-driver.js +12 -4
- package/dist/esm/dialect/postgres/postgres-dialect-config.d.ts +103 -0
- package/dist/esm/dialect/postgres/postgres-dialect-config.js +2 -0
- package/dist/esm/dialect/postgres/postgres-dialect.d.ts +1 -103
- package/dist/esm/dialect/postgres/postgres-dialect.js +2 -1
- package/dist/esm/dialect/postgres/postgres-driver.d.ts +1 -1
- package/dist/esm/dialect/postgres/postgres-driver.js +12 -3
- package/dist/esm/dialect/sqlite/sqlite-dialect-config.d.ts +43 -0
- package/dist/esm/dialect/sqlite/sqlite-dialect-config.js +2 -0
- package/dist/esm/dialect/sqlite/sqlite-dialect.d.ts +1 -43
- package/dist/esm/dialect/sqlite/sqlite-dialect.js +2 -1
- package/dist/esm/dialect/sqlite/sqlite-driver.d.ts +1 -1
- package/dist/esm/dialect/sqlite/sqlite-driver.js +12 -4
- package/dist/esm/driver/dummy-driver.d.ts +68 -0
- package/dist/esm/driver/dummy-driver.js +59 -0
- package/dist/esm/driver/runtime-driver.d.ts +1 -1
- package/dist/esm/driver/runtime-driver.js +29 -32
- package/dist/esm/index-nodeless.d.ts +2 -0
- package/dist/esm/index-nodeless.js +2 -0
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/kysely.d.ts +8 -6
- package/dist/esm/kysely.js +8 -6
- package/dist/esm/migration/migrator.d.ts +2 -2
- package/dist/esm/operation-node/primitive-value-list-node.d.ts +2 -3
- package/dist/esm/operation-node/value-node.d.ts +3 -4
- package/dist/esm/parser/complex-expression-parser.d.ts +1 -0
- package/dist/esm/parser/complex-expression-parser.js +3 -0
- package/dist/esm/parser/default-value-parser.d.ts +2 -3
- package/dist/esm/parser/default-value-parser.js +2 -2
- package/dist/esm/parser/insert-values-parser.js +5 -4
- package/dist/esm/parser/reference-parser.d.ts +2 -3
- package/dist/esm/parser/reference-parser.js +1 -1
- package/dist/esm/parser/value-parser.d.ts +2 -3
- package/dist/esm/parser/value-parser.js +8 -9
- package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-plugin.d.ts +48 -0
- package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-plugin.js +16 -0
- package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-transformer.d.ts +9 -0
- package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-transformer.js +39 -0
- package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.d.ts +0 -0
- package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.js +0 -0
- package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.d.ts +0 -0
- package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.js +0 -0
- package/dist/esm/query-builder/delete-query-builder.d.ts +51 -9
- package/dist/esm/query-builder/delete-query-builder.js +56 -8
- package/dist/esm/query-builder/expression-builder.d.ts +1 -1
- package/dist/esm/query-builder/expression-builder.js +1 -1
- package/dist/esm/query-builder/function-builder.d.ts +29 -5
- package/dist/esm/query-builder/function-builder.js +31 -7
- package/dist/esm/query-builder/insert-query-builder.d.ts +53 -11
- package/dist/esm/query-builder/insert-query-builder.js +58 -10
- package/dist/esm/query-builder/select-query-builder.d.ts +58 -16
- package/dist/esm/query-builder/select-query-builder.js +64 -15
- package/dist/esm/query-builder/update-query-builder.d.ts +52 -9
- package/dist/esm/query-builder/update-query-builder.js +57 -8
- package/dist/esm/query-compiler/default-query-compiler.d.ts +1 -2
- package/dist/esm/query-creator.d.ts +2 -2
- package/dist/esm/query-creator.js +2 -2
- package/dist/esm/schema/alter-table-builder.d.ts +30 -4
- package/dist/esm/schema/alter-table-builder.js +2 -5
- package/dist/esm/schema/column-definition-builder.d.ts +27 -2
- package/dist/esm/schema/create-view-builder.js +1 -1
- package/dist/esm/util/log.d.ts +1 -0
- package/dist/esm/util/log.js +3 -0
- package/dist/esm/util/object-utils.d.ts +5 -5
- package/dist/esm/util/object-utils.js +50 -9
- package/dist/esm/util/type-utils.d.ts +21 -0
- package/package.json +11 -5
- package/.prettierignore +0 -1
- package/dist/cjs/util/dynamic-import.d.ts +0 -2
- package/dist/cjs/util/dynamic-import.js +0 -32
- package/dist/esm/util/dynamic-import.d.ts +0 -2
- package/dist/esm/util/dynamic-import.js +0 -28
package/README.md
CHANGED
|
@@ -3,14 +3,15 @@
|
|
|
3
3
|
|
|
4
4
|
# [Kysely](https://koskimas.github.io/kysely/index.html)
|
|
5
5
|
|
|
6
|
-
A type-safe and autocompletion-friendly typescript SQL query builder
|
|
7
|
-
[
|
|
6
|
+
A type-safe and autocompletion-friendly typescript SQL query builder. Inspired by [knex](http://knexjs.org/).
|
|
7
|
+
Mainly developed for [node.js](https://nodejs.org/en/) but also runs on [deno](https://deno.land/) and
|
|
8
|
+
in the browser.
|
|
8
9
|
|
|
9
10
|

|
|
10
11
|
|
|
11
|
-
Kysely
|
|
12
|
-
you are writing. The result type only has the selected columns with correct types and aliases.
|
|
13
|
-
|
|
12
|
+
Kysely makes sure you only refer to tables and columns that are visible to the part of the query
|
|
13
|
+
you are writing. The result type only has the selected columns with correct types and aliases. As an
|
|
14
|
+
added bonus you get autocompletion for all that stuff.
|
|
14
15
|
|
|
15
16
|
As you can see in the gif above, through the pure magic of modern typescript, Kysely is even able to parse
|
|
16
17
|
the alias given to `pet.name` and add the `pet_name` column to the result row type. Kysely is able to infer
|
|
@@ -32,7 +33,10 @@ You can find a more thorough introduction [here](https://www.jakso.me/blog/kysel
|
|
|
32
33
|
- [Installation](#installation)
|
|
33
34
|
- [Minimal example](#minimal-example)
|
|
34
35
|
- [Query examples](#query-examples)
|
|
36
|
+
- [Recipes](https://github.com/koskimas/kysely/tree/master/recipes)
|
|
35
37
|
- [Migrations](#migrations)
|
|
38
|
+
- [Deno](#deno)
|
|
39
|
+
- [Browser](#browser)
|
|
36
40
|
- [API reference](https://koskimas.github.io/kysely/index.html)
|
|
37
41
|
- [Changelog](https://github.com/koskimas/kysely/releases)
|
|
38
42
|
|
|
@@ -162,19 +166,19 @@ type UpdateablePerson = Updateable<PersonTable>
|
|
|
162
166
|
## Select queries
|
|
163
167
|
|
|
164
168
|
You can find examples of select queries in the documentation of the
|
|
165
|
-
[select method](https://koskimas.github.io/kysely/classes/
|
|
166
|
-
the [where method](https://koskimas.github.io/kysely/classes/
|
|
169
|
+
[select method](https://koskimas.github.io/kysely/classes/SelectQueryBuilder.html#select) and
|
|
170
|
+
the [where method](https://koskimas.github.io/kysely/classes/SelectQueryBuilder.html#where)
|
|
167
171
|
among other places.
|
|
168
172
|
|
|
169
173
|
## Update queries
|
|
170
174
|
|
|
171
|
-
See the [set method](https://koskimas.github.io/kysely/classes/
|
|
175
|
+
See the [set method](https://koskimas.github.io/kysely/classes/UpdateQueryBuilder.html#set) and the
|
|
172
176
|
[updateTable method](https://koskimas.github.io/kysely/classes/Kysely.html#updateTable)
|
|
173
177
|
documentation.
|
|
174
178
|
|
|
175
179
|
## Insert queries
|
|
176
180
|
|
|
177
|
-
See the [values method](https://koskimas.github.io/kysely/classes/
|
|
181
|
+
See the [values method](https://koskimas.github.io/kysely/classes/InsertQueryBuilder.html#values) and the
|
|
178
182
|
[insertInto method](https://koskimas.github.io/kysely/classes/Kysely.html#insertInto)
|
|
179
183
|
documentation.
|
|
180
184
|
|
|
@@ -341,6 +345,8 @@ async function migrateToLatest() {
|
|
|
341
345
|
console.error(error)
|
|
342
346
|
process.exit(1)
|
|
343
347
|
}
|
|
348
|
+
|
|
349
|
+
await db.destroy()
|
|
344
350
|
}
|
|
345
351
|
|
|
346
352
|
migrateToLatest()
|
|
@@ -350,6 +356,113 @@ The migration methods use a lock on the database level, and parallel calls are e
|
|
|
350
356
|
This means that you can safely call `migrateToLatest` and other migration methods from multiple
|
|
351
357
|
server instances simultaneously and the migrations are guaranteed to only be executed once.
|
|
352
358
|
|
|
359
|
+
# Deno
|
|
360
|
+
|
|
361
|
+
Kysely doesn't include drivers for deno, but you can still use Kysely as a query builder
|
|
362
|
+
or implement your own driver:
|
|
363
|
+
|
|
364
|
+
```ts
|
|
365
|
+
// We use jsdeliver to get Kysely from npm. We also need to
|
|
366
|
+
// import the `dist/esm/index-nodeless.js` file instead of the
|
|
367
|
+
// root which has node dependencies.
|
|
368
|
+
import {
|
|
369
|
+
DummyDriver,
|
|
370
|
+
Generated,
|
|
371
|
+
Kysely,
|
|
372
|
+
PostgresAdapter,
|
|
373
|
+
PostgresIntrospector,
|
|
374
|
+
PostgresQueryCompiler,
|
|
375
|
+
} from 'https://cdn.jsdelivr.net/npm/kysely/dist/esm/index-nodeless.js'
|
|
376
|
+
|
|
377
|
+
interface Person {
|
|
378
|
+
id: Generated<number>
|
|
379
|
+
first_name: string
|
|
380
|
+
last_name: string | null
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
interface Database {
|
|
384
|
+
person: Person
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const db = new Kysely<Database>({
|
|
388
|
+
dialect: {
|
|
389
|
+
createAdapter() {
|
|
390
|
+
return new PostgresAdapter()
|
|
391
|
+
},
|
|
392
|
+
createDriver() {
|
|
393
|
+
// You need a driver to be able to execute queries. In this example
|
|
394
|
+
// we use the dummy driver that never does anything.
|
|
395
|
+
return new DummyDriver()
|
|
396
|
+
},
|
|
397
|
+
createIntrospector(db: Kysely<unknown>) {
|
|
398
|
+
return new PostgresIntrospector(db)
|
|
399
|
+
},
|
|
400
|
+
createQueryCompiler() {
|
|
401
|
+
return new PostgresQueryCompiler()
|
|
402
|
+
},
|
|
403
|
+
},
|
|
404
|
+
})
|
|
405
|
+
|
|
406
|
+
const query = db.selectFrom('person').select('id')
|
|
407
|
+
const sql = query.compile()
|
|
408
|
+
|
|
409
|
+
console.log(sql.sql)
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
# Browser
|
|
413
|
+
|
|
414
|
+
Kysely also runs in the browser as long as you import the `dist/esm/index-nodeless.js`
|
|
415
|
+
module instead of the root that has node dependencies. You can also import the
|
|
416
|
+
`dist/cjs/index-nodeless.js` file if you are using commonjs modules instead of ESM.
|
|
417
|
+
|
|
418
|
+
```ts
|
|
419
|
+
import {
|
|
420
|
+
Kysely,
|
|
421
|
+
Generated,
|
|
422
|
+
DummyDriver,
|
|
423
|
+
SqliteAdapter,
|
|
424
|
+
SqliteIntrospector,
|
|
425
|
+
SqliteQueryCompiler,
|
|
426
|
+
} from 'kysely/dist/esm/index-nodeless.js'
|
|
427
|
+
|
|
428
|
+
interface Person {
|
|
429
|
+
id: Generated<number>
|
|
430
|
+
first_name: string
|
|
431
|
+
last_name: string | null
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
interface Database {
|
|
435
|
+
person: Person
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
const db = new Kysely<Database>({
|
|
439
|
+
dialect: {
|
|
440
|
+
createAdapter() {
|
|
441
|
+
return new SqliteAdapter()
|
|
442
|
+
},
|
|
443
|
+
createDriver() {
|
|
444
|
+
return new DummyDriver()
|
|
445
|
+
},
|
|
446
|
+
createIntrospector(db: Kysely<unknown>) {
|
|
447
|
+
return new SqliteIntrospector(db)
|
|
448
|
+
},
|
|
449
|
+
createQueryCompiler() {
|
|
450
|
+
return new SqliteQueryCompiler()
|
|
451
|
+
},
|
|
452
|
+
},
|
|
453
|
+
})
|
|
454
|
+
|
|
455
|
+
window.addEventListener('load', () => {
|
|
456
|
+
const sql = db.selectFrom('person').select('id').compile()
|
|
457
|
+
|
|
458
|
+
const result = document.createElement('span')
|
|
459
|
+
result.id = 'result'
|
|
460
|
+
result.innerHTML = sql.sql
|
|
461
|
+
|
|
462
|
+
document.body.appendChild(result)
|
|
463
|
+
})
|
|
464
|
+
```
|
|
465
|
+
|
|
353
466
|
# Why not just contribute to knex
|
|
354
467
|
|
|
355
468
|
Kysely is very similar to knex, but it also attempts to fix things that I personally find not-so-good
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { DatabaseConnection } from '../../driver/database-connection.js';
|
|
2
|
+
/**
|
|
3
|
+
* Config for the MySQL dialect.
|
|
4
|
+
*
|
|
5
|
+
* This interface is equal to `mysql2` library's pool config.
|
|
6
|
+
*
|
|
7
|
+
* https://github.com/sidorares/node-mysql2#using-connection-pools
|
|
8
|
+
*/
|
|
9
|
+
export interface MysqlDialectConfig {
|
|
10
|
+
/**
|
|
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.
|
|
102
|
+
*
|
|
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;
|
|
109
|
+
*
|
|
110
|
+
* (Default: false)
|
|
111
|
+
*/
|
|
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
|
+
/**
|
|
166
|
+
* Called once for each created connection.
|
|
167
|
+
*
|
|
168
|
+
* This is a Kysely specific feature and does not come from the `mysql2` module.
|
|
169
|
+
*/
|
|
170
|
+
onCreateConnection?: (connection: DatabaseConnection) => Promise<void>;
|
|
171
|
+
}
|
|
172
|
+
export interface MysqlSslOptions {
|
|
173
|
+
/**
|
|
174
|
+
* A string or buffer holding the PFX or PKCS12 encoded private key, certificate and CA certificates
|
|
175
|
+
*/
|
|
176
|
+
pfx?: string;
|
|
177
|
+
/**
|
|
178
|
+
* A string holding the PEM encoded private key
|
|
179
|
+
*/
|
|
180
|
+
key?: string;
|
|
181
|
+
/**
|
|
182
|
+
* A string of passphrase for the private key or pfx
|
|
183
|
+
*/
|
|
184
|
+
passphrase?: string;
|
|
185
|
+
/**
|
|
186
|
+
* A string holding the PEM encoded certificate
|
|
187
|
+
*/
|
|
188
|
+
cert?: string;
|
|
189
|
+
/**
|
|
190
|
+
* Either a string or list of strings of PEM encoded CA certificates to trust.
|
|
191
|
+
*/
|
|
192
|
+
ca?: string | string[];
|
|
193
|
+
/**
|
|
194
|
+
* Either a string or list of strings of PEM encoded CRLs (Certificate Revocation List)
|
|
195
|
+
*/
|
|
196
|
+
crl?: string | string[];
|
|
197
|
+
/**
|
|
198
|
+
* A string describing the ciphers to use or exclude
|
|
199
|
+
*/
|
|
200
|
+
ciphers?: string;
|
|
201
|
+
/**
|
|
202
|
+
* You can also connect to a MySQL server without properly providing the appropriate CA to trust. You should not do this.
|
|
203
|
+
*/
|
|
204
|
+
rejectUnauthorized?: boolean;
|
|
205
|
+
}
|
|
@@ -4,7 +4,7 @@ import { QueryCompiler } from '../../query-compiler/query-compiler.js';
|
|
|
4
4
|
import { Dialect } from '../dialect.js';
|
|
5
5
|
import { DatabaseIntrospector } from '../database-introspector.js';
|
|
6
6
|
import { DialectAdapter } from '../dialect-adapter.js';
|
|
7
|
-
import {
|
|
7
|
+
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
|
*
|
|
@@ -36,207 +36,3 @@ export declare class MysqlDialect implements Dialect {
|
|
|
36
36
|
*/
|
|
37
37
|
createIntrospector(db: Kysely<any>): DatabaseIntrospector;
|
|
38
38
|
}
|
|
39
|
-
/**
|
|
40
|
-
* Config for the MySQL dialect.
|
|
41
|
-
*
|
|
42
|
-
* This interface is equal to `mysql2` library's pool config.
|
|
43
|
-
*
|
|
44
|
-
* https://github.com/sidorares/node-mysql2#using-connection-pools
|
|
45
|
-
*/
|
|
46
|
-
export interface MysqlDialectConfig {
|
|
47
|
-
/**
|
|
48
|
-
* The MySQL user to authenticate as
|
|
49
|
-
*/
|
|
50
|
-
user?: string;
|
|
51
|
-
/**
|
|
52
|
-
* The password of that MySQL user
|
|
53
|
-
*/
|
|
54
|
-
password?: string;
|
|
55
|
-
/**
|
|
56
|
-
* Name of the database to use for this connection
|
|
57
|
-
*/
|
|
58
|
-
database?: string;
|
|
59
|
-
/**
|
|
60
|
-
* The charset for the connection. This is called 'collation' in the SQL-level of MySQL (like utf8_general_ci).
|
|
61
|
-
* If a SQL-level charset is specified (like utf8mb4) then the default collation for that charset is used.
|
|
62
|
-
* (Default: 'UTF8_GENERAL_CI')
|
|
63
|
-
*/
|
|
64
|
-
charset?: string;
|
|
65
|
-
/**
|
|
66
|
-
* The hostname of the database you are connecting to. (Default: localhost)
|
|
67
|
-
*/
|
|
68
|
-
host?: string;
|
|
69
|
-
/**
|
|
70
|
-
* The port number to connect to. (Default: 3306)
|
|
71
|
-
*/
|
|
72
|
-
port?: number;
|
|
73
|
-
/**
|
|
74
|
-
* The source IP address to use for TCP connection
|
|
75
|
-
*/
|
|
76
|
-
localAddress?: string;
|
|
77
|
-
/**
|
|
78
|
-
* The path to a unix domain socket to connect to. When used host and port are ignored
|
|
79
|
-
*/
|
|
80
|
-
socketPath?: string;
|
|
81
|
-
/**
|
|
82
|
-
* The timezone used to store local dates. (Default: 'local')
|
|
83
|
-
*/
|
|
84
|
-
timezone?: string | 'local';
|
|
85
|
-
/**
|
|
86
|
-
* The milliseconds before a timeout occurs during the initial connection to the MySQL server. (Default: 10 seconds)
|
|
87
|
-
*/
|
|
88
|
-
connectTimeout?: number;
|
|
89
|
-
/**
|
|
90
|
-
* Stringify objects instead of converting to values. (Default: 'false')
|
|
91
|
-
*/
|
|
92
|
-
stringifyObjects?: boolean;
|
|
93
|
-
/**
|
|
94
|
-
* Allow connecting to MySQL instances that ask for the old (insecure) authentication method. (Default: false)
|
|
95
|
-
*/
|
|
96
|
-
insecureAuth?: boolean;
|
|
97
|
-
/**
|
|
98
|
-
* Determines if column values should be converted to native JavaScript types. It is not recommended (and may go away / change in the future)
|
|
99
|
-
* to disable type casting, but you can currently do so on either the connection or query level. (Default: true)
|
|
100
|
-
*
|
|
101
|
-
* You can also specify a function (field: any, next: () => void) => {} to do the type casting yourself.
|
|
102
|
-
*
|
|
103
|
-
* WARNING: YOU MUST INVOKE the parser using one of these three field functions in your custom typeCast callback. They can only be called once.
|
|
104
|
-
*
|
|
105
|
-
* field.string()
|
|
106
|
-
* field.buffer()
|
|
107
|
-
* field.geometry()
|
|
108
|
-
*
|
|
109
|
-
* are aliases for
|
|
110
|
-
*
|
|
111
|
-
* parser.parseLengthCodedString()
|
|
112
|
-
* parser.parseLengthCodedBuffer()
|
|
113
|
-
* parser.parseGeometryValue()
|
|
114
|
-
*
|
|
115
|
-
* You can find which field function you need to use by looking at: RowDataPacket.prototype._typeCast
|
|
116
|
-
*/
|
|
117
|
-
typeCast?: boolean | ((field: any, next: () => void) => any);
|
|
118
|
-
/**
|
|
119
|
-
* A custom query format function
|
|
120
|
-
*/
|
|
121
|
-
queryFormat?: (query: string, values: any) => void;
|
|
122
|
-
/**
|
|
123
|
-
* When dealing with big numbers (BIGINT and DECIMAL columns) in the database, you should enable this option
|
|
124
|
-
* (Default: false)
|
|
125
|
-
*/
|
|
126
|
-
supportBigNumbers?: boolean;
|
|
127
|
-
/**
|
|
128
|
-
* Enabling both supportBigNumbers and bigNumberStrings forces big numbers (BIGINT and DECIMAL columns) to be
|
|
129
|
-
* always returned as JavaScript String objects (Default: false). Enabling supportBigNumbers but leaving
|
|
130
|
-
* bigNumberStrings disabled will return big numbers as String objects only when they cannot be accurately
|
|
131
|
-
* represented with [JavaScript Number objects] (http://ecma262-5.com/ELS5_HTML.htm#Section_8.5)
|
|
132
|
-
* (which happens when they exceed the [-2^53, +2^53] range), otherwise they will be returned as Number objects.
|
|
133
|
-
* This option is ignored if supportBigNumbers is disabled.
|
|
134
|
-
*/
|
|
135
|
-
bigNumberStrings?: boolean;
|
|
136
|
-
/**
|
|
137
|
-
* Force date types (TIMESTAMP, DATETIME, DATE) to be returned as strings rather then inflated into JavaScript Date
|
|
138
|
-
* objects. Can be true/false or an array of type names to keep as strings.
|
|
139
|
-
*
|
|
140
|
-
* (Default: false)
|
|
141
|
-
*/
|
|
142
|
-
dateStrings?: boolean | Array<'TIMESTAMP' | 'DATETIME' | 'DATE'>;
|
|
143
|
-
/**
|
|
144
|
-
* This will print all incoming and outgoing packets on stdout.
|
|
145
|
-
* You can also restrict debugging to packet types by passing an array of types (strings) to debug;
|
|
146
|
-
*
|
|
147
|
-
* (Default: false)
|
|
148
|
-
*/
|
|
149
|
-
debug?: any;
|
|
150
|
-
/**
|
|
151
|
-
* Generates stack traces on Error to include call site of library entrance ('long stack traces'). Slight
|
|
152
|
-
* performance penalty for most calls. (Default: true)
|
|
153
|
-
*/
|
|
154
|
-
trace?: boolean;
|
|
155
|
-
/**
|
|
156
|
-
* Allow multiple MySQL statements per query. Be careful with this, it exposes you to SQL injection attacks. (Default: false)
|
|
157
|
-
*/
|
|
158
|
-
multipleStatements?: boolean;
|
|
159
|
-
/**
|
|
160
|
-
* List of connection flags to use other than the default ones. It is also possible to blacklist default ones
|
|
161
|
-
*/
|
|
162
|
-
flags?: Array<string>;
|
|
163
|
-
/**
|
|
164
|
-
* object with ssl parameters or a string containing name of ssl profile
|
|
165
|
-
*/
|
|
166
|
-
ssl?: string | MysqlSslOptions;
|
|
167
|
-
/**
|
|
168
|
-
* Return each row as an array, not as an object.
|
|
169
|
-
* This is useful when you have duplicate column names.
|
|
170
|
-
* This can also be set in the `QueryOption` object to be applied per-query.
|
|
171
|
-
*/
|
|
172
|
-
rowsAsArray?: boolean;
|
|
173
|
-
/**
|
|
174
|
-
* The milliseconds before a timeout occurs during the connection acquisition. This is slightly different from connectTimeout,
|
|
175
|
-
* because acquiring a pool connection does not always involve making a connection. (Default: 10 seconds)
|
|
176
|
-
*/
|
|
177
|
-
acquireTimeout?: number;
|
|
178
|
-
/**
|
|
179
|
-
* Determines the pool's action when no connections are available and the limit has been reached. If true, the pool will queue
|
|
180
|
-
* the connection request and call it when one becomes available. If false, the pool will immediately call back with an error.
|
|
181
|
-
* (Default: true)
|
|
182
|
-
*/
|
|
183
|
-
waitForConnections?: boolean;
|
|
184
|
-
/**
|
|
185
|
-
* The maximum number of connections to create at once. (Default: 10)
|
|
186
|
-
*/
|
|
187
|
-
connectionLimit?: number;
|
|
188
|
-
/**
|
|
189
|
-
* The maximum number of connection requests the pool will queue before returning an error from getConnection. If set to 0, there
|
|
190
|
-
* is no limit to the number of queued connection requests. (Default: 0)
|
|
191
|
-
*/
|
|
192
|
-
queueLimit?: number;
|
|
193
|
-
/**
|
|
194
|
-
* Enable keep-alive on the socket. It's disabled by default, but the
|
|
195
|
-
* user can enable it and supply an initial delay.
|
|
196
|
-
*/
|
|
197
|
-
enableKeepAlive?: true;
|
|
198
|
-
/**
|
|
199
|
-
* If keep-alive is enabled users can supply an initial delay.
|
|
200
|
-
*/
|
|
201
|
-
keepAliveInitialDelay?: number;
|
|
202
|
-
/**
|
|
203
|
-
* Called once for each created connection.
|
|
204
|
-
*
|
|
205
|
-
* This is a Kysely specific feature and does not come from the `mysql2` module.
|
|
206
|
-
*/
|
|
207
|
-
onCreateConnection?: (connection: DatabaseConnection) => Promise<void>;
|
|
208
|
-
}
|
|
209
|
-
export interface MysqlSslOptions {
|
|
210
|
-
/**
|
|
211
|
-
* A string or buffer holding the PFX or PKCS12 encoded private key, certificate and CA certificates
|
|
212
|
-
*/
|
|
213
|
-
pfx?: string;
|
|
214
|
-
/**
|
|
215
|
-
* A string holding the PEM encoded private key
|
|
216
|
-
*/
|
|
217
|
-
key?: string;
|
|
218
|
-
/**
|
|
219
|
-
* A string of passphrase for the private key or pfx
|
|
220
|
-
*/
|
|
221
|
-
passphrase?: string;
|
|
222
|
-
/**
|
|
223
|
-
* A string holding the PEM encoded certificate
|
|
224
|
-
*/
|
|
225
|
-
cert?: string;
|
|
226
|
-
/**
|
|
227
|
-
* Either a string or list of strings of PEM encoded CA certificates to trust.
|
|
228
|
-
*/
|
|
229
|
-
ca?: string | string[];
|
|
230
|
-
/**
|
|
231
|
-
* Either a string or list of strings of PEM encoded CRLs (Certificate Revocation List)
|
|
232
|
-
*/
|
|
233
|
-
crl?: string | string[];
|
|
234
|
-
/**
|
|
235
|
-
* A string describing the ciphers to use or exclude
|
|
236
|
-
*/
|
|
237
|
-
ciphers?: string;
|
|
238
|
-
/**
|
|
239
|
-
* You can also connect to a MySQL server without properly providing the appropriate CA to trust. You should not do this.
|
|
240
|
-
*/
|
|
241
|
-
rejectUnauthorized?: boolean;
|
|
242
|
-
}
|
|
@@ -5,6 +5,7 @@ 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");
|
|
8
9
|
/**
|
|
9
10
|
* MySQL dialect that uses the [mysql2](https://github.com/sidorares/node-mysql2#readme) library.
|
|
10
11
|
*
|
|
@@ -15,7 +16,7 @@ const mysql_adapter_js_1 = require("./mysql-adapter.js");
|
|
|
15
16
|
class MysqlDialect {
|
|
16
17
|
#config;
|
|
17
18
|
constructor(config) {
|
|
18
|
-
this.#config = config;
|
|
19
|
+
this.#config = (0, object_utils_js_1.freeze)({ ...config });
|
|
19
20
|
}
|
|
20
21
|
createDriver() {
|
|
21
22
|
return new mysql_driver_js_1.MysqlDriver(this.#config);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DatabaseConnection } from '../../driver/database-connection.js';
|
|
2
2
|
import { Driver, TransactionSettings } from '../../driver/driver.js';
|
|
3
|
-
import { MysqlDialectConfig } from './mysql-dialect.js';
|
|
3
|
+
import { MysqlDialectConfig } from './mysql-dialect-config.js';
|
|
4
4
|
export declare class MysqlDriver implements Driver {
|
|
5
5
|
#private;
|
|
6
6
|
constructor(config: MysqlDialectConfig);
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
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
|
-
const dynamic_import_js_1 = require("../../util/dynamic-import.js");
|
|
6
5
|
const object_utils_js_1 = require("../../util/object-utils.js");
|
|
7
6
|
const PRIVATE_RELEASE_METHOD = Symbol();
|
|
8
7
|
class MysqlDriver {
|
|
@@ -10,7 +9,7 @@ class MysqlDriver {
|
|
|
10
9
|
#connections = new WeakMap();
|
|
11
10
|
#pool;
|
|
12
11
|
constructor(config) {
|
|
13
|
-
this.#config = config;
|
|
12
|
+
this.#config = (0, object_utils_js_1.freeze)({ ...config });
|
|
14
13
|
}
|
|
15
14
|
async init() {
|
|
16
15
|
// Import the `mysql2` module here instead at the top of the file
|
|
@@ -83,7 +82,16 @@ class MysqlDriver {
|
|
|
83
82
|
exports.MysqlDriver = MysqlDriver;
|
|
84
83
|
async function importMysqlPoolFactory() {
|
|
85
84
|
try {
|
|
86
|
-
|
|
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
|
+
}
|
|
87
95
|
}
|
|
88
96
|
catch (error) {
|
|
89
97
|
throw new Error('MySQL client not installed. Please run `npm install mysql2`');
|