kysely-generate 0.20.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/.kysely-generaterc.json +3 -0
- package/.prettierignore +1 -0
- package/LICENSE +19 -0
- package/README.md +462 -0
- package/dist/cli/bin.d.ts +2 -0
- package/dist/cli/bin.js +6 -0
- package/dist/cli/bin.js.map +1 -0
- package/dist/cli/cli.d.ts +17 -0
- package/dist/cli/cli.js +247 -0
- package/dist/cli/cli.js.map +1 -0
- package/dist/cli/config-error.d.ts +6 -0
- package/dist/cli/config-error.js +11 -0
- package/dist/cli/config-error.js.map +1 -0
- package/dist/cli/config.d.ts +106 -0
- package/dist/cli/config.js +87 -0
- package/dist/cli/config.js.map +1 -0
- package/dist/cli/constants.d.ts +5 -0
- package/dist/cli/constants.js +23 -0
- package/dist/cli/constants.js.map +1 -0
- package/dist/cli/flags.d.ts +76 -0
- package/dist/cli/flags.js +152 -0
- package/dist/cli/flags.js.map +1 -0
- package/dist/cli/index.d.ts +4 -0
- package/dist/cli/index.js +21 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/db.d.ts +1 -0
- package/dist/db.js +3 -0
- package/dist/db.js.map +1 -0
- package/dist/generator/adapter.d.ts +16 -0
- package/dist/generator/adapter.js +18 -0
- package/dist/generator/adapter.js.map +1 -0
- package/dist/generator/ast/alias-declaration-node.d.ts +9 -0
- package/dist/generator/ast/alias-declaration-node.js +13 -0
- package/dist/generator/ast/alias-declaration-node.js.map +1 -0
- package/dist/generator/ast/array-expression-node.d.ts +6 -0
- package/dist/generator/ast/array-expression-node.js +11 -0
- package/dist/generator/ast/array-expression-node.js.map +1 -0
- package/dist/generator/ast/column-type-node.d.ts +5 -0
- package/dist/generator/ast/column-type-node.js +11 -0
- package/dist/generator/ast/column-type-node.js.map +1 -0
- package/dist/generator/ast/definition-node.d.ts +3 -0
- package/dist/generator/ast/definition-node.js +3 -0
- package/dist/generator/ast/definition-node.js.map +1 -0
- package/dist/generator/ast/export-statement-node.d.ts +10 -0
- package/dist/generator/ast/export-statement-node.js +11 -0
- package/dist/generator/ast/export-statement-node.js.map +1 -0
- package/dist/generator/ast/expression-node.d.ts +11 -0
- package/dist/generator/ast/expression-node.js +3 -0
- package/dist/generator/ast/expression-node.js.map +1 -0
- package/dist/generator/ast/extends-clause-node.d.ts +9 -0
- package/dist/generator/ast/extends-clause-node.js +14 -0
- package/dist/generator/ast/extends-clause-node.js.map +1 -0
- package/dist/generator/ast/generic-expression-node.d.ts +7 -0
- package/dist/generator/ast/generic-expression-node.js +12 -0
- package/dist/generator/ast/generic-expression-node.js.map +1 -0
- package/dist/generator/ast/identifier-node.d.ts +11 -0
- package/dist/generator/ast/identifier-node.js +18 -0
- package/dist/generator/ast/identifier-node.js.map +1 -0
- package/dist/generator/ast/import-clause-node.d.ts +6 -0
- package/dist/generator/ast/import-clause-node.js +12 -0
- package/dist/generator/ast/import-clause-node.js.map +1 -0
- package/dist/generator/ast/import-statement-node.d.ts +7 -0
- package/dist/generator/ast/import-statement-node.js +12 -0
- package/dist/generator/ast/import-statement-node.js.map +1 -0
- package/dist/generator/ast/infer-clause-node.d.ts +5 -0
- package/dist/generator/ast/infer-clause-node.js +11 -0
- package/dist/generator/ast/infer-clause-node.js.map +1 -0
- package/dist/generator/ast/interface-declaration-node.d.ts +8 -0
- package/dist/generator/ast/interface-declaration-node.js +12 -0
- package/dist/generator/ast/interface-declaration-node.js.map +1 -0
- package/dist/generator/ast/json-column-type-node.d.ts +5 -0
- package/dist/generator/ast/json-column-type-node.js +11 -0
- package/dist/generator/ast/json-column-type-node.js.map +1 -0
- package/dist/generator/ast/literal-node.d.ts +7 -0
- package/dist/generator/ast/literal-node.js +11 -0
- package/dist/generator/ast/literal-node.js.map +1 -0
- package/dist/generator/ast/mapped-type-node.d.ts +6 -0
- package/dist/generator/ast/mapped-type-node.js +11 -0
- package/dist/generator/ast/mapped-type-node.js.map +1 -0
- package/dist/generator/ast/module-reference-node.d.ts +15 -0
- package/dist/generator/ast/module-reference-node.js +12 -0
- package/dist/generator/ast/module-reference-node.js.map +1 -0
- package/dist/generator/ast/object-expression-node.d.ts +6 -0
- package/dist/generator/ast/object-expression-node.js +11 -0
- package/dist/generator/ast/object-expression-node.js.map +1 -0
- package/dist/generator/ast/property-node.d.ts +8 -0
- package/dist/generator/ast/property-node.js +13 -0
- package/dist/generator/ast/property-node.js.map +1 -0
- package/dist/generator/ast/raw-expression-node.d.ts +5 -0
- package/dist/generator/ast/raw-expression-node.js +11 -0
- package/dist/generator/ast/raw-expression-node.js.map +1 -0
- package/dist/generator/ast/runtime-enum-declaration-node.d.ts +13 -0
- package/dist/generator/ast/runtime-enum-declaration-node.js +28 -0
- package/dist/generator/ast/runtime-enum-declaration-node.js.map +1 -0
- package/dist/generator/ast/statement-node.d.ts +3 -0
- package/dist/generator/ast/statement-node.js +3 -0
- package/dist/generator/ast/statement-node.js.map +1 -0
- package/dist/generator/ast/template-node.d.ts +7 -0
- package/dist/generator/ast/template-node.js +12 -0
- package/dist/generator/ast/template-node.js.map +1 -0
- package/dist/generator/ast/union-expression-node.d.ts +6 -0
- package/dist/generator/ast/union-expression-node.js +11 -0
- package/dist/generator/ast/union-expression-node.js.map +1 -0
- package/dist/generator/connection-string-parser.d.ts +24 -0
- package/dist/generator/connection-string-parser.js +94 -0
- package/dist/generator/connection-string-parser.js.map +1 -0
- package/dist/generator/constants.d.ts +1 -0
- package/dist/generator/constants.js +6 -0
- package/dist/generator/constants.js.map +1 -0
- package/dist/generator/dialect.d.ts +11 -0
- package/dist/generator/dialect.js +38 -0
- package/dist/generator/dialect.js.map +1 -0
- package/dist/generator/dialects/kysely-bun-sqlite/kysely-bun-sqlite-adapter.d.ts +3 -0
- package/dist/generator/dialects/kysely-bun-sqlite/kysely-bun-sqlite-adapter.js +8 -0
- package/dist/generator/dialects/kysely-bun-sqlite/kysely-bun-sqlite-adapter.js.map +1 -0
- package/dist/generator/dialects/kysely-bun-sqlite/kysely-bun-sqlite-dialect.d.ts +6 -0
- package/dist/generator/dialects/kysely-bun-sqlite/kysely-bun-sqlite-dialect.js +13 -0
- package/dist/generator/dialects/kysely-bun-sqlite/kysely-bun-sqlite-dialect.js.map +1 -0
- package/dist/generator/dialects/libsql/libsql-adapter.d.ts +3 -0
- package/dist/generator/dialects/libsql/libsql-adapter.js +8 -0
- package/dist/generator/dialects/libsql/libsql-adapter.js.map +1 -0
- package/dist/generator/dialects/libsql/libsql-dialect.d.ts +6 -0
- package/dist/generator/dialects/libsql/libsql-dialect.js +13 -0
- package/dist/generator/dialects/libsql/libsql-dialect.js.map +1 -0
- package/dist/generator/dialects/mssql/mssql-adapter.d.ts +36 -0
- package/dist/generator/dialects/mssql/mssql-adapter.js +45 -0
- package/dist/generator/dialects/mssql/mssql-adapter.js.map +1 -0
- package/dist/generator/dialects/mssql/mssql-dialect.d.ts +6 -0
- package/dist/generator/dialects/mssql/mssql-dialect.js +13 -0
- package/dist/generator/dialects/mssql/mssql-dialect.js.map +1 -0
- package/dist/generator/dialects/mysql/mysql-adapter.d.ts +58 -0
- package/dist/generator/dialects/mysql/mysql-adapter.js +80 -0
- package/dist/generator/dialects/mysql/mysql-adapter.js.map +1 -0
- package/dist/generator/dialects/mysql/mysql-dialect.d.ts +6 -0
- package/dist/generator/dialects/mysql/mysql-dialect.js +13 -0
- package/dist/generator/dialects/mysql/mysql-dialect.js.map +1 -0
- package/dist/generator/dialects/postgres/postgres-adapter.d.ts +72 -0
- package/dist/generator/dialects/postgres/postgres-adapter.js +137 -0
- package/dist/generator/dialects/postgres/postgres-adapter.js.map +1 -0
- package/dist/generator/dialects/postgres/postgres-dialect.d.ts +16 -0
- package/dist/generator/dialects/postgres/postgres-dialect.js +22 -0
- package/dist/generator/dialects/postgres/postgres-dialect.js.map +1 -0
- package/dist/generator/dialects/sqlite/sqlite-adapter.d.ts +14 -0
- package/dist/generator/dialects/sqlite/sqlite-adapter.js +22 -0
- package/dist/generator/dialects/sqlite/sqlite-adapter.js.map +1 -0
- package/dist/generator/dialects/sqlite/sqlite-dialect.d.ts +6 -0
- package/dist/generator/dialects/sqlite/sqlite-dialect.js +13 -0
- package/dist/generator/dialects/sqlite/sqlite-dialect.js.map +1 -0
- package/dist/generator/dialects/worker-bun-sqlite/worker-bun-sqlite-dialect.d.ts +6 -0
- package/dist/generator/dialects/worker-bun-sqlite/worker-bun-sqlite-dialect.js +13 -0
- package/dist/generator/dialects/worker-bun-sqlite/worker-bun-sqlite-dialect.js.map +1 -0
- package/dist/generator/generator/diff-checker.d.ts +4 -0
- package/dist/generator/generator/diff-checker.js +31 -0
- package/dist/generator/generator/diff-checker.js.map +1 -0
- package/dist/generator/generator/generate.d.ts +34 -0
- package/dist/generator/generator/generate.js +84 -0
- package/dist/generator/generator/generate.js.map +1 -0
- package/dist/generator/generator/runtime-enums-style.d.ts +1 -0
- package/dist/generator/generator/runtime-enums-style.js +3 -0
- package/dist/generator/generator/runtime-enums-style.js.map +1 -0
- package/dist/generator/generator/serializer.d.ts +68 -0
- package/dist/generator/generator/serializer.js +311 -0
- package/dist/generator/generator/serializer.js.map +1 -0
- package/dist/generator/generator/singularizer.d.ts +1 -0
- package/dist/generator/generator/singularizer.js +32 -0
- package/dist/generator/generator/singularizer.js.map +1 -0
- package/dist/generator/index.d.ts +50 -0
- package/dist/generator/index.js +67 -0
- package/dist/generator/index.js.map +1 -0
- package/dist/generator/logger/log-level.d.ts +8 -0
- package/dist/generator/logger/log-level.js +14 -0
- package/dist/generator/logger/log-level.js.map +1 -0
- package/dist/generator/logger/logger.d.ts +12 -0
- package/dist/generator/logger/logger.js +62 -0
- package/dist/generator/logger/logger.js.map +1 -0
- package/dist/generator/transformer/definitions.d.ts +18 -0
- package/dist/generator/transformer/definitions.js +44 -0
- package/dist/generator/transformer/definitions.js.map +1 -0
- package/dist/generator/transformer/identifier-style.d.ts +1 -0
- package/dist/generator/transformer/identifier-style.js +3 -0
- package/dist/generator/transformer/identifier-style.js.map +1 -0
- package/dist/generator/transformer/imports.d.ts +5 -0
- package/dist/generator/transformer/imports.js +9 -0
- package/dist/generator/transformer/imports.js.map +1 -0
- package/dist/generator/transformer/symbol-collection.d.ts +44 -0
- package/dist/generator/transformer/symbol-collection.js +59 -0
- package/dist/generator/transformer/symbol-collection.js.map +1 -0
- package/dist/generator/transformer/transformer.d.ts +38 -0
- package/dist/generator/transformer/transformer.js +380 -0
- package/dist/generator/transformer/transformer.js.map +1 -0
- package/dist/generator/utils/case-converter.d.ts +30 -0
- package/dist/generator/utils/case-converter.js +67 -0
- package/dist/generator/utils/case-converter.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/introspector/dialect.d.ts +19 -0
- package/dist/introspector/dialect.js +10 -0
- package/dist/introspector/dialect.js.map +1 -0
- package/dist/introspector/dialects/kysely-bun-sqlite/kysely-bun-sqlite-dialect.d.ts +7 -0
- package/dist/introspector/dialects/kysely-bun-sqlite/kysely-bun-sqlite-dialect.js +56 -0
- package/dist/introspector/dialects/kysely-bun-sqlite/kysely-bun-sqlite-dialect.js.map +1 -0
- package/dist/introspector/dialects/kysely-bun-sqlite/kysely-bun-sqlite-introspector.d.ts +6 -0
- package/dist/introspector/dialects/kysely-bun-sqlite/kysely-bun-sqlite-introspector.js +15 -0
- package/dist/introspector/dialects/kysely-bun-sqlite/kysely-bun-sqlite-introspector.js.map +1 -0
- package/dist/introspector/dialects/libsql/libsql-dialect.d.ts +7 -0
- package/dist/introspector/dialects/libsql/libsql-dialect.js +59 -0
- package/dist/introspector/dialects/libsql/libsql-dialect.js.map +1 -0
- package/dist/introspector/dialects/libsql/libsql-introspector.d.ts +6 -0
- package/dist/introspector/dialects/libsql/libsql-introspector.js +15 -0
- package/dist/introspector/dialects/libsql/libsql-introspector.js.map +1 -0
- package/dist/introspector/dialects/mssql/mssql-dialect.d.ts +9 -0
- package/dist/introspector/dialects/mssql/mssql-dialect.js +112 -0
- package/dist/introspector/dialects/mssql/mssql-dialect.js.map +1 -0
- package/dist/introspector/dialects/mssql/mssql-introspector.d.ts +6 -0
- package/dist/introspector/dialects/mssql/mssql-introspector.js +15 -0
- package/dist/introspector/dialects/mssql/mssql-introspector.js.map +1 -0
- package/dist/introspector/dialects/mysql/mysql-db.d.ts +8 -0
- package/dist/introspector/dialects/mysql/mysql-db.js +3 -0
- package/dist/introspector/dialects/mysql/mysql-db.js.map +1 -0
- package/dist/introspector/dialects/mysql/mysql-dialect.d.ts +8 -0
- package/dist/introspector/dialects/mysql/mysql-dialect.js +55 -0
- package/dist/introspector/dialects/mysql/mysql-dialect.js.map +1 -0
- package/dist/introspector/dialects/mysql/mysql-introspector.d.ts +14 -0
- package/dist/introspector/dialects/mysql/mysql-introspector.js +46 -0
- package/dist/introspector/dialects/mysql/mysql-introspector.js.map +1 -0
- package/dist/introspector/dialects/mysql/mysql-parser.d.ts +7 -0
- package/dist/introspector/dialects/mysql/mysql-parser.js +66 -0
- package/dist/introspector/dialects/mysql/mysql-parser.js.map +1 -0
- package/dist/introspector/dialects/postgres/date-parser.d.ts +2 -0
- package/dist/introspector/dialects/postgres/date-parser.js +5 -0
- package/dist/introspector/dialects/postgres/date-parser.js.map +1 -0
- package/dist/introspector/dialects/postgres/numeric-parser.d.ts +2 -0
- package/dist/introspector/dialects/postgres/numeric-parser.js +5 -0
- package/dist/introspector/dialects/postgres/numeric-parser.js.map +1 -0
- package/dist/introspector/dialects/postgres/postgres-db.d.ts +15 -0
- package/dist/introspector/dialects/postgres/postgres-db.js +3 -0
- package/dist/introspector/dialects/postgres/postgres-db.js.map +1 -0
- package/dist/introspector/dialects/postgres/postgres-dialect.d.ts +20 -0
- package/dist/introspector/dialects/postgres/postgres-dialect.js +83 -0
- package/dist/introspector/dialects/postgres/postgres-dialect.js.map +1 -0
- package/dist/introspector/dialects/postgres/postgres-introspector.d.ts +37 -0
- package/dist/introspector/dialects/postgres/postgres-introspector.js +200 -0
- package/dist/introspector/dialects/postgres/postgres-introspector.js.map +1 -0
- package/dist/introspector/dialects/sqlite/sqlite-dialect.d.ts +8 -0
- package/dist/introspector/dialects/sqlite/sqlite-dialect.js +53 -0
- package/dist/introspector/dialects/sqlite/sqlite-dialect.js.map +1 -0
- package/dist/introspector/dialects/sqlite/sqlite-introspector.d.ts +6 -0
- package/dist/introspector/dialects/sqlite/sqlite-introspector.js +15 -0
- package/dist/introspector/dialects/sqlite/sqlite-introspector.js.map +1 -0
- package/dist/introspector/enum-collection.d.ts +10 -0
- package/dist/introspector/enum-collection.js +26 -0
- package/dist/introspector/enum-collection.js.map +1 -0
- package/dist/introspector/index.d.ts +24 -0
- package/dist/introspector/index.js +41 -0
- package/dist/introspector/index.js.map +1 -0
- package/dist/introspector/introspector.d.ts +23 -0
- package/dist/introspector/introspector.fixtures.d.ts +4 -0
- package/dist/introspector/introspector.fixtures.js +136 -0
- package/dist/introspector/introspector.fixtures.js.map +1 -0
- package/dist/introspector/introspector.js +50 -0
- package/dist/introspector/introspector.js.map +1 -0
- package/dist/introspector/metadata/column-metadata.d.ts +23 -0
- package/dist/introspector/metadata/column-metadata.js +18 -0
- package/dist/introspector/metadata/column-metadata.js.map +1 -0
- package/dist/introspector/metadata/database-metadata.d.ts +12 -0
- package/dist/introspector/metadata/database-metadata.js +13 -0
- package/dist/introspector/metadata/database-metadata.js.map +1 -0
- package/dist/introspector/metadata/table-metadata.d.ts +17 -0
- package/dist/introspector/metadata/table-metadata.js +15 -0
- package/dist/introspector/metadata/table-metadata.js.map +1 -0
- package/dist/introspector/table-matcher.d.ts +6 -0
- package/dist/introspector/table-matcher.js +16 -0
- package/dist/introspector/table-matcher.js.map +1 -0
- package/package.json +202 -0
- package/tsconfig.build.json +5 -0
package/.prettierignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
**/src/**/*.snapshot.ts
|
package/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Robin Blomberg
|
|
4
|
+
Copyright (c) 2025 VastBlast
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
|
7
|
+
associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
8
|
+
including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
|
9
|
+
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
13
|
+
portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
|
16
|
+
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
|
18
|
+
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
19
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
# kysely-generate <!-- omit from toc -->
|
|
2
|
+
|
|
3
|
+
> Fork of [RobinBlomberg/kysely-codegen](https://github.com/RobinBlomberg/kysely-codegen) with some additional features. This fork lives at https://github.com/VastBlast/kysely-generate and remains MIT-licensed; see LICENSE for attribution.
|
|
4
|
+
|
|
5
|
+
Changes in this fork:
|
|
6
|
+
|
|
7
|
+
- PostgreSQL materialized view support (introspects materialized views alongside tables).
|
|
8
|
+
|
|
9
|
+
`kysely-generate` generates Kysely type definitions from your database. That's it.
|
|
10
|
+
|
|
11
|
+
## Table of contents <!-- omit from toc -->
|
|
12
|
+
|
|
13
|
+
- [Installation](#installation)
|
|
14
|
+
- [Generating type definitions](#generating-type-definitions)
|
|
15
|
+
- [Using the type definitions](#using-the-type-definitions)
|
|
16
|
+
- [CLI arguments](#cli-arguments) - [Basic example](#basic-example) - [Named imports with aliasing](#named-imports-with-aliasing)
|
|
17
|
+
- [Configuration file](#configuration-file)
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
npm install --save-dev kysely-generate
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
You will also need to install Kysely with your driver of choice:
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
# PostgreSQL
|
|
29
|
+
npm install kysely pg
|
|
30
|
+
|
|
31
|
+
# MySQL
|
|
32
|
+
npm install kysely mysql2
|
|
33
|
+
|
|
34
|
+
# SQLite
|
|
35
|
+
npm install kysely better-sqlite3
|
|
36
|
+
|
|
37
|
+
# MSSQL
|
|
38
|
+
npm install kysely tedious tarn @tediousjs/connection-string@0.5.0
|
|
39
|
+
|
|
40
|
+
# LibSQL
|
|
41
|
+
npm install @libsql/kysely-libsql
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Generating type definitions
|
|
45
|
+
|
|
46
|
+
The most convenient way to get started is to create an `.env` file with your database connection string:
|
|
47
|
+
|
|
48
|
+
```sh
|
|
49
|
+
# PostgreSQL
|
|
50
|
+
DATABASE_URL=postgres://username:password@yourdomain.com/database
|
|
51
|
+
|
|
52
|
+
# MySQL
|
|
53
|
+
DATABASE_URL=mysql://username:password@yourdomain.com/database
|
|
54
|
+
|
|
55
|
+
# SQLite
|
|
56
|
+
DATABASE_URL=C:/Program Files/sqlite3/db
|
|
57
|
+
|
|
58
|
+
# MSSQL
|
|
59
|
+
DATABASE_URL=Server=mssql;Database=database;User Id=user;Password=password
|
|
60
|
+
|
|
61
|
+
# LibSQL
|
|
62
|
+
DATABASE_URL=libsql://token@host:port/database
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
> If your URL contains a password with special characters, those characters may need to be [percent-encoded](https://en.wikipedia.org/wiki/Percent-encoding#Reserved_characters).
|
|
66
|
+
>
|
|
67
|
+
> If you are using _PlanetScale_, make sure your URL contains this SSL query string parameter: `ssl={"rejectUnauthorized":true}`
|
|
68
|
+
|
|
69
|
+
Then run the following command, or add it to the scripts section in your package.json file:
|
|
70
|
+
|
|
71
|
+
```sh
|
|
72
|
+
kysely-generate
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
This command will generate a `.d.ts` file from your database, for example:
|
|
76
|
+
|
|
77
|
+
<!-- prettier-ignore -->
|
|
78
|
+
```ts
|
|
79
|
+
import { ColumnType } from 'kysely';
|
|
80
|
+
|
|
81
|
+
export type Generated<T> = T extends ColumnType<infer S, infer I, infer U>
|
|
82
|
+
? ColumnType<S, I | undefined, U>
|
|
83
|
+
: ColumnType<T, T | undefined, T>;
|
|
84
|
+
|
|
85
|
+
export type Timestamp = ColumnType<Date, Date | string, Date | string>;
|
|
86
|
+
|
|
87
|
+
export interface Company {
|
|
88
|
+
id: Generated<number>;
|
|
89
|
+
name: string;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export interface User {
|
|
93
|
+
company_id: number | null;
|
|
94
|
+
created_at: Generated<Timestamp>;
|
|
95
|
+
email: string;
|
|
96
|
+
id: Generated<number>;
|
|
97
|
+
is_active: boolean;
|
|
98
|
+
name: string;
|
|
99
|
+
updated_at: Timestamp;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export interface DB {
|
|
103
|
+
company: Company;
|
|
104
|
+
user: User;
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
To specify a different output file:
|
|
109
|
+
|
|
110
|
+
```sh
|
|
111
|
+
kysely-generate --out-file ./src/db/db.d.ts
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Using the type definitions
|
|
115
|
+
|
|
116
|
+
Import `DB` into `new Kysely<DB>`, and you're done!
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
import { Kysely, PostgresDialect } from 'kysely';
|
|
120
|
+
import { DB } from 'kysely-generate';
|
|
121
|
+
import { Pool } from 'pg';
|
|
122
|
+
|
|
123
|
+
const db = new Kysely<DB>({
|
|
124
|
+
dialect: new PostgresDialect({
|
|
125
|
+
pool: new Pool({
|
|
126
|
+
connectionString: process.env.DATABASE_URL,
|
|
127
|
+
}),
|
|
128
|
+
}),
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
const rows = await db.selectFrom('users').selectAll().execute();
|
|
132
|
+
// ^ { created_at: Date; email: string; id: number; ... }[]
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
If you need to use the generated types in e.g. function parameters and type definitions, you may need to use the Kysely `Insertable`, `Selectable`, `Updateable` types. Note that you don't need to explicitly annotate query return values, as it's recommended to let Kysely infer the types for you.
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
import { Insertable, Updateable } from 'kysely';
|
|
139
|
+
import { DB } from 'kysely-generate';
|
|
140
|
+
import { db } from './db';
|
|
141
|
+
|
|
142
|
+
async function insertUser(user: Insertable<User>) {
|
|
143
|
+
return await db
|
|
144
|
+
.insertInto('users')
|
|
145
|
+
.values(user)
|
|
146
|
+
.returningAll()
|
|
147
|
+
.executeTakeFirstOrThrow();
|
|
148
|
+
// ^ Selectable<User>
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async function updateUser(id: number, user: Updateable<User>) {
|
|
152
|
+
return await db
|
|
153
|
+
.updateTable('users')
|
|
154
|
+
.set(user)
|
|
155
|
+
.where('id', '=', id)
|
|
156
|
+
.returning(['email', 'id'])
|
|
157
|
+
.executeTakeFirstOrThrow();
|
|
158
|
+
// ^ { email: string; id: number; }
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Read the [Kysely documentation](https://kysely.dev/docs/getting-started) for more information.
|
|
163
|
+
|
|
164
|
+
## CLI arguments
|
|
165
|
+
|
|
166
|
+
#### --camel-case <!-- omit from toc -->
|
|
167
|
+
|
|
168
|
+
Use the Kysely CamelCasePlugin for generated table column names.
|
|
169
|
+
|
|
170
|
+
**Example:**
|
|
171
|
+
|
|
172
|
+
```ts
|
|
173
|
+
export interface User {
|
|
174
|
+
companyId: number | null;
|
|
175
|
+
createdAt: Generated<Timestamp>;
|
|
176
|
+
email: string;
|
|
177
|
+
id: Generated<number>;
|
|
178
|
+
isActive: boolean;
|
|
179
|
+
name: string;
|
|
180
|
+
updatedAt: Timestamp;
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
#### --config-file <!-- omit from toc -->
|
|
185
|
+
|
|
186
|
+
Specify the path to the configuration file to use.
|
|
187
|
+
|
|
188
|
+
#### --custom-imports <!-- omit from toc -->
|
|
189
|
+
|
|
190
|
+
Specify custom type imports to use with type overrides. This is particularly useful when using custom types from external packages or local files.
|
|
191
|
+
|
|
192
|
+
##### Basic example
|
|
193
|
+
|
|
194
|
+
```sh
|
|
195
|
+
kysely-generate --custom-imports='{"InstantRange":"./custom-types","MyCustomType":"@my-org/custom-types"}'
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
##### Named imports with aliasing
|
|
199
|
+
|
|
200
|
+
You can import specific named exports and optionally alias them using the `#` syntax:
|
|
201
|
+
|
|
202
|
+
```sh
|
|
203
|
+
kysely-generate --custom-imports='{"MyType":"./types#OriginalType","DateRange":"@org/utils#CustomDateRange"}'
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
This generates:
|
|
207
|
+
|
|
208
|
+
```ts
|
|
209
|
+
import type { OriginalType as MyType } from './types';
|
|
210
|
+
import type { CustomDateRange as DateRange } from '@org/utils';
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
Then you can use these imported types in your overrides:
|
|
214
|
+
|
|
215
|
+
```sh
|
|
216
|
+
kysely-generate --overrides='{"columns":{"events.date_range":"ColumnType<DateRange, DateRange, never>"}}'
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
#### --date-parser <!-- omit from toc -->
|
|
220
|
+
|
|
221
|
+
Specify which parser to use for PostgreSQL date values. (values: `string`/`timestamp`, default: `timestamp`)
|
|
222
|
+
|
|
223
|
+
#### --default-schema [value] <!-- omit from toc -->
|
|
224
|
+
|
|
225
|
+
Set the default schema(s) for the database connection.
|
|
226
|
+
|
|
227
|
+
Multiple schemas can be specified:
|
|
228
|
+
|
|
229
|
+
```sh
|
|
230
|
+
kysely-generate --default-schema=public --default-schema=hidden
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
#### --dialect [value] <!-- omit from toc -->
|
|
234
|
+
|
|
235
|
+
Set the SQL dialect. (values: `postgres`/`mysql`/`sqlite`/`mssql`/`libsql`/`bun-sqlite`/`kysely-bun-sqlite`/`worker-bun-sqlite`)
|
|
236
|
+
|
|
237
|
+
#### --env-file [value] <!-- omit from toc -->
|
|
238
|
+
|
|
239
|
+
Specify the path to an environment file to use.
|
|
240
|
+
|
|
241
|
+
#### --help, -h <!-- omit from toc -->
|
|
242
|
+
|
|
243
|
+
Print all command line options.
|
|
244
|
+
|
|
245
|
+
#### --include-pattern [value], --exclude-pattern [value] <!-- omit from toc -->
|
|
246
|
+
|
|
247
|
+
You can choose which tables should be included during code generation by providing a glob pattern to the `--include-pattern` and `--exclude-pattern` flags. We use [micromatch](https://github.com/micromatch/micromatch) under the hood, which provides advanced glob support. For instance, if you only want to include your public tables:
|
|
248
|
+
|
|
249
|
+
```sh
|
|
250
|
+
kysely-generate --include-pattern="public.*"
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
You can also include only certain tables within a schema:
|
|
254
|
+
|
|
255
|
+
```sh
|
|
256
|
+
kysely-generate --include-pattern="public.+(user|post)"
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
Or exclude an entire class of tables:
|
|
260
|
+
|
|
261
|
+
```sh
|
|
262
|
+
kysely-generate --exclude-pattern="documents.*"
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
#### --log-level [value] <!-- omit from toc -->
|
|
266
|
+
|
|
267
|
+
Set the terminal log level. (values: `debug`/`info`/`warn`/`error`/`silent`, default: `warn`)
|
|
268
|
+
|
|
269
|
+
#### --no-domains <!-- omit from toc -->
|
|
270
|
+
|
|
271
|
+
Skip generating types for PostgreSQL domains. (default: `false`)
|
|
272
|
+
|
|
273
|
+
#### --numeric-parser <!-- omit from toc -->
|
|
274
|
+
|
|
275
|
+
Specify which parser to use for PostgreSQL numeric values. (values: `string`/`number`/`number-or-string`, default: `string`)
|
|
276
|
+
|
|
277
|
+
#### --overrides <!-- omit from toc -->
|
|
278
|
+
|
|
279
|
+
Specify type overrides for specific table columns in JSON format.
|
|
280
|
+
|
|
281
|
+
**Example:**
|
|
282
|
+
|
|
283
|
+
```sh
|
|
284
|
+
kysely-generate --overrides='{"columns":{"table_name.column_name":"{foo:\"bar\"}"}}'
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
#### --out-file [value] <!-- omit from toc -->
|
|
288
|
+
|
|
289
|
+
Set the file build path. (default: `./node_modules/kysely-generate/dist/db.d.ts`)
|
|
290
|
+
|
|
291
|
+
#### --partitions <!-- omit from toc -->
|
|
292
|
+
|
|
293
|
+
Include partitions of PostgreSQL tables in the generated code.
|
|
294
|
+
|
|
295
|
+
#### --print <!-- omit from toc -->
|
|
296
|
+
|
|
297
|
+
Print the generated output to the terminal instead of a file.
|
|
298
|
+
|
|
299
|
+
#### --runtime-enums <!-- omit from toc -->
|
|
300
|
+
|
|
301
|
+
The PostgreSQL `--runtime-enums` option generates runtime enums instead of string unions. You can optionally specify which naming convention to use for runtime enum keys. (values: [`pascal-case`, `screaming-snake-case`], default: `screaming-snake-case`)
|
|
302
|
+
|
|
303
|
+
**Examples:**
|
|
304
|
+
|
|
305
|
+
`--runtime-enums=false`
|
|
306
|
+
|
|
307
|
+
```ts
|
|
308
|
+
export type Status = 'CONFIRMED' | 'UNCONFIRMED';
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
`--runtime-enums` or `--runtime-enums=screaming-snake-case`
|
|
312
|
+
|
|
313
|
+
```ts
|
|
314
|
+
export enum Status {
|
|
315
|
+
CONFIRMED = 'CONFIRMED',
|
|
316
|
+
UNCONFIRMED = 'UNCONFIRMED',
|
|
317
|
+
}
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
`--runtime-enums=pascal-case`
|
|
321
|
+
|
|
322
|
+
```ts
|
|
323
|
+
export enum Status {
|
|
324
|
+
Confirmed = 'CONFIRMED',
|
|
325
|
+
Unconfirmed = 'UNCONFIRMED',
|
|
326
|
+
}
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
#### --singularize <!-- omit from toc -->
|
|
330
|
+
|
|
331
|
+
Singularize generated type aliases, e.g. as `BlogPost` instead of `BlogPosts`. The codegen uses the [pluralize](https://www.npmjs.com/package/pluralize) package for singularization.
|
|
332
|
+
|
|
333
|
+
You can specify custom singularization rules in the [configuration file](#configuration-file).
|
|
334
|
+
|
|
335
|
+
#### --type-mapping <!-- omit from toc -->
|
|
336
|
+
|
|
337
|
+
Specify type mappings for database types, in JSON format. This allows you to automatically map database types to custom TypeScript types.
|
|
338
|
+
|
|
339
|
+
**Example:**
|
|
340
|
+
|
|
341
|
+
```sh
|
|
342
|
+
kysely-generate --type-mapping='{"timestamptz":"Temporal.Instant","tstzrange":"InstantRange"}' --custom-imports='{"Temporal":"@js-temporal/polyfill","InstantRange":"./custom-types"}'
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
This is especially useful when you want to use modern JavaScript types like Temporal API instead of Date objects:
|
|
346
|
+
|
|
347
|
+
```json
|
|
348
|
+
{
|
|
349
|
+
"typeMapping": {
|
|
350
|
+
"date": "Temporal.PlainDate",
|
|
351
|
+
"daterange": "DateRange",
|
|
352
|
+
"interval": "Temporal.Duration",
|
|
353
|
+
"time": "Temporal.PlainTime",
|
|
354
|
+
"timestamp": "Temporal.Instant",
|
|
355
|
+
"timestamptz": "Temporal.Instant",
|
|
356
|
+
"tsrange": "InstantRange",
|
|
357
|
+
"tstzrange": "InstantRange"
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
Type mappings are automatically applied to all columns of the specified database type, eliminating the need to override each column individually. This feature works with all supported databases, though some types (like PostgreSQL range types) are database-specific.
|
|
363
|
+
|
|
364
|
+
#### --type-only-imports <!-- omit from toc -->
|
|
365
|
+
|
|
366
|
+
Generate code using the TypeScript 3.8+ `import type` syntax. (default: `true`)
|
|
367
|
+
|
|
368
|
+
#### --url [value] <!-- omit from toc -->
|
|
369
|
+
|
|
370
|
+
Set the database connection string URL. This may point to an environment variable. (default: `env(DATABASE_URL)`)
|
|
371
|
+
|
|
372
|
+
#### --verify <!-- omit from toc -->
|
|
373
|
+
|
|
374
|
+
Verify that the generated types are up-to-date. (default: `false`)
|
|
375
|
+
|
|
376
|
+
## Configuration file
|
|
377
|
+
|
|
378
|
+
All codegen options can also be configured in a `.kysely-generaterc.json` (or `.js`, `.ts`, `.yaml` etc.) file or the `kysely-generate` property in `package.json`. See [Cosmiconfig](https://github.com/cosmiconfig/cosmiconfig) for all available configuration file formats.
|
|
379
|
+
|
|
380
|
+
The default configuration:
|
|
381
|
+
|
|
382
|
+
```json
|
|
383
|
+
{
|
|
384
|
+
"camelCase": false,
|
|
385
|
+
"customImports": {},
|
|
386
|
+
"dateParser": "timestamp",
|
|
387
|
+
"defaultSchemas": [], // ["public"] for PostgreSQL.
|
|
388
|
+
"dialect": null,
|
|
389
|
+
"domains": true,
|
|
390
|
+
"envFile": null,
|
|
391
|
+
"excludePattern": null,
|
|
392
|
+
"includePattern": null,
|
|
393
|
+
"logLevel": "warn",
|
|
394
|
+
"numericParser": "string",
|
|
395
|
+
"outFile": "./node_modules/kysely-generate/dist/db.d.ts",
|
|
396
|
+
"overrides": {},
|
|
397
|
+
"partitions": false,
|
|
398
|
+
"print": false,
|
|
399
|
+
"runtimeEnums": false,
|
|
400
|
+
"singularize": false,
|
|
401
|
+
"typeMapping": {},
|
|
402
|
+
"typeOnlyImports": true,
|
|
403
|
+
"url": "env(DATABASE_URL)",
|
|
404
|
+
"verify": false
|
|
405
|
+
}
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
The configuration object adds support for more advanced options:
|
|
409
|
+
|
|
410
|
+
```json
|
|
411
|
+
{
|
|
412
|
+
"camelCase": true,
|
|
413
|
+
"customImports": {
|
|
414
|
+
"InstantRange": "./custom-types",
|
|
415
|
+
"MyCustomType": "@my-org/custom-types",
|
|
416
|
+
"AliasedType": "./types#OriginalType"
|
|
417
|
+
},
|
|
418
|
+
"overrides": {
|
|
419
|
+
"columns": {
|
|
420
|
+
"events.date_range": "ColumnType<InstantRange, InstantRange, never>",
|
|
421
|
+
"posts.author_type": "AliasedType",
|
|
422
|
+
"users.settings": "{ theme: 'dark' }"
|
|
423
|
+
}
|
|
424
|
+
},
|
|
425
|
+
"singularize": {
|
|
426
|
+
"/^(.*?)s?$/": "$1_model",
|
|
427
|
+
"/(bacch)(?:us|i)$/i": "$1us"
|
|
428
|
+
},
|
|
429
|
+
"typeMapping": {
|
|
430
|
+
"date": "Temporal.PlainDate",
|
|
431
|
+
"interval": "Temporal.Duration",
|
|
432
|
+
"timestamptz": "Temporal.Instant"
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
The generated output:
|
|
438
|
+
|
|
439
|
+
```ts
|
|
440
|
+
import type { InstantRange } from './custom-types';
|
|
441
|
+
import type { MyCustomType } from '@my-org/custom-types';
|
|
442
|
+
import type { OriginalType as AliasedType } from './types';
|
|
443
|
+
import type { Temporal } from '@js-temporal/polyfill';
|
|
444
|
+
|
|
445
|
+
export interface EventModel {
|
|
446
|
+
createdAt: Temporal.Instant;
|
|
447
|
+
dateRange: ColumnType<InstantRange, InstantRange, never>;
|
|
448
|
+
eventDate: Temporal.PlainDate;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
export interface UserModel {
|
|
452
|
+
settings: { theme: 'dark' };
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// ...
|
|
456
|
+
|
|
457
|
+
export interface DB {
|
|
458
|
+
bacchi: Bacchus;
|
|
459
|
+
events: EventModel;
|
|
460
|
+
users: UserModel;
|
|
461
|
+
}
|
|
462
|
+
```
|
package/dist/cli/bin.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bin.js","sourceRoot":"","sources":["../../src/cli/bin.ts"],"names":[],"mappings":";;;AACA,+BAA4B;AAE5B,KAAK,IAAI,SAAG,EAAE,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Config } from './config';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a kysely-generate command-line interface.
|
|
4
|
+
*/
|
|
5
|
+
export declare class Cli {
|
|
6
|
+
#private;
|
|
7
|
+
logLevel: "error" | "debug" | "silent" | "warn" | "info";
|
|
8
|
+
generate(options: Config): Promise<string>;
|
|
9
|
+
parseOptions(args: string[], options?: {
|
|
10
|
+
config?: Config;
|
|
11
|
+
silent?: boolean;
|
|
12
|
+
}): Config;
|
|
13
|
+
run(options?: {
|
|
14
|
+
argv?: string[];
|
|
15
|
+
config?: Config;
|
|
16
|
+
}): Promise<string>;
|
|
17
|
+
}
|