LinuxCommandLibrary

sequelize

Manage database migrations with Sequelize

TLDR

Create a model with 3 fields and a migration file

$ sequelize model:generate --name [table_name] --attributes [field1:integer,field2:string,field3:boolean]
copy

Run the migration file
$ sequelize db:migrate
copy

Revert all migrations
$ sequelize db:migrate:undo:all
copy

Create a seed file with the specified name to populate the database
$ sequelize seed:generate --name [seed_filename]
copy

Populate database using all seed files
$ sequelize db:seed:all
copy

SYNOPSIS

sequelize [command] [options]

PARAMETERS

init
    Initializes a new Sequelize project structure, creating default configuration files, models, migrations, and seeders directories.

db:migrate
    Runs all pending migrations that have not yet been applied to the database, bringing the schema up to date.

db:migrate:undo
    Undoes the last applied migration, rolling back the database schema change.

db:migrate:undo:all
    Undoes all applied migrations, effectively resetting the database schema to its initial state.

db:seed
    Runs all pending seeders, populating the database with initial or sample data.

db:seed:undo
    Undoes the last applied seeder.

model:generate --name=name --attributes=attrs
    Generates a new model file and an associated migration file. name is the model's name (e.g., User), and attrs define its attributes (e.g., firstName:string,lastName:string,age:integer).

migration:generate --name=name
    Generates a new empty migration file with the specified name (e.g., add-user-column).

seed:generate --name=name
    Generates a new empty seeder file with the specified name (e.g., initial-users-seeder).

--help, -h
    Displays help information for the main command or a specific subcommand.

--version, -v
    Displays the installed version of the Sequelize CLI.

--env, -e environment
    Specifies the database environment (e.g., development, test, production) to use from the configuration file. Defaults to 'development'.

--config, -c path
    Specifies the path to the Sequelize configuration file (e.g., 'config/config.json').

--migrations-path, -i path
    Specifies the path to the directory containing migration files (default: 'migrations').

--models-path, -m path
    Specifies the path to the directory containing model files (default: 'models').

--seeders-path, -s path
    Specifies the path to the directory containing seeder files (default: 'seeders').

DESCRIPTION

The sequelize command-line interface (CLI) is a powerful tool designed to manage and interact with database schemas and data when using the Sequelize ORM for Node.js. It simplifies common database operations such as creating and running migrations, generating models and seeders, and initializing new Sequelize projects.

Unlike traditional Linux commands, sequelize is not a core operating system utility but a JavaScript package installed via npm or yarn within a Node.js development environment. It acts as an orchestrator for managing your application's database state, allowing developers to define database schema changes in a version-controlled manner and apply them across different environments consistently. Its primary use cases revolve around database setup, evolution, and data seeding for development, testing, and production workflows.

CAVEATS

The sequelize command is not a native Linux utility. It requires Node.js and a package manager like npm or yarn to be installed. It is typically run within a Node.js project directory that has sequelize-cli listed as a dependency. Its functionality depends on the project's configuration files (e.g., config/config.json) and the specific version of Sequelize ORM being used.

USAGE WITH NPX

Since sequelize-cli is often installed as a local project dependency, it's common practice to invoke it using npx (Node Package Execute), for example: npx sequelize db:migrate. This ensures that the version of the CLI installed in the current project's node_modules is used, avoiding global installation conflicts.

CONFIGURATION FILES

The behavior of the sequelize command is heavily reliant on configuration files, typically located in a config/ directory. The primary file is config.json (or config.js), which defines database connection details for different environments (development, test, production). Other settings like paths to models, migrations, and seeders can also be configured here or overridden via command-line options.

HISTORY

Sequelize itself, a promise-based Node.js ORM for Postgres, MySQL, SQL Server, SQLite and MariaDB, was first released in 2010. The sequelize-cli emerged as a separate package to provide a robust command-line interface, simplifying common development tasks like database migrations and model scaffolding. This separation allowed the ORM and its CLI to evolve independently, catering to different aspects of database management in Node.js applications.

SEE ALSO

npm(1), node(1), yarn(1)

Copied to clipboard