mysql-types-generator

raw JSON →
2.1.0 verified Sat Apr 25 auth: no javascript

Generates TypeScript type definitions from a MySQL database schema. v2.1.0, active development, requires Node >=18.3. Automatically converts snake_case table names to PascalCase types, supports custom suffix, column type overrides, enum string generation, and tinyint-as-boolean toggle. Outputs per-table files with index re-export or single file. Useful for Knex or raw MySQL users who want typed database schemas without an ORM. Alternatives like mysql-type-defs-generator exist but this package offers a more focused API and CLI with minimal configuration.

error Error: Cannot find module 'pluralize'
cause Missing dependency 'pluralize' (internal dependency not installed).
fix
Ensure 'mysql-types-generator' is installed with all dependencies: npm install mysql-types-generator
error Error: connect ECONNREFUSED 127.0.0.1:3306
cause MySQL server not running or unreachable with given credentials.
fix
Verify MySQL is running on port 3306, check host/port/credentials.
error Error: Cannot use 'require' with ESM modules (ERR_REQUIRE_ESM)
cause Using require() to import the package in an ESM-only project.
fix
Use import statement: import { generateMysqlTypes } from 'mysql-types-generator'
error TypeError: generateMysqlTypes is not a function
cause Default import instead of named import.
fix
Use named import: import { generateMysqlTypes } from 'mysql-types-generator'
warning Output directory will be emptied and overwritten if 'dir' option is used.
fix Ensure the output directory is dedicated; back up any existing files.
breaking CLI changed from positional arguments to options in v2.0.0 (database name is now required as positional).
fix Use syntax: npx mysql-types-generator --outFile types.ts mydb
gotcha Table names must be in snake_case; otherwise PascalCase conversion may be incorrect.
fix Rename tables to snake_case or use overrides to manually map types.
gotcha If 'uri' is provided in db config, host/port/user/password are ignored.
fix Use only 'uri' or the individual fields, not both.
npm install mysql-types-generator
yarn add mysql-types-generator
pnpm add mysql-types-generator

Generates TypeScript types from a MySQL database, outputting each table as a PascalCase type with 'PO' suffix in separate files.

// file: updateTypes.js
import { generateMysqlTypes } from 'mysql-types-generator';

const dbConfig = {
  host: process.env.DB_HOST ?? 'localhost',
  port: parseInt(process.env.DB_PORT ?? '3306'),
  user: process.env.DB_USER ?? 'root',
  password: process.env.DB_PASSWORD ?? '',
  database: process.env.DB_NAME ?? 'mydb',
};

generateMysqlTypes({
  db: dbConfig,
  output: { dir: 'src/types' },
  suffix: 'PO',
  ignoreTables: ['knex_migrations', 'knex_migrations_lock'],
  tinyintIsBoolean: false,
})
  .then(() => console.log('Types generated!'))
  .catch(err => console.error(err));