Cheat Sheet for SQLite3 CLI Commands: Sequelize Command Sheet

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).

Initialize Sequelize

Sets up the basic Sequelize structure (config, models, migrations, seeders) in your project.

npx sequelize-cli init

Add a Migration File & Model

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>

Add a Blank Migration File

Creates an empty migration file without generating a model.

npx sequelize migration:generate --name <name-of-migration-file>

Run Migration Files

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

Undo All Migrations

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.

Add a Blank Seeder File

Creates an empty seeder file, which you can manually populate to insert seed data.

npx sequelize seed:generate --name <name-of-seed-file>

Commit All Seeder Files

Executes the up method in every pending seeder, inserting initial data into your tables.

npx dotenv sequelize db:seed:all

Undo All Seeder Files

Rolls back every seeder that has been applied, removing the inserted data in reverse order.

npx dotenv sequelize db:seed:undo:all

Migration File Syntax Cheat Sheet

Create Table

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'>
  },
  // ...
});

Drop Table

await queryInterface.dropTable(<TableName>);

Add Column

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'>
});

Remove Column

await queryInterface.removeColumn(<TableName>, <columnName>);

Final Tips

By keeping this cheat sheet handy, you’ll save time and avoid confusion when managing SQLite3 with Sequelize. Happy coding and migrating!