sequelize.define("ModelName", { fields }, { options });
const User = sequelize.define("User", {
name: DataTypes.STRING,
}, {
tableName: "users",
timestamps: true,
});
"User" → Model name (used in JS)tableName → SQL table nameEvery column follows:
fieldName: {
type: DataTypes.<TYPE>,
allowNull: true/false,
unique: true/false,
defaultValue: <VALUE>,
validate: { ...validators }
}
email: {
type: DataTypes.STRING(100),
allowNull: false,
unique: true,
validate: {
isEmail: true, // built-in validator
}
}
| Sequelize | SQL Equivalent | Use Case |
|---|---|---|
STRING(255) |
VARCHAR(255) | Names, Text |
TEXT |
TEXT | Long text (posts, descriptions) |
INTEGER |
INT | Count values |
BOOLEAN |
BOOLEAN | true/false |
UUID |
UUID | Primary keys |
DATE |
TIMESTAMP | createdAt, updatedAt |
JSON |
JSON | Nested metadata |
ENUM("A","B") |
ENUM | Controlled value sets |
role: {
type: DataTypes.ENUM("user", "admin", "moderator"),
defaultValue: "user",
}