Registry / database / mysql-to-zod

mysql-to-zod

JSON →
library2.3.1jsnpmunverified

Converts MySQL table schemas directly into Zod validation schemas (TypeScript). Version 2.3.1. Connects to a MySQL database, reads CREATE TABLE statements using node-sql-parser, and generates .ts files with Zod object schemas and inferred types. Differentiators: zero-config CLI usage via npx, config file support, handles MySQL-specific types and defaults. Release cadence: irregular, minor updates.

npm install mysql-to-zod
INSTALL
IMPORT
SIG · MYSQL-TO-ZOD
M
mysql-to-zod
databasejavascriptv2.3.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

TodoSchema
import { TodoSchema } from './mysqlToZod/schema';
const TodoSchema = require('./mysqlToZod/schema');
The output is an ESM TypeScript file; use ES import syntax.
Todo (type)
import type { Todo } from './mysqlToZod/schema';
import { Todo } from './mysqlToZod/schema';
Todo is a type alias inferred from the Zod schema; use `import type` to avoid runtime overhead.
default export (config)
module.exports = options;
export default options;
Config file uses CommonJS; do not use ESM export default.

Run the CLI to connect to MySQL and generate Zod schemas for all tables in the database.

npx mysql-to-zod mysql://user:pass@localhost:3306/mydb # Output: ./mysqlToZod/schema.ts containing Zod schemas for all tables
Debug
Known issues
gotchaConnection string special characters (@, #, etc.) may fail; must use config object for credentials with special characters.
fix
Use mysqlToZod.config.js with `dbConnection: { host, port, user, password, database }` instead of a URL.
affects: >=1.0
gotchaCLI always reads entire database; cannot exclude specific tables without explicit `tableNames` config.
fix
In mysqlToZod.config.js, set `tableNames: ['table1', 'table2']` to restrict which tables are processed.
affects: >=2.0
gotchaGenerated schemas use `nullish()` for nullable columns in default configuration; this may not match desired null handling.
fix
In config, set `schema.nullType: 'nullable'` or `'null'` to change null handling behavior.
affects: >=2.0
gotchaOutput file overwrites without warning if outDir already exists; no diff or confirmation.
fix
Ensure you have a backup or version control before running; use a dedicated output directory.
affects: >=1.0
gotchaMySQL connection requires network access; wrong host/port causes silent timeout or unhelpful errors.
fix
Test connection separately (e.g., mysql CLI) before running mysql-to-zod.
affects: >=1.0
Errors
Common errors & fixes
Error: Cannot find module 'zod'
zod is not installed in the consuming project but is needed for the generated schemas.
fix
Run `npm install zod` in the project where you copy the generated .ts files.
Error: ER_ACCESS_DENIED_ERROR: Access denied for user '...'
Invalid MySQL credentials or host/port not reachable.
fix
Double-check connection string or config object credentials; ensure MySQL allows remote connections if not localhost.
TypeError: Cannot read properties of undefined (reading 'type')
node-sql-parser failed to parse a CREATE TABLE statement (e.g., unsupported syntax).
fix
Simplify complex table definitions; avoid MySQL-specific extensions not supported by node-sql-parser. Check table creation for any unusual column types.
Error: The 'uri' argument must be a string
Connection URL is missing or undefined; CLI argument not provided.
fix
Supply a valid MySQL connection string as first argument: `npx mysql-to-zod mysql://user:pass@host:port/db`.
Upgrade
Version history
2.3.1latest on npm
Audit
Dependencies
zodrequiredGenerated output depends on zod; users must have it installed to use the schemas.
Agent activity
5 hits · last 30 days
node
4
Resources
mysql-to-zod — npm install mysql-to-zod · libregistry