Convert Sequelize (TS) models to JSON!

Input Sequelize (TypeScript) models to generate example JSON! Handy for API tests and mock request bodies.
Sequelize (TS)
Result
 

Convert Sequelize (TS) models to JSON!

Feed it @Table model classes written with sequelize-typescript and it builds example JSON shaped like the instance's toJSON() output. That includes the id, createdAt, updatedAt and deletedAt attributes Sequelize adds even when they are not declared in the class.

Unlike the JS page, values here come from each property's TypeScript type rather than the DataType argument. @Column(DataType.DECIMAL) declared as amount: string produces a string.

How to use

  1. Paste a file containing @Table classes into the Sequelize (TS) box. Add associated models (via @HasMany and the like) to get nested values.
  2. Check the Result box for the JSON with the auto-added attributes.
  3. Toggle @Table options such as timestamps or paranoid to see right away how the date fields change.

Example

Input (sequelize-typescript)
@Table({ tableName: "invoices", paranoid: true })
export class Invoice extends Model {
  @Column(DataType.STRING(20))
  number: string;

  @Column(DataType.DECIMAL(10, 2))
  amount: string;

  @Column(DataType.ENUM("draft", "sent", "paid"))
  status: "draft" | "sent" | "paid";

  @ForeignKey(() => Customer)
  @Column
  customerId: number;
}
Output (JSON)
{
  "id": 0,
  "number": "",
  "amount": "",
  "status": "draft",
  "customerId": 0,
  "createdAt": "1970-01-01T00:00:00.000Z",
  "updatedAt": "1970-01-01T00:00:00.000Z",
  "deletedAt": null
}

The class has no primary key, so id is added first, and paranoid: true adds deletedAt: null. status takes the first literal of its union, "draft", and amount is an empty string because it is declared as string.

Supported features

  • Without @PrimaryKey, @Column({ primaryKey: true }) or an id property, id: 0 is inserted at the top, both for the root and for associated models.
  • Unless @Table sets timestamps: false, createdAt and updatedAt are appended; renames like createdAt: "created_on", updatedAt: false and paranoid: true (deletedAt: null) are honored per model.
  • Post[] from @HasMany becomes an array; @BelongsTo and @HasOne become nested objects.
  • Literal unions like "draft" | "sent" use the first value, and everything inherited through extends Model is ignored.
  • A DataType.JSON column typed as an object, e.g. { theme: string }, is filled in that shape.

Use cases

Documenting Express handlers

For endpoints that do res.json(await Invoice.findByPk(id)), the output gives a response example whose keys match reality, auto-added attributes included.

Before and after enabling soft delete

Turning on paranoid adds deletedAt to every response. Compare the output with the option on and off to tell API consumers exactly what will change.

Notes and limitations

  • Associated models nested inside the root get their own id and timestamps from their own @Table options, but foreign keys Sequelize adds implicitly (such as invoiceId on a @HasMany target without a @ForeignKey column) are not invented.
  • allowNull and column renames via @Column({ field: ... }) are ignored, since JSON keys are the property names. @Default("x") and a literal defaultValue in @Column({...}) are used as sample values.
  • underscored: true changes database column names only; toJSON() still uses attribute names, so createdAt in the output is correct.
  • Values come from TypeScript types, so a property typed only through the decorator (@Column(DataType.INTEGER) count; with no annotation) becomes null.

FAQ

Should a DECIMAL column be typed number or string?

With MySQL and PostgreSQL, Sequelize returns DECIMAL values as strings by default. Typing it amount: string keeps the type honest, and the example here will show a string too.

Can I use declare fields or CreationOptional?

Yes. declare id: CreationOptional<number> is read as number, and the same unwrapping applies to NonAttribute<...> and ForeignKey<...>. Helper types beyond these, like InferAttributes, are not expanded.

Does this page handle sequelize.define models written in JavaScript?

No. This page reads classes and TypeScript types. Use the Sequelize (JS) to JSON page for define and Model.init models, which reads DataTypes instead.