This is a template for NestJs, which contains Sequelize with postgres Database, sequelize migration, graphQl and Swagger API Documentation.
Open a new terminal and enter the following command npx sequelize-cli migration:generate --name <migration-name>
then press Enter.
A migration file will be created inside the database/migration folder.
Example:
npx sequelize-cli migration:generate --name add-firstname
This command will create a migration file inside the database/migration folder with the name 20220415042414-add-firstname with the timestamp at the front.
For more Migration related doubts go to the Sequelize Migration page.
-
To run the migration, open a new terminal and enter the command
npm run migrate status
first to check is there any pending migrations that should be run or not. -
Running this command will give you a object with keys ex (executed) and pd (pending).
-
If there is any Migrations pending enter the following command to migrate
npm run migrate up
. -
to revert all the migrations use
npm run migrate down
. This will revert all the migration that was ever done. -
To run more than one migration at a time use the
npm run migrate next
, this will run all the migrations. -
If there was any error in the previous migration you can undo it by
npm run migration prev
ornpm run migrate reset-prev
. This will revert only the last migration. -
npm run migrate reset-hard
, this will drops and creates a new table with the same name.
In this I'm following the code first method of nestJs-graphQl. So the Schema.gql file will automatically get created.