mysql-ts-typegen
v0.1.0
Published
Generate TypeScript interfaces automatically from your MySQL database tables. This CLI tool connects to a MySQL database, reads the schema, and creates a single `type.ts` interface file for each table.
Readme
MySQL TypeScript Interface Generator
Generate TypeScript interfaces automatically from your MySQL database tables.
This CLI tool connects to a MySQL database, reads the schema, and creates a single type.ts interface file for each table.
Summary
This tool helps developers keep their TypeScript types in sync with their MySQL database schema. Instead of manually writing interfaces for database tables, this package generates them automatically based on table and column metadata.
Features
- Connects to any MySQL database with user-supplied credentials
- Generates one TypeScript interface file for the given database
- Adds correct types for the table relationships (One-to-many relationships)
- Configurable output directory and type naming
Usage
Run the generator CLI (assuming localhost:3306 is your database endpoint):
npx mysql-ts-typegen --dbuser root --dbpassword secret --dbname mydbAdvanced usage:
npx mysql-ts-typegen \
--dbhost localhost --dbport 3306 \
--dbuser root --dbpassword secret --dbname mydb \
--directory ./types --file types.ts \
--prefix dbModel --camelcase true| Option | Alias | Description | Default |
| -------------- | -------- | ----------------------------------- | ----------- |
| --dbhost | -h | Database host | localhost |
| --dbport | -p | Database port | 3306 |
| --dbuser | -u | Database user | sqluser |
| --dbpassword | -pass | Database password | none |
| --dbname | -db | Database name | none |
| --directory | -dir | Output directory for type file | dbtypes |
| --file | -f | Output file name for interface file | types.ts |
| --prefix | -pre | Prefix to prepend to interface names | (empty) |
| --camelcase | -cc | Convert interface types to camelCase | false |
Example output
- using
--prefix dbModel --camelcase true - table names: books, book_page using snake_case column names
// types.ts
export interface dbModelBooksType {
id: number
path: string
createdAt: string
updatedAt: string
bookPages?: dbModelBooksPageType[]
}
export interface dbModelBooksPageType {
id: number
page: number
}Development
git clone https://github.com/asallay/mysql-ts-typegen.git
cd mysql-ts-typegen
npm installRunning locally:
npm run dev -- --dbuser root --dbpassword secret --dbname mydbLicense
MIT License
