Skip to content

Schema

Schema Setting

Configuration file

const options = {
schema: {
format: "camel",
prefix: "",
suffix: "Schema",
replacements: [],
nullType: "nullish",
inline: true,
zod: {
implementation: [],
references: [],
},
},
};
module.exports = options;

The default configuration outputs zodSchema as follows.

CREATE TABLE `my_todo_list` (
`id` int NOT NULL AUTO_INCREMENT,
`task` varchar(255) NOT NULL,
`description` text,
`due_date` date DEFAULT NULL,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
export const myTodoListSchema = z.object({
id: z.number(),
task: z.string(),
description: z.string().nullish(),
due_date: z.date().nullish(),
created_at: z.date().nullish(),
updated_at: z.date().nullish(),
});

format

Specifies the format of the schema name.

  • camel - camel case (default)
  • pascal - pascal case
  • snake - snake case
  • original - Use the original table name as it is

prefix

A string to be prepended to the type name. (Default is an empty string)

suffix

A string to be given after the type name. (Default is an empty string)

replacements

Performs type name substitution. Specify the string before and after the substitution in string[][] format. (Multiple specifications are allowed.) (Default is an empty array.)

nullType

Specifies what type to use for columns that are not NOT NULL in MySQL.

  • nullish - Use types that allow null | undefined (default)
  • nullable - Use types that allow null.

inline

Specifies whether schema should be defined inline. (Default is true) If inline is set to false, the schema is output to a separate file.

Below is an example of output to a separate file.

import { z } from "zod";
import { globalSchema } from "./globalSchema";
export const myTodoListSchema = z.object({
id: globalSchema.mysqlINT,
task: globalSchema.mysqlVARCHAR,
description: globalSchema.mysqlTEXT.nullish(),
due_date: globalSchema.mysqlDATE.nullish(),
created_at: globalSchema.mysqlTIMESTAMP.nullish(),
updated_at: globalSchema.mysqlTIMESTAMP.nullish(),
});
import { z } from "zod";
export const globalSchema = {
mysqlINT: z.number(),
mysqlVARCHAR: z.string(),
mysqlTINYINT: z.number(),
mysqlTIMESTAMP: z.date(),
mysqlTEXT: z.string(),
mysqlDATE: z.date(),
};

By outputting to a separate file, it is possible, for example, to convert a DATE type to a string type, or to specify your own zodSchema, which may be useful if you want to do things like refine.

zod

Specifies the schema implementation, valid only if inline is true.

  • implementation - Specifies the implementation of schema. Specify the zodSchema to implement in string[][] format. (Default is an empty array.) Specify in the format of [MySQLTYPE, zodSchema].

If [[“DATE”, “z.string()”]] is specified for implementation, what would be z.date() becomes z.string().

export const myTodoListSchema = z.object({
id: z.number(),
task: z.string(),
description: z.string().nullish(),
due_date: z.string().nullish(), // z.date() is converted to z.string()
created_at: z.date().nullish(),
updated_at: z.date().nullish(),
});
  • references - Specifies a reference to a schema. Specify the zodSchema to be referenced in string[][] format. (Default is an empty array.) Valid only if `inline’ is false.

If [[“DATE”, “superMyDate”]] is specified in references, the output will be as follows.

export const myTodoListSchema = z.object({
id: globalSchema.mysqlINT,
task: globalSchema.mysqlVARCHAR,
description: globalSchema.mysqlTEXT.nullish(),
due_date: globalSchema.superMyDateSchema.nullish(), // convert to globalSchema.superMyDateSchema
created_at: globalSchema.mysqlTIMESTAMP.nullish(),
updated_at: globalSchema.mysqlTIMESTAMP.nullish(),
});

conversion Procedure of schema name

Schema name conversion is the same as for Type; see the Type conversion procedure.

conclusion

When you want to use your own implemented type, you can use reference or implementation to perform the conversion.