The Sequelize model you create using the sequelize.define() method comes with a sync() method that you can use to create a table.
The table created using the sync() method will follow the model definition for its column(s).For example, suppose you have the following User model defined in your code:
const User = sequelize.define("User", {
firstName: {
type: Sequelize.STRING,
},
isActive: {
type: Sequelize.BOOLEAN,
defaultValue: false,
},
});
await User.sync();
The call to User.sync() above will cause Sequelize to synchronize the model with the database.
The method will create the database table if it doesn’t already exist.
By default, Sequelize will include the createdAt and updatedAt columns to keep track of when you insert or update a row with Sequelize.
If you don’t want the createdAt and updatedAt columns, you can add the timestamps: false option to your model definition as shown below:
const User = sequelize.define("User", {
firstName: {
type: Sequelize.STRING,
},
isActive: {
type: Sequelize.BOOLEAN,
defaultValue: false,
},
},
{
timestamps: false,
});
sync() method options
sync() method options
The sync() method also accepts two options as described below:
sync({ force: true }) – This option force Sequelize to create a table, dropping it first if it already existed
sync({ alter: true }) – This option alters the columns and data types of an existing table to match the model