@deanc/kysely-schema-generator
v0.2.0
Published
A schema generator for kysely
Readme
Kysely schema generator
A very quickly hacked-together schema generator for Kysely which (currently) only supports MySQL.
Installation
With your package manager of choice install:
npm install --save @deanc/kysely-schema-generator
yarn add @deanc/kysely-schema-generator
pnpm add @deanc/kysely-schema-generatorConfiguration
Either in your project root or somewhere else create a .env file with a DATABASE_URL environmental variable:
DATABASE_URL='mysql://user:password@host:3306/databaseName'Usage
Option 1 (.env in project root and run)
kysely-schema-generatorOption 2 (.env somewhere else)
kysely-schema-generator -p /path/to/.envNote if command can't be found you might need to use your package manager to run it (e.g. pnpm exec kysely-schema-generator)
Option 3 (write output to a file)
kysely-schema-generator -o ./kysely-db.tsOption 4 (override database/schema name)
kysely-schema-generator -d my_databaseOption 5 (include or exclude specific tables)
kysely-schema-generator -t users,orders
kysely-schema-generator -x audit_logs,temp_recordsOption 6 (disable formatting)
kysely-schema-generator --no-formatOption 7 (map TINYINT(1) to boolean)
kysely-schema-generator --tinyint1-as-booleanOptions
-p, --path-env <path> path to .env file
-o, --output <path> write output to a file
-d, --database <name> override database/schema name
-t, --tables <list> comma-separated list of tables to include
-x, --exclude-tables <list> comma-separated list of tables to exclude
--no-format disable output formatting
--tinyint1-as-boolean map TINYINT(1) columns to boolean
-h, --help display helpExample Output
Given a users table:
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
email VARCHAR(255) NOT NULL,
status ENUM('active','inactive') NOT NULL,
created_at TIMESTAMP NULL
)The generator outputs:
import { Generated, Insertable, Selectable, Updateable } from 'kysely'
interface UsersTable {
id: Generated<number>
email: string
status: 'active' | 'inactive'
created_at: string | null
}
export type Users = Selectable<UsersTable>
export type NewUsers = Insertable<UsersTable>
export type UsersUpdate = Updateable<UsersTable>
export interface DB {
users: Users
}Integration Test (MySQL)
This test uses Testcontainers and will spin up MySQL automatically (Docker must be running).
pnpm run test:integrationTroubleshooting
- No valid DATABASE_URL process.env found: ensure your
.envfile is in the path provided via--path-env, or in the project root, and contains a validDATABASE_URL. - Testcontainers runtime unavailable: make sure Docker is running and accessible from your terminal.
- Permissions errors: the MySQL user in the URL needs access to
information_schemaand the target database.
Limitations
- MySQL only (no Postgres/SQLite support yet).
- Some MySQL types may still be missing or mapped conservatively (e.g. JSON uses
unknown). - Numeric precision/scale is not currently used to refine type output.
Development
pnpm install
pnpm test
pnpm run test:integration
pnpm run lintTodo
(Contributions very welcome)
- Ensure all MySQL field types are mapped correctly
- Add support for other database engines (Postgres, SQLite at least)
- Add test coverage
- (Maybe) offer option to write to disk instead of relying on user to capture
