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.
Model name (default MyModel); it also becomes the upper snake case tableName in @Table.invoice.model.ts and register it with new Sequelize({ ..., models: [Invoice] }) or NestJS's SequelizeModule.forFeature([Invoice]).{
"invoice_no": "INV-2024-0042",
"amount": 1250.75,
"currency": "EUR",
"paid": false,
"due_date": "2024-07-31",
"memo": ""
}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.
{ 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.@Column states type and allowNull; nullable columns are typed string | null and so on.INTEGER/BIGINT/DOUBLE → number, BOOLEAN → boolean, DATE → Date, DATEONLY → string, JSON → Record<string, unknown> or a typed array such as string[].field: 'invoice_no' pointing at the original column; an id key in the JSON is used as the primary key rather than duplicated.@IsNotEmpty(); the others get @IsOptional().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).
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.
DataType.DECIMAL(12, 2) is safer than DataType.DOUBLE.experimentalDecorators and emitDecoratorMetadata must be enabled in tsconfig for the decorators to work.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.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.
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.
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.