Convert JSON to Sequelize Schema (TypeScript)!

Input JSON to generate sequelize-typescript models (@Table, @Column, Model) with class-validator!
JSON
Result
 

Convert JSON to Sequelize Schema (TypeScript)!

Converts JSON into a decorator-based model for the sequelize-typescript library: a class extending Model with @Table and @Column({ type: DataType.*, allowNull }), TypeScript property types, and class-validator decorators for validation.

It targets sequelize-typescript (the library behind NestJS's @nestjs/sequelize) rather than plain sequelize, so the import path and the DataType spelling (no trailing s) differ from the JavaScript version.

How to use

  1. Paste a JSON object for one table row, or an array of rows.
  2. Enter the class name in Model name (default MyModel); it also becomes the upper snake case tableName in @Table.
  3. Save the result as invoice.model.ts and register it with new Sequelize({ ..., models: [Invoice] }) or NestJS's SequelizeModule.forFeature([Invoice]).

Example

Input (JSON)
{
  "invoice_no": "INV-2024-0042",
  "amount": 1250.75,
  "currency": "EUR",
  "paid": false,
  "due_date": "2024-07-31",
  "memo": ""
}
Output (sequelize-typescript)
import { Table, Column, Model, DataType } from 'sequelize-typescript';
import { IsNotEmpty, IsOptional } from 'class-validator';

@Table({ tableName: 'INVOICE', timestamps: false })
export class Invoice extends Model<Invoice> {
  @Column({ type: DataType.INTEGER, primaryKey: true, autoIncrement: true })
  id: number;

  @Column({ type: DataType.STRING, allowNull: false, field: 'invoice_no' })
  @IsNotEmpty()
  invoiceNo: string;

  @Column({ type: DataType.DOUBLE, allowNull: false })
  @IsNotEmpty()
  amount: number;

  @Column({ type: DataType.STRING, allowNull: false })
  @IsNotEmpty()
  currency: string;

  @Column({ type: DataType.BOOLEAN, allowNull: false })
  @IsNotEmpty()
  paid: boolean;

  @Column({ type: DataType.DATEONLY, allowNull: false, field: 'due_date' })
  @IsNotEmpty()
  dueDate: string;

  @Column({ type: DataType.STRING, allowNull: false })
  @IsOptional()
  memo: string;
}

memo is an empty string: it is not NULL, so the column keeps allowNull: false, but it gets @IsOptional() rather than @IsNotEmpty(). amount becomes DataType.DOUBLE, and due_date becomes DataType.DATEONLY typed string, because Sequelize returns date-only values as text.

Supported features

  • Imports { Table, Column, Model, DataType } from sequelize-typescript and declares the class as extends Model<Invoice>.
  • @Table({ tableName: 'INVOICE', timestamps: false }), matching the JavaScript generator, so no createdAt/updatedAt columns are assumed.
  • Every @Column states type and allowNull; nullable columns are typed string | null and so on.
  • Type pairs: INTEGER/BIGINT/DOUBLE → number, BOOLEAN → boolean, DATE → Date, DATEONLY → string, JSON → Record<string, unknown> or a typed array such as string[].
  • Properties are camelCase with field: 'invoice_no' pointing at the original column; an id key in the JSON is used as the primary key rather than duplicated.
  • Fields with a non-empty value in every row get @IsNotEmpty(); the others get @IsOptional().

Use cases

Models for NestJS with `@nestjs/sequelize`

When you need to persist data an external billing or settlement system sends as JSON, build the model class from that payload and use it through @InjectModel(Invoice).

Turning JavaScript Sequelize models into typed code

Export existing table data as JSON and paste it to get a typed model draft, instead of rewriting a define() object into a class by hand.

Notes and limitations

  • For money and other exact decimals, DataType.DECIMAL(12, 2) is safer than DataType.DOUBLE.
  • experimentalDecorators and emitDecoratorMetadata must be enabled in tsconfig for the decorators to work.
  • With useDefineForClassFields (the default for target ES2022 and later), plain property declarations shadow Sequelize's getters. Turn that option off or prefix the properties with declare.

FAQ

Shouldn't it be Model<InvoiceAttributes, InvoiceCreationAttributes> instead of Model<Invoice>?

sequelize-typescript v2 accepts two generics; the generated code simply passes the class itself. If you want Invoice.create({...}) type-checked without auto-generated fields like id, declare a creation-attributes interface and pass it as the second generic.

Why DataType and not DataTypes?

DataType is the name sequelize-typescript exports. Importing DataTypes from sequelize works the same, but sticking to one name per file reads better.

Are the class-validator decorators checked automatically on the model?

No. Validation only happens when you call validate(invoice) yourself or when the decorators sit on a DTO handled by NestJS's ValidationPipe. For Sequelize-level validation, use @Column({ validate: { notEmpty: true } }) or sequelize-typescript decorators such as @NotEmpty.