SQL to TypeORM Entity
Generate a TypeORM entity class from a SQL CREATE TABLE statement.
Related Tools
Generate a Prisma schema model from a SQL CREATE TABLE statement.
Generate a Drizzle ORM schema from a SQL CREATE TABLE statement.
Generate a SQLAlchemy declarative model from a SQL CREATE TABLE statement.
Convert CSV rows into SQL INSERT statements.
Generate TypeScript interfaces or type aliases from JSON.
Generate a Sequelize model from a SQL CREATE TABLE statement.
Documentation
What is SQL to TypeORM?
SQL to TypeORM converts CREATE TABLE statements into TypeORM decorator-based entity classes — @Entity('table') on the class, @Column({ ... }) on each property, with TypeScript types alongside the database column options.
How it works
Each column gets both a database column type (for the type: option — e.g. 'varchar', 'timestamp', 'uuid') and a separate TypeScript type for the class property (string, Date, number). A primary key that's also SERIAL/AUTO_INCREMENT becomes @PrimaryGeneratedColumn(); a primary key without auto-increment (a UUID or manually-assigned ID) becomes @PrimaryColumn() instead — TypeORM treats those as genuinely different decorators, and the generator picks correctly based on the parsed autoIncrement flag. Regular columns get length, nullable, and unique options from the parsed constraints, plus a TypeScript ? marker when nullable. As elsewhere in this cluster, only literal defaults and CURRENT_TIMESTAMP/NOW() (→ default: () => 'CURRENT_TIMESTAMP') are translated into a default: option.
Features
- One
@Entityclass per table, fields renamed to camelCase - Correct choice between
@PrimaryGeneratedColumn()and@PrimaryColumn()based on auto-increment - Column
length,nullable, anduniqueoptions preserved from the SQL constraints - Literal and
CURRENT_TIMESTAMPdefaults translated; everything else left as a comment - Copy or download the generated
.tsentity file
Example
Input:
CREATE TABLE users ( id SERIAL PRIMARY KEY, external_id UUID DEFAULT uuid_generate_v4(), name VARCHAR(255) NOT NULL, is_active BOOLEAN DEFAULT true ); CREATE TABLE posts ( id SERIAL PRIMARY KEY, user_id INTEGER NOT NULL REFERENCES users(id), title VARCHAR(255) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
Output:
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity('users')
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column({ type: 'uuid', nullable: true })
externalId?: string; // SQL default not translated: uuid_generate_v4()
@Column({ type: 'varchar', length: 255 })
name: string;
@Column({ type: 'boolean', nullable: true, default: true })
isActive?: boolean;
}
@Entity('posts')
export class Post {
@PrimaryGeneratedColumn()
id: number;
@Column({ type: 'int' })
userId: number; // FK -> users.id
@Column({ type: 'varchar', length: 255 })
title: string;
@Column({ type: 'timestamp', nullable: true, default: () => 'CURRENT_TIMESTAMP' })
createdAt?: Date;
}Common errors
An unmapped DEFAULT such as uuid_generate_v4() is surfaced as // SQL default not translated: uuid_generate_v4() rather than fabricated into a default: option — add the real value yourself, e.g. default: () => 'uuid_generate_v4()' if you want the database to compute it, or generate it in application code before insert. Foreign keys show up only as // FK -> table.column comments on a plain @Column() — TypeORM's @ManyToOne/@OneToMany decorators need an import of the related entity class and a decision about the relation's property name and cardinality, none of which the raw DDL determines on its own, so they're intentionally left out.
Best practices
Search the generated file for not translated and FK -> before wiring the entities into a DataSource, then add the corresponding @ManyToOne/@JoinColumn pair for each flagged foreign key. Run typeorm-ts-node-commonjs schema:log (or your migration tool) against the entities to confirm the generated schema matches the source table before syncing.
Frequently Asked Questions
Why @PrimaryGeneratedColumn vs @PrimaryColumn?▾
A primary key that's also SERIAL/AUTO_INCREMENT becomes @PrimaryGeneratedColumn() (TypeORM's auto-incrementing key). A primary key without auto-increment (e.g. a UUID or manually-assigned ID) becomes @PrimaryColumn() instead, since TypeORM treats those as two distinct decorators.
Does it generate @ManyToOne / @OneToMany relations for foreign keys?▾
No — a foreign key column becomes a plain @Column() with a comment noting the reference. Real TypeORM relations need you to import the related entity class and decide the relation's name and cardinality, which isn't something the DDL alone determines unambiguously.
Are column lengths and nullability preserved?▾
Yes — VARCHAR(255) becomes @Column({ type: 'varchar', length: 255 }), and a nullable SQL column adds nullable: true plus a TypeScript optional (?) marker on the field.
What TypeScript type does DECIMAL map to?▾
number — TypeORM's decimal column type still round-trips through a JS number at the property level (with the DB-side precision/scale handled by the column type), matching how TypeORM's own generated entities behave.