Convert MySQL schemas into Zod schemas
If you have any questions about usage or suggestions for improvement, please visit disucussions. https://github.com/araera111/mysql-to-zod/discussions
Connect to MySQL using mysql2 and retrieve the CREATE TABLE statement. It is then parsed by node-sql-parser and output as zodSchema.
npx mysql-to-zod mysql://user:pass@localhost:3306/dbname
For the following SQL schema
CREATE TABLE todo (
id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
description TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
run:
npx mysql-to-zod@latest mysql://user@pass:3306/dbname
import { z } from "zod";
export const TodoSchema = z.object({
id: z.number(),
title: z.string(),
description: z.string().nullable(),
created_at: z.date().nullable(),
updated_at: z.date().nullable(),
});
export type Todo = z.infer<typeof TodoSchema>;
WIP...
rootdir
touch mysqlToZod.config.js
const options = {
/*
output
If you set the following
The output schemas will be in "./mysqlToZod/schema.ts"
*/
output: {
outDir: "./mysqlToZod",
fileName: "schema.ts",
},
dbConnection: "mysql://root:root@localhost:3306/mydb", //argv0 is priority 1. thisConfig is priority 2.
tableNames: [], //if empty, all tables.
};
module.exports = options;
If dbConnection contains "@" or other special characters, pass it as Config for Knex.
/** @type {import("./src/options/options").MysqlToZodOption} */
const options = {
/* You can specify the destination directory and file name. */
output: {
outDir: "./mysqlToZod",
fileName: "schema.ts",
},
/*
You can specify the URL to connect to MySQL(mysql://user:pass@host:port:dbName)
or
You can specify the connection information for MySQL.
*/
dbConnection: {
host: "127.0.0.1",
port: 3306,
user: "root",
password: "root",
database: "test",
},
/* if empty, all tables */
tableNames: [],
/* Below are the ADVANCED OPTIONS. A detailed explanation will be written at a later date. */
comments: {
table: {
active: true,
format: "// [table:!name] : !text",
},
column: {
active: true,
format: "// !name : !text",
},
},
type: {
declared: "type",
format: "pascal",
prefix: "",
suffix: "",
replacements: [],
},
schema: {
format: "camel",
prefix: "",
suffix: "Schema",
replacements: [],
nullType: "nullish",
inline: false,
zod: {
implementation: [],
references: [],
},
},
};
module.exports = options;
MIT