When working with Sequelize for Node.js, you often need to manage database migrations, models, and seed files. The sequelize-cli tool offers convenient commands to help you initialize, generate, and run migrations and seeds, among other tasks.
This cheat sheet highlights the most common commands you’ll use when dealing with SQLite3 (though many of these also apply to other SQL dialects with minimal changes).
Sets up the basic Sequelize structure (config, models, migrations, seeders) in your project.
npx sequelize-cli init
config folder (for database configs), models folder,
migrations folder, and seeders folder.Generates both a migration file (to create a SQL table) and a corresponding model file.
npx sequelize model:generate --name <NameOfModel>
--attributes <column1Name:type,column2Name:type>
<NameOfModel> – The name of the model (e.g. User).
Sequelize will use the plural of this name for the table (e.g. Users).
<column1Name:type,column2Name:type> – Comma-separated list of columns,
e.g., firstName:string,age:integer.
Creates an empty migration file without generating a model.
npx sequelize migration:generate --name <name-of-migration-file>
npx sequelize migration:generate --name add-name-column-to-people.
This generates a file where you can manually define up and down functions.
Executes all pending migrations, creating or modifying your SQLite3 database
accordingly. Use dotenv if your config depends on environment variables.
npx dotenv sequelize db:migrate
dotenv if you read environment variables (like DATABASE_URL).
Rolls back every migration that has been applied to the database, from the latest to the earliest.
npx dotenv sequelize db:migrate:undo:all
This is handy for resetting your local DB to a clean slate.
Creates an empty seeder file, which you can manually populate to insert seed data.
npx sequelize seed:generate --name <name-of-seed-file>
npx sequelize seed:generate --name demo-user-seeder
would generate a file in the seeders folder.
Executes the up method in every pending seeder, inserting initial data into your tables.
npx dotenv sequelize db:seed:all
Rolls back every seeder that has been applied, removing the inserted data in reverse order.
npx dotenv sequelize db:seed:undo:all
await queryInterface.createTable(<TableName>, {
<columnName>: {
type: Sequelize.<type>,
allowNull: <true|false>,
unique: <true|false>,
references: {
model: {
tableName: <TableName>
}
},
onDelete: <'NO ACTION'|'CASCADE'|'SET NULL'>,
onUpdate: <'NO ACTION'|'CASCADE'|'SET NULL'>
},
// ...
});
<TableName> is typically the plural version of your model name,
e.g. “Users.”
await queryInterface.dropTable(<TableName>);
await queryInterface.addColumn(<TableName>, <columnName>, {
type: Sequelize.<type>,
allowNull: <true|false>,
unique: <true|false>,
references: { model: <TableName> },
onDelete: <'NO ACTION'|'CASCADE'|'SET NULL'>,
onUpdate: <'NO ACTION'|'CASCADE'|'SET NULL'>
});
await queryInterface.removeColumn(<TableName>, <columnName>);
NODE_ENV)
are set correctly before running DB commands.
You don’t want to accidentally migrate your production DB from your local environment.
By keeping this cheat sheet handy, you’ll save time and avoid confusion when managing SQLite3 with Sequelize. Happy coding and migrating!