Converts JSON rows into a Sequelize model defined with sequelize.define(). Every column gets a DataTypes type and an allowNull flag, and the CommonJS file includes the id primary key, tableName and timestamps: false.
It fits an Express server on MySQL or PostgreSQL when you have sample rows for a table (a spreadsheet exported to JSON, an external API response) but no model definition yet.
allowNull comes out right.Model name (default MyModel); it is used both in define and as the exported variable.require('../db') returns your Sequelize instance. Adjust that path to your connection module, then save the file under models/.[
{ "email": "kim@example.com", "nickname": "kim", "age": 31, "marketing_opt_in": true, "joined_at": "2023-11-02" },
{ "email": "ana@example.com", "nickname": "ana", "age": null, "marketing_opt_in": false, "joined_at": "2024-01-15" }
]const { Sequelize, DataTypes } = require('sequelize');
const sequelize = require('../db');
const Member = sequelize.define('Member', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
email: {
type: DataTypes.STRING,
allowNull: false
},
nickname: {
type: DataTypes.STRING,
allowNull: false
},
age: {
type: DataTypes.INTEGER,
allowNull: true
},
marketingOptIn: {
type: DataTypes.BOOLEAN,
allowNull: false,
field: 'marketing_opt_in'
},
joinedAt: {
type: DataTypes.DATEONLY,
allowNull: false,
field: 'joined_at'
}
}, {
tableName: 'MEMBER',
timestamps: false
});
module.exports = Member;The second member's age is null, so only age gets allowNull: true. joined_at holds dates without a time, so it becomes DataTypes.DATEONLY, and the attribute joinedAt is tied back to the real column with field: 'joined_at'.
INTEGER (BIGINT past 2,147,483,647), decimals → DOUBLE, booleans → BOOLEAN, YYYY-MM-DD → DATEONLY, date-times → DATE, objects and arrays → JSON, other strings → STRING."01234" stays STRING.field: 'marketing_opt_in' maps the attribute to the original column name.id key in your JSON becomes the primary key instead of a second column: integers get autoIncrement, strings such as UUIDs become a plain STRING key. Without one, an auto-increment INTEGER id is added.null or missing values in any row set allowNull: true; they do not influence the column type.UserProfile gets tableName: 'USER_PROFILE' and timestamps: false, so no createdAt/updatedAt columns are expected.Export the result of SELECT ... LIMIT 5 from your DB client as JSON and paste it to get define code with the column order, nullability and snake_case column names already reflected. Handy for mapping a single table without installing sequelize-auto.
During prototyping, build a model from sample JSON, run sync() to create the table in local SQLite or MySQL, then load seed data with bulkCreate.
DataTypes.STRING defaults to VARCHAR(255); switch long text to DataTypes.TEXT.DATEONLY values come back as '2023-11-02' strings, not Date objects.DataTypes.JSON works on MySQL, MariaDB, PostgreSQL and SQLite but not MSSQL; on PostgreSQL DataTypes.JSONB is usually the better choice.tableName is upper case (MEMBER). On case-sensitive setups such as PostgreSQL or MySQL on Linux it must match the real table name exactly.DOUBLE with DataTypes.DECIMAL(12, 2) to avoid floating-point rounding.Model.init() style?This page only generates sequelize.define(). The attributes object is identical in both styles, so declare class Member extends Model {} and pass the same object to Member.init(attributes, { sequelize, tableName: 'MEMBER' }). For TypeScript, use the JSON → Sequelize (TS) page.
allowNull: false, but empty strings still get in.allowNull only blocks SQL NULL. To reject empty strings too, add validate: { notEmpty: true } to the attribute.
field on each attribute instead of underscored: true?underscored only converts camelCase to snake_case, so it cannot map keys like home-url or columns that are already camelCase. An explicit field keeps the exact JSON key as the column name whatever its style.