MySQL exporter documentation

Overview

MySQLExporter Convert the JSON Schema generated by schema-dsl into MySQL DDL statements, including CREATE TABLE and index creation statements.

Core functions

  • ✅ Generate CREATE TABLE DDL statement
  • ✅ Automatic type mapping (JSON Schema → MySQL)
  • ✅Support NOT NULL, DEFAULT, COMMENT
  • ✅ Automatically detect primary keys
  • ✅ Generate index DDL
  • ✅ Configurable storage engine and character set

quick start

import { s, exporters } from 'schema-dsl/pure';

// 1. Define Schema
const userSchema = s({
  id: 'string!',
  username: 'string:3-32!',
  email: 'email!',
  age: 'number:18-120',
  status: 'active|inactive'
});

// 2. Create exporter
const exporter = new exporters.MySQLExporter();

// 3. Export to MySQL DDL
const ddl = exporter.export('users', userSchema);
console.log(ddl);

Output:

CREATE TABLE `users` (
  `id` VARCHAR(255) NOT NULL,
  `username` VARCHAR(32) NOT NULL,
  `email` VARCHAR(255) NOT NULL,
  `age` DOUBLE NULL,
  `status` VARCHAR(255) NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

API reference

Constructor

new MySQLExporter(options)

Parameters:

Parametertypedefault valueDescription
options.enginestring'InnoDB'storage engine
options.charsetstring'utf8mb4'character set
options.collatestring'utf8mb4_unicode_ci'Sorting rules

method

export(tableName, jsonSchema)

Generate a MySQL CREATE TABLE statement.

const ddl = exporter.export('users', userSchema);

Parameters:

  • tableName (string): table name
  • jsonSchema (Object): JSON Schema object (must be of object type)

Return Value:

  • string: MySQL DDL statement

generateIndex(tableName, columnName, options)

Generate index creation statements.

const indexDdl = exporter.generateIndex('users', 'email', { unique: true });
console.log(indexDdl);
// CREATE UNIQUE INDEX `idx_users_email` ON `users` (`email`);

Parameters:

  • tableName (string): table name
  • columnName (string): column name
  • options.name (string): index name (optional, default idx_table_column)
  • options.unique (boolean): Whether the index is unique (default false)

MySQLExporter.export(tableName, jsonSchema) (static method)

Fast export without instantiation.

const ddl = exporters.MySQLExporter.export('users', userSchema);

Configuration options

storage engine

//Use MyISAM engine
const exporter = new exporters.MySQLExporter({
  engine: 'MyISAM'
});

Character set configuration

// Use utf8 character set
const exporter = new exporters.MySQLExporter({
  charset: 'utf8',
  collate: 'utf8_general_ci'
});

Complete example

User table DDL generation

import { s, exporters } from 'schema-dsl/pure';

//Define user Schema (with description)
const userSchema = s({
  id: 'string!',
  username: s('string:3-32!').description('User login name'),
  email: s('email!').description('User Email'),
  password: 'string:8-64!',
  age: 'number:0-150',
  status: 'active|inactive|banned',
  createdAt: 'datetime!'
});

// Generate DDL
const exporter = new exporters.MySQLExporter();
const ddl = exporter.export('users', userSchema);

console.log(ddl);

// Generate index
console.log(exporter.generateIndex('users', 'email', { unique: true }));
console.log(exporter.generateIndex('users', 'username', { unique: true }));
console.log(exporter.generateIndex('users', 'status'));

Output:

CREATE TABLE `users` (
  `id` VARCHAR(255) NOT NULL,
  `username` VARCHAR(32) NOT NULL COMMENT 'User login name',
  `email` VARCHAR(255) NOT NULL COMMENT 'User Email',
  `password` VARCHAR(64) NOT NULL,
  `age` DOUBLE NULL,
  `status` VARCHAR(255) NULL,
  `createdAt` DATETIME NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE UNIQUE INDEX `idx_users_email` ON `users` (`email`);
CREATE UNIQUE INDEX `idx_users_username` ON `users` (`username`);
CREATE INDEX `idx_users_status` ON `users` (`status`);

type mapping

JSON Schema typeformat/constraintsMySQL type
string-VARCHAR(255)
stringmaxLength: 50VARCHAR(50)
stringmaxLength: 500TEXT
stringformat: emailVARCHAR(255)
stringformat: date-timeDATETIME
integermaximum: 127TINYINT
integermaximum: 32767SMALLINT
integermaximum: 2147483647INT
integer-BIGINT
number-DOUBLE
boolean-BOOLEAN
object-JSON
array-JSON

constraint mapping

constraintMySQL processing
requiredNOT NULL
Not requiredNULL
defaultDEFAULT value
descriptionCOMMENT 'text'

Primary key detection

The exporter automatically detects the following fields as primary keys:

  1. Field named id
  2. Field named _id

If these fields exist, PRIMARY KEY constraints are automatically added.


Export restrictions

⚠️ IMPORTANT: MySQL has limited support for constraints.

Features not supported by MySQL:

  • ❌ Regular expression constraints (pattern)
  • ❌ Numerical range constraints (minimum/maximum)
  • ❌ Enumeration CHECK constraints (exported as normal VARCHAR)
  • ❌ Minimum string length (minLength)
  • ❌ Conditional validation logic (s.match(), s.if())

Detailed instructions: Please read Export restrictions document



Corresponding sample file

Example entry: mysql-exporter.ts Description: Overrides export() generation of DDL, primary key detection, and generateIndex() generation of normal/unique indexes.