npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@icetype/mysql

v0.3.0

Published

IceType adapter for MySQL - generates DDL from IceType schemas

Downloads

126

Readme

@icetype/mysql

MySQL adapter for IceType schema transformations. This package generates MySQL DDL (Data Definition Language) statements from IceType schemas.

Installation

npm install @icetype/mysql
# or
pnpm add @icetype/mysql

Usage

import { parseSchema } from '@icetype/core';
import { MySQLAdapter, transformToMySQLDDL } from '@icetype/mysql';

// Parse an IceType schema
const schema = parseSchema({
  $type: 'User',
  id: 'uuid!',
  email: 'string#',
  name: 'string',
  age: 'int?',
  balance: 'decimal',
  createdAt: 'timestamp',
});

// Option 1: Use the adapter directly
const adapter = new MySQLAdapter();
const ddl = adapter.transform(schema, { ifNotExists: true });
const sql = adapter.serialize(ddl);

// Option 2: Use the convenience function
const sql2 = transformToMySQLDDL(schema, {
  ifNotExists: true,
  charset: 'utf8mb4',
  collation: 'utf8mb4_unicode_ci',
});

console.log(sql2);
// CREATE TABLE IF NOT EXISTS User (
//   `$id` VARCHAR(255) NOT NULL,
//   `$type` VARCHAR(255) NOT NULL,
//   `$version` INT NOT NULL DEFAULT 1,
//   `$createdAt` BIGINT NOT NULL,
//   `$updatedAt` BIGINT NOT NULL,
//   id CHAR(36) NOT NULL,
//   email VARCHAR(255) UNIQUE,
//   name VARCHAR(255),
//   age INT,
//   balance DECIMAL(38, 9),
//   createdAt DATETIME,
//   PRIMARY KEY (`$id`),
//   UNIQUE (email)
// ) ENGINE=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

API

Main Exports

| Export | Description | |--------|-------------| | MySQLAdapter | Adapter class for MySQL DDL generation | | createMySQLAdapter() | Factory function to create adapter | | transformToMySQLDDL(schema, options) | Transform schema to DDL string | | generateMySQLDDL(schema, options) | Generate DDL object |

DDL Helpers

| Export | Description | |--------|-------------| | mapIceTypeToMySQL(type) | Map IceType to MySQL type | | getMySQLTypeString(mapping) | Get MySQL type as string | | fieldToMySQLColumn(field) | Convert field to column definition | | serializeDDL(ddl) | Serialize DDL object to SQL string | | generateIndexStatements(table, columns) | Generate CREATE INDEX statements | | escapeIdentifier(name) | Escape identifier for MySQL (backticks) | | formatDefaultValue(value, type) | Format default value for MySQL |

Migration Exports

| Export | Description | |--------|-------------| | MySQLMigrationGenerator | Class for generating migration SQL | | createMySQLMigrationGenerator() | Factory function for migrations |

Types

| Type | Description | |------|-------------| | MySQLType | MySQL data types | | MySQLColumn | Column definition for MySQL | | MySQLDDL | Complete DDL structure | | MySQLAdapterOptions | Options for DDL generation |

Examples

Basic Table Generation

import { parseSchema } from '@icetype/core';
import { transformToMySQLDDL } from '@icetype/mysql';

const schema = parseSchema({
  $type: 'Product',
  id: 'uuid!',
  name: 'string!',
  price: 'decimal(10,2)!',
  stock: 'int!',
  description: 'text?',
});

const sql = transformToMySQLDDL(schema, {
  engine: 'InnoDB',
  charset: 'utf8mb4',
});

With Engine and Character Set

const sql = transformToMySQLDDL(schema, {
  engine: 'InnoDB',
  charset: 'utf8mb4',
  collation: 'utf8mb4_unicode_ci',
  ifNotExists: true,
});

Generate Multiple Tables

import { MySQLAdapter } from '@icetype/mysql';

const adapter = new MySQLAdapter();
const schemas = [userSchema, postSchema, commentSchema];

const ddlStatements = schemas.map(schema => {
  const ddl = adapter.transform(schema, {
    engine: 'InnoDB',
    charset: 'utf8mb4',
  });
  return adapter.serialize(ddl);
});

const fullSQL = ddlStatements.join('\n\n');

Using with Adapter Registry

import { createAdapterRegistry } from '@icetype/adapters';
import { MySQLAdapter } from '@icetype/mysql';

const registry = createAdapterRegistry();
registry.register(new MySQLAdapter());

const adapter = registry.get('mysql');
const ddl = adapter?.transform(schema, { engine: 'InnoDB' });
const sql = adapter?.serialize(ddl);

Generate Migrations

import { diffSchemas, parseSchema } from '@icetype/core';
import { createMySQLMigrationGenerator } from '@icetype/mysql';

const oldSchema = parseSchema({
  $type: 'User',
  id: 'uuid!',
  name: 'string!',
});

const newSchema = parseSchema({
  $type: 'User',
  id: 'uuid!',
  name: 'string!',
  email: 'string!',
  createdAt: 'timestamp!',
});

const diff = diffSchemas(oldSchema, newSchema);
const generator = createMySQLMigrationGenerator();
const statements = generator.generate(diff);

console.log(statements);
// ['ALTER TABLE `User` ADD COLUMN `email` VARCHAR(255) NOT NULL;',
//  'ALTER TABLE `User` ADD COLUMN `createdAt` DATETIME NOT NULL;']

Generate Index Statements

import { generateIndexStatements } from '@icetype/mysql';

const indexes = generateIndexStatements('User', undefined, [
  { name: 'email', type: 'VARCHAR(255)', nullable: false, unique: true },
  { name: 'createdAt', type: 'DATETIME', nullable: false },
]);

Text Fields with Length

const schema = parseSchema({
  $type: 'Article',
  id: 'uuid!',
  title: 'string!',        // VARCHAR(255)
  summary: 'text!',        // TEXT
  content: 'text!',        // TEXT
});

Type Mappings

| IceType | MySQL Type | Notes | |---------|------------|-------| | string | VARCHAR(255) | Default length 255 | | text | TEXT | For longer strings | | int | INT | | | long | BIGINT | | | bigint | BIGINT | | | float | FLOAT | | | double | DOUBLE | | | boolean | TINYINT(1) | 0/1 values | | uuid | CHAR(36) | Fixed-length string | | timestamp | DATETIME | | | date | DATE | | | time | TIME | | | binary | BLOB | | | json | JSON | MySQL 5.7+ native JSON | | decimal(p,s) | DECIMAL(p,s) | |

MySQL-Specific Notes

  • No native UUID type: UUIDs stored as CHAR(36)
  • No native arrays: Array types stored as JSON
  • VARCHAR limit: Default 255, use TEXT for longer strings
  • Engine selection: InnoDB recommended for transactions and foreign keys
  • Character set: Use utf8mb4 for full Unicode support
  • Collation: utf8mb4_unicode_ci recommended for case-insensitive sorting

Documentation

For full documentation, visit the IceType Documentation.

Related Packages

License

MIT