Sequelize CLI

6.6.5 · active · verified Wed Apr 22

The Sequelize CLI is the official command-line interface for the Sequelize ORM, providing essential tools for managing Sequelize projects. It is currently stable at version 6.6.5, with minor releases and bug fixes occurring every few months, demonstrating active maintenance. The CLI facilitates common database operations such as creating and dropping databases, managing schema migrations (applying, reverting, checking status), generating and running seed files for initial data population, and scaffolding new models, migrations, and seeders. Its key differentiator is its tight integration with the Sequelize ORM, making it the de facto tool for many Sequelize workflows, especially for projects utilizing its migration system. It supports both CommonJS and ESM configuration files, and since v6.2.0, allows for TypeScript migration files, adapting to modern JavaScript ecosystems.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up a new Sequelize project using sequelize-cli, including initialization, database creation, model and migration generation, running migrations, and seeding initial data.

npm install --save-dev sequelize-cli sequelize mysql2

# Initialize a new Sequelize project structure
npx sequelize init

# Adjust config/config.js to your database settings. For example (using environment variables):
# module.exports = {
#   development: {
#     username: process.env.DB_USER ?? 'root',
#     password: process.env.DB_PASSWORD ?? '',
#     database: process.env.DB_NAME ?? 'my_sequelize_db_dev',
#     host: process.env.DB_HOST ?? '127.0.0.1',
#     dialect: 'mysql'
#   }
# };

# Create the database specified in your configuration
npx sequelize db:create

# Generate a new model and its corresponding migration file
npx sequelize model:generate --name User --attributes firstName:string,lastName:string,email:string

# Run pending migrations to apply schema changes to the database
npx sequelize db:migrate

# Generate a seeder file for initial data population
npx sequelize seed:generate --name initial-users

# Edit the generated seeder file (e.g., seeders/<timestamp>-initial-users.js) to insert data:
# module.exports = {
#   up: async (queryInterface, Sequelize) => {
#     await queryInterface.bulkInsert('Users', [{
#       firstName: 'Jane', lastName: 'Doe', email: 'jane.doe@example.com',
#       createdAt: new Date(), updatedAt: new Date()
#     }], {});
#   },
#   down: async (queryInterface, Sequelize) => {
#     await queryInterface.bulkDelete('Users', null, {});
#   }
# };

# Run all seeders to populate the database with initial data
npx sequelize db:seed:all

view raw JSON →