Convert Sequelize (JS) models to JSON!

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

Convert Sequelize (JS) models to JSON!

This reads Sequelize models written in plain JavaScript, sequelize.define("User", {...}) or Model.init({...}, {...}), and turns them into example JSON. With no type annotations to go on, each attribute's value is chosen from its DataTypes entry.

It suits older Node.js servers running Express and Sequelize without TypeScript, where the model file is the only description of what an API response or seed row looks like.

How to use

  1. Paste the model file into the Sequelize (JS) box. Leftover require lines and module.exports are fine.
  2. If the file defines several models, the output is keyed by model name, e.g. { "User": {...}, "Post": {...} }.
  3. Check that the model options (the second argument) such as timestamps, paranoid or a renamed createdAt are reflected, then copy.

Example

Input (Sequelize · JavaScript)
class Reservation extends Model {}

Reservation.init(
  {
    code: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true },
    guests: DataTypes.SMALLINT,
    checkIn: DataTypes.DATEONLY,
    arrivalTime: DataTypes.TIME,
    options: DataTypes.JSONB,
    tags: DataTypes.ARRAY(DataTypes.STRING),
  },
  { sequelize, modelName: "Reservation", updatedAt: false }
);
Output (JSON)
{
  "code": "00000000-0000-0000-0000-000000000000",
  "guests": 0,
  "checkIn": "1970-01-01",
  "arrivalTime": "00:00:00",
  "options": {},
  "tags": [
    ""
  ],
  "createdAt": "1970-01-01T00:00:00.000Z"
}

code has primaryKey: true, so no id is added, and its defaultValue is DataTypes.UUIDV4, a generator rather than a literal, so the placeholder UUID is shown. updatedAt: false leaves createdAt as the only timestamp, and DATEONLY, TIME, JSONB and ARRAY(STRING) become a date string, a time string, an empty object and a one-element array.

Supported features

  • DataTypes.STRING, TEXT, CHAR → ""; INTEGER, BIGINT, FLOAT, DECIMAL → 0; BOOLEAN → false.
  • DATE gives 1970-01-01T00:00:00.000Z, DATEONLY gives 1970-01-01 and TIME gives 00:00:00.
  • UUID becomes an all-zero UUID string, ENUM("admin", "user") its first value, and JSON/JSONB an empty object.
  • ARRAY(DataTypes.STRING) follows its element type, and VIRTUAL(DataTypes.STRING) is filled from the inner type.
  • Model options timestamps: false, updatedAt: false, createdAt: "created_on" and paranoid: true add, drop or rename the date attributes.
  • Both the long form { type: ..., allowNull: ... } and the short form DataTypes.X are understood.

Use cases

Drafting seeder data

Start from the output when writing the object array for queryInterface.bulkInsert, so key names and types match the model. Remove the generated id and timestamps where the database fills them.

Reading untyped legacy code

Without TypeScript it is hard to tell what JSON a model sends just by reading it. Running each model file through here gives a quick map of the API's response shapes.

Notes and limitations

  • A literal defaultValue ("pending", 1, true) becomes the sample value. Generated defaults like DataTypes.NOW, DataTypes.UUIDV4 or sequelize.fn(...) fall back to the type's placeholder.
  • Associations like hasMany and belongsTo live outside the model definition, so they are not in the output. Add data you load with include by hand.
  • On MySQL and PostgreSQL, DECIMAL usually comes back as a string at runtime, but the example shows 0.
  • Types without an obvious sample value, such as GEOMETRY, become null.

FAQ

I set underscored: true. Why is it createdAt, not created_at?

underscored only changes database column names to snake_case. Attribute names used by toJSON() stay createdAt, so the output matches. To change the JSON key as well, name it explicitly with createdAt: "created_at".

Does it work when class User extends Model {} and User.init() are separate?

Yes. The identifier right before .init( is used as the model name, and the first argument is read as the attribute map.

Where do I convert TypeScript decorator models?

Use the Sequelize (TS) to JSON page for sequelize-typescript models with @Table and @Column. That page reads property types instead of DataType.