SQL to Exposed Table Generator

Convert SQL CREATE TABLE statements to Kotlin Exposed ORM table object definitions with proper type mapping, references, and constraints.

What is SQL to Exposed Table Generator?

Exposed is JetBrains' lightweight SQL library for Kotlin, offering both a typesafe DSL for building queries and an optional DAO layer for object-relational mapping. Unlike heavy ORMs, Exposed stays close to SQL while providing Kotlin-fluent API and compile-time type checking. This converter takes your SQL CREATE TABLE statements and generates Kotlin Exposed Table object definitions with proper column types, reference() mappings for foreign keys, and optional Entity classes using the DAO pattern with IntEntity and IntEntityClass.

How to Use

  1. Select your database dialect — PostgreSQL, MySQL, or SQLite
  2. Paste your SQL CREATE TABLE statements into the input area
  3. Toggle "Include Entity classes" to generate IntEntity subclasses with typed property delegates
  4. Toggle "Include DAO pattern" to add IntEntityClass companion objects for each entity
  5. Click "Generate Exposed Tables" and copy the Kotlin code into your project

Why Use This Tool?

Generates type-safe Exposed Table object definitions with proper column types like integer, varchar, text, bool, timestamp, uuid
Foreign key references are converted to reference() column definitions that maintain referential integrity in the DSL
SERIAL and auto-increment columns automatically receive the autoIncrement() modifier
Optional Entity classes provide an ORM-style layer with typed property access via delegates
Nullable columns get .nullable() modifier, matching Exposed conventions for optional database fields

Tips & Best Practices

  • Use the DSL approach (Table objects only) for maximum control over your SQL queries and performance
  • Enable Entity classes when you prefer object-oriented data access with change tracking and lazy loading
  • Foreign key references use reference() which creates both the column and the relationship in a single definition
  • Add the Exposed dependency with your chosen dialect module: org.jetbrains.exposed:exposed-core and exposed-dao if using entities

Frequently Asked Questions

How are SQL types mapped to Exposed column types?

SQL types map to Exposed column types: INTEGER/SERIAL become integer, BIGINT/BIGSERIAL become long, SMALLINT becomes short, TINYINT becomes byte, VARCHAR becomes varchar(length), TEXT becomes text, BOOLEAN becomes bool, TIMESTAMP becomes timestamp, DATE becomes date, UUID becomes uuid, JSON/JSONB become text, BYTEA/BLOB become blob. Nullable columns get .nullable() modifier.

When should I avoid using Exposed?

Avoid Exposed if you need a full-featured ORM with automatic migration generation (consider Ktorm or Hibernate instead), if you prefer writing raw SQL strings, or if your project uses a JVM language other than Kotlin since Exposed relies heavily on Kotlin DSL features and extension functions.

What is the difference between DSL and DAO approaches?

The DSL approach uses Table objects to build type-safe SQL queries directly. The DAO approach adds Entity classes on top, providing an object-oriented layer with features like lazy loading, change tracking, and entity references. DSL gives more control; DAO provides convenience.

How are foreign keys handled?

Foreign key REFERENCES constraints are converted to Exposed reference() column definitions. This creates a typed relationship between table objects that Exposed uses for join generation and referential integrity. The reference points to the primary key of the referenced table.

Is my SQL data sent to a server?

No. All parsing and code generation runs entirely in your browser. Your SQL schema is never transmitted over the network, and no external services are contacted during the conversion process.

Real-world Examples

Ktor API with Exposed DSL

A Kotlin developer building a Ktor REST API pastes their PostgreSQL schema into this tool, generates Exposed Table objects, and uses them to build type-safe queries in their route handlers. The DSL approach gives them full SQL control with Kotlin type safety.

Input
CREATE TABLE invoices (
  id SERIAL PRIMARY KEY,
  customer_id INTEGER NOT NULL REFERENCES users(id),
  amount DECIMAL(10,2) NOT NULL,
  status VARCHAR(20) DEFAULT 'pending',
  issued_at TIMESTAMP DEFAULT NOW()
);
Output
object Invoices : Table("invoices") {
    val id = integer("id").autoIncrement()
    val customerId = reference("customer_id", Users.id)
    val amount = decimal("amount", 10, 2)
    val status = varchar("status", 20).default("pending")
    val issuedAt = timestamp("issued_at").default(NOW())
}

DAO pattern with Entity classes

A team prefers the DAO pattern for their Spring Boot application. They enable both Entity class and DAO pattern options, getting IntEntity subclasses with companion objects that provide findById, all, and other convenience methods.

Related Tools