CruxDevCruxDev Tools Prompts
dev Utility • Zero-Server Privacy • 100% Client-Side

SQL Schema to TypeScript & Prisma

Convert SQL CREATE TABLE DDL schemas into TypeScript interfaces, Prisma models, and Knex migrations.

Overview

Turn a SQL CREATE TABLE statement into a TypeScript interface, a Prisma schema model, or a Knex migration. Column types are mapped from SQL data types, nullability becomes optional fields, and table and column names are converted to idiomatic casing.

How It Works

Paste one CREATE TABLE statement into the left editor and pick the target format with the three buttons: TypeScript interface, Prisma model, or Knex migration. The generated code appears immediately and updates as you edit. Constraint-only lines such as PRIMARY KEY or FOREIGN KEY declarations are skipped, and columns marked NOT NULL come out required while nullable columns become optional fields.

Step-by-Step Usage Guide

  1. Paste a single CREATE TABLE statement into the editor.
  2. Choose TypeScript interface, Prisma model, or Knex migration as the target.
  3. Review the output and fix any fields the parser could not classify.
  4. Copy the generated code into your project and restore dropped constraints.

Technical Specifications & Standards

The parser matches a single CREATE TABLE, optionally with IF NOT EXISTS, splits the column list on commas, and ignores lines that start with constraint keywords. Type mapping for TypeScript follows SQL families: integer and decimal types become number, boolean stays boolean, timestamp, date, and time map to the union Date | string, JSON columns become Record<string, any>, and everything else, including UUID and varchar, becomes string. Names are converted for idiomatic output: columns become camelCase and the table name becomes a PascalCase type. Three honest limitations to plan around. Splitting on commas is naive, so a default value containing a comma breaks the parse. The Prisma output maps SERIAL to Int without an autoincrement annotation and drops DEFAULT and UNIQUE attributes, so constraints must be re-added. The Knex output, unlike the other two, types every column as table.string() regardless of the SQL type, so you will want to replace the builder calls with the correct typed methods before running that migration.

Targeted Use Cases

  • Creating a TypeScript model to match an existing database table.
  • Bootstrapping a Prisma schema from a legacy database's DDL.
  • Starting a Knex migration file from a table definition the team already agreed on.
  • Checking how a schema's nullability translates into optional TypeScript fields.

Notes & Gotchas

  • Feed one table at a time; the parser handles a single CREATE TABLE statement.
  • Remove or simplify default values containing commas before pasting.
  • Re-add DEFAULT, UNIQUE, and relation attributes in Prisma by hand; the mapper emits plain types.
  • Replace the Knex output's string columns with the correct typed builder calls for non-text SQL types.

Frequently Asked Questions

Why is my UUID column a string?

TypeScript has no native UUID type, so UUID columns map to string, the same choice most ORMs make at the boundary. If you use a branded UUID type, replace the annotation after generating.

Why does the Knex migration make every column a string?

The Knex template emits table.string() for all columns as a scaffold. Unlike the TypeScript and Prisma outputs, it does not map SQL types to builder methods, so edit each column call to integer, boolean, timestamp, and so on.

Can I convert several tables at once?

No. The parser accepts one CREATE TABLE statement. Run the tool once per table, which also keeps each generated model reviewable on its own.

What happened to my DEFAULT and UNIQUE constraints?

The generator maps columns and nullability only. Constraint metadata is dropped in all three outputs, most visibly in Prisma, where you should add @default, @unique, and @id attributes manually.