Database
For this project, we will be using Postgres as a database and the ORM Sequelize. They are amongst the most popular. But you can find many other viable alternatives.
Run a local database
If you have installed docker, you can simply run the following command:
$ docker run -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=bookapp -p 5432:5432 postgresOtherwise, you need to download and install it from the official website.
Find a GUI to interact with your database. Have a look at TablePlus (Free or $59) or PgAdmin (Open Source).
We should now be able to connect to our database:
Host: 127.0.0.1
Port: 5432
User: postgres
Password: postgres
Database: bookappUpdate the config and .env
Models
Update our dependencies
You'll see the following message:
Fix it by installing the required dependencies:
Let's create a folder models to keep our files organised and create aLink model.
Create our Sequelize instance
and sync our model when we bootstrap the app
We should now see the link table in our database (try to refresh if you don't).

Migrations
Let's try to rename url to uriin our Link model and run the app again.
If we look in your UI, nothing changed.
This is because of CREATE TABLE IF NOT EXISTS. The table already exists and the statement gets ignored.
One way of solving this issue is to force sequelize to recreate all the tables with
Usingforce: truewill remove the existing data. Use it only for development.
This is fine while we are building database and we don't have any data in production. However, how do we deal with this once we're live? We write migrations
Umzung
Umzung is a framework agnostic migration tool for Node.js. We create migration files and umzug will run them one after the other. It will also store the last migration in a new table.
We will store our files in a new migrations folder and create our migration 001_rename_url_to_uri.ts.
We need a file to configure the migration
And run the migration when we start the app
Run the app
Two things happened:
urlhas been renamed touri.A new table
SequelizeMetahas been created.


Last updated
Was this helpful?