Skip to content

Adding a Table

While you can certainly write your own create table statements and execute them on the database, there is a more modern, easier, and scalable way to manage your database evolutions. StarterStack uses Sequelize database ORM which also include database migrations via the sequelize-cli.

The sequelize-cli has many utility commands, including the ability to create and run a database migration. In this document, we will walk through the steps to create a new table Products

TIP

These commands need to be run from inside the backend docker container

Accessing the Backend Docker Container

Run the following command to access the container.

sh
docker exec -it {container name}-api /bin/ash

The {container name} is the string you used for your project name when you installed StarterStack. If you want to see what containers are running you can run the following command:

sh
docker ps

Generate the Migration File

Next, run the following command to generate a new table and the Sequelize model class to connect and query that table:

sh
npx sequelize-cli model:generate --name Products --attributes product_name:string,description:string

The output from this command will show you the migration and model files it created at /backend/migrations and /backend/models.

Creating the Table

In the previous step we only created the migration file which is a definition of the table structure. In this step we need to run migrations to use this file to create the table.

Run this command:

sh
npx sequelize-cli db:migrate

Your Products table is now created.

You can read more about migrations in the Sequelize docs