The Sequelize Migration Generator CLI is a command-line tool designed to streamline the process of creating migration files for Sequelize-based databases. This tool helps developers manage database schema changes effectively and maintain version control for database structures.
Currently CLI supports createTable
, removeTable
, addColumn
, modifyColumn
, removeColumn
migrations.
Please note that the Sequelize Migration Generator CLI is currently in development mode and should not be used in a production environment.
This tool is actively being developed and may undergo significant changes, including features, code structure, and APIs. While we encourage you to explore and test it, we strongly advise against using it in a production setting at this time.
Feel free to provide feedback, report issues, or contribute to the development efforts. Your input will be valuable in shaping the tool's stability and usability as it progresses.
Thank you for your understanding and patience as we work towards making this CLI production-ready.
Using npm:
npm i sequelize-mig-generator --save-dev
Using yarn:
yarn add sequelize-mig-generator -D
Using pnpm:
pnpm i sequelize-mig-generator -D
-
Init sequelize-cli
npx sequelize-cli init
This will create following folders
- config, contains config file, which tells CLI how to connect with database
- models, contains all models for your project
- migrations, contains all migration files
- seeders, contains all seed files
read more about sequelize migrations and sequelize-cli from the official sequelize docs
-
Use sequelize-mig-generator cli
add
make:migration
script to yourpackage.json
{ ... "scripts": { ... "make:migrations": "sequelize-mig-generator -s ./models/init.js", "run:migrations": "sequelize-cli db:migrate" } }
sequelize-mig-generator only needs 2 things to know:
- sequelize path - path to sequelize init script, should have a default exported function that return a promise with the updated sequelize object. (check the example for more info.)
- migrations folder - tell the cli where to save the generated migration files.
Both of them are taken automatically from
.sequelizerc
file in case they are not set manually.npx sequelize-mig-generator -h Usage: sequelize-mig-generator [options] Streamline the management of Sequelize database schema changes with a CLI tool for generating migration files effortlessly. Options: -V, --version output the version number -r, --rc-path <path> Path for your .sequelizerc file (default: ".sequelizerc") -e, --extension <extension> Extensiton for your migration files (default: ".js") -s, --sequelize-path [path] Path for init sequelize, models and associations (default: [models-path] key from sequelize rc file) -m, --migrations-path [path] Folder to save the generated migrations (default: [migrations-path] key from sequelize rc file) -h, --help display help for command
Currently sequelize-path
can only handle js
file. So it recomended to compile your ts files and run the cli after.
example:
{
...
"scripts": {
...
"compile": "tsc",
"premake:migrations": "yarn compile",
"make:migrations": "sequelize-mig-generator -s ./dist/db/models/init.js -m ./db/migrations",
"run:migrations": "sequelize-cli db:migrate"
}
}
Check out our example contains an express server with sequelize on sqlite with migrations generated by the tool.