erd.sketch

SQL to TypeORM: models from a DDL script

What follows is not a mock-up: it is the real output of the generator for the script on the left. You get the same code in the editor under “Codegen → TypeORM”.

The inputschema.sql
CREATE TABLE users (
    id BIGINT NOT NULL PRIMARY KEY,
    email       VARCHAR(255) NOT NULL UNIQUE,
    full_name   VARCHAR(120),
    is_active   BOOLEAN NOT NULL,
    created_at  TIMESTAMP NOT NULL
);

CREATE TABLE orders (
    id BIGINT NOT NULL PRIMARY KEY,
    user_id     BIGINT NOT NULL REFERENCES users(id),
    total       DECIMAL(12,2) NOT NULL,
    note        TEXT,
    placed_at   TIMESTAMP NOT NULL
);
What comes outentities.ts
import { Column, Entity, JoinColumn, ManyToOne, PrimaryColumn } from "typeorm";

@Entity("users")
export class Users {
  @PrimaryColumn({ type: "bigint" })
  id!: string;

  @Column({ type: "varchar", length: 255, unique: true })
  email!: string;

  @Column({ type: "varchar", length: 120, nullable: true, name: "full_name" })
  fullName!: string | null;

  @Column({ type: "boolean", name: "is_active" })
  isActive!: boolean;

  @Column({ type: "timestamp", name: "created_at" })
  createdAt!: Date;
}

@Entity("orders")
export class Orders {
  @PrimaryColumn({ type: "bigint" })
  id!: string;

  @Column({ type: "bigint", name: "user_id" })
  userId!: string;

  @Column({ type: "decimal", precision: 12, scale: 2 })
  total!: number;

  @Column({ type: "text", nullable: true })
  note!: string | null;

  @Column({ type: "timestamp", name: "placed_at" })
  placedAt!: Date;

  @ManyToOne(() => Users)
  @JoinColumn({ name: "user_id" })
  users!: Users;
}
Open the editor

The script stays in the browser: both parsing and generation run on your machine, nothing goes to a server.

How the types land

in SQLin the model
BIGINTstring
VARCHAR(255)string
BOOLEANboolean
TIMESTAMPDate
DECIMAL(12,2)number
TEXTstring | null
Optional field
nullable: true
Column name
name: "full_name"
Relation
@ManyToOne(() => Users)